From e48e5737f30788eaef93c24f40e385b69e895c5f Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Mon, 6 Mar 2023 18:48:02 -0800 Subject: [PATCH] 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 --- audio/aidl/default/Android.bp | 1 + audio/aidl/default/StreamStub.cpp | 29 ++++++++++++++----- audio/aidl/default/include/core-impl/Module.h | 2 +- .../default/include/core-impl/StreamStub.h | 2 ++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/audio/aidl/default/Android.bp b/audio/aidl/default/Android.bp index ea00dcb103..8bacbb3d3a 100644 --- a/audio/aidl/default/Android.bp +++ b/audio/aidl/default/Android.bp @@ -13,6 +13,7 @@ cc_defaults { shared_libs: [ "libalsautilsv2", "libaudioaidlcommon", + "libaudioutils", "libbase", "libbinder_ndk", "libcutils", diff --git a/audio/aidl/default/StreamStub.cpp b/audio/aidl/default/StreamStub.cpp index 0ed935785a..2467320f0c 100644 --- a/audio/aidl/default/StreamStub.cpp +++ b/audio/aidl/default/StreamStub.cpp @@ -14,8 +14,11 @@ * limitations under the License. */ +#include + #define LOG_TAG "AHAL_Stream" #include +#include #include "core-impl/Module.h" #include "core-impl/StreamStub.h" @@ -29,31 +32,42 @@ using aidl::android::media::audio::common::MicrophoneInfo; 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( + std::roundf(kScaleFactor * frameCount * kMicrosPerSecond / mSampleRate)); + usleep(delayUs); + } if (mIsInput) { uint8_t* byteBuffer = static_cast(buffer); for (size_t i = 0; i < frameCount * mFrameSizeBytes; ++i) { @@ -66,12 +80,13 @@ DriverStub::DriverStub(const StreamContext& context, bool isInput) } ::android::status_t DriverStub::standby() { - usleep(1000); + usleep(500); return ::android::OK; } ::android::status_t DriverStub::setConnectedDevices( const std::vector& connectedDevices __unused) { + usleep(500); return ::android::OK; } diff --git a/audio/aidl/default/include/core-impl/Module.h b/audio/aidl/default/include/core-impl/Module.h index c35025f9a5..0544646a2e 100644 --- a/audio/aidl/default/include/core-impl/Module.h +++ b/audio/aidl/default/include/core-impl/Module.h @@ -164,7 +164,7 @@ class Module : public BnModule { bool isMmapSupported(); // This value is used for all AudioPatches. - static constexpr int32_t kMinimumStreamBufferSizeFrames = 16; + static constexpr int32_t kMinimumStreamBufferSizeFrames = 256; // The maximum stream buffer size is 1 GiB = 2 ** 30 bytes; static constexpr int32_t kMaximumStreamBufferSizeBytes = 1 << 30; diff --git a/audio/aidl/default/include/core-impl/StreamStub.h b/audio/aidl/default/include/core-impl/StreamStub.h index 69fd7b3983..df0182cf9f 100644 --- a/audio/aidl/default/include/core-impl/StreamStub.h +++ b/audio/aidl/default/include/core-impl/StreamStub.h @@ -36,6 +36,8 @@ class DriverStub : public DriverInterface { private: const size_t mFrameSizeBytes; + const int mSampleRate; + const bool mIsAsynchronous; const bool mIsInput; };