2004-05-12 22:56:55 +00:00
|
|
|
/*
|
2009-01-26 15:06:44 +00:00
|
|
|
* ALSA 0.9.x-1.x audio output driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Alex Beregszaszi
|
2013-10-23 17:07:27 +00:00
|
|
|
* Zsolt Barat <joy@streamminister.de>
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdlib.h>
|
2006-02-27 10:09:05 +00:00
|
|
|
#include <stdarg.h>
|
2007-02-09 19:49:31 +00:00
|
|
|
#include <ctype.h>
|
2004-05-12 22:56:55 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "config.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/m_option.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
#define ALSA_PCM_NEW_HW_PARAMS_API
|
|
|
|
#define ALSA_PCM_NEW_SW_PARAMS_API
|
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
2004-05-12 22:56:55 +00:00
|
|
|
|
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;
|
2013-11-10 22:39:08 +00:00
|
|
|
int buffersize; // in frames
|
|
|
|
int outburst; // in frames
|
2013-07-21 19:51:51 +00:00
|
|
|
|
|
|
|
int cfg_block;
|
|
|
|
char *cfg_device;
|
2013-07-21 21:27:09 +00:00
|
|
|
char *cfg_mixer_device;
|
|
|
|
char *cfg_mixer_name;
|
|
|
|
int cfg_mixer_index;
|
ao_alsa: disable ALSA resampling by default again
This partially reverts commit 7d152965. It turns out that at least some
ALSA drivers (at least snd-hda-intel) report incorrect audio delay with
non-native sample rates, even if the sample rate is only very slightly
different from the native one.
For example, 48000Hz is fine on my hda-intel system, while both 8000Hz
and 47999Hz lead to a delay off by 40ms (according to mpv's A/V
difference display), which suggests that something in ALSA is
calculating the delay using the wrong sample rate.
As an additional problem, with ALSA resampling enabled, using
48001Hz/float/2ch fails, while 49000Hz/float/2ch or 48001Hz/s16/2ch
work. With resampling disabled, all these cases work obviously, because
our own resampler doesn't just refuse any of these formats.
Since some people want to use the ALSA resampler (because it's highly
configurable, supports multiple backends, etc.), we still allow enabling
ALSA resampling with an ao_alsa suboption.
2013-11-29 14:43:39 +00:00
|
|
|
int cfg_resample;
|
2013-04-18 22:24:26 +00:00
|
|
|
};
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2014-03-10 00:28:27 +00:00
|
|
|
#define BUFFER_TIME 250000 // 250ms
|
2009-12-29 14:37:27 +00:00
|
|
|
#define FRAGCOUNT 16
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2013-04-07 19:34:09 +00:00
|
|
|
#define CHECK_ALSA_ERROR(message) \
|
|
|
|
do { \
|
|
|
|
if (err < 0) { \
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "%s: %s\n", (message), snd_strerror(err)); \
|
2013-04-07 19:34:09 +00:00
|
|
|
goto alsa_error; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2013-04-18 22:16:23 +00:00
|
|
|
static float get_delay(struct ao *ao);
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao);
|
2013-04-18 22:16:23 +00:00
|
|
|
|
2004-05-12 22:56:55 +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)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2013-07-21 21:27:09 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-04-07 19:34:09 +00:00
|
|
|
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:
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
if (AF_FORMAT_IS_IEC61937(ao->format))
|
2014-03-10 00:18:10 +00:00
|
|
|
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
|
2013-07-21 21:27:09 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-07-21 21:27:09 +00:00
|
|
|
err = snd_mixer_attach(handle, p->cfg_mixer_device);
|
2013-04-07 19:34:09 +00:00
|
|
|
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);
|
2013-04-07 19:34:09 +00:00
|
|
|
CHECK_ALSA_ERROR("Mixer load error");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
elem = snd_mixer_find_selem(handle, sid);
|
|
|
|
if (!elem) {
|
2013-08-22 21:12:35 +00:00
|
|
|
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));
|
2013-04-07 19:34:09 +00:00
|
|
|
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
|
2013-04-07 19:34:09 +00:00
|
|
|
err = snd_mixer_selem_set_playback_volume
|
|
|
|
(elem, SND_MIXER_SCHN_FRONT_LEFT, set_vol);
|
|
|
|
CHECK_ALSA_ERROR("Error setting left channel");
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_DBG(ao, "left=%li, ", set_vol);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
set_vol = vol->right / f_multi + pmin + 0.5;
|
|
|
|
|
2013-04-07 19:34:09 +00:00
|
|
|
err = snd_mixer_selem_set_playback_volume
|
|
|
|
(elem, SND_MIXER_SCHN_FRONT_RIGHT, set_vol);
|
|
|
|
CHECK_ALSA_ERROR("Error setting right channel");
|
2013-08-22 21:12:35 +00:00
|
|
|
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;
|
2013-04-07 19:34:09 +00:00
|
|
|
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;
|
2013-04-07 19:34:09 +00:00
|
|
|
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;
|
2013-08-22 21:12:35 +00:00
|
|
|
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))
|
2013-04-07 19:34:09 +00:00
|
|
|
goto alsa_error;
|
2013-04-07 19:34:09 +00:00
|
|
|
if (!snd_mixer_selem_has_playback_switch_joined(elem)) {
|
2013-04-07 19:34:09 +00:00
|
|
|
snd_mixer_selem_set_playback_switch
|
|
|
|
(elem, SND_MIXER_SCHN_FRONT_RIGHT, !*mute);
|
2013-04-07 19:34:09 +00:00
|
|
|
}
|
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))
|
2013-04-07 19:34:09 +00:00
|
|
|
goto alsa_error;
|
2013-04-07 19:34:09 +00:00
|
|
|
int tmp = 1;
|
2013-04-07 19:34:09 +00:00
|
|
|
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)) {
|
2013-04-07 19:34:09 +00:00
|
|
|
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;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-04-07 19:34:09 +00:00
|
|
|
} //end switch
|
|
|
|
return CONTROL_UNKNOWN;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
alsa_error:
|
|
|
|
if (handle)
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 21:08:10 +00:00
|
|
|
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)
|
|
|
|
{
|
2013-11-10 22:39:08 +00:00
|
|
|
af_format = af_fmt_from_planar(af_format);
|
2013-04-07 21:08:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-07 22:51:18 +00:00
|
|
|
// 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"},
|
2013-05-13 16:17:27 +00:00
|
|
|
{"surround40", "fl-fr-bl-br"},
|
2013-04-07 22:51:18 +00:00
|
|
|
{"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"},
|
|
|
|
};
|
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
#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)
|
2013-04-07 22:51:18 +00:00
|
|
|
{
|
2013-05-09 16:06:26 +00:00
|
|
|
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]))
|
2013-04-07 22:51:18 +00:00
|
|
|
return device_channel_layouts[n][0];
|
|
|
|
}
|
2013-05-09 16:06:26 +00:00
|
|
|
|
|
|
|
char *name = mp_chmap_to_str(&ao->channels);
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "channel layout %s (%d ch) not supported.\n",
|
|
|
|
name, ao->channels.num);
|
2013-05-09 16:06:26 +00:00
|
|
|
talloc_free(name);
|
|
|
|
return "default";
|
2013-04-07 22:51:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-09 00:30:02 +00:00
|
|
|
static int map_iec958_srate(int srate)
|
|
|
|
{
|
|
|
|
switch (srate) {
|
2013-11-09 22:22:35 +00:00
|
|
|
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;
|
2013-11-09 00:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-09 00:27:10 +00:00
|
|
|
static int try_open_device(struct ao *ao, const char *device, int open_mode)
|
2006-09-18 16:58:21 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2013-11-09 00:27:10 +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 */
|
2013-11-09 00:30:02 +00:00
|
|
|
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));
|
2013-11-09 00:27:10 +00:00
|
|
|
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 */
|
2013-11-09 00:27:10 +00:00
|
|
|
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 {
|
2013-11-09 00:27:10 +00:00
|
|
|
/* 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
|
|
|
}
|
2013-11-09 00:27:10 +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;
|
2006-09-18 16:58:21 +00:00
|
|
|
}
|
2013-11-09 00:27:10 +00:00
|
|
|
|
|
|
|
return snd_pcm_open(&p->alsa, device, SND_PCM_STREAM_PLAYBACK, open_mode);
|
2006-09-18 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
*/
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
int err;
|
2009-08-24 07:32:25 +00:00
|
|
|
snd_pcm_uframes_t chunk_size;
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_pcm_uframes_t bufsize;
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_pcm_uframes_t boundary;
|
2005-01-03 14:23:18 +00:00
|
|
|
|
2013-07-21 19:51:51 +00:00
|
|
|
struct priv *p = ao->priv;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2005-01-03 14:23:18 +00:00
|
|
|
/* 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'
|
|
|
|
*/
|
2013-07-21 19:51:51 +00:00
|
|
|
const char *device;
|
2013-04-18 22:16:23 +00:00
|
|
|
if (AF_FORMAT_IS_IEC61937(ao->format)) {
|
2013-07-21 19:51:51 +00:00
|
|
|
device = "iec958";
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "playing AC3/iec61937/iec958, %i channels\n",
|
|
|
|
ao->channels.num);
|
2013-04-07 19:34:09 +00:00
|
|
|
} else {
|
2013-07-21 19:51:51 +00:00
|
|
|
device = select_chmap(ao);
|
2013-11-30 17:55:39 +00:00
|
|
|
if (strcmp(device, "default") != 0 && (ao->format & AF_FORMAT_F)) {
|
2013-04-07 22:51:18 +00:00
|
|
|
// hack - use the converter plugin (why the heck?)
|
2013-07-21 19:51:51 +00:00
|
|
|
device = talloc_asprintf(ao, "plug:%s", device);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2013-04-07 19:34:09 +00:00
|
|
|
}
|
2013-07-21 19:51:51 +00:00
|
|
|
if (p->cfg_device && p->cfg_device[0])
|
|
|
|
device = p->cfg_device;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "using device: %s\n", device);
|
|
|
|
MP_VERBOSE(ao, "using ALSA version: %s\n", snd_asoundlib_version());
|
2013-05-25 13:07:05 +00:00
|
|
|
|
2013-07-21 19:51:51 +00:00
|
|
|
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
|
2013-11-09 00:27:10 +00:00
|
|
|
err = try_open_device(ao, device, open_mode);
|
2013-04-07 21:03:31 +00:00
|
|
|
if (err < 0) {
|
2013-07-21 19:51:51 +00:00
|
|
|
if (err != -EBUSY && !p->cfg_block) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_WARN(ao, "Open in nonblock-mode "
|
2013-04-07 21:03:31 +00:00
|
|
|
"failed, trying to open in block-mode.\n");
|
2013-11-09 00:27:10 +00:00
|
|
|
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) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "Error setting block-mode: %s.\n", snd_strerror(err));
|
2013-04-07 21:03:31 +00:00
|
|
|
} else {
|
2013-08-22 21:12:35 +00:00
|
|
|
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");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2013-05-10 12:02:04 +00:00
|
|
|
p->alsa_fmt = find_alsa_format(ao->format);
|
|
|
|
if (p->alsa_fmt == SND_PCM_FORMAT_UNKNOWN) {
|
|
|
|
p->alsa_fmt = SND_PCM_FORMAT_S16;
|
2013-11-15 20:25:05 +00:00
|
|
|
ao->format = AF_FORMAT_S16;
|
2013-05-10 12:02:04 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_INFO(ao, "Format %s is not supported by hardware, trying default.\n",
|
2013-11-07 21:12:36 +00:00
|
|
|
af_fmt_to_str(ao->format));
|
2013-04-18 22:24:26 +00:00
|
|
|
p->alsa_fmt = SND_PCM_FORMAT_S16_LE;
|
|
|
|
if (AF_FORMAT_IS_AC3(ao->format))
|
|
|
|
ao->format = AF_FORMAT_AC3_LE;
|
|
|
|
else if (AF_FORMAT_IS_IEC61937(ao->format))
|
|
|
|
ao->format = AF_FORMAT_IEC61937_LE;
|
2013-04-07 21:03:31 +00:00
|
|
|
else
|
2013-04-18 22:24:26 +00:00
|
|
|
ao->format = AF_FORMAT_S16_LE;
|
2013-04-07 21:03:31 +00:00
|
|
|
}
|
2013-04-07 19:34:09 +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
|
|
|
|
2013-11-10 22:39:08 +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);
|
2013-11-14 20:19:04 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-11-10 22:39:08 +00:00
|
|
|
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
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
if (num_channels != ao->channels.num) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "Couldn't get requested number of channels.\n");
|
2013-05-09 16:06:26 +00:00
|
|
|
mp_chmap_from_channels_alsa(&ao->channels, num_channels);
|
|
|
|
}
|
2013-04-07 19:34:09 +00:00
|
|
|
|
ao_alsa: disable ALSA resampling by default again
This partially reverts commit 7d152965. It turns out that at least some
ALSA drivers (at least snd-hda-intel) report incorrect audio delay with
non-native sample rates, even if the sample rate is only very slightly
different from the native one.
For example, 48000Hz is fine on my hda-intel system, while both 8000Hz
and 47999Hz lead to a delay off by 40ms (according to mpv's A/V
difference display), which suggests that something in ALSA is
calculating the delay using the wrong sample rate.
As an additional problem, with ALSA resampling enabled, using
48001Hz/float/2ch fails, while 49000Hz/float/2ch or 48001Hz/s16/2ch
work. With resampling disabled, all these cases work obviously, because
our own resampler doesn't just refuse any of these formats.
Since some people want to use the ALSA resampler (because it's highly
configurable, supports multiple backends, etc.), we still allow enabling
ALSA resampling with an ao_alsa suboption.
2013-11-29 14:43:39 +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 19:34:09 +00:00
|
|
|
|
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 19:34:09 +00:00
|
|
|
|
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");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2013-11-10 22:39:08 +00:00
|
|
|
p->buffersize = bufsize;
|
|
|
|
MP_VERBOSE(ao, "got buffersize=%i samples\n", p->buffersize);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
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");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "got period size %li\n", chunk_size);
|
2013-11-10 22:39:08 +00:00
|
|
|
p->outburst = chunk_size;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
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 19:34:09 +00:00
|
|
|
|
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 19:34:09 +00:00
|
|
|
|
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
|
|
|
|
2013-11-10 22:39:08 +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),
|
2013-08-22 21:12:35 +00:00
|
|
|
p->buffersize, snd_pcm_format_description(p->alsa_fmt));
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2013-04-18 22:16:23 +00:00
|
|
|
return 0;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
alsa_error:
|
2014-03-08 23:49:39 +00:00
|
|
|
uninit(ao);
|
2013-04-18 22:16:23 +00:00
|
|
|
return -1;
|
2004-05-12 22:56:55 +00:00
|
|
|
} // end init
|
|
|
|
|
|
|
|
|
|
|
|
/* close audio device */
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
if (p->alsa) {
|
2013-04-07 19:34:09 +00:00
|
|
|
int err;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
err = snd_pcm_close(p->alsa);
|
2013-04-07 19:34:09 +00:00
|
|
|
CHECK_ALSA_ERROR("pcm close error");
|
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "uninit: pcm closed\n");
|
2013-04-07 19:34:09 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 13:07:05 +00:00
|
|
|
alsa_error:
|
|
|
|
p->alsa = NULL;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
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)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2004-05-12 22:56:55 +00:00
|
|
|
int err;
|
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
if (p->can_pause) {
|
2014-03-09 20:32:00 +00:00
|
|
|
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");
|
|
|
|
}
|
2004-05-12 22:56:55 +00:00
|
|
|
} else {
|
2013-08-22 21:12:35 +00:00
|
|
|
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;
|
2008-12-08 20:21:23 +00:00
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
err = snd_pcm_drop(p->alsa);
|
2013-04-07 19:34:09 +00:00
|
|
|
CHECK_ALSA_ERROR("pcm drop error");
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
alsa_error: ;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 22:16:23 +00:00
|
|
|
static void audio_resume(struct ao *ao)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2004-05-12 22:56:55 +00:00
|
|
|
int err;
|
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_SUSPENDED) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_INFO(ao, "PCM in suspend mode, trying to resume.\n");
|
2014-01-02 15:09:27 +00:00
|
|
|
|
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);
|
2014-01-02 17:44:49 +00:00
|
|
|
}
|
2014-01-02 15:09:27 +00:00
|
|
|
|
2014-01-02 17:44:49 +00:00
|
|
|
if (p->can_pause) {
|
2014-03-09 20:32:00 +00:00
|
|
|
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_PAUSED) {
|
|
|
|
err = snd_pcm_pause(p->alsa, 0);
|
|
|
|
CHECK_ALSA_ERROR("pcm resume error");
|
|
|
|
}
|
2014-01-02 17:44:49 +00:00
|
|
|
} 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);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
alsa_error: ;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stop playing and empty buffers (for seeking/pause) */
|
2013-04-18 22:16:23 +00:00
|
|
|
static void reset(struct ao *ao)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2004-05-12 22:56:55 +00:00
|
|
|
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);
|
2013-04-07 19:34:09 +00:00
|
|
|
CHECK_ALSA_ERROR("pcm prepare error");
|
2013-04-18 22:24:26 +00:00
|
|
|
err = snd_pcm_prepare(p->alsa);
|
2013-04-07 19:34:09 +00:00
|
|
|
CHECK_ALSA_ERROR("pcm prepare error");
|
|
|
|
|
|
|
|
alsa_error: ;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 22:24:21 +00:00
|
|
|
static int play(struct ao *ao, void **data, int samples, int flags)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
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))
|
2013-11-10 22:39:08 +00:00
|
|
|
samples = samples / p->outburst * p->outburst;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2013-11-10 22:39:08 +00:00
|
|
|
if (samples == 0)
|
2013-04-07 19:34:09 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
do {
|
2013-11-10 22:39:08 +00:00
|
|
|
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 */
|
2014-01-02 15:09:27 +00:00
|
|
|
audio_resume(ao);
|
|
|
|
} else if (res < 0) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "Write error: %s\n", snd_strerror(res));
|
2013-04-18 22:24:26 +00:00
|
|
|
res = snd_pcm_prepare(p->alsa);
|
2013-04-07 19:34:09 +00:00
|
|
|
int err = res;
|
|
|
|
CHECK_ALSA_ERROR("pcm prepare error");
|
2013-04-07 19:34:09 +00:00
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
} while (res == 0);
|
|
|
|
|
2013-11-10 22:24:21 +00:00
|
|
|
return res < 0 ? -1 : res;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
alsa_error:
|
2013-10-30 20:11:28 +00:00
|
|
|
return -1;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 22:16:23 +00:00
|
|
|
static int get_space(struct ao *ao)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_pcm_status_t *status;
|
2013-04-07 19:34:09 +00:00
|
|
|
int err;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-07-02 04:30:46 +00:00
|
|
|
snd_pcm_status_alloca(&status);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
err = snd_pcm_status(p->alsa, status);
|
2013-04-07 19:34:09 +00:00
|
|
|
CHECK_ALSA_ERROR("cannot get pcm status");
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-11-10 22:39:08 +00:00
|
|
|
unsigned space = snd_pcm_status_get_avail(status);
|
2013-06-16 17:15:32 +00:00
|
|
|
if (space > p->buffersize) // Buffer underrun?
|
|
|
|
space = p->buffersize;
|
2013-11-10 22:39:08 +00:00
|
|
|
return space;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
alsa_error:
|
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-11-30 17:56:44 +00:00
|
|
|
snd_pcm_sframes_t delay;
|
2013-01-06 18:28:08 +00:00
|
|
|
|
2013-11-30 17:56:44 +00:00
|
|
|
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_PAUSED)
|
|
|
|
return p->delay_before_pause;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-11-30 17:56:44 +00:00
|
|
|
if (snd_pcm_delay(p->alsa, &delay) < 0)
|
2013-04-07 19:34:09 +00:00
|
|
|
return 0;
|
2013-11-30 17:56:44 +00:00
|
|
|
|
|
|
|
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;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2013-04-18 22:16:23 +00:00
|
|
|
|
2013-07-21 19:51:51 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2013-04-18 22:16:23 +00:00
|
|
|
const struct ao_driver audio_out_alsa = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.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,
|
2014-03-08 23:49:39 +00:00
|
|
|
.drain = drain,
|
2013-07-21 19:51:51 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
2013-07-21 21:27:09 +00:00
|
|
|
.priv_defaults = &(const struct priv) {
|
|
|
|
.cfg_block = 1,
|
|
|
|
.cfg_mixer_device = "default",
|
|
|
|
.cfg_mixer_name = "Master",
|
|
|
|
.cfg_mixer_index = 0,
|
|
|
|
},
|
2013-07-21 19:51:51 +00:00
|
|
|
.options = (const struct m_option[]) {
|
ao_alsa: disable ALSA resampling by default again
This partially reverts commit 7d152965. It turns out that at least some
ALSA drivers (at least snd-hda-intel) report incorrect audio delay with
non-native sample rates, even if the sample rate is only very slightly
different from the native one.
For example, 48000Hz is fine on my hda-intel system, while both 8000Hz
and 47999Hz lead to a delay off by 40ms (according to mpv's A/V
difference display), which suggests that something in ALSA is
calculating the delay using the wrong sample rate.
As an additional problem, with ALSA resampling enabled, using
48001Hz/float/2ch fails, while 49000Hz/float/2ch or 48001Hz/s16/2ch
work. With resampling disabled, all these cases work obviously, because
our own resampler doesn't just refuse any of these formats.
Since some people want to use the ALSA resampler (because it's highly
configurable, supports multiple backends, etc.), we still allow enabling
ALSA resampling with an ao_alsa suboption.
2013-11-29 14:43:39 +00:00
|
|
|
OPT_FLAG("resample", cfg_resample, 0),
|
2013-07-21 19:51:51 +00:00
|
|
|
OPT_STRING("device", cfg_device, 0),
|
|
|
|
OPT_FLAG("block", cfg_block, 0),
|
2013-07-21 21:27:09 +00:00
|
|
|
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),
|
2013-07-21 19:51:51 +00:00
|
|
|
{0}
|
|
|
|
},
|
2013-04-18 22:16:23 +00:00
|
|
|
};
|