1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-19 13:21:13 +00:00
mpv/audio/out
wm4 c36faf8c49 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 02:24:17 +02:00
..
ao_alsa.c af_lavrresample: remove avresample_set_channel_mapping() fallbacks 2014-03-16 13:19:28 +01:00
ao_coreaudio_properties.c
ao_coreaudio_properties.h
ao_coreaudio_utils.c ao_coreaudio: skip unknown channel labels 2014-05-10 14:07:45 +02:00
ao_coreaudio_utils.h ao_coreaudio: use description-based channel layouts 2014-05-10 14:07:45 +02:00
ao_coreaudio.c ao_coreaudio: remove useless code 2014-05-10 14:07:45 +02:00
ao_dsound.c af_fmt2bits: change to af_fmt2bps (bytes/sample) where appropriate 2014-05-28 21:38:00 +02:00
ao_jack.c audio/out/pull: remove race conditions 2014-05-29 02:24:17 +02:00
ao_lavc.c af_fmt2bits: change to af_fmt2bps (bytes/sample) where appropriate 2014-05-28 21:38:00 +02:00
ao_null.c ao_null: fix unit mismatch with latency option 2014-05-04 16:57:32 +02:00
ao_openal.c
ao_oss.c af_fmt2bits: change to af_fmt2bps (bytes/sample) where appropriate 2014-05-28 21:38:00 +02:00
ao_pcm.c af_fmt2bits: change to af_fmt2bps (bytes/sample) where appropriate 2014-05-28 21:38:00 +02:00
ao_portaudio.c audio/out/pull: remove race conditions 2014-05-29 02:24:17 +02:00
ao_pulse.c ao_pulse: use ao_need_data() 2014-04-15 22:42:15 +02:00
ao_rsound.c ao_rsound: pass correct data type to rsd_set_param() 2014-03-09 19:11:49 +01:00
ao_sdl.c audio/out/pull: remove race conditions 2014-05-29 02:24:17 +02:00
ao_sndio.c
ao_wasapi_utils.c ao_wasapi: simplify nAvgBytesPerSec calculation 2014-05-28 21:38:15 +02:00
ao_wasapi_utils.h ao_wasapi: Move non-critical code outside of the event thread 2014-03-11 16:37:02 -03:00
ao_wasapi.c audio/out/pull: remove race conditions 2014-05-29 02:24:17 +02:00
ao_wasapi.h atomics: switch to C11 stdatomic.h 2014-05-21 02:21:18 +02:00
ao.c af_fmt2bits: change to af_fmt2bps (bytes/sample) where appropriate 2014-05-28 21:38:00 +02:00
ao.h player: unrangle one aspect of audio EOF handling 2014-04-17 23:48:09 +02:00
internal.h audio/out/pull: remove race conditions 2014-05-29 02:24:17 +02:00
pull.c audio/out/pull: remove race conditions 2014-05-29 02:24:17 +02:00
push.c threads: use mpv time for mpthread_cond_timedwait wrapper 2014-05-18 19:20:32 +02:00