2014-03-08 23:04:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "compat/atomics.h"
|
|
|
|
#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
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Device delay of the last written sample, in realtime.
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_llong end_time_us;
|
2014-03-08 23:04:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int get_space(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
// 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-05-20 23:04:47 +00:00
|
|
|
if (atomic_load(&p->state) != AO_STATE_PLAY) {
|
|
|
|
atomic_store(&p->state, AO_STATE_PLAY);
|
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->resume(ao);
|
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.
|
|
|
|
// The caller should set out_time_us to the expected delay the last sample
|
|
|
|
// 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;
|
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 state = atomic_load(&p->state);
|
|
|
|
int bytes = 0;
|
2014-03-08 23:04:37 +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
|
|
|
if (state != AO_STATE_PLAY) {
|
|
|
|
if (state == AO_STATE_WAIT)
|
|
|
|
atomic_store(&p->state, AO_STATE_NONE);
|
|
|
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
if (buffered_bytes - bytes <= mp_ring_size(p->buffers[0]) / 2)
|
|
|
|
mp_input_wakeup_nolock(ao->input_ctx);
|
|
|
|
|
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:
|
|
|
|
// pad with silence (underflow/paused/eof)
|
|
|
|
for (int n = 0; n < ao->num_planes; n++)
|
|
|
|
af_fill_silence(data[n], full_bytes - bytes, ao->format);
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
return bytes / ao->sstride;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
static float get_delay(struct ao *ao)
|
|
|
|
{
|
|
|
|
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;
|
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
|
|
|
if (ao->driver->reset) {
|
|
|
|
ao->driver->reset(ao); // assumes the audio callback thread is stopped
|
|
|
|
atomic_store(&p->state, AO_STATE_NONE);
|
|
|
|
} else {
|
|
|
|
// The thread keeps running. Wait until the audio callback gets into
|
|
|
|
// a defined state where it won't touch the ringbuffer. We must do
|
|
|
|
// this, because emptying the ringbuffer is not an atomic operation.
|
|
|
|
if (atomic_load(&p->state) != AO_STATE_NONE) {
|
|
|
|
atomic_store(&p->state, AO_STATE_WAIT);
|
|
|
|
while (atomic_load(&p->state) != AO_STATE_NONE)
|
|
|
|
mp_sleep_us(1);
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
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
|
|
|
if (ao->driver->reset)
|
|
|
|
ao->driver->reset(ao);
|
|
|
|
atomic_store(&p->state, AO_STATE_NONE);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void resume(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_store(&p->state, AO_STATE_PLAY);
|
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->resume(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 18:06:33 +00:00
|
|
|
static void drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_pull_state *p = ao->api_priv;
|
|
|
|
if (atomic_load(&p->state) == AO_STATE_PLAY)
|
|
|
|
mp_sleep_us(get_delay(ao) * 1000000);
|
|
|
|
reset(ao);
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2014-03-08 23:04:37 +00:00
|
|
|
{
|
2014-03-08 23:49:39 +00:00
|
|
|
ao->driver->uninit(ao);
|
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);
|
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,
|
|
|
|
.pause = pause,
|
|
|
|
.resume = resume,
|
|
|
|
.priv_size = sizeof(struct ao_pull_state),
|
|
|
|
};
|