Spatial Audio: Fix display orientation, add fold state am: 203e69c4c0

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2499035

Change-Id: I47986452e1baa338683dc9e0e8a143e39150230f
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Andy Hung
2023-03-21 17:54:32 +00:00
committed by Automerger Merge Worker
3 changed files with 83 additions and 41 deletions

View File

@@ -1313,8 +1313,8 @@ public class AudioService extends IAudioService.Stub
intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
if (mMonitorRotation) { if (mMonitorRotation) {
RotationHelper.init(mContext, mAudioHandler, RotationHelper.init(mContext, mAudioHandler,
rotationParam -> onRotationUpdate(rotationParam), rotation -> onRotationUpdate(rotation),
foldParam -> onFoldUpdate(foldParam)); foldState -> onFoldStateUpdate(foldState));
} }
intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION); intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
@@ -1463,16 +1463,20 @@ public class AudioService extends IAudioService.Stub
//----------------------------------------------------------------- //-----------------------------------------------------------------
// rotation/fold updates coming from RotationHelper // rotation/fold updates coming from RotationHelper
void onRotationUpdate(String rotationParameter) { void onRotationUpdate(Integer rotation) {
mSpatializerHelper.setDisplayOrientation((float) (rotation * Math.PI / 180.));
// use REPLACE as only the last rotation matters // use REPLACE as only the last rotation matters
final String rotationParameter = "rotation=" + rotation;
sendMsg(mAudioHandler, MSG_ROTATION_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0, sendMsg(mAudioHandler, MSG_ROTATION_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0,
/*obj*/ rotationParameter, /*delay*/ 0); /*obj*/ rotationParameter, /*delay*/ 0);
} }
void onFoldUpdate(String foldParameter) { void onFoldStateUpdate(Boolean foldState) {
mSpatializerHelper.setFoldState(foldState);
// use REPLACE as only the last fold state matters // use REPLACE as only the last fold state matters
final String foldStateParameter = "device_folded=" + (foldState ? "on" : "off");
sendMsg(mAudioHandler, MSG_FOLD_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0, sendMsg(mAudioHandler, MSG_FOLD_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0,
/*obj*/ foldParameter, /*delay*/ 0); /*obj*/ foldStateParameter, /*delay*/ 0);
} }
//----------------------------------------------------------------- //-----------------------------------------------------------------
@@ -1687,6 +1691,9 @@ public class AudioService extends IAudioService.Stub
mSpatializerHelper.reset(/* featureEnabled */ mHasSpatializerEffect); mSpatializerHelper.reset(/* featureEnabled */ mHasSpatializerEffect);
// Restore rotation information.
RotationHelper.forceUpdate();
onIndicateSystemReady(); onIndicateSystemReady();
// indicate the end of reconfiguration phase to audio HAL // indicate the end of reconfiguration phase to audio HAL
AudioSystem.setParameters("restarting=false"); AudioSystem.setParameters("restarting=false");

View File

@@ -55,14 +55,14 @@ class RotationHelper {
private static AudioDisplayListener sDisplayListener; private static AudioDisplayListener sDisplayListener;
private static FoldStateListener sFoldStateListener; private static FoldStateListener sFoldStateListener;
/** callback to send rotation updates to AudioSystem */ /** callback to send rotation updates to AudioSystem */
private static Consumer<String> sRotationUpdateCb; private static Consumer<Integer> sRotationCallback;
/** callback to send folded state updates to AudioSystem */ /** callback to send folded state updates to AudioSystem */
private static Consumer<String> sFoldUpdateCb; private static Consumer<Boolean> sFoldStateCallback;
private static final Object sRotationLock = new Object(); private static final Object sRotationLock = new Object();
private static final Object sFoldStateLock = new Object(); private static final Object sFoldStateLock = new Object();
private static int sDeviceRotation = Surface.ROTATION_0; // R/W synchronized on sRotationLock private static Integer sRotation = null; // R/W synchronized on sRotationLock
private static boolean sDeviceFold = true; // R/W synchronized on sFoldStateLock private static Boolean sFoldState = null; // R/W synchronized on sFoldStateLock
private static Context sContext; private static Context sContext;
private static Handler sHandler; private static Handler sHandler;
@@ -73,15 +73,15 @@ class RotationHelper {
* - sContext != null * - sContext != null
*/ */
static void init(Context context, Handler handler, static void init(Context context, Handler handler,
Consumer<String> rotationUpdateCb, Consumer<String> foldUpdateCb) { Consumer<Integer> rotationCallback, Consumer<Boolean> foldStateCallback) {
if (context == null) { if (context == null) {
throw new IllegalArgumentException("Invalid null context"); throw new IllegalArgumentException("Invalid null context");
} }
sContext = context; sContext = context;
sHandler = handler; sHandler = handler;
sDisplayListener = new AudioDisplayListener(); sDisplayListener = new AudioDisplayListener();
sRotationUpdateCb = rotationUpdateCb; sRotationCallback = rotationCallback;
sFoldUpdateCb = foldUpdateCb; sFoldStateCallback = foldStateCallback;
enable(); enable();
} }
@@ -112,9 +112,9 @@ class RotationHelper {
int newRotation = DisplayManagerGlobal.getInstance() int newRotation = DisplayManagerGlobal.getInstance()
.getDisplayInfo(Display.DEFAULT_DISPLAY).rotation; .getDisplayInfo(Display.DEFAULT_DISPLAY).rotation;
synchronized(sRotationLock) { synchronized(sRotationLock) {
if (newRotation != sDeviceRotation) { if (sRotation == null || sRotation != newRotation) {
sDeviceRotation = newRotation; sRotation = newRotation;
publishRotation(sDeviceRotation); publishRotation(sRotation);
} }
} }
} }
@@ -123,43 +123,52 @@ class RotationHelper {
if (DEBUG_ROTATION) { if (DEBUG_ROTATION) {
Log.i(TAG, "publishing device rotation =" + rotation + " (x90deg)"); Log.i(TAG, "publishing device rotation =" + rotation + " (x90deg)");
} }
String rotationParam; int rotationDegrees;
switch (rotation) { switch (rotation) {
case Surface.ROTATION_0: case Surface.ROTATION_0:
rotationParam = "rotation=0"; rotationDegrees = 0;
break; break;
case Surface.ROTATION_90: case Surface.ROTATION_90:
rotationParam = "rotation=90"; rotationDegrees = 90;
break; break;
case Surface.ROTATION_180: case Surface.ROTATION_180:
rotationParam = "rotation=180"; rotationDegrees = 180;
break; break;
case Surface.ROTATION_270: case Surface.ROTATION_270:
rotationParam = "rotation=270"; rotationDegrees = 270;
break; break;
default: default:
Log.e(TAG, "Unknown device rotation"); Log.e(TAG, "Unknown device rotation");
rotationParam = null; rotationDegrees = -1;
} }
if (rotationParam != null) { if (rotationDegrees != -1) {
sRotationUpdateCb.accept(rotationParam); sRotationCallback.accept(rotationDegrees);
} }
} }
/** /**
* publish the change of device folded state if any. * publish the change of device folded state if any.
*/ */
static void updateFoldState(boolean newFolded) { static void updateFoldState(boolean foldState) {
synchronized (sFoldStateLock) { synchronized (sFoldStateLock) {
if (sDeviceFold != newFolded) { if (sFoldState == null || sFoldState != foldState) {
sDeviceFold = newFolded; sFoldState = foldState;
String foldParam; sFoldStateCallback.accept(foldState);
if (newFolded) { }
foldParam = "device_folded=on"; }
} else { }
foldParam = "device_folded=off";
} /**
sFoldUpdateCb.accept(foldParam); * forceUpdate is called when audioserver restarts.
*/
static void forceUpdate() {
synchronized (sRotationLock) {
sRotation = null;
}
updateOrientation(); // We will get at least one orientation update now.
synchronized (sFoldStateLock) {
if (sFoldState != null) {
sFoldStateCallback.accept(sFoldState);
} }
} }
} }

