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

opensles: implement capacity using buffer queue length #1601

Merged
merged 4 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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
84 changes: 55 additions & 29 deletions src/opensles/AudioStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,35 +123,61 @@ 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)
philburk marked this conversation as resolved.
Show resolved Hide resolved
/ likelyFramesPerBurst;
queueLength = std::max(queueLength, queueLengthFromCapacity);
}
// What is required based on requested callback size?
if (mFramesPerCallback > 0) {
int32_t minCapacity = 2 * mFramesPerCallback;
philburk marked this conversation as resolved.
Show resolved Hide resolved
int32_t queueLengthFromCallbackSize = (minCapacity + likelyFramesPerBurst - 1)
/ likelyFramesPerBurst;
queueLength = std::max(queueLength, queueLengthFromCallbackSize);
}
queueLength = std::min(queueLength, kBufferQueueLengthMax);
// TODO Investigate the effect of queueLength on latency for normal streams. (not low latency)
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;
philburk marked this conversation as resolved.
Show resolved Hide resolved
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;
philburk marked this conversation as resolved.
Show resolved Hide resolved
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 +186,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 +352,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;
philburk marked this conversation as resolved.
Show resolved Hide resolved
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