Skip to content

Commit

Permalink
Use an array of callback buffer for OpenSL ES. (#1245)
Browse files Browse the repository at this point in the history
Use an array of callback buffer for OpenSL ES.
Prevent possible overwriting of data in buffer.

Fixes #1165
  • Loading branch information
flamme authored Aug 2, 2021
1 parent ee267ab commit 7933a7e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/opensles/AudioStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ Result AudioStreamOpenSLES::configureBufferSizes(int32_t sampleRate) {
return Result::ErrorInvalidFormat; // causing bytesPerFrame == 0
}

mCallbackBuffer = std::make_unique<uint8_t[]>(mBytesPerCallback);
for (int i = 0; i < kBufferQueueLength; ++i) {
mCallbackBuffer[i] = std::make_unique<uint8_t[]>(mBytesPerCallback);
}

if (!usingFIFO()) {
mBufferCapacityInFrames = mFramesPerBurst * kBufferQueueLength;
Expand Down Expand Up @@ -292,7 +294,10 @@ Result AudioStreamOpenSLES::close_l() {
}

SLresult AudioStreamOpenSLES::enqueueCallbackBuffer(SLAndroidSimpleBufferQueueItf bq) {
return (*bq)->Enqueue(bq, mCallbackBuffer.get(), mBytesPerCallback);
SLresult result = (*bq)->Enqueue(
bq, mCallbackBuffer[mCallbackBufferIndex].get(), mBytesPerCallback);
mCallbackBufferIndex = (mCallbackBufferIndex + 1) % kBufferQueueLength;
return result;
}

int32_t AudioStreamOpenSLES::getBufferDepth(SLAndroidSimpleBufferQueueItf bq) {
Expand All @@ -304,7 +309,8 @@ int32_t AudioStreamOpenSLES::getBufferDepth(SLAndroidSimpleBufferQueueItf bq) {
void AudioStreamOpenSLES::processBufferCallback(SLAndroidSimpleBufferQueueItf bq) {
bool stopStream = false;
// Ask the app callback to process the buffer.
DataCallbackResult result = fireDataCallback(mCallbackBuffer.get(), mFramesPerCallback);
DataCallbackResult result =
fireDataCallback(mCallbackBuffer[mCallbackBufferIndex].get(), mFramesPerCallback);
if (result == DataCallbackResult::Continue) {
// Pass the buffer to OpenSLES.
SLresult enqueueResult = enqueueCallbackBuffer(bq);
Expand All @@ -327,6 +333,7 @@ void AudioStreamOpenSLES::processBufferCallback(SLAndroidSimpleBufferQueueItf bq
}
if (stopStream) {
requestStop();
mCallbackBufferIndex = 0;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/opensles/AudioStreamOpenSLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ class AudioStreamOpenSLES : public AudioStreamBuffered {
MonotonicCounter mPositionMillis; // for tracking OpenSL ES service position

private:
std::unique_ptr<uint8_t[]> mCallbackBuffer;
std::unique_ptr<uint8_t[]> mCallbackBuffer[kBufferQueueLength];
int mCallbackBufferIndex = 0;
std::atomic<StreamState> mState{StreamState::Uninitialized};

};
Expand Down

0 comments on commit 7933a7e

Please sign in to comment.