mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-01 22:58:50 -04:00
spa: improve info parsing
Make info parsing a bit easier to read by assigning the key and value to temporary variables. Improve the parsing of channelmap using json parser to make it support more cases. Add a unit test for channelmap parsing options.
This commit is contained in:
parent
972cf8d657
commit
8e590df92f
6 changed files with 138 additions and 82 deletions
|
|
@ -785,33 +785,27 @@ impl_init(const struct spa_handle_factory *factory,
|
|||
snd_config_update_free_global();
|
||||
|
||||
for (i = 0; info && i < info->n_items; i++) {
|
||||
const char *k = info->items[i].key;
|
||||
const char *s = info->items[i].value;
|
||||
if (!strcmp(info->items[i].key, SPA_KEY_API_ALSA_PATH)) {
|
||||
snprintf(this->props.device, 63, "%s", info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_CHANNELS)) {
|
||||
this->default_channels = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_RATE)) {
|
||||
this->default_rate = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_FORMAT)) {
|
||||
this->default_format = spa_alsa_format_from_name(info->items[i].value, 128);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_POSITION)) {
|
||||
size_t len;
|
||||
while (*s && this->default_pos.channels < SPA_AUDIO_MAX_CHANNELS) {
|
||||
if ((len = strcspn(s, ",")) == 0)
|
||||
break;
|
||||
this->default_pos.pos[this->default_pos.channels++] =
|
||||
spa_alsa_channel_from_name(s, len);
|
||||
s += len + strspn(s+len, ",");
|
||||
}
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.period-size")) {
|
||||
this->default_period_size = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.headroom")) {
|
||||
this->default_headroom = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.disable-mmap")) {
|
||||
if (!strcmp(k, SPA_KEY_API_ALSA_PATH)) {
|
||||
snprintf(this->props.device, 63, "%s", s);
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_CHANNELS)) {
|
||||
this->default_channels = atoi(s);
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_RATE)) {
|
||||
this->default_rate = atoi(s);
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_FORMAT)) {
|
||||
this->default_format = spa_alsa_format_from_name(s, strlen(s));
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_POSITION)) {
|
||||
spa_alsa_parse_position(&this->default_pos, s, strlen(s));
|
||||
} else if (!strcmp(k, "api.alsa.period-size")) {
|
||||
this->default_period_size = atoi(s);
|
||||
} else if (!strcmp(k, "api.alsa.headroom")) {
|
||||
this->default_headroom = atoi(s);
|
||||
} else if (!strcmp(k, "api.alsa.disable-mmap")) {
|
||||
this->disable_mmap = (strcmp(s, "true") == 0 || atoi(s) == 1);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.disable-batch")) {
|
||||
} else if (!strcmp(k, "api.alsa.disable-batch")) {
|
||||
this->disable_batch = (strcmp(s, "true") == 0 || atoi(s) == 1);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.use-chmap")) {
|
||||
} else if (!strcmp(k, "api.alsa.use-chmap")) {
|
||||
this->props.use_chmap = (strcmp(s, "true") == 0 || atoi(s) == 1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -805,33 +805,27 @@ impl_init(const struct spa_handle_factory *factory,
|
|||
snd_config_update_free_global();
|
||||
|
||||
for (i = 0; info && i < info->n_items; i++) {
|
||||
const char *k = info->items[i].key;
|
||||
const char *s = info->items[i].value;
|
||||
if (!strcmp(info->items[i].key, SPA_KEY_API_ALSA_PATH)) {
|
||||
snprintf(this->props.device, 63, "%s", info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_CHANNELS)) {
|
||||
this->default_channels = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_RATE)) {
|
||||
this->default_rate = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_FORMAT)) {
|
||||
this->default_format = spa_alsa_format_from_name(info->items[i].value, 128);
|
||||
} else if (!strcmp(info->items[i].key, SPA_KEY_AUDIO_POSITION)) {
|
||||
size_t len;
|
||||
while (*s && this->default_pos.channels < SPA_AUDIO_MAX_CHANNELS) {
|
||||
if ((len = strcspn(s, ",")) == 0)
|
||||
break;
|
||||
this->default_pos.pos[this->default_pos.channels++] =
|
||||
spa_alsa_channel_from_name(s, len);
|
||||
s += len + strspn(s+len, ",");
|
||||
}
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.period-size")) {
|
||||
this->default_period_size = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.headroom")) {
|
||||
this->default_headroom = atoi(info->items[i].value);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.disable-mmap")) {
|
||||
if (!strcmp(k, SPA_KEY_API_ALSA_PATH)) {
|
||||
snprintf(this->props.device, 63, "%s", s);
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_CHANNELS)) {
|
||||
this->default_channels = atoi(s);
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_RATE)) {
|
||||
this->default_rate = atoi(s);
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_FORMAT)) {
|
||||
this->default_format = spa_alsa_format_from_name(s, strlen(s));
|
||||
} else if (!strcmp(k, SPA_KEY_AUDIO_POSITION)) {
|
||||
spa_alsa_parse_position(&this->default_pos, s, strlen(s));
|
||||
} else if (!strcmp(k, "api.alsa.period-size")) {
|
||||
this->default_period_size = atoi(s);
|
||||
} else if (!strcmp(k, "api.alsa.headroom")) {
|
||||
this->default_headroom = atoi(s);
|
||||
} else if (!strcmp(k, "api.alsa.disable-mmap")) {
|
||||
this->disable_mmap = (strcmp(s, "true") == 0 || atoi(s) == 1);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.disable-batch")) {
|
||||
} else if (!strcmp(k, "api.alsa.disable-batch")) {
|
||||
this->disable_batch = (strcmp(s, "true") == 0 || atoi(s) == 1);
|
||||
} else if (!strcmp(info->items[i].key, "api.alsa.use-chmap")) {
|
||||
} else if (!strcmp(k, "api.alsa.use-chmap")) {
|
||||
this->props.use_chmap = (strcmp(s, "true") == 0 || atoi(s) == 1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ extern "C" {
|
|||
#include <spa/support/loop.h>
|
||||
#include <spa/support/log.h>
|
||||
#include <spa/utils/list.h>
|
||||
#include <spa/utils/json.h>
|
||||
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/utils.h>
|
||||
|
|
@ -216,6 +217,23 @@ static inline uint32_t spa_alsa_channel_from_name(const char *name, size_t len)
|
|||
return SPA_AUDIO_CHANNEL_UNKNOWN;
|
||||
}
|
||||
|
||||
static inline void spa_alsa_parse_position(struct channel_map *map, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
char v[256];
|
||||
int l;
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
|
||||
map->channels = 0;
|
||||
while ((l = spa_json_get_string(&it[1], v, sizeof(v))) > 0 &&
|
||||
map->channels < SPA_AUDIO_MAX_CHANNELS) {
|
||||
map->pos[map->channels++] = spa_alsa_channel_from_name(v, l);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue