2009-01-26 16:06:44 +01:00
|
|
|
/*
|
2015-04-13 09:36:54 +02:00
|
|
|
* This file is part of mpv.
|
2009-01-26 16:06:44 +01:00
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2009-01-26 16:06:44 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-01-26 16:06:44 +01:00
|
|
|
* 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
|
2015-04-13 09:36:54 +02:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-26 16:06:44 +01:00
|
|
|
*/
|
|
|
|
|
2001-06-03 01:25:43 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-10-22 18:24:26 +02:00
|
|
|
#include <string.h>
|
2011-04-09 02:03:22 +02:00
|
|
|
#include <assert.h>
|
2001-06-03 01:25:43 +02:00
|
|
|
|
2016-01-11 19:03:40 +01:00
|
|
|
#include "mpv_talloc.h"
|
2011-05-04 13:55:15 +02:00
|
|
|
|
2004-12-07 03:24:15 +01:00
|
|
|
#include "config.h"
|
2012-11-09 01:06:43 +01:00
|
|
|
#include "ao.h"
|
2014-03-07 15:24:32 +01:00
|
|
|
#include "internal.h"
|
2013-06-16 18:47:02 +02:00
|
|
|
#include "audio/format.h"
|
2014-03-07 15:24:32 +01:00
|
|
|
#include "audio/audio.h"
|
2001-06-05 20:40:44 +02:00
|
|
|
|
2014-10-27 11:52:42 +01:00
|
|
|
#include "input/input.h"
|
2013-12-17 02:02:25 +01:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/m_config.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
2014-03-09 00:04:37 +01:00
|
|
|
#include "common/common.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/global.h"
|
2002-09-30 00:57:54 +02:00
|
|
|
|
2011-05-04 13:55:15 +02:00
|
|
|
extern const struct ao_driver audio_out_oss;
|
|
|
|
extern const struct ao_driver audio_out_coreaudio;
|
2014-06-30 19:09:03 +02:00
|
|
|
extern const struct ao_driver audio_out_coreaudio_exclusive;
|
2011-06-24 15:56:43 +02:00
|
|
|
extern const struct ao_driver audio_out_rsound;
|
2013-09-28 18:01:12 +02:00
|
|
|
extern const struct ao_driver audio_out_sndio;
|
2011-05-04 13:55:15 +02:00
|
|
|
extern const struct ao_driver audio_out_pulse;
|
|
|
|
extern const struct ao_driver audio_out_jack;
|
|
|
|
extern const struct ao_driver audio_out_openal;
|
2016-02-14 18:03:47 +01:00
|
|
|
extern const struct ao_driver audio_out_opensles;
|
2011-05-04 13:55:15 +02:00
|
|
|
extern const struct ao_driver audio_out_null;
|
|
|
|
extern const struct ao_driver audio_out_alsa;
|
2013-07-20 19:13:39 +02:00
|
|
|
extern const struct ao_driver audio_out_wasapi;
|
2011-05-04 13:55:15 +02:00
|
|
|
extern const struct ao_driver audio_out_pcm;
|
2012-09-14 17:51:26 +02:00
|
|
|
extern const struct ao_driver audio_out_lavc;
|
2012-12-28 08:07:14 +01:00
|
|
|
extern const struct ao_driver audio_out_sdl;
|
2011-05-04 13:55:15 +02:00
|
|
|
|
|
|
|
static const struct ao_driver * const audio_out_drivers[] = {
|
2002-10-06 03:08:04 +02:00
|
|
|
// native:
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_COREAUDIO
|
2011-05-04 00:15:58 +02:00
|
|
|
&audio_out_coreaudio,
|
2005-10-12 01:07:53 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_PULSE
|
2012-01-05 15:02:03 +01:00
|
|
|
&audio_out_pulse,
|
2001-08-28 12:54:31 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2011-05-04 00:15:58 +02:00
|
|
|
&audio_out_alsa,
|
2002-05-28 03:52:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_WASAPI
|
2013-07-20 04:42:58 +02:00
|
|
|
&audio_out_wasapi,
|
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2012-01-05 15:02:03 +01:00
|
|
|
&audio_out_oss,
|
2001-06-05 12:37:50 +02:00
|
|
|
#endif
|
2011-05-04 00:15:58 +02:00
|
|
|
// wrappers:
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_JACK
|
2011-05-04 00:15:58 +02:00
|
|
|
&audio_out_jack,
|
2004-06-25 20:11:15 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OPENAL
|
2011-05-04 00:15:58 +02:00
|
|
|
&audio_out_openal,
|
2012-12-28 08:07:14 +01:00
|
|
|
#endif
|
2016-02-14 18:03:47 +01:00
|
|
|
#if HAVE_OPENSLES
|
|
|
|
&audio_out_opensles,
|
|
|
|
#endif
|
2014-01-25 09:18:07 +01:00
|
|
|
#if HAVE_SDL1 || HAVE_SDL2
|
2012-12-28 08:07:14 +01:00
|
|
|
&audio_out_sdl,
|
2014-09-26 15:52:29 +02:00
|
|
|
#endif
|
|
|
|
#if HAVE_SNDIO
|
|
|
|
&audio_out_sndio,
|
2014-10-22 16:15:49 +02:00
|
|
|
#endif
|
2015-01-07 22:31:34 +01:00
|
|
|
&audio_out_null,
|
2014-10-22 16:15:49 +02:00
|
|
|
#if HAVE_COREAUDIO
|
|
|
|
&audio_out_coreaudio_exclusive,
|
2006-02-16 23:35:04 +01:00
|
|
|
#endif
|
2011-05-04 00:15:58 +02:00
|
|
|
&audio_out_pcm,
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ENCODING
|
2012-10-05 15:17:16 +02:00
|
|
|
&audio_out_lavc,
|
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_RSOUND
|
2011-06-24 15:56:43 +02:00
|
|
|
&audio_out_rsound,
|
|
|
|
#endif
|
2011-05-04 00:15:58 +02:00
|
|
|
NULL
|
2001-06-03 01:25:43 +02:00
|
|
|
};
|
2001-11-24 06:21:22 +01:00
|
|
|
|
2013-07-21 21:33:17 +02:00
|
|
|
static bool get_desc(struct m_obj_desc *dst, int index)
|
2011-05-04 00:15:58 +02:00
|
|
|
{
|
2013-07-21 21:33:17 +02:00
|
|
|
if (index >= MP_ARRAY_SIZE(audio_out_drivers) - 1)
|
|
|
|
return false;
|
|
|
|
const struct ao_driver *ao = audio_out_drivers[index];
|
|
|
|
*dst = (struct m_obj_desc) {
|
2013-10-23 19:07:27 +02:00
|
|
|
.name = ao->name,
|
|
|
|
.description = ao->description,
|
2013-07-21 21:33:17 +02:00
|
|
|
.priv_size = ao->priv_size,
|
|
|
|
.priv_defaults = ao->priv_defaults,
|
|
|
|
.options = ao->options,
|
2013-07-21 22:03:53 +02:00
|
|
|
.hidden = ao->encode,
|
2013-07-21 21:33:17 +02:00
|
|
|
.p = ao,
|
|
|
|
};
|
|
|
|
return true;
|
2002-09-30 00:57:54 +02:00
|
|
|
}
|
2001-11-24 06:21:22 +01:00
|
|
|
|
2013-07-21 21:33:17 +02:00
|
|
|
// For the ao option
|
|
|
|
const struct m_obj_list ao_obj_list = {
|
|
|
|
.get_desc = get_desc,
|
|
|
|
.description = "audio outputs",
|
|
|
|
.allow_unknown_entries = true,
|
|
|
|
.allow_trailer = true,
|
2016-09-01 14:21:32 +02:00
|
|
|
.disallow_positional_parameters = true,
|
2013-07-21 21:33:17 +02:00
|
|
|
};
|
2011-04-09 02:03:22 +02:00
|
|
|
|
2014-10-10 18:27:21 +02:00
|
|
|
static struct ao *ao_alloc(bool probing, struct mpv_global *global,
|
|
|
|
struct input_ctx *input_ctx, char *name, char **args)
|
2013-02-06 22:54:03 +01:00
|
|
|
{
|
2014-10-09 21:21:31 +02:00
|
|
|
struct MPOpts *opts = global->opts;
|
2013-08-01 17:41:32 +02:00
|
|
|
struct mp_log *log = mp_log_new(NULL, global->log, "ao");
|
2013-07-21 21:33:17 +02:00
|
|
|
struct m_obj_desc desc;
|
|
|
|
if (!m_obj_list_find(&desc, &ao_obj_list, bstr0(name))) {
|
2013-12-21 21:49:13 +01:00
|
|
|
mp_msg(log, MSGL_ERR, "Audio output %s not found!\n", name);
|
2013-08-01 17:41:32 +02:00
|
|
|
talloc_free(log);
|
2013-07-21 21:33:17 +02:00
|
|
|
return NULL;
|
|
|
|
};
|
|
|
|
struct ao *ao = talloc_ptrtype(NULL, ao);
|
2013-08-01 17:41:32 +02:00
|
|
|
talloc_steal(ao, log);
|
2013-07-21 21:33:17 +02:00
|
|
|
*ao = (struct ao) {
|
|
|
|
.driver = desc.p,
|
|
|
|
.probing = probing,
|
|
|
|
.input_ctx = input_ctx,
|
2013-08-01 17:41:32 +02:00
|
|
|
.log = mp_log_new(ao, log, name),
|
2014-10-09 21:21:31 +02:00
|
|
|
.def_buffer = opts->audio_buffer,
|
2015-02-12 13:54:02 +01:00
|
|
|
.client_name = talloc_strdup(ao, opts->audio_client_name),
|
2013-07-21 21:33:17 +02:00
|
|
|
};
|
2016-09-02 14:49:34 +02:00
|
|
|
struct m_config *config =
|
|
|
|
m_config_from_obj_desc_and_args(ao, ao->log, global, &desc,
|
|
|
|
name, opts->ao_defs, args);
|
|
|
|
if (!config)
|
2013-07-21 21:33:17 +02:00
|
|
|
goto error;
|
2013-07-22 22:57:51 +02:00
|
|
|
ao->priv = config->optstruct;
|
2014-10-10 18:27:21 +02:00
|
|
|
return ao;
|
|
|
|
error:
|
|
|
|
talloc_free(ao);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-20 14:25:47 +01:00
|
|
|
static struct ao *ao_init(bool probing, struct mpv_global *global,
|
|
|
|
struct input_ctx *input_ctx,
|
2016-08-04 20:49:20 +02:00
|
|
|
struct encode_lavc_context *encode_lavc_ctx, int flags,
|
2015-01-20 14:25:47 +01:00
|
|
|
int samplerate, int format, struct mp_chmap channels,
|
|
|
|
char *dev, char *name, char **args)
|
2014-10-10 18:27:21 +02:00
|
|
|
{
|
|
|
|
struct ao *ao = ao_alloc(probing, global, input_ctx, name, args);
|
2014-10-22 16:57:28 +02:00
|
|
|
if (!ao)
|
2014-10-22 17:12:08 +02:00
|
|
|
return NULL;
|
2014-10-10 18:27:21 +02:00
|
|
|
ao->samplerate = samplerate;
|
|
|
|
ao->channels = channels;
|
|
|
|
ao->format = format;
|
|
|
|
ao->encode_lavc_ctx = encode_lavc_ctx;
|
2016-08-04 20:49:20 +02:00
|
|
|
ao->init_flags = flags;
|
2015-01-20 14:25:47 +01:00
|
|
|
if (ao->driver->encode != !!ao->encode_lavc_ctx)
|
|
|
|
goto fail;
|
2014-10-09 21:21:31 +02:00
|
|
|
|
2013-11-09 23:18:09 +01:00
|
|
|
MP_VERBOSE(ao, "requested format: %d Hz, %s channels, %s\n",
|
2014-11-24 19:56:01 +01:00
|
|
|
ao->samplerate, mp_chmap_to_str(&ao->channels),
|
|
|
|
af_fmt_to_str(ao->format));
|
2014-03-09 00:04:37 +01:00
|
|
|
|
2015-01-20 14:25:47 +01:00
|
|
|
ao->device = talloc_strdup(ao, dev);
|
|
|
|
|
2014-03-09 00:04:37 +01:00
|
|
|
ao->api = ao->driver->play ? &ao_api_push : &ao_api_pull;
|
|
|
|
ao->api_priv = talloc_zero_size(ao, ao->api->priv_size);
|
|
|
|
assert(!ao->api->priv_defaults && !ao->api->options);
|
|
|
|
|
2016-08-09 16:22:06 +02:00
|
|
|
ao->stream_silence = flags & AO_INIT_STREAM_SILENCE;
|
|
|
|
|
2014-10-22 17:12:08 +02:00
|
|
|
int r = ao->driver->init(ao);
|
2015-01-20 14:25:47 +01:00
|
|
|
if (r < 0) {
|
|
|
|
// Silly exception for coreaudio spdif redirection
|
|
|
|
if (ao->redirect) {
|
|
|
|
char redirect[80], rdevice[80];
|
|
|
|
snprintf(redirect, sizeof(redirect), "%s", ao->redirect);
|
|
|
|
snprintf(rdevice, sizeof(rdevice), "%s", ao->device ? ao->device : "");
|
|
|
|
talloc_free(ao);
|
2016-08-04 20:49:20 +02:00
|
|
|
return ao_init(probing, global, input_ctx, encode_lavc_ctx, flags,
|
2015-07-03 19:23:09 +02:00
|
|
|
samplerate, format, channels, rdevice, redirect, NULL);
|
2015-01-20 14:25:47 +01:00
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-09 00:04:37 +01:00
|
|
|
|
2015-06-26 23:06:37 +02:00
|
|
|
ao->sstride = af_fmt_to_bytes(ao->format);
|
2014-03-09 00:04:37 +01:00
|
|
|
ao->num_planes = 1;
|
2015-06-26 23:06:37 +02:00
|
|
|
if (af_fmt_is_planar(ao->format)) {
|
2014-03-09 00:04:37 +01:00
|
|
|
ao->num_planes = ao->channels.num;
|
|
|
|
} else {
|
2013-11-10 23:24:21 +01:00
|
|
|
ao->sstride *= ao->channels.num;
|
2014-03-09 00:04:37 +01:00
|
|
|
}
|
2013-11-10 23:24:21 +01:00
|
|
|
ao->bps = ao->samplerate * ao->sstride;
|
2014-03-09 00:04:37 +01:00
|
|
|
|
2014-11-18 12:49:31 +01:00
|
|
|
if (!ao->device_buffer && ao->driver->get_space)
|
2014-03-09 00:04:37 +01:00
|
|
|
ao->device_buffer = ao->driver->get_space(ao);
|
2014-11-18 12:49:31 +01:00
|
|
|
if (ao->device_buffer)
|
2014-03-14 22:37:19 +01:00
|
|
|
MP_VERBOSE(ao, "device buffer: %d samples.\n", ao->device_buffer);
|
2014-09-04 23:48:27 +02:00
|
|
|
ao->buffer = MPMAX(ao->device_buffer, ao->def_buffer * ao->samplerate);
|
2015-03-13 20:49:22 +01:00
|
|
|
|
|
|
|
int align = af_format_sample_alignment(ao->format);
|
|
|
|
ao->buffer = (ao->buffer + align - 1) / align * align;
|
2014-03-09 00:04:37 +01:00
|
|
|
MP_VERBOSE(ao, "using soft-buffer of %d samples.\n", ao->buffer);
|
|
|
|
|
2015-01-20 14:25:47 +01:00
|
|
|
if (ao->api->init(ao) < 0)
|
|
|
|
goto fail;
|
|
|
|
return ao;
|
2014-03-09 00:04:37 +01:00
|
|
|
|
2015-01-20 14:25:47 +01:00
|
|
|
fail:
|
2013-07-21 21:33:17 +02:00
|
|
|
talloc_free(ao);
|
|
|
|
return NULL;
|
2013-02-06 22:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-01-20 14:25:47 +01:00
|
|
|
static void split_ao_device(void *tmp, char *opt, char **out_ao, char **out_dev)
|
|
|
|
{
|
|
|
|
*out_ao = NULL;
|
|
|
|
*out_dev = NULL;
|
|
|
|
if (!opt)
|
|
|
|
return;
|
|
|
|
if (!opt[0] || strcmp(opt, "auto") == 0)
|
|
|
|
return;
|
|
|
|
// Split on "/". If there's no "/", leave out_device NULL.
|
|
|
|
bstr b_dev, b_ao;
|
|
|
|
if (bstr_split_tok(bstr0(opt), "/", &b_ao, &b_dev))
|
|
|
|
*out_dev = bstrto0(tmp, b_dev);
|
|
|
|
*out_ao = bstrto0(tmp, b_ao);
|
|
|
|
}
|
|
|
|
|
2013-08-01 17:41:32 +02:00
|
|
|
struct ao *ao_init_best(struct mpv_global *global,
|
2016-08-04 20:49:20 +02:00
|
|
|
int init_flags,
|
2013-07-21 21:33:17 +02:00
|
|
|
struct input_ctx *input_ctx,
|
|
|
|
struct encode_lavc_context *encode_lavc_ctx,
|
|
|
|
int samplerate, int format, struct mp_chmap channels)
|
2011-04-09 02:03:22 +02:00
|
|
|
{
|
2014-10-09 21:21:31 +02:00
|
|
|
struct MPOpts *opts = global->opts;
|
2015-01-20 14:25:47 +01:00
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
struct mp_log *log = mp_log_new(tmp, global->log, "ao");
|
2013-12-21 18:43:03 +01:00
|
|
|
struct ao *ao = NULL;
|
2015-10-05 18:40:42 +02:00
|
|
|
struct m_obj_settings *ao_list = NULL;
|
|
|
|
int ao_num = 0;
|
|
|
|
|
|
|
|
for (int n = 0; opts->audio_driver_list && opts->audio_driver_list[n].name; n++)
|
|
|
|
MP_TARRAY_APPEND(tmp, ao_list, ao_num, opts->audio_driver_list[n]);
|
2015-01-20 14:25:47 +01:00
|
|
|
|
|
|
|
bool forced_dev = false;
|
|
|
|
char *pref_ao, *pref_dev;
|
|
|
|
split_ao_device(tmp, opts->audio_device, &pref_ao, &pref_dev);
|
2015-10-05 18:40:42 +02:00
|
|
|
if (!ao_num && pref_ao) {
|
2015-01-20 14:25:47 +01:00
|
|
|
// Reuse the autoselection code
|
2015-10-05 18:40:42 +02:00
|
|
|
MP_TARRAY_APPEND(tmp, ao_list, ao_num,
|
|
|
|
(struct m_obj_settings){.name = pref_ao});
|
2015-01-20 14:25:47 +01:00
|
|
|
forced_dev = true;
|
|
|
|
}
|
|
|
|
|
2015-10-05 18:40:42 +02:00
|
|
|
bool autoprobe = ao_num == 0;
|
|
|
|
|
|
|
|
// Something like "--ao=a,b," means do autoprobing after a and b fail.
|
|
|
|
if (ao_num && strlen(ao_list[ao_num - 1].name) == 0) {
|
|
|
|
ao_num -= 1;
|
|
|
|
autoprobe = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoprobe) {
|
|
|
|
for (int n = 0; audio_out_drivers[n]; n++) {
|
|
|
|
const struct ao_driver *driver = audio_out_drivers[n];
|
|
|
|
if (driver == &audio_out_null)
|
|
|
|
break;
|
|
|
|
MP_TARRAY_APPEND(tmp, ao_list, ao_num,
|
|
|
|
(struct m_obj_settings){.name = (char *)driver->name});
|
2004-10-13 18:22:59 +02:00
|
|
|
}
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
2015-01-20 14:25:47 +01:00
|
|
|
|
2016-08-04 20:49:20 +02:00
|
|
|
if (init_flags & AO_INIT_NULL_FALLBACK) {
|
2015-10-05 18:53:02 +02:00
|
|
|
MP_TARRAY_APPEND(tmp, ao_list, ao_num,
|
|
|
|
(struct m_obj_settings){.name = "null"});
|
|
|
|
}
|
|
|
|
|
2015-10-05 18:40:42 +02:00
|
|
|
for (int n = 0; n < ao_num; n++) {
|
|
|
|
struct m_obj_settings *entry = &ao_list[n];
|
|
|
|
bool probing = n + 1 != ao_num;
|
|
|
|
mp_verbose(log, "Trying audio driver '%s'\n", entry->name);
|
|
|
|
char *dev = NULL;
|
|
|
|
if (pref_ao && pref_dev && strcmp(entry->name, pref_ao) == 0) {
|
|
|
|
dev = pref_dev;
|
|
|
|
mp_verbose(log, "Using preferred device '%s'\n", dev);
|
|
|
|
}
|
2016-08-04 20:49:20 +02:00
|
|
|
ao = ao_init(probing, global, input_ctx, encode_lavc_ctx, init_flags,
|
2015-10-05 18:40:42 +02:00
|
|
|
samplerate, format, channels, dev,
|
|
|
|
entry->name, entry->attribs);
|
2013-07-21 21:33:17 +02:00
|
|
|
if (ao)
|
2015-10-05 18:40:42 +02:00
|
|
|
break;
|
|
|
|
if (!probing)
|
|
|
|
mp_err(log, "Failed to initialize audio driver '%s'\n", entry->name);
|
|
|
|
if (dev && forced_dev) {
|
|
|
|
mp_err(log, "This audio driver/device was forced with the "
|
|
|
|
"--audio-device option.\nTry unsetting it.\n");
|
|
|
|
}
|
2002-09-30 00:57:54 +02:00
|
|
|
}
|
2015-01-20 14:25:47 +01:00
|
|
|
|
|
|
|
talloc_free(tmp);
|
2013-12-21 18:43:03 +01:00
|
|
|
return ao;
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-03-09 00:49:39 +01:00
|
|
|
// Uninitialize and destroy the AO. Remaining audio must be dropped.
|
|
|
|
void ao_uninit(struct ao *ao)
|
2011-04-09 02:03:22 +02:00
|
|
|
{
|
2015-06-02 21:03:04 +02:00
|
|
|
if (ao)
|
|
|
|
ao->api->uninit(ao);
|
2011-05-04 13:55:15 +02:00
|
|
|
talloc_free(ao);
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-28 00:56:10 +01:00
|
|
|
// Queue the given audio data. Start playback if it hasn't started yet. Return
|
|
|
|
// the number of samples that was accepted (the core will try to queue the rest
|
|
|
|
// again later). Should never block.
|
|
|
|
// data: start pointer for each plane. If the audio data is packed, only
|
|
|
|
// data[0] is valid, otherwise there is a plane for each channel.
|
|
|
|
// samples: size of the audio data (see ao->sstride)
|
|
|
|
// flags: currently AOPLAY_FINAL_CHUNK can be set
|
2013-11-10 23:38:18 +01:00
|
|
|
int ao_play(struct ao *ao, void **data, int samples, int flags)
|
2011-04-09 02:03:22 +02:00
|
|
|
{
|
2014-03-09 00:04:37 +01:00
|
|
|
return ao->api->play(ao, data, samples, flags);
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2012-04-07 15:26:56 +02:00
|
|
|
int ao_control(struct ao *ao, enum aocontrol cmd, void *arg)
|
2011-04-09 02:03:22 +02:00
|
|
|
{
|
2014-09-06 02:30:57 +02:00
|
|
|
return ao->api->control ? ao->api->control(ao, cmd, arg) : CONTROL_UNKNOWN;
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-28 00:56:10 +01:00
|
|
|
// Return size of the buffered data in seconds. Can include the device latency.
|
|
|
|
// Basically, this returns how much data there is still to play, and how long
|
|
|
|
// it takes until the last sample in the buffer reaches the speakers. This is
|
|
|
|
// used for audio/video synchronization, so it's very important to implement
|
|
|
|
// this correctly.
|
2011-04-09 02:03:22 +02:00
|
|
|
double ao_get_delay(struct ao *ao)
|
|
|
|
{
|
2014-03-09 00:04:37 +01:00
|
|
|
return ao->api->get_delay(ao);
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-28 00:56:10 +01:00
|
|
|
// Return free size of the internal audio buffer. This controls how much audio
|
|
|
|
// the core should decode and try to queue with ao_play().
|
2011-04-09 02:03:22 +02:00
|
|
|
int ao_get_space(struct ao *ao)
|
|
|
|
{
|
2014-03-09 00:04:37 +01:00
|
|
|
return ao->api->get_space(ao);
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-28 00:56:10 +01:00
|
|
|
// Stop playback and empty buffers. Essentially go back to the state after
|
|
|
|
// ao->init().
|
2011-04-09 02:03:22 +02:00
|
|
|
void ao_reset(struct ao *ao)
|
|
|
|
{
|
2014-03-09 00:04:37 +01:00
|
|
|
if (ao->api->reset)
|
|
|
|
ao->api->reset(ao);
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-28 00:56:10 +01:00
|
|
|
// Pause playback. Keep the current buffer. ao_get_delay() must return the
|
|
|
|
// same value as before pausing.
|
2011-04-09 02:03:22 +02:00
|
|
|
void ao_pause(struct ao *ao)
|
|
|
|
{
|
2014-03-09 00:04:37 +01:00
|
|
|
if (ao->api->pause)
|
|
|
|
ao->api->pause(ao);
|
2011-04-09 02:03:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-28 00:56:10 +01:00
|
|
|
// Resume playback. Play the remaining buffer. If the driver doesn't support
|
|
|
|
// pausing, it has to work around this and e.g. use ao_play_silence() to fill
|
|
|
|
// the lost audio.
|
2011-04-09 02:03:22 +02:00
|
|
|
void ao_resume(struct ao *ao)
|
|
|
|
{
|
2014-03-09 00:04:37 +01:00
|
|
|
if (ao->api->resume)
|
|
|
|
ao->api->resume(ao);
|
2011-05-04 13:55:15 +02:00
|
|
|
}
|
|
|
|
|
2014-03-09 00:49:39 +01:00
|
|
|
// Block until the current audio buffer has played completely.
|
|
|
|
void ao_drain(struct ao *ao)
|
|
|
|
{
|
2014-07-13 20:06:33 +02:00
|
|
|
if (ao->api->drain)
|
2014-03-09 00:49:39 +01:00
|
|
|
ao->api->drain(ao);
|
2013-11-10 23:05:51 +01:00
|
|
|
}
|
|
|
|
|
2014-04-17 23:48:09 +02:00
|
|
|
bool ao_eof_reached(struct ao *ao)
|
|
|
|
{
|
2014-09-05 22:21:06 +02:00
|
|
|
return ao->api->get_eof ? ao->api->get_eof(ao) : true;
|
2014-04-17 23:48:09 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 09:58:44 +01:00
|
|
|
// Query the AO_EVENT_*s as requested by the events parameter, and return them.
|
|
|
|
int ao_query_and_reset_events(struct ao *ao, int events)
|
|
|
|
{
|
2015-05-11 23:27:41 +02:00
|
|
|
return atomic_fetch_and(&ao->events_, ~(unsigned)events) & events;
|
2014-11-09 09:58:44 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:27:41 +02:00
|
|
|
static void ao_add_events(struct ao *ao, int events)
|
2014-11-09 09:58:44 +01:00
|
|
|
{
|
2015-05-11 23:27:41 +02:00
|
|
|
atomic_fetch_or(&ao->events_, events);
|
2015-02-12 16:53:56 +01:00
|
|
|
if (ao->input_ctx)
|
|
|
|
mp_input_wakeup(ao->input_ctx);
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:27:41 +02:00
|
|
|
// Request that the player core destroys and recreates the AO. Fully thread-safe.
|
|
|
|
void ao_request_reload(struct ao *ao)
|
|
|
|
{
|
|
|
|
ao_add_events(ao, AO_EVENT_RELOAD);
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:53:56 +01:00
|
|
|
// Notify the player that the device list changed. Fully thread-safe.
|
|
|
|
void ao_hotplug_event(struct ao *ao)
|
|
|
|
{
|
2015-05-11 23:27:41 +02:00
|
|
|
ao_add_events(ao, AO_EVENT_HOTPLUG);
|
2014-11-09 09:58:44 +01:00
|
|
|
}
|
|
|
|
|
2013-05-09 15:12:16 +02:00
|
|
|
bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,
|
|
|
|
struct mp_chmap *map)
|
|
|
|
{
|
2015-10-26 15:52:08 +01:00
|
|
|
MP_VERBOSE(ao, "Channel layouts:\n");
|
|
|
|
mp_chmal_sel_log(s, ao->log, MSGL_V);
|
2015-03-16 20:29:52 +01:00
|
|
|
bool r = mp_chmap_sel_adjust(s, map);
|
2015-10-26 15:52:08 +01:00
|
|
|
if (r)
|
|
|
|
MP_VERBOSE(ao, "result: %s\n", mp_chmap_to_str(map));
|
2015-03-16 20:29:52 +01:00
|
|
|
return r;
|
2013-05-09 15:12:16 +02:00
|
|
|
}
|
2011-05-04 13:55:15 +02:00
|
|
|
|
2016-08-04 20:49:20 +02:00
|
|
|
// safe_multichannel=true behaves like ao_chmap_sel_adjust.
|
|
|
|
// safe_multichannel=false is a helper for callers which do not support safe
|
|
|
|
// handling of arbitrary channel layouts. If the multichannel layouts are not
|
|
|
|
// considered "always safe" (e.g. HDMI), then allow only stereo or mono, if
|
|
|
|
// they are part of the list in *s.
|
|
|
|
bool ao_chmap_sel_adjust2(struct ao *ao, const struct mp_chmap_sel *s,
|
|
|
|
struct mp_chmap *map, bool safe_multichannel)
|
|
|
|
{
|
|
|
|
if (!safe_multichannel && (ao->init_flags & AO_INIT_SAFE_MULTICHANNEL_ONLY)) {
|
|
|
|
struct mp_chmap res = *map;
|
|
|
|
if (mp_chmap_sel_adjust(s, &res)) {
|
|
|
|
if (!mp_chmap_equals(&res, &(struct mp_chmap)MP_CHMAP_INIT_MONO) &&
|
|
|
|
!mp_chmap_equals(&res, &(struct mp_chmap)MP_CHMAP_INIT_STEREO))
|
|
|
|
{
|
|
|
|
MP_WARN(ao, "Disabling multichannel output.\n");
|
|
|
|
*map = (struct mp_chmap)MP_CHMAP_INIT_STEREO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ao_chmap_sel_adjust(ao, s, map);
|
|
|
|
}
|
|
|
|
|
2013-05-09 15:12:16 +02:00
|
|
|
bool ao_chmap_sel_get_def(struct ao *ao, const struct mp_chmap_sel *s,
|
|
|
|
struct mp_chmap *map, int num)
|
|
|
|
{
|
|
|
|
return mp_chmap_sel_get_def(s, map, num);
|
|
|
|
}
|
2014-03-07 15:24:32 +01:00
|
|
|
|
|
|
|
// --- The following functions just return immutable information.
|
|
|
|
|
|
|
|
void ao_get_format(struct ao *ao, struct mp_audio *format)
|
|
|
|
{
|
|
|
|
*format = (struct mp_audio){0};
|
|
|
|
mp_audio_set_format(format, ao->format);
|
|
|
|
mp_audio_set_channels(format, &ao->channels);
|
|
|
|
format->rate = ao->samplerate;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ao_get_name(struct ao *ao)
|
|
|
|
{
|
|
|
|
return ao->driver->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ao_get_description(struct ao *ao)
|
|
|
|
{
|
|
|
|
return ao->driver->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ao_untimed(struct ao *ao)
|
|
|
|
{
|
|
|
|
return ao->untimed;
|
|
|
|
}
|
2014-10-09 21:21:31 +02:00
|
|
|
|
2015-02-12 16:53:56 +01:00
|
|
|
// ---
|
|
|
|
|
|
|
|
struct ao_hotplug {
|
|
|
|
struct mpv_global *global;
|
|
|
|
struct input_ctx *input_ctx;
|
|
|
|
// A single AO instance is used to listen to hotplug events. It wouldn't
|
|
|
|
// make much sense to allow multiple AO drivers; all sane platforms have
|
|
|
|
// a single such audio API.
|
|
|
|
// This is _not_ the same AO instance as used for playing audio.
|
|
|
|
struct ao *ao;
|
|
|
|
// cached
|
|
|
|
struct ao_device_list *list;
|
|
|
|
bool needs_update;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ao_hotplug *ao_hotplug_create(struct mpv_global *global,
|
|
|
|
struct input_ctx *input_ctx)
|
|
|
|
{
|
|
|
|
struct ao_hotplug *hp = talloc_ptrtype(NULL, hp);
|
|
|
|
*hp = (struct ao_hotplug){
|
|
|
|
.global = global,
|
|
|
|
.input_ctx = input_ctx,
|
|
|
|
.needs_update = true,
|
|
|
|
};
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_devices(struct ao *ao, struct ao_device_list *list)
|
|
|
|
{
|
|
|
|
int num = list->num_devices;
|
2016-06-29 17:38:57 +02:00
|
|
|
if (ao->driver->list_devs) {
|
2015-02-12 16:53:56 +01:00
|
|
|
ao->driver->list_devs(ao, list);
|
2016-06-29 17:38:57 +02:00
|
|
|
} else {
|
2015-11-27 14:42:10 +01:00
|
|
|
char name[80] = "Default";
|
|
|
|
if (num > 1)
|
|
|
|
mp_snprintf_cat(name, sizeof(name), " (%s)", ao->driver->name);
|
|
|
|
ao_device_list_add(list, ao, &(struct ao_device_desc){"", name});
|
|
|
|
}
|
2015-02-12 16:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ao_hotplug_check_update(struct ao_hotplug *hp)
|
|
|
|
{
|
|
|
|
if (hp->ao && ao_query_and_reset_events(hp->ao, AO_EVENT_HOTPLUG)) {
|
|
|
|
hp->needs_update = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-14 12:43:55 +01:00
|
|
|
const char *ao_hotplug_get_detected_device(struct ao_hotplug *hp)
|
|
|
|
{
|
2015-02-14 16:41:08 +01:00
|
|
|
if (!hp || !hp->ao)
|
|
|
|
return NULL;
|
2015-02-14 12:43:55 +01:00
|
|
|
return hp->ao->detected_device;
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:53:56 +01:00
|
|
|
// The return value is valid until the next call to this API.
|
|
|
|
struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp)
|
2014-10-09 21:21:31 +02:00
|
|
|
{
|
2015-02-12 16:53:56 +01:00
|
|
|
if (hp->list && !hp->needs_update)
|
|
|
|
return hp->list;
|
|
|
|
|
|
|
|
talloc_free(hp->list);
|
|
|
|
struct ao_device_list *list = talloc_zero(hp, struct ao_device_list);
|
|
|
|
hp->list = list;
|
|
|
|
|
2014-10-13 16:42:44 +02:00
|
|
|
MP_TARRAY_APPEND(list, list->devices, list->num_devices,
|
|
|
|
(struct ao_device_desc){"auto", "Autoselect device"});
|
2015-02-12 16:53:56 +01:00
|
|
|
|
2014-10-09 21:21:31 +02:00
|
|
|
for (int n = 0; audio_out_drivers[n]; n++) {
|
|
|
|
const struct ao_driver *d = audio_out_drivers[n];
|
2014-10-22 16:16:35 +02:00
|
|
|
if (d == &audio_out_null)
|
|
|
|
break; // don't add unsafe/special entries
|
2015-02-12 16:53:56 +01:00
|
|
|
|
|
|
|
struct ao *ao = ao_alloc(true, hp->global, hp->input_ctx,
|
|
|
|
(char *)d->name, NULL);
|
2014-10-10 18:27:21 +02:00
|
|
|
if (!ao)
|
|
|
|
continue;
|
2015-02-12 16:53:56 +01:00
|
|
|
|
|
|
|
if (ao->driver->hotplug_init) {
|
|
|
|
if (!hp->ao && ao->driver->hotplug_init(ao) >= 0)
|
|
|
|
hp->ao = ao; // keep this one
|
|
|
|
if (hp->ao && hp->ao->driver == d)
|
|
|
|
get_devices(hp->ao, list);
|
|
|
|
} else {
|
|
|
|
get_devices(ao, list);
|
|
|
|
}
|
|
|
|
if (ao != hp->ao)
|
|
|
|
talloc_free(ao);
|
2014-10-09 21:21:31 +02:00
|
|
|
}
|
2015-02-12 16:53:56 +01:00
|
|
|
hp->needs_update = false;
|
2014-10-09 21:21:31 +02:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:27:21 +02:00
|
|
|
void ao_device_list_add(struct ao_device_list *list, struct ao *ao,
|
2014-10-09 21:21:31 +02:00
|
|
|
struct ao_device_desc *e)
|
|
|
|
{
|
|
|
|
struct ao_device_desc c = *e;
|
2014-10-10 18:27:21 +02:00
|
|
|
const char *dname = ao->driver->name;
|
|
|
|
c.name = c.name[0] ? talloc_asprintf(list, "%s/%s", dname, c.name)
|
|
|
|
: talloc_strdup(list, dname);
|
2014-10-09 21:21:31 +02:00
|
|
|
c.desc = talloc_strdup(list, c.desc);
|
|
|
|
MP_TARRAY_APPEND(list, list->devices, list->num_devices, c);
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:53:56 +01:00
|
|
|
void ao_hotplug_destroy(struct ao_hotplug *hp)
|
|
|
|
{
|
|
|
|
if (!hp)
|
|
|
|
return;
|
|
|
|
if (hp->ao && hp->ao->driver->hotplug_uninit)
|
|
|
|
hp->ao->driver->hotplug_uninit(hp->ao);
|
|
|
|
talloc_free(hp->ao);
|
|
|
|
talloc_free(hp);
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:27:21 +02:00
|
|
|
void ao_print_devices(struct mpv_global *global, struct mp_log *log)
|
2014-10-09 21:21:31 +02:00
|
|
|
{
|
2015-02-12 16:53:56 +01:00
|
|
|
struct ao_hotplug *hp = ao_hotplug_create(global, NULL);
|
|
|
|
struct ao_device_list *list = ao_hotplug_get_device_list(hp);
|
2014-10-09 21:21:31 +02:00
|
|
|
mp_info(log, "List of detected audio devices:\n");
|
|
|
|
for (int n = 0; n < list->num_devices; n++) {
|
|
|
|
struct ao_device_desc *desc = &list->devices[n];
|
2014-10-19 16:36:38 +02:00
|
|
|
mp_info(log, " '%s' (%s)\n", desc->name, desc->desc);
|
2014-10-09 21:21:31 +02:00
|
|
|
}
|
2015-02-12 16:53:56 +01:00
|
|
|
ao_hotplug_destroy(hp);
|
2014-10-09 21:21:31 +02:00
|
|
|
}
|