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

Commit a81d6028 authored by Eric Laurent's avatar Eric Laurent
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

Change-Id: I1d7c0651c67925eb5027a5d17f006211b92a349c
(cherry picked from commit b259aa89)
parent 089280f5
Loading
Loading
Loading
Loading
+6 −5
Original line number Original line Diff line number Diff line
@@ -1535,8 +1535,9 @@ public class AudioDeviceBroker {
        sendLMsgNoDelay(MSG_L_SYNCHRONIZE_ADI_DEVICES_IN_INVENTORY, SENDMSG_QUEUE, deviceState);
        sendLMsgNoDelay(MSG_L_SYNCHRONIZE_ADI_DEVICES_IN_INVENTORY, SENDMSG_QUEUE, deviceState);
    }
    }


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


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


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


                default:
                default:
@@ -2156,7 +2157,7 @@ public class AudioDeviceBroker {
    private static final int MSG_CHECK_COMMUNICATION_ROUTE_CLIENT_STATE = 56;
    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_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_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 Original line Diff line number Diff line
@@ -169,7 +169,7 @@ public class AudioDeviceInventory {
            if (ads != null) {
            if (ads != null) {
                if (ads.getAudioDeviceCategory() != category) {
                if (ads.getAudioDeviceCategory() != category) {
                    ads.setAudioDeviceCategory(category);
                    ads.setAudioDeviceCategory(category);
                    mDeviceBroker.postUpdatedAdiDeviceState(ads);
                    mDeviceBroker.postUpdatedAdiDeviceState(ads, false /*initSA*/);
                    mDeviceBroker.postPersistAudioDeviceSettings();
                    mDeviceBroker.postPersistAudioDeviceSettings();
                }
                }
                mDeviceBroker.postSynchronizeAdiDevicesInInventory(ads);
                mDeviceBroker.postSynchronizeAdiDevicesInInventory(ads);
@@ -182,7 +182,7 @@ public class AudioDeviceInventory {
            mDeviceInventory.put(ads.getDeviceId(), ads);
            mDeviceInventory.put(ads.getDeviceId(), ads);
            checkDeviceInventorySize_l();
            checkDeviceInventorySize_l();


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


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


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


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


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


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


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


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


    private void doTestConnectionDisconnectionReconnection(int delayAfterDisconnection,
    private void doTestConnectionDisconnectionReconnection(int delayAfterDisconnection,