2012-12-28 07:07:14 +00:00
|
|
|
/*
|
2012-12-28 10:41:30 +00:00
|
|
|
* audio output driver for SDL 1.2+
|
2012-12-28 07:07:14 +00:00
|
|
|
* Copyright (C) 2012 Rudolf Polzer <divVerent@xonotic.org>
|
|
|
|
*
|
2012-12-28 10:41:30 +00:00
|
|
|
* This file is part of mpv.
|
2012-12-28 07:07:14 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +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.
|
2012-12-28 07:07:14 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2012-12-28 07:07:14 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-12-28 07:07:14 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +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/>.
|
2012-12-28 07:07:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "audio/format.h"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2012-12-28 07:07:14 +00:00
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2014-03-09 18:08:47 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2012-12-28 07:07:14 +00:00
|
|
|
#include "osdep/timer.h"
|
|
|
|
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
struct priv
|
|
|
|
{
|
|
|
|
bool paused;
|
2014-03-09 18:08:47 +00:00
|
|
|
|
2013-07-21 20:28:40 +00:00
|
|
|
float buflen;
|
2012-12-28 07:07:14 +00:00
|
|
|
};
|
|
|
|
|
2014-10-10 11:44:08 +00:00
|
|
|
static const int fmtmap[][2] = {
|
2014-09-23 19:04:37 +00:00
|
|
|
{AF_FORMAT_U8, AUDIO_U8},
|
|
|
|
{AF_FORMAT_S16, AUDIO_S16SYS},
|
|
|
|
#ifdef AUDIO_S32SYS
|
|
|
|
{AF_FORMAT_S32, AUDIO_S32SYS},
|
|
|
|
#endif
|
|
|
|
#ifdef AUDIO_F32SYS
|
|
|
|
{AF_FORMAT_FLOAT, AUDIO_F32SYS},
|
|
|
|
#endif
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2012-12-28 07:07:14 +00:00
|
|
|
static void audio_callback(void *userdata, Uint8 *stream, int len)
|
|
|
|
{
|
|
|
|
struct ao *ao = userdata;
|
|
|
|
|
2014-03-09 18:08:47 +00:00
|
|
|
void *data[1] = {stream};
|
2012-12-28 07:07:14 +00:00
|
|
|
|
2014-03-09 18:08:47 +00:00
|
|
|
if (len % ao->sstride)
|
|
|
|
MP_ERR(ao, "SDL audio callback not sample aligned");
|
2012-12-28 07:07:14 +00:00
|
|
|
|
2014-03-09 18:08:47 +00:00
|
|
|
// Time this buffer will take, plus assume 1 period (1 callback invocation)
|
|
|
|
// fixed latency.
|
|
|
|
double delay = 2 * len / (double)ao->bps;
|
2012-12-28 07:07:14 +00:00
|
|
|
|
2023-10-11 18:45:40 +00:00
|
|
|
ao_read_data(ao, data, len / ao->sstride, mp_time_ns() + MP_TIME_S_TO_NS(delay));
|
2012-12-28 07:07:14 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2012-12-28 07:07:14 +00:00
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
|
|
|
if (!priv)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (SDL_WasInit(SDL_INIT_AUDIO)) {
|
|
|
|
// make sure the callback exits
|
|
|
|
SDL_LockAudio();
|
|
|
|
|
|
|
|
// close audio device
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int ceil_power_of_two(unsigned int x)
|
|
|
|
{
|
|
|
|
int y = 1;
|
|
|
|
while (y < x)
|
|
|
|
y *= 2;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2012-12-28 07:07:14 +00:00
|
|
|
{
|
|
|
|
if (SDL_WasInit(SDL_INIT_AUDIO)) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "already initialized\n");
|
2012-12-28 07:07:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-21 20:28:40 +00:00
|
|
|
struct priv *priv = ao->priv;
|
2012-12-28 07:07:14 +00:00
|
|
|
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO)) {
|
|
|
|
if (!ao->probing)
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "SDL_Init failed\n");
|
2014-03-08 23:49:39 +00:00
|
|
|
uninit(ao);
|
2012-12-28 07:07:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
struct mp_chmap_sel sel = {0};
|
|
|
|
mp_chmap_sel_add_waveext_def(&sel);
|
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels)) {
|
2014-03-08 23:49:39 +00:00
|
|
|
uninit(ao);
|
2013-05-09 16:06:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-10 22:15:02 +00:00
|
|
|
ao->format = af_fmt_from_planar(ao->format);
|
|
|
|
|
2018-03-07 22:01:52 +00:00
|
|
|
SDL_AudioSpec desired = {0};
|
2014-09-23 19:04:37 +00:00
|
|
|
desired.format = AUDIO_S16SYS;
|
|
|
|
for (int n = 0; fmtmap[n][0]; n++) {
|
|
|
|
if (ao->format == fmtmap[n][0]) {
|
|
|
|
desired.format = fmtmap[n][1];
|
|
|
|
break;
|
|
|
|
}
|
2012-12-28 07:07:14 +00:00
|
|
|
}
|
|
|
|
desired.freq = ao->samplerate;
|
2013-04-05 21:06:22 +00:00
|
|
|
desired.channels = ao->channels.num;
|
2018-03-07 22:01:52 +00:00
|
|
|
if (priv->buflen) {
|
|
|
|
desired.samples = MPMIN(32768, ceil_power_of_two(ao->samplerate *
|
|
|
|
priv->buflen));
|
|
|
|
}
|
2012-12-28 07:07:14 +00:00
|
|
|
desired.callback = audio_callback;
|
|
|
|
desired.userdata = ao;
|
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "requested format: %d Hz, %d channels, %x, "
|
|
|
|
"buffer size: %d samples\n",
|
|
|
|
(int) desired.freq, (int) desired.channels,
|
|
|
|
(int) desired.format, (int) desired.samples);
|
2012-12-28 07:07:14 +00:00
|
|
|
|
2018-03-07 22:01:52 +00:00
|
|
|
SDL_AudioSpec obtained = desired;
|
2012-12-28 07:07:14 +00:00
|
|
|
if (SDL_OpenAudio(&desired, &obtained)) {
|
|
|
|
if (!ao->probing)
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "could not open audio: %s\n", SDL_GetError());
|
2014-03-08 23:49:39 +00:00
|
|
|
uninit(ao);
|
2012-12-28 07:07:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_VERBOSE(ao, "obtained format: %d Hz, %d channels, %x, "
|
|
|
|
"buffer size: %d samples\n",
|
|
|
|
(int) obtained.freq, (int) obtained.channels,
|
|
|
|
(int) obtained.format, (int) obtained.samples);
|
2012-12-28 07:07:14 +00:00
|
|
|
|
2014-03-10 20:30:40 +00:00
|
|
|
// The sample count is usually the number of samples the callback requests,
|
|
|
|
// which we assume is the period size. Normally, ao.c will allocate a large
|
|
|
|
// enough buffer. But in case the period size should be pathologically
|
|
|
|
// large, this will help.
|
|
|
|
ao->device_buffer = 3 * obtained.samples;
|
|
|
|
|
2014-09-23 19:04:37 +00:00
|
|
|
ao->format = 0;
|
|
|
|
for (int n = 0; fmtmap[n][0]; n++) {
|
|
|
|
if (obtained.format == fmtmap[n][1]) {
|
|
|
|
ao->format = fmtmap[n][0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ao->format) {
|
|
|
|
if (!ao->probing)
|
|
|
|
MP_ERR(ao, "could not find matching format\n");
|
|
|
|
uninit(ao);
|
|
|
|
return -1;
|
2012-12-28 07:07:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
if (!ao_chmap_sel_get_def(ao, &sel, &ao->channels, obtained.channels)) {
|
2014-03-08 23:49:39 +00:00
|
|
|
uninit(ao);
|
2013-05-09 16:06:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-28 07:07:14 +00:00
|
|
|
ao->samplerate = obtained.freq;
|
|
|
|
|
|
|
|
priv->paused = 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
static void reset(struct ao *ao)
|
2012-12-28 07:07:14 +00:00
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2014-03-09 18:08:47 +00:00
|
|
|
if (!priv->paused)
|
|
|
|
SDL_PauseAudio(SDL_TRUE);
|
2012-12-28 07:07:14 +00:00
|
|
|
priv->paused = 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void start(struct ao *ao)
|
2012-12-28 07:07:14 +00:00
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2014-03-09 18:08:47 +00:00
|
|
|
if (priv->paused)
|
|
|
|
SDL_PauseAudio(SDL_FALSE);
|
|
|
|
priv->paused = 0;
|
2012-12-28 07:07:14 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 20:28:40 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2012-12-28 07:07:14 +00:00
|
|
|
const struct ao_driver audio_out_sdl = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "SDL Audio",
|
|
|
|
.name = "sdl",
|
2012-12-28 07:07:14 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
.reset = reset,
|
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
|
|
|
.start = start,
|
2013-07-21 20:28:40 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
|
|
|
.priv_defaults = &(const struct priv) {
|
|
|
|
.buflen = 0, // use SDL default
|
|
|
|
},
|
|
|
|
.options = (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
|
|
|
{"buflen", OPT_FLOAT(buflen)},
|
2013-07-21 20:28:40 +00:00
|
|
|
{0}
|
|
|
|
},
|
2016-11-25 20:00:39 +00:00
|
|
|
.options_prefix = "sdl",
|
2012-12-28 07:07:14 +00:00
|
|
|
};
|