audio: Fix glitching of remote submix

Align behavior with the HIDL implementation. This helps
to prevent glitches in the scenarios that use remote
submix for sending audio externally: Auto and Cast.

Bug: 322247032
Bug: 327220024
Test: Repro steps in b/327220024#comment14
Change-Id: If2e4ebd7145375a268ee5f0e4ab656bb3748b2fe
Merged-In: If2e4ebd7145375a268ee5f0e4ab656bb3748b2fe
(cherry picked from commit 1735123db2554d7debf581ad073cacca0df8d92e)
diff --git a/audio/aidl/default/include/core-impl/Module.h b/audio/aidl/default/include/core-impl/Module.h
index 8abf9ef..d6e8905 100644
--- a/audio/aidl/default/include/core-impl/Module.h
+++ b/audio/aidl/default/include/core-impl/Module.h
@@ -212,7 +212,12 @@
                 aidl::android::hardware::audio::common::frameCountFromDurationMs(latencyMs,
                                                                                  sampleRateHz);
         // Round up to nearest 16 frames since in the framework this is the size of a mixer burst.
-        return (rawSizeFrames + 15) & ~15;
+        const int32_t multipleOf16 = (rawSizeFrames + 15) & ~15;
+        if (multipleOf16 <= 512) return multipleOf16;
+        // Larger buffers should use powers of 2.
+        int32_t powerOf2 = 1;
+        while (powerOf2 < multipleOf16) powerOf2 <<= 1;
+        return powerOf2;
     }
 
     ndk::ScopedAStatus bluetoothParametersUpdated();
diff --git a/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp b/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
index fa4135d..2376ed9 100644
--- a/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
+++ b/audio/aidl/default/r_submix/StreamRemoteSubmix.cpp
@@ -136,10 +136,12 @@
     mCurrentRoute->exitStandby(mIsInput);
     RETURN_STATUS_IF_ERROR(mIsInput ? inRead(buffer, frameCount, actualFrameCount)
                                     : outWrite(buffer, frameCount, actualFrameCount));
+    mFramesSinceStart += *actualFrameCount;
+    if (!mIsInput) return ::android::OK;
+    // Only input streams need to block, for output this is implemented by MonoPipe.
     const long bufferDurationUs =
             (*actualFrameCount) * MICROS_PER_SECOND / mContext.getSampleRate();
     const auto totalDurationUs = (::android::uptimeNanos() - mStartTimeNs) / NANOS_PER_MICROSECOND;
-    mFramesSinceStart += *actualFrameCount;
     const long totalOffsetUs =
             mFramesSinceStart * MICROS_PER_SECOND / mContext.getSampleRate() - totalDurationUs;
     LOG(VERBOSE) << __func__ << ": totalOffsetUs " << totalOffsetUs;
@@ -275,8 +277,9 @@
     char* buff = (char*)buffer;
     size_t actuallyRead = 0;
     long remainingFrames = frameCount;
-    const int64_t deadlineTimeNs = ::android::uptimeNanos() +
-                                   getDelayInUsForFrameCount(frameCount) * NANOS_PER_MICROSECOND;
+    const int64_t deadlineTimeNs =
+            ::android::uptimeNanos() +
+            getDelayInUsForFrameCount(frameCount) * NANOS_PER_MICROSECOND / 2;
     while (remainingFrames > 0) {
         ssize_t framesRead = source->read(buff, remainingFrames);
         LOG(VERBOSE) << __func__ << ": frames read " << framesRead;