aaudio: clip bufferSize at top of MMAP code
Set size to a reasonable value regardless of the user input.
Bug: 69469801
Test: test_various.cpp
Change-Id: I6c155383e70959485882c17189388be1c9e6fc8b
diff --git a/media/libaaudio/src/client/AudioStreamInternal.cpp b/media/libaaudio/src/client/AudioStreamInternal.cpp
index 1944d5b..3a7a342 100644
--- a/media/libaaudio/src/client/AudioStreamInternal.cpp
+++ b/media/libaaudio/src/client/AudioStreamInternal.cpp
@@ -61,7 +61,6 @@
, mClockModel()
, mAudioEndpoint()
, mServiceStreamHandle(AAUDIO_HANDLE_INVALID)
- , mFramesPerBurst(16)
, mInService(inService)
, mServiceInterface(serviceInterface)
, mAtomicTimestamp()
@@ -79,6 +78,7 @@
aaudio_result_t result = AAUDIO_OK;
int32_t capacity;
+ int32_t framesPerBurst;
AAudioStreamRequest request;
AAudioStreamConfiguration configurationOutput;
@@ -151,16 +151,18 @@
goto error;
}
- mFramesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
- capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
// Validate result from server.
- if (mFramesPerBurst < 16 || mFramesPerBurst > 16 * 1024) {
- ALOGE("%s - framesPerBurst out of range = %d", __func__, mFramesPerBurst);
+ framesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
+ if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) {
+ ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst);
result = AAUDIO_ERROR_OUT_OF_RANGE;
goto error;
}
- if (capacity < mFramesPerBurst || capacity > 32 * 1024) {
+ mFramesPerBurst = framesPerBurst; // only save good value
+
+ capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
+ if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) {
ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity);
result = AAUDIO_ERROR_OUT_OF_RANGE;
goto error;
@@ -649,14 +651,29 @@
}
aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
+ int32_t adjustedFrames = requestedFrames;
int32_t actualFrames = 0;
- // Round to the next highest burst size.
- if (getFramesPerBurst() > 0) {
- int32_t numBursts = (requestedFrames + getFramesPerBurst() - 1) / getFramesPerBurst();
- requestedFrames = numBursts * getFramesPerBurst();
+ int32_t maximumSize = getBufferCapacity();
+
+ // Clip to minimum size so that rounding up will work better.
+ if (adjustedFrames < 1) {
+ adjustedFrames = 1;
}
- aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(requestedFrames, &actualFrames);
+ if (adjustedFrames > maximumSize) {
+ // Clip to maximum size.
+ adjustedFrames = maximumSize;
+ } else {
+ // Round to the next highest burst size.
+ int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst;
+ adjustedFrames = numBursts * mFramesPerBurst;
+ // Rounding may have gone above maximum.
+ if (adjustedFrames > maximumSize) {
+ adjustedFrames = maximumSize;
+ }
+ }
+
+ aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(adjustedFrames, &actualFrames);
ALOGD("setBufferSize() req = %d => %d", requestedFrames, actualFrames);
if (result < 0) {
return result;
@@ -674,7 +691,7 @@
}
int32_t AudioStreamInternal::getFramesPerBurst() const {
- return mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
+ return mFramesPerBurst;
}
aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) {