From 809fbc6fc1e862ab2dfcfcceeb65d7382d8b51e9 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 23 Oct 2014 17:36:14 +0200 Subject: ao_alsa: move parameter append code to a function Why not. (I thought I needed this, but my other experiments failed. So this is merely a minor cleanup.) --- audio/out/ao_alsa.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/audio/out/ao_alsa.c b/audio/out/ao_alsa.c index fb3cef75d4..0d078d17fe 100644 --- a/audio/out/ao_alsa.c +++ b/audio/out/ao_alsa.c @@ -293,6 +293,32 @@ static int map_iec958_srate(int srate) } } +// ALSA device strings can have parameters. They are usually appended to the +// device name. Since there can be various forms, and we (sometimes) want to +// append them to unknown device strings, which possibly already include params. +static char *append_params(void *ta_parent, const char *device, const char *p) +{ + if (!p || !p[0]) + return talloc_strdup(ta_parent, device); + + int len = strlen(device); + char *end = strchr(device, ':'); + if (!end) { + /* no existing parameters: add it behind device name */ + return talloc_asprintf(ta_parent, "%s:%s", device, p); + } else if (end[1] == '\0') { + /* ":" but no parameters */ + return talloc_asprintf(ta_parent, "%s%s", device, p); + } else if (end[1] == '{' && device[len - 1] == '}') { + /* parameters in config syntax: add it inside the { } block */ + return talloc_asprintf(ta_parent, "%.*s %s}", len - 1, device, p); + } else { + /* a simple list of parameters: add it at the end of the list */ + return talloc_asprintf(ta_parent, "%s,%s", device, p); + } + abort(); +} + static int try_open_device(struct ao *ao, const char *device, int open_mode) { struct priv *p = ao->priv; @@ -305,22 +331,7 @@ static int try_open_device(struct ao *ao, const char *device, int open_mode) IEC958_AES0_NONAUDIO | IEC958_AES0_PRO_EMPHASIS_NONE, IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER, map_iec958_srate(ao->samplerate)); - const char *ac3_device = device; - int len = strlen(device); - char *end = strchr(device, ':'); - if (!end) { - /* no existing parameters: add it behind device name */ - ac3_device = talloc_asprintf(tmp, "%s:%s", device, params); - } else if (end[1] == '\0') { - /* ":" but no parameters */ - ac3_device = talloc_asprintf(tmp, "%s%s", device, params); - } else if (end[1] == '{' && device[len - 1] == '}') { - /* parameters in config syntax: add it inside the { } block */ - ac3_device = talloc_asprintf(tmp, "%.*s %s}", len - 1, device, params); - } else { - /* a simple list of parameters: add it at the end of the list */ - ac3_device = talloc_asprintf(tmp, "%s,%s", device, params); - } + const char *ac3_device = append_params(tmp, device, params); int err = snd_pcm_open (&p->alsa, ac3_device, SND_PCM_STREAM_PLAYBACK, open_mode); talloc_free(tmp); -- cgit v1.2.3