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/Android.bp b/audio/aidl/default/Android.bp
index ea00dcb..8bacbb3 100644
--- a/audio/aidl/default/Android.bp
+++ b/audio/aidl/default/Android.bp
@@ -13,6 +13,7 @@
     shared_libs: [
         "libalsautilsv2",
         "libaudioaidlcommon",
+        "libaudioutils",
         "libbase",
         "libbinder_ndk",
         "libcutils",
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;
 }
 
diff --git a/audio/aidl/default/include/core-impl/Module.h b/audio/aidl/default/include/core-impl/Module.h
index c35025f..0544646 100644
--- a/audio/aidl/default/include/core-impl/Module.h
+++ b/audio/aidl/default/include/core-impl/Module.h
@@ -164,7 +164,7 @@
     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 69fd7b3..df0182c 100644
--- a/audio/aidl/default/include/core-impl/StreamStub.h
+++ b/audio/aidl/default/include/core-impl/StreamStub.h
@@ -36,6 +36,8 @@
 
   private:
     const size_t mFrameSizeBytes;
+    const int mSampleRate;
+    const bool mIsAsynchronous;
     const bool mIsInput;
 };