From e42469d7df6c84897409d6154164063cb778e4f4 Mon Sep 17 00:00:00 2001 From: Jean-Michel Trivi Date: Mon, 27 Jun 2022 21:33:24 +0000 Subject: [PATCH] AudioService: async rotation/fold update Do not send the rotation and fold state update to AudioSystem on the notification thread. Bug: 235575035 Test: play channel check in YT, rotate screen, verify A/V match Change-Id: I39e9400ee15a1f7a88142fdfc4c1231be2be3c99 --- .../android/server/audio/AudioService.java | 30 +++++++++++++++++- .../android/server/audio/RotationHelper.java | 31 ++++++++++++++----- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java index 02648c4da76f6..2c05dd2a2f65b 100644 --- a/services/core/java/com/android/server/audio/AudioService.java +++ b/services/core/java/com/android/server/audio/AudioService.java @@ -346,6 +346,8 @@ public class AudioService extends IAudioService.Stub private static final int MSG_REMOVE_ASSISTANT_SERVICE_UID = 45; private static final int MSG_UPDATE_ACTIVE_ASSISTANT_SERVICE_UID = 46; private static final int MSG_DISPATCH_DEVICE_VOLUME_BEHAVIOR = 47; + private static final int MSG_ROTATION_UPDATE = 48; + private static final int MSG_FOLD_UPDATE = 49; // start of messages handled under wakelock // these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(), @@ -1214,7 +1216,9 @@ public class AudioService extends IAudioService.Stub intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); if (mMonitorRotation) { - RotationHelper.init(mContext, mAudioHandler); + RotationHelper.init(mContext, mAudioHandler, + rotationParam -> onRotationUpdate(rotationParam), + foldParam -> onFoldUpdate(foldParam)); } intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); @@ -1360,6 +1364,20 @@ public class AudioService extends IAudioService.Stub checkMuteAwaitConnection(); } + //----------------------------------------------------------------- + // rotation/fold updates coming from RotationHelper + void onRotationUpdate(String rotationParameter) { + // use REPLACE as only the last rotation matters + sendMsg(mAudioHandler, MSG_ROTATION_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0, + /*obj*/ rotationParameter, /*delay*/ 0); + } + + void onFoldUpdate(String foldParameter) { + // use REPLACE as only the last fold state matters + sendMsg(mAudioHandler, MSG_FOLD_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0, + /*obj*/ foldParameter, /*delay*/ 0); + } + //----------------------------------------------------------------- // monitoring requests for volume range initialization @Override // AudioSystemAdapter.OnVolRangeInitRequestListener @@ -8280,6 +8298,16 @@ public class AudioService extends IAudioService.Stub case MSG_DISPATCH_DEVICE_VOLUME_BEHAVIOR: dispatchDeviceVolumeBehavior((AudioDeviceAttributes) msg.obj, msg.arg1); break; + + case MSG_ROTATION_UPDATE: + // rotation parameter format: "rotation=x" where x is one of 0, 90, 180, 270 + mAudioSystem.setParameters((String) msg.obj); + break; + + case MSG_FOLD_UPDATE: + // fold parameter format: "device_folded=x" where x is one of on, off + mAudioSystem.setParameters((String) msg.obj); + break; } } } diff --git a/services/core/java/com/android/server/audio/RotationHelper.java b/services/core/java/com/android/server/audio/RotationHelper.java index eb8387fe85e58..5cdf58bdd62f2 100644 --- a/services/core/java/com/android/server/audio/RotationHelper.java +++ b/services/core/java/com/android/server/audio/RotationHelper.java @@ -21,13 +21,14 @@ import android.hardware.devicestate.DeviceStateManager; import android.hardware.devicestate.DeviceStateManager.FoldStateListener; import android.hardware.display.DisplayManager; import android.hardware.display.DisplayManagerGlobal; -import android.media.AudioSystem; import android.os.Handler; import android.os.HandlerExecutor; import android.util.Log; import android.view.Display; import android.view.Surface; +import java.util.function.Consumer; + /** * Class to handle device rotation events for AudioService, and forward device rotation * and folded state to the audio HALs through AudioSystem. @@ -53,6 +54,10 @@ class RotationHelper { private static AudioDisplayListener sDisplayListener; private static FoldStateListener sFoldStateListener; + /** callback to send rotation updates to AudioSystem */ + private static Consumer sRotationUpdateCb; + /** callback to send folded state updates to AudioSystem */ + private static Consumer sFoldUpdateCb; private static final Object sRotationLock = new Object(); private static final Object sFoldStateLock = new Object(); @@ -67,13 +72,16 @@ class RotationHelper { * - sDisplayListener != null * - sContext != null */ - static void init(Context context, Handler handler) { + static void init(Context context, Handler handler, + Consumer rotationUpdateCb, Consumer foldUpdateCb) { if (context == null) { throw new IllegalArgumentException("Invalid null context"); } sContext = context; sHandler = handler; sDisplayListener = new AudioDisplayListener(); + sRotationUpdateCb = rotationUpdateCb; + sFoldUpdateCb = foldUpdateCb; enable(); } @@ -115,21 +123,26 @@ class RotationHelper { if (DEBUG_ROTATION) { Log.i(TAG, "publishing device rotation =" + rotation + " (x90deg)"); } + String rotationParam; switch (rotation) { case Surface.ROTATION_0: - AudioSystem.setParameters("rotation=0"); + rotationParam = "rotation=0"; break; case Surface.ROTATION_90: - AudioSystem.setParameters("rotation=90"); + rotationParam = "rotation=90"; break; case Surface.ROTATION_180: - AudioSystem.setParameters("rotation=180"); + rotationParam = "rotation=180"; break; case Surface.ROTATION_270: - AudioSystem.setParameters("rotation=270"); + rotationParam = "rotation=270"; break; default: Log.e(TAG, "Unknown device rotation"); + rotationParam = null; + } + if (rotationParam != null) { + sRotationUpdateCb.accept(rotationParam); } } @@ -140,11 +153,13 @@ class RotationHelper { synchronized (sFoldStateLock) { if (sDeviceFold != newFolded) { sDeviceFold = newFolded; + String foldParam; if (newFolded) { - AudioSystem.setParameters("device_folded=on"); + foldParam = "device_folded=on"; } else { - AudioSystem.setParameters("device_folded=off"); + foldParam = "device_folded=off"; } + sFoldUpdateCb.accept(foldParam); } } }