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

Commit 408b0e08 authored by Pavlin Radoslavov's avatar Pavlin Radoslavov
Browse files

Add new internal API: enableOptionalCodecs()/disableOptionalCodecs()

This API can be used to enable the optional codecs, or disable them
and use only the mandatory SBC.
Internally, it is implemented by raising the SBC priority to
highest (so SBC will be used/selected), or reducing the SBC priority
to its default value (lowest).

Test: A2DP streaming and enabling/disabling/selecting optional codecs
Bug: 35873828
Change-Id: I3f0dfc2e965e89647c03d032089dcccbd5fb8ffc
(cherry picked from commit 4316545f)
parent 7d53addf
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -237,6 +237,18 @@ public class A2dpService extends ProfileService {
        mStateMachine.setCodecConfigPreference(codecConfig);
    }

    public void enableOptionalCodecs() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (DBG) Log.d(TAG, "enableOptionalCodecs()");
        mStateMachine.enableOptionalCodecs();
    }

    public void disableOptionalCodecs() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (DBG) Log.d(TAG, "disableOptionalCodecs()");
        mStateMachine.disableOptionalCodecs();
    }

    //Binder object: Must be static class or memory leak may occur 
    private static class BluetoothA2dpBinder extends IBluetoothA2dp.Stub 
        implements IProfileServiceBinder {
@@ -340,6 +352,18 @@ public class A2dpService extends ProfileService {
            if (service == null) return;
            service.setCodecConfigPreference(codecConfig);
        }

        public void enableOptionalCodecs() {
            A2dpService service = getService();
            if (service == null) return;
            service.enableOptionalCodecs();
        }

        public void disableOptionalCodecs() {
            A2dpService service = getService();
            if (service == null) return;
            service.disableOptionalCodecs();
        }
    };

    @Override
+57 −17
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@ final class A2dpStateMachine extends StateMachine {
        mService = svc;
        mContext = context;
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mCodecConfigPriorities = assignCodecConfigPriorities(context.getResources());
        mCodecConfigPriorities = assignCodecConfigPriorities();

        initNative(mCodecConfigPriorities);

@@ -155,7 +155,8 @@ final class A2dpStateMachine extends StateMachine {
    }

    // Assign the A2DP Source codec config priorities
    private BluetoothCodecConfig[] assignCodecConfigPriorities(Resources resources) {
    private BluetoothCodecConfig[] assignCodecConfigPriorities() {
        Resources resources = mContext.getResources();
        if (resources == null) {
            return null;
        }
@@ -215,29 +216,34 @@ final class A2dpStateMachine extends StateMachine {
        BluetoothCodecConfig[] codecConfigArray =
                new BluetoothCodecConfig[BluetoothCodecConfig.SOURCE_CODEC_TYPE_MAX];
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                mA2dpSourceCodecPrioritySbc, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPrioritySbc, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[0] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
                mA2dpSourceCodecPriorityAac, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityAac, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[1] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX,
                mA2dpSourceCodecPriorityAptx, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityAptx, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[2] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD,
                mA2dpSourceCodecPriorityAptxHd, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityAptxHd, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[3] = codecConfig;
        codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC,
                mA2dpSourceCodecPriorityLdac, 0 /* sampleRate */, 0 /* bitsPerSample */,
                0 /* channelMode */, 0 /* codecSpecific1 */, 0 /* codecSpecific2 */,
                0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
                mA2dpSourceCodecPriorityLdac, BluetoothCodecConfig.SAMPLE_RATE_NONE,
                BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig.CHANNEL_MODE_NONE,
                0 /* codecSpecific1 */, 0 /* codecSpecific2 */, 0 /* codecSpecific3 */,
                0 /* codecSpecific4 */);
        codecConfigArray[4] = codecConfig;

        return codecConfigArray;
@@ -803,6 +809,40 @@ final class A2dpStateMachine extends StateMachine {
        setCodecConfigPreferenceNative(codecConfigArray);
    }

    void enableOptionalCodecs() {
        BluetoothCodecConfig[] codecConfigArray = assignCodecConfigPriorities();
        if (codecConfigArray == null) {
            return;
        }

        // Set the mandatory codec's priority to default, and remove the rest
        for (int i = 0; i < codecConfigArray.length; i++) {
            BluetoothCodecConfig codecConfig = codecConfigArray[i];
            if (!codecConfig.isMandatoryCodec()) {
                codecConfigArray[i] = null;
            }
        }

        setCodecConfigPreferenceNative(codecConfigArray);
    }

    void disableOptionalCodecs() {
        BluetoothCodecConfig[] codecConfigArray = assignCodecConfigPriorities();
        if (codecConfigArray == null) {
            return;
        }
        // Set the mandatory codec's priority to highest, and ignore the rest
        for (int i = 0; i < codecConfigArray.length; i++) {
            BluetoothCodecConfig codecConfig = codecConfigArray[i];
            if (codecConfig.isMandatoryCodec()) {
                codecConfig.setCodecPriority(BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST);
            } else {
                codecConfigArray[i] = null;
            }
        }
        setCodecConfigPreferenceNative(codecConfigArray);
    }

    boolean okToConnect(BluetoothDevice device) {
        AdapterService adapterService = AdapterService.getAdapterService();
        int priority = mService.getPriority(device);