2014-03-08 23:04:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
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.
|
2014-03-08 23:04:37 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
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.
|
2014-03-08 23:04:37 +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/>.
|
2014-03-08 23:04:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "ao.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "audio/format.h"
|
|
|
|
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "common/common.h"
|
|
|
|
|
2014-04-15 20:50:16 +00:00
|
|
|
#include "input/input.h"
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
#include "osdep/timer.h"
|
|
|
|
#include "osdep/threads.h"
|
2016-09-07 09:26:25 +00:00
|
|
|
#include "osdep/atomic.h"
|
2014-03-08 23:04:37 +00:00
|
|
|
#include "misc/ring.h"
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
* Note: there is some stupid stuff in this file in order to avoid mutexes.
|
|
|
|
* This requirement is dictated by several audio APIs, at least jackaudio.
|
|
|
|
*/
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
enum {
|
|
|
|
AO_STATE_NONE, // idle (e.g. before playback started, or after playback
|
|
|
|
// finished, but device is open)
|
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
|
|
|
AO_STATE_WAIT, // wait for callback to go into AO_STATE_NONE state
|
2014-03-08 23:04:37 +00:00
|
|
|
AO_STATE_PLAY, // play the buffer
|
2014-11-09 14:22:00 +00:00
|
|
|
AO_STATE_BUSY, // like AO_STATE_PLAY, but ao_read_data() is being called
|
2014-03-08 23:04:37 +00:00
|
|
|
};
|
|
|
|
|
2014-11-09 14:22:00 +00:00
|
|
|
#define IS_PLAYING(st) ((st) == AO_STATE_PLAY || (st) == AO_STATE_BUSY)
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
struct ao_pull_state {
|
|
|
|
// Be very careful with the order when accessing planes.
|
|
|
|
struct mp_ring *buffers[MP_NUM_CHANNELS];
|
|
|
|
|
|
|
|
// AO_STATE_*
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_int state;
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2017-06-28 10:26:27 +00:00
|
|
|
// Set when the buffer is intentionally not fed anymore in PLAY state.
|
|
|
|
atomic_bool draining;
|
|
|
|
|
|
|
|
// Set by the audio thread when an underflow was detected.
|
|
|
|
// It adds the number of samples.
|
|
|
|
atomic_int underflow;
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
// Device delay of the last written sample, in realtime.
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_llong end_time_us;
|
2017-07-07 15:35:09 +00:00
|
|
|
|
|
|
|
char *convert_buffer;
|
2014-03-08 23:04:37 +00:00
|
|
|
};
|
|
|
|
|
2014-11-09 14:22:00 +00:00
|
|
|
static void set_state(struct ao *ao, int new_state)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
while (1) {
|
|
|
|
int old = atomic_load(&p->state);
|
|
|
|
if (old == AO_STATE_BUSY) {
|
|
|
|
// A spinlock, because some audio APIs don't want us to use mutexes.
|
|
|
|
mp_sleep_us(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (atomic_compare_exchange_strong(&p->state, &old, new_state))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
static int get_space(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
player: consider audio buffer if AO driver does not report underruns
AOs can report audio underruns, but only ao_alsa and ao_sdl (???)
currently do so. If the AO was marked as not reporting it, the cache
state was used to determine whether playback was interrupted due to slow
input.
This caused problems in some cases, such as video with very low video
frame rate: when a new frame is displayed, a new frame has to be
decoded, and since there it's so much further into the file (long frame
durations), the cache gets into an underrun state for a short moment,
even though both audio and video are playing fine. Enlarging the audio
buffer didn't help.
Fix this by making all AOs report underruns. If the AO driver does not
report underruns, fall back to using the buffer state.
pull.c behavior is slightly changed. Pull AOs are normally intended to
be used by pseudo-realtime audio APIs that fetch an audio buffer from
the API user via callback. I think it makes no sense to consider a
buffer underflow not an underrun in any situation, since we return
silence to the reader. (OK, maybe the reader could check the return
value? But let's not go there as long as there's no implementation.)
Remove the flag from ao_sdl.c, since it just worked via the generic
mechanism. Make the redundant underrun message verbose only.
push.c seems to log a redundant underflow message when resuming (because
somehow ao_play_data() is called when there's still no new data in the
buffer). But since ao_alsa does its own underrun reporting, and I only
use ao_alsa, I don't really care.
Also in all my tests, there seemed to be a rather high delay until the
underflow was logged (with audio only). I have no idea why this happened
and didn't try to debug this, but there's probably something wrong
somewhere.
This commit may cause random regressions.
See: #7440
2020-02-13 00:28:59 +00:00
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
// Since the reader will read the last plane last, its free space is the
|
|
|
|
// minimum free space across all planes.
|
|
|
|
return mp_ring_available(p->buffers[ao->num_planes - 1]) / ao->sstride;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int play(struct ao *ao, void **data, int samples, int flags)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
|
|
|
|
int write_samples = get_space(ao);
|
|
|
|
write_samples = MPMIN(write_samples, samples);
|
|
|
|
|
|
|
|
// Write starting from the last plane - this way, the first plane will
|
|
|
|
// always contain the minimum amount of data readable across all planes
|
|
|
|
// (assumes the reader starts with the first plane).
|
|
|
|
int write_bytes = write_samples * ao->sstride;
|
|
|
|
for (int n = ao->num_planes - 1; n >= 0; n--) {
|
|
|
|
int r = mp_ring_write(p->buffers[n], data[n], write_bytes);
|
|
|
|
assert(r == write_bytes);
|
|
|
|
}
|
2014-11-09 14:22:00 +00:00
|
|
|
|
|
|
|
int state = atomic_load(&p->state);
|
|
|
|
if (!IS_PLAYING(state)) {
|
2017-06-28 10:26:27 +00:00
|
|
|
atomic_store(&p->draining, false);
|
|
|
|
atomic_store(&p->underflow, 0);
|
2014-11-09 14:22:00 +00:00
|
|
|
set_state(ao, AO_STATE_PLAY);
|
2016-11-21 18:33:31 +00:00
|
|
|
if (!ao->stream_silence)
|
|
|
|
ao->driver->resume(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:26:27 +00:00
|
|
|
bool draining = write_samples == samples && (flags & AOPLAY_FINAL_CHUNK);
|
|
|
|
atomic_store(&p->draining, draining);
|
|
|
|
|
2020-02-13 12:24:11 +00:00
|
|
|
int underflow = atomic_fetch_and(&p->underflow, 0);
|
|
|
|
if (underflow)
|
|
|
|
MP_DBG(ao, "Audio underrun by %d samples.\n", underflow);
|
2017-06-28 10:26:27 +00:00
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
return write_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the given amount of samples in the user-provided data buffer. Returns
|
|
|
|
// the number of samples copied. If there is not enough data (buffer underrun
|
|
|
|
// or EOF), return the number of samples that could be copied, and fill the
|
|
|
|
// rest of the user-provided buffer with silence.
|
|
|
|
// This basically assumes that the audio device doesn't care about underruns.
|
|
|
|
// If this is called in paused mode, it will always return 0.
|
2015-12-21 21:19:44 +00:00
|
|
|
// The caller should set out_time_us to the expected delay until the last sample
|
2014-03-08 23:04:37 +00:00
|
|
|
// reaches the speakers, in microseconds, using mp_time_us() as reference.
|
|
|
|
int ao_read_data(struct ao *ao, void **data, int samples, int64_t out_time_us)
|
|
|
|
{
|
|
|
|
assert(ao->api == &ao_api_pull);
|
|
|
|
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
int full_bytes = samples * ao->sstride;
|
2014-11-09 14:22:00 +00:00
|
|
|
bool need_wakeup = false;
|
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
|
|
|
int bytes = 0;
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2014-11-09 14:22:00 +00:00
|
|
|
// Play silence in states other than AO_STATE_PLAY.
|
|
|
|
if (!atomic_compare_exchange_strong(&p->state, &(int){AO_STATE_PLAY},
|
|
|
|
AO_STATE_BUSY))
|
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
|
|
|
goto end;
|
2014-03-08 23:04:37 +00:00
|
|
|
|
|
|
|
// Since the writer will write the first plane last, its buffered amount
|
|
|
|
// of data is the minimum amount across all planes.
|
2014-04-15 20:50:16 +00:00
|
|
|
int buffered_bytes = mp_ring_buffered(p->buffers[0]);
|
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
|
|
|
bytes = MPMIN(buffered_bytes, full_bytes);
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2020-02-13 12:24:11 +00:00
|
|
|
if (full_bytes > bytes && !atomic_load(&p->draining)) {
|
player: consider audio buffer if AO driver does not report underruns
AOs can report audio underruns, but only ao_alsa and ao_sdl (???)
currently do so. If the AO was marked as not reporting it, the cache
state was used to determine whether playback was interrupted due to slow
input.
This caused problems in some cases, such as video with very low video
frame rate: when a new frame is displayed, a new frame has to be
decoded, and since there it's so much further into the file (long frame
durations), the cache gets into an underrun state for a short moment,
even though both audio and video are playing fine. Enlarging the audio
buffer didn't help.
Fix this by making all AOs report underruns. If the AO driver does not
report underruns, fall back to using the buffer state.
pull.c behavior is slightly changed. Pull AOs are normally intended to
be used by pseudo-realtime audio APIs that fetch an audio buffer from
the API user via callback. I think it makes no sense to consider a
buffer underflow not an underrun in any situation, since we return
silence to the reader. (OK, maybe the reader could check the return
value? But let's not go there as long as there's no implementation.)
Remove the flag from ao_sdl.c, since it just worked via the generic
mechanism. Make the redundant underrun message verbose only.
push.c seems to log a redundant underflow message when resuming (because
somehow ao_play_data() is called when there's still no new data in the
buffer). But since ao_alsa does its own underrun reporting, and I only
use ao_alsa, I don't really care.
Also in all my tests, there seemed to be a rather high delay until the
underflow was logged (with audio only). I have no idea why this happened
and didn't try to debug this, but there's probably something wrong
somewhere.
This commit may cause random regressions.
See: #7440
2020-02-13 00:28:59 +00:00
|
|
|
atomic_fetch_add(&p->underflow, (full_bytes - bytes) / ao->sstride);
|
2020-02-13 12:24:11 +00:00
|
|
|
ao_underrun_event(ao);
|
|
|
|
}
|
2017-06-28 10:26:27 +00:00
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
if (bytes > 0)
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_store(&p->end_time_us, out_time_us);
|
2014-03-08 23:04:37 +00:00
|
|
|
|
|
|
|
for (int n = 0; n < ao->num_planes; n++) {
|
|
|
|
int r = mp_ring_read(p->buffers[n], data[n], bytes);
|
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
|
|
|
bytes = MPMIN(bytes, r);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
2014-04-15 20:50:16 +00:00
|
|
|
|
|
|
|
// Half of the buffer played -> request more.
|
2014-11-09 14:22:00 +00:00
|
|
|
need_wakeup = buffered_bytes - bytes <= mp_ring_size(p->buffers[0]) / 2;
|
|
|
|
|
|
|
|
// Should never fail.
|
|
|
|
atomic_compare_exchange_strong(&p->state, &(int){AO_STATE_BUSY}, AO_STATE_PLAY);
|
2014-04-15 20:50:16 +00:00
|
|
|
|
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
|
|
|
end:
|
2014-11-09 14:22:00 +00:00
|
|
|
|
|
|
|
if (need_wakeup)
|
2016-09-16 12:23:54 +00:00
|
|
|
ao->wakeup_cb(ao->wakeup_ctx);
|
2014-11-09 14:22:00 +00:00
|
|
|
|
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
|
|
|
// pad with silence (underflow/paused/eof)
|
|
|
|
for (int n = 0; n < ao->num_planes; n++)
|
2015-06-09 16:18:41 +00:00
|
|
|
af_fill_silence((char *)data[n] + bytes, full_bytes - bytes, ao->format);
|
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
|
|
|
|
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_post_process_data(ao, data, samples);
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
return bytes / ao->sstride;
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:52:32 +00:00
|
|
|
// Same as ao_read_data(), but convert data according to *fmt.
|
2017-07-07 15:35:09 +00:00
|
|
|
// fmt->src_fmt and fmt->channels must be the same as the AO parameters.
|
|
|
|
int ao_read_data_converted(struct ao *ao, struct ao_convert_fmt *fmt,
|
|
|
|
void **data, int samples, int64_t out_time_us)
|
|
|
|
{
|
|
|
|
assert(ao->api == &ao_api_pull);
|
|
|
|
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
2017-10-27 12:11:33 +00:00
|
|
|
void *ndata[MP_NUM_CHANNELS] = {0};
|
2017-07-07 15:35:09 +00:00
|
|
|
|
|
|
|
if (!ao_need_conversion(fmt))
|
|
|
|
return ao_read_data(ao, data, samples, out_time_us);
|
|
|
|
|
|
|
|
assert(ao->format == fmt->src_fmt);
|
|
|
|
assert(ao->channels.num == fmt->channels);
|
|
|
|
|
|
|
|
bool planar = af_fmt_is_planar(fmt->src_fmt);
|
|
|
|
int planes = planar ? fmt->channels : 1;
|
2017-07-09 07:52:32 +00:00
|
|
|
int plane_samples = samples * (planar ? 1: fmt->channels);
|
|
|
|
int src_plane_size = plane_samples * af_fmt_to_bytes(fmt->src_fmt);
|
|
|
|
int dst_plane_size = plane_samples * fmt->dst_bits / 8;
|
2017-07-07 15:35:09 +00:00
|
|
|
|
2017-07-09 07:52:32 +00:00
|
|
|
int needed = src_plane_size * planes;
|
2017-07-07 15:35:09 +00:00
|
|
|
if (needed > talloc_get_size(p->convert_buffer) || !p->convert_buffer) {
|
|
|
|
talloc_free(p->convert_buffer);
|
|
|
|
p->convert_buffer = talloc_size(NULL, needed);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int n = 0; n < planes; n++)
|
2017-07-09 07:52:32 +00:00
|
|
|
ndata[n] = p->convert_buffer + n * src_plane_size;
|
2017-07-07 15:35:09 +00:00
|
|
|
|
|
|
|
int res = ao_read_data(ao, ndata, samples, out_time_us);
|
|
|
|
|
|
|
|
ao_convert_inplace(fmt, ndata, samples);
|
|
|
|
for (int n = 0; n < planes; n++)
|
2017-07-09 07:52:32 +00:00
|
|
|
memcpy(data[n], ndata[n], dst_plane_size);
|
2017-07-07 15:35:09 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
|
|
|
{
|
|
|
|
if (ao->driver->control)
|
|
|
|
return ao->driver->control(ao, cmd, arg);
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2014-11-09 10:45:04 +00:00
|
|
|
static double get_delay(struct ao *ao)
|
2014-03-08 23:04:37 +00:00
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
|
2014-05-20 23:04:47 +00:00
|
|
|
int64_t end = atomic_load(&p->end_time_us);
|
2014-03-08 23:04:37 +00:00
|
|
|
int64_t now = mp_time_us();
|
|
|
|
double driver_delay = MPMAX(0, (end - now) / (1000.0 * 1000.0));
|
|
|
|
return mp_ring_buffered(p->buffers[0]) / (double)ao->bps + driver_delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
2016-08-09 14:22:06 +00:00
|
|
|
if (!ao->stream_silence && ao->driver->reset)
|
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
|
|
|
ao->driver->reset(ao); // assumes the audio callback thread is stopped
|
2014-11-09 14:22:00 +00:00
|
|
|
set_state(ao, AO_STATE_NONE);
|
2014-03-08 23:04:37 +00:00
|
|
|
for (int n = 0; n < ao->num_planes; n++)
|
|
|
|
mp_ring_reset(p->buffers[n]);
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_store(&p->end_time_us, 0);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pause(struct ao *ao)
|
|
|
|
{
|
2016-08-09 14:22:06 +00:00
|
|
|
if (!ao->stream_silence && ao->driver->reset)
|
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
|
|
|
ao->driver->reset(ao);
|
2014-11-09 14:22:00 +00:00
|
|
|
set_state(ao, AO_STATE_NONE);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void resume(struct ao *ao)
|
|
|
|
{
|
2014-11-09 14:22:00 +00:00
|
|
|
set_state(ao, AO_STATE_PLAY);
|
2016-11-21 18:33:31 +00:00
|
|
|
if (!ao->stream_silence)
|
|
|
|
ao->driver->resume(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 20:21:06 +00:00
|
|
|
static bool get_eof(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
// For simplicity, ignore the latency. Otherwise, we would have to run an
|
|
|
|
// extra thread to time it.
|
|
|
|
return mp_ring_buffered(p->buffers[0]) == 0;
|
|
|
|
}
|
|
|
|
|
2015-06-09 16:21:07 +00:00
|
|
|
static void drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
int state = atomic_load(&p->state);
|
|
|
|
if (IS_PLAYING(state)) {
|
2017-06-28 10:26:27 +00:00
|
|
|
atomic_store(&p->draining, true);
|
2015-06-09 16:21:07 +00:00
|
|
|
// Wait for lower bound.
|
|
|
|
mp_sleep_us(mp_ring_buffered(p->buffers[0]) / (double)ao->bps * 1e6);
|
|
|
|
// And then poll for actual end. (Unfortunately, this code considers
|
|
|
|
// audio APIs which do not want you to use mutexes in the audio
|
|
|
|
// callback, and an extra semaphore would require slightly more effort.)
|
|
|
|
// Limit to arbitrary ~250ms max. waiting for robustness.
|
|
|
|
int64_t max = mp_time_us() + 250000;
|
|
|
|
while (mp_time_us() < max && !get_eof(ao))
|
|
|
|
mp_sleep_us(1);
|
|
|
|
}
|
|
|
|
reset(ao);
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2014-03-08 23:04:37 +00:00
|
|
|
{
|
2017-07-07 15:35:09 +00:00
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
ao->driver->uninit(ao);
|
2017-07-07 15:35:09 +00:00
|
|
|
|
|
|
|
talloc_free(p->convert_buffer);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int init(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
for (int n = 0; n < ao->num_planes; n++)
|
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
|
|
|
p->buffers[n] = mp_ring_new(ao, ao->buffer * ao->sstride);
|
|
|
|
atomic_store(&p->state, AO_STATE_NONE);
|
|
|
|
assert(ao->driver->resume);
|
2016-08-09 14:22:06 +00:00
|
|
|
|
|
|
|
if (ao->stream_silence)
|
|
|
|
ao->driver->resume(ao);
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ao_driver ao_api_pull = {
|
|
|
|
.init = init,
|
|
|
|
.control = control,
|
|
|
|
.uninit = uninit,
|
2014-07-13 18:06:33 +00:00
|
|
|
.drain = drain,
|
2014-03-08 23:04:37 +00:00
|
|
|
.reset = reset,
|
|
|
|
.get_space = get_space,
|
|
|
|
.play = play,
|
|
|
|
.get_delay = get_delay,
|
2014-09-05 20:21:06 +00:00
|
|
|
.get_eof = get_eof,
|
2014-03-08 23:04:37 +00:00
|
|
|
.pause = pause,
|
|
|
|
.resume = resume,
|
|
|
|
.priv_size = sizeof(struct ao_pull_state),
|
|
|
|
};
|