acp: add channel to string function

Add a function to convert the channel to a string and use that
instead of the unsafe one.

Fixes #519
This commit is contained in:
Wim Taymans 2020-12-28 13:27:34 +01:00
parent 509152108a
commit c84646b66f
3 changed files with 62 additions and 5 deletions

View file

@ -94,6 +94,47 @@ static const uint32_t channel_table[PA_CHANNEL_POSITION_MAX] = {
[PA_CHANNEL_POSITION_TOP_REAR_CENTER] = ACP_CHANNEL_TRC,
};
static const char *channel_names[] = {
[ACP_CHANNEL_UNKNOWN] = "UNK",
[ACP_CHANNEL_NA] = "NA",
[ACP_CHANNEL_MONO] = "MONO",
[ACP_CHANNEL_FL] = "FL",
[ACP_CHANNEL_FR] = "FR",
[ACP_CHANNEL_FC] = "FC",
[ACP_CHANNEL_LFE] = "LFE",
[ACP_CHANNEL_SL] = "SL",
[ACP_CHANNEL_SR] = "SR",
[ACP_CHANNEL_FLC] = "FLC",
[ACP_CHANNEL_FRC] = "FRC",
[ACP_CHANNEL_RC] = "RC",
[ACP_CHANNEL_RL] = "RL",
[ACP_CHANNEL_RR] = "RR",
[ACP_CHANNEL_TC] = "TC",
[ACP_CHANNEL_TFL] = "TFL",
[ACP_CHANNEL_TFC] = "TFC",
[ACP_CHANNEL_TFR] = "TFR",
[ACP_CHANNEL_TRL] = "TRL",
[ACP_CHANNEL_TRC] = "TRC",
[ACP_CHANNEL_TRR] = "TRR",
[ACP_CHANNEL_RLC] = "RLC",
[ACP_CHANNEL_RRC] = "RRC",
[ACP_CHANNEL_FLW] = "FLW",
[ACP_CHANNEL_FRW] = "FRW",
[ACP_CHANNEL_LFE2] = "LFE2",
[ACP_CHANNEL_FLH] = "FLH",
[ACP_CHANNEL_FCH] = "FCH",
[ACP_CHANNEL_FRH] = "FRH",
[ACP_CHANNEL_TFLC] = "TFLC",
[ACP_CHANNEL_TFRC] = "TFRC",
[ACP_CHANNEL_TSL] = "TSL",
[ACP_CHANNEL_TSR] = "TSR",
[ACP_CHANNEL_LLFE] = "LLFE",
[ACP_CHANNEL_RLFE] = "RLFE",
[ACP_CHANNEL_BC] = "BC",
[ACP_CHANNEL_BLC] = "BLC",
[ACP_CHANNEL_BRC] = "BRC",
};
#define ACP_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
static inline uint32_t channel_pa2acp(pa_channel_position_t channel)
@ -103,6 +144,19 @@ static inline uint32_t channel_pa2acp(pa_channel_position_t channel)
return channel_table[channel];
}
char *acp_channel_str(char *buf, size_t len, enum acp_channel ch)
{
if (ch >= ACP_CHANNEL_CUSTOM_START) {
snprintf(buf, len, "AUX%d", ch - ACP_CHANNEL_CUSTOM_START);
} else if (ch >= ACP_CHANNEL_UNKNOWN && ch <= ACP_CHANNEL_BRC) {
snprintf(buf, len, "%s", channel_names[ch]);
} else {
snprintf(buf, len, "UNK");
}
return buf;
}
const char *acp_available_str(enum acp_available status)
{
switch (status) {

View file

@ -101,6 +101,8 @@ enum acp_channel {
ACP_CHANNEL_CUSTOM_START = 0x10000,
};
char *acp_channel_str(char *buf, size_t len, enum acp_channel ch);
struct acp_format {
uint32_t flags;
uint32_t format_mask;

View file

@ -142,9 +142,9 @@ static int emit_node(struct impl *this, struct acp_device *dev)
struct spa_dict_item *items;
const struct acp_dict_item *it;
uint32_t n_items, i;
char device_name[128], path[180], channels[16];
char device_name[128], path[180], channels[16], ch[12];
char card_id[16], *p;
char positions[SPA_AUDIO_MAX_CHANNELS * 6];
char positions[SPA_AUDIO_MAX_CHANNELS * 12];
struct spa_device_object_info info;
struct acp_card *card = this->card;
const char *stream, *devstr;;
@ -186,9 +186,10 @@ static int emit_node(struct impl *this, struct acp_device *dev)
items[4] = SPA_DICT_ITEM_INIT(SPA_KEY_AUDIO_CHANNELS, channels);
p = positions;
for (i = 0; i < dev->format.channels; i++)
p += snprintf(p, 6, "%s%s", i == 0 ? "" : ",",
spa_debug_type_short_name(spa_type_audio_channel[dev->format.map[i]].name));
for (i = 0; i < dev->format.channels; i++) {
p += snprintf(p, 12, "%s%s", i == 0 ? "" : ",",
acp_channel_str(ch, sizeof(ch), dev->format.map[i]));
}
items[5] = SPA_DICT_ITEM_INIT(SPA_KEY_AUDIO_POSITION, positions);
n_items = 6;