Start documenting audio_thread_wrapper.c

Also - early returns if thr is NULL, and move declaration of
variables to top
This commit is contained in:
twinaphex 2015-01-09 22:47:53 +01:00
parent 500aa292f5
commit 2ba1a3a527
3 changed files with 76 additions and 13 deletions

View File

@ -1,5 +1,6 @@
/* RetroArch - A frontend for libretro. /* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2015 - Daniel De Matteis
* *
* RetroArch is free software: you can redistribute it and/or modify it under the terms * RetroArch 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 Found- * of the GNU General Public License as published by the Free Software Found-
@ -36,7 +37,7 @@ typedef struct audio_thread
int inited; int inited;
// Init options. /* Initialization options. */
const char *device; const char *device;
unsigned out_rate; unsigned out_rate;
unsigned latency; unsigned latency;
@ -46,6 +47,9 @@ static void audio_thread_loop(void *data)
{ {
audio_thread_t *thr = (audio_thread_t*)data; audio_thread_t *thr = (audio_thread_t*)data;
if (!thr)
return;
RARCH_LOG("[Audio Thread]: Initializing audio driver.\n"); RARCH_LOG("[Audio Thread]: Initializing audio driver.\n");
thr->driver_data = thr->driver->init(thr->device, thr->out_rate, thr->latency); thr->driver_data = thr->driver->init(thr->device, thr->out_rate, thr->latency);
slock_lock(thr->lock); slock_lock(thr->lock);
@ -58,7 +62,8 @@ static void audio_thread_loop(void *data)
if (thr->inited < 0) if (thr->inited < 0)
return; return;
// Wait until we start to avoid calling stop immediately after init. /* Wait until we start to avoid calling
* stop immediately after initialization. */
slock_lock(thr->lock); slock_lock(thr->lock);
while (thr->stopped) while (thr->stopped)
scond_wait(thr->cond, thr->lock); scond_wait(thr->cond, thr->lock);
@ -95,6 +100,9 @@ static void audio_thread_loop(void *data)
static void audio_thread_block(audio_thread_t *thr) static void audio_thread_block(audio_thread_t *thr)
{ {
if (!thr)
return;
slock_lock(thr->lock); slock_lock(thr->lock);
thr->stopped = true; thr->stopped = true;
scond_signal(thr->cond); scond_signal(thr->cond);
@ -103,6 +111,9 @@ static void audio_thread_block(audio_thread_t *thr)
static void audio_thread_unblock(audio_thread_t *thr) static void audio_thread_unblock(audio_thread_t *thr)
{ {
if (!thr)
return;
slock_lock(thr->lock); slock_lock(thr->lock);
thr->stopped = false; thr->stopped = false;
scond_signal(thr->cond); scond_signal(thr->cond);
@ -112,6 +123,7 @@ static void audio_thread_unblock(audio_thread_t *thr)
static void audio_thread_free(void *data) static void audio_thread_free(void *data)
{ {
audio_thread_t *thr = (audio_thread_t*)data; audio_thread_t *thr = (audio_thread_t*)data;
if (!thr) if (!thr)
return; return;
@ -137,27 +149,42 @@ static bool audio_thread_alive(void *data)
{ {
bool alive = false; bool alive = false;
audio_thread_t *thr = (audio_thread_t*)data; audio_thread_t *thr = (audio_thread_t*)data;
if (!thr)
return false;
audio_thread_block(thr); audio_thread_block(thr);
alive = !thr->is_paused; alive = !thr->is_paused;
audio_thread_unblock(thr); audio_thread_unblock(thr);
return alive; return alive;
} }
static bool audio_thread_stop(void *data) static bool audio_thread_stop(void *data)
{ {
audio_thread_t *thr = (audio_thread_t*)data; audio_thread_t *thr = (audio_thread_t*)data;
if (!thr)
return false;
audio_thread_block(thr); audio_thread_block(thr);
thr->is_paused = true; thr->is_paused = true;
g_extern.system.audio_callback.set_state(false); g_extern.system.audio_callback.set_state(false);
return true; return true;
} }
static bool audio_thread_start(void *data) static bool audio_thread_start(void *data)
{ {
audio_thread_t *thr = (audio_thread_t*)data; audio_thread_t *thr = (audio_thread_t*)data;
if (!thr)
return false;
g_extern.system.audio_callback.set_state(true); g_extern.system.audio_callback.set_state(true);
thr->is_paused = false; thr->is_paused = false;
audio_thread_unblock(thr); audio_thread_unblock(thr);
return true; return true;
} }
@ -170,20 +197,27 @@ static void audio_thread_set_nonblock_state(void *data, bool state)
static bool audio_thread_use_float(void *data) static bool audio_thread_use_float(void *data)
{ {
audio_thread_t *thr = (audio_thread_t*)data; audio_thread_t *thr = (audio_thread_t*)data;
if (!thr)
return false;
return thr->use_float; return thr->use_float;
} }
static ssize_t audio_thread_write(void *data, const void *buf, size_t size) static ssize_t audio_thread_write(void *data, const void *buf, size_t size)
{ {
ssize_t ret;
audio_thread_t *thr = (audio_thread_t*)data; audio_thread_t *thr = (audio_thread_t*)data;
ssize_t ret = thr->driver->write(thr->driver_data, buf, size);
if (!thr)
return 0;
ret = thr->driver->write(thr->driver_data, buf, size);
if (ret < 0) if (ret < 0)
{ {
slock_lock(thr->lock); slock_lock(thr->lock);
thr->alive = false; thr->alive = false;
scond_signal(thr->cond); scond_signal(thr->cond);
slock_unlock(thr->lock); slock_unlock(thr->lock);
return ret;
} }
return ret; return ret;
@ -199,10 +233,25 @@ static const audio_driver_t audio_thread = {
audio_thread_free, audio_thread_free,
audio_thread_use_float, audio_thread_use_float,
"audio-thread", "audio-thread",
NULL, // No point in using rate control with threaded audio. NULL, /* No point in using rate control with threaded audio. */
NULL, NULL,
}; };
/**
* rarch_threaded_audio_init:
* @out_driver : output driver
* @out_data : output audio data
* @device : audio device (optional)
* @out_rate : output audio rate
* @latency : audio latency
* @driver : audio driver
*
* Starts a audio driver in a new thread.
* Access to audio driver will be mediated through this driver.
* This driver interfaces with audio callback and is only used in that case.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool rarch_threaded_audio_init(const audio_driver_t **out_driver, void **out_data, bool rarch_threaded_audio_init(const audio_driver_t **out_driver, void **out_data,
const char *device, unsigned audio_out_rate, unsigned latency, const char *device, unsigned audio_out_rate, unsigned latency,
const audio_driver_t *drv) const audio_driver_t *drv)
@ -227,13 +276,13 @@ bool rarch_threaded_audio_init(const audio_driver_t **out_driver, void **out_dat
if (!(thr->thread = sthread_create(audio_thread_loop, thr))) if (!(thr->thread = sthread_create(audio_thread_loop, thr)))
goto error; goto error;
// Wait until thread has initialized (or failed) the driver. /* Wait until thread has initialized (or failed) the driver. */
slock_lock(thr->lock); slock_lock(thr->lock);
while (!thr->inited) while (!thr->inited)
scond_wait(thr->cond, thr->lock); scond_wait(thr->cond, thr->lock);
slock_unlock(thr->lock); slock_unlock(thr->lock);
if (thr->inited < 0) // Thread failed. if (thr->inited < 0) /* Thread failed. */
goto error; goto error;
*out_driver = &audio_thread; *out_driver = &audio_thread;
@ -246,4 +295,3 @@ error:
audio_thread_free(thr); audio_thread_free(thr);
return false; return false;
} }

View File

@ -1,5 +1,6 @@
/* RetroArch - A frontend for libretro. /* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2015 - Daniel De Matteis
* *
* RetroArch is free software: you can redistribute it and/or modify it under the terms * RetroArch 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 Found- * of the GNU General Public License as published by the Free Software Found-
@ -19,9 +20,21 @@
#include "../driver.h" #include "../driver.h"
#include <boolean.h> #include <boolean.h>
// Starts a audio driver in a new thread. /**
// Access to audio driver will be mediated through this driver. * rarch_threaded_audio_init:
// This driver interfaces with audio callback and is only used in that case. * @out_driver : output driver
* @out_data : output audio data
* @device : audio device (optional)
* @out_rate : output audio rate
* @latency : audio latency
* @driver : audio driver
*
* Starts a audio driver in a new thread.
* Access to audio driver will be mediated through this driver.
* This driver interfaces with audio callback and is only used in that case.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool rarch_threaded_audio_init(const audio_driver_t **out_driver, void **out_data, bool rarch_threaded_audio_init(const audio_driver_t **out_driver, void **out_data,
const char *device, unsigned out_rate, unsigned latency, const char *device, unsigned out_rate, unsigned latency,
const audio_driver_t *driver); const audio_driver_t *driver);

View File

@ -123,8 +123,9 @@ void audio_convert_s16_to_float_SSE2(float *out,
void audio_convert_float_to_s16_SSE2(int16_t *out, void audio_convert_float_to_s16_SSE2(int16_t *out,
const float *in, size_t samples) const float *in, size_t samples)
{ {
__m128 factor = _mm_set1_ps((float)0x8000);
size_t i; size_t i;
__m128 factor = _mm_set1_ps((float)0x8000);
for (i = 0; i + 8 <= samples; i += 8, in += 8, out += 8) for (i = 0; i + 8 <= samples; i += 8, in += 8, out += 8)
{ {
__m128 input[2] = { _mm_loadu_ps(in + 0), _mm_loadu_ps(in + 4) }; __m128 input[2] = { _mm_loadu_ps(in + 0), _mm_loadu_ps(in + 4) };
@ -354,6 +355,8 @@ void audio_convert_s16_to_float_ALLEGREX(float *out,
void audio_convert_float_to_s16_ALLEGREX(int16_t *out, void audio_convert_float_to_s16_ALLEGREX(int16_t *out,
const float *in, size_t samples) const float *in, size_t samples)
{ {
size_t i;
#ifdef DEBUG #ifdef DEBUG
/* Make sure the buffers are 16 byte aligned, this should be /* Make sure the buffers are 16 byte aligned, this should be
* the default behaviour of malloc in the PSPSDK. * the default behaviour of malloc in the PSPSDK.
@ -362,7 +365,6 @@ void audio_convert_float_to_s16_ALLEGREX(int16_t *out,
rarch_assert(((uintptr_t)out & 0xf) == 0); rarch_assert(((uintptr_t)out & 0xf) == 0);
#endif #endif
size_t i;
for (i = 0; i + 8 <= samples; i += 8) for (i = 0; i + 8 <= samples; i += 8)
{ {
__asm__ ( __asm__ (