alsa: refactor property parsing

Move the common property parsing to the init function.
This commit is contained in:
Wim Taymans 2021-11-23 12:27:36 +01:00
parent 25bfc9c63d
commit 8995129e6c
4 changed files with 47 additions and 80 deletions

View file

@ -910,7 +910,6 @@ impl_init(const struct spa_handle_factory *factory,
struct spa_handle *handle, const struct spa_dict *info, const struct spa_support *support, uint32_t n_support)
{
struct state *this;
uint32_t i;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
@ -982,46 +981,7 @@ impl_init(const struct spa_handle_factory *factory,
spa_list_init(&this->ready);
for (i = 0; info && i < info->n_items; i++) {
const char *k = info->items[i].key;
const char *s = info->items[i].value;
if (spa_streq(k, SPA_KEY_API_ALSA_PATH)) {
snprintf(this->props.device, 63, "%s", s);
} else if (spa_streq(k, SPA_KEY_API_ALSA_PCM_CARD)) {
this->card_index = atoi(s);
} else if (spa_streq(k, SPA_KEY_API_ALSA_OPEN_UCM)) {
this->open_ucm = spa_atob(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_CHANNELS)) {
this->default_channels = atoi(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_RATE)) {
this->default_rate = atoi(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_FORMAT)) {
this->default_format = spa_alsa_format_from_name(s, strlen(s));
} else if (spa_streq(k, SPA_KEY_AUDIO_POSITION)) {
spa_alsa_parse_position(&this->default_pos, s, strlen(s));
} else if (spa_streq(k, "latency.internal.rate")) {
this->process_latency.rate = atoi(s);
} else if (spa_streq(k, "latency.internal.ns")) {
this->process_latency.ns = atoi(s);
} else if (spa_streq(k, "iec958.codecs")) {
spa_alsa_parse_iec958_codecs(&this->iec958_codecs, s, strlen(s));
} else if (spa_streq(k, "api.alsa.period-size")) {
this->default_period_size = atoi(s);
} else if (spa_streq(k, "api.alsa.headroom")) {
this->default_headroom = atoi(s);
} else if (spa_streq(k, "api.alsa.start-delay")) {
this->default_start_delay = atoi(s);
} else if (spa_streq(k, "api.alsa.disable-mmap")) {
this->disable_mmap = spa_atob(s);
} else if (spa_streq(k, "api.alsa.disable-batch")) {
this->disable_batch = spa_atob(s);
} else if (spa_streq(k, "api.alsa.use-chmap")) {
this->props.use_chmap = spa_atob(s);
} else if (spa_streq(k, "api.alsa.multi-rate")) {
this->multi_rate = spa_atob(s);
}
}
return spa_alsa_init(this);
return spa_alsa_init(this, info);
}
static const struct spa_interface_info impl_interfaces[] = {

View file

@ -860,7 +860,6 @@ impl_init(const struct spa_handle_factory *factory,
uint32_t n_support)
{
struct state *this;
uint32_t i;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
@ -927,42 +926,7 @@ impl_init(const struct spa_handle_factory *factory,
spa_list_init(&this->free);
spa_list_init(&this->ready);
for (i = 0; info && i < info->n_items; i++) {
const char *k = info->items[i].key;
const char *s = info->items[i].value;
if (spa_streq(k, SPA_KEY_API_ALSA_PATH)) {
snprintf(this->props.device, 63, "%s", s);
} else if (spa_streq(k, SPA_KEY_API_ALSA_PCM_CARD)) {
this->card_index = atoi(s);
} else if (spa_streq(k, SPA_KEY_API_ALSA_OPEN_UCM)) {
this->open_ucm = spa_atob(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_CHANNELS)) {
this->default_channels = atoi(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_RATE)) {
this->default_rate = atoi(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_FORMAT)) {
this->default_format = spa_alsa_format_from_name(s, strlen(s));
} else if (spa_streq(k, SPA_KEY_AUDIO_POSITION)) {
spa_alsa_parse_position(&this->default_pos, s, strlen(s));
} else if (spa_streq(k, "latency.internal.rate")) {
this->process_latency.rate = atoi(s);
} else if (spa_streq(k, "latency.internal.ns")) {
this->process_latency.ns = atoi(s);
} else if (spa_streq(k, "api.alsa.period-size")) {
this->default_period_size = atoi(s);
} else if (spa_streq(k, "api.alsa.headroom")) {
this->default_headroom = atoi(s);
} else if (spa_streq(k, "api.alsa.disable-mmap")) {
this->disable_mmap = spa_atob(s);
} else if (spa_streq(k, "api.alsa.disable-batch")) {
this->disable_batch = spa_atob(s);
} else if (spa_streq(k, "api.alsa.use-chmap")) {
this->props.use_chmap = spa_atob(s);
} else if (spa_streq(k, "api.alsa.multi-rate")) {
this->multi_rate = spa_atob(s);
}
}
return spa_alsa_init(this);
return spa_alsa_init(this, info);
}
static const struct spa_interface_info impl_interfaces[] = {

View file

@ -11,6 +11,7 @@
#include <spa/pod/filter.h>
#include <spa/utils/string.h>
#include <spa/support/system.h>
#include <spa/utils/keys.h>
#include "alsa-pcm.h"
@ -88,10 +89,52 @@ static void release_card(uint32_t index)
free(c);
}
int spa_alsa_init(struct state *state)
int spa_alsa_init(struct state *state, const struct spa_dict *info)
{
uint32_t i;
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 (spa_streq(k, SPA_KEY_API_ALSA_PATH)) {
snprintf(state->props.device, 63, "%s", s);
} else if (spa_streq(k, SPA_KEY_API_ALSA_PCM_CARD)) {
state->card_index = atoi(s);
} else if (spa_streq(k, SPA_KEY_API_ALSA_OPEN_UCM)) {
state->open_ucm = spa_atob(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_CHANNELS)) {
state->default_channels = atoi(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_RATE)) {
state->default_rate = atoi(s);
} else if (spa_streq(k, SPA_KEY_AUDIO_FORMAT)) {
state->default_format = spa_alsa_format_from_name(s, strlen(s));
} else if (spa_streq(k, SPA_KEY_AUDIO_POSITION)) {
spa_alsa_parse_position(&state->default_pos, s, strlen(s));
} else if (spa_streq(k, "latency.internal.rate")) {
state->process_latency.rate = atoi(s);
} else if (spa_streq(k, "latency.internal.ns")) {
state->process_latency.ns = atoi(s);
} else if (spa_streq(k, "iec958.codecs")) {
spa_alsa_parse_iec958_codecs(&state->iec958_codecs, s, strlen(s));
} else if (spa_streq(k, "api.alsa.period-size")) {
state->default_period_size = atoi(s);
} else if (spa_streq(k, "api.alsa.headroom")) {
state->default_headroom = atoi(s);
} else if (spa_streq(k, "api.alsa.start-delay")) {
state->default_start_delay = atoi(s);
} else if (spa_streq(k, "api.alsa.disable-mmap")) {
state->disable_mmap = spa_atob(s);
} else if (spa_streq(k, "api.alsa.disable-batch")) {
state->disable_batch = spa_atob(s);
} else if (spa_streq(k, "api.alsa.use-chmap")) {
state->props.use_chmap = spa_atob(s);
} else if (spa_streq(k, "api.alsa.multi-rate")) {
state->multi_rate = spa_atob(s);
}
}
if (state->stream == SND_PCM_STREAM_PLAYBACK) {
state->is_iec958 = spa_strstartswith(state->props.device, "iec958");
state->is_hdmi = spa_strstartswith(state->props.device, "hdmi");

View file

@ -228,7 +228,7 @@ spa_alsa_enum_format(struct state *state, int seq,
int spa_alsa_set_format(struct state *state, struct spa_audio_info *info, uint32_t flags);
int spa_alsa_init(struct state *state);
int spa_alsa_init(struct state *state, const struct spa_dict *info);
int spa_alsa_clear(struct state *state);
int spa_alsa_open(struct state *state, const char *params);