From 8e6701bcbb915a0635bd4ed369ac872f55e8b029 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Sat, 9 Sep 2017 15:35:00 -1000 Subject: [PATCH] Allow CLI dynamic choice of A/V backends --- src/audio/soundcore-openal.c | 5 ++++ src/audio/soundcore-opensles.c | 5 ++++ src/audio/soundcore.c | 55 +++++++++++++++++++++++++++------- src/audio/soundcore.h | 6 ++++ src/misc.c | 43 +++++++++++++++++++++++++- src/video/glnode.c | 12 +++++--- src/video/video.c | 43 +++++++++++++++++++++++--- src/video/video.h | 9 +++++- src/video/xvideo.c | 5 ++++ 9 files changed, 163 insertions(+), 20 deletions(-) diff --git a/src/audio/soundcore-openal.c b/src/audio/soundcore-openal.c index 2f58dc7c..32532372 100644 --- a/src/audio/soundcore-openal.c +++ b/src/audio/soundcore-openal.c @@ -539,6 +539,10 @@ static long openal_systemShutdown(INOUT AudioContext_s **audio_context) { return 0; } +static const char *openal_systemName(void) { + return "OpenAL"; +} + static long openal_systemSetup(INOUT AudioContext_s **audio_context) { assert(*audio_context == NULL); assert(voices == NULL); @@ -625,6 +629,7 @@ static long openal_systemResume(AudioContext_s *audio_context) { static void _init_openal(void) { LOG("Initializing OpenAL sound system"); + openal_audio_backend.name = &openal_systemName; openal_audio_backend.setup = &openal_systemSetup; openal_audio_backend.shutdown = &openal_systemShutdown; openal_audio_backend.pause = &openal_systemPause; diff --git a/src/audio/soundcore-opensles.c b/src/audio/soundcore-opensles.c index 714a14c4..24ef0530 100644 --- a/src/audio/soundcore-opensles.c +++ b/src/audio/soundcore-opensles.c @@ -526,6 +526,10 @@ static long opensles_systemShutdown(AudioContext_s **audio_context) { return 0; } +static const char *opensles_systemName(void) { + return "OpenSLES"; +} + static long opensles_systemSetup(INOUT AudioContext_s **audio_context) { assert(*audio_context == NULL); @@ -760,6 +764,7 @@ static long opensles_systemResume(AudioContext_s *audio_context) { static void _init_opensl(void) { LOG("Initializing OpenSLES sound system"); + opensles_audio_backend.name = &opensles_systemName; opensles_audio_backend.setup = &opensles_systemSetup; opensles_audio_backend.shutdown = &opensles_systemShutdown; opensles_audio_backend.pause = &opensles_systemPause; diff --git a/src/audio/soundcore.c b/src/audio/soundcore.c index 5844dd81..9678a880 100644 --- a/src/audio/soundcore.c +++ b/src/audio/soundcore.c @@ -34,6 +34,8 @@ typedef struct backend_node_s { static backend_node_s *head = NULL; +static AudioBackend_s *currentBackend = NULL; + //----------------------------------------------------------------------------- long audio_createSoundBuffer(INOUT AudioBuffer_s **audioBuffer) { @@ -82,10 +84,10 @@ bool audio_init(void) { do { if (audioContext) { - audio_getCurrentBackend()->shutdown(&audioContext); + currentBackend->shutdown(&audioContext); } - long err = audio_getCurrentBackend()->setup((AudioContext_s**)&audioContext); + long err = currentBackend->setup((AudioContext_s**)&audioContext); if (err) { LOG("Failed to create an audio context!"); break; @@ -103,7 +105,7 @@ void audio_shutdown(void) { if (!audio_isAvailable) { return; } - audio_getCurrentBackend()->shutdown(&audioContext); + currentBackend->shutdown(&audioContext); audio_isAvailable = false; } @@ -118,7 +120,7 @@ void audio_pause(void) { if (!audio_isAvailable) { return; } - audio_getCurrentBackend()->pause(audioContext); + currentBackend->pause(audioContext); } void audio_resume(void) { @@ -127,7 +129,7 @@ void audio_resume(void) { if (!audio_isAvailable) { return; } - audio_getCurrentBackend()->resume(audioContext); + currentBackend->resume(audioContext); } void audio_setLatency(float latencySecs) { @@ -141,9 +143,6 @@ float audio_getLatency(void) { //----------------------------------------------------------------------------- void audio_registerBackend(AudioBackend_s *backend, long order) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_lock(&mutex); - backend_node_s *node = MALLOC(sizeof(backend_node_s)); assert(node); node->next = NULL; @@ -163,11 +162,46 @@ void audio_registerBackend(AudioBackend_s *backend, long order) { } node->next = p; - pthread_mutex_unlock(&mutex); + currentBackend = head->backend; +} + +void audio_printBackends(FILE *out) { + backend_node_s *p = head; + int count = 0; + while (p) { + const char *name = p->backend->name(); + if (count++) { + fprintf(out, "|"); + } + fprintf(out, "%s", name); + p = p->next; + } +} + +static const char *_null_backend_name(void); +void audio_chooseBackend(const char *name) { + if (!name) { + name = _null_backend_name(); + } + + backend_node_s *p = head; + while (p) { + const char *bname = p->backend->name(); + if (strcasecmp(name, bname) == 0) { + currentBackend = p->backend; + LOG("Setting current audio backend to %s", name); + break; + } + p = p->next; + } } AudioBackend_s *audio_getCurrentBackend(void) { - return head->backend; + return currentBackend; +} + +static const char *_null_backend_name(void) { + return "none"; } static long _null_backend_setup(INOUT AudioContext_s **audio_context) { @@ -191,6 +225,7 @@ static long _null_backend_resume(AudioContext_s *audio_context) { static void _init_soundcore(void) { LOG("Initializing audio subsystem"); static AudioBackend_s null_backend = { { 0 } }; + null_backend.name = &_null_backend_name; null_backend.setup = &_null_backend_setup; null_backend.shutdown = &_null_backend_shutdown; null_backend.pause = &_null_backend_pause; diff --git a/src/audio/soundcore.h b/src/audio/soundcore.h index e27b96f1..1a57f248 100644 --- a/src/audio/soundcore.h +++ b/src/audio/soundcore.h @@ -137,6 +137,8 @@ typedef struct AudioBackend_s { AudioSettings_s systemSettings; + const char *(*name)(void); + // basic backend functionality controlled by soundcore PRIVATE long (*setup)(INOUT AudioContext_s **audio_context); PRIVATE long (*shutdown)(INOUT AudioContext_s **audio_context); @@ -155,6 +157,10 @@ enum { void audio_registerBackend(AudioBackend_s *backend, long prio); +void audio_printBackends(FILE *out); + +void audio_chooseBackend(const char *name); + AudioBackend_s *audio_getCurrentBackend(void); #endif /* whole file */ diff --git a/src/misc.c b/src/misc.c index daf997e0..623bff5e 100644 --- a/src/misc.c +++ b/src/misc.c @@ -44,6 +44,43 @@ static void _init_common(void) { static __attribute__((constructor)) void __init_common(void) { emulator_registerStartupCallback(CTOR_PRIORITY_FIRST, &_init_common); } + +static void _cli_help(void) { + fprintf(stderr, "\n"); + fprintf(stderr, "Usage: %s [-A