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.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
2017-11-23 15:24:02 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-01-26 15:06:44 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-11-23 15:24:02 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
2017-11-23 15:24:02 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-26 15:06:44 +00:00
|
|
|
*/
|
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>
|
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"
|
2016-09-02 18:07:25 +00:00
|
|
|
#include "options/m_config.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-09-23 19:04:37 +00:00
|
|
|
#include "osdep/endian.h"
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
2018-01-17 11:10:41 +00:00
|
|
|
#if defined(SND_CHMAP_API_VERSION) && SND_CHMAP_API_VERSION >= (1 << 16)
|
|
|
|
#define HAVE_CHMAP_API 1
|
|
|
|
#else
|
|
|
|
#define HAVE_CHMAP_API 0
|
|
|
|
#endif
|
2014-11-24 18:40:24 +00:00
|
|
|
|
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
|
|
|
|
2016-09-02 18:07:25 +00:00
|
|
|
struct ao_alsa_opts {
|
|
|
|
char *mixer_device;
|
|
|
|
char *mixer_name;
|
|
|
|
int mixer_index;
|
|
|
|
int resample;
|
|
|
|
int ni;
|
|
|
|
int ignore_chmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct ao_alsa_opts
|
2016-09-05 19:04:17 +00:00
|
|
|
static const struct m_sub_options ao_alsa_conf = {
|
2016-09-02 18:07:25 +00:00
|
|
|
.opts = (const struct m_option[]) {
|
|
|
|
OPT_FLAG("alsa-resample", resample, 0),
|
|
|
|
OPT_STRING("alsa-mixer-device", mixer_device, 0),
|
|
|
|
OPT_STRING("alsa-mixer-name", mixer_name, 0),
|
|
|
|
OPT_INTRANGE("alsa-mixer-index", mixer_index, 0, 0, 99),
|
|
|
|
OPT_FLAG("alsa-non-interleaved", ni, 0),
|
|
|
|
OPT_FLAG("alsa-ignore-chmap", ignore_chmap, 0),
|
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.defaults = &(const struct ao_alsa_opts) {
|
|
|
|
.mixer_device = "default",
|
|
|
|
.mixer_name = "Master",
|
|
|
|
.mixer_index = 0,
|
|
|
|
.ni = 0,
|
|
|
|
},
|
|
|
|
.size = sizeof(struct ao_alsa_opts),
|
|
|
|
};
|
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv {
|
|
|
|
snd_pcm_t *alsa;
|
2015-09-28 15:29:47 +00:00
|
|
|
bool device_lost;
|
2013-04-18 22:24:26 +00:00
|
|
|
snd_pcm_format_t alsa_fmt;
|
2015-11-02 23:00:02 +00:00
|
|
|
bool can_pause;
|
2016-08-09 14:52:33 +00:00
|
|
|
bool paused;
|
2013-04-18 22:24:26 +00:00
|
|
|
snd_pcm_sframes_t prepause_frames;
|
2014-11-09 10:45:04 +00:00
|
|
|
double delay_before_pause;
|
2015-11-02 23:00:02 +00:00
|
|
|
snd_pcm_uframes_t buffersize;
|
|
|
|
snd_pcm_uframes_t outburst;
|
2013-07-21 19:51:51 +00:00
|
|
|
|
2016-04-28 11:31:13 +00:00
|
|
|
snd_output_t *output;
|
|
|
|
|
2017-07-07 15:37:23 +00:00
|
|
|
struct ao_convert_fmt convert;
|
|
|
|
|
2016-09-02 18:07:25 +00:00
|
|
|
struct ao_alsa_opts *opts;
|
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)
|
|
|
|
|
2014-10-31 00:09:53 +00:00
|
|
|
#define CHECK_ALSA_WARN(message) \
|
|
|
|
do { \
|
|
|
|
if (err < 0) \
|
|
|
|
MP_WARN(ao, "%s: %s\n", (message), snd_strerror(err)); \
|
|
|
|
} while (0)
|
|
|
|
|
2015-09-28 15:29:47 +00:00
|
|
|
// Common code for handling ENODEV, which happens if a device gets "lost", and
|
|
|
|
// can't be used anymore. Returns true if alsa_err is not ENODEV.
|
|
|
|
static bool check_device_present(struct ao *ao, int alsa_err)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
if (alsa_err != -ENODEV)
|
|
|
|
return true;
|
|
|
|
if (!p->device_lost) {
|
|
|
|
MP_WARN(ao, "Device lost, trying to recover...\n");
|
|
|
|
ao_request_reload(ao);
|
|
|
|
p->device_lost = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2015-06-26 21:06:37 +00:00
|
|
|
if (!af_fmt_is_pcm(ao->format))
|
2014-03-10 00:18:10 +00:00
|
|
|
return CONTROL_FALSE;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
snd_mixer_selem_id_alloca(&sid);
|
|
|
|
|
2016-09-02 18:07:25 +00:00
|
|
|
snd_mixer_selem_id_set_index(sid, p->opts->mixer_index);
|
|
|
|
snd_mixer_selem_id_set_name(sid, p->opts->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
|
|
|
|
2016-09-02 18:07:25 +00:00
|
|
|
err = snd_mixer_attach(handle, p->opts->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;
|
|
|
|
|
2017-11-23 15:24:02 +00:00
|
|
|
err = snd_mixer_selem_set_playback_volume(elem, 0, set_vol);
|
2013-04-07 19:34:09 +00:00
|
|
|
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;
|
|
|
|
|
2017-11-23 15:24:02 +00:00
|
|
|
err = snd_mixer_selem_set_playback_volume(elem, 1, set_vol);
|
2013-04-07 19:34:09 +00:00
|
|
|
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",
|
2014-11-25 10:10:44 +00:00
|
|
|
set_vol, pmin, pmax, f_multi);
|
2013-04-07 19:34:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AOCONTROL_GET_VOLUME: {
|
|
|
|
ao_control_vol_t *vol = arg;
|
2017-11-23 15:24:02 +00:00
|
|
|
snd_mixer_selem_get_playback_volume(elem, 0, &get_vol);
|
2013-04-07 19:34:09 +00:00
|
|
|
vol->left = (get_vol - pmin) * f_multi;
|
2017-11-23 15:24:02 +00:00
|
|
|
snd_mixer_selem_get_playback_volume(elem, 1, &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)) {
|
2017-11-23 15:24:02 +00:00
|
|
|
snd_mixer_selem_set_playback_switch(elem, 1, !*mute);
|
2013-04-07 19:34:09 +00:00
|
|
|
}
|
2017-11-23 15:24:02 +00:00
|
|
|
snd_mixer_selem_set_playback_switch(elem, 0, !*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;
|
2017-11-23 15:24:02 +00:00
|
|
|
snd_mixer_selem_get_playback_switch(elem, 0, &tmp);
|
2013-04-07 19:34:09 +00:00
|
|
|
*mute = !tmp;
|
|
|
|
if (!snd_mixer_selem_has_playback_switch_joined(elem)) {
|
2017-11-23 15:24:02 +00:00
|
|
|
snd_mixer_selem_get_playback_switch(elem, 1, &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
|
|
|
}
|
|
|
|
|
2017-07-07 15:37:23 +00:00
|
|
|
struct alsa_fmt {
|
|
|
|
int mp_format;
|
|
|
|
int alsa_format;
|
|
|
|
int bits; // alsa format full sample size (optional)
|
|
|
|
int pad_msb; // how many MSB bits are 0 (optional)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Entries that have the same mp_format must be:
|
|
|
|
// 1. consecutive
|
|
|
|
// 2. sorted by preferred format (worst comes last)
|
|
|
|
static const struct alsa_fmt mp_alsa_formats[] = {
|
2013-04-07 21:08:10 +00:00
|
|
|
{AF_FORMAT_U8, SND_PCM_FORMAT_U8},
|
2014-09-23 19:04:37 +00:00
|
|
|
{AF_FORMAT_S16, SND_PCM_FORMAT_S16},
|
|
|
|
{AF_FORMAT_S32, SND_PCM_FORMAT_S32},
|
2017-07-07 15:37:23 +00:00
|
|
|
{AF_FORMAT_S32, SND_PCM_FORMAT_S24, .bits = 32, .pad_msb = 8},
|
|
|
|
{AF_FORMAT_S32,
|
|
|
|
MP_SELECT_LE_BE(SND_PCM_FORMAT_S24_3LE, SND_PCM_FORMAT_S24_3BE),
|
|
|
|
.bits = 24, .pad_msb = 0},
|
2014-09-23 19:04:37 +00:00
|
|
|
{AF_FORMAT_FLOAT, SND_PCM_FORMAT_FLOAT},
|
2015-09-11 04:24:01 +00:00
|
|
|
{AF_FORMAT_DOUBLE, SND_PCM_FORMAT_FLOAT64},
|
2017-07-07 15:37:23 +00:00
|
|
|
{0},
|
2013-04-07 21:08:10 +00:00
|
|
|
};
|
|
|
|
|
2017-07-07 15:37:23 +00:00
|
|
|
static const struct alsa_fmt *find_alsa_format(int mp_format)
|
2013-04-07 21:08:10 +00:00
|
|
|
{
|
2017-07-07 15:37:23 +00:00
|
|
|
for (int n = 0; mp_alsa_formats[n].mp_format; n++) {
|
|
|
|
if (mp_alsa_formats[n].mp_format == mp_format)
|
|
|
|
return &mp_alsa_formats[n];
|
2013-04-07 21:08:10 +00:00
|
|
|
}
|
2017-07-07 15:37:23 +00:00
|
|
|
return NULL;
|
2013-04-07 21:08:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 18:40:24 +00:00
|
|
|
#if HAVE_CHMAP_API
|
|
|
|
|
|
|
|
static const int alsa_to_mp_channels[][2] = {
|
|
|
|
{SND_CHMAP_FL, MP_SP(FL)},
|
|
|
|
{SND_CHMAP_FR, MP_SP(FR)},
|
|
|
|
{SND_CHMAP_RL, MP_SP(BL)},
|
|
|
|
{SND_CHMAP_RR, MP_SP(BR)},
|
|
|
|
{SND_CHMAP_FC, MP_SP(FC)},
|
|
|
|
{SND_CHMAP_LFE, MP_SP(LFE)},
|
|
|
|
{SND_CHMAP_SL, MP_SP(SL)},
|
|
|
|
{SND_CHMAP_SR, MP_SP(SR)},
|
|
|
|
{SND_CHMAP_RC, MP_SP(BC)},
|
|
|
|
{SND_CHMAP_FLC, MP_SP(FLC)},
|
|
|
|
{SND_CHMAP_FRC, MP_SP(FRC)},
|
|
|
|
{SND_CHMAP_FLW, MP_SP(WL)},
|
|
|
|
{SND_CHMAP_FRW, MP_SP(WR)},
|
|
|
|
{SND_CHMAP_TC, MP_SP(TC)},
|
|
|
|
{SND_CHMAP_TFL, MP_SP(TFL)},
|
|
|
|
{SND_CHMAP_TFR, MP_SP(TFR)},
|
|
|
|
{SND_CHMAP_TFC, MP_SP(TFC)},
|
|
|
|
{SND_CHMAP_TRL, MP_SP(TBL)},
|
|
|
|
{SND_CHMAP_TRR, MP_SP(TBR)},
|
|
|
|
{SND_CHMAP_TRC, MP_SP(TBC)},
|
2015-06-12 21:08:09 +00:00
|
|
|
{SND_CHMAP_RRC, MP_SP(SDR)},
|
|
|
|
{SND_CHMAP_RLC, MP_SP(SDL)},
|
2014-11-24 18:40:24 +00:00
|
|
|
{SND_CHMAP_MONO, MP_SP(FC)},
|
2015-05-07 21:07:14 +00:00
|
|
|
{SND_CHMAP_NA, MP_SPEAKER_ID_NA},
|
2015-11-02 22:55:52 +00:00
|
|
|
{SND_CHMAP_UNKNOWN, MP_SPEAKER_ID_NA},
|
2014-11-24 18:40:24 +00:00
|
|
|
{SND_CHMAP_LAST, MP_SPEAKER_ID_COUNT}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int find_mp_channel(int alsa_channel)
|
|
|
|
{
|
|
|
|
for (int i = 0; alsa_to_mp_channels[i][1] != MP_SPEAKER_ID_COUNT; i++) {
|
|
|
|
if (alsa_to_mp_channels[i][0] == alsa_channel)
|
|
|
|
return alsa_to_mp_channels[i][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_SPEAKER_ID_COUNT;
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:50:22 +00:00
|
|
|
#define CHMAP(n, ...) &(struct mp_chmap) MP_CONCAT(MP_CHMAP, n) (__VA_ARGS__)
|
|
|
|
|
|
|
|
// Replace each channel in a with b (a->num == b->num)
|
|
|
|
static void replace_submap(struct mp_chmap *dst, struct mp_chmap *a,
|
|
|
|
struct mp_chmap *b)
|
|
|
|
{
|
|
|
|
struct mp_chmap t = *dst;
|
|
|
|
if (!mp_chmap_is_valid(&t) || mp_chmap_diffn(a, &t) != 0)
|
|
|
|
return;
|
|
|
|
assert(a->num == b->num);
|
|
|
|
for (int n = 0; n < t.num; n++) {
|
|
|
|
for (int i = 0; i < a->num; i++) {
|
|
|
|
if (t.speaker[n] == a->speaker[i]) {
|
|
|
|
t.speaker[n] = b->speaker[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mp_chmap_is_valid(&t))
|
|
|
|
*dst = t;
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
static bool mp_chmap_from_alsa(struct mp_chmap *dst, snd_pcm_chmap_t *src)
|
2015-05-06 19:33:54 +00:00
|
|
|
{
|
|
|
|
*dst = (struct mp_chmap) {0};
|
|
|
|
|
|
|
|
if (src->channels > MP_NUM_CHANNELS)
|
2015-11-04 12:44:52 +00:00
|
|
|
return false;
|
2015-05-06 19:33:54 +00:00
|
|
|
|
|
|
|
dst->num = src->channels;
|
2015-05-07 21:07:14 +00:00
|
|
|
for (int c = 0; c < dst->num; c++)
|
2015-05-06 19:33:54 +00:00
|
|
|
dst->speaker[c] = find_mp_channel(src->pos[c]);
|
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
// Assume anything with 1 channel is mono.
|
|
|
|
if (dst->num == 1)
|
|
|
|
dst->speaker[0] = MP_SP(FC);
|
|
|
|
|
2015-11-04 12:50:22 +00:00
|
|
|
// Remap weird Intel HDA HDMI 7.1 layouts correctly.
|
|
|
|
replace_submap(dst, CHMAP(6, FL, FR, BL, BR, SDL, SDR),
|
|
|
|
CHMAP(6, FL, FR, SL, SR, BL, BR));
|
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
return mp_chmap_is_valid(dst);
|
2015-05-06 19:33:54 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 17:09:36 +00:00
|
|
|
static bool query_chmaps(struct ao *ao, struct mp_chmap *chmap)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
2014-12-01 14:28:06 +00:00
|
|
|
struct mp_chmap_sel chmap_sel = {.tmp = p};
|
2014-11-25 17:09:36 +00:00
|
|
|
|
|
|
|
snd_pcm_chmap_query_t **maps = snd_pcm_query_chmaps(p->alsa);
|
2016-08-22 18:05:34 +00:00
|
|
|
if (!maps) {
|
|
|
|
MP_VERBOSE(ao, "snd_pcm_query_chmaps() returned NULL\n");
|
2014-11-25 17:09:36 +00:00
|
|
|
return false;
|
2016-08-22 18:05:34 +00:00
|
|
|
}
|
2014-11-25 17:09:36 +00:00
|
|
|
|
|
|
|
for (int i = 0; maps[i] != NULL; i++) {
|
2015-11-04 12:44:44 +00:00
|
|
|
char aname[128];
|
|
|
|
if (snd_pcm_chmap_print(&maps[i]->map, sizeof(aname), aname) <= 0)
|
|
|
|
aname[0] = '\0';
|
|
|
|
|
2015-05-06 19:33:54 +00:00
|
|
|
struct mp_chmap entry;
|
2015-11-04 12:44:52 +00:00
|
|
|
if (mp_chmap_from_alsa(&entry, &maps[i]->map)) {
|
2015-11-04 12:44:44 +00:00
|
|
|
struct mp_chmap reorder = entry;
|
2016-11-08 15:59:12 +00:00
|
|
|
mp_chmap_reorder_norm(&reorder);
|
|
|
|
|
|
|
|
MP_DBG(ao, "got ALSA chmap: %s (%s) -> %s", aname,
|
|
|
|
snd_pcm_chmap_type_name(maps[i]->type),
|
|
|
|
mp_chmap_to_str(&entry));
|
|
|
|
if (!mp_chmap_equals(&entry, &reorder))
|
|
|
|
MP_DBG(ao, " -> %s", mp_chmap_to_str(&reorder));
|
|
|
|
MP_DBG(ao, "\n");
|
|
|
|
|
|
|
|
struct mp_chmap final =
|
|
|
|
maps[i]->type == SND_CHMAP_TYPE_VAR ? reorder : entry;
|
|
|
|
mp_chmap_sel_add_map(&chmap_sel, &final);
|
2014-11-25 17:09:36 +00:00
|
|
|
} else {
|
2015-11-04 12:44:44 +00:00
|
|
|
MP_VERBOSE(ao, "skipping unknown ALSA channel map: %s\n", aname);
|
2014-11-25 17:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_free_chmaps(maps);
|
|
|
|
|
2016-08-04 18:49:20 +00:00
|
|
|
return ao_chmap_sel_adjust2(ao, &chmap_sel, chmap, false);
|
2014-11-25 17:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
// Map back our selected channel layout to an ALSA one. This is done this way so
|
|
|
|
// that our ALSA->mp_chmap mapping function only has to go one way.
|
|
|
|
// The return value is to be freed with free().
|
|
|
|
static snd_pcm_chmap_t *map_back_chmap(struct ao *ao, struct mp_chmap *chmap)
|
2015-11-02 22:59:36 +00:00
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
2015-11-04 12:44:52 +00:00
|
|
|
if (!mp_chmap_is_valid(chmap))
|
|
|
|
return NULL;
|
2015-11-02 22:59:36 +00:00
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
snd_pcm_chmap_query_t **maps = snd_pcm_query_chmaps(p->alsa);
|
|
|
|
if (!maps)
|
|
|
|
return NULL;
|
2015-11-02 22:59:36 +00:00
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
snd_pcm_chmap_t *alsa_chmap = NULL;
|
2015-11-02 22:59:36 +00:00
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
for (int i = 0; maps[i] != NULL; i++) {
|
|
|
|
struct mp_chmap entry;
|
|
|
|
if (!mp_chmap_from_alsa(&entry, &maps[i]->map))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mp_chmap_equals(chmap, &entry) ||
|
|
|
|
(mp_chmap_equals_reordered(chmap, &entry) &&
|
|
|
|
maps[i]->type == SND_CHMAP_TYPE_VAR))
|
|
|
|
{
|
|
|
|
alsa_chmap = calloc(1, sizeof(*alsa_chmap) +
|
|
|
|
sizeof(alsa_chmap->pos[0]) * entry.num);
|
|
|
|
if (!alsa_chmap)
|
|
|
|
break;
|
|
|
|
alsa_chmap->channels = entry.num;
|
|
|
|
|
|
|
|
// Undo if mp_chmap_reorder() was called on the result.
|
|
|
|
int reorder[MP_NUM_CHANNELS];
|
|
|
|
mp_chmap_get_reorder(reorder, chmap, &entry);
|
|
|
|
for (int n = 0; n < entry.num; n++)
|
|
|
|
alsa_chmap->pos[n] = maps[i]->map.pos[reorder[n]];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_free_chmaps(maps);
|
|
|
|
return alsa_chmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int set_chmap(struct ao *ao, struct mp_chmap *dev_chmap, int num_channels)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
int err;
|
2015-11-02 22:59:36 +00:00
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
snd_pcm_chmap_t *alsa_chmap = map_back_chmap(ao, dev_chmap);
|
|
|
|
if (alsa_chmap) {
|
2015-11-02 22:59:36 +00:00
|
|
|
char tmp[128];
|
|
|
|
if (snd_pcm_chmap_print(alsa_chmap, sizeof(tmp), tmp) > 0)
|
|
|
|
MP_VERBOSE(ao, "trying to set ALSA channel map: %s\n", tmp);
|
|
|
|
|
|
|
|
err = snd_pcm_set_chmap(p->alsa, alsa_chmap);
|
|
|
|
if (err == -ENXIO) {
|
|
|
|
// A device my not be able to set any channel map, even channel maps
|
|
|
|
// that were reported as supported. This is either because the ALSA
|
|
|
|
// device is broken (dmix), or because the driver has only 1
|
|
|
|
// channel map per channel count, and setting the map is not needed.
|
|
|
|
MP_VERBOSE(ao, "device returned ENXIO when setting channel map %s\n",
|
|
|
|
mp_chmap_to_str(dev_chmap));
|
|
|
|
} else {
|
|
|
|
CHECK_ALSA_WARN("Channel map setup failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(alsa_chmap);
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:44:52 +00:00
|
|
|
alsa_chmap = snd_pcm_get_chmap(p->alsa);
|
2015-11-02 22:59:36 +00:00
|
|
|
if (alsa_chmap) {
|
|
|
|
char tmp[128];
|
|
|
|
if (snd_pcm_chmap_print(alsa_chmap, sizeof(tmp), tmp) > 0)
|
|
|
|
MP_VERBOSE(ao, "channel map reported by ALSA: %s\n", tmp);
|
|
|
|
|
|
|
|
struct mp_chmap chmap;
|
|
|
|
mp_chmap_from_alsa(&chmap, alsa_chmap);
|
|
|
|
|
|
|
|
MP_VERBOSE(ao, "which we understand as: %s\n", mp_chmap_to_str(&chmap));
|
|
|
|
|
2016-09-02 18:07:25 +00:00
|
|
|
if (p->opts->ignore_chmap) {
|
2015-11-02 22:59:36 +00:00
|
|
|
MP_VERBOSE(ao, "user set ignore-chmap; ignoring the channel map.\n");
|
|
|
|
} else if (af_fmt_is_spdif(ao->format)) {
|
|
|
|
MP_VERBOSE(ao, "using spdif passthrough; ignoring the channel map.\n");
|
2015-11-02 22:59:49 +00:00
|
|
|
} else if (!mp_chmap_is_valid(&chmap)) {
|
2015-11-02 22:59:36 +00:00
|
|
|
MP_WARN(ao, "Got unknown channel map from ALSA.\n");
|
2015-11-02 22:59:49 +00:00
|
|
|
} else if (chmap.num != num_channels) {
|
|
|
|
MP_WARN(ao, "ALSA channel map conflicts with channel count!\n");
|
|
|
|
} else {
|
2016-11-08 16:06:00 +00:00
|
|
|
if (mp_chmap_equals(&chmap, &ao->channels)) {
|
2015-11-02 22:59:49 +00:00
|
|
|
MP_VERBOSE(ao, "which is what we requested.\n");
|
2016-11-08 16:06:00 +00:00
|
|
|
} else if (!mp_chmap_is_valid(dev_chmap)) {
|
|
|
|
MP_VERBOSE(ao, "ignoring the ALSA channel map.\n");
|
|
|
|
} else {
|
|
|
|
MP_VERBOSE(ao, "using the ALSA channel map.\n");
|
|
|
|
ao->channels = chmap;
|
|
|
|
}
|
2015-11-02 22:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(alsa_chmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-25 17:09:36 +00:00
|
|
|
#else /* HAVE_CHMAP_API */
|
|
|
|
|
|
|
|
static bool query_chmaps(struct ao *ao, struct mp_chmap *chmap)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-02 22:59:36 +00:00
|
|
|
static int set_chmap(struct ao *ao, struct mp_chmap *dev_chmap, int num_channels)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-25 17:09:36 +00:00
|
|
|
#endif /* else HAVE_CHMAP_API */
|
2014-11-24 18:40:24 +00:00
|
|
|
|
2018-01-23 23:07:00 +00:00
|
|
|
static void dump_hw_params(struct ao *ao, const char *msg,
|
2016-04-28 11:31:13 +00:00
|
|
|
snd_pcm_hw_params_t *hw_params)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_dump(hw_params, p->output);
|
|
|
|
CHECK_ALSA_WARN("Dump hwparams error");
|
|
|
|
|
|
|
|
char *tmp = NULL;
|
|
|
|
size_t tmp_s = snd_output_buffer_string(p->output, &tmp);
|
|
|
|
if (tmp)
|
2018-01-23 23:07:00 +00:00
|
|
|
mp_msg(ao->log, MSGL_DEBUG, "%s---\n%.*s---\n", msg, (int)tmp_s, tmp);
|
2016-04-28 11:31:13 +00:00
|
|
|
snd_output_flush(p->output);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:36:14 +00:00
|
|
|
// ALSA device strings can have parameters. They are usually appended to the
|
2015-11-02 23:00:02 +00:00
|
|
|
// device name. There can be various forms, and we (sometimes) want to append
|
|
|
|
// them to unknown device strings, which possibly already include params.
|
2014-10-23 15:36:14 +00:00
|
|
|
static char *append_params(void *ta_parent, const char *device, const char *p)
|
|
|
|
{
|
|
|
|
if (!p || !p[0])
|
|
|
|
return talloc_strdup(ta_parent, device);
|
|
|
|
|
|
|
|
int len = strlen(device);
|
|
|
|
char *end = strchr(device, ':');
|
|
|
|
if (!end) {
|
|
|
|
/* no existing parameters: add it behind device name */
|
|
|
|
return talloc_asprintf(ta_parent, "%s:%s", device, p);
|
|
|
|
} else if (end[1] == '\0') {
|
|
|
|
/* ":" but no parameters */
|
|
|
|
return talloc_asprintf(ta_parent, "%s%s", device, p);
|
|
|
|
} else if (end[1] == '{' && device[len - 1] == '}') {
|
|
|
|
/* parameters in config syntax: add it inside the { } block */
|
|
|
|
return talloc_asprintf(ta_parent, "%.*s %s}", len - 1, device, p);
|
|
|
|
} else {
|
|
|
|
/* a simple list of parameters: add it at the end of the list */
|
|
|
|
return talloc_asprintf(ta_parent, "%s,%s", device, p);
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2016-05-06 15:20:02 +00:00
|
|
|
static int try_open_device(struct ao *ao, const char *device, int mode)
|
2006-09-18 16:58:21 +00:00
|
|
|
{
|
2013-04-18 22:24:26 +00:00
|
|
|
struct priv *p = ao->priv;
|
2015-06-04 19:54:08 +00:00
|
|
|
int err;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2015-06-26 21:06:37 +00:00
|
|
|
if (af_fmt_is_spdif(ao->format)) {
|
2013-11-09 00:27:10 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
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));
|
2014-10-23 15:36:14 +00:00
|
|
|
const char *ac3_device = append_params(tmp, device, params);
|
2015-04-07 16:05:09 +00:00
|
|
|
MP_VERBOSE(ao, "opening device '%s' => '%s'\n", device, ac3_device);
|
2016-05-06 15:20:02 +00:00
|
|
|
err = snd_pcm_open(&p->alsa, ac3_device, SND_PCM_STREAM_PLAYBACK, mode);
|
2015-06-04 11:10:33 +00:00
|
|
|
if (err < 0) {
|
|
|
|
// Some spdif-capable devices do not accept the AES0 parameter,
|
|
|
|
// and instead require the iec958 pseudo-device (they will play
|
|
|
|
// noise otherwise). Unfortunately, ALSA gives us no way to map
|
|
|
|
// these devices, so try it for the default device only.
|
|
|
|
bstr dev;
|
|
|
|
bstr_split_tok(bstr0(device), ":", &dev, &(bstr){0});
|
|
|
|
if (bstr_equals0(dev, "default")) {
|
2016-10-07 15:21:08 +00:00
|
|
|
const char *const fallbacks[] = {"hdmi", "iec958", NULL};
|
|
|
|
for (int n = 0; fallbacks[n]; n++) {
|
|
|
|
char *ndev = append_params(tmp, fallbacks[n], params);
|
2016-12-07 11:51:17 +00:00
|
|
|
MP_VERBOSE(ao, "got error '%s'; opening iec fallback "
|
|
|
|
"device '%s'\n", snd_strerror(err), ndev);
|
2016-10-07 15:21:08 +00:00
|
|
|
err = snd_pcm_open
|
|
|
|
(&p->alsa, ndev, SND_PCM_STREAM_PLAYBACK, mode);
|
|
|
|
if (err >= 0)
|
|
|
|
break;
|
|
|
|
}
|
2015-06-04 11:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-09 00:27:10 +00:00
|
|
|
talloc_free(tmp);
|
2015-06-04 19:54:08 +00:00
|
|
|
} else {
|
|
|
|
MP_VERBOSE(ao, "opening device '%s'\n", device);
|
2016-05-06 15:20:02 +00:00
|
|
|
err = snd_pcm_open(&p->alsa, device, SND_PCM_STREAM_PLAYBACK, mode);
|
2006-09-18 16:58:21 +00:00
|
|
|
}
|
2013-11-09 00:27:10 +00:00
|
|
|
|
2015-06-04 19:54:08 +00:00
|
|
|
return err;
|
2006-09-18 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2014-11-25 10:10:44 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2016-04-28 11:31:13 +00:00
|
|
|
if (p->output)
|
|
|
|
snd_output_close(p->output);
|
|
|
|
p->output = NULL;
|
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
if (p->alsa) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = snd_pcm_close(p->alsa);
|
2015-11-02 22:59:49 +00:00
|
|
|
p->alsa = NULL;
|
2014-11-25 10:10:44 +00:00
|
|
|
CHECK_ALSA_ERROR("pcm close error");
|
|
|
|
}
|
|
|
|
|
|
|
|
alsa_error: ;
|
|
|
|
}
|
2005-01-03 14:23:18 +00:00
|
|
|
|
2016-05-06 15:20:02 +00:00
|
|
|
#define INIT_DEVICE_ERR_GENERIC -1
|
|
|
|
#define INIT_DEVICE_ERR_HWPARAMS -2
|
|
|
|
static int init_device(struct ao *ao, int mode)
|
2014-11-25 10:10:44 +00:00
|
|
|
{
|
2013-07-21 19:51:51 +00:00
|
|
|
struct priv *p = ao->priv;
|
2016-05-06 15:20:02 +00:00
|
|
|
int ret = INIT_DEVICE_ERR_GENERIC;
|
2016-04-28 11:31:13 +00:00
|
|
|
char *tmp;
|
|
|
|
size_t tmp_s;
|
2014-11-25 10:10:44 +00:00
|
|
|
int err;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2017-07-07 15:37:23 +00:00
|
|
|
p->alsa_fmt = SND_PCM_FORMAT_UNKNOWN;
|
|
|
|
|
2016-04-28 11:31:13 +00:00
|
|
|
err = snd_output_buffer_open(&p->output);
|
|
|
|
CHECK_ALSA_ERROR("Unable to create output buffer");
|
|
|
|
|
2014-12-15 15:58:03 +00:00
|
|
|
const char *device = "default";
|
2014-10-09 19:22:44 +00:00
|
|
|
if (ao->device)
|
|
|
|
device = ao->device;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2016-05-06 15:20:02 +00:00
|
|
|
err = try_open_device(ao, device, mode);
|
2014-12-05 00:23:09 +00:00
|
|
|
CHECK_ALSA_ERROR("Playback open error");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2016-04-28 11:31:13 +00:00
|
|
|
err = snd_pcm_dump(p->alsa, p->output);
|
|
|
|
CHECK_ALSA_WARN("Dump PCM error");
|
|
|
|
tmp_s = snd_output_buffer_string(p->output, &tmp);
|
|
|
|
if (tmp)
|
|
|
|
MP_DBG(ao, "PCM setup:\n---\n%.*s---\n", (int)tmp_s, tmp);
|
|
|
|
snd_output_flush(p->output);
|
|
|
|
|
2013-04-18 22:24:26 +00:00
|
|
|
err = snd_pcm_nonblock(p->alsa, 0);
|
2014-12-05 15:00:25 +00:00
|
|
|
CHECK_ALSA_WARN("Unable to set blocking mode");
|
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_hw_params_alloca(&alsa_hwparams);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
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
|
|
|
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "Start HW params:\n", alsa_hwparams);
|
2016-04-28 11:31:13 +00:00
|
|
|
|
2015-11-02 22:58:27 +00:00
|
|
|
// Some ALSA drivers have broken delay reporting, so disable the ALSA
|
|
|
|
// resampling plugin by default.
|
2016-09-02 18:07:25 +00:00
|
|
|
if (!p->opts->resample) {
|
2015-11-02 22:58:27 +00:00
|
|
|
err = snd_pcm_hw_params_set_rate_resample(p->alsa, alsa_hwparams, 0);
|
|
|
|
CHECK_ALSA_ERROR("Unable to disable resampling");
|
|
|
|
}
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "HW params after rate:\n", alsa_hwparams);
|
2015-11-02 22:58:27 +00:00
|
|
|
|
2015-11-02 22:57:34 +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");
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "HW params after access:\n", alsa_hwparams);
|
2015-11-02 22:57:34 +00:00
|
|
|
|
2015-10-06 09:07:00 +00:00
|
|
|
bool found_format = false;
|
2018-01-23 23:02:13 +00:00
|
|
|
int try_formats[AF_FORMAT_COUNT + 1];
|
2015-09-11 04:24:26 +00:00
|
|
|
af_get_best_sample_formats(ao->format, try_formats);
|
2017-07-07 15:37:23 +00:00
|
|
|
for (int n = 0; try_formats[n] && !found_format; n++) {
|
|
|
|
int mp_format = try_formats[n];
|
|
|
|
if (af_fmt_is_planar(ao->format) != af_fmt_is_planar(mp_format))
|
2015-11-02 22:57:34 +00:00
|
|
|
continue; // implied SND_PCM_ACCESS mismatches
|
2017-07-07 15:37:23 +00:00
|
|
|
int mp_pformat = af_fmt_from_planar(mp_format);
|
|
|
|
if (af_fmt_is_spdif(mp_pformat))
|
|
|
|
mp_pformat = AF_FORMAT_S16;
|
|
|
|
const struct alsa_fmt *fmt = find_alsa_format(mp_pformat);
|
|
|
|
if (!fmt)
|
|
|
|
continue;
|
|
|
|
for (; fmt->mp_format == mp_pformat; fmt++) {
|
|
|
|
p->alsa_fmt = fmt->alsa_format;
|
|
|
|
p->convert = (struct ao_convert_fmt){
|
|
|
|
.src_fmt = mp_format,
|
|
|
|
.dst_bits = fmt->bits ? fmt->bits : af_fmt_to_bytes(mp_format) * 8,
|
|
|
|
.pad_msb = fmt->pad_msb,
|
|
|
|
};
|
|
|
|
if (!ao_can_convert_inplace(&p->convert))
|
|
|
|
continue;
|
|
|
|
MP_VERBOSE(ao, "trying format %s/%d\n", af_fmt_to_str(mp_pformat),
|
|
|
|
p->alsa_fmt);
|
|
|
|
if (snd_pcm_hw_params_test_format(p->alsa, alsa_hwparams,
|
|
|
|
p->alsa_fmt) >= 0)
|
|
|
|
{
|
|
|
|
ao->format = mp_format;
|
|
|
|
found_format = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-10-06 09:07:00 +00:00
|
|
|
}
|
2013-05-10 12:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 09:07:00 +00:00
|
|
|
if (!found_format) {
|
2015-09-11 04:24:26 +00:00
|
|
|
MP_ERR(ao, "Can't find appropriate sample format.\n");
|
|
|
|
goto alsa_error;
|
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");
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "HW params after format:\n", alsa_hwparams);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2016-11-08 16:35:27 +00:00
|
|
|
// Stereo, or mono if input is 1 channel.
|
|
|
|
struct mp_chmap reduced;
|
|
|
|
mp_chmap_from_channels(&reduced, MPMIN(2, ao->channels.num));
|
|
|
|
|
|
|
|
struct mp_chmap dev_chmap = {0};
|
|
|
|
if (!af_fmt_is_spdif(ao->format) && !p->opts->ignore_chmap &&
|
|
|
|
!mp_chmap_equals(&ao->channels, &reduced))
|
|
|
|
{
|
|
|
|
struct mp_chmap res = ao->channels;
|
|
|
|
if (query_chmaps(ao, &res))
|
|
|
|
dev_chmap = res;
|
|
|
|
|
|
|
|
// Whatever it is, we dumb it down to mono or stereo. Some drivers may
|
|
|
|
// return things like bl-br, but the user (probably) still wants stereo.
|
|
|
|
// This also handles the failure case (dev_chmap.num==0).
|
|
|
|
if (dev_chmap.num <= 2) {
|
|
|
|
dev_chmap.num = 0;
|
|
|
|
ao->channels = reduced;
|
|
|
|
} else if (dev_chmap.num) {
|
|
|
|
ao->channels = dev_chmap;
|
|
|
|
}
|
2014-11-25 17:09:36 +00:00
|
|
|
}
|
|
|
|
|
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");
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "HW params after channels:\n", alsa_hwparams);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2014-11-25 17:09:36 +00:00
|
|
|
if (num_channels > MP_NUM_CHANNELS) {
|
|
|
|
MP_FATAL(ao, "Too many audio channels (%d).\n", num_channels);
|
|
|
|
goto alsa_error;
|
|
|
|
}
|
|
|
|
|
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");
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "HW params after rate-2:\n", alsa_hwparams);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2015-11-02 22:58:55 +00:00
|
|
|
snd_pcm_hw_params_t *hwparams_backup;
|
|
|
|
snd_pcm_hw_params_alloca(&hwparams_backup);
|
|
|
|
snd_pcm_hw_params_copy(hwparams_backup, alsa_hwparams);
|
|
|
|
|
|
|
|
// Cargo-culted buffer settings; might still be useful for PulseAudio.
|
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);
|
2014-10-31 00:09:53 +00:00
|
|
|
CHECK_ALSA_WARN("Unable to set buffer time near");
|
2015-11-02 22:58:55 +00:00
|
|
|
if (err >= 0) {
|
|
|
|
err = snd_pcm_hw_params_set_periods_near
|
|
|
|
(p->alsa, alsa_hwparams, &(unsigned int){FRAGCOUNT}, NULL);
|
|
|
|
CHECK_ALSA_WARN("Unable to set periods");
|
|
|
|
}
|
|
|
|
if (err < 0)
|
|
|
|
snd_pcm_hw_params_copy(alsa_hwparams, hwparams_backup);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "Going to set final HW params:\n", alsa_hwparams);
|
2016-04-28 11:31:13 +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);
|
2016-05-06 15:20:02 +00:00
|
|
|
ret = INIT_DEVICE_ERR_HWPARAMS;
|
2013-04-07 21:03:31 +00:00
|
|
|
CHECK_ALSA_ERROR("Unable to set hw-parameters");
|
2016-05-06 15:20:02 +00:00
|
|
|
ret = INIT_DEVICE_ERR_GENERIC;
|
2018-01-23 23:07:00 +00:00
|
|
|
dump_hw_params(ao, "Final HW params:\n", alsa_hwparams);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2015-11-02 22:59:49 +00:00
|
|
|
if (set_chmap(ao, &dev_chmap, num_channels) < 0)
|
|
|
|
goto alsa_error;
|
2014-11-25 17:09:36 +00:00
|
|
|
|
2015-11-02 22:57:12 +00:00
|
|
|
if (num_channels != ao->channels.num) {
|
|
|
|
int req = ao->channels.num;
|
2015-11-07 14:17:30 +00:00
|
|
|
mp_chmap_from_channels(&ao->channels, MPMIN(2, num_channels));
|
2016-11-08 16:46:42 +00:00
|
|
|
mp_chmap_fill_na(&ao->channels, num_channels);
|
2015-11-02 22:57:12 +00:00
|
|
|
MP_ERR(ao, "Asked for %d channels, got %d - fallback to %s.\n", req,
|
|
|
|
num_channels, mp_chmap_to_str(&ao->channels));
|
2016-11-08 16:46:42 +00:00
|
|
|
if (num_channels != ao->channels.num) {
|
|
|
|
MP_FATAL(ao, "mismatching channel counts.\n");
|
|
|
|
goto alsa_error;
|
|
|
|
}
|
2015-11-02 22:57:12 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 23:00:02 +00:00
|
|
|
err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &p->buffersize);
|
2013-04-07 21:03:31 +00:00
|
|
|
CHECK_ALSA_ERROR("Unable to get buffersize");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2015-11-02 23:00:02 +00:00
|
|
|
err = snd_pcm_hw_params_get_period_size(alsa_hwparams, &p->outburst, NULL);
|
2013-04-07 21:03:31 +00:00
|
|
|
CHECK_ALSA_ERROR("Unable to get period size");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2015-11-02 23:00:02 +00:00
|
|
|
p->can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams);
|
|
|
|
|
|
|
|
snd_pcm_sw_params_t *alsa_swparams;
|
|
|
|
snd_pcm_sw_params_alloca(&alsa_swparams);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
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
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
snd_pcm_uframes_t boundary;
|
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
|
2015-11-02 23:00:02 +00:00
|
|
|
(p->alsa, alsa_swparams, p->outburst);
|
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);
|
2014-11-25 10:10:44 +00:00
|
|
|
CHECK_ALSA_ERROR("Unable to set sw-parameters");
|
2013-04-07 21:03:31 +00:00
|
|
|
|
2015-11-02 23:00:02 +00:00
|
|
|
MP_VERBOSE(ao, "hw pausing supported: %s\n", p->can_pause ? "yes" : "no");
|
|
|
|
MP_VERBOSE(ao, "buffersize: %d samples\n", (int)p->buffersize);
|
|
|
|
MP_VERBOSE(ao, "period size: %d samples\n", (int)p->outburst);
|
2013-04-07 21:03:31 +00:00
|
|
|
|
2016-08-09 14:35:33 +00:00
|
|
|
ao->device_buffer = p->buffersize;
|
2017-06-25 13:57:15 +00:00
|
|
|
ao->period_size = p->outburst;
|
2016-08-09 14:35:33 +00:00
|
|
|
|
2017-07-07 15:37:23 +00:00
|
|
|
p->convert.channels = ao->channels.num;
|
|
|
|
|
2015-11-02 22:59:49 +00:00
|
|
|
return 0;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
alsa_error:
|
2014-03-08 23:49:39 +00:00
|
|
|
uninit(ao);
|
2016-05-06 15:20:02 +00:00
|
|
|
return ret;
|
2014-12-15 15:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int init(struct ao *ao)
|
|
|
|
{
|
2015-04-07 16:05:09 +00:00
|
|
|
struct priv *p = ao->priv;
|
2016-09-02 18:07:25 +00:00
|
|
|
p->opts = mp_get_config_group(ao, ao->global, &ao_alsa_conf);
|
|
|
|
|
|
|
|
if (!p->opts->ni)
|
2015-04-07 16:05:09 +00:00
|
|
|
ao->format = af_fmt_from_planar(ao->format);
|
|
|
|
|
|
|
|
MP_VERBOSE(ao, "using ALSA version: %s\n", snd_asoundlib_version());
|
|
|
|
|
2016-05-06 15:20:02 +00:00
|
|
|
int mode = 0;
|
|
|
|
int r = init_device(ao, mode);
|
|
|
|
if (r == INIT_DEVICE_ERR_HWPARAMS) {
|
|
|
|
// With some drivers, ALSA appears to be unable to set valid hwparams,
|
|
|
|
// but they work if at least SND_PCM_NO_AUTO_FORMAT is set. Also, it
|
|
|
|
// appears you can set this flag only on opening a device, thus there
|
|
|
|
// is the need to retry opening the device.
|
|
|
|
MP_WARN(ao, "Attempting to work around even more ALSA bugs...\n");
|
|
|
|
mode |= SND_PCM_NO_AUTO_CHANNELS | SND_PCM_NO_AUTO_FORMAT |
|
|
|
|
SND_PCM_NO_AUTO_RESAMPLE;
|
|
|
|
r = init_device(ao, mode);
|
|
|
|
}
|
2015-11-02 22:59:49 +00:00
|
|
|
|
|
|
|
// Sometimes, ALSA will advertise certain chmaps, but it's not possible to
|
|
|
|
// set them. This can happen with dmix: as of alsa 1.0.29, dmix can do
|
|
|
|
// stereo only, but advertises the surround chmaps of the underlying device.
|
|
|
|
// In this case, e.g. setting 6 channels will succeed, but requesting 5.1
|
|
|
|
// afterwards will fail. Then it will return something like "FL FR NA NA NA NA"
|
|
|
|
// as channel map. This means we would have to pad stereo output to 6
|
|
|
|
// channels with silence, which would require lots of extra processing. You
|
|
|
|
// can't change the number of channels to 2 either, because the hw params
|
|
|
|
// are already set! So just fuck it and reopen the device with the chmap
|
|
|
|
// "cleaned out" of NA entries.
|
|
|
|
if (r >= 0) {
|
|
|
|
struct mp_chmap without_na = ao->channels;
|
|
|
|
mp_chmap_remove_na(&without_na);
|
|
|
|
|
|
|
|
if (mp_chmap_is_valid(&without_na) && without_na.num <= 2 &&
|
|
|
|
ao->channels.num > 2)
|
|
|
|
{
|
|
|
|
MP_VERBOSE(ao, "Working around braindead dmix multichannel behavior.\n");
|
|
|
|
uninit(ao);
|
|
|
|
ao->channels = without_na;
|
2016-05-06 15:20:02 +00:00
|
|
|
r = init_device(ao, mode);
|
2015-11-02 22:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2014-11-25 10:10:44 +00:00
|
|
|
}
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
static void drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
snd_pcm_drain(p->alsa);
|
|
|
|
}
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2014-11-25 10:10:44 +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;
|
2014-11-25 10:10:44 +00:00
|
|
|
snd_pcm_status_t *status;
|
|
|
|
int err;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
snd_pcm_status_alloca(&status);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
err = snd_pcm_status(p->alsa, status);
|
2015-09-28 15:29:47 +00:00
|
|
|
if (!check_device_present(ao, err))
|
|
|
|
goto alsa_error;
|
2014-11-25 10:10:44 +00:00
|
|
|
CHECK_ALSA_ERROR("cannot get pcm status");
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
unsigned space = snd_pcm_status_get_avail(status);
|
|
|
|
if (space > p->buffersize) // Buffer underrun?
|
|
|
|
space = p->buffersize;
|
|
|
|
return space / p->outburst * p->outburst;
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2013-05-25 13:07:05 +00:00
|
|
|
alsa_error:
|
2014-11-25 10:10:44 +00:00
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
/* delay in seconds between first and last sample in buffer */
|
|
|
|
static double get_delay(struct ao *ao)
|
2014-03-08 23:49:39 +00:00
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
2014-11-25 10:10:44 +00:00
|
|
|
snd_pcm_sframes_t delay;
|
|
|
|
|
2016-08-09 14:52:33 +00:00
|
|
|
if (p->paused)
|
2014-11-25 10:10:44 +00:00
|
|
|
return p->delay_before_pause;
|
|
|
|
|
|
|
|
if (snd_pcm_delay(p->alsa, &delay) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (delay < 0) {
|
|
|
|
/* underrun - move the application pointer forward to catch up */
|
|
|
|
snd_pcm_forward(p->alsa, -delay);
|
|
|
|
delay = 0;
|
|
|
|
}
|
|
|
|
return delay / (double)ao->samplerate;
|
2014-03-08 23:49:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 14:35:33 +00:00
|
|
|
// For stream-silence mode: replace remaining buffer with silence.
|
|
|
|
// Tries to cause an instant buffer underrun.
|
|
|
|
static void soft_reset(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
snd_pcm_sframes_t frames = snd_pcm_rewindable(p->alsa);
|
|
|
|
if (frames > 0 && snd_pcm_state(p->alsa) == SND_PCM_STATE_RUNNING) {
|
|
|
|
frames = snd_pcm_rewind(p->alsa, frames);
|
|
|
|
if (frames < 0) {
|
|
|
|
int err = frames;
|
|
|
|
CHECK_ALSA_WARN("pcm rewind error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-08-09 14:52:33 +00:00
|
|
|
if (p->paused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p->delay_before_pause = get_delay(ao);
|
|
|
|
p->prepause_frames = p->delay_before_pause * ao->samplerate;
|
|
|
|
|
2016-08-09 14:35:33 +00:00
|
|
|
if (ao->stream_silence) {
|
2016-08-09 14:52:33 +00:00
|
|
|
soft_reset(ao);
|
2016-08-09 14:35:33 +00:00
|
|
|
} else if (p->can_pause) {
|
2014-03-09 20:32:00 +00:00
|
|
|
if (snd_pcm_state(p->alsa) == SND_PCM_STATE_RUNNING) {
|
|
|
|
err = snd_pcm_pause(p->alsa, 1);
|
|
|
|
CHECK_ALSA_ERROR("pcm pause error");
|
2016-08-09 14:52:33 +00:00
|
|
|
p->prepause_frames = 0;
|
2014-03-09 20:32:00 +00:00
|
|
|
}
|
2004-05-12 22:56:55 +00:00
|
|
|
} else {
|
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
|
|
|
|
2016-08-09 14:52:33 +00:00
|
|
|
p->paused = true;
|
|
|
|
|
2013-04-07 19:34:09 +00:00
|
|
|
alsa_error: ;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 02:59:14 +00:00
|
|
|
static void resume_device(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-12-23 02:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_resume(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
int err;
|
2014-01-02 15:09:27 +00:00
|
|
|
|
2016-08-09 14:52:33 +00:00
|
|
|
if (!p->paused)
|
|
|
|
return;
|
|
|
|
|
2014-12-23 12:20:32 +00:00
|
|
|
resume_device(ao);
|
|
|
|
|
2016-08-09 14:35:33 +00:00
|
|
|
if (ao->stream_silence) {
|
2016-08-09 14:52:33 +00:00
|
|
|
p->paused = false;
|
2016-08-09 14:35:33 +00:00
|
|
|
get_delay(ao); // recovers from underrun (as a side-effect)
|
|
|
|
} else 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");
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2013-04-07 19:34:09 +00:00
|
|
|
|
2016-08-09 14:52:33 +00:00
|
|
|
if (p->prepause_frames)
|
|
|
|
ao_play_silence(ao, p->prepause_frames);
|
|
|
|
|
2013-04-07 19:34:09 +00:00
|
|
|
alsa_error: ;
|
2016-08-09 14:52:33 +00:00
|
|
|
p->paused = false;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-08-09 14:52:33 +00:00
|
|
|
p->paused = false;
|
|
|
|
p->prepause_frames = 0;
|
|
|
|
p->delay_before_pause = 0;
|
2016-08-09 14:35:33 +00:00
|
|
|
|
|
|
|
if (ao->stream_silence) {
|
|
|
|
soft_reset(ao);
|
|
|
|
} else {
|
|
|
|
err = snd_pcm_drop(p->alsa);
|
|
|
|
CHECK_ALSA_ERROR("pcm prepare error");
|
|
|
|
err = snd_pcm_prepare(p->alsa);
|
|
|
|
CHECK_ALSA_ERROR("pcm prepare error");
|
|
|
|
}
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
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;
|
2017-11-23 14:50:50 +00:00
|
|
|
ao_convert_inplace(&p->convert, data, samples);
|
2013-04-07 19:34:09 +00:00
|
|
|
|
|
|
|
do {
|
2015-06-26 21:06:37 +00:00
|
|
|
if (af_fmt_is_planar(ao->format)) {
|
2013-11-10 22:39:08 +00:00
|
|
|
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
|
|
|
|
2014-11-25 10:10:44 +00:00
|
|
|
if (res == -EINTR || res == -EAGAIN) { /* retry */
|
2013-04-07 19:34:09 +00:00
|
|
|
res = 0;
|
2015-09-28 15:29:47 +00:00
|
|
|
} else if (!check_device_present(ao, res)) {
|
|
|
|
goto alsa_error;
|
2014-01-02 15:09:27 +00:00
|
|
|
} else if (res < 0) {
|
2014-12-23 02:59:14 +00:00
|
|
|
if (res == -ESTRPIPE) { /* suspend */
|
|
|
|
resume_device(ao);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
|
2016-08-09 14:52:33 +00:00
|
|
|
p->paused = false;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-29 21:57:18 +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");
|
|
|
|
|
2014-05-30 21:54:11 +00:00
|
|
|
while (1) {
|
|
|
|
int r = ao_wait_poll(ao, fds, num_fds, lock);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
unsigned short revents;
|
2017-02-27 15:25:47 +00:00
|
|
|
err = snd_pcm_poll_descriptors_revents(p->alsa, fds, num_fds, &revents);
|
2014-05-30 21:54:11 +00:00
|
|
|
CHECK_ALSA_ERROR("cannot read poll events");
|
|
|
|
|
2017-02-27 18:09:42 +00:00
|
|
|
if (revents & POLLERR) {
|
2017-03-14 14:43:36 +00:00
|
|
|
snd_pcm_status_t *status;
|
|
|
|
snd_pcm_status_alloca(&status);
|
|
|
|
|
|
|
|
err = snd_pcm_status(p->alsa, status);
|
|
|
|
check_device_present(ao, err);
|
2014-05-30 21:54:11 +00:00
|
|
|
return -1;
|
2017-02-27 18:09:42 +00:00
|
|
|
}
|
2014-05-30 21:54:11 +00:00
|
|
|
if (revents & POLLOUT)
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-29 21:57:18 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
alsa_error:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-24 18:47:58 +00:00
|
|
|
static bool is_useless_device(char *name)
|
|
|
|
{
|
2017-03-14 14:46:11 +00:00
|
|
|
char *crap[] = {"rear", "center_lfe", "side", "pulse", "null", "dsnoop", "hw"};
|
2015-11-24 18:47:58 +00:00
|
|
|
for (int i = 0; i < MP_ARRAY_SIZE(crap); i++) {
|
|
|
|
int l = strlen(crap[i]);
|
|
|
|
if (name && strncmp(name, crap[i], l) == 0 &&
|
|
|
|
(!name[l] || name[l] == ':'))
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-14 17:06:17 +00:00
|
|
|
// The standard default entry will achieve exactly the same.
|
|
|
|
if (name && strcmp(name, "default") == 0)
|
|
|
|
return true;
|
2015-11-24 18:47:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:27:21 +00:00
|
|
|
static void list_devs(struct ao *ao, struct ao_device_list *list)
|
2014-10-09 19:22:44 +00:00
|
|
|
{
|
|
|
|
void **hints;
|
|
|
|
if (snd_device_name_hint(-1, "pcm", &hints) < 0)
|
|
|
|
return;
|
|
|
|
|
2016-11-14 12:41:25 +00:00
|
|
|
ao_device_list_add(list, ao, &(struct ao_device_desc){"", ""});
|
|
|
|
|
2014-10-09 19:22:44 +00:00
|
|
|
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");
|
2015-11-24 18:47:58 +00:00
|
|
|
if (!is_useless_device(name) && (!io || strcmp(io, "Output") == 0)) {
|
2015-08-25 13:45:57 +00:00
|
|
|
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});
|
2014-10-09 19:22:44 +00:00
|
|
|
}
|
2015-08-25 13:45:57 +00:00
|
|
|
free(name);
|
|
|
|
free(desc);
|
|
|
|
free(io);
|
2014-10-09 19:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snd_device_name_free_hint(hints);
|
|
|
|
}
|
|
|
|
|
2013-04-18 22:16:23 +00:00
|
|
|
const struct ao_driver audio_out_alsa = {
|
2014-11-25 16:27:00 +00:00
|
|
|
.description = "ALSA audio output",
|
2013-10-23 17:07:27 +00:00
|
|
|
.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,
|
2014-05-29 21:57:18 +00:00
|
|
|
.wait = audio_wait,
|
|
|
|
.wakeup = ao_wakeup_poll,
|
2014-10-09 19:22:44 +00:00
|
|
|
.list_devs = list_devs,
|
2013-07-21 19:51:51 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
2016-09-05 19:04:17 +00:00
|
|
|
.global_opts = &ao_alsa_conf,
|
2013-04-18 22:16:23 +00:00
|
|
|
};
|