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

Commit 09e3851c authored by Eric Laurent's avatar Eric Laurent Committed by Mikhail Naganov
Browse files

AudioService: initialize spatial audio state on first device connection

Add missing initialization of spatial audio state in device inventory
according to defaults in spatializer helper when a device is connected
for the first time.

Bug: 328936846
Bug: 326367563
Bug: 331734025
Test: repro steps in bugs
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:b259aa89876e1a33d6ddea1ee8395baee4022ba2)
Merged-In: I1d7c0651c67925eb5027a5d17f006211b92a349c
Change-Id: I1d7c0651c67925eb5027a5d17f006211b92a349c
parent 24f98a53
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -1525,8 +1525,9 @@ public class AudioDeviceBroker {
        sendLMsgNoDelay(MSG_L_SYNCHRONIZE_ADI_DEVICES_IN_INVENTORY, SENDMSG_QUEUE, deviceState);
    }

    /*package*/ void postUpdatedAdiDeviceState(AdiDeviceState deviceState) {
        sendLMsgNoDelay(MSG_L_UPDATED_ADI_DEVICE_STATE, SENDMSG_QUEUE, deviceState);
    /*package*/ void postUpdatedAdiDeviceState(AdiDeviceState deviceState, boolean initSA) {
        sendILMsgNoDelay(
                MSG_IL_UPDATED_ADI_DEVICE_STATE, SENDMSG_QUEUE, initSA ? 1 : 0, deviceState);
    }

    /*package*/ static final class CommunicationDeviceInfo {
@@ -2057,8 +2058,8 @@ public class AudioDeviceBroker {
                        }
                    } break;

                case MSG_L_UPDATED_ADI_DEVICE_STATE:
                    mAudioService.onUpdatedAdiDeviceState((AdiDeviceState) msg.obj);
                case MSG_IL_UPDATED_ADI_DEVICE_STATE:
                    mAudioService.onUpdatedAdiDeviceState((AdiDeviceState) msg.obj, msg.arg1 == 1);
                    break;

                default:
@@ -2145,7 +2146,7 @@ public class AudioDeviceBroker {
    private static final int MSG_CHECK_COMMUNICATION_ROUTE_CLIENT_STATE = 56;
    private static final int MSG_I_UPDATE_LE_AUDIO_GROUP_ADDRESSES = 57;
    private static final int MSG_L_SYNCHRONIZE_ADI_DEVICES_IN_INVENTORY = 58;
    private static final int MSG_L_UPDATED_ADI_DEVICE_STATE = 59;
    private static final int MSG_IL_UPDATED_ADI_DEVICE_STATE = 59;



+6 −6
Original line number Diff line number Diff line
@@ -169,7 +169,7 @@ public class AudioDeviceInventory {
            if (ads != null) {
                if (ads.getAudioDeviceCategory() != category) {
                    ads.setAudioDeviceCategory(category);
                    mDeviceBroker.postUpdatedAdiDeviceState(ads);
                    mDeviceBroker.postUpdatedAdiDeviceState(ads, false /*initSA*/);
                    mDeviceBroker.postPersistAudioDeviceSettings();
                }
                mDeviceBroker.postSynchronizeAdiDevicesInInventory(ads);
@@ -182,7 +182,7 @@ public class AudioDeviceInventory {
            mDeviceInventory.put(ads.getDeviceId(), ads);
            checkDeviceInventorySize_l();

            mDeviceBroker.postUpdatedAdiDeviceState(ads);
            mDeviceBroker.postUpdatedAdiDeviceState(ads, true /*initSA*/);
            mDeviceBroker.postPersistAudioDeviceSettings();
        }
    }
@@ -212,7 +212,7 @@ public class AudioDeviceInventory {
            checkDeviceInventorySize_l();
        }
        if (updatedCategory.get()) {
            mDeviceBroker.postUpdatedAdiDeviceState(deviceState);
            mDeviceBroker.postUpdatedAdiDeviceState(deviceState, false /*initSA*/);
        }
        mDeviceBroker.postSynchronizeAdiDevicesInInventory(deviceState);
    }
@@ -314,7 +314,7 @@ public class AudioDeviceInventory {
                    }
                    ads2.setAudioDeviceCategory(updatedDevice.getAudioDeviceCategory());

                    mDeviceBroker.postUpdatedAdiDeviceState(ads2);
                    mDeviceBroker.postUpdatedAdiDeviceState(ads2, false /*initSA*/);
                    AudioService.sDeviceLogger.enqueue(new EventLogger.StringEvent(
                            "synchronizeBleDeviceInInventory synced device pair ads1="
                                    + updatedDevice + " ads2=" + ads2).printLog(TAG));
@@ -335,7 +335,7 @@ public class AudioDeviceInventory {
                    }
                    ads2.setAudioDeviceCategory(updatedDevice.getAudioDeviceCategory());

                    mDeviceBroker.postUpdatedAdiDeviceState(ads2);
                    mDeviceBroker.postUpdatedAdiDeviceState(ads2, false /*initSA*/);
                    AudioService.sDeviceLogger.enqueue(new EventLogger.StringEvent(
                            "synchronizeBleDeviceInInventory synced device pair ads1="
                                    + updatedDevice + " peer ads2=" + ads2).printLog(TAG));
@@ -360,7 +360,7 @@ public class AudioDeviceInventory {
            }
            ads.setAudioDeviceCategory(updatedDevice.getAudioDeviceCategory());

            mDeviceBroker.postUpdatedAdiDeviceState(ads);
            mDeviceBroker.postUpdatedAdiDeviceState(ads, false /*initSA*/);
            AudioService.sDeviceLogger.enqueue(new EventLogger.StringEvent(
                    "synchronizeDeviceProfilesInInventory synced device pair ads1="
                            + updatedDevice + " ads2=" + ads).printLog(TAG));
+4 −3
Original line number Diff line number Diff line
@@ -11265,7 +11265,8 @@ public class AudioService extends IAudioService.Stub
        mDeviceBroker.addOrUpdateBtAudioDeviceCategoryInInventory(deviceState);
        mDeviceBroker.postPersistAudioDeviceSettings();
        mSpatializerHelper.refreshDevice(deviceState.getAudioDeviceAttributes());
        mSpatializerHelper.refreshDevice(deviceState.getAudioDeviceAttributes(),
                false /* initState */);
        mSoundDoseHelper.setAudioDeviceCategory(addr, internalType,
                btAudioDeviceCategory == AUDIO_DEVICE_CATEGORY_HEADPHONES);
    }
@@ -11336,11 +11337,11 @@ public class AudioService extends IAudioService.Stub
    /** Update the sound dose and spatializer state based on the new AdiDeviceState. */
    @VisibleForTesting(visibility = PACKAGE)
    public void onUpdatedAdiDeviceState(AdiDeviceState deviceState) {
    public void onUpdatedAdiDeviceState(AdiDeviceState deviceState, boolean initSA) {
        if (deviceState == null) {
            return;
        }
        mSpatializerHelper.refreshDevice(deviceState.getAudioDeviceAttributes());
        mSpatializerHelper.refreshDevice(deviceState.getAudioDeviceAttributes(), initSA);
        mSoundDoseHelper.setAudioDeviceCategory(deviceState.getDeviceAddress(),
                deviceState.getInternalDeviceType(),
                deviceState.getAudioDeviceCategory() == AUDIO_DEVICE_CATEGORY_HEADPHONES);
+9 −6
Original line number Diff line number Diff line
@@ -295,11 +295,11 @@ public class SpatializerHelper {
            // could have been called another time after boot in case of audioserver restart
            addCompatibleAudioDevice(
                    new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_SPEAKER, ""),
                            false /*forceEnable*/);
                            false /*forceEnable*/, false /*forceInit*/);
            // not force-enabling as this device might already be in the device list
            addCompatibleAudioDevice(
                    new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE, ""),
                            false /*forceEnable*/);
                            false /*forceEnable*/, false /*forceInit*/);
        } catch (RemoteException e) {
            resetCapabilities();
        } finally {
@@ -523,7 +523,7 @@ public class SpatializerHelper {
    }

    synchronized void addCompatibleAudioDevice(@NonNull AudioDeviceAttributes ada) {
        addCompatibleAudioDevice(ada, true /*forceEnable*/);
        addCompatibleAudioDevice(ada, true /*forceEnable*/, false /*forceInit*/);
    }

    /**
@@ -537,7 +537,7 @@ public class SpatializerHelper {
     */
    @GuardedBy("this")
    private void addCompatibleAudioDevice(@NonNull AudioDeviceAttributes ada,
            boolean forceEnable) {
            boolean forceEnable, boolean forceInit) {
        if (!isDeviceCompatibleWithSpatializationModes(ada)) {
            return;
        }
@@ -545,6 +545,9 @@ public class SpatializerHelper {
        final AdiDeviceState deviceState = findSACompatibleDeviceStateForAudioDeviceAttributes(ada);
        AdiDeviceState updatedDevice = null; // non-null on update.
        if (deviceState != null) {
            if (forceInit) {
                initSAState(deviceState);
            }
            if (forceEnable && !deviceState.isSAEnabled()) {
                updatedDevice = deviceState;
                updatedDevice.setSAEnabled(true);
@@ -753,10 +756,10 @@ public class SpatializerHelper {
        }
    }

    synchronized void refreshDevice(@NonNull AudioDeviceAttributes ada) {
    synchronized void refreshDevice(@NonNull AudioDeviceAttributes ada, boolean initState) {
        final AdiDeviceState deviceState = findSACompatibleDeviceStateForAudioDeviceAttributes(ada);
        if (isAvailableForAdiDeviceState(deviceState)) {
            addCompatibleAudioDevice(ada, /*forceEnable=*/deviceState.isSAEnabled());
            addCompatibleAudioDevice(ada, /*forceEnable=*/deviceState.isSAEnabled(), initState);
            setHeadTrackerEnabled(deviceState.isHeadTrackerEnabled(), ada);
        } else {
            removeCompatibleAudioDevice(ada);
+4 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
@@ -315,7 +316,7 @@ public class AudioDeviceBrokerTest {
                                mFakeBtDevice.getAddress()));
                verify(mMockAudioService,
                        timeout(MAX_MESSAGE_HANDLING_DELAY_MS).times(0)).onUpdatedAdiDeviceState(
                        eq(devState));
                        eq(devState), anyBoolean());
            }

            // metadata set
@@ -326,7 +327,7 @@ public class AudioDeviceBrokerTest {
                                mFakeBtDevice.getAddress()));
                verify(mMockAudioService,
                        timeout(MAX_MESSAGE_HANDLING_DELAY_MS)).onUpdatedAdiDeviceState(
                        any());
                        any(), anyBoolean());
            }
        } finally {
            // reset the metadata device type
@@ -354,7 +355,7 @@ public class AudioDeviceBrokerTest {
        verify(mMockAudioService,
                timeout(MAX_MESSAGE_HANDLING_DELAY_MS).atLeast(1)).onUpdatedAdiDeviceState(
                ArgumentMatchers.argThat(devState -> devState.getAudioDeviceCategory()
                        == AudioManager.AUDIO_DEVICE_CATEGORY_OTHER));
                        == AudioManager.AUDIO_DEVICE_CATEGORY_OTHER), anyBoolean());
    }

    private void doTestConnectionDisconnectionReconnection(int delayAfterDisconnection,