View File

@@ -1063,7 +1063,7 @@ public class SpatializerHelper {
if (transform.length != 6) { if (transform.length != 6) {
throw new IllegalArgumentException("invalid array size" + transform.length); throw new IllegalArgumentException("invalid array size" + transform.length);
} }
if (!checkSpatForHeadTracking("setGlobalTransform")) { if (!checkSpatializerForHeadTracking("setGlobalTransform")) {
return; return;
} }
try { try {
@@ -1074,7 +1074,7 @@ public class SpatializerHelper {
} }
synchronized void recenterHeadTracker() { synchronized void recenterHeadTracker() {
if (!checkSpatForHeadTracking("recenterHeadTracker")) { if (!checkSpatializerForHeadTracking("recenterHeadTracker")) {
return; return;
} }
try { try {
@@ -1084,8 +1084,30 @@ public class SpatializerHelper {
} }
} }
synchronized void setDisplayOrientation(float displayOrientation) {
if (!checkSpatializer("setDisplayOrientation")) {
return;
}
try {
mSpat.setDisplayOrientation(displayOrientation);
} catch (RemoteException e) {
Log.e(TAG, "Error calling setDisplayOrientation", e);
}
}
synchronized void setFoldState(boolean folded) {
if (!checkSpatializer("setFoldState")) {
return;
}
try {
mSpat.setFoldState(folded);
} catch (RemoteException e) {
Log.e(TAG, "Error calling setFoldState", e);
}
}
synchronized void setDesiredHeadTrackingMode(@Spatializer.HeadTrackingModeSet int mode) { synchronized void setDesiredHeadTrackingMode(@Spatializer.HeadTrackingModeSet int mode) {
if (!checkSpatForHeadTracking("setDesiredHeadTrackingMode")) { if (!checkSpatializerForHeadTracking("setDesiredHeadTrackingMode")) {
return; return;
} }
if (mode != Spatializer.HEAD_TRACKING_MODE_DISABLED) { if (mode != Spatializer.HEAD_TRACKING_MODE_DISABLED) {
@@ -1178,7 +1200,7 @@ public class SpatializerHelper {
return mHeadTrackerAvailable; return mHeadTrackerAvailable;
} }
private boolean checkSpatForHeadTracking(String funcName) { private boolean checkSpatializer(String funcName) {
switch (mState) { switch (mState) {
case STATE_UNINITIALIZED: case STATE_UNINITIALIZED:
case STATE_NOT_SUPPORTED: case STATE_NOT_SUPPORTED:
@@ -1189,14 +1211,18 @@ public class SpatializerHelper {
case STATE_ENABLED_AVAILABLE: case STATE_ENABLED_AVAILABLE:
if (mSpat == null) { if (mSpat == null) {
// try to recover by resetting the native spatializer state // try to recover by resetting the native spatializer state
Log.e(TAG, "checkSpatForHeadTracking(): " Log.e(TAG, "checkSpatializer(): called from " + funcName
+ "native spatializer should not be null in state: " + mState); + "(), native spatializer should not be null in state: " + mState);
postReset(); postReset();
return false; return false;
} }
break; break;
} }
return mIsHeadTrackingSupported; return true;
}
private boolean checkSpatializerForHeadTracking(String funcName) {
return checkSpatializer(funcName) && mIsHeadTrackingSupported;
} }
private void dispatchActualHeadTrackingMode(int newMode) { private void dispatchActualHeadTrackingMode(int newMode) {