Skip to content
Permalink
Browse files

Android audio: Turns out the initial queue operation queued up too li…

…ttle data anyway (sizeof on dynamic buffer.. sigh).

Anyway like I said, none of this really seems to matter.
  • Loading branch information...
hrydgard committed Sep 17, 2019
1 parent e69b71b commit f9bf1f96bd88315f0034fe0dbb86d2f3e9f068ba
Showing with 28 additions and 21 deletions.
  1. +21 −19 android/jni/native-audio-so.cpp
  2. +7 −2 android/jni/native-audio-so.h
@@ -56,7 +56,9 @@ void OpenSLContext::BqPlayerCallback(SLAndroidSimpleBufferQueueItf bq) {
ELOG("OpenSL ES: Failed to enqueue! %i %i", renderedFrames, sizeInBytes);
}

curBuffer ^= 1; // Switch buffer
curBuffer += 1; // Switch buffer
if (curBuffer == NUM_BUFFERS)
curBuffer = 0;
}

// create the engine and output mix objects
@@ -83,7 +85,7 @@ bool OpenSLContext::Init() {
// The constants, such as SL_SAMPLINGRATE_44_1, are just 44100000.
SLuint32 sr = (SLuint32)sampleRate * 1000;

SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, NUM_BUFFERS};
SLDataFormat_PCM format_pcm = {
SL_DATAFORMAT_PCM,
2,
@@ -124,21 +126,20 @@ bool OpenSLContext::Init() {
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
assert(SL_RESULT_SUCCESS == result);

// Enqueue two empty buffers.
buffer[0] = new short[framesPerBuffer * 2]{};
buffer[1] = new short[framesPerBuffer * 2]{};

curBuffer = 0;
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, buffer[curBuffer], sizeof(buffer[curBuffer]));
if (SL_RESULT_SUCCESS != result) {
return false;
// Allocate and enqueue N empty buffers.
for (int i = 0; i < NUM_BUFFERS; i++) {
buffer[i] = new short[framesPerBuffer * 2]{};
}
curBuffer ^= 1;
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, buffer[curBuffer], sizeof(buffer[curBuffer]));
if (SL_RESULT_SUCCESS != result) {
return false;

int sizeInBytes = framesPerBuffer * 2 * sizeof(short);
for (int i = 0; i < NUM_BUFFERS; i++) {
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, buffer[i], sizeInBytes);
if (SL_RESULT_SUCCESS != result) {
return false;
}
}
curBuffer ^= 1;

curBuffer = 0;
return true;
}

@@ -177,10 +178,11 @@ OpenSLContext::~OpenSLContext() {
engineObject = nullptr;
engineEngine = nullptr;
}
delete [] buffer[0];
delete [] buffer[1];
buffer[0] = nullptr;
buffer[1] = nullptr;

for (int i = 0; i < NUM_BUFFERS; i++) {
delete[] buffer[i];
buffer[i] = nullptr;
}
ILOG("OpenSLWrap_Shutdown - finished");
}

@@ -37,10 +37,15 @@ class OpenSLContext : public AudioContext {
SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue = nullptr;
SLVolumeItf bqPlayerVolume = nullptr;

// Should be no reason to need more than two buffers, but make it clear in the code.
enum {
NUM_BUFFERS = 2,
};

// Double buffering.
short *buffer[2]{};
short *buffer[NUM_BUFFERS]{};
int curBuffer = 0;

static void bqPlayerCallbackWrap(SLAndroidSimpleBufferQueueItf bq, void *context);
void BqPlayerCallback(SLAndroidSimpleBufferQueueItf bq);
};
};

0 comments on commit f9bf1f9

Please sign in to comment.
You can’t perform that action at this time.