Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 85bc59b9 authored by Andy Hung's avatar Andy Hung Committed by Android (Google) Code Review
Browse files

Merge "Spatial Audio: Fix display orientation, add fold state" into tm-qpr-dev

parents edf1ca06 7443f381
Loading
Loading
Loading
Loading
+12 −5
Original line number Diff line number Diff line
@@ -1352,8 +1352,8 @@ public class AudioService extends IAudioService.Stub
        intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        if (mMonitorRotation) {
            RotationHelper.init(mContext, mAudioHandler,
                    rotationParam -> onRotationUpdate(rotationParam),
                    foldParam -> onFoldUpdate(foldParam));
                    rotation -> onRotationUpdate(rotation),
                    foldState -> onFoldStateUpdate(foldState));
        }
        intentFilter.addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
@@ -1502,16 +1502,20 @@ public class AudioService extends IAudioService.Stub
    //-----------------------------------------------------------------
    // 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
        final String rotationParameter = "rotation=" + rotation;
        sendMsg(mAudioHandler, MSG_ROTATION_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 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
        final String foldStateParameter = "device_folded=" + (foldState ? "on" : "off");
        sendMsg(mAudioHandler, MSG_FOLD_UPDATE, SENDMSG_REPLACE, /*arg1*/ 0, /*arg2*/ 0,
                /*obj*/ foldParameter, /*delay*/ 0);
                /*obj*/ foldStateParameter, /*delay*/ 0);
    }
    //-----------------------------------------------------------------
@@ -1726,6 +1730,9 @@ public class AudioService extends IAudioService.Stub
        mSpatializerHelper.reset(/* featureEnabled */ mHasSpatializerEffect);
        // Restore rotation information.
        RotationHelper.forceUpdate();
        onIndicateSystemReady();
        // indicate the end of reconfiguration phase to audio HAL
        AudioSystem.setParameters("restarting=false");
+38 −29
Original line number Diff line number Diff line
@@ -55,14 +55,14 @@ class RotationHelper {
    private static AudioDisplayListener sDisplayListener;
    private static FoldStateListener sFoldStateListener;
    /** 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 */
    private static Consumer<String> sFoldUpdateCb;
    private static Consumer<Boolean> sFoldStateCallback;

    private static final Object sRotationLock = new Object();
    private static final Object sFoldStateLock = new Object();
    private static int sDeviceRotation = Surface.ROTATION_0; // R/W synchronized on sRotationLock
    private static boolean sDeviceFold = true; // R/W synchronized on sFoldStateLock
    private static Integer sRotation = null; // R/W synchronized on sRotationLock
    private static Boolean sFoldState = null; // R/W synchronized on sFoldStateLock

    private static Context sContext;
    private static Handler sHandler;
@@ -73,15 +73,15 @@ class RotationHelper {
     * - sContext != null
     */
    static void init(Context context, Handler handler,
            Consumer<String> rotationUpdateCb, Consumer<String> foldUpdateCb) {
            Consumer<Integer> rotationCallback, Consumer<Boolean> foldStateCallback) {
        if (context == null) {
            throw new IllegalArgumentException("Invalid null context");
        }
        sContext = context;
        sHandler = handler;
        sDisplayListener = new AudioDisplayListener();
        sRotationUpdateCb = rotationUpdateCb;
        sFoldUpdateCb = foldUpdateCb;
        sRotationCallback = rotationCallback;
        sFoldStateCallback = foldStateCallback;
        enable();
    }

@@ -112,9 +112,9 @@ class RotationHelper {
        int newRotation = DisplayManagerGlobal.getInstance()
                .getDisplayInfo(Display.DEFAULT_DISPLAY).rotation;
        synchronized(sRotationLock) {
            if (newRotation != sDeviceRotation) {
                sDeviceRotation = newRotation;
                publishRotation(sDeviceRotation);
            if (sRotation == null || sRotation != newRotation) {
                sRotation = newRotation;
                publishRotation(sRotation);
            }
        }
    }
@@ -123,43 +123,52 @@ class RotationHelper {
        if (DEBUG_ROTATION) {
            Log.i(TAG, "publishing device rotation =" + rotation + " (x90deg)");
        }
        String rotationParam;
        int rotationDegrees;
        switch (rotation) {
            case Surface.ROTATION_0:
                rotationParam = "rotation=0";
                rotationDegrees = 0;
                break;
            case Surface.ROTATION_90:
                rotationParam = "rotation=90";
                rotationDegrees = 90;
                break;
            case Surface.ROTATION_180:
                rotationParam = "rotation=180";
                rotationDegrees = 180;
                break;
            case Surface.ROTATION_270:
                rotationParam = "rotation=270";
                rotationDegrees = 270;
                break;
            default:
                Log.e(TAG, "Unknown device rotation");
                rotationParam = null;
                rotationDegrees = -1;
        }
        if (rotationParam != null) {
            sRotationUpdateCb.accept(rotationParam);
        if (rotationDegrees != -1) {
            sRotationCallback.accept(rotationDegrees);
        }
    }

    /**
     * publish the change of device folded state if any.
     */
    static void updateFoldState(boolean newFolded) {
    static void updateFoldState(boolean foldState) {
        synchronized (sFoldStateLock) {
            if (sDeviceFold != newFolded) {
                sDeviceFold = newFolded;
                String foldParam;
                if (newFolded) {
                    foldParam = "device_folded=on";
                } else {
                    foldParam = "device_folded=off";
                }
                sFoldUpdateCb.accept(foldParam);
            if (sFoldState == null || sFoldState != foldState) {
                sFoldState = foldState;
                sFoldStateCallback.accept(foldState);
            }
        }
    }

    /**
     *  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);
            }
        }
    }
+33 −7
Original line number Diff line number Diff line
@@ -1063,7 +1063,7 @@ public class SpatializerHelper {
        if (transform.length != 6) {
            throw new IllegalArgumentException("invalid array size" + transform.length);
        }
        if (!checkSpatForHeadTracking("setGlobalTransform")) {
        if (!checkSpatializerForHeadTracking("setGlobalTransform")) {
            return;
        }
        try {
@@ -1074,7 +1074,7 @@ public class SpatializerHelper {
    }

    synchronized void recenterHeadTracker() {
        if (!checkSpatForHeadTracking("recenterHeadTracker")) {
        if (!checkSpatializerForHeadTracking("recenterHeadTracker")) {
            return;
        }
        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) {
        if (!checkSpatForHeadTracking("setDesiredHeadTrackingMode")) {
        if (!checkSpatializerForHeadTracking("setDesiredHeadTrackingMode")) {
            return;
        }
        if (mode != Spatializer.HEAD_TRACKING_MODE_DISABLED) {
@@ -1178,7 +1200,7 @@ public class SpatializerHelper {
        return mHeadTrackerAvailable;
    }

    private boolean checkSpatForHeadTracking(String funcName) {
    private boolean checkSpatializer(String funcName) {
        switch (mState) {
            case STATE_UNINITIALIZED:
            case STATE_NOT_SUPPORTED:
@@ -1189,14 +1211,18 @@ public class SpatializerHelper {
            case STATE_ENABLED_AVAILABLE:
                if (mSpat == null) {
                    // try to recover by resetting the native spatializer state
                    Log.e(TAG, "checkSpatForHeadTracking(): "
                            + "native spatializer should not be null in state: " + mState);
                    Log.e(TAG, "checkSpatializer(): called from " + funcName
                            + "(), native spatializer should not be null in state: " + mState);
                    postReset();
                    return false;
                }
                break;
        }
        return mIsHeadTrackingSupported;
        return true;
    }

    private boolean checkSpatializerForHeadTracking(String funcName) {
        return checkSpatializer(funcName) && mIsHeadTrackingSupported;
    }

    private void dispatchActualHeadTrackingMode(int newMode) {