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

Commit dc5080a9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "AudioService: surround info has its own lock" into main

parents 1efa2137 ffa314f2
Loading
Loading
Loading
Loading
+70 −37
Original line number Original line Diff line number Diff line
@@ -1173,8 +1173,14 @@ public class AudioService extends IAudioService.Stub
    @GuardedBy("mInputMethodServiceUidLock")
    @GuardedBy("mInputMethodServiceUidLock")
    private int mInputMethodServiceUid = android.os.Process.INVALID_UID;
    private int mInputMethodServiceUid = android.os.Process.INVALID_UID;
    // Surround formats
    // lock for related fields, also protects setting ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS
    private final Object mSurroundLock = new Object();
    @GuardedBy("mSurroundLock")
    private int mEncodedSurroundMode;
    private int mEncodedSurroundMode;
    @GuardedBy("mSurroundLock")
    private String mEnabledSurroundFormats;
    private String mEnabledSurroundFormats;
    @GuardedBy("mSurroundLock")
    private boolean mSurroundModeChanged;
    private boolean mSurroundModeChanged;
    private boolean mSupportsMicPrivacyToggle;
    private boolean mSupportsMicPrivacyToggle;
@@ -2099,8 +2105,12 @@ public class AudioService extends IAudioService.Stub
        final int forDock = mDockAudioMediaEnabled ?
        final int forDock = mDockAudioMediaEnabled ?
                AudioSystem.FORCE_DIGITAL_DOCK : AudioSystem.FORCE_NONE;
                AudioSystem.FORCE_DIGITAL_DOCK : AudioSystem.FORCE_NONE;
        mDeviceBroker.setForceUse_Async(AudioSystem.FOR_DOCK, forDock, "onAudioServerDied");
        mDeviceBroker.setForceUse_Async(AudioSystem.FOR_DOCK, forDock, "onAudioServerDied");
        synchronized (mSurroundLock) {
            sendEncodedSurroundMode(mContentResolver, "onAudioServerDied");
            sendEncodedSurroundMode(mContentResolver, "onAudioServerDied");
            sendEnabledSurroundFormats(mContentResolver, true);
            sendEnabledSurroundFormats(mContentResolver, true);
        }
        AudioSystem.setRttEnabled(mRttEnabled.get());
        AudioSystem.setRttEnabled(mRttEnabled.get());
        synchronized (mAssistantUidLock) {
        synchronized (mAssistantUidLock) {
            resetAssistantServicesUidsLocked();
            resetAssistantServicesUidsLocked();
@@ -2484,7 +2494,9 @@ public class AudioService extends IAudioService.Stub
                    }
                    }
                }
                }
            }
            }
            synchronized (mSurroundLock) {
                sendEnabledSurroundFormats(mContentResolver, true);
                sendEnabledSurroundFormats(mContentResolver, true);
            }
        } else {
        } else {
            // DEVICE_OUT_HDMI disconnected
            // DEVICE_OUT_HDMI disconnected
            if (isPlatformTelevision()) {
            if (isPlatformTelevision()) {
@@ -2862,6 +2874,7 @@ public class AudioService extends IAudioService.Stub
        }
        }
    }
    }
    @GuardedBy("mSurroundLock")
    private void sendEncodedSurroundMode(ContentResolver cr, String eventSource)
    private void sendEncodedSurroundMode(ContentResolver cr, String eventSource)
    {
    {
        final int encodedSurroundMode = mSettings.getGlobalInt(
        final int encodedSurroundMode = mSettings.getGlobalInt(
@@ -2870,6 +2883,7 @@ public class AudioService extends IAudioService.Stub
        sendEncodedSurroundMode(encodedSurroundMode, eventSource);
        sendEncodedSurroundMode(encodedSurroundMode, eventSource);
    }
    }
    @GuardedBy("mSurroundLock")
    private void sendEncodedSurroundMode(int encodedSurroundMode, String eventSource)
    private void sendEncodedSurroundMode(int encodedSurroundMode, String eventSource)
    {
    {
        // initialize to guaranteed bad value
        // initialize to guaranteed bad value
@@ -2924,6 +2938,7 @@ public class AudioService extends IAudioService.Stub
    @Override
    @Override
    public Map<Integer, Boolean> getSurroundFormats() {
    public Map<Integer, Boolean> getSurroundFormats() {
        Map<Integer, Boolean> surroundFormats = new HashMap<>();
        Map<Integer, Boolean> surroundFormats = new HashMap<>();
        synchronized (mSurroundLock) {
            int status = AudioSystem.getSurroundFormats(surroundFormats);
            int status = AudioSystem.getSurroundFormats(surroundFormats);
            if (status != AudioManager.SUCCESS) {
            if (status != AudioManager.SUCCESS) {
                // fail and bail!
                // fail and bail!
@@ -2932,11 +2947,13 @@ public class AudioService extends IAudioService.Stub
            }
            }
            return surroundFormats;
            return surroundFormats;
        }
        }
    }
    /** @see AudioManager#getReportedSurroundFormats() */
    /** @see AudioManager#getReportedSurroundFormats() */
    @Override
    @Override
    public List<Integer> getReportedSurroundFormats() {
    public List<Integer> getReportedSurroundFormats() {
        ArrayList<Integer> reportedSurroundFormats = new ArrayList<>();
        ArrayList<Integer> reportedSurroundFormats = new ArrayList<>();
        synchronized (mSurroundLock) {
            int status = AudioSystem.getReportedSurroundFormats(reportedSurroundFormats);
            int status = AudioSystem.getReportedSurroundFormats(reportedSurroundFormats);
            if (status != AudioManager.SUCCESS) {
            if (status != AudioManager.SUCCESS) {
                // fail and bail!
                // fail and bail!
@@ -2945,6 +2962,7 @@ public class AudioService extends IAudioService.Stub
            }
            }
            return reportedSurroundFormats;
            return reportedSurroundFormats;
        }
        }
    }
    /** @see AudioManager#isSurroundFormatEnabled(int) */
    /** @see AudioManager#isSurroundFormatEnabled(int) */
    @Override
    @Override
@@ -2956,7 +2974,7 @@ public class AudioService extends IAudioService.Stub
        final long token = Binder.clearCallingIdentity();
        final long token = Binder.clearCallingIdentity();
        try {
        try {
            synchronized (mSettingsLock) {
            synchronized (mSurroundLock) {
                HashSet<Integer> enabledFormats = getEnabledFormats();
                HashSet<Integer> enabledFormats = getEnabledFormats();
                return enabledFormats.contains(audioFormat);
                return enabledFormats.contains(audioFormat);
            }
            }
@@ -2977,6 +2995,7 @@ public class AudioService extends IAudioService.Stub
            throw new SecurityException("Missing WRITE_SETTINGS permission");
            throw new SecurityException("Missing WRITE_SETTINGS permission");
        }
        }
        synchronized (mSurroundLock) {
            HashSet<Integer> enabledFormats = getEnabledFormats();
            HashSet<Integer> enabledFormats = getEnabledFormats();
            if (enabled) {
            if (enabled) {
                enabledFormats.add(audioFormat);
                enabledFormats.add(audioFormat);
@@ -2985,14 +3004,13 @@ public class AudioService extends IAudioService.Stub
            }
            }
            final long token = Binder.clearCallingIdentity();
            final long token = Binder.clearCallingIdentity();
            try {
            try {
            synchronized (mSettingsLock) {
                mSettings.putGlobalString(mContentResolver,
                mSettings.putGlobalString(mContentResolver,
                        Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS,
                        Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS,
                        TextUtils.join(",", enabledFormats));
                        TextUtils.join(",", enabledFormats));
            }
            } finally {
            } finally {
                Binder.restoreCallingIdentity(token);
                Binder.restoreCallingIdentity(token);
            }
            }
        }
        return true;
        return true;
    }
    }
@@ -3004,7 +3022,7 @@ public class AudioService extends IAudioService.Stub
        final long token = Binder.clearCallingIdentity();
        final long token = Binder.clearCallingIdentity();
        try {
        try {
            synchronized (mSettingsLock) {
            synchronized (mSurroundLock) {
                mSettings.putGlobalInt(mContentResolver,
                mSettings.putGlobalInt(mContentResolver,
                        Settings.Global.ENCODED_SURROUND_OUTPUT,
                        Settings.Global.ENCODED_SURROUND_OUTPUT,
                        toEncodedSurroundSetting(mode));
                        toEncodedSurroundSetting(mode));
@@ -3017,10 +3035,11 @@ public class AudioService extends IAudioService.Stub
    /** @see AudioManager#getEncodedSurroundMode() */
    /** @see AudioManager#getEncodedSurroundMode() */
    @Override
    @Override
    @AudioManager.EncodedSurroundOutputMode
    public int getEncodedSurroundMode(int targetSdkVersion) {
    public int getEncodedSurroundMode(int targetSdkVersion) {
        final long token = Binder.clearCallingIdentity();
        final long token = Binder.clearCallingIdentity();
        try {
        try {
            synchronized (mSettingsLock) {
            synchronized (mSurroundLock) {
                int encodedSurroundSetting = mSettings.getGlobalInt(mContentResolver,
                int encodedSurroundSetting = mSettings.getGlobalInt(mContentResolver,
                        Settings.Global.ENCODED_SURROUND_OUTPUT,
                        Settings.Global.ENCODED_SURROUND_OUTPUT,
                        AudioManager.ENCODED_SURROUND_OUTPUT_AUTO);
                        AudioManager.ENCODED_SURROUND_OUTPUT_AUTO);
@@ -3032,6 +3051,7 @@ public class AudioService extends IAudioService.Stub
    }
    }
    /** @return the formats that are enabled in global settings */
    /** @return the formats that are enabled in global settings */
    @GuardedBy("mSurroundLock")
    private HashSet<Integer> getEnabledFormats() {
    private HashSet<Integer> getEnabledFormats() {
        HashSet<Integer> formats = new HashSet<>();
        HashSet<Integer> formats = new HashSet<>();
        String enabledFormats = mSettings.getGlobalString(mContentResolver,
        String enabledFormats = mSettings.getGlobalString(mContentResolver,
@@ -3050,7 +3070,8 @@ public class AudioService extends IAudioService.Stub
    @SuppressWarnings("AndroidFrameworkCompatChange")
    @SuppressWarnings("AndroidFrameworkCompatChange")
    @AudioManager.EncodedSurroundOutputMode
    @AudioManager.EncodedSurroundOutputMode
    private int toEncodedSurroundOutputMode(int encodedSurroundSetting, int targetSdkVersion) {
    private static int toEncodedSurroundOutputMode(int encodedSurroundSetting, int targetSdkVersion)
    {
        if (targetSdkVersion <= Build.VERSION_CODES.S
        if (targetSdkVersion <= Build.VERSION_CODES.S
                && encodedSurroundSetting > Settings.Global.ENCODED_SURROUND_SC_MAX) {
                && encodedSurroundSetting > Settings.Global.ENCODED_SURROUND_SC_MAX) {
            return AudioManager.ENCODED_SURROUND_OUTPUT_UNKNOWN;
            return AudioManager.ENCODED_SURROUND_OUTPUT_UNKNOWN;
@@ -3069,7 +3090,7 @@ public class AudioService extends IAudioService.Stub
        }
        }
    }
    }
    private int toEncodedSurroundSetting(
    private static int toEncodedSurroundSetting(
            @AudioManager.EncodedSurroundOutputMode int encodedSurroundOutputMode) {
            @AudioManager.EncodedSurroundOutputMode int encodedSurroundOutputMode) {
        switch (encodedSurroundOutputMode) {
        switch (encodedSurroundOutputMode) {
            case AudioManager.ENCODED_SURROUND_OUTPUT_NEVER:
            case AudioManager.ENCODED_SURROUND_OUTPUT_NEVER:
@@ -3083,7 +3104,7 @@ public class AudioService extends IAudioService.Stub
        }
        }
    }
    }
    private boolean isSurroundFormat(int audioFormat) {
    private static boolean isSurroundFormat(int audioFormat) {
        for (int sf : AudioFormat.SURROUND_SOUND_ENCODING) {
        for (int sf : AudioFormat.SURROUND_SOUND_ENCODING) {
            if (sf == audioFormat) {
            if (sf == audioFormat) {
                return true;
                return true;
@@ -3092,6 +3113,7 @@ public class AudioService extends IAudioService.Stub
        return false;
        return false;
    }
    }
    @GuardedBy("mSurroundLock")
    private void sendEnabledSurroundFormats(ContentResolver cr, boolean forceUpdate) {
    private void sendEnabledSurroundFormats(ContentResolver cr, boolean forceUpdate) {
        if (mEncodedSurroundMode != Settings.Global.ENCODED_SURROUND_OUTPUT_MANUAL) {
        if (mEncodedSurroundMode != Settings.Global.ENCODED_SURROUND_OUTPUT_MANUAL) {
            // Manually enable surround formats only when the setting is in manual mode.
            // Manually enable surround formats only when the setting is in manual mode.
@@ -3131,12 +3153,16 @@ public class AudioService extends IAudioService.Stub
        sendMsg(mAudioHandler, MSG_ENABLE_SURROUND_FORMATS, SENDMSG_QUEUE, 0, 0, formats, 0);
        sendMsg(mAudioHandler, MSG_ENABLE_SURROUND_FORMATS, SENDMSG_QUEUE, 0, 0, formats, 0);
    }
    }
    // handles MSG_ENABLE_SURROUND_FORMATS
    private void onEnableSurroundFormats(ArrayList<Integer> enabledSurroundFormats) {
    private void onEnableSurroundFormats(ArrayList<Integer> enabledSurroundFormats) {
        synchronized (mSurroundLock) {
            // Set surround format enabled accordingly.
            // Set surround format enabled accordingly.
            for (int surroundFormat : AudioFormat.SURROUND_SOUND_ENCODING) {
            for (int surroundFormat : AudioFormat.SURROUND_SOUND_ENCODING) {
                boolean enabled = enabledSurroundFormats.contains(surroundFormat);
                boolean enabled = enabledSurroundFormats.contains(surroundFormat);
                int ret = AudioSystem.setSurroundFormatEnabled(surroundFormat, enabled);
                int ret = AudioSystem.setSurroundFormatEnabled(surroundFormat, enabled);
            Log.i(TAG, "enable surround format:" + surroundFormat + " " + enabled + " " + ret);
                Log.i(TAG, "enable surround format:"
                        + surroundFormat + " " + enabled + " " + ret);
            }
        }
        }
    }
    }
@@ -3235,6 +3261,9 @@ public class AudioService extends IAudioService.Stub
            updateRingerAndZenModeAffectedStreams();
            updateRingerAndZenModeAffectedStreams();
            readDockAudioSettings(cr);
            readDockAudioSettings(cr);
        }
        synchronized (mSurroundLock) {
            sendEncodedSurroundMode(cr, "readPersistedSettings");
            sendEncodedSurroundMode(cr, "readPersistedSettings");
            sendEnabledSurroundFormats(cr, true);
            sendEnabledSurroundFormats(cr, true);
        }
        }
@@ -10717,6 +10746,9 @@ public class AudioService extends IAudioService.Stub
                readDockAudioSettings(mContentResolver);
                readDockAudioSettings(mContentResolver);
                updateMasterMono(mContentResolver);
                updateMasterMono(mContentResolver);
                updateMasterBalance(mContentResolver);
                updateMasterBalance(mContentResolver);
            }
            synchronized (mSurroundLock) {
                updateEncodedSurroundOutput();
                updateEncodedSurroundOutput();
                sendEnabledSurroundFormats(mContentResolver, mSurroundModeChanged);
                sendEnabledSurroundFormats(mContentResolver, mSurroundModeChanged);
            }
            }
@@ -10726,6 +10758,7 @@ public class AudioService extends IAudioService.Stub
            }
            }
        }
        }
        @GuardedBy("mSurroundLock")
        private void updateEncodedSurroundOutput() {
        private void updateEncodedSurroundOutput() {
            int newSurroundMode = mSettings.getGlobalInt(
            int newSurroundMode = mSettings.getGlobalInt(
                mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT,
                mContentResolver, Settings.Global.ENCODED_SURROUND_OUTPUT,