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

Commit 6172dc5b authored by Jean-Michel Trivi's avatar Jean-Michel Trivi
Browse files

Spatial audio: read prop for spatializer init, persist feature state

Use property ro.audio.spatializer_enabled to optimize the
AudioService initialization of Spatializer.

Persist the state of the spatial audo feature whenever its state
changes. Read it at start or whenever AudioService needs to
re-initialize the feature (after audio_server restart)

Bug: 188502620
Test: adb shell dumpsys audio | grep -A 4 Spatial
Change-Id: I667ff7f0247396aad698765455e400337d9c6dcb
parent 212fc8b3
Loading
Loading
Loading
Loading
+9 −0
Original line number Original line Diff line number Diff line
@@ -8965,6 +8965,15 @@ public final class Settings {
        @Readable
        @Readable
        public static final String UNSAFE_VOLUME_MUSIC_ACTIVE_MS = "unsafe_volume_music_active_ms";
        public static final String UNSAFE_VOLUME_MUSIC_ACTIVE_MS = "unsafe_volume_music_active_ms";
        /**
         * Indicates whether the spatial audio feature was enabled for this user.
         *
         * Type : int (0 disabled, 1 enabled)
         *
         * @hide
         */
        public static final String SPATIAL_AUDIO_ENABLED = "spatial_audio_enabled";
        /**
        /**
         * Indicates whether notification display on the lock screen is enabled.
         * Indicates whether notification display on the lock screen is enabled.
         * <p>
         * <p>
+60 −8
Original line number Original line Diff line number Diff line
@@ -319,6 +319,7 @@ public class AudioService extends IAudioService.Stub
    private static final int MSG_DISPATCH_AUDIO_MODE = 40;
    private static final int MSG_DISPATCH_AUDIO_MODE = 40;
    private static final int MSG_ROUTING_UPDATED = 41;
    private static final int MSG_ROUTING_UPDATED = 41;
    private static final int MSG_INIT_HEADTRACKING_SENSORS = 42;
    private static final int MSG_INIT_HEADTRACKING_SENSORS = 42;
    private static final int MSG_PERSIST_SPATIAL_AUDIO_ENABLED = 43;


    // start of messages handled under wakelock
    // start of messages handled under wakelock
    //   these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(),
    //   these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(),
@@ -1038,12 +1039,13 @@ public class AudioService extends IAudioService.Stub


        mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false);
        mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false);


        mHasSpatializerEffect = SystemProperties.getBoolean("ro.audio.spatializer_enabled", false);

        // done with service initialization, continue additional work in our Handler thread
        // done with service initialization, continue additional work in our Handler thread
        queueMsgUnderWakeLock(mAudioHandler, MSG_INIT_STREAMS_VOLUMES,
        queueMsgUnderWakeLock(mAudioHandler, MSG_INIT_STREAMS_VOLUMES,
                0 /* arg1 */,  0 /* arg2 */, null /* obj */,  0 /* delay */);
                0 /* arg1 */,  0 /* arg2 */, null /* obj */,  0 /* delay */);
        queueMsgUnderWakeLock(mAudioHandler, MSG_INIT_SPATIALIZER,
        queueMsgUnderWakeLock(mAudioHandler, MSG_INIT_SPATIALIZER,
                0 /* arg1 */, 0 /* arg2 */, null /* obj */, 0 /* delay */);
                0 /* arg1 */, 0 /* arg2 */, null /* obj */, 0 /* delay */);

    }
    }


    /**
    /**
@@ -1237,6 +1239,9 @@ public class AudioService extends IAudioService.Stub
    // routing monitoring from AudioSystemAdapter
    // routing monitoring from AudioSystemAdapter
    @Override
    @Override
    public void onRoutingUpdatedFromNative() {
    public void onRoutingUpdatedFromNative() {
        if (!mHasSpatializerEffect) {
            return;
        }
        sendMsg(mAudioHandler,
        sendMsg(mAudioHandler,
                MSG_ROUTING_UPDATED,
                MSG_ROUTING_UPDATED,
                SENDMSG_REPLACE, 0, 0, null,
                SENDMSG_REPLACE, 0, 0, null,
@@ -1433,8 +1438,10 @@ public class AudioService extends IAudioService.Stub
            }
            }
        }
        }


        // TODO check property if feature enabled
        if (mHasSpatializerEffect) {
        mSpatializerHelper.reset(/* featureEnabled */ SPATIALIZER_FEATURE_ENABLED_DEFAULT);
            mSpatializerHelper.reset(/* featureEnabled */ isSpatialAudioEnabled());
            monitorRoutingChanges(true);
        }


        onIndicateSystemReady();
        onIndicateSystemReady();
        // indicate the end of reconfiguration phase to audio HAL
        // indicate the end of reconfiguration phase to audio HAL
@@ -7570,9 +7577,11 @@ public class AudioService extends IAudioService.Stub
                    break;
                    break;


                case MSG_INIT_SPATIALIZER:
                case MSG_INIT_SPATIALIZER:
                    mSpatializerHelper.init();
                    mSpatializerHelper.init(/*effectExpected*/ mHasSpatializerEffect);
                    // TODO read property to see if enabled
                    if (mHasSpatializerEffect) {
                    mSpatializerHelper.setFeatureEnabled(SPATIALIZER_FEATURE_ENABLED_DEFAULT);
                        mSpatializerHelper.setFeatureEnabled(isSpatialAudioEnabled());
                        monitorRoutingChanges(true);
                    }
                    mAudioEventWakeLock.release();
                    mAudioEventWakeLock.release();
                    break;
                    break;


@@ -7716,6 +7725,10 @@ public class AudioService extends IAudioService.Stub
                case MSG_ROUTING_UPDATED:
                case MSG_ROUTING_UPDATED:
                    mSpatializerHelper.onRoutingUpdated();
                    mSpatializerHelper.onRoutingUpdated();
                    break;
                    break;

                case MSG_PERSIST_SPATIAL_AUDIO_ENABLED:
                    onPersistSpatialAudioEnabled(msg.arg1 == 1);
                    break;
            }
            }
        }
        }
    }
    }
@@ -8285,7 +8298,40 @@ public class AudioService extends IAudioService.Stub


    //==========================================================================================
    //==========================================================================================
    private final @NonNull SpatializerHelper mSpatializerHelper;
    private final @NonNull SpatializerHelper mSpatializerHelper;
    private static final boolean SPATIALIZER_FEATURE_ENABLED_DEFAULT = false;
    /**
     * Initialized from property ro.audio.spatializer_enabled
     * Should only be 1 when the device ships with a Spatializer effect
     */
    private final boolean mHasSpatializerEffect;
    /**
     * Default value for the spatial audio feature
     */
    private static final boolean SPATIAL_AUDIO_ENABLED_DEFAULT = true;

    /**
     * persist in user settings whether the feature is enabled.
     * Can change when {@link Spatializer#setEnabled(boolean)} is called and successfully
     * changes the state of the feature
     * @param featureEnabled
     */
    void persistSpatialAudioEnabled(boolean featureEnabled) {
        sendMsg(mAudioHandler,
                MSG_PERSIST_SPATIAL_AUDIO_ENABLED,
                SENDMSG_REPLACE, featureEnabled ? 1 : 0, 0, null,
                /*delay ms*/ 100);
    }

    void onPersistSpatialAudioEnabled(boolean enabled) {
        Settings.Secure.putIntForUser(mContentResolver,
                Settings.Secure.SPATIAL_AUDIO_ENABLED, enabled ? 1 : 0,
                UserHandle.USER_CURRENT);
    }

    boolean isSpatialAudioEnabled() {
        return Settings.Secure.getIntForUser(mContentResolver,
                Settings.Secure.SPATIAL_AUDIO_ENABLED, SPATIAL_AUDIO_ENABLED_DEFAULT ? 1 : 0,
                UserHandle.USER_CURRENT) == 1;
    }


    private void enforceModifyDefaultAudioEffectsPermission() {
    private void enforceModifyDefaultAudioEffectsPermission() {
        if (mContext.checkCallingOrSelfPermission(
        if (mContext.checkCallingOrSelfPermission(
@@ -9044,6 +9090,12 @@ public class AudioService extends IAudioService.Stub
        sVolumeLogger.dump(pw);
        sVolumeLogger.dump(pw);
        pw.println("\n");
        pw.println("\n");
        dumpSupportedSystemUsage(pw);
        dumpSupportedSystemUsage(pw);

        pw.println("\n");
        pw.println("\nSpatial audio:");
        pw.println("mHasSpatializerEffect:" + mHasSpatializerEffect);
        pw.println("isSpatializerEnabled:" + isSpatializerEnabled());
        pw.println("isSpatialAudioEnabled:" + isSpatialAudioEnabled());
    }
    }


    private void dumpSupportedSystemUsage(PrintWriter pw) {
    private void dumpSupportedSystemUsage(PrintWriter pw) {
+8 −3
Original line number Original line Diff line number Diff line
@@ -106,8 +106,13 @@ public class SpatializerHelper {
        mASA = asa;
        mASA = asa;
    }
    }


    synchronized void init() {
    synchronized void init(boolean effectExpected) {
        Log.i(TAG, "Initializing");
        Log.i(TAG, "Initializing");
        if (!effectExpected) {
            Log.i(TAG, "Setting state to STATE_NOT_SUPPORTED due to effect not expected");
            mState = STATE_NOT_SUPPORTED;
            return;
        }
        if (mState != STATE_UNINITIALIZED) {
        if (mState != STATE_UNINITIALIZED) {
            throw new IllegalStateException(("init() called in state:" + mState));
            throw new IllegalStateException(("init() called in state:" + mState));
        }
        }
@@ -165,7 +170,7 @@ public class SpatializerHelper {
        mSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
        mSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
        mCapableSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
        mCapableSpatLevel = Spatializer.SPATIALIZER_IMMERSIVE_LEVEL_NONE;
        mActualHeadTrackingMode = Spatializer.HEAD_TRACKING_MODE_UNSUPPORTED;
        mActualHeadTrackingMode = Spatializer.HEAD_TRACKING_MODE_UNSUPPORTED;
        init();
        init(true);
        setFeatureEnabled(featureEnabled);
        setFeatureEnabled(featureEnabled);
    }
    }


@@ -391,7 +396,7 @@ public class SpatializerHelper {
            }
            }
        }
        }
        mStateCallbacks.finishBroadcast();
        mStateCallbacks.finishBroadcast();
        // TODO persist enabled state
        mAudioService.persistSpatialAudioEnabled(featureEnabled);
    }
    }


    private synchronized void setDispatchAvailableState(boolean available) {
    private synchronized void setDispatchAvailableState(boolean available) {