From 4feee6407fdcc69728d7bd2ec9f7b393b26a0fb2 Mon Sep 17 00:00:00 2001 From: Jean-Michel Trivi Date: Thu, 25 Aug 2022 17:51:22 +0000 Subject: [PATCH] AudioService: properties for configuring head tracking default Define new property to indicate whether head tracking for spatial audio is enabled by default. Read property in AudioService and pass it to SpatializerHeper which will use the defalut value every time a new device connects. Fix unit test to account for recent hardening of device compatibility management. Add initialiation test method in SpatializerHelper to ensure we can test settings with devices for binaural and transaural modes. Bug: 242620201 Test: pair BT headset, observe head tracking toggle in UI Test: atest SpatializerHelper Merged-In: Ie3af7a13537d114cb603d3c83e9ca2ba86cd7f67 Change-Id: Ie3af7a13537d114cb603d3c83e9ca2ba86cd7f67 --- core/res/res/values/config.xml | 4 +++ core/res/res/values/symbols.xml | 1 + .../android/server/audio/AudioService.java | 4 ++- .../server/audio/SpatializerHelper.java | 25 ++++++++++++++++--- .../server/audio/SpatializerHelperTest.java | 15 ++++++++--- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index 7d8e06df21858..d689aab96ce94 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -1987,6 +1987,10 @@ STREAM_MUSIC as if it's on TV platform. --> false + + false + true diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 60d847e59368f..e2f29ef5f3260 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -273,6 +273,7 @@ + diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java index f2c50c570ccbe..75bc63ec56bd2 100644 --- a/services/core/java/com/android/server/audio/AudioService.java +++ b/services/core/java/com/android/server/audio/AudioService.java @@ -965,7 +965,9 @@ public class AudioService extends IAudioService.Stub mSfxHelper = new SoundEffectsHelper(mContext); - mSpatializerHelper = new SpatializerHelper(this, mAudioSystem); + final boolean headTrackingDefault = mContext.getResources().getBoolean( + com.android.internal.R.bool.config_spatial_audio_head_tracking_enabled_default); + mSpatializerHelper = new SpatializerHelper(this, mAudioSystem, headTrackingDefault); mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); mHasVibrator = mVibrator == null ? false : mVibrator.hasVibrator(); diff --git a/services/core/java/com/android/server/audio/SpatializerHelper.java b/services/core/java/com/android/server/audio/SpatializerHelper.java index dd44af1b68eea..af9351765054d 100644 --- a/services/core/java/com/android/server/audio/SpatializerHelper.java +++ b/services/core/java/com/android/server/audio/SpatializerHelper.java @@ -170,9 +170,20 @@ public class SpatializerHelper { //------------------------------------------------------ // initialization - SpatializerHelper(@NonNull AudioService mother, @NonNull AudioSystemAdapter asa) { + @SuppressWarnings("StaticAssignmentInConstructor") + SpatializerHelper(@NonNull AudioService mother, @NonNull AudioSystemAdapter asa, + boolean headTrackingEnabledByDefault) { mAudioService = mother; mASA = asa; + // "StaticAssignmentInConstructor" warning is suppressed as the SpatializerHelper being + // constructed here is the factory for SADeviceState, thus SADeviceState and its + // private static field sHeadTrackingEnabledDefault should never be accessed directly. + SADeviceState.sHeadTrackingEnabledDefault = headTrackingEnabledByDefault; + } + + synchronized void initForTest(boolean hasBinaural, boolean hasTransaural) { + mBinauralSupported = hasBinaural; + mTransauralSupported = hasTransaural; } synchronized void init(boolean effectExpected) { @@ -1490,18 +1501,26 @@ public class SpatializerHelper { } /*package*/ static final class SADeviceState { + private static boolean sHeadTrackingEnabledDefault = false; final @AudioDeviceInfo.AudioDeviceType int mDeviceType; final @NonNull String mDeviceAddress; boolean mEnabled = true; // by default, SA is enabled on any device boolean mHasHeadTracker = false; - boolean mHeadTrackerEnabled = true; // by default, if head tracker is present, use it + boolean mHeadTrackerEnabled; static final String SETTING_FIELD_SEPARATOR = ","; static final String SETTING_DEVICE_SEPARATOR_CHAR = "|"; static final String SETTING_DEVICE_SEPARATOR = "\\|"; - SADeviceState(@AudioDeviceInfo.AudioDeviceType int deviceType, @NonNull String address) { + /** + * Constructor + * @param deviceType + * @param address must be non-null for wireless devices + * @throws NullPointerException if a null address is passed for a wireless device + */ + SADeviceState(@AudioDeviceInfo.AudioDeviceType int deviceType, @Nullable String address) { mDeviceType = deviceType; mDeviceAddress = isWireless(deviceType) ? Objects.requireNonNull(address) : ""; + mHeadTrackerEnabled = sHeadTrackingEnabledDefault; } @Override diff --git a/services/tests/servicestests/src/com/android/server/audio/SpatializerHelperTest.java b/services/tests/servicestests/src/com/android/server/audio/SpatializerHelperTest.java index b17c3a18d89e2..428eaff9e5bcc 100644 --- a/services/tests/servicestests/src/com/android/server/audio/SpatializerHelperTest.java +++ b/services/tests/servicestests/src/com/android/server/audio/SpatializerHelperTest.java @@ -55,14 +55,20 @@ public class SpatializerHelperTest { mMockAudioService = mock(AudioService.class); mSpyAudioSystem = spy(new NoOpAudioSystemAdapter()); - mSpatHelper = new SpatializerHelper(mMockAudioService, mSpyAudioSystem); + mSpatHelper = new SpatializerHelper(mMockAudioService, mSpyAudioSystem, + false /*headTrackingEnabledByDefault*/); } + /** + * Test that constructing an SADeviceState instance requires a non-null address for a + * wireless type, but can take null for a non-wireless type; + * @throws Exception + */ @Test public void testSADeviceStateNullAddressCtor() throws Exception { try { - SADeviceState devState = new SADeviceState( - AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, null); + SADeviceState devState = new SADeviceState(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, null); + devState = new SADeviceState(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP, null); Assert.fail(); } catch (NullPointerException e) { } } @@ -88,11 +94,12 @@ public class SpatializerHelperTest { final AudioDeviceAttributes dev1 = new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_SPEAKER, ""); final AudioDeviceAttributes dev2 = - new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, "C3:P0:beep"); + new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, "C3:PO:beep"); final AudioDeviceAttributes dev3 = new AudioDeviceAttributes(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, "R2:D2:bloop"); doNothing().when(mMockAudioService).persistSpatialAudioDeviceSettings(); + mSpatHelper.initForTest(true /*binaural*/, true /*transaural*/); // test with single device mSpatHelper.addCompatibleAudioDevice(dev1);