audio: Make I/O operations in default stub more realistic
1. Increase the minimum buffer size to 256 frames. This is
more realistic than 16 frames: 256 frames is ~5ms @ 48 kHz.
2. Make transfer delay in the stub module dependent on the frame
count for synchronous transfers.
Bug: 270154517
Test: atest VtsHalAudioCoreTargetTest
Test: atest android.media.audio.cts.LoudnessEnhancerTest (w/AIDL enabled)
Change-Id: If968e30d145b52220f4dc3c33af48dbc163c78cd
diff --git a/audio/aidl/default/StreamStub.cpp b/audio/aidl/default/StreamStub.cpp
index 0ed9357..2467320 100644
--- a/audio/aidl/default/StreamStub.cpp
+++ b/audio/aidl/default/StreamStub.cpp
@@ -14,8 +14,11 @@
* limitations under the License.
*/
+#include <cmath>
+
#define LOG_TAG "AHAL_Stream"
#include <android-base/logging.h>
+#include <audio_utils/clock.h>
#include "core-impl/Module.h"
#include "core-impl/StreamStub.h"
@@ -29,31 +32,42 @@
namespace aidl::android::hardware::audio::core {
DriverStub::DriverStub(const StreamContext& context, bool isInput)
- : mFrameSizeBytes(context.getFrameSize()), mIsInput(isInput) {}
+ : mFrameSizeBytes(context.getFrameSize()),
+ mSampleRate(context.getSampleRate()),
+ mIsAsynchronous(!!context.getAsyncCallback()),
+ mIsInput(isInput) {}
::android::status_t DriverStub::init() {
- usleep(1000);
+ usleep(500);
return ::android::OK;
}
::android::status_t DriverStub::drain(StreamDescriptor::DrainMode) {
- usleep(1000);
+ usleep(500);
return ::android::OK;
}
::android::status_t DriverStub::flush() {
- usleep(1000);
+ usleep(500);
return ::android::OK;
}
::android::status_t DriverStub::pause() {
- usleep(1000);
+ usleep(500);
return ::android::OK;
}
::android::status_t DriverStub::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
int32_t* latencyMs) {
- usleep(3000);
+ static constexpr float kMicrosPerSecond = MICROS_PER_SECOND;
+ static constexpr float kScaleFactor = .8f;
+ if (mIsAsynchronous) {
+ usleep(500);
+ } else {
+ const size_t delayUs = static_cast<size_t>(
+ std::roundf(kScaleFactor * frameCount * kMicrosPerSecond / mSampleRate));
+ usleep(delayUs);
+ }
if (mIsInput) {
uint8_t* byteBuffer = static_cast<uint8_t*>(buffer);
for (size_t i = 0; i < frameCount * mFrameSizeBytes; ++i) {
@@ -66,12 +80,13 @@
}
::android::status_t DriverStub::standby() {
- usleep(1000);
+ usleep(500);
return ::android::OK;
}
::android::status_t DriverStub::setConnectedDevices(
const std::vector<AudioDevice>& connectedDevices __unused) {
+ usleep(500);
return ::android::OK;
}