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

Commit 1a1c3d6f authored by Eric Laurent's avatar Eric Laurent Committed by Android (Google) Code Review
Browse files

Merge "AudioDeviceBroker: avoid acquiring mDeviceStateLock when not necessary" into main

parents 85e30b9d a4a848f5
Loading
Loading
Loading
Loading
+45 −62
Original line number Diff line number Diff line
@@ -127,8 +127,7 @@ public class AudioDeviceBroker {
    private final Object mDeviceStateLock = new Object();

    // Request to override default use of A2DP for media.
    @GuardedBy("mDeviceStateLock")
    private boolean mBluetoothA2dpEnabled;
    private AtomicBoolean mBluetoothA2dpEnabled = new AtomicBoolean(false);

    // lock always taken when accessing AudioService.mSetModeDeathHandlers
    // TODO do not "share" the lock between AudioService and BtHelpr, see b/123769055
@@ -275,17 +274,8 @@ public class AudioDeviceBroker {
    }

    /*package*/ void setBluetoothA2dpOn_Async(boolean on, String source) {
        synchronized (mDeviceStateLock) {
            if (mBluetoothA2dpEnabled == on) {
                return;
            }
            mBluetoothA2dpEnabled = on;
            mBrokerHandler.removeMessages(MSG_IIL_SET_FORCE_BT_A2DP_USE);
            sendIILMsgNoDelay(MSG_IIL_SET_FORCE_BT_A2DP_USE, SENDMSG_QUEUE,
                    AudioSystem.FOR_MEDIA,
                    mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP,
                    source);
        }
        mBluetoothA2dpEnabled.set(on);
        sendLMsgNoDelay(MSG_L_SET_FORCE_BT_A2DP_USE, SENDMSG_REPLACE, source);
    }

    /**
@@ -1223,16 +1213,8 @@ public class AudioDeviceBroker {
        }
    }

    /*package*/ boolean isAvrcpAbsoluteVolumeSupported() {
        synchronized (mDeviceStateLock) {
            return mBtHelper.isAvrcpAbsoluteVolumeSupported();
        }
    }

    /*package*/ boolean isBluetoothA2dpOn() {
        synchronized (mDeviceStateLock) {
            return mBluetoothA2dpEnabled;
        }
        return mBluetoothA2dpEnabled.get();
    }

    /*package*/ void postSetAvrcpAbsoluteVolumeIndex(int index) {
@@ -1601,16 +1583,13 @@ public class AudioDeviceBroker {
                .append(") from u/pid:").append(Binder.getCallingUid()).append("/")
                .append(Binder.getCallingPid()).append(" src:").append(source).toString();

        synchronized (mDeviceStateLock) {
            mBluetoothA2dpEnabled = on;
            mBrokerHandler.removeMessages(MSG_IIL_SET_FORCE_BT_A2DP_USE);
        mBluetoothA2dpEnabled.set(on);
        onSetForceUse(
                AudioSystem.FOR_MEDIA,
                    mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP,
                on ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP,
                fromA2dp,
                eventSource);
    }
    }

    /*package*/ boolean handleDeviceConnection(@NonNull AudioDeviceAttributes attributes,
                                boolean connect, @Nullable BluetoothDevice btDevice) {
@@ -1658,9 +1637,7 @@ public class AudioDeviceBroker {
    }

    /*package*/ boolean getBluetoothA2dpEnabled() {
        synchronized (mDeviceStateLock) {
            return mBluetoothA2dpEnabled;
        }
        return mBluetoothA2dpEnabled.get();
    }

    /*package*/ int getLeAudioDeviceGroupId(BluetoothDevice device) {
@@ -1821,9 +1798,12 @@ public class AudioDeviceBroker {
                    }
                    break;
                case MSG_IIL_SET_FORCE_USE: // intended fall-through
                case MSG_IIL_SET_FORCE_BT_A2DP_USE:
                    onSetForceUse(msg.arg1, msg.arg2,
                                  (msg.what == MSG_IIL_SET_FORCE_BT_A2DP_USE), (String) msg.obj);
                    onSetForceUse(msg.arg1, msg.arg2, false, (String) msg.obj);
                    break;
                case MSG_L_SET_FORCE_BT_A2DP_USE:
                    int forcedUsage = mBluetoothA2dpEnabled.get()
                            ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP;
                    onSetForceUse(AudioSystem.FOR_MEDIA, forcedUsage, true, (String) msg.obj);
                    break;
                case MSG_REPORT_NEW_ROUTES:
                case MSG_REPORT_NEW_ROUTES_A2DP:
@@ -1831,9 +1811,7 @@ public class AudioDeviceBroker {
                        mDeviceInventory.onReportNewRoutes();
                    }
                    break;
                case MSG_L_SET_BT_ACTIVE_DEVICE:
                    synchronized (mSetModeLock) {
                        synchronized (mDeviceStateLock) {
                case MSG_L_SET_BT_ACTIVE_DEVICE: {
                    final BtDeviceInfo btInfo = (BtDeviceInfo) msg.obj;
                    if (btInfo.mState == BluetoothProfile.STATE_CONNECTED
                            && !mBtHelper.isProfilePoxyConnected(btInfo.mProfile)) {
@@ -1846,20 +1824,25 @@ public class AudioDeviceBroker {
                                mBtHelper.getCodecWithFallback(btInfo.mDevice,
                                        btInfo.mProfile, btInfo.mIsLeOutput,
                                        "MSG_L_SET_BT_ACTIVE_DEVICE");
                        synchronized (mSetModeLock) {
                            synchronized (mDeviceStateLock) {
                                mDeviceInventory.onSetBtActiveDevice(btInfo, codec,
                                        (btInfo.mProfile
                                                != BluetoothProfile.LE_AUDIO || btInfo.mIsLeOutput)
                                                != BluetoothProfile.LE_AUDIO
                                                || btInfo.mIsLeOutput)
                                                ? mAudioService.getBluetoothContextualVolumeStream()
                                                : AudioSystem.STREAM_DEFAULT);
                                if (btInfo.mProfile == BluetoothProfile.LE_AUDIO
                                        || btInfo.mProfile == BluetoothProfile.HEARING_AID) {
                                    onUpdateCommunicationRouteClient(isBluetoothScoRequested(),
                                        || btInfo.mProfile
                                        == BluetoothProfile.HEARING_AID) {
                                    onUpdateCommunicationRouteClient(
                                            isBluetoothScoRequested(),
                                            "setBluetoothActiveDevice");
                                }
                            }
                        }
                    }
                    break;
                } break;
                case MSG_BT_HEADSET_CNCT_FAILED:
                    synchronized (mSetModeLock) {
                        synchronized (mDeviceStateLock) {
@@ -1883,11 +1866,11 @@ public class AudioDeviceBroker {
                    break;
                case MSG_L_BLUETOOTH_DEVICE_CONFIG_CHANGE: {
                    final BtDeviceInfo btInfo = (BtDeviceInfo) msg.obj;
                    synchronized (mDeviceStateLock) {
                    @AudioSystem.AudioFormatNativeEnumForBtCodec final int codec =
                            mBtHelper.getCodecWithFallback(btInfo.mDevice,
                                    btInfo.mProfile, btInfo.mIsLeOutput,
                                    "MSG_L_BLUETOOTH_DEVICE_CONFIG_CHANGE");
                    synchronized (mDeviceStateLock) {
                        mDeviceInventory.onBluetoothDeviceConfigChange(
                                btInfo, codec, BtHelper.EVENT_DEVICE_CONFIG_CHANGE);
                    }
@@ -2098,7 +2081,7 @@ public class AudioDeviceBroker {
    private static final int MSG_L_SET_WIRED_DEVICE_CONNECTION_STATE = 2;
    private static final int MSG_I_BROADCAST_BT_CONNECTION_STATE = 3;
    private static final int MSG_IIL_SET_FORCE_USE = 4;
    private static final int MSG_IIL_SET_FORCE_BT_A2DP_USE = 5;
    private static final int MSG_L_SET_FORCE_BT_A2DP_USE = 5;
    private static final int MSG_TOGGLE_HDMI = 6;
    private static final int MSG_L_SET_BT_ACTIVE_DEVICE = 7;
    private static final int MSG_BT_HEADSET_CNCT_FAILED = 9;
@@ -2295,7 +2278,7 @@ public class AudioDeviceBroker {
        MESSAGES_MUTE_MUSIC.add(MSG_L_SET_BT_ACTIVE_DEVICE);
        MESSAGES_MUTE_MUSIC.add(MSG_L_BLUETOOTH_DEVICE_CONFIG_CHANGE);
        MESSAGES_MUTE_MUSIC.add(MSG_L_A2DP_DEVICE_CONNECTION_CHANGE_EXT);
        MESSAGES_MUTE_MUSIC.add(MSG_IIL_SET_FORCE_BT_A2DP_USE);
        MESSAGES_MUTE_MUSIC.add(MSG_L_SET_FORCE_BT_A2DP_USE);
    }

    private AtomicBoolean mMusicMuted = new AtomicBoolean(false);
+0 −6
Original line number Diff line number Diff line
@@ -235,10 +235,6 @@ public class BtHelper {
        mDeviceBroker.setForceUse_Async(AudioSystem.FOR_MEDIA, forMed, "onAudioServerDied()");
    }

    /*package*/ synchronized boolean isAvrcpAbsoluteVolumeSupported() {
        return (mA2dp != null && mAvrcpAbsVolSupported);
    }

    /*package*/ synchronized void setAvrcpAbsoluteVolumeSupported(boolean supported) {
        mAvrcpAbsVolSupported = supported;
        Log.i(TAG, "setAvrcpAbsoluteVolumeSupported supported=" + supported);
@@ -648,8 +644,6 @@ public class BtHelper {
        }
    }

    // @GuardedBy("mDeviceBroker.mSetModeLock")
    @GuardedBy("AudioDeviceBroker.this.mDeviceStateLock")
    /*package*/ synchronized boolean isProfilePoxyConnected(int profile) {
        switch (profile) {
            case BluetoothProfile.HEADSET: