2009-01-26 15:06:44 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
audio/out: change license of some core files to LGPL
All contributors of the current code have agreed. ao.c requires a
"driver" entry for each audio output - we assume that if someone who
didn't agree to LGPL added a line, it's fine for ao.c to be LGPL
anyway. If the affected audio output is not disabled at compilation
time, the resulting binary will be GPL anyway, and ootherwise the
code is not included.
The audio output code itself was inspired or partially copied from
libao in 7a2eec4b59f4 (thus why MPlayer's audio code is named libao2).
Just to be sure we got permission from Aaron Holtzman, Jack Moffitt, and
Stan Seibert, who according to libao's SVN history and README are the
initial author. (Something similar was done for libvo, although the
commit relicensing it forgot to mention it.)
242aa6ebd40: anders mostly disagreed with the LGPL relicensing, but we
got permission for this particular commit.
0ef8e555735: nick could not be reached, but the include statement was
removed again anyway.
879e05a7c17: iive agreed to LGPL v3+ only, but this line of code was
removed anyway, so ao_null.c can be LGPL v2.1+.
9dd8f241ac2: patch author could not be reached, but the corresponding
code (old slave mode interface) was completely removed later.
2017-05-20 09:35:25 +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
|
audio/out: change license of some core files to LGPL
All contributors of the current code have agreed. ao.c requires a
"driver" entry for each audio output - we assume that if someone who
didn't agree to LGPL added a line, it's fine for ao.c to be LGPL
anyway. If the affected audio output is not disabled at compilation
time, the resulting binary will be GPL anyway, and ootherwise the
code is not included.
The audio output code itself was inspired or partially copied from
libao in 7a2eec4b59f4 (thus why MPlayer's audio code is named libao2).
Just to be sure we got permission from Aaron Holtzman, Jack Moffitt, and
Stan Seibert, who according to libao's SVN history and README are the
initial author. (Something similar was done for libvo, although the
commit relicensing it forgot to mention it.)
242aa6ebd40: anders mostly disagreed with the LGPL relicensing, but we
got permission for this particular commit.
0ef8e555735: nick could not be reached, but the include statement was
removed again anyway.
879e05a7c17: iive agreed to LGPL v3+ only, but this line of code was
removed anyway, so ao_null.c can be LGPL v2.1+.
9dd8f241ac2: patch author could not be reached, but the corresponding
code (old slave mode interface) was completely removed later.
2017-05-20 09:35:25 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
audio/out: change license of some core files to LGPL
All contributors of the current code have agreed. ao.c requires a
"driver" entry for each audio output - we assume that if someone who
didn't agree to LGPL added a line, it's fine for ao.c to be LGPL
anyway. If the affected audio output is not disabled at compilation
time, the resulting binary will be GPL anyway, and ootherwise the
code is not included.
The audio output code itself was inspired or partially copied from
libao in 7a2eec4b59f4 (thus why MPlayer's audio code is named libao2).
Just to be sure we got permission from Aaron Holtzman, Jack Moffitt, and
Stan Seibert, who according to libao's SVN history and README are the
initial author. (Something similar was done for libvo, although the
commit relicensing it forgot to mention it.)
242aa6ebd40: anders mostly disagreed with the LGPL relicensing, but we
got permission for this particular commit.
0ef8e555735: nick could not be reached, but the include statement was
removed again anyway.
879e05a7c17: iive agreed to LGPL v3+ only, but this line of code was
removed anyway, so ao_null.c can be LGPL v2.1+.
9dd8f241ac2: patch author could not be reached, but the corresponding
code (old slave mode interface) was completely removed later.
2017-05-20 09:35:25 +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
|
|
|
*/
|
|
|
|
|
2001-06-02 23:25:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-10-22 16:24:26 +00:00
|
|
|
#include <string.h>
|
audio: add audio softvol processing to AO
This does what af_volume used to do. Since we couldn't relicense it,
just rewrite it. Since we don't have a new filter mechanism yet, and the
libavfilter is too inconvenient, do applying the volume gain in ao.c
directly. This is done before handling the audio data to the driver.
Since push.c runs a separate thread, and pull.c is called asynchronously
from the audio driver's thread, the volume value needs to be
synchronized. There's no existing central mutex, so do some shit with
atomics. Since there's no atomic_float type predefined (which is at
least needed when using the legacy wrapper), do some nonsense about
reinterpret casting the float value to an int for the purpose of atomic
access. Not sure if using memcpy() is undefined behavior, but for now I
don't care.
The advantage of not using a filter is lower complexity (no filter auto
insertion), and lower latency (gain processing is done after our
internal audio buffer of at least 200ms).
Disavdantages include inability to use native volume control _before_
other filters with custom filter chains, and the need to add new
processing for each new sample type.
Since this doesn't reuse any of the old GPL code, nor does indirectly
rely on it, volume and replaygain handling now works in LGPL mode.
How to process the gain is inspired by libavfilter's af_volume (LGPL).
In particular, we use exactly the same rounding, and we quantize
processing for integer sample types by 256 steps. Some of libavfilter's
copyright may or may not apply, but I think not, and it's the same
license anyway.
2017-11-29 20:30:10 +00:00
|
|
|
#include <math.h>
|
2011-04-09 00:03:22 +00:00
|
|
|
#include <assert.h>
|
2001-06-02 23:25:43 +00:00
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2011-05-04 11:55:15 +00:00
|
|
|
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "config.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2013-06-16 16:47:02 +00:00
|
|
|
#include "audio/format.h"
|
2001-06-05 18:40:44 +00:00
|
|
|
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2020-03-12 22:07:05 +00:00
|
|
|
#include "options/m_config_frontend.h"
|
2017-07-07 15:35:09 +00:00
|
|
|
#include "osdep/endian.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-03-08 23:04:37 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/global.h"
|
2002-09-29 22:57:54 +00:00
|
|
|
|
2020-11-24 01:22:40 +00:00
|
|
|
extern const struct ao_driver audio_out_oss;
|
2018-03-10 03:28:09 +00:00
|
|
|
extern const struct ao_driver audio_out_audiotrack;
|
2016-10-19 22:08:48 +00:00
|
|
|
extern const struct ao_driver audio_out_audiounit;
|
2011-05-04 11:55:15 +00:00
|
|
|
extern const struct ao_driver audio_out_coreaudio;
|
2014-06-30 17:09:03 +00:00
|
|
|
extern const struct ao_driver audio_out_coreaudio_exclusive;
|
2011-06-24 13:56:43 +00:00
|
|
|
extern const struct ao_driver audio_out_rsound;
|
2022-01-06 20:54:21 +00:00
|
|
|
extern const struct ao_driver audio_out_pipewire;
|
2011-05-04 11:55:15 +00: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 17:03:47 +00:00
|
|
|
extern const struct ao_driver audio_out_opensles;
|
2011-05-04 11:55:15 +00:00
|
|
|
extern const struct ao_driver audio_out_null;
|
|
|
|
extern const struct ao_driver audio_out_alsa;
|
2013-07-20 17:13:39 +00:00
|
|
|
extern const struct ao_driver audio_out_wasapi;
|
2011-05-04 11:55:15 +00:00
|
|
|
extern const struct ao_driver audio_out_pcm;
|
2012-09-14 15:51:26 +00:00
|
|
|
extern const struct ao_driver audio_out_lavc;
|
2012-12-28 07:07:14 +00:00
|
|
|
extern const struct ao_driver audio_out_sdl;
|
2011-05-04 11:55:15 +00:00
|
|
|
|
|
|
|
static const struct ao_driver * const audio_out_drivers[] = {
|
2002-10-06 01:08:04 +00:00
|
|
|
// native:
|
2018-03-10 03:28:09 +00:00
|
|
|
#if HAVE_ANDROID
|
|
|
|
&audio_out_audiotrack,
|
|
|
|
#endif
|
2016-10-19 22:08:48 +00:00
|
|
|
#if HAVE_AUDIOUNIT
|
|
|
|
&audio_out_audiounit,
|
|
|
|
#endif
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_COREAUDIO
|
2011-05-03 22:15:58 +00:00
|
|
|
&audio_out_coreaudio,
|
2005-10-11 23:07:53 +00:00
|
|
|
#endif
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_PULSE
|
2012-01-05 14:02:03 +00:00
|
|
|
&audio_out_pulse,
|
2001-08-28 10:54:31 +00:00
|
|
|
#endif
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_ALSA
|
2011-05-03 22:15:58 +00:00
|
|
|
&audio_out_alsa,
|
2002-05-28 01:52:40 +00:00
|
|
|
#endif
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_WASAPI
|
2013-07-20 02:42:58 +00:00
|
|
|
&audio_out_wasapi,
|
2020-11-24 01:22:40 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_OSS_AUDIO
|
|
|
|
&audio_out_oss,
|
2001-06-05 10:37:50 +00:00
|
|
|
#endif
|
2011-05-03 22:15:58 +00:00
|
|
|
// wrappers:
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_JACK
|
2011-05-03 22:15:58 +00:00
|
|
|
&audio_out_jack,
|
2004-06-25 18:11:15 +00:00
|
|
|
#endif
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_OPENAL
|
2011-05-03 22:15:58 +00:00
|
|
|
&audio_out_openal,
|
2012-12-28 07:07:14 +00:00
|
|
|
#endif
|
2016-02-14 17:03:47 +00:00
|
|
|
#if HAVE_OPENSLES
|
|
|
|
&audio_out_opensles,
|
|
|
|
#endif
|
2019-10-22 14:41:19 +00:00
|
|
|
#if HAVE_SDL2_AUDIO
|
2012-12-28 07:07:14 +00:00
|
|
|
&audio_out_sdl,
|
2022-01-06 20:54:21 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_PIPEWIRE
|
|
|
|
&audio_out_pipewire,
|
2014-10-22 14:15:49 +00:00
|
|
|
#endif
|
2015-01-07 21:31:34 +00:00
|
|
|
&audio_out_null,
|
2014-10-22 14:15:49 +00:00
|
|
|
#if HAVE_COREAUDIO
|
|
|
|
&audio_out_coreaudio_exclusive,
|
2006-02-16 22:35:04 +00:00
|
|
|
#endif
|
2011-05-03 22:15:58 +00:00
|
|
|
&audio_out_pcm,
|
2012-10-05 13:17:16 +00:00
|
|
|
&audio_out_lavc,
|
2011-05-03 22:15:58 +00:00
|
|
|
NULL
|
2001-06-02 23:25:43 +00:00
|
|
|
};
|
2001-11-24 05:21:22 +00:00
|
|
|
|
2013-07-21 19:33:17 +00:00
|
|
|
static bool get_desc(struct m_obj_desc *dst, int index)
|
2011-05-03 22:15:58 +00:00
|
|
|
{
|
2013-07-21 19:33:17 +00: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 17:07:27 +00:00
|
|
|
.name = ao->name,
|
|
|
|
.description = ao->description,
|
2013-07-21 19:33:17 +00:00
|
|
|
.priv_size = ao->priv_size,
|
|
|
|
.priv_defaults = ao->priv_defaults,
|
|
|
|
.options = ao->options,
|
2016-11-25 20:00:39 +00:00
|
|
|
.options_prefix = ao->options_prefix,
|
2016-09-05 19:04:17 +00:00
|
|
|
.global_opts = ao->global_opts,
|
2013-07-21 20:03:53 +00:00
|
|
|
.hidden = ao->encode,
|
2013-07-21 19:33:17 +00:00
|
|
|
.p = ao,
|
|
|
|
};
|
|
|
|
return true;
|
2002-09-29 22:57:54 +00:00
|
|
|
}
|
2001-11-24 05:21:22 +00:00
|
|
|
|
2013-07-21 19:33:17 +00:00
|
|
|
// For the ao option
|
2018-05-21 13:11:19 +00:00
|
|
|
static const struct m_obj_list ao_obj_list = {
|
2013-07-21 19:33:17 +00:00
|
|
|
.get_desc = get_desc,
|
|
|
|
.description = "audio outputs",
|
|
|
|
.allow_trailer = true,
|
2016-09-01 12:21:32 +00:00
|
|
|
.disallow_positional_parameters = true,
|
2016-11-25 20:00:39 +00:00
|
|
|
.use_global_options = true,
|
2013-07-21 19:33:17 +00:00
|
|
|
};
|
2011-04-09 00:03:22 +00:00
|
|
|
|
2018-05-21 13:11:19 +00:00
|
|
|
#define OPT_BASE_STRUCT struct ao_opts
|
|
|
|
const struct m_sub_options ao_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"ao", OPT_SETTINGSLIST(audio_driver_list, &ao_obj_list),
|
|
|
|
.flags = UPDATE_AUDIO},
|
|
|
|
{"audio-device", OPT_STRING(audio_device), .flags = UPDATE_AUDIO},
|
|
|
|
{"audio-client-name", OPT_STRING(audio_client_name), .flags = UPDATE_AUDIO},
|
|
|
|
{"audio-buffer", OPT_DOUBLE(audio_buffer),
|
|
|
|
.flags = UPDATE_AUDIO, M_RANGE(0, 10)},
|
2018-05-21 13:11:19 +00:00
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(OPT_BASE_STRUCT),
|
|
|
|
.defaults = &(const OPT_BASE_STRUCT){
|
|
|
|
.audio_buffer = 0.2,
|
|
|
|
.audio_device = "auto",
|
|
|
|
.audio_client_name = "mpv",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2014-10-10 16:27:21 +00:00
|
|
|
static struct ao *ao_alloc(bool probing, struct mpv_global *global,
|
2016-09-16 12:23:54 +00:00
|
|
|
void (*wakeup_cb)(void *ctx), void *wakeup_ctx,
|
2016-11-25 20:00:39 +00:00
|
|
|
char *name)
|
2013-02-06 21:54:03 +00:00
|
|
|
{
|
2016-09-16 12:23:54 +00:00
|
|
|
assert(wakeup_cb);
|
|
|
|
|
2013-08-01 15:41:32 +00:00
|
|
|
struct mp_log *log = mp_log_new(NULL, global->log, "ao");
|
2013-07-21 19:33:17 +00:00
|
|
|
struct m_obj_desc desc;
|
|
|
|
if (!m_obj_list_find(&desc, &ao_obj_list, bstr0(name))) {
|
2013-12-21 20:49:13 +00:00
|
|
|
mp_msg(log, MSGL_ERR, "Audio output %s not found!\n", name);
|
2013-08-01 15:41:32 +00:00
|
|
|
talloc_free(log);
|
2013-07-21 19:33:17 +00:00
|
|
|
return NULL;
|
|
|
|
};
|
2018-05-21 13:11:19 +00:00
|
|
|
struct ao_opts *opts = mp_get_config_group(NULL, global, &ao_conf);
|
2013-07-21 19:33:17 +00:00
|
|
|
struct ao *ao = talloc_ptrtype(NULL, ao);
|
2013-08-01 15:41:32 +00:00
|
|
|
talloc_steal(ao, log);
|
2013-07-21 19:33:17 +00:00
|
|
|
*ao = (struct ao) {
|
|
|
|
.driver = desc.p,
|
|
|
|
.probing = probing,
|
2016-09-02 18:07:25 +00:00
|
|
|
.global = global,
|
2016-09-16 12:23:54 +00:00
|
|
|
.wakeup_cb = wakeup_cb,
|
|
|
|
.wakeup_ctx = wakeup_ctx,
|
2013-08-01 15:41:32 +00:00
|
|
|
.log = mp_log_new(ao, log, name),
|
2014-10-09 19:21:31 +00:00
|
|
|
.def_buffer = opts->audio_buffer,
|
2015-02-12 12:54:02 +00:00
|
|
|
.client_name = talloc_strdup(ao, opts->audio_client_name),
|
2013-07-21 19:33:17 +00:00
|
|
|
};
|
2018-05-21 13:11:19 +00:00
|
|
|
talloc_free(opts);
|
2016-11-25 20:00:39 +00:00
|
|
|
ao->priv = m_config_group_from_desc(ao, ao->log, global, &desc, name);
|
|
|
|
if (!ao->priv)
|
2013-07-21 19:33:17 +00:00
|
|
|
goto error;
|
audio: add audio softvol processing to AO
This does what af_volume used to do. Since we couldn't relicense it,
just rewrite it. Since we don't have a new filter mechanism yet, and the
libavfilter is too inconvenient, do applying the volume gain in ao.c
directly. This is done before handling the audio data to the driver.
Since push.c runs a separate thread, and pull.c is called asynchronously
from the audio driver's thread, the volume value needs to be
synchronized. There's no existing central mutex, so do some shit with
atomics. Since there's no atomic_float type predefined (which is at
least needed when using the legacy wrapper), do some nonsense about
reinterpret casting the float value to an int for the purpose of atomic
access. Not sure if using memcpy() is undefined behavior, but for now I
don't care.
The advantage of not using a filter is lower complexity (no filter auto
insertion), and lower latency (gain processing is done after our
internal audio buffer of at least 200ms).
Disavdantages include inability to use native volume control _before_
other filters with custom filter chains, and the need to add new
processing for each new sample type.
Since this doesn't reuse any of the old GPL code, nor does indirectly
rely on it, volume and replaygain handling now works in LGPL mode.
How to process the gain is inspired by libavfilter's af_volume (LGPL).
In particular, we use exactly the same rounding, and we quantize
processing for integer sample types by 256 steps. Some of libavfilter's
copyright may or may not apply, but I think not, and it's the same
license anyway.
2017-11-29 20:30:10 +00:00
|
|
|
ao_set_gain(ao, 1.0f);
|
2014-10-10 16:27:21 +00:00
|
|
|
return ao;
|
|
|
|
error:
|
|
|
|
talloc_free(ao);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-20 13:25:47 +00:00
|
|
|
static struct ao *ao_init(bool probing, struct mpv_global *global,
|
2016-09-16 12:23:54 +00:00
|
|
|
void (*wakeup_cb)(void *ctx), void *wakeup_ctx,
|
2016-08-04 18:49:20 +00:00
|
|
|
struct encode_lavc_context *encode_lavc_ctx, int flags,
|
2015-01-20 13:25:47 +00:00
|
|
|
int samplerate, int format, struct mp_chmap channels,
|
2016-11-25 20:00:39 +00:00
|
|
|
char *dev, char *name)
|
2014-10-10 16:27:21 +00:00
|
|
|
{
|
2016-11-25 20:00:39 +00:00
|
|
|
struct ao *ao = ao_alloc(probing, global, wakeup_cb, wakeup_ctx, name);
|
2014-10-22 14:57:28 +00:00
|
|
|
if (!ao)
|
2014-10-22 15:12:08 +00:00
|
|
|
return NULL;
|
2014-10-10 16:27:21 +00:00
|
|
|
ao->samplerate = samplerate;
|
|
|
|
ao->channels = channels;
|
|
|
|
ao->format = format;
|
|
|
|
ao->encode_lavc_ctx = encode_lavc_ctx;
|
2016-08-04 18:49:20 +00:00
|
|
|
ao->init_flags = flags;
|
2015-01-20 13:25:47 +00:00
|
|
|
if (ao->driver->encode != !!ao->encode_lavc_ctx)
|
|
|
|
goto fail;
|
2014-10-09 19:21:31 +00:00
|
|
|
|
2013-11-09 22:18:09 +00:00
|
|
|
MP_VERBOSE(ao, "requested format: %d Hz, %s channels, %s\n",
|
2014-11-24 18:56:01 +00:00
|
|
|
ao->samplerate, mp_chmap_to_str(&ao->channels),
|
|
|
|
af_fmt_to_str(ao->format));
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2015-01-20 13:25:47 +00:00
|
|
|
ao->device = talloc_strdup(ao, dev);
|
2016-08-09 14:22:06 +00:00
|
|
|
ao->stream_silence = flags & AO_INIT_STREAM_SILENCE;
|
|
|
|
|
audio: merge pull/push ring buffer glue code
This is preparation to further cleanups (and eventually actual
improvements) of the audio output code.
AOs are split into two classes: pull and push. Pull AOs let an audio
callback of the native audio API read from a ring buffer. Push AOs
expose a function that works similar to write(), and for which we start
a "feeder" thread. It seems making this split was beneficial, because of
the different data flow, and emulating the one or other in the AOs
directly would have created code duplication (all the "pull" AOs had
their own ring buffer implementation before it was cleaned up).
Unfortunately, both types had completely separate implementations (in
pull.c and push.c). The idea was that little can be shared anyway. But
that's very annoying now, because I want to change the API between AO
and player.
This commit attempts to merge them. I've moved everything from push.c to
pull.c, the trivial entrypoints from ao.c to pull.c, and attempted to
reconcile the differences. It's a mess, but at least there's only one
ring buffer within the AO code now. Everything should work mostly the
same. Pull AOs now always copy the audio data under a lock; before this
commit, all ring buffer access was lock-free (except for the decoder
wakeup callback, which acquired a mutex). In theory, this is "bad", and
people obsessed with lock-free stuff will hate me, but in practice
probably won't matter. The planned change will probably remove this
copying-under-lock again, but who knows when this will happen.
One change for the push AOs now makes it drop audio, where before only a
warning was logged. This is only in case of AOs or drivers which exhibit
unexpected (and now unsupported) behavior.
This is a risky change. Although it's completely trivial conceptually,
there are too many special cases. In addition, I barely tested it, and
I've messed with it in a half-motivated state over a longer time, barely
making any progress, and finishing it under a rush when I already should
have been asleep. Most things seem to work, and I made superficial tests
with alsa, sdl, and encode mode. This should cover most things, but
there are a lot of tricky things that received no coverage. All this
text means you should be prepared to roll back to an older commit and
report your problem.
2020-05-24 23:53:41 +00:00
|
|
|
init_buffer_pre(ao);
|
|
|
|
|
2014-10-22 15:12:08 +00:00
|
|
|
int r = ao->driver->init(ao);
|
2015-01-20 13:25:47 +00: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 : "");
|
audio: merge pull/push ring buffer glue code
This is preparation to further cleanups (and eventually actual
improvements) of the audio output code.
AOs are split into two classes: pull and push. Pull AOs let an audio
callback of the native audio API read from a ring buffer. Push AOs
expose a function that works similar to write(), and for which we start
a "feeder" thread. It seems making this split was beneficial, because of
the different data flow, and emulating the one or other in the AOs
directly would have created code duplication (all the "pull" AOs had
their own ring buffer implementation before it was cleaned up).
Unfortunately, both types had completely separate implementations (in
pull.c and push.c). The idea was that little can be shared anyway. But
that's very annoying now, because I want to change the API between AO
and player.
This commit attempts to merge them. I've moved everything from push.c to
pull.c, the trivial entrypoints from ao.c to pull.c, and attempted to
reconcile the differences. It's a mess, but at least there's only one
ring buffer within the AO code now. Everything should work mostly the
same. Pull AOs now always copy the audio data under a lock; before this
commit, all ring buffer access was lock-free (except for the decoder
wakeup callback, which acquired a mutex). In theory, this is "bad", and
people obsessed with lock-free stuff will hate me, but in practice
probably won't matter. The planned change will probably remove this
copying-under-lock again, but who knows when this will happen.
One change for the push AOs now makes it drop audio, where before only a
warning was logged. This is only in case of AOs or drivers which exhibit
unexpected (and now unsupported) behavior.
This is a risky change. Although it's completely trivial conceptually,
there are too many special cases. In addition, I barely tested it, and
I've messed with it in a half-motivated state over a longer time, barely
making any progress, and finishing it under a rush when I already should
have been asleep. Most things seem to work, and I made superficial tests
with alsa, sdl, and encode mode. This should cover most things, but
there are a lot of tricky things that received no coverage. All this
text means you should be prepared to roll back to an older commit and
report your problem.
2020-05-24 23:53:41 +00:00
|
|
|
ao_uninit(ao);
|
2016-09-16 12:23:54 +00:00
|
|
|
return ao_init(probing, global, wakeup_cb, wakeup_ctx,
|
|
|
|
encode_lavc_ctx, flags, samplerate, format, channels,
|
2016-11-25 20:00:39 +00:00
|
|
|
rdevice, redirect);
|
2015-01-20 13:25:47 +00:00
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
audio: merge pull/push ring buffer glue code
This is preparation to further cleanups (and eventually actual
improvements) of the audio output code.
AOs are split into two classes: pull and push. Pull AOs let an audio
callback of the native audio API read from a ring buffer. Push AOs
expose a function that works similar to write(), and for which we start
a "feeder" thread. It seems making this split was beneficial, because of
the different data flow, and emulating the one or other in the AOs
directly would have created code duplication (all the "pull" AOs had
their own ring buffer implementation before it was cleaned up).
Unfortunately, both types had completely separate implementations (in
pull.c and push.c). The idea was that little can be shared anyway. But
that's very annoying now, because I want to change the API between AO
and player.
This commit attempts to merge them. I've moved everything from push.c to
pull.c, the trivial entrypoints from ao.c to pull.c, and attempted to
reconcile the differences. It's a mess, but at least there's only one
ring buffer within the AO code now. Everything should work mostly the
same. Pull AOs now always copy the audio data under a lock; before this
commit, all ring buffer access was lock-free (except for the decoder
wakeup callback, which acquired a mutex). In theory, this is "bad", and
people obsessed with lock-free stuff will hate me, but in practice
probably won't matter. The planned change will probably remove this
copying-under-lock again, but who knows when this will happen.
One change for the push AOs now makes it drop audio, where before only a
warning was logged. This is only in case of AOs or drivers which exhibit
unexpected (and now unsupported) behavior.
This is a risky change. Although it's completely trivial conceptually,
there are too many special cases. In addition, I barely tested it, and
I've messed with it in a half-motivated state over a longer time, barely
making any progress, and finishing it under a rush when I already should
have been asleep. Most things seem to work, and I made superficial tests
with alsa, sdl, and encode mode. This should cover most things, but
there are a lot of tricky things that received no coverage. All this
text means you should be prepared to roll back to an older commit and
report your problem.
2020-05-24 23:53:41 +00:00
|
|
|
ao->driver_initialized = true;
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2015-06-26 21:06:37 +00:00
|
|
|
ao->sstride = af_fmt_to_bytes(ao->format);
|
2014-03-08 23:04:37 +00:00
|
|
|
ao->num_planes = 1;
|
2015-06-26 21:06:37 +00:00
|
|
|
if (af_fmt_is_planar(ao->format)) {
|
2014-03-08 23:04:37 +00:00
|
|
|
ao->num_planes = ao->channels.num;
|
|
|
|
} else {
|
2013-11-10 22:24:21 +00:00
|
|
|
ao->sstride *= ao->channels.num;
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
2013-11-10 22:24:21 +00:00
|
|
|
ao->bps = ao->samplerate * ao->sstride;
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2020-06-09 14:49:05 +00:00
|
|
|
if (ao->device_buffer <= 0 && ao->driver->write) {
|
audio: redo internal AO API
This affects "pull" AOs only: ao_alsa, ao_pulse, ao_openal, ao_pcm,
ao_lavc. There are changes to the other AOs too, but that's only about
renaming ao_driver.resume to ao_driver.start.
ao_openal is broken because I didn't manage to fix it, so it exits with
an error message. If you want it, why don't _you_ put effort into it? I
see no reason to waste my own precious lifetime over this (I realize the
irony).
ao_alsa loses the poll() mechanism, but it was mostly broken and didn't
really do what it was supposed to. There doesn't seem to be anything in
the ALSA API to watch the playback status without polling (unless you
want to use raw UNIX signals).
No idea if ao_pulse is correct, or whether it's subtly broken now. There
is no documentation, so I can't tell what is correct, without reverse
engineering the whole project. I recommend using ALSA.
This was supposed to be just a simple fix, but somehow it expanded scope
like a train wreck. Very high chance of regressions, but probably only
for the AOs listed above. The rest you can figure out from reading the
diff.
2020-05-31 13:00:35 +00:00
|
|
|
MP_ERR(ao, "Device buffer size not set.\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-11-18 11:49:31 +00:00
|
|
|
if (ao->device_buffer)
|
2014-03-14 21:37:19 +00:00
|
|
|
MP_VERBOSE(ao, "device buffer: %d samples.\n", ao->device_buffer);
|
2014-09-04 21:48:27 +00:00
|
|
|
ao->buffer = MPMAX(ao->device_buffer, ao->def_buffer * ao->samplerate);
|
2018-03-07 21:58:47 +00:00
|
|
|
ao->buffer = MPMAX(ao->buffer, 1);
|
2015-03-13 19:49:22 +00:00
|
|
|
|
|
|
|
int align = af_format_sample_alignment(ao->format);
|
|
|
|
ao->buffer = (ao->buffer + align - 1) / align * align;
|
2014-03-08 23:04:37 +00:00
|
|
|
MP_VERBOSE(ao, "using soft-buffer of %d samples.\n", ao->buffer);
|
|
|
|
|
audio: merge pull/push ring buffer glue code
This is preparation to further cleanups (and eventually actual
improvements) of the audio output code.
AOs are split into two classes: pull and push. Pull AOs let an audio
callback of the native audio API read from a ring buffer. Push AOs
expose a function that works similar to write(), and for which we start
a "feeder" thread. It seems making this split was beneficial, because of
the different data flow, and emulating the one or other in the AOs
directly would have created code duplication (all the "pull" AOs had
their own ring buffer implementation before it was cleaned up).
Unfortunately, both types had completely separate implementations (in
pull.c and push.c). The idea was that little can be shared anyway. But
that's very annoying now, because I want to change the API between AO
and player.
This commit attempts to merge them. I've moved everything from push.c to
pull.c, the trivial entrypoints from ao.c to pull.c, and attempted to
reconcile the differences. It's a mess, but at least there's only one
ring buffer within the AO code now. Everything should work mostly the
same. Pull AOs now always copy the audio data under a lock; before this
commit, all ring buffer access was lock-free (except for the decoder
wakeup callback, which acquired a mutex). In theory, this is "bad", and
people obsessed with lock-free stuff will hate me, but in practice
probably won't matter. The planned change will probably remove this
copying-under-lock again, but who knows when this will happen.
One change for the push AOs now makes it drop audio, where before only a
warning was logged. This is only in case of AOs or drivers which exhibit
unexpected (and now unsupported) behavior.
This is a risky change. Although it's completely trivial conceptually,
there are too many special cases. In addition, I barely tested it, and
I've messed with it in a half-motivated state over a longer time, barely
making any progress, and finishing it under a rush when I already should
have been asleep. Most things seem to work, and I made superficial tests
with alsa, sdl, and encode mode. This should cover most things, but
there are a lot of tricky things that received no coverage. All this
text means you should be prepared to roll back to an older commit and
report your problem.
2020-05-24 23:53:41 +00:00
|
|
|
if (!init_buffer_post(ao))
|
2015-01-20 13:25:47 +00:00
|
|
|
goto fail;
|
|
|
|
return ao;
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2015-01-20 13:25:47 +00:00
|
|
|
fail:
|
audio: merge pull/push ring buffer glue code
This is preparation to further cleanups (and eventually actual
improvements) of the audio output code.
AOs are split into two classes: pull and push. Pull AOs let an audio
callback of the native audio API read from a ring buffer. Push AOs
expose a function that works similar to write(), and for which we start
a "feeder" thread. It seems making this split was beneficial, because of
the different data flow, and emulating the one or other in the AOs
directly would have created code duplication (all the "pull" AOs had
their own ring buffer implementation before it was cleaned up).
Unfortunately, both types had completely separate implementations (in
pull.c and push.c). The idea was that little can be shared anyway. But
that's very annoying now, because I want to change the API between AO
and player.
This commit attempts to merge them. I've moved everything from push.c to
pull.c, the trivial entrypoints from ao.c to pull.c, and attempted to
reconcile the differences. It's a mess, but at least there's only one
ring buffer within the AO code now. Everything should work mostly the
same. Pull AOs now always copy the audio data under a lock; before this
commit, all ring buffer access was lock-free (except for the decoder
wakeup callback, which acquired a mutex). In theory, this is "bad", and
people obsessed with lock-free stuff will hate me, but in practice
probably won't matter. The planned change will probably remove this
copying-under-lock again, but who knows when this will happen.
One change for the push AOs now makes it drop audio, where before only a
warning was logged. This is only in case of AOs or drivers which exhibit
unexpected (and now unsupported) behavior.
This is a risky change. Although it's completely trivial conceptually,
there are too many special cases. In addition, I barely tested it, and
I've messed with it in a half-motivated state over a longer time, barely
making any progress, and finishing it under a rush when I already should
have been asleep. Most things seem to work, and I made superficial tests
with alsa, sdl, and encode mode. This should cover most things, but
there are a lot of tricky things that received no coverage. All this
text means you should be prepared to roll back to an older commit and
report your problem.
2020-05-24 23:53:41 +00:00
|
|
|
ao_uninit(ao);
|
2013-07-21 19:33:17 +00:00
|
|
|
return NULL;
|
2013-02-06 21:54:03 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 13:25:47 +00: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;
|
2017-02-21 06:46:48 +00:00
|
|
|
// Split on "/". If "/" is the final character, or absent, out_dev is NULL.
|
2015-01-20 13:25:47 +00:00
|
|
|
bstr b_dev, b_ao;
|
2017-02-21 06:46:48 +00:00
|
|
|
bstr_split_tok(bstr0(opt), "/", &b_ao, &b_dev);
|
|
|
|
if (b_dev.len > 0)
|
2015-01-20 13:25:47 +00:00
|
|
|
*out_dev = bstrto0(tmp, b_dev);
|
|
|
|
*out_ao = bstrto0(tmp, b_ao);
|
|
|
|
}
|
|
|
|
|
2013-08-01 15:41:32 +00:00
|
|
|
struct ao *ao_init_best(struct mpv_global *global,
|
2016-08-04 18:49:20 +00:00
|
|
|
int init_flags,
|
2016-09-16 12:23:54 +00:00
|
|
|
void (*wakeup_cb)(void *ctx), void *wakeup_ctx,
|
2013-07-21 19:33:17 +00:00
|
|
|
struct encode_lavc_context *encode_lavc_ctx,
|
|
|
|
int samplerate, int format, struct mp_chmap channels)
|
2011-04-09 00:03:22 +00:00
|
|
|
{
|
2015-01-20 13:25:47 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
2018-05-21 13:11:19 +00:00
|
|
|
struct ao_opts *opts = mp_get_config_group(tmp, global, &ao_conf);
|
2015-01-20 13:25:47 +00:00
|
|
|
struct mp_log *log = mp_log_new(tmp, global->log, "ao");
|
2013-12-21 17:43:03 +00:00
|
|
|
struct ao *ao = NULL;
|
2015-10-05 16:40:42 +00: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 13:25:47 +00:00
|
|
|
|
|
|
|
bool forced_dev = false;
|
|
|
|
char *pref_ao, *pref_dev;
|
|
|
|
split_ao_device(tmp, opts->audio_device, &pref_ao, &pref_dev);
|
2015-10-05 16:40:42 +00:00
|
|
|
if (!ao_num && pref_ao) {
|
2015-01-20 13:25:47 +00:00
|
|
|
// Reuse the autoselection code
|
2015-10-05 16:40:42 +00:00
|
|
|
MP_TARRAY_APPEND(tmp, ao_list, ao_num,
|
|
|
|
(struct m_obj_settings){.name = pref_ao});
|
2015-01-20 13:25:47 +00:00
|
|
|
forced_dev = true;
|
|
|
|
}
|
|
|
|
|
2015-10-05 16:40:42 +00: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 16:22:59 +00:00
|
|
|
}
|
2011-04-09 00:03:22 +00:00
|
|
|
}
|
2015-01-20 13:25:47 +00:00
|
|
|
|
2016-08-04 18:49:20 +00:00
|
|
|
if (init_flags & AO_INIT_NULL_FALLBACK) {
|
2015-10-05 16:53:02 +00:00
|
|
|
MP_TARRAY_APPEND(tmp, ao_list, ao_num,
|
|
|
|
(struct m_obj_settings){.name = "null"});
|
|
|
|
}
|
|
|
|
|
2015-10-05 16:40:42 +00: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-09-16 12:23:54 +00:00
|
|
|
ao = ao_init(probing, global, wakeup_cb, wakeup_ctx, encode_lavc_ctx,
|
2016-11-25 20:00:39 +00:00
|
|
|
init_flags, samplerate, format, channels, dev, entry->name);
|
2013-07-21 19:33:17 +00:00
|
|
|
if (ao)
|
2015-10-05 16:40:42 +00: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-29 22:57:54 +00:00
|
|
|
}
|
2015-01-20 13:25:47 +00:00
|
|
|
|
|
|
|
talloc_free(tmp);
|
2013-12-21 17:43:03 +00:00
|
|
|
return ao;
|
2011-04-09 00:03:22 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 08:58:44 +00: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 21:27:41 +00:00
|
|
|
return atomic_fetch_and(&ao->events_, ~(unsigned)events) & events;
|
2014-11-09 08:58:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 00:06:02 +00:00
|
|
|
// Returns events that were set by this calls.
|
|
|
|
int ao_add_events(struct ao *ao, int events)
|
2014-11-09 08:58:44 +00:00
|
|
|
{
|
2020-02-13 00:06:02 +00:00
|
|
|
unsigned prev_events = atomic_fetch_or(&ao->events_, events);
|
|
|
|
unsigned new = events & ~prev_events;
|
|
|
|
if (new)
|
|
|
|
ao->wakeup_cb(ao->wakeup_ctx);
|
|
|
|
return new;
|
2015-02-12 15:53:56 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 21:27:41 +00: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 15:53:56 +00:00
|
|
|
// Notify the player that the device list changed. Fully thread-safe.
|
|
|
|
void ao_hotplug_event(struct ao *ao)
|
|
|
|
{
|
2015-05-11 21:27:41 +00:00
|
|
|
ao_add_events(ao, AO_EVENT_HOTPLUG);
|
2014-11-09 08:58:44 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 13:12:16 +00:00
|
|
|
bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,
|
|
|
|
struct mp_chmap *map)
|
|
|
|
{
|
2015-10-26 14:52:08 +00:00
|
|
|
MP_VERBOSE(ao, "Channel layouts:\n");
|
|
|
|
mp_chmal_sel_log(s, ao->log, MSGL_V);
|
2015-03-16 19:29:52 +00:00
|
|
|
bool r = mp_chmap_sel_adjust(s, map);
|
2015-10-26 14:52:08 +00:00
|
|
|
if (r)
|
|
|
|
MP_VERBOSE(ao, "result: %s\n", mp_chmap_to_str(map));
|
2015-03-16 19:29:52 +00:00
|
|
|
return r;
|
2013-05-09 13:12:16 +00:00
|
|
|
}
|
2011-05-04 11:55:15 +00:00
|
|
|
|
2016-08-04 18:49:20 +00: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))
|
|
|
|
{
|
2017-04-02 15:23:11 +00:00
|
|
|
MP_VERBOSE(ao, "Disabling multichannel output.\n");
|
2016-08-04 18:49:20 +00:00
|
|
|
*map = (struct mp_chmap)MP_CHMAP_INIT_STEREO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ao_chmap_sel_adjust(ao, s, map);
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:12:16 +00: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 14:24:32 +00:00
|
|
|
|
|
|
|
// --- The following functions just return immutable information.
|
|
|
|
|
audio: introduce a new type to hold audio frames
This is pretty pointless, but I believe it allows us to claim that the
new code is not affected by the copyright of the old code. This is
needed, because the original mp_audio struct was written by someone who
has disagreed with LGPL relicensing (it was called af_data at the time,
and was defined in af.h).
The "GPL'ed" struct contents that surive are pretty trivial: just the
data pointer, and some metadata like the format, samplerate, etc. - but
at least in this case, any new code would be extremely similar anyway,
and I'm not really sure whether it's OK to claim different copyright. So
what we do is we just use AVFrame (which of course is LGPL with 100%
certainty), and add some accessors around it to adapt it to mpv
conventions.
Also, this gets rid of some annoying conventions of mp_audio, like the
struct fields that require using an accessor to write to them anyway.
For the most part, this change is only dumb replacements of mp_audio
related functions and fields. One minor actual change is that you can't
allocate the new type on the stack anymore.
Some code still uses mp_audio. All audio filter code will be deleted, so
it makes no sense to convert this code. (Audio filters which are LGPL
and which we keep will have to be ported to a new filter infrastructure
anyway.) player/audio.c uses it because it interacts with the old filter
code. push.c has some complex use of mp_audio and mp_audio_buffer, but
this and pull.c will most likely be rewritten to do something else.
2017-08-16 19:00:20 +00:00
|
|
|
void ao_get_format(struct ao *ao,
|
|
|
|
int *samplerate, int *format, struct mp_chmap *channels)
|
2014-03-07 14:24:32 +00:00
|
|
|
{
|
audio: introduce a new type to hold audio frames
This is pretty pointless, but I believe it allows us to claim that the
new code is not affected by the copyright of the old code. This is
needed, because the original mp_audio struct was written by someone who
has disagreed with LGPL relicensing (it was called af_data at the time,
and was defined in af.h).
The "GPL'ed" struct contents that surive are pretty trivial: just the
data pointer, and some metadata like the format, samplerate, etc. - but
at least in this case, any new code would be extremely similar anyway,
and I'm not really sure whether it's OK to claim different copyright. So
what we do is we just use AVFrame (which of course is LGPL with 100%
certainty), and add some accessors around it to adapt it to mpv
conventions.
Also, this gets rid of some annoying conventions of mp_audio, like the
struct fields that require using an accessor to write to them anyway.
For the most part, this change is only dumb replacements of mp_audio
related functions and fields. One minor actual change is that you can't
allocate the new type on the stack anymore.
Some code still uses mp_audio. All audio filter code will be deleted, so
it makes no sense to convert this code. (Audio filters which are LGPL
and which we keep will have to be ported to a new filter infrastructure
anyway.) player/audio.c uses it because it interacts with the old filter
code. push.c has some complex use of mp_audio and mp_audio_buffer, but
this and pull.c will most likely be rewritten to do something else.
2017-08-16 19:00:20 +00:00
|
|
|
*samplerate = ao->samplerate;
|
|
|
|
*format = ao->format;
|
|
|
|
*channels = ao->channels;
|
2014-03-07 14:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 19:21:31 +00:00
|
|
|
|
2015-02-12 15:53:56 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
struct ao_hotplug {
|
|
|
|
struct mpv_global *global;
|
2016-09-16 12:23:54 +00:00
|
|
|
void (*wakeup_cb)(void *ctx);
|
|
|
|
void *wakeup_ctx;
|
2015-02-12 15:53:56 +00:00
|
|
|
// 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,
|
2016-09-16 12:23:54 +00:00
|
|
|
void (*wakeup_cb)(void *ctx),
|
|
|
|
void *wakeup_ctx)
|
2015-02-12 15:53:56 +00:00
|
|
|
{
|
|
|
|
struct ao_hotplug *hp = talloc_ptrtype(NULL, hp);
|
|
|
|
*hp = (struct ao_hotplug){
|
|
|
|
.global = global,
|
2016-09-16 12:23:54 +00:00
|
|
|
.wakeup_cb = wakeup_cb,
|
|
|
|
.wakeup_ctx = wakeup_ctx,
|
2015-02-12 15:53:56 +00:00
|
|
|
.needs_update = true,
|
|
|
|
};
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_devices(struct ao *ao, struct ao_device_list *list)
|
|
|
|
{
|
2016-06-29 15:38:57 +00:00
|
|
|
if (ao->driver->list_devs) {
|
2015-02-12 15:53:56 +00:00
|
|
|
ao->driver->list_devs(ao, list);
|
2016-06-29 15:38:57 +00:00
|
|
|
} else {
|
2016-11-14 12:39:47 +00:00
|
|
|
ao_device_list_add(list, ao, &(struct ao_device_desc){"", ""});
|
2015-11-27 13:42:10 +00:00
|
|
|
}
|
2015-02-12 15:53:56 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 19:21:31 +00:00
|
|
|
{
|
2015-02-12 15:53:56 +00: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 14:42:44 +00:00
|
|
|
MP_TARRAY_APPEND(list, list->devices, list->num_devices,
|
|
|
|
(struct ao_device_desc){"auto", "Autoselect device"});
|
2015-02-12 15:53:56 +00:00
|
|
|
|
2014-10-09 19:21:31 +00:00
|
|
|
for (int n = 0; audio_out_drivers[n]; n++) {
|
|
|
|
const struct ao_driver *d = audio_out_drivers[n];
|
2014-10-22 14:16:35 +00:00
|
|
|
if (d == &audio_out_null)
|
|
|
|
break; // don't add unsafe/special entries
|
2015-02-12 15:53:56 +00:00
|
|
|
|
2016-09-16 12:23:54 +00:00
|
|
|
struct ao *ao = ao_alloc(true, hp->global, hp->wakeup_cb, hp->wakeup_ctx,
|
2016-11-25 20:00:39 +00:00
|
|
|
(char *)d->name);
|
2014-10-10 16:27:21 +00:00
|
|
|
if (!ao)
|
|
|
|
continue;
|
2015-02-12 15:53:56 +00: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 19:21:31 +00:00
|
|
|
}
|
2015-02-12 15:53:56 +00:00
|
|
|
hp->needs_update = false;
|
2014-10-09 19:21:31 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:27:21 +00:00
|
|
|
void ao_device_list_add(struct ao_device_list *list, struct ao *ao,
|
2014-10-09 19:21:31 +00:00
|
|
|
struct ao_device_desc *e)
|
|
|
|
{
|
|
|
|
struct ao_device_desc c = *e;
|
2014-10-10 16:27:21 +00:00
|
|
|
const char *dname = ao->driver->name;
|
2016-11-14 12:39:47 +00:00
|
|
|
char buf[80];
|
|
|
|
if (!c.desc || !c.desc[0]) {
|
|
|
|
if (c.name && c.name[0]) {
|
|
|
|
c.desc = c.name;
|
|
|
|
} else if (list->num_devices) {
|
|
|
|
// Assume this is the default device.
|
|
|
|
snprintf(buf, sizeof(buf), "Default (%s)", dname);
|
|
|
|
c.desc = buf;
|
|
|
|
} else {
|
|
|
|
// First default device (and maybe the only one).
|
|
|
|
c.desc = "Default";
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 12:50:37 +00:00
|
|
|
c.name = (c.name && c.name[0]) ? talloc_asprintf(list, "%s/%s", dname, c.name)
|
|
|
|
: talloc_strdup(list, dname);
|
2014-10-09 19:21:31 +00:00
|
|
|
c.desc = talloc_strdup(list, c.desc);
|
|
|
|
MP_TARRAY_APPEND(list, list->devices, list->num_devices, c);
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:53:56 +00: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);
|
|
|
|
}
|
|
|
|
|
2016-09-16 12:23:54 +00:00
|
|
|
static void dummy_wakeup(void *ctx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:27:21 +00:00
|
|
|
void ao_print_devices(struct mpv_global *global, struct mp_log *log)
|
2014-10-09 19:21:31 +00:00
|
|
|
{
|
2016-09-16 12:23:54 +00:00
|
|
|
struct ao_hotplug *hp = ao_hotplug_create(global, dummy_wakeup, NULL);
|
2015-02-12 15:53:56 +00:00
|
|
|
struct ao_device_list *list = ao_hotplug_get_device_list(hp);
|
2014-10-09 19:21:31 +00: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 14:36:38 +00:00
|
|
|
mp_info(log, " '%s' (%s)\n", desc->name, desc->desc);
|
2014-10-09 19:21:31 +00:00
|
|
|
}
|
2015-02-12 15:53:56 +00:00
|
|
|
ao_hotplug_destroy(hp);
|
2014-10-09 19:21:31 +00:00
|
|
|
}
|
2017-07-07 15:35:09 +00:00
|
|
|
|
audio: add audio softvol processing to AO
This does what af_volume used to do. Since we couldn't relicense it,
just rewrite it. Since we don't have a new filter mechanism yet, and the
libavfilter is too inconvenient, do applying the volume gain in ao.c
directly. This is done before handling the audio data to the driver.
Since push.c runs a separate thread, and pull.c is called asynchronously
from the audio driver's thread, the volume value needs to be
synchronized. There's no existing central mutex, so do some shit with
atomics. Since there's no atomic_float type predefined (which is at
least needed when using the legacy wrapper), do some nonsense about
reinterpret casting the float value to an int for the purpose of atomic
access. Not sure if using memcpy() is undefined behavior, but for now I
don't care.
The advantage of not using a filter is lower complexity (no filter auto
insertion), and lower latency (gain processing is done after our
internal audio buffer of at least 200ms).
Disavdantages include inability to use native volume control _before_
other filters with custom filter chains, and the need to add new
processing for each new sample type.
Since this doesn't reuse any of the old GPL code, nor does indirectly
rely on it, volume and replaygain handling now works in LGPL mode.
How to process the gain is inspired by libavfilter's af_volume (LGPL).
In particular, we use exactly the same rounding, and we quantize
processing for integer sample types by 256 steps. Some of libavfilter's
copyright may or may not apply, but I think not, and it's the same
license anyway.
2017-11-29 20:30:10 +00:00
|
|
|
void ao_set_gain(struct ao *ao, float gain)
|
|
|
|
{
|
2017-11-30 00:14:33 +00:00
|
|
|
atomic_store(&ao->gain, gain);
|
audio: add audio softvol processing to AO
This does what af_volume used to do. Since we couldn't relicense it,
just rewrite it. Since we don't have a new filter mechanism yet, and the
libavfilter is too inconvenient, do applying the volume gain in ao.c
directly. This is done before handling the audio data to the driver.
Since push.c runs a separate thread, and pull.c is called asynchronously
from the audio driver's thread, the volume value needs to be
synchronized. There's no existing central mutex, so do some shit with
atomics. Since there's no atomic_float type predefined (which is at
least needed when using the legacy wrapper), do some nonsense about
reinterpret casting the float value to an int for the purpose of atomic
access. Not sure if using memcpy() is undefined behavior, but for now I
don't care.
The advantage of not using a filter is lower complexity (no filter auto
insertion), and lower latency (gain processing is done after our
internal audio buffer of at least 200ms).
Disavdantages include inability to use native volume control _before_
other filters with custom filter chains, and the need to add new
processing for each new sample type.
Since this doesn't reuse any of the old GPL code, nor does indirectly
rely on it, volume and replaygain handling now works in LGPL mode.
How to process the gain is inspired by libavfilter's af_volume (LGPL).
In particular, we use exactly the same rounding, and we quantize
processing for integer sample types by 256 steps. Some of libavfilter's
copyright may or may not apply, but I think not, and it's the same
license anyway.
2017-11-29 20:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MUL_GAIN_i(d, num_samples, gain, low, center, high) \
|
|
|
|
for (int n = 0; n < (num_samples); n++) \
|
|
|
|
(d)[n] = MPCLAMP( \
|
|
|
|
((((int64_t)((d)[n]) - (center)) * (gain) + 128) >> 8) + (center), \
|
|
|
|
(low), (high))
|
|
|
|
|
|
|
|
#define MUL_GAIN_f(d, num_samples, gain) \
|
|
|
|
for (int n = 0; n < (num_samples); n++) \
|
|
|
|
(d)[n] = MPCLAMP(((d)[n]) * (gain), -1.0, 1.0)
|
|
|
|
|
|
|
|
static void process_plane(struct ao *ao, void *data, int num_samples)
|
|
|
|
{
|
2017-11-30 00:14:33 +00:00
|
|
|
float gain = atomic_load_explicit(&ao->gain, memory_order_relaxed);
|
audio: add audio softvol processing to AO
This does what af_volume used to do. Since we couldn't relicense it,
just rewrite it. Since we don't have a new filter mechanism yet, and the
libavfilter is too inconvenient, do applying the volume gain in ao.c
directly. This is done before handling the audio data to the driver.
Since push.c runs a separate thread, and pull.c is called asynchronously
from the audio driver's thread, the volume value needs to be
synchronized. There's no existing central mutex, so do some shit with
atomics. Since there's no atomic_float type predefined (which is at
least needed when using the legacy wrapper), do some nonsense about
reinterpret casting the float value to an int for the purpose of atomic
access. Not sure if using memcpy() is undefined behavior, but for now I
don't care.
The advantage of not using a filter is lower complexity (no filter auto
insertion), and lower latency (gain processing is done after our
internal audio buffer of at least 200ms).
Disavdantages include inability to use native volume control _before_
other filters with custom filter chains, and the need to add new
processing for each new sample type.
Since this doesn't reuse any of the old GPL code, nor does indirectly
rely on it, volume and replaygain handling now works in LGPL mode.
How to process the gain is inspired by libavfilter's af_volume (LGPL).
In particular, we use exactly the same rounding, and we quantize
processing for integer sample types by 256 steps. Some of libavfilter's
copyright may or may not apply, but I think not, and it's the same
license anyway.
2017-11-29 20:30:10 +00:00
|
|
|
int gi = lrint(256.0 * gain);
|
2017-11-30 00:31:29 +00:00
|
|
|
if (gi == 256)
|
|
|
|
return;
|
|
|
|
switch (af_fmt_from_planar(ao->format)) {
|
audio: add audio softvol processing to AO
This does what af_volume used to do. Since we couldn't relicense it,
just rewrite it. Since we don't have a new filter mechanism yet, and the
libavfilter is too inconvenient, do applying the volume gain in ao.c
directly. This is done before handling the audio data to the driver.
Since push.c runs a separate thread, and pull.c is called asynchronously
from the audio driver's thread, the volume value needs to be
synchronized. There's no existing central mutex, so do some shit with
atomics. Since there's no atomic_float type predefined (which is at
least needed when using the legacy wrapper), do some nonsense about
reinterpret casting the float value to an int for the purpose of atomic
access. Not sure if using memcpy() is undefined behavior, but for now I
don't care.
The advantage of not using a filter is lower complexity (no filter auto
insertion), and lower latency (gain processing is done after our
internal audio buffer of at least 200ms).
Disavdantages include inability to use native volume control _before_
other filters with custom filter chains, and the need to add new
processing for each new sample type.
Since this doesn't reuse any of the old GPL code, nor does indirectly
rely on it, volume and replaygain handling now works in LGPL mode.
How to process the gain is inspired by libavfilter's af_volume (LGPL).
In particular, we use exactly the same rounding, and we quantize
processing for integer sample types by 256 steps. Some of libavfilter's
copyright may or may not apply, but I think not, and it's the same
license anyway.
2017-11-29 20:30:10 +00:00
|
|
|
case AF_FORMAT_U8:
|
|
|
|
MUL_GAIN_i((uint8_t *)data, num_samples, gi, 0, 128, 255);
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_S16:
|
|
|
|
MUL_GAIN_i((int16_t *)data, num_samples, gi, INT16_MIN, 0, INT16_MAX);
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_S32:
|
|
|
|
MUL_GAIN_i((int32_t *)data, num_samples, gi, INT32_MIN, 0, INT32_MAX);
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_FLOAT:
|
|
|
|
MUL_GAIN_f((float *)data, num_samples, gain);
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_DOUBLE:
|
|
|
|
MUL_GAIN_f((double *)data, num_samples, gain);
|
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
// all other sample formats are simply not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ao_post_process_data(struct ao *ao, void **data, int num_samples)
|
|
|
|
{
|
|
|
|
bool planar = af_fmt_is_planar(ao->format);
|
|
|
|
int planes = planar ? ao->channels.num : 1;
|
|
|
|
int plane_samples = num_samples * (planar ? 1: ao->channels.num);
|
|
|
|
for (int n = 0; n < planes; n++)
|
|
|
|
process_plane(ao, data[n], plane_samples);
|
|
|
|
}
|
|
|
|
|
2017-07-07 15:35:09 +00:00
|
|
|
static int get_conv_type(struct ao_convert_fmt *fmt)
|
|
|
|
{
|
|
|
|
if (af_fmt_to_bytes(fmt->src_fmt) * 8 == fmt->dst_bits && !fmt->pad_msb)
|
|
|
|
return 0; // passthrough
|
|
|
|
if (fmt->src_fmt == AF_FORMAT_S32 && fmt->dst_bits == 24 && !fmt->pad_msb)
|
|
|
|
return 1; // simple 32->24 bit conversion
|
|
|
|
if (fmt->src_fmt == AF_FORMAT_S32 && fmt->dst_bits == 32 && fmt->pad_msb == 8)
|
|
|
|
return 2; // simple 32->24 bit conversion, with MSB padding
|
|
|
|
return -1; // unsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether ao_convert_inplace() can be called. As an exception, the
|
|
|
|
// planar-ness of the sample format and the number of channels is ignored.
|
|
|
|
// All other parameters must be as passed to ao_convert_inplace().
|
|
|
|
bool ao_can_convert_inplace(struct ao_convert_fmt *fmt)
|
|
|
|
{
|
|
|
|
return get_conv_type(fmt) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ao_need_conversion(struct ao_convert_fmt *fmt)
|
|
|
|
{
|
|
|
|
return get_conv_type(fmt) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The LSB is always ignored.
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
#define SHIFT24(x) ((3-(x))*8)
|
|
|
|
#else
|
|
|
|
#define SHIFT24(x) (((x)+1)*8)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void convert_plane(int type, void *data, int num_samples)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1: /* fall through */
|
|
|
|
case 2: {
|
|
|
|
int bytes = type == 1 ? 3 : 4;
|
|
|
|
for (int s = 0; s < num_samples; s++) {
|
|
|
|
uint32_t val = *((uint32_t *)data + s);
|
|
|
|
uint8_t *ptr = (uint8_t *)data + s * bytes;
|
|
|
|
ptr[0] = val >> SHIFT24(0);
|
|
|
|
ptr[1] = val >> SHIFT24(1);
|
|
|
|
ptr[2] = val >> SHIFT24(2);
|
|
|
|
if (type == 2)
|
|
|
|
ptr[3] = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// data[n] contains the pointer to the first sample of the n-th plane, in the
|
|
|
|
// format implied by fmt->src_fmt. src_fmt also controls whether the data is
|
2017-07-09 07:56:48 +00:00
|
|
|
// all in one plane, or if there is a plane per channel.
|
2017-07-07 15:35:09 +00:00
|
|
|
void ao_convert_inplace(struct ao_convert_fmt *fmt, void **data, int num_samples)
|
|
|
|
{
|
|
|
|
int type = get_conv_type(fmt);
|
|
|
|
bool planar = af_fmt_is_planar(fmt->src_fmt);
|
|
|
|
int planes = planar ? fmt->channels : 1;
|
|
|
|
int plane_samples = num_samples * (planar ? 1: fmt->channels);
|
|
|
|
for (int n = 0; n < planes; n++)
|
|
|
|
convert_plane(type, data[n], plane_samples);
|
|
|
|
}
|