Fixed queue initialization and overflow checks.

This commit is contained in:
Armin Novak 2013-09-30 10:11:54 +02:00
parent 6d14739ada
commit 8ccf7f8ca2
3 changed files with 25 additions and 25 deletions

View File

@ -66,9 +66,16 @@ static SLresult openSLPlayOpen(OPENSL_STREAM *p)
SLuint32 sr = p->sr; SLuint32 sr = p->sr;
SLuint32 channels = p->outchannels; SLuint32 channels = p->outchannels;
assert(p->engineObject);
assert(p->engineEngine);
if(channels){ if(channels){
// configure audio source // configure audio source
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2}; SLDataLocator_AndroidSimpleBufferQueue loc_bufq =
{
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
p->queuesize
};
switch(sr){ switch(sr){
@ -227,6 +234,7 @@ OPENSL_STREAM *android_OpenAudioDevice(int sr, int outchannels, int bufferframes
p = (OPENSL_STREAM *) calloc(sizeof(OPENSL_STREAM),1); p = (OPENSL_STREAM *) calloc(sizeof(OPENSL_STREAM),1);
memset(p, 0, sizeof(OPENSL_STREAM)); memset(p, 0, sizeof(OPENSL_STREAM));
p->queuesize = bufferframes;
p->outchannels = outchannels; p->outchannels = outchannels;
p->sr = sr; p->sr = sr;
@ -240,7 +248,6 @@ OPENSL_STREAM *android_OpenAudioDevice(int sr, int outchannels, int bufferframes
return NULL; return NULL;
} }
p->time = 0.;
p->queue = Queue_New(TRUE, -1, -1); p->queue = Queue_New(TRUE, -1, -1);
return p; return p;
@ -253,16 +260,12 @@ void android_CloseAudioDevice(OPENSL_STREAM *p){
return; return;
openSLDestroyEngine(p); openSLDestroyEngine(p);
if (p->queue)
Queue_Free(p->queue); Queue_Free(p->queue);
free(p); free(p);
} }
// returns timestamp of the processed stream
double android_GetTimestamp(OPENSL_STREAM *p){
return p->time;
}
// this callback handler is called every time a buffer finishes playing // this callback handler is called every time a buffer finishes playing
void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{ {
@ -282,6 +285,10 @@ int android_AudioOut(OPENSL_STREAM *p, const short *buffer,int size)
assert(buffer); assert(buffer);
assert(size > 0); assert(size > 0);
/* Assure, that the queue is not full. */
if (p->queuesize <= Queue_Count(p->queue))
WaitForSingleObject(p->queue->event, INFINITE);
void *data = calloc(size, sizeof(short)); void *data = calloc(size, sizeof(short));
memcpy(data, buffer, size * sizeof(short)); memcpy(data, buffer, size * sizeof(short));
(*p->bqPlayerBufferQueue)->Enqueue(p->bqPlayerBufferQueue, (*p->bqPlayerBufferQueue)->Enqueue(p->bqPlayerBufferQueue,

View File

@ -54,10 +54,10 @@ typedef struct opensl_stream {
SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue; SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
SLEffectSendItf bqPlayerEffectSend; SLEffectSendItf bqPlayerEffectSend;
double time;
unsigned int outchannels; unsigned int outchannels;
unsigned int sr; unsigned int sr;
unsigned int queuesize;
wQueue *queue; wQueue *queue;
} OPENSL_STREAM; } OPENSL_STREAM;
@ -74,11 +74,6 @@ typedef struct opensl_stream {
Write a buffer to the OpenSL stream *p, of size samples. Returns the number of samples written. Write a buffer to the OpenSL stream *p, of size samples. Returns the number of samples written.
*/ */
int android_AudioOut(OPENSL_STREAM *p, const short *buffer, int size); int android_AudioOut(OPENSL_STREAM *p, const short *buffer, int size);
/*
Get the current IO block time in seconds
*/
double android_GetTimestamp(OPENSL_STREAM *p);
/* /*
* Set the volume input level. * Set the volume input level.
*/ */

View File

@ -89,15 +89,12 @@ static bool rdpsnd_opensles_check_handle(const rdpsndopenslesPlugin *hdl)
{ {
bool rc = true; bool rc = true;
assert(hdl);
if (!hdl) if (!hdl)
rc = false; rc = false;
else else
{ {
assert(hdl->dsp_context);
if (!hdl->dsp_context) if (!hdl->dsp_context)
rc = false; rc = false;
assert(hdl->stream);
if (!hdl->stream) if (!hdl->stream)
rc = false; rc = false;
} }
@ -111,13 +108,14 @@ static void rdpsnd_opensles_set_volume(rdpsndDevicePlugin* device,
static int rdpsnd_opensles_set_params(rdpsndopenslesPlugin* opensles) static int rdpsnd_opensles_set_params(rdpsndopenslesPlugin* opensles)
{ {
DEBUG_SND("opensles=%p", opensles); DEBUG_SND("opensles=%p", opensles);
rdpsnd_opensles_check_handle(opensles); if (!rdpsnd_opensles_check_handle(opensles))
return;
if (opensles->stream) if (opensles->stream)
android_CloseAudioDevice(opensles->stream); android_CloseAudioDevice(opensles->stream);
opensles->stream = android_OpenAudioDevice( opensles->stream = android_OpenAudioDevice(
opensles->rate, opensles->channels, opensles->rate); opensles->rate, opensles->channels, 20);
return 0; return 0;
} }
@ -180,11 +178,12 @@ static void rdpsnd_opensles_open(rdpsndDevicePlugin* device,
DEBUG_SND("opensles=%p format=%p, latency=%d, rate=%d", DEBUG_SND("opensles=%p format=%p, latency=%d, rate=%d",
opensles, format, latency, opensles->rate); opensles, format, latency, opensles->rate);
if (opensles->stream)
if( rdpsnd_opensles_check_handle(opensles))
return; return;
opensles->stream = android_OpenAudioDevice( opensles->stream = android_OpenAudioDevice(
opensles->rate, opensles->channels, opensles->rate); opensles->rate, opensles->channels, 20);
assert(opensles->stream); assert(opensles->stream);
if (!opensles->stream) if (!opensles->stream)
@ -198,10 +197,9 @@ static void rdpsnd_opensles_open(rdpsndDevicePlugin* device,
static void rdpsnd_opensles_close(rdpsndDevicePlugin* device) static void rdpsnd_opensles_close(rdpsndDevicePlugin* device)
{ {
rdpsndopenslesPlugin* opensles = (rdpsndopenslesPlugin*) device; rdpsndopenslesPlugin* opensles = (rdpsndopenslesPlugin*) device;
rdpsnd_opensles_check_handle(opensles);
DEBUG_SND("opensles=%p", opensles); DEBUG_SND("opensles=%p", opensles);
if (!opensles->stream) if( !rdpsnd_opensles_check_handle(opensles))
return; return;
android_CloseAudioDevice(opensles->stream); android_CloseAudioDevice(opensles->stream);
@ -302,7 +300,6 @@ static void rdpsnd_opensles_set_volume(rdpsndDevicePlugin* device,
opensles->volume = value; opensles->volume = value;
return;
if (opensles->stream) if (opensles->stream)
{ {
if (0 == opensles->volume) if (0 == opensles->volume)
@ -364,6 +361,7 @@ static void rdpsnd_opensles_play(rdpsndDevicePlugin* device,
assert(0 == size % 2); assert(0 == size % 2);
assert(size > 0); assert(size > 0);
assert(src.b); assert(src.b);
ret = android_AudioOut(opensles->stream, src.s, size / 2); ret = android_AudioOut(opensles->stream, src.s, size / 2);
if (ret < 0) if (ret < 0)
DEBUG_WARN("android_AudioOut failed (%d)", ret); DEBUG_WARN("android_AudioOut failed (%d)", ret);