Skip to content

Commit

Permalink
opensles: implement capacity using buffer queue length
Browse files Browse the repository at this point in the history
The OpenSL ES buffer queue length controls the allocated size of
the internal buffer. So we can use that to make sure there is enough
capacity to protect against glitches when setting a callback size
or a high capacity.

Fixes #744
It might help with #952
  • Loading branch information
philburk committed Aug 6, 2022
1 parent 7a666b2 commit 0b28c1a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/opensles/AudioInputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ Result AudioInputStreamOpenSLES::open() {
SLuint32 bitsPerSample = static_cast<SLuint32>(getBytesPerSample() * kBitsPerByte);

// configure audio sink
mBufferQueueLength = calculateOptimalBufferQueueLength();
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // locatorType
static_cast<SLuint32>(kBufferQueueLength)}; // numBuffers
static_cast<SLuint32>(mBufferQueueLength)}; // numBuffers

// Define the audio data format.
SLDataFormat_PCM format_pcm = {
Expand Down
3 changes: 2 additions & 1 deletion src/opensles/AudioOutputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ Result AudioOutputStreamOpenSLES::open() {
SLuint32 bitsPerSample = static_cast<SLuint32>(getBytesPerSample() * kBitsPerByte);

// configure audio source
mBufferQueueLength = calculateOptimalBufferQueueLength();
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // locatorType
static_cast<SLuint32>(kBufferQueueLength)}; // numBuffers
static_cast<SLuint32>(mBufferQueueLength)}; // numBuffers

// Define the audio data format.
SLDataFormat_PCM format_pcm = {
Expand Down
83 changes: 54 additions & 29 deletions src/opensles/AudioStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,35 +123,60 @@ SLresult AudioStreamOpenSLES::finishCommonOpen(SLAndroidConfigurationItf configI
return SL_RESULT_SUCCESS;
}

Result AudioStreamOpenSLES::configureBufferSizes(int32_t sampleRate) {
LOGD("AudioStreamOpenSLES:%s(%d) initial mFramesPerBurst = %d, mFramesPerCallback = %d",
__func__, sampleRate, mFramesPerBurst, mFramesPerCallback);
// Decide frames per burst based on hints from caller.
if (mFramesPerCallback != kUnspecified) {
// Requested framesPerCallback must be honored.
mFramesPerBurst = mFramesPerCallback;
} else {
mFramesPerBurst = DefaultStreamValues::FramesPerBurst;

// Calculate the size of a fixed duration high latency buffer based on sample rate.
int32_t framesPerHighLatencyBuffer =
(kHighLatencyBufferSizeMillis * sampleRate) / kMillisPerSecond;
int32_t AudioStreamOpenSLES::calculateOptimalBufferQueueLength() {
int32_t queueLength = kBufferQueueLengthDefault;
int32_t likelyFramesPerBurst = estimateNativeFramesPerBurst();
// What is required based on capacity?
if (mBufferCapacityInFrames > 0) {
int32_t queueLengthFromCapacity = (mBufferCapacityInFrames + likelyFramesPerBurst - 1)
/ likelyFramesPerBurst;
queueLength = std::max(queueLength, queueLengthFromCapacity);
}
// What is required based on requested callback size?
if (mFramesPerCallback > 0) {
int32_t minCapacity = 2 * mFramesPerCallback;
int32_t queueLengthFromCallbackSize = (minCapacity + likelyFramesPerBurst - 1)
/ likelyFramesPerBurst;
queueLength = std::max(queueLength, queueLengthFromCallbackSize);
}
queueLength = std::min(queueLength, kBufferQueueLengthMax);
return queueLength;
}

// For high latency streams, use a larger buffer size.
// Performance Mode support was added in N_MR1 (7.1)
if (getSdkVersion() >= __ANDROID_API_N_MR1__
/**
* The best information we have is if DefaultStreamValues::FramesPerBurst
* was set by the app based on AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER.
* Without that we just have to guess.
* @return
*/
int32_t AudioStreamOpenSLES::estimateNativeFramesPerBurst() {
int32_t framesPerBurst = DefaultStreamValues::FramesPerBurst;
framesPerBurst = std::max(framesPerBurst, 16);
// Calculate the size of a fixed duration high latency buffer based on sample rate.
int32_t sampleRate = (mSampleRate > 0) ? mSampleRate : 48000;
int32_t framesPerHighLatencyBuffer =
(kHighLatencyBufferSizeMillis * sampleRate) / kMillisPerSecond;
// For high latency streams, use a larger buffer size.
// Performance Mode support was added in N_MR1 (7.1)
if (getSdkVersion() >= __ANDROID_API_N_MR1__
&& mPerformanceMode != PerformanceMode::LowLatency
&& mFramesPerBurst < framesPerHighLatencyBuffer) {
// Find a multiple of framesPerBurst >= framesPerHighLatencyBuffer.
int32_t numBursts = (framesPerHighLatencyBuffer + mFramesPerBurst - 1) / mFramesPerBurst;
mFramesPerBurst *= numBursts;
LOGD("AudioStreamOpenSLES:%s() NOT low latency, set mFramesPerBurst = %d",
__func__, mFramesPerBurst);
}
mFramesPerCallback = mFramesPerBurst;
}
&& framesPerBurst < framesPerHighLatencyBuffer) {
// Find a multiple of framesPerBurst >= framesPerHighLatencyBuffer.
int32_t numBursts = (framesPerHighLatencyBuffer + framesPerBurst - 1) / framesPerBurst;
framesPerBurst *= numBursts;
LOGD("AudioStreamOpenSLES:%s() NOT low latency, numBursts = %d, mSampleRate = %d, set framesPerBurst = %d",
__func__, numBursts, mSampleRate, framesPerBurst);
}
return framesPerBurst;
}

Result AudioStreamOpenSLES::configureBufferSizes(int32_t sampleRate) {
LOGD("AudioStreamOpenSLES:%s(%d) initial mFramesPerBurst = %d, mFramesPerCallback = %d",
__func__, mSampleRate, mFramesPerBurst, mFramesPerCallback);
mFramesPerBurst = estimateNativeFramesPerBurst();
mFramesPerCallback = (mFramesPerCallback > 0) ? mFramesPerCallback : mFramesPerBurst;
LOGD("AudioStreamOpenSLES:%s(%d) final mFramesPerBurst = %d, mFramesPerCallback = %d",
__func__, sampleRate, mFramesPerBurst, mFramesPerCallback);
__func__, mSampleRate, mFramesPerBurst, mFramesPerCallback);

mBytesPerCallback = mFramesPerCallback * getBytesPerFrame();
if (mBytesPerCallback <= 0) {
Expand All @@ -160,12 +185,12 @@ Result AudioStreamOpenSLES::configureBufferSizes(int32_t sampleRate) {
return Result::ErrorInvalidFormat; // causing bytesPerFrame == 0
}

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

if (!usingFIFO()) {
mBufferCapacityInFrames = mFramesPerBurst * kBufferQueueLength;
mBufferCapacityInFrames = mFramesPerBurst * mBufferQueueLength;
// Check for overflow.
if (mBufferCapacityInFrames <= 0) {
mBufferCapacityInFrames = 0;
Expand Down Expand Up @@ -326,7 +351,7 @@ Result AudioStreamOpenSLES::close_l() {
SLresult AudioStreamOpenSLES::enqueueCallbackBuffer(SLAndroidSimpleBufferQueueItf bq) {
SLresult result = (*bq)->Enqueue(
bq, mCallbackBuffer[mCallbackBufferIndex].get(), mBytesPerCallback);
mCallbackBufferIndex = (mCallbackBufferIndex + 1) % kBufferQueueLength;
mCallbackBufferIndex = (mCallbackBufferIndex + 1) % kBufferQueueLengthMax;
return result;
}

Expand Down
9 changes: 7 additions & 2 deletions src/opensles/AudioStreamOpenSLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
namespace oboe {

constexpr int kBitsPerByte = 8;
constexpr int kBufferQueueLength = 2; // double buffered for callbacks
constexpr int kBufferQueueLengthDefault = 2; // double buffered for callbacks
constexpr int kBufferQueueLengthMax = 8; // AudioFlinger won't use more than 8

/**
* INTERNAL USE ONLY
Expand Down Expand Up @@ -98,6 +99,9 @@ class AudioStreamOpenSLES : public AudioStreamBuffered {

int32_t getBufferDepth(SLAndroidSimpleBufferQueueItf bq);

int32_t calculateOptimalBufferQueueLength();
int32_t estimateNativeFramesPerBurst();

SLresult enqueueCallbackBuffer(SLAndroidSimpleBufferQueueItf bq);

SLresult configurePerformanceMode(SLAndroidConfigurationItf configItf);
Expand All @@ -120,6 +124,7 @@ class AudioStreamOpenSLES : public AudioStreamBuffered {
// OpenSLES stuff
SLObjectItf mObjectInterface = nullptr;
SLAndroidSimpleBufferQueueItf mSimpleBufferQueueInterface = nullptr;
int mBufferQueueLength = 0;

int32_t mBytesPerCallback = oboe::kUnspecified;
MonotonicCounter mPositionMillis; // for tracking OpenSL ES service position
Expand All @@ -129,7 +134,7 @@ class AudioStreamOpenSLES : public AudioStreamBuffered {
SLresult updateStreamParameters(SLAndroidConfigurationItf configItf);
Result configureBufferSizes(int32_t sampleRate);

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

Expand Down

0 comments on commit 0b28c1a

Please sign in to comment.