From 23d73664c4c929b4c4a3c7b3e040a01e69fffe82 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 May 2013 14:30:31 -0700 Subject: [PATCH] Play only the rendered/valid samples in SLES. --- android/native-audio-so.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/android/native-audio-so.cpp b/android/native-audio-so.cpp index 64a6b05b97..0fba729dc9 100644 --- a/android/native-audio-so.cpp +++ b/android/native-audio-so.cpp @@ -6,6 +6,7 @@ #include #include +#include // for native audio #include @@ -46,8 +47,16 @@ static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) { assert(bq == bqPlayerBufferQueue); assert(NULL == context); + int nextSamples = audioCallback(buffer[curBuffer], BUFFER_SIZE_IN_SAMPLES); + // We can't enqueue nothing, the callback will never be called again. + // Delay until we get some audio. + while (nextSamples == 0) { + usleep(40); + nextSamples = audioCallback(buffer[curBuffer], BUFFER_SIZE_IN_SAMPLES); + } + short *nextBuffer = buffer[curBuffer]; - int nextSize = sizeof(buffer[0]); + int nextSize = nextSamples * 2 * sizeof(short); SLresult result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, nextBuffer, nextSize); @@ -57,8 +66,6 @@ static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) { assert(SL_RESULT_SUCCESS == result); curBuffer ^= 1; // Switch buffer - // Render to the fresh buffer - audioCallback(buffer[curBuffer], BUFFER_SIZE_IN_SAMPLES); } // create the engine and output mix objects