Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an array of callback buffer for OpenSL ES. #1245

Merged
merged 3 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 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 @@ -291,8 +293,14 @@ Result AudioStreamOpenSLES::close_l() {
return Result::OK;
}

uint8_t* AudioStreamOpenSLES::getBufferToProcess(int* bufferIdx) {
uint8_t* bufferToProcess = mCallbackBuffer[*bufferIdx].get();
*bufferIdx = (*bufferIdx + 1) % kBufferQueueLength;
return bufferToProcess;
}

SLresult AudioStreamOpenSLES::enqueueCallbackBuffer(SLAndroidSimpleBufferQueueItf bq) {
return (*bq)->Enqueue(bq, mCallbackBuffer.get(), mBytesPerCallback);
return (*bq)->Enqueue(bq, getBufferToProcess(&mCallbackBufferIdxOpenSLES), mBytesPerCallback);
}

int32_t AudioStreamOpenSLES::getBufferDepth(SLAndroidSimpleBufferQueueItf bq) {
Expand All @@ -304,7 +312,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(getBufferToProcess(&mCallbackBufferIdxApp), mFramesPerCallback);
if (result == DataCallbackResult::Continue) {
// Pass the buffer to OpenSLES.
SLresult enqueueResult = enqueueCallbackBuffer(bq);
Expand Down
6 changes: 5 additions & 1 deletion src/opensles/AudioStreamOpenSLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ class AudioStreamOpenSLES : public AudioStreamBuffered {
MonotonicCounter mPositionMillis; // for tracking OpenSL ES service position

private:
std::unique_ptr<uint8_t[]> mCallbackBuffer;
uint8_t* getBufferToProcess(int* bufferIdx);

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

};
Expand Down