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

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

Merge "AudioService: fix spatial audio state restoring and head tracker detection" into main

parents 973ae3b1 73f19b45
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -2793,12 +2793,13 @@ public class AudioDeviceBroker {
        return mDeviceInventory.getImmutableDeviceInventory();
    }

    void addOrUpdateDeviceSAStateInInventory(AdiDeviceState deviceState) {
        mDeviceInventory.addOrUpdateDeviceSAStateInInventory(deviceState);
    void addOrUpdateDeviceSAStateInInventory(AdiDeviceState deviceState, boolean syncInventory) {
        mDeviceInventory.addOrUpdateDeviceSAStateInInventory(deviceState, syncInventory);
    }

    void addOrUpdateBtAudioDeviceCategoryInInventory(AdiDeviceState deviceState) {
        mDeviceInventory.addOrUpdateAudioDeviceCategoryInInventory(deviceState);
    void addOrUpdateBtAudioDeviceCategoryInInventory(
            AdiDeviceState deviceState, boolean syncInventory) {
        mDeviceInventory.addOrUpdateAudioDeviceCategoryInInventory(deviceState, syncInventory);
    }

    @Nullable
+46 −17
Original line number Diff line number Diff line
@@ -135,9 +135,10 @@ public class AudioDeviceInventory {
     * AdiDeviceState in the {@link AudioDeviceInventory#mDeviceInventory} list.
     * @param deviceState the device to update
     */
    void addOrUpdateDeviceSAStateInInventory(AdiDeviceState deviceState) {
    void addOrUpdateDeviceSAStateInInventory(AdiDeviceState deviceState, boolean syncInventory) {
        synchronized (mDeviceInventoryLock) {
            mDeviceInventory.merge(deviceState.getDeviceId(), deviceState, (oldState, newState) -> {
            mDeviceInventory.merge(deviceState.getDeviceId(), deviceState,
                    (oldState, newState) -> {
                oldState.setHasHeadTracker(newState.hasHeadTracker());
                oldState.setHeadTrackerEnabled(newState.isHeadTrackerEnabled());
                oldState.setSAEnabled(newState.isSAEnabled());
@@ -145,8 +146,10 @@ public class AudioDeviceInventory {
            });
            checkDeviceInventorySize_l();
        }
        if (syncInventory) {
            mDeviceBroker.postSynchronizeAdiDevicesInInventory(deviceState);
        }
    }

    /**
     * Adds a new entry in mDeviceInventory if the attributes passed represent a sink
@@ -196,7 +199,8 @@ public class AudioDeviceInventory {
     * AdiDeviceState in the {@link AudioDeviceInventory#mDeviceInventory} list.
     * @param deviceState the device to update
     */
    void addOrUpdateAudioDeviceCategoryInInventory(AdiDeviceState deviceState) {
    void addOrUpdateAudioDeviceCategoryInInventory(
            AdiDeviceState deviceState, boolean syncInventory) {
        AtomicBoolean updatedCategory = new AtomicBoolean(false);
        synchronized (mDeviceInventoryLock) {
            if (automaticBtDeviceType()) {
@@ -218,8 +222,10 @@ public class AudioDeviceInventory {
        if (updatedCategory.get()) {
            mDeviceBroker.postUpdatedAdiDeviceState(deviceState, false /*initSA*/);
        }
        if (syncInventory) {
            mDeviceBroker.postSynchronizeAdiDevicesInInventory(deviceState);
        }
    }

    void addAudioDeviceWithCategoryInInventoryIfNeeded(@NonNull String address,
            @AudioDeviceCategory int btAudioDeviceCategory) {
@@ -235,14 +241,14 @@ public class AudioDeviceInventory {
        boolean bleCategoryFound = false;
        AdiDeviceState deviceState = findBtDeviceStateForAddress(address, DEVICE_OUT_BLE_HEADSET);
        if (deviceState != null) {
            addOrUpdateAudioDeviceCategoryInInventory(deviceState);
            addOrUpdateAudioDeviceCategoryInInventory(deviceState, true /*syncInventory*/);
            btCategory = deviceState.getAudioDeviceCategory();
            bleCategoryFound = true;
        }

        deviceState = findBtDeviceStateForAddress(address, DEVICE_OUT_BLUETOOTH_A2DP);
        if (deviceState != null) {
            addOrUpdateAudioDeviceCategoryInInventory(deviceState);
            addOrUpdateAudioDeviceCategoryInInventory(deviceState, true /*syncInventory*/);
            int a2dpCategory = deviceState.getAudioDeviceCategory();
            if (bleCategoryFound && a2dpCategory != btCategory) {
                Log.w(TAG, "Found different audio device category for A2DP and BLE profiles with "
@@ -269,11 +275,33 @@ public class AudioDeviceInventory {
    }

    /**
     * synchronize AdiDeviceState for LE devices in the same group
     * Synchronize AdiDeviceState for LE devices in the same group
     * or BT classic devices with the same address.
     * @param updatedDevice the device state to synchronize or null.
     * Called with null once after the device inventory and spatializer helper
     * have been initialized to resync all devices.
     */
    void onSynchronizeAdiDevicesInInventory(AdiDeviceState updatedDevice) {
        synchronized (mDevicesLock) {
            synchronized (mDeviceInventoryLock) {
                if (updatedDevice != null) {
                    onSynchronizeAdiDeviceInInventory_l(updatedDevice);
                } else {
                    for (AdiDeviceState ads : mDeviceInventory.values()) {
                        onSynchronizeAdiDeviceInInventory_l(ads);
                    }
                }
            }
        }
    }

    /**
     * Synchronize AdiDeviceState for LE devices in the same group
     * or BT classic devices with the same address.
     * @param updatedDevice the device state to synchronize.
     */
    @GuardedBy({"mDevicesLock", "mDeviceInventoryLock"})
    void onSynchronizeAdiDeviceInInventory_l(AdiDeviceState updatedDevice) {
        boolean found = false;
        found |= synchronizeBleDeviceInInventory(updatedDevice);
        if (automaticBtDeviceType()) {
@@ -283,8 +311,6 @@ public class AudioDeviceInventory {
            mDeviceBroker.postPersistAudioDeviceSettings();
        }
    }
        }
    }

    @GuardedBy("mDeviceInventoryLock")
    private void checkDeviceInventorySize_l() {
@@ -595,6 +621,9 @@ public class AudioDeviceInventory {
            mDeviceName = TextUtils.emptyIfNull(deviceName);
            mDeviceAddress = TextUtils.emptyIfNull(address);
            mDeviceIdentityAddress = TextUtils.emptyIfNull(identityAddress);
            if (mDeviceIdentityAddress.isEmpty()) {
                mDeviceIdentityAddress = mDeviceAddress;
            }
            mDeviceCodecFormat = codecFormat;
            mGroupId = groupId;
            mPeerDeviceAddress = TextUtils.emptyIfNull(peerAddress);
@@ -2951,8 +2980,8 @@ public class AudioDeviceInventory {
            // Note if the device is not compatible with spatialization mode or the device
            // type is not canonical, it will be ignored in {@link SpatializerHelper}.
            if (devState != null) {
                addOrUpdateDeviceSAStateInInventory(devState);
                addOrUpdateAudioDeviceCategoryInInventory(devState);
                addOrUpdateDeviceSAStateInInventory(devState, false /*syncInventory*/);
                addOrUpdateAudioDeviceCategoryInInventory(devState, false /*syncInventory*/);
            }
        }
    }
+5 −1
Original line number Diff line number Diff line
@@ -9639,6 +9639,9 @@ public class AudioService extends IAudioService.Stub
                case MSG_INIT_SPATIALIZER:
                    onInitSpatializer();
                    // the device inventory can only be synchronized after the
                    // spatializer has been initialized
                    mDeviceBroker.postSynchronizeAdiDevicesInInventory(null);
                    mAudioEventWakeLock.release();
                    break;
@@ -11394,7 +11397,8 @@ public class AudioService extends IAudioService.Stub
        deviceState.setAudioDeviceCategory(btAudioDeviceCategory);
        mDeviceBroker.addOrUpdateBtAudioDeviceCategoryInInventory(deviceState);
        mDeviceBroker.addOrUpdateBtAudioDeviceCategoryInInventory(
                deviceState, true /*syncInventory*/);
        mDeviceBroker.postPersistAudioDeviceSettings();
        mSpatializerHelper.refreshDevice(deviceState.getAudioDeviceAttributes(),
+3 −2
Original line number Diff line number Diff line
@@ -568,7 +568,8 @@ public class SpatializerHelper {
            updatedDevice = new AdiDeviceState(canonicalDeviceType, ada.getInternalType(),
                    ada.getAddress());
            initSAState(updatedDevice);
            mDeviceBroker.addOrUpdateDeviceSAStateInInventory(updatedDevice);
            mDeviceBroker.addOrUpdateDeviceSAStateInInventory(
                    updatedDevice, true /*syncInventory*/);
        }
        if (updatedDevice != null) {
            onRoutingUpdated();
@@ -723,7 +724,7 @@ public class SpatializerHelper {
                    new AdiDeviceState(canonicalDeviceType, ada.getInternalType(),
                            ada.getAddress());
            initSAState(deviceState);
            mDeviceBroker.addOrUpdateDeviceSAStateInInventory(deviceState);
            mDeviceBroker.addOrUpdateDeviceSAStateInInventory(deviceState, true /*syncInventory*/);
            mDeviceBroker.postPersistAudioDeviceSettings();
            logDeviceState(deviceState, "addWirelessDeviceIfNew"); // may be updated later.
        }