diff options
author | wm4 <wm4@nowhere> | 2013-05-12 21:47:55 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2013-05-12 21:47:55 +0200 |
commit | e6e5a7b221ef2fcdd5a1982d6fdcb627100447d2 (patch) | |
tree | 08b54ef9bb771434fc7fbe9185793503d3ba314c /audio/out | |
parent | 6a83ef1552de4a1a71da49e45647ce1a4ce64e53 (diff) | |
parent | 48f94311516dc1426644b3e68b2a48c22727e1e7 (diff) | |
download | mpv-e6e5a7b221ef2fcdd5a1982d6fdcb627100447d2.tar.bz2 mpv-e6e5a7b221ef2fcdd5a1982d6fdcb627100447d2.tar.xz |
Merge branch 'audio_changes'
Conflicts:
audio/out/ao_lavc.c
Diffstat (limited to 'audio/out')
-rw-r--r-- | audio/out/ao.c | 12 | ||||
-rw-r--r-- | audio/out/ao.h | 14 | ||||
-rw-r--r-- | audio/out/ao_alsa.c | 1345 | ||||
-rw-r--r-- | audio/out/ao_coreaudio.c | 19 | ||||
-rw-r--r-- | audio/out/ao_dsound.c | 91 | ||||
-rw-r--r-- | audio/out/ao_jack.c | 23 | ||||
-rw-r--r-- | audio/out/ao_lavc.c | 85 | ||||
-rw-r--r-- | audio/out/ao_null.c | 11 | ||||
-rw-r--r-- | audio/out/ao_openal.c | 82 | ||||
-rw-r--r-- | audio/out/ao_oss.c | 36 | ||||
-rw-r--r-- | audio/out/ao_pcm.c | 38 | ||||
-rw-r--r-- | audio/out/ao_portaudio.c | 11 | ||||
-rw-r--r-- | audio/out/ao_pulse.c | 106 | ||||
-rw-r--r-- | audio/out/ao_rsound.c | 13 | ||||
-rw-r--r-- | audio/out/ao_sdl.c | 18 | ||||
-rw-r--r-- | audio/out/audio_out_internal.h | 3 |
16 files changed, 960 insertions, 947 deletions
diff --git a/audio/out/ao.c b/audio/out/ao.c index 9fb201a333..10badcfa07 100644 --- a/audio/out/ao.c +++ b/audio/out/ao.c @@ -250,14 +250,24 @@ void ao_resume(struct ao *ao) ao->driver->resume(ao); } +bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s, + struct mp_chmap *map) +{ + return mp_chmap_sel_adjust(s, map); +} +bool ao_chmap_sel_get_def(struct ao *ao, const struct mp_chmap_sel *s, + struct mp_chmap *map, int num) +{ + return mp_chmap_sel_get_def(s, map, num); +} int old_ao_init(struct ao *ao, char *params) { assert(!global_ao); global_ao = ao; ao_subdevice = params ? talloc_strdup(ao, params) : NULL; - if (ao->driver->old_functions->init(ao->samplerate, ao->channels, + if (ao->driver->old_functions->init(ao->samplerate, &ao->channels, ao->format, 0) == 0) { global_ao = NULL; return -1; diff --git a/audio/out/ao.h b/audio/out/ao.h index 4a7c928824..d908841457 100644 --- a/audio/out/ao.h +++ b/audio/out/ao.h @@ -23,6 +23,8 @@ #include "core/bstr.h" #include "core/mp_common.h" +#include "audio/chmap.h" +#include "audio/chmap_sel.h" enum aocontrol { // _VOLUME commands take struct ao_control_vol pointer for input/output. @@ -55,7 +57,7 @@ typedef struct ao_info { /* interface towards mplayer and */ typedef struct ao_old_functions { int (*control)(int cmd, void *arg); - int (*init)(int rate, int channels, int format, int flags); + int (*init)(int rate, const struct mp_chmap *channels, int format, int flags); void (*uninit)(int immed); void (*reset)(void); int (*get_space)(void); @@ -68,7 +70,6 @@ typedef struct ao_old_functions { struct ao; struct ao_driver { - bool is_new; bool encode; const struct ao_info *info; const struct ao_old_functions *old_functions; @@ -86,9 +87,9 @@ struct ao_driver { /* global data used by mplayer and plugins */ struct ao { int samplerate; - int channels; + struct mp_chmap channels; int format; - int bps; + int bps; // bytes per second int outburst; int buffersize; double pts; @@ -121,6 +122,11 @@ void ao_reset(struct ao *ao); void ao_pause(struct ao *ao); void ao_resume(struct ao *ao); +bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s, + struct mp_chmap *map); +bool ao_chmap_sel_get_def(struct ao *ao, const struct mp_chmap_sel *s, + struct mp_chmap *map, int num); + int old_ao_control(struct ao *ao, enum aocontrol cmd, void *arg); int old_ao_init(struct ao *ao, char *params); void old_ao_uninit(struct ao *ao, bool cut_audio); diff --git a/audio/out/ao_alsa.c b/audio/out/ao_alsa.c index 366b912b76..93327881e5 100644 --- a/audio/out/ao_alsa.c +++ b/audio/out/ao_alsa.c @@ -36,6 +36,7 @@ #include <alloca.h> #include "config.h" +#include "core/options.h" #include "core/subopt-helper.h" #include "audio/mixer.h" #include "core/mp_msg.h" @@ -46,292 +47,372 @@ #include <alsa/asoundlib.h> #include "ao.h" -#include "audio_out_internal.h" #include "audio/format.h" - -static const ao_info_t info = -{ - "ALSA-0.9.x-1.x audio output", - "alsa", - "Alex Beregszaszi, Zsolt Barat <joy@streamminister.de>", - "under development" +#include "audio/reorder_ch.h" + +struct priv { + snd_pcm_t *alsa; + snd_pcm_format_t alsa_fmt; + size_t bytes_per_sample; + int can_pause; + snd_pcm_sframes_t prepause_frames; + float delay_before_pause; }; -LIBAO_EXTERN(alsa) - -static snd_pcm_t *alsa_handler; -static snd_pcm_format_t alsa_format; - #define BUFFER_TIME 500000 // 0.5 s #define FRAGCOUNT 16 -static size_t bytes_per_sample; +#define ALSA_DEVICE_SIZE 256 -static int alsa_can_pause; -static snd_pcm_sframes_t prepause_frames; -static float delay_before_pause; +#define CHECK_ALSA_ERROR(message) \ + do { \ + if (err < 0) { \ + mp_msg(MSGT_VO, MSGL_ERR, "[AO_ALSA] %s: %s\n", \ + (message), snd_strerror(err)); \ + goto alsa_error; \ + } \ + } while (0) -#define ALSA_DEVICE_SIZE 256 +static float get_delay(struct ao *ao); +static int play(struct ao *ao, void *data, int len, int flags); static void alsa_error_handler(const char *file, int line, const char *function, - int err, const char *format, ...) + int err, const char *format, ...) { - char tmp[0xc00]; - va_list va; - - va_start(va, format); - vsnprintf(tmp, sizeof tmp, format, va); - va_end(va); - - if (err) - mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s: %s\n", - file, line, function, tmp, snd_strerror(err)); - else - mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s\n", - file, line, function, tmp); + char tmp[0xc00]; + va_list va; + + va_start(va, format); + vsnprintf(tmp, sizeof tmp, format, va); + va_end(va); + + if (err) { + mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s: %s\n", + file, line, function, tmp, snd_strerror(err)); + } else { + mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s\n", + file, line, function, tmp); + } } /* to set/get/query special features/parameters */ -static int control(int cmd, void *arg) +static int control(struct ao *ao, enum aocontrol cmd, void *arg) { - switch(cmd) { - case AOCONTROL_GET_MUTE: - case AOCONTROL_SET_MUTE: - case AOCONTROL_GET_VOLUME: - case AOCONTROL_SET_VOLUME: + snd_mixer_t *handle = NULL; + switch (cmd) { + case AOCONTROL_GET_MUTE: + case AOCONTROL_SET_MUTE: + case AOCONTROL_GET_VOLUME: + case AOCONTROL_SET_VOLUME: { - int err; - snd_mixer_t *handle; - snd_mixer_elem_t *elem; - snd_mixer_selem_id_t *sid; - - char *mix_name = "Master"; - char *card = "default"; - int mix_index = 0; - - long pmin, pmax; - long get_vol, set_vol; - float f_multi; - - if(AF_FORMAT_IS_IEC61937(ao_data.format)) - return CONTROL_TRUE; - - if(mixer_channel) { - char *test_mix_index; - - mix_name = strdup(mixer_channel); - if ((test_mix_index = strchr(mix_name, ','))){ - *test_mix_index = 0; - test_mix_index++; - mix_index = strtol(test_mix_index, &test_mix_index, 0); - - if (*test_mix_index){ - mp_tmsg(MSGT_AO,MSGL_ERR, - "[AO_ALSA] Invalid mixer index. Defaulting to 0.\n"); - mix_index = 0 ; - } - } - } - if(mixer_device) card = mixer_device; - - //allocate simple id - snd_mixer_selem_id_alloca(&sid); - - //sets simple-mixer index and name - snd_mixer_selem_id_set_index(sid, mix_index); - snd_mixer_selem_id_set_name(sid, mix_name); - - if (mixer_channel) { - free(mix_name); - mix_name = NULL; - } - - if ((err = snd_mixer_open(&handle, 0)) < 0) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer open error: %s\n", snd_strerror(err)); - return CONTROL_ERROR; - } - - if ((err = snd_mixer_attach(handle, card)) < 0) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer attach %s error: %s\n", - card, snd_strerror(err)); - snd_mixer_close(handle); - return CONTROL_ERROR; - } - - if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer register error: %s\n", snd_strerror(err)); - snd_mixer_close(handle); - return CONTROL_ERROR; - } - err = snd_mixer_load(handle); - if (err < 0) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer load error: %s\n", snd_strerror(err)); - snd_mixer_close(handle); - return CONTROL_ERROR; - } - - elem = snd_mixer_find_selem(handle, sid); - if (!elem) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to find simple control '%s',%i.\n", - snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); - snd_mixer_close(handle); - return CONTROL_ERROR; - } - - snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax); - f_multi = (100 / (float)(pmax - pmin)); - - switch (cmd) { - case AOCONTROL_SET_VOLUME: { - ao_control_vol_t *vol = arg; - set_vol = vol->left / f_multi + pmin + 0.5; - - //setting channels - if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, set_vol)) < 0) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting left channel, %s\n", - snd_strerror(err)); - goto mixer_error; - } - mp_msg(MSGT_AO,MSGL_DBG2,"left=%li, ", set_vol); - - set_vol = vol->right / f_multi + pmin + 0.5; - - if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, set_vol)) < 0) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting right channel, %s\n", - snd_strerror(err)); - goto mixer_error; - } - mp_msg(MSGT_AO,MSGL_DBG2,"right=%li, pmin=%li, pmax=%li, mult=%f\n", - set_vol, pmin, pmax, f_multi); - break; - } - case AOCONTROL_GET_VOLUME: { - ao_control_vol_t *vol = arg; - snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &get_vol); - vol->left = (get_vol - pmin) * f_multi; - snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &get_vol); - vol->right = (get_vol - pmin) * f_multi; - mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right); - break; - } - case AOCONTROL_SET_MUTE: { - bool *mute = arg; - if (!snd_mixer_selem_has_playback_switch(elem)) - goto mixer_error; - if (!snd_mixer_selem_has_playback_switch_joined(elem)) { - snd_mixer_selem_set_playback_switch( - elem, SND_MIXER_SCHN_FRONT_RIGHT, !*mute); - } - snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, - !*mute); - break; - } - case AOCONTROL_GET_MUTE: { - bool *mute = arg; - if (!snd_mixer_selem_has_playback_switch(elem)) - goto mixer_error; - int tmp = 1; - snd_mixer_selem_get_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, - &tmp); - *mute = !tmp; - if (!snd_mixer_selem_has_playback_switch_joined(elem)) { - snd_mixer_selem_get_playback_switch( - elem, SND_MIXER_SCHN_FRONT_RIGHT, &tmp); - *mute &= !tmp; + int err; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + + char *mix_name = "Master"; + char *card = "default"; + int mix_index = 0; + + long pmin, pmax; + long get_vol, set_vol; + float f_multi; + + if (AF_FORMAT_IS_IEC61937(ao->format)) + return CONTROL_TRUE; + + if (ao->opts->mixer_channel) { + char *test_mix_index; + + mix_name = strdup(ao->opts->mixer_channel); + if ((test_mix_index = strchr(mix_name, ','))) { + *test_mix_index = 0; + test_mix_index++; + mix_index = strtol(test_mix_index, &test_mix_index, 0); + + if (*test_mix_index) { + mp_tmsg(MSGT_AO, MSGL_ERR, + "[AO_ALSA] Invalid mixer index. Defaulting to 0.\n"); + mix_index = 0; + } + } + } + if (ao->opts->mixer_device) + card = ao->opts->mixer_device; + + //allocate simple id + snd_mixer_selem_id_alloca(&sid); + + //sets simple-mixer index and name + snd_mixer_selem_id_set_index(sid, mix_index); + snd_mixer_selem_id_set_name(sid, mix_name); + + if (ao->opts->mixer_channel) { + free(mix_name); + mix_name = NULL; } - break; - } - } - snd_mixer_close(handle); - return CONTROL_OK; - mixer_error: - snd_mixer_close(handle); - return CONTROL_ERROR; + + err = snd_mixer_open(&handle, 0); + CHECK_ALSA_ERROR("Mixer open error"); + + err = snd_mixer_attach(handle, card); + CHECK_ALSA_ERROR("Mixer attach error"); + + err = snd_mixer_selem_register(handle, NULL, NULL); + CHECK_ALSA_ERROR("Mixer register error"); + + err = snd_mixer_load(handle); + CHECK_ALSA_ERROR("Mixer load error"); + + elem = snd_mixer_find_selem(handle, sid); + if (!elem) { + mp_tmsg(MSGT_AO, MSGL_ERR, + "[AO_ALSA] Unable to find simple control '%s',%i.\n", + snd_mixer_selem_id_get_name(sid), + snd_mixer_selem_id_get_index(sid)); + goto alsa_error; + } + + snd_mixer_selem_get_playback_volume_range(elem, &pmin, &pmax); + f_multi = (100 / (float)(pmax - pmin)); + + switch (cmd) { + case AOCONTROL_SET_VOLUME: { + ao_control_vol_t *vol = arg; + set_vol = vol->left / f_multi + pmin + 0.5; + + //setting channels + err = snd_mixer_selem_set_playback_volume + (elem, SND_MIXER_SCHN_FRONT_LEFT, set_vol); + CHECK_ALSA_ERROR("Error setting left channel"); + mp_msg(MSGT_AO, MSGL_DBG2, "left=%li, ", set_vol); + + set_vol = vol->right / f_multi + pmin + 0.5; + + err = snd_mixer_selem_set_playback_volume + (elem, SND_MIXER_SCHN_FRONT_RIGHT, set_vol); + CHECK_ALSA_ERROR("Error setting right channel"); + mp_msg(MSGT_AO, MSGL_DBG2, + "right=%li, pmin=%li, pmax=%li, mult=%f\n", + set_vol, pmin, pmax, + f_multi); + break; + } + case AOCONTROL_GET_VOLUME: { + ao_control_vol_t *vol = arg; + snd_mixer_selem_get_playback_volume + (elem, SND_MIXER_SCHN_FRONT_LEFT, &get_vol); + vol->left = (get_vol - pmin) * f_multi; + snd_mixer_selem_get_playback_volume + (elem, SND_MIXER_SCHN_FRONT_RIGHT, &get_vol); + vol->right = (get_vol - pmin) * f_multi; + mp_msg(MSGT_AO, MSGL_DBG2, "left=%f, right=%f\n", vol->left, + vol->right); + break; + } + case AOCONTROL_SET_MUTE: { + bool *mute = arg; + if (!snd_mixer_selem_has_playback_switch(elem)) + goto alsa_error; + if (!snd_mixer_selem_has_playback_switch_joined(elem)) { + snd_mixer_selem_set_playback_switch + (elem, SND_MIXER_SCHN_FRONT_RIGHT, !*mute); + } + snd_mixer_selem_set_playback_switch + (elem, SND_MIXER_SCHN_FRONT_LEFT, !*mute); + break; + } + case AOCONTROL_GET_MUTE: { + bool *mute = arg; + if (!snd_mixer_selem_has_playback_switch(elem)) + goto alsa_error; + int tmp = 1; + snd_mixer_selem_get_playback_switch + (elem, SND_MIXER_SCHN_FRONT_LEFT, &tmp); + *mute = !tmp; + if (!snd_mixer_selem_has_playback_switch_joined(elem)) { + snd_mixer_selem_get_playback_switch + (elem, SND_MIXER_SCHN_FRONT_RIGHT, &tmp); + *mute &= !tmp; + } + break; + } + } + snd_mixer_close(handle); + return CONTROL_OK; } - } //end switch - return CONTROL_UNKNOWN; + } //end switch + return CONTROL_UNKNOWN; + +alsa_error: + if (handle) + snd_mixer_close(handle); + return CONTROL_ERROR; } -static void parse_device (char *dest, const char *src, int len) +static void parse_device(char *dest, const char *src, int len) { - char *tmp; - memmove(dest, src, len); - dest[len] = 0; - while ((tmp = strrchr(dest, '.'))) - tmp[0] = ','; - while ((tmp = strrchr(dest, '='))) - tmp[0] = ':'; + char *tmp; + memmove(dest, src, len); + dest[len] = 0; + while ((tmp = strrchr(dest, '.'))) + tmp[0] = ','; + while ((tmp = strrchr(dest, '='))) + tmp[0] = ':'; } -static void print_help (void) +static void print_help(void) { - mp_tmsg (MSGT_AO, MSGL_FATAL, - "\n[AO_ALSA] -ao alsa commandline help:\n"\ - "[AO_ALSA] Example: mpv -ao alsa:device=hw=0.3\n"\ - "[AO_ALSA] Sets first card fourth hardware device.\n\n"\ - "[AO_ALSA] Options:\n"\ - "[AO_ALSA] noblock\n"\ - "[AO_ALSA] Opens device in non-blocking mode.\n"\ - "[AO_ALSA] device=<device-name>\n"\ - "[AO_ALSA] Sets device (change , to . and : to =)\n"); + mp_tmsg(MSGT_AO, MSGL_FATAL, + "\n[AO_ALSA] -ao alsa commandline help:\n" \ + "[AO_ALSA] Example: mpv -ao alsa:device=hw=0.3\n" \ + "[AO_ALSA] Sets first card fourth hardware device.\n\n" \ + "[AO_ALSA] Options:\n" \ + "[AO_ALSA] noblock\n" \ + "[AO_ALSA] Opens device in non-blocking mode.\n" \ + "[AO_ALSA] device=<device-name>\n" \ + "[AO_ALSA] Sets device (change , to . and : to =)\n"); } -static int str_maxlen(void *strp) { - strarg_t *str = strp; - return str->len <= ALSA_DEVICE_SIZE; +static int str_maxlen(void *strp) +{ + strarg_t *str = strp; + return str->len <= ALSA_DEVICE_SIZE; } -static int try_open_device(const char *device, int open_mode, int try_ac3) + +static const int mp_to_alsa_format[][2] = { + {AF_FORMAT_S8, SND_PCM_FORMAT_S8}, + {AF_FORMAT_U8, SND_PCM_FORMAT_U8}, + {AF_FORMAT_U16_LE, SND_PCM_FORMAT_U16_LE}, + {AF_FORMAT_U16_BE, SND_PCM_FORMAT_U16_BE}, + {AF_FORMAT_S16_LE, SND_PCM_FORMAT_S16_LE}, + {AF_FORMAT_S16_BE, SND_PCM_FORMAT_S16_BE}, + {AF_FORMAT_U32_LE, SND_PCM_FORMAT_U32_LE}, + {AF_FORMAT_U32_BE, SND_PCM_FORMAT_U32_BE}, + {AF_FORMAT_S32_LE, SND_PCM_FORMAT_S32_LE}, + {AF_FORMAT_S32_BE, SND_PCM_FORMAT_S32_BE}, + {AF_FORMAT_U24_LE, SND_PCM_FORMAT_U24_3LE}, + {AF_FORMAT_U24_BE, SND_PCM_FORMAT_U24_3BE}, + {AF_FORMAT_S24_LE, SND_PCM_FORMAT_S24_3LE}, + {AF_FORMAT_S24_BE, SND_PCM_FORMAT_S24_3BE}, + {AF_FORMAT_FLOAT_LE, SND_PCM_FORMAT_FLOAT_LE}, + {AF_FORMAT_FLOAT_BE, SND_PCM_FORMAT_FLOAT_BE}, + {AF_FORMAT_AC3_LE, SND_PCM_FORMAT_S16_LE}, + {AF_FORMAT_AC3_BE, SND_PCM_FORMAT_S16_BE}, + {AF_FORMAT_IEC61937_LE, SND_PCM_FORMAT_S16_LE}, + {AF_FORMAT_IEC61937_BE, SND_PCM_FORMAT_S16_BE}, + {AF_FORMAT_MPEG2, SND_PCM_FORMAT_MPEG}, + {AF_FORMAT_UNKNOWN, SND_PCM_FORMAT_UNKNOWN}, +}; + +static int find_alsa_format(int af_format) { - int err, len; - char *ac3_device, *args; - - if (try_ac3) { - /* to set the non-audio bit, use AES0=6 */ - len = strlen(device); - ac3_device = malloc(len + 7 + 1); - if (!ac3_device) - return -ENOMEM; - strcpy(ac3_device, device); - args = strchr(ac3_device, ':'); - if (!args) { - /* no existing parameters: add it behind device name */ - strcat(ac3_device, ":AES0=6"); - } else { - do - ++args; - while (isspace(*args)); - if (*args == '\0') { - /* ":" but no parameters */ - strcat(ac3_device, "AES0=6"); - } else if (*args != '{') { - /* a simple list of parameters: add it at the end of the list */ - strcat(ac3_device, ",AES0=6"); - } else { - /* parameters in config syntax: add it inside the { } block */ - do - --len; - while (len > 0 && isspace(ac3_device[len])); - if (ac3_device[len] == '}') - strcpy(ac3_device + len, " AES0=6}"); - } + for (int n = 0; mp_to_alsa_format[n][0] != AF_FORMAT_UNKNOWN; n++) { + if (mp_to_alsa_format[n][0] == af_format) + return mp_to_alsa_format[n][1]; + } + return SND_PCM_FORMAT_UNKNOWN; +} + +// Lists device names and their implied channel map. +// The second item must be resolvable with mp_chmap_from_str(). +// Source: http://www.alsa-project.org/main/index.php/DeviceNames +// (Speaker names are slightly different from mpv's.) +static const char *device_channel_layouts[][2] = { + {"default", "fc"}, + {"default", "fl-fr"}, + {"rear", "bl-br"}, + {"center_lfe", "fc-lfe"}, + {"side", "sl-sr"}, + {"surround40", "fl-fr-fc-bc"}, + {"surround50", "fl-fr-bl-br-fc"}, + {"surround41", "fl-fr-bl-br-lfe"}, + {"surround51", "fl-fr-bl-br-fc-lfe"}, + {"surround71", "fl-fr-bl-br-fc-lfe-sl-sr"}, +}; + +#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0])) + +#define NUM_ALSA_CHMAPS ARRAY_LEN(device_channel_layouts) + +static const char *select_chmap(struct ao *ao) +{ + struct mp_chmap_sel sel = {0}; + struct mp_chmap maps[NUM_ALSA_CHMAPS]; + for (int n = 0; n < NUM_ALSA_CHMAPS; n++) { + mp_chmap_from_str(&maps[n], bstr0(device_channel_layouts[n][1])); + mp_chmap_sel_add_map(&sel, &maps[n]); + }; + + if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels)) + return NULL; + + for (int n = 0; n < NUM_ALSA_CHMAPS; n++) { + if (mp_chmap_equals(&ao->channels, &maps[n])) + return device_channel_layouts[n][0]; + } + + char *name = mp_chmap_to_str(&ao->channels); + mp_tmsg(MSGT_AO, MSGL_ERR, + "[AO_ALSA] channel layout %s (%d ch) not supported.\n", + name, ao->channels.num); + talloc_free(name); + return "default"; +} + +static int try_open_device(struct ao *ao, const char *device, int open_mode, + int try_ac3) +{ + struct priv *p = ao->priv; + int err, len; + char *ac3_device, *args; + + if (try_ac3) { + /* to set the non-audio bit, use AES0=6 */ + len = strlen(device); + ac3_device = malloc(len + 7 + 1); + if (!ac3_device) + return -ENOMEM; + strcpy(ac3_device, device); + args = strchr(ac3_device, ':'); + if (!args) { + /* no existing parameters: add it behind device name */ + strcat(ac3_device, ":AES0=6"); + } else { + do { + ++args; + } while (isspace(*args)); + if (*args == '\0') { + /* ":" but no parameters */ + strcat(ac3_device, "AES0=6"); + } else if (*args != '{') { + /* a simple list of parameters: add it at the end of the list */ + strcat(ac3_device, ",AES0=6"); + } else { + /* parameters in config syntax: add it inside the { } block */ + do { + --len; + } while (len > 0 && isspace(ac3_device[len])); + if (ac3_device[len] == '}') + strcpy(ac3_device + len, " AES0=6}"); + } + } + err = snd_pcm_open + (&p->alsa, ac3_device, SND_PCM_STREAM_PLAYBACK, open_mode); + free(ac3_device); + if (!err) + return 0; } - err = snd_pcm_open(&alsa_handler, ac3_device, SND_PCM_STREAM_PLAYBACK, - open_mode); - free(ac3_device); - if (!err) - return 0; - } - return snd_pcm_open(&alsa_handler, device, SND_PCM_STREAM_PLAYBACK, - open_mode); + return snd_pcm_open + (&p->alsa, device, SND_PCM_STREAM_PLAYBACK, open_mode); } /* open & setup audio device - return: 1=success 0=fail -*/ -static int init(int rate_hz, int channels, int format, int flags) + return: 0=success -1=fail + */ +static int init(struct ao *ao, char *params) { int err; int block; @@ -340,89 +421,29 @@ static int init(int rate_hz, int channels, int format, int flags) snd_pcm_uframes_t bufsize; snd_pcm_uframes_t boundary; const opt_t subopts[] = { - {"block", OPT_ARG_BOOL, &block, NULL}, - {"device", OPT_ARG_STR, &device, str_maxlen}, - {NULL} + {"block", OPT_ARG_BOOL, &block, NULL}, + {"device", OPT_ARG_STR, &device, str_maxlen}, + {NULL} }; + struct priv *p = talloc_zero(ao, struct priv); + ao->priv = p; + char alsa_device[ALSA_DEVICE_SIZE + 1]; // make sure alsa_device is null-terminated even when using strncpy etc. memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1); - mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz, - channels, format); - alsa_handler = NULL; - mp_msg(MSGT_AO,MSGL_V,"alsa-init: using ALSA %s\n", snd_asoundlib_version()); + mp_msg(MSGT_AO, MSGL_V, + "alsa-init: requested format: %d Hz, %d channels, %x\n", + ao->samplerate, ao->channels.num, ao->format); + p->alsa = NULL; + mp_msg(MSGT_AO, MSGL_V, "alsa-init: using ALSA %s\n", snd_asoundlib_version()); - prepause_frames = 0; - delay_before_pause = 0; + p->prepause_frames = 0; + p->delay_before_pause = 0; snd_lib_error_set_handler(alsa_error_handler); - ao_data.samplerate = rate_hz; - ao_data.format = format; - ao_data.channels = channels; - - switch (format) - { - case AF_FORMAT_S8: - alsa_format = SND_PCM_FORMAT_S8; - break; - case AF_FORMAT_U8: - alsa_format = SND_PCM_FORMAT_U8; - break; - case AF_FORMAT_U16_LE: - alsa_format = SND_PCM_FORMAT_U16_LE; - break; - case AF_FORMAT_U16_BE: - alsa_format = SND_PCM_FORMAT_U16_BE; - break; - case AF_FORMAT_AC3_LE: - case AF_FORMAT_S16_LE: - case AF_FORMAT_IEC61937_LE: - alsa_format = SND_PCM_FORMAT_S16_LE; - break; - case AF_FORMAT_AC3_BE: - case AF_FORMAT_S16_BE: - case AF_FORMAT_IEC61937_BE: - alsa_format = SND_PCM_FORMAT_S16_BE; - break; - case AF_FORMAT_U32_LE: - alsa_format = SND_PCM_FORMAT_U32_LE; - break; - case AF_FORMAT_U32_BE: - alsa_format = SND_PCM_FORMAT_U32_BE; - break; - case AF_FORMAT_S32_LE: - alsa_format = SND_PCM_FORMAT_S32_LE; - break; - case AF_FORMAT_S32_BE: - alsa_format = SND_PCM_FORMAT_S32_BE; - break; - case AF_FORMAT_U24_LE: - alsa_format = SND_PCM_FORMAT_U24_3LE; - break; - case AF_FORMAT_U24_BE: - alsa_format = SND_PCM_FORMAT_U24_3BE; - break; - case AF_FORMAT_S24_LE: - alsa_format = SND_PCM_FORMAT_S24_3LE; - break; - case AF_FORMAT_S24_BE: - alsa_format = SND_PCM_FORMAT_S24_3BE; - break; - case AF_FORMAT_FLOAT_LE: - alsa_format = SND_PCM_FORMAT_FLOAT_LE; - break; - case AF_FORMAT_FLOAT_BE: - alsa_format = SND_PCM_FORMAT_FLOAT_BE; - break; - - default: - alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1 - break; - } - //subdevice parsing // set defaults block = 1; @@ -432,347 +453,271 @@ static int init(int rate_hz, int channels, int format, int flags) * while opening the abstract alias for the spdif subdevice * 'iec958' */ - if (AF_FORMAT_IS_IEC61937(format)) { - device.str = "iec958"; - mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3/iec61937/iec958, %i channels\n", channels); - } - else - /* in any case for multichannel playback we should select - * appropriate device - */ - switch (channels) { - case 1: - case 2: - device.str = "default"; - mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n"); - break; - case 4: - if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) - // hack - use the converter plugin - device.str = "plug:surround40"; - else - device.str = "surround40"; - mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n"); - break; - case 6: - if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) - device.str = "plug:surround51"; - else - device.str = "surround51"; - mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n"); - break; - case 8: - if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) - device.str = "plug:surround71"; - else - device.str = "surround71"; - mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround71\n"); - break; - default: - device.str = "default"; - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] %d channels are not supported.\n",channels); + device.str = NULL; + if (AF_FORMAT_IS_IEC61937(ao->format)) { + device.str = "iec958"; + mp_msg(MSGT_AO, MSGL_V, + "alsa-spdif-init: playing AC3/iec61937/iec958, %i channels\n", + ao->channels.num); + } else { + device.str = select_chmap(ao); + if (strcmp(device.str, "default") != 0 && ao->format == AF_FORMAT_FLOAT_NE) + { + // hack - use the converter plugin (why the heck?) + device.str = talloc_asprintf(ao, "plug:%s", device.str); } + } device.len = strlen(device.str); - if (subopt_parse(ao_subdevice, subopts) != 0) { + if (subopt_parse(params, subopts) != 0) { print_help(); return 0; } parse_device(alsa_device, device.str, device.len); - mp_msg(MSGT_AO,MSGL_V,"alsa-init: using device %s\n", alsa_device); - - alsa_can_pause = 1; - - if (!alsa_handler) { - int open_mode = block ? 0 : SND_PCM_NONBLOCK; - int isac3 = AF_FORMAT_IS_IEC61937(format); - //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC - if ((err = try_open_device(alsa_device, open_mode, isac3)) < 0) - { - if (err != -EBUSY && !block) { - mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Open in nonblock-mode failed, trying to open in block-mode.\n"); - if ((err = try_open_device(alsa_device, 0, isac3)) < 0) { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err)); - return 0; - } - } else { - mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err)); - return 0; - } |