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

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

Merge "AudioService: fix Bluetooth codec sampling rate change" into 24D1-dev

parents 8d5a06f1 bf7f8f93
Loading
Loading
Loading
Loading
+16 −14
Original line number Diff line number Diff line
@@ -1842,13 +1842,14 @@ public class AudioDeviceBroker {
                                                + "received with null profile proxy: "
                                                + btInfo)).printLog(TAG));
                            } else {
                                @AudioSystem.AudioFormatNativeEnumForBtCodec final int codec =
                                final Pair<Integer, Boolean> codecAndChanged =
                                        mBtHelper.getCodecWithFallback(btInfo.mDevice,
                                                btInfo.mProfile, btInfo.mIsLeOutput,
                                                "MSG_L_SET_BT_ACTIVE_DEVICE");
                                mDeviceInventory.onSetBtActiveDevice(btInfo, codec,
                                        (btInfo.mProfile
                                                != BluetoothProfile.LE_AUDIO || btInfo.mIsLeOutput)
                                mDeviceInventory.onSetBtActiveDevice(btInfo,
                                        codecAndChanged.first,
                                        (btInfo.mProfile != BluetoothProfile.LE_AUDIO
                                                || btInfo.mIsLeOutput)
                                            ? mAudioService.getBluetoothContextualVolumeStream()
                                            : AudioSystem.STREAM_DEFAULT);
                                if (btInfo.mProfile == BluetoothProfile.LE_AUDIO
@@ -1884,12 +1885,13 @@ public class AudioDeviceBroker {
                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,
                        final Pair<Integer, Boolean> codecAndChanged =
                                mBtHelper.getCodecWithFallback(
                                    btInfo.mDevice, btInfo.mProfile, btInfo.mIsLeOutput,
                                    "MSG_L_BLUETOOTH_DEVICE_CONFIG_CHANGE");
                        mDeviceInventory.onBluetoothDeviceConfigChange(
                                btInfo, codec, BtHelper.EVENT_DEVICE_CONFIG_CHANGE);
                        mDeviceInventory.onBluetoothDeviceConfigChange(btInfo,
                                codecAndChanged.first, codecAndChanged.second,
                                BtHelper.EVENT_DEVICE_CONFIG_CHANGE);
                    }
                } break;
                case MSG_BROADCAST_AUDIO_BECOMING_NOISY:
+4 −5
Original line number Diff line number Diff line
@@ -864,7 +864,8 @@ public class AudioDeviceInventory {
    @GuardedBy("mDeviceBroker.mDeviceStateLock")
    /*package*/ void onBluetoothDeviceConfigChange(
            @NonNull AudioDeviceBroker.BtDeviceInfo btInfo,
            @AudioSystem.AudioFormatNativeEnumForBtCodec int codec, int event) {
            @AudioSystem.AudioFormatNativeEnumForBtCodec int codec,
            boolean codecChanged, int event) {
        MediaMetrics.Item mmi = new MediaMetrics.Item(mMetricsId
                + "onBluetoothDeviceConfigChange")
                .set(MediaMetrics.Property.EVENT, BtHelper.deviceEventToString(event));
@@ -912,14 +913,12 @@ public class AudioDeviceInventory {


            if (event == BtHelper.EVENT_DEVICE_CONFIG_CHANGE) {
                boolean codecChange = false;
                if (btInfo.mProfile == BluetoothProfile.A2DP
                        || btInfo.mProfile == BluetoothProfile.LE_AUDIO
                        || btInfo.mProfile == BluetoothProfile.LE_AUDIO_BROADCAST) {
                    if (di.mDeviceCodecFormat != codec) {
                    if (codecChanged) {
                        di.mDeviceCodecFormat = codec;
                        mConnectedDevices.replace(key, di);
                        codecChange = true;
                        final int res = mAudioSystem.handleDeviceConfigChange(
                                btInfo.mAudioSystemDevice, address,
                                BtHelper.getName(btDevice), codec);
@@ -943,7 +942,7 @@ public class AudioDeviceInventory {
                        }
                    }
                }
                if (!codecChange) {
                if (!codecChanged) {
                    updateBluetoothPreferredModes_l(btDevice /*connectedDevice*/);
                }
            }
+54 −21
Original line number Diff line number Diff line
@@ -98,9 +98,16 @@ public class BtHelper {

    private @Nullable BluetoothLeAudio mLeAudio;

    private @Nullable BluetoothLeAudioCodecConfig mLeAudioCodecConfig;

    // Reference to BluetoothA2dp to query for AbsoluteVolume.
    private @Nullable BluetoothA2dp mA2dp;

    private @Nullable BluetoothCodecConfig mA2dpCodecConfig;

    private @AudioSystem.AudioFormatNativeEnumForBtCodec
            int mLeAudioBroadcastCodec = AudioSystem.AUDIO_FORMAT_DEFAULT;

    // If absolute volume is supported in AVRCP device
    private boolean mAvrcpAbsVolSupported = false;

@@ -269,12 +276,15 @@ public class BtHelper {
        }
    }

    /*package*/ synchronized @AudioSystem.AudioFormatNativeEnumForBtCodec int getCodec(
    private synchronized Pair<Integer, Boolean> getCodec(
            @NonNull BluetoothDevice device, @AudioService.BtProfile int profile) {

        switch (profile) {
            case BluetoothProfile.A2DP: {
                boolean changed = mA2dpCodecConfig != null;
                if (mA2dp == null) {
                    return AudioSystem.AUDIO_FORMAT_DEFAULT;
                    mA2dpCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
                BluetoothCodecStatus btCodecStatus = null;
                try {
@@ -283,17 +293,24 @@ public class BtHelper {
                    Log.e(TAG, "Exception while getting status of " + device, e);
                }
                if (btCodecStatus == null) {
                    return AudioSystem.AUDIO_FORMAT_DEFAULT;
                    mA2dpCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
                final BluetoothCodecConfig btCodecConfig = btCodecStatus.getCodecConfig();
                if (btCodecConfig == null) {
                    return AudioSystem.AUDIO_FORMAT_DEFAULT;
                    mA2dpCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
                return AudioSystem.bluetoothA2dpCodecToAudioFormat(btCodecConfig.getCodecType());
                changed = !btCodecConfig.equals(mA2dpCodecConfig);
                mA2dpCodecConfig = btCodecConfig;
                return new Pair<>(AudioSystem.bluetoothA2dpCodecToAudioFormat(
                        btCodecConfig.getCodecType()), changed);
            }
            case BluetoothProfile.LE_AUDIO: {
                boolean changed = mLeAudioCodecConfig != null;
                if (mLeAudio == null) {
                    return AudioSystem.AUDIO_FORMAT_DEFAULT;
                    mLeAudioCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
                BluetoothLeAudioCodecStatus btLeCodecStatus = null;
                int groupId = mLeAudio.getGroupId(device);
@@ -303,23 +320,35 @@ public class BtHelper {
                    Log.e(TAG, "Exception while getting status of " + device, e);
                }
                if (btLeCodecStatus == null) {
                    return AudioSystem.AUDIO_FORMAT_DEFAULT;
                    mLeAudioCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
                BluetoothLeAudioCodecConfig btLeCodecConfig =
                        btLeCodecStatus.getOutputCodecConfig();
                if (btLeCodecConfig == null) {
                    return AudioSystem.AUDIO_FORMAT_DEFAULT;
                    mLeAudioCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
                return AudioSystem.bluetoothLeCodecToAudioFormat(btLeCodecConfig.getCodecType());
                changed = !btLeCodecConfig.equals(mLeAudioCodecConfig);
                mLeAudioCodecConfig = btLeCodecConfig;
                return new Pair<>(AudioSystem.bluetoothLeCodecToAudioFormat(
                        btLeCodecConfig.getCodecType()), changed);
            }
            case BluetoothProfile.LE_AUDIO_BROADCAST: {
                // We assume LC3 for LE Audio broadcast codec as there is no API to get the codec
                // config on LE Broadcast profile proxy.
                boolean changed = mLeAudioBroadcastCodec != AudioSystem.AUDIO_FORMAT_LC3;
                mLeAudioBroadcastCodec = AudioSystem.AUDIO_FORMAT_LC3;
                return new Pair<>(mLeAudioBroadcastCodec, changed);
            }
            default:
                return AudioSystem.AUDIO_FORMAT_DEFAULT;
                return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, false);
        }
    }

    /*package*/ synchronized @AudioSystem.AudioFormatNativeEnumForBtCodec
            int getCodecWithFallback(
                    @NonNull BluetoothDevice device, @AudioService.BtProfile int profile,
    /*package*/ synchronized Pair<Integer, Boolean>
                    getCodecWithFallback(@NonNull BluetoothDevice device,
                                         @AudioService.BtProfile int profile,
                                         boolean isLeOutput, @NonNull String source) {
        // For profiles other than A2DP and LE Audio output, the audio codec format must be
        // AUDIO_FORMAT_DEFAULT as native audio policy manager expects a specific audio format
@@ -327,18 +356,18 @@ public class BtHelper {
        if (!(profile == BluetoothProfile.A2DP
                || (isLeOutput && ((profile == BluetoothProfile.LE_AUDIO)
                        || (profile == BluetoothProfile.LE_AUDIO_BROADCAST))))) {
            return AudioSystem.AUDIO_FORMAT_DEFAULT;
            return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, false);
        }
        @AudioSystem.AudioFormatNativeEnumForBtCodec int codec =
        Pair<Integer, Boolean> codecAndChanged =
                getCodec(device, profile);
        if (codec == AudioSystem.AUDIO_FORMAT_DEFAULT) {
        if (codecAndChanged.first == AudioSystem.AUDIO_FORMAT_DEFAULT) {
            AudioService.sDeviceLogger.enqueue(new EventLogger.StringEvent(
                    "getCodec DEFAULT from " + source + " fallback to "
                            + (profile == BluetoothProfile.A2DP ? "SBC" : "LC3")));
            return profile == BluetoothProfile.A2DP
                    ? AudioSystem.AUDIO_FORMAT_SBC : AudioSystem.AUDIO_FORMAT_LC3;
            return new Pair<>(profile == BluetoothProfile.A2DP
                    ? AudioSystem.AUDIO_FORMAT_SBC : AudioSystem.AUDIO_FORMAT_LC3, true);
        }
        return codec;
        return codecAndChanged;
    }

    // @GuardedBy("mDeviceBroker.mSetModeLock")
@@ -543,15 +572,19 @@ public class BtHelper {
                break;
            case BluetoothProfile.A2DP:
                mA2dp = null;
                mA2dpCodecConfig = null;
                break;
            case BluetoothProfile.HEARING_AID:
                mHearingAid = null;
                break;
            case BluetoothProfile.LE_AUDIO:
                mLeAudio = null;
                mLeAudioCodecConfig = null;
                break;
            case BluetoothProfile.A2DP_SINK:
            case BluetoothProfile.LE_AUDIO_BROADCAST:
                mLeAudioBroadcastCodec = AudioSystem.AUDIO_FORMAT_DEFAULT;
                break;
            case BluetoothProfile.A2DP_SINK:
                // nothing to do in BtHelper
                break;
            default: