mpv/audio/out/ao_alsa.c

797 lines
25 KiB
C
Raw Normal View History

/*
* ALSA 0.9.x-1.x audio output driver
*
* Copyright (C) 2004 Alex Beregszaszi
* Zsolt Barat <joy@streamminister.de>
*
* modified for real ALSA 0.9.0 support by Zsolt Barat <joy@streamminister.de>
* additional AC-3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org>
* 08/22/2002 iec958-init rewritten and merged with common init, zsolt
* 04/13/2004 merged with ao_alsa1.x, fixes provided by Jindrich Makovicka
* 04/25/2004 printfs converted to mp_msg, Zsolt.
*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include <string.h>
#include "config.h"
#include "options/options.h"
#include "options/m_option.h"
#include "common/msg.h"
#include "osdep/endian.h"
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#include <alsa/asoundlib.h>
#include "ao.h"
#include "internal.h"
#include "audio/format.h"
2013-04-18 22:24:26 +00:00
struct priv {
snd_pcm_t *alsa;
snd_pcm_format_t alsa_fmt;
int can_pause;
snd_pcm_sframes_t prepause_frames;
float delay_before_pause;
int buffersize; // in frames
int outburst; // in frames
int cfg_block;
char *cfg_device;
char *cfg_mixer_device;
char *cfg_mixer_name;
int cfg_mixer_index;
int cfg_resample;
int cfg_ni;
2013-04-18 22:24:26 +00:00
};
#define BUFFER_TIME 250000 // 250ms
#define FRAGCOUNT 16
#define CHECK_ALSA_ERROR(message) \
do { \
if (err < 0) { \
MP_ERR(ao, "%s: %s\n", (message), snd_strerror(err)); \
goto alsa_error; \
} \
} while (0)
2013-04-18 22:16:23 +00:00
static float get_delay(struct ao *ao);
static void uninit(struct ao *ao);
2013-04-18 22:16:23 +00:00
/* to set/get/query special features/parameters */
2013-04-18 22:16:23 +00:00
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
{
struct priv *p = ao->priv;
snd_mixer_t *handle = NULL;
2013-04-07 19:34:09 +00:00
switch (cmd) {
case AOCONTROL_GET_MUTE:
case AOCONTROL_SET_MUTE:
case AOCONTROL_GET_VOLUME:
case AOCONTROL_SET_VOLUME:
{
2013-04-07 19:34:09 +00:00
int err;
snd_mixer_elem_t *elem;
snd_mixer_selem_id_t *sid;
long pmin, pmax;
long get_vol, set_vol;
float f_multi;
audio: cleanup spdif format definitions Before this commit, there was AF_FORMAT_AC3 (the original spdif format, used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS and DTS-HD), which was handled as some sort of superset for AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used IEC61937-framing, but still was handled as something "separate". Technically, all of them are pretty similar, but may use different bitrates. Since digital passthrough pretends to be PCM (just with special headers that wrap digital packets), this is easily detectable by the higher samplerate or higher number of channels, so I don't know why you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs. AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is just a mess. Simplify this by handling all these formats the same way. AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3). All AOs just accept all spdif formats now - whether that works or not is not really clear (seems inconsistent due to earlier attempts to make DTS-HD work). But on the other hand, enabling spdif requires manual user interaction, so it doesn't matter much if initialization fails in slightly less graceful ways if it can't work at all. At a later point, we will support passthrough with ao_pulse. It seems the PulseAudio API wants to know the codec type (or maybe not - feeding it DTS while telling it it's AC3 works), add separate formats for each codecs. While this reminds of the earlier chaos, it's stricter, and most code just uses AF_FORMAT_IS_IEC61937(). Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to include special formats, so that it always describes the fundamental sample format type. This also ensures valid AF formats are never 0 (this was probably broken in one of the earlier commits from today).
2014-09-23 20:44:54 +00:00
if (AF_FORMAT_IS_SPECIAL(ao->format))
return CONTROL_FALSE;
2013-04-07 19:34:09 +00:00
//allocate simple id
snd_mixer_selem_id_alloca(&sid);
//sets simple-mixer index and name
snd_mixer_selem_id_set_index(sid, p->cfg_mixer_index);
snd_mixer_selem_id_set_name(sid, p->cfg_mixer_name);
2013-04-07 19:34:09 +00:00
err = snd_mixer_open(&handle, 0);
CHECK_ALSA_ERROR("Mixer open error");
2013-04-07 19:34:09 +00:00
err = snd_mixer_attach(handle, p->cfg_mixer_device);
CHECK_ALSA_ERROR("Mixer attach error");
err = snd_mixer_selem_register(handle, NULL, NULL);
CHECK_ALSA_ERROR("Mixer register error");
2013-04-07 19:34:09 +00:00
err = snd_mixer_load(handle);
CHECK_ALSA_ERROR("Mixer load error");
2013-04-07 19:34:09 +00:00
elem = snd_mixer_find_selem(handle, sid);
if (!elem) {
MP_VERBOSE(ao, "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;
2013-04-07 19:34:09 +00:00
}
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_DBG(ao, "left=%li, ", set_vol);
2013-04-07 19:34:09 +00:00
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_DBG(ao, "right=%li, pmin=%li, pmax=%li, mult=%f\n",
2013-04-07 19:34:09 +00:00
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);
2013-04-07 19:34:09 +00:00
vol->left = (get_vol - pmin) * f_multi;
snd_mixer_selem_get_playback_volume
(elem, SND_MIXER_SCHN_FRONT_RIGHT, &get_vol);
2013-04-07 19:34:09 +00:00
vol->right = (get_vol - pmin) * f_multi;
MP_DBG(ao, "left=%f, right=%f\n", vol->left, vol->right);
2013-04-07 19:34:09 +00:00
break;
}
case AOCONTROL_SET_MUTE: {
bool *mute = arg;
if (!snd_mixer_selem_has_playback_switch(elem))
goto alsa_error;
2013-04-07 19:34:09 +00:00
if (!snd_mixer_selem_has_playback_switch_joined(elem)) {
snd_mixer_selem_set_playback_switch
(elem, SND_MIXER_SCHN_FRONT_RIGHT, !*mute);
2013-04-07 19:34:09 +00:00
}
snd_mixer_selem_set_playback_switch
(elem, SND_MIXER_SCHN_FRONT_LEFT, !*mute);
2013-04-07 19:34:09 +00:00
break;
}
case AOCONTROL_GET_MUTE: {
bool *mute = arg;
if (!snd_mixer_selem_has_playback_switch(elem))
goto alsa_error;
2013-04-07 19:34:09 +00:00
int tmp = 1;
snd_mixer_selem_get_playback_switch
(elem, SND_MIXER_SCHN_FRONT_LEFT, &tmp);
2013-04-07 19:34:09 +00:00
*mute = !tmp;
if (!snd_mixer_selem_has_playback_switch_joined(elem)) {
snd_mixer_selem_get_playback_switch
(elem, SND_MIXER_SCHN_FRONT_RIGHT, &tmp);
2013-04-07 19:34:09 +00:00
*mute &= !tmp;
}
break;
}
}
snd_mixer_close(handle);
return CONTROL_OK;
}
2013-04-07 19:34:09 +00:00
} //end switch
return CONTROL_UNKNOWN;
alsa_error:
if (handle)
snd_mixer_close(handle);
return CONTROL_ERROR;
}
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, SND_PCM_FORMAT_U16},
{AF_FORMAT_S16, SND_PCM_FORMAT_S16},
{AF_FORMAT_U32, SND_PCM_FORMAT_U32},
{AF_FORMAT_S32, SND_PCM_FORMAT_S32},
{AF_FORMAT_U24,
MP_SELECT_LE_BE(SND_PCM_FORMAT_U24_3LE, SND_PCM_FORMAT_U24_3BE)},
{AF_FORMAT_S24,
MP_SELECT_LE_BE(SND_PCM_FORMAT_S24_3LE, SND_PCM_FORMAT_S24_3BE)},
{AF_FORMAT_FLOAT, SND_PCM_FORMAT_FLOAT},
{AF_FORMAT_UNKNOWN, SND_PCM_FORMAT_UNKNOWN},
};
static int find_alsa_format(int af_format)
{
af_format = af_fmt_from_planar(af_format);
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 *const device_channel_layouts[][2] = {
{"default", "fc"},
{"default", "fl-fr"},
{"rear", "bl-br"},
{"center_lfe", "fc-lfe"},
{"side", "sl-sr"},
{"surround40", "fl-fr-bl-br"},
{"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_ERR(ao, "channel layout %s (%d ch) not supported.\n",
name, ao->channels.num);
talloc_free(name);
return "default";
}
static int map_iec958_srate(int srate)
{
switch (srate) {
case 44100: return IEC958_AES3_CON_FS_44100;
case 48000: return IEC958_AES3_CON_FS_48000;
case 32000: return IEC958_AES3_CON_FS_32000;
case 22050: return IEC958_AES3_CON_FS_22050;
case 24000: return IEC958_AES3_CON_FS_24000;
case 88200: return IEC958_AES3_CON_FS_88200;
case 768000: return IEC958_AES3_CON_FS_768000;
case 96000: return IEC958_AES3_CON_FS_96000;
case 176400: return IEC958_AES3_CON_FS_176400;
case 192000: return IEC958_AES3_CON_FS_192000;
default: return IEC958_AES3_CON_FS_NOTID;
}
}
static int try_open_device(struct ao *ao, const char *device, int open_mode)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
2013-04-07 19:34:09 +00:00
if (AF_FORMAT_IS_IEC61937(ao->format)) {
void *tmp = talloc_new(NULL);
2013-04-07 19:34:09 +00:00
/* to set the non-audio bit, use AES0=6 */
char *params = talloc_asprintf(tmp,
"AES0=%d,AES1=%d,AES2=0,AES3=%d",
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) {
2013-04-07 19:34:09 +00:00
/* 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);
2013-04-07 19:34:09 +00:00
} else {
/* a simple list of parameters: add it at the end of the list */
ac3_device = talloc_asprintf(tmp, "%s,%s", device, params);
2013-04-07 19:34:09 +00:00
}
int err = snd_pcm_open
(&p->alsa, ac3_device, SND_PCM_STREAM_PLAYBACK, open_mode);
talloc_free(tmp);
2013-04-07 19:34:09 +00:00
if (!err)
return 0;
}
return snd_pcm_open(&p->alsa, device, SND_PCM_STREAM_PLAYBACK, open_mode);
}
/*
open & setup audio device
2013-04-18 22:16:23 +00:00
return: 0=success -1=fail
2013-04-07 19:34:09 +00:00
*/
static int init(struct ao *ao)
{
int err;
snd_pcm_uframes_t chunk_size;
snd_pcm_uframes_t bufsize;
snd_pcm_uframes_t boundary;
struct priv *p = ao->priv;
if (!p->cfg_ni)
ao->format = af_fmt_from_planar(ao->format);
/* switch for spdif
* sets opening sequence for SPDIF
* sets also the playback and other switches 'on the fly'
* while opening the abstract alias for the spdif subdevice
* 'iec958'
*/
const char *device;
2013-04-18 22:16:23 +00:00
if (AF_FORMAT_IS_IEC61937(ao->format)) {
device = "iec958";
MP_VERBOSE(ao, "playing AC3/iec61937/iec958, %i channels\n",
ao->channels.num);
} else {
device = select_chmap(ao);
if (strcmp(device, "default") != 0 && (ao->format & AF_FORMAT_F)) {
// hack - use the converter plugin (why the heck?)
device = talloc_asprintf(ao, "plug:%s", device);
}
}
if (ao->device)
device = ao->device;
if (p->cfg_device && p->cfg_device[0])
device = p->cfg_device;
MP_VERBOSE(ao, "using device: %s\n", device);
MP_VERBOSE(ao, "using ALSA version: %s\n", snd_asoundlib_version());
int open_mode = p->cfg_block ? 0 : SND_PCM_NONBLOCK;
2013-04-07 21:03:31 +00:00
//modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
err = try_open_device(ao, device, open_mode);
2013-04-07 21:03:31 +00:00
if (err < 0) {
if (err != -EBUSY && !p->cfg_block) {
MP_WARN(ao, "Open in nonblock-mode "
2013-04-07 21:03:31 +00:00
"failed, trying to open in block-mode.\n");
err = try_open_device(ao, device, 0);
2013-04-07 19:34:09 +00:00
}
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Playback open error");
}
2013-04-07 19:34:09 +00:00
2013-04-18 22:24:26 +00:00
err = snd_pcm_nonblock(p->alsa, 0);
2013-04-07 21:03:31 +00:00
if (err < 0) {
MP_ERR(ao, "Error setting block-mode: %s.\n", snd_strerror(err));
2013-04-07 21:03:31 +00:00
} else {
MP_VERBOSE(ao, "pcm opened in blocking mode\n");
2013-04-07 21:03:31 +00:00
}
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
snd_pcm_hw_params_t *alsa_hwparams;
snd_pcm_sw_params_t *alsa_swparams;
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
snd_pcm_hw_params_alloca(&alsa_hwparams);
snd_pcm_sw_params_alloca(&alsa_swparams);
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
// setting hw-parameters
2013-04-18 22:24:26 +00:00
err = snd_pcm_hw_params_any(p->alsa, alsa_hwparams);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to get initial parameters");
audio: cleanup spdif format definitions Before this commit, there was AF_FORMAT_AC3 (the original spdif format, used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS and DTS-HD), which was handled as some sort of superset for AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used IEC61937-framing, but still was handled as something "separate". Technically, all of them are pretty similar, but may use different bitrates. Since digital passthrough pretends to be PCM (just with special headers that wrap digital packets), this is easily detectable by the higher samplerate or higher number of channels, so I don't know why you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs. AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is just a mess. Simplify this by handling all these formats the same way. AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3). All AOs just accept all spdif formats now - whether that works or not is not really clear (seems inconsistent due to earlier attempts to make DTS-HD work). But on the other hand, enabling spdif requires manual user interaction, so it doesn't matter much if initialization fails in slightly less graceful ways if it can't work at all. At a later point, we will support passthrough with ao_pulse. It seems the PulseAudio API wants to know the codec type (or maybe not - feeding it DTS while telling it it's AC3 works), add separate formats for each codecs. While this reminds of the earlier chaos, it's stricter, and most code just uses AF_FORMAT_IS_IEC61937(). Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to include special formats, so that it always describes the fundamental sample format type. This also ensures valid AF formats are never 0 (this was probably broken in one of the earlier commits from today).
2014-09-23 20:44:54 +00:00
if (AF_FORMAT_IS_IEC61937(ao->format)) {
if (ao->format == AF_FORMAT_S_MP3) {
p->alsa_fmt = SND_PCM_FORMAT_MPEG;
} else {
p->alsa_fmt = SND_PCM_FORMAT_S16;
}
} else {
p->alsa_fmt = find_alsa_format(ao->format);
}
if (p->alsa_fmt == SND_PCM_FORMAT_UNKNOWN) {
p->alsa_fmt = SND_PCM_FORMAT_S16;
ao->format = AF_FORMAT_S16;
}
2013-04-18 22:24:26 +00:00
err = snd_pcm_hw_params_test_format(p->alsa, alsa_hwparams, p->alsa_fmt);
2013-04-07 21:03:31 +00:00
if (err < 0) {
audio: cleanup spdif format definitions Before this commit, there was AF_FORMAT_AC3 (the original spdif format, used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS and DTS-HD), which was handled as some sort of superset for AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used IEC61937-framing, but still was handled as something "separate". Technically, all of them are pretty similar, but may use different bitrates. Since digital passthrough pretends to be PCM (just with special headers that wrap digital packets), this is easily detectable by the higher samplerate or higher number of channels, so I don't know why you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs. AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is just a mess. Simplify this by handling all these formats the same way. AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3). All AOs just accept all spdif formats now - whether that works or not is not really clear (seems inconsistent due to earlier attempts to make DTS-HD work). But on the other hand, enabling spdif requires manual user interaction, so it doesn't matter much if initialization fails in slightly less graceful ways if it can't work at all. At a later point, we will support passthrough with ao_pulse. It seems the PulseAudio API wants to know the codec type (or maybe not - feeding it DTS while telling it it's AC3 works), add separate formats for each codecs. While this reminds of the earlier chaos, it's stricter, and most code just uses AF_FORMAT_IS_IEC61937(). Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to include special formats, so that it always describes the fundamental sample format type. This also ensures valid AF formats are never 0 (this was probably broken in one of the earlier commits from today).
2014-09-23 20:44:54 +00:00
if (AF_FORMAT_IS_IEC61937(ao->format))
CHECK_ALSA_ERROR("Unable to set IEC61937 format");
MP_INFO(ao, "Format %s is not supported by hardware, trying default.\n",
af_fmt_to_str(ao->format));
p->alsa_fmt = SND_PCM_FORMAT_S16;
audio: cleanup spdif format definitions Before this commit, there was AF_FORMAT_AC3 (the original spdif format, used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS and DTS-HD), which was handled as some sort of superset for AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used IEC61937-framing, but still was handled as something "separate". Technically, all of them are pretty similar, but may use different bitrates. Since digital passthrough pretends to be PCM (just with special headers that wrap digital packets), this is easily detectable by the higher samplerate or higher number of channels, so I don't know why you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs. AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is just a mess. Simplify this by handling all these formats the same way. AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3). All AOs just accept all spdif formats now - whether that works or not is not really clear (seems inconsistent due to earlier attempts to make DTS-HD work). But on the other hand, enabling spdif requires manual user interaction, so it doesn't matter much if initialization fails in slightly less graceful ways if it can't work at all. At a later point, we will support passthrough with ao_pulse. It seems the PulseAudio API wants to know the codec type (or maybe not - feeding it DTS while telling it it's AC3 works), add separate formats for each codecs. While this reminds of the earlier chaos, it's stricter, and most code just uses AF_FORMAT_IS_IEC61937(). Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to include special formats, so that it always describes the fundamental sample format type. This also ensures valid AF formats are never 0 (this was probably broken in one of the earlier commits from today).
2014-09-23 20:44:54 +00:00
ao->format = AF_FORMAT_S16;
2013-04-07 21:03:31 +00:00
}
2013-04-18 22:24:26 +00:00
err = snd_pcm_hw_params_set_format(p->alsa, alsa_hwparams, p->alsa_fmt);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set format");
2013-04-07 19:34:09 +00:00
snd_pcm_access_t access = af_fmt_is_planar(ao->format)
? SND_PCM_ACCESS_RW_NONINTERLEAVED
: SND_PCM_ACCESS_RW_INTERLEAVED;
err = snd_pcm_hw_params_set_access(p->alsa, alsa_hwparams, access);
if (err < 0 && af_fmt_is_planar(ao->format)) {
ao->format = af_fmt_from_planar(ao->format);
access = SND_PCM_ACCESS_RW_INTERLEAVED;
err = snd_pcm_hw_params_set_access(p->alsa, alsa_hwparams, access);
}
CHECK_ALSA_ERROR("Unable to set access type");
2013-04-18 22:24:26 +00:00
int num_channels = ao->channels.num;
2013-04-07 21:03:31 +00:00
err = snd_pcm_hw_params_set_channels_near
2013-04-18 22:24:26 +00:00
(p->alsa, alsa_hwparams, &num_channels);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set channels");
2013-04-07 19:34:09 +00:00
if (num_channels != ao->channels.num) {
MP_ERR(ao, "Couldn't get requested number of channels.\n");
mp_chmap_from_channels_alsa(&ao->channels, num_channels);
}
2013-04-07 19:34:09 +00:00
// Some ALSA drivers have broken delay reporting, so disable the ALSA
// resampling plugin by default.
if (!p->cfg_resample) {
err = snd_pcm_hw_params_set_rate_resample(p->alsa, alsa_hwparams, 0);
CHECK_ALSA_ERROR("Unable to disable resampling");
}
2013-04-07 21:03:31 +00:00
err = snd_pcm_hw_params_set_rate_near
2013-04-18 22:24:26 +00:00
(p->alsa, alsa_hwparams, &ao->samplerate, NULL);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set samplerate-2");
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
err = snd_pcm_hw_params_set_buffer_time_near
2013-04-18 22:24:26 +00:00
(p->alsa, alsa_hwparams, &(unsigned int){BUFFER_TIME}, NULL);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set buffer time near");
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
err = snd_pcm_hw_params_set_periods_near
2013-04-18 22:24:26 +00:00
(p->alsa, alsa_hwparams, &(unsigned int){FRAGCOUNT}, NULL);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set periods");
2013-04-07 21:03:31 +00:00
/* finally install hardware parameters */
2013-04-18 22:24:26 +00:00
err = snd_pcm_hw_params(p->alsa, alsa_hwparams);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set hw-parameters");
2013-04-07 21:03:31 +00:00
// end setting hw-params
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
// gets buffersize for control
err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize);
CHECK_ALSA_ERROR("Unable to get buffersize");
p->buffersize = bufsize;
MP_VERBOSE(ao, "got buffersize=%i samples\n", p->buffersize);
2013-04-07 21:03:31 +00:00
err = snd_pcm_hw_params_get_period_size(alsa_hwparams, &chunk_size, NULL);
CHECK_ALSA_ERROR("Unable to get period size");
MP_VERBOSE(ao, "got period size %li\n", chunk_size);
p->outburst = chunk_size;
2013-04-07 21:03:31 +00:00
/* setting software parameters */
2013-04-18 22:24:26 +00:00
err = snd_pcm_sw_params_current(p->alsa, alsa_swparams);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to get sw-parameters");
2013-04-07 21:03:31 +00:00
err = snd_pcm_sw_params_get_boundary(alsa_swparams, &boundary);
CHECK_ALSA_ERROR("Unable to get boundary");
2013-04-07 21:03:31 +00:00
/* start playing when one period has been written */
err = snd_pcm_sw_params_set_start_threshold
2013-04-18 22:24:26 +00:00
(p->alsa, alsa_swparams, chunk_size);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set start threshold");
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
/* disable underrun reporting */
err = snd_pcm_sw_params_set_stop_threshold
2013-04-18 22:24:26 +00:00
(p->alsa, alsa_swparams, boundary);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set stop threshold");
2013-04-07 19:34:09 +00:00
2013-04-07 21:03:31 +00:00
/* play silence when there is an underrun */
err = snd_pcm_sw_params_set_silence_size
2013-04-18 22:24:26 +00:00
(p->alsa, alsa_swparams, boundary);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to set silence size");
2013-04-18 22:24:26 +00:00
err = snd_pcm_sw_params(p->alsa, alsa_swparams);
2013-04-07 21:03:31 +00:00
CHECK_ALSA_ERROR("Unable to get sw-parameters");
/* end setting sw-params */
2013-04-18 22:24:26 +00:00
p->can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams);
2013-04-07 21:03:31 +00:00
MP_VERBOSE(ao, "opened: %d Hz/%d channels/%d bps/%d samples buffer/%s\n",
ao->samplerate, ao->channels.num, af_fmt2bits(ao->format),
p->buffersize, snd_pcm_format_description(p->alsa_fmt));
2013-04-18 22:16:23 +00:00
return 0;
alsa_error:
uninit(ao);
2013-04-18 22:16:23 +00:00
return -1;
} // end init
/* close audio device */
static void uninit(struct ao *ao)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
2013-04-18 22:24:26 +00:00
if (p->alsa) {
2013-04-07 19:34:09 +00:00
int err;
2013-04-18 22:24:26 +00:00
err = snd_pcm_close(p->alsa);
CHECK_ALSA_ERROR("pcm close error");
MP_VERBOSE(ao, "uninit: pcm closed\n");
}
alsa_error:
p->alsa = NULL;
}
static void drain(struct ao *ao)
{
struct priv *p = ao->priv;
snd_pcm_drain(p->alsa);
}
2013-04-18 22:16:23 +00:00
static void audio_pause(struct ao *ao)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
int err;
2013-04-18 22:24:26 +00:00
if (p->can_pause) {
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_RUNNING) {
p->delay_before_pause = get_delay(ao);
err = snd_pcm_pause(p->alsa, 1);
CHECK_ALSA_ERROR("pcm pause error");
}
} else {
MP_VERBOSE(ao, "pause not supported by hardware\n");
2013-04-18 22:24:26 +00:00
if (snd_pcm_delay(p->alsa, &p->prepause_frames) < 0
|| p->prepause_frames < 0)
p->prepause_frames = 0;
p->delay_before_pause = p->prepause_frames / (float)ao->samplerate;
2013-04-18 22:24:26 +00:00
err = snd_pcm_drop(p->alsa);
CHECK_ALSA_ERROR("pcm drop error");
}
alsa_error: ;
}
2013-04-18 22:16:23 +00:00
static void audio_resume(struct ao *ao)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
int err;
2013-04-18 22:24:26 +00:00
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_SUSPENDED) {
MP_INFO(ao, "PCM in suspend mode, trying to resume.\n");
2013-04-18 22:24:26 +00:00
while ((err = snd_pcm_resume(p->alsa)) == -EAGAIN)
2013-04-07 19:34:09 +00:00
sleep(1);
}
if (p->can_pause) {
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_PAUSED) {
err = snd_pcm_pause(p->alsa, 0);
CHECK_ALSA_ERROR("pcm resume error");
}
} else {
MP_VERBOSE(ao, "resume not supported by hardware\n");
err = snd_pcm_prepare(p->alsa);
CHECK_ALSA_ERROR("pcm prepare error");
if (p->prepause_frames)
ao_play_silence(ao, p->prepause_frames);
}
alsa_error: ;
}
/* stop playing and empty buffers (for seeking/pause) */
2013-04-18 22:16:23 +00:00
static void reset(struct ao *ao)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
int err;
2013-04-18 22:24:26 +00:00
p->prepause_frames = 0;
p->delay_before_pause = 0;
err = snd_pcm_drop(p->alsa);
CHECK_ALSA_ERROR("pcm prepare error");
2013-04-18 22:24:26 +00:00
err = snd_pcm_prepare(p->alsa);
CHECK_ALSA_ERROR("pcm prepare error");
alsa_error: ;
}
static int play(struct ao *ao, void **data, int samples, int flags)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
2013-04-07 19:34:09 +00:00
snd_pcm_sframes_t res = 0;
if (!(flags & AOPLAY_FINAL_CHUNK))
samples = samples / p->outburst * p->outburst;
2013-04-07 19:34:09 +00:00
if (samples == 0)
2013-04-07 19:34:09 +00:00
return 0;
do {
if (af_fmt_is_planar(ao->format)) {
res = snd_pcm_writen(p->alsa, data, samples);
} else {
res = snd_pcm_writei(p->alsa, data[0], samples);
}
2013-04-07 19:34:09 +00:00
if (res == -EINTR) {
/* nothing to do */
res = 0;
} else if (res == -ESTRPIPE) { /* suspend */
audio_resume(ao);
} else if (res < 0) {
MP_ERR(ao, "Write error: %s\n", snd_strerror(res));
2013-04-18 22:24:26 +00:00
res = snd_pcm_prepare(p->alsa);
int err = res;
CHECK_ALSA_ERROR("pcm prepare error");
2013-04-07 19:34:09 +00:00
res = 0;
}
} while (res == 0);
return res < 0 ? -1 : res;
alsa_error:
return -1;
}
2013-04-18 22:16:23 +00:00
static int get_space(struct ao *ao)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
snd_pcm_status_t *status;
int err;
snd_pcm_status_alloca(&status);
2013-04-18 22:24:26 +00:00
err = snd_pcm_status(p->alsa, status);
CHECK_ALSA_ERROR("cannot get pcm status");
unsigned space = snd_pcm_status_get_avail(status);
if (space > p->buffersize) // Buffer underrun?
space = p->buffersize;
return space / p->outburst * p->outburst;
alsa_error:
return 0;
}
/* delay in seconds between first and last sample in buffer */
2013-04-18 22:16:23 +00:00
static float get_delay(struct ao *ao)
{
2013-04-18 22:24:26 +00:00
struct priv *p = ao->priv;
snd_pcm_sframes_t delay;
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_PAUSED)
return p->delay_before_pause;
if (snd_pcm_delay(p->alsa, &delay) < 0)
2013-04-07 19:34:09 +00:00
return 0;
if (delay < 0) {
/* underrun - move the application pointer forward to catch up */
snd_pcm_forward(p->alsa, -delay);
delay = 0;
}
return (float)delay / (float)ao->samplerate;
}
2013-04-18 22:16:23 +00:00
#define MAX_POLL_FDS 20
static int audio_wait(struct ao *ao, pthread_mutex_t *lock)
{
struct priv *p = ao->priv;
int err;
int num_fds = snd_pcm_poll_descriptors_count(p->alsa);
if (num_fds <= 0 || num_fds >= MAX_POLL_FDS)
goto alsa_error;
struct pollfd fds[MAX_POLL_FDS];
err = snd_pcm_poll_descriptors(p->alsa, fds, num_fds);
CHECK_ALSA_ERROR("cannot get pollfds");
while (1) {
int r = ao_wait_poll(ao, fds, num_fds, lock);
if (r)
return r;
unsigned short revents;
snd_pcm_poll_descriptors_revents(p->alsa, fds, num_fds, &revents);
CHECK_ALSA_ERROR("cannot read poll events");
if (revents & POLLERR)
return -1;
if (revents & POLLOUT)
return 0;
}
return 0;
alsa_error:
return -1;
}
static void list_devs(struct ao *ao, struct ao_device_list *list)
{
void **hints;
if (snd_device_name_hint(-1, "pcm", &hints) < 0)
return;
for (int n = 0; hints[n]; n++) {
char *name = snd_device_name_get_hint(hints[n], "NAME");
char *desc = snd_device_name_get_hint(hints[n], "DESC");
char *io = snd_device_name_get_hint(hints[n], "IOID");
if (io && strcmp(io, "Output") != 0)
continue;
char desc2[1024];
snprintf(desc2, sizeof(desc2), "%s", desc ? desc : "");
for (int i = 0; desc2[i]; i++) {
if (desc2[i] == '\n')
desc2[i] = '/';
}
ao_device_list_add(list, ao, &(struct ao_device_desc){name, desc2});
}
snd_device_name_free_hint(hints);
}
#define OPT_BASE_STRUCT struct priv
2013-04-18 22:16:23 +00:00
const struct ao_driver audio_out_alsa = {
.description = "ALSA-0.9.x-1.x audio output",
.name = "alsa",
2013-04-18 22:16:23 +00:00
.init = init,
.uninit = uninit,
.control = control,
.get_space = get_space,
.play = play,
.get_delay = get_delay,
.pause = audio_pause,
.resume = audio_resume,
.reset = reset,
.drain = drain,
.wait = audio_wait,
.wakeup = ao_wakeup_poll,
.list_devs = list_devs,
.priv_size = sizeof(struct priv),
.priv_defaults = &(const struct priv) {
.cfg_block = 1,
.cfg_mixer_device = "default",
.cfg_mixer_name = "Master",
.cfg_mixer_index = 0,
.cfg_ni = 0,
},
.options = (const struct m_option[]) {
OPT_STRING("device", cfg_device, 0),
OPT_FLAG("resample", cfg_resample, 0),
OPT_FLAG("block", cfg_block, 0),
OPT_STRING("mixer-device", cfg_mixer_device, 0),
OPT_STRING("mixer-name", cfg_mixer_name, 0),
OPT_INTRANGE("mixer-index", cfg_mixer_index, 0, 0, 99),
OPT_FLAG("non-interleaved", cfg_ni, 0),
{0}
},
2013-04-18 22:16:23 +00:00
};