diff options
author | Kacper Michajłow <kasper93@gmail.com> | 2023-10-21 04:55:41 +0200 |
---|---|---|
committer | Dudemanguy <random342@airmail.cc> | 2023-11-05 17:36:17 +0000 |
commit | 174df99ffa53f1091589eaa4fa0c16cdd55a9326 (patch) | |
tree | 3a60d45615f18beed98a9b08267c28ed7e05dd5f /audio/out | |
parent | 3a8b107f6216b38a151d5ca1e9d4f2727e3418f5 (diff) | |
download | mpv-174df99ffa53f1091589eaa4fa0c16cdd55a9326.tar.bz2 mpv-174df99ffa53f1091589eaa4fa0c16cdd55a9326.tar.xz |
ALL: use new mp_thread abstraction
Diffstat (limited to 'audio/out')
-rw-r--r-- | audio/out/ao_audiotrack.c | 39 | ||||
-rw-r--r-- | audio/out/ao_lavc.c | 6 | ||||
-rw-r--r-- | audio/out/ao_opensles.c | 13 | ||||
-rw-r--r-- | audio/out/ao_pulse.c | 1 | ||||
-rw-r--r-- | audio/out/ao_wasapi.c | 2 | ||||
-rw-r--r-- | audio/out/buffer.c | 117 | ||||
-rw-r--r-- | audio/out/internal.h | 1 |
7 files changed, 87 insertions, 92 deletions
diff --git a/audio/out/ao_audiotrack.c b/audio/out/ao_audiotrack.c index 321e3cb851..fcb9aae4e8 100644 --- a/audio/out/ao_audiotrack.c +++ b/audio/out/ao_audiotrack.c @@ -62,9 +62,9 @@ struct priv { bool thread_terminate; bool thread_created; - pthread_t thread; - pthread_mutex_t lock; - pthread_cond_t wakeup; + mp_thread thread; + mp_mutex lock; + mp_cond wakeup; }; struct JNIByteBuffer { @@ -549,13 +549,13 @@ static int init_jni(struct ao *ao) return 0; } -static void *playthread(void *arg) +static MP_THREAD_VOID playthread(void *arg) { struct ao *ao = arg; struct priv *p = ao->priv; JNIEnv *env = MP_JNI_GET_ENV(ao); - mpthread_set_name("ao/audiotrack"); - pthread_mutex_lock(&p->lock); + mp_thread_set_name("ao/audiotrack"); + mp_mutex_lock(&p->lock); while (!p->thread_terminate) { int state = AudioTrack.PLAYSTATE_PAUSED; if (p->audiotrack) { @@ -579,12 +579,11 @@ static void *playthread(void *arg) MP_ERR(ao, "AudioTrack.write failed with %d\n", ret); } } else { - struct timespec wait = mp_rel_time_to_timespec(0.300); - pthread_cond_timedwait(&p->wakeup, &p->lock, &wait); + mp_cond_timedwait(&p->wakeup, &p->lock, MP_TIME_MS_TO_NS(300)); } } - pthread_mutex_unlock(&p->lock); - return NULL; + mp_mutex_unlock(&p->lock); + MP_THREAD_RETURN(); } static void uninit(struct ao *ao) @@ -598,13 +597,13 @@ static void uninit(struct ao *ao) MP_JNI_EXCEPTION_LOG(ao); } - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); p->thread_terminate = true; - pthread_cond_signal(&p->wakeup); - pthread_mutex_unlock(&p->lock); + mp_cond_signal(&p->wakeup); + mp_mutex_unlock(&p->lock); if (p->thread_created) - pthread_join(p->thread, NULL); + mp_thread_join(p->thread); if (p->audiotrack) { MP_JNI_CALL_VOID(p->audiotrack, AudioTrack.release); @@ -638,8 +637,8 @@ static void uninit(struct ao *ao) p->timestamp = NULL; } - pthread_cond_destroy(&p->wakeup); - pthread_mutex_destroy(&p->lock); + mp_cond_destroy(&p->wakeup); + mp_mutex_destroy(&p->lock); uninit_jni(ao); } @@ -651,8 +650,8 @@ static int init(struct ao *ao) if (!env) return -1; - pthread_mutex_init(&p->lock, NULL); - pthread_cond_init(&p->wakeup, NULL); + mp_mutex_init(&p->lock); + mp_cond_init(&p->wakeup); if (init_jni(ao) < 0) return -1; @@ -781,7 +780,7 @@ static int init(struct ao *ao) goto error; } - if (pthread_create(&p->thread, NULL, playthread, ao)) { + if (mp_thread_create(&p->thread, playthread, ao)) { MP_ERR(ao, "pthread creation failed\n"); goto error; } @@ -828,7 +827,7 @@ static void start(struct ao *ao) MP_JNI_CALL_VOID(p->audiotrack, AudioTrack.play); MP_JNI_EXCEPTION_LOG(ao); - pthread_cond_signal(&p->wakeup); + mp_cond_signal(&p->wakeup); } #define OPT_BASE_STRUCT struct priv diff --git a/audio/out/ao_lavc.c b/audio/out/ao_lavc.c index 0f176cb54e..163fdcae4f 100644 --- a/audio/out/ao_lavc.c +++ b/audio/out/ao_lavc.c @@ -173,7 +173,7 @@ static int init(struct ao *ao) return 0; fail: - pthread_mutex_unlock(&ao->encode_lavc_ctx->lock); + mp_mutex_unlock(&ao->encode_lavc_ctx->lock); ac->shutdown = true; return -1; } @@ -261,7 +261,7 @@ static bool audio_write(struct ao *ao, void **data, int samples) double outpts = pts; // for ectx PTS fields - pthread_mutex_lock(&ectx->lock); + mp_mutex_lock(&ectx->lock); if (!ectx->options->rawts) { // Fix and apply the discontinuity pts offset. @@ -290,7 +290,7 @@ static bool audio_write(struct ao *ao, void **data, int samples) ectx->next_in_pts = nextpts; } - pthread_mutex_unlock(&ectx->lock); + mp_mutex_unlock(&ectx->lock); mp_aframe_set_pts(af, outpts); diff --git a/audio/out/ao_opensles.c b/audio/out/ao_opensles.c index 9ade73c857..ddcff1904a 100644 --- a/audio/out/ao_opensles.c +++ b/audio/out/ao_opensles.c @@ -23,13 +23,12 @@ #include "common/msg.h" #include "audio/format.h" #include "options/m_option.h" +#include "osdep/threads.h" #include "osdep/timer.h" #include <SLES/OpenSLES.h> #include <SLES/OpenSLES_Android.h> -#include <pthread.h> - struct priv { SLObjectItf sl, output_mix, player; SLBufferQueueItf buffer_queue; @@ -37,7 +36,7 @@ struct priv { SLPlayItf play; void *buf; int bytes_per_enqueue; - pthread_mutex_t buffer_lock; + mp_mutex buffer_lock; double audio_latency; int frames_per_enqueue; @@ -62,7 +61,7 @@ static void uninit(struct ao *ao) p->engine = NULL; p->play = NULL; - pthread_mutex_destroy(&p->buffer_lock); + mp_mutex_destroy(&p->buffer_lock); free(p->buf); p->buf = NULL; @@ -77,7 +76,7 @@ static void buffer_callback(SLBufferQueueItf buffer_queue, void *context) SLresult res; double delay; - pthread_mutex_lock(&p->buffer_lock); + mp_mutex_lock(&p->buffer_lock); delay = p->frames_per_enqueue / (double)ao->samplerate; delay += p->audio_latency; @@ -88,7 +87,7 @@ static void buffer_callback(SLBufferQueueItf buffer_queue, void *context) if (res != SL_RESULT_SUCCESS) MP_ERR(ao, "Failed to Enqueue: %d\n", res); - pthread_mutex_unlock(&p->buffer_lock); + mp_mutex_unlock(&p->buffer_lock); } #define CHK(stmt) \ @@ -170,7 +169,7 @@ static int init(struct ao *ao) goto error; } - int r = pthread_mutex_init(&p->buffer_lock, NULL); + int r = mp_mutex_init(&p->buffer_lock); if (r) { MP_ERR(ao, "Failed to initialize the mutex: %d\n", r); goto error; diff --git a/audio/out/ao_pulse.c b/audio/out/ao_pulse.c index ae19c875c0..3b29b1a3ad 100644 --- a/audio/out/ao_pulse.c +++ b/audio/out/ao_pulse.c @@ -24,7 +24,6 @@ #include <string.h> #include <stdint.h> #include <math.h> -#include <pthread.h> #include <pulse/pulseaudio.h> diff --git a/audio/out/ao_wasapi.c b/audio/out/ao_wasapi.c index 5f6468f33e..b201f260e6 100644 --- a/audio/out/ao_wasapi.c +++ b/audio/out/ao_wasapi.c @@ -199,7 +199,7 @@ static DWORD __stdcall AudioThread(void *lpParameter) { struct ao *ao = lpParameter; struct wasapi_state *state = ao->priv; - mpthread_set_name("ao/wasapi"); + mp_thread_set_name("ao/wasapi"); CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); state->init_ok = wasapi_thread_init(ao); diff --git a/audio/out/buffer.c b/audio/out/buffer.c index c0457ba279..4ab3e405bc 100644 --- a/audio/out/buffer.c +++ b/audio/out/buffer.c @@ -16,7 +16,6 @@ */ #include <stddef.h> -#include <pthread.h> #include <inttypes.h> #include <math.h> #include <unistd.h> @@ -39,12 +38,12 @@ struct buffer_state { // Buffer and AO - pthread_mutex_t lock; - pthread_cond_t wakeup; + mp_mutex lock; + mp_cond wakeup; // Playthread sleep - pthread_mutex_t pt_lock; - pthread_cond_t pt_wakeup; + mp_mutex pt_lock; + mp_cond pt_wakeup; // Access from AO driver's thread only. char *convert_buffer; @@ -70,7 +69,7 @@ struct buffer_state { bool hw_paused; // driver->set_pause() was used successfully bool recover_pause; // non-hw_paused: needs to recover delay struct mp_pcm_state prepause_state; - pthread_t thread; // thread shoveling data to AO + mp_thread thread; // thread shoveling data to AO bool thread_valid; // thread is running struct mp_aframe *temp_buf; @@ -79,15 +78,15 @@ struct buffer_state { bool terminate; // exit thread }; -static void *playthread(void *arg); +static MP_THREAD_VOID playthread(void *arg); void ao_wakeup_playthread(struct ao *ao) { struct buffer_state *p = ao->buffer_state; - pthread_mutex_lock(&p->pt_lock); + mp_mutex_lock(&p->pt_lock); p->need_wakeup = true; - pthread_cond_broadcast(&p->pt_wakeup); - pthread_mutex_unlock(&p->pt_lock); + mp_cond_broadcast(&p->pt_wakeup); + mp_mutex_unlock(&p->pt_lock); } // called locked @@ -184,7 +183,7 @@ int ao_read_data(struct ao *ao, void **data, int samples, int64_t out_time_ns) struct buffer_state *p = ao->buffer_state; assert(!ao->driver->write); - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); int pos = read_buffer(ao, data, samples, &(bool){0}); @@ -195,10 +194,10 @@ int ao_read_data(struct ao *ao, void **data, int samples, int64_t out_time_ns) p->playing = false; ao->wakeup_cb(ao->wakeup_ctx); // For ao_drain(). - pthread_cond_broadcast(&p->wakeup); + mp_cond_broadcast(&p->wakeup); } - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); return pos; } @@ -248,12 +247,12 @@ int ao_control(struct ao *ao, enum aocontrol cmd, void *arg) if (ao->driver->control) { // Only need to lock in push mode. if (ao->driver->write) - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); r = ao->driver->control(ao, cmd, arg); if (ao->driver->write) - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); } return r; } @@ -262,7 +261,7 @@ double ao_get_delay(struct ao *ao) { struct buffer_state *p = ao->buffer_state; - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); double driver_delay; if (ao->driver->write) { @@ -279,7 +278,7 @@ double ao_get_delay(struct ao *ao) if (p->pending) pending += mp_aframe_get_size(p->pending); - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); return driver_delay + pending / (double)ao->samplerate; } @@ -290,7 +289,7 @@ void ao_reset(struct ao *ao) bool wakeup = false; bool do_reset = false; - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); TA_FREEP(&p->pending); mp_async_queue_reset(p->queue); @@ -313,7 +312,7 @@ void ao_reset(struct ao *ao) p->hw_paused = false; p->end_time_ns = 0; - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); if (do_reset) ao->driver->reset(ao); @@ -331,7 +330,7 @@ void ao_start(struct ao *ao) struct buffer_state *p = ao->buffer_state; bool do_start = false; - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); p->playing = true; @@ -340,7 +339,7 @@ void ao_start(struct ao *ao) do_start = true; } - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); // Pull AOs might call ao_read_data() so do this outside the lock. if (do_start) @@ -360,7 +359,7 @@ void ao_set_paused(struct ao *ao, bool paused, bool eof) if (eof && paused && ao_is_playing(ao)) ao_drain(ao); - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); if ((p->playing || !ao->driver->write) && !p->paused && paused) { if (p->streaming && !ao->stream_silence) { @@ -395,7 +394,7 @@ void ao_set_paused(struct ao *ao, bool paused, bool eof) } p->paused = paused; - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); if (do_reset) ao->driver->reset(ao); @@ -414,9 +413,9 @@ bool ao_is_playing(struct ao *ao) { struct buffer_state *p = ao->buffer_state; - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); bool playing = p->playing; - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); return playing; } @@ -426,30 +425,31 @@ void ao_drain(struct ao *ao) { struct buffer_state *p = ao->buffer_state; - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); while (!p->paused && p->playing) { - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); double delay = ao_get_delay(ao); - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); // Limit to buffer + arbitrary ~250ms max. waiting for robustness. delay += mp_async_queue_get_samples(p->queue) / (double)ao->samplerate; - struct timespec ts = mp_rel_time_to_timespec(MPMAX(delay, 0) + 0.25); // Wait for EOF signal from AO. - if (pthread_cond_timedwait(&p->wakeup, &p->lock, &ts)) { + if (mp_cond_timedwait(&p->wakeup, &p->lock, + MP_TIME_S_TO_NS(MPMAX(delay, 0) + 0.25))) + { MP_VERBOSE(ao, "drain timeout\n"); break; } if (!p->playing && mp_async_queue_get_samples(p->queue)) { MP_WARN(ao, "underrun during draining\n"); - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); ao_start(ao); - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); } } - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); ao_reset(ao); } @@ -465,12 +465,12 @@ void ao_uninit(struct ao *ao) struct buffer_state *p = ao->buffer_state; if (p && p->thread_valid) { - pthread_mutex_lock(&p->pt_lock); + mp_mutex_lock(&p->pt_lock); p->terminate = true; - pthread_cond_broadcast(&p->pt_wakeup); - pthread_mutex_unlock(&p->pt_lock); + mp_cond_broadcast(&p->pt_wakeup); + mp_mutex_unlock(&p->pt_lock); - pthread_join(p->thread, NULL); + mp_thread_join(p->thread); p->thread_valid = false; } @@ -484,11 +484,11 @@ void ao_uninit(struct ao *ao) talloc_free(p->convert_buffer); talloc_free(p->temp_buf); - pthread_cond_destroy(&p->wakeup); - pthread_mutex_destroy(&p->lock); + mp_cond_destroy(&p->wakeup); + mp_mutex_destroy(&p->lock); - pthread_cond_destroy(&p->pt_wakeup); - pthread_mutex_destroy(&p->pt_lock); + mp_cond_destroy(&p->pt_wakeup); + mp_mutex_destroy(&p->pt_lock); } talloc_free(ao); @@ -509,11 +509,11 @@ bool init_buffer_post(struct ao *ao) assert(ao->driver->get_state); } - pthread_mutex_init(&p->lock, NULL); - pthread_cond_init(&p->wakeup, NULL); + mp_mutex_init(&p->lock); + mp_cond_init(&p->wakeup); - pthread_mutex_init(&p->pt_lock, NULL); - pthread_cond_init(&p->pt_wakeup, NULL); + mp_mutex_init(&p->pt_lock); + mp_cond_init(&p->pt_wakeup); p->queue = mp_async_queue_create(); p->filter_root = mp_filter_create_root(ao->global); @@ -532,7 +532,7 @@ bool init_buffer_post(struct ao *ao) mp_filter_graph_set_wakeup_cb(p->filter_root, wakeup_filters, ao); p->thread_valid = true; - if (pthread_create(&p->thread, NULL, playthread, ao)) { + if (mp_thread_create(&p->thread, playthread, ao)) { p->thread_valid = false; return false; } @@ -651,17 +651,17 @@ eof: } ao->wakeup_cb(ao->wakeup_ctx); // For ao_drain(). - pthread_cond_broadcast(&p->wakeup); + mp_cond_broadcast(&p->wakeup); return true; } -static void *playthread(void *arg) +static MP_THREAD_VOID playthread(void *arg) { struct ao *ao = arg; struct buffer_state *p = ao->buffer_state; - mpthread_set_name("ao"); + mp_thread_set_name("ao"); while (1) { - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); bool retry = false; if (!ao->driver->initially_blocked || p->initial_unblocked) @@ -677,32 +677,31 @@ static void *playthread(void *arg) timeout = ao->device_buffer / (double)ao->samplerate * 0.25; } - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); - pthread_mutex_lock(&p->pt_lock); + mp_mutex_lock(&p->pt_lock); if (p->terminate) { - pthread_mutex_unlock(&p->pt_lock); + mp_mutex_unlock(&p->pt_lock); break; } if (!p->need_wakeup && !retry) { MP_STATS(ao, "start audio wait"); - struct timespec ts = mp_rel_time_to_timespec(timeout); - pthread_cond_timedwait(&p->pt_wakeup, &p->pt_lock, &ts); + mp_cond_timedwait(&p->pt_wakeup, &p->pt_lock, MP_TIME_S_TO_NS(timeout)); MP_STATS(ao, "end audio wait"); } p->need_wakeup = false; - pthread_mutex_unlock(&p->pt_lock); + mp_mutex_unlock(&p->pt_lock); } - return NULL; + MP_THREAD_RETURN(); } void ao_unblock(struct ao *ao) { if (ao->driver->write) { struct buffer_state *p = ao->buffer_state; - pthread_mutex_lock(&p->lock); + mp_mutex_lock(&p->lock); p->initial_unblocked = true; - pthread_mutex_unlock(&p->lock); + mp_mutex_unlock(&p->lock); ao_wakeup_playthread(ao); } } diff --git a/audio/out/internal.h b/audio/out/internal.h index 182b2cbc93..1ae92a8b92 100644 --- a/audio/out/internal.h +++ b/audio/out/internal.h @@ -18,7 +18,6 @@ #ifndef MP_AO_INTERNAL_H_ #define MP_AO_INTERNAL_H_ -#include <pthread.h> #include <stdatomic.h> #include <stdbool.h> |