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

Commit 0cb82a29 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

leaudio: Improve inactivating LeAudio device

with this patch, we notify Audio framework as soon as possible that
LeAudio group is going to be inactive, without waiting for Native to
acknolege group inactivation.

Bug: 242908683
Test: atest BluetoothInstrumentationTests
Tag: #feature
Change-Id: I426f2a933ddc265e1d55fc89eff4f19bed71a119
parent 77723b5b
Loading
Loading
Loading
Loading
+44 −30
Original line number Diff line number Diff line
@@ -996,6 +996,13 @@ public class LeAudioService extends ProfileService {
            return;
        }
        mLeAudioNativeInterface.groupSetActive(groupId);
        if (groupId == LE_AUDIO_GROUP_ID_INVALID) {
            /* Native will clear its states and send us group Inactive.
             * However we would like to notify audio framework that LeAudio is not
             * active anymore and does not want to get more audio data.
             */
            handleGroupTransitToInactive(currentlyActiveGroupId);
        }
    }

    /**
@@ -1131,6 +1138,41 @@ public class LeAudioService extends ProfileService {
        }
    }

    private void handleGroupTransitToActive(int groupId) {
        synchronized (mGroupLock) {
            LeAudioGroupDescriptor descriptor = getGroupDescriptor(groupId);
            if (descriptor == null || descriptor.mIsActive) {
                Log.e(TAG, "no descriptors for group: " + groupId + " or group already active");
                return;
            }

            descriptor.mIsActive = updateActiveDevices(groupId,
                            ACTIVE_CONTEXTS_NONE, descriptor.mActiveContexts, true);

            if (descriptor.mIsActive) {
                notifyGroupStatusChanged(groupId, LeAudioStackEvent.GROUP_STATUS_ACTIVE);
            }
        }
    }

    private void handleGroupTransitToInactive(int groupId) {
        synchronized (mGroupLock) {
            LeAudioGroupDescriptor descriptor = getGroupDescriptor(groupId);
            if (descriptor == null || !descriptor.mIsActive) {
                Log.e(TAG, "no descriptors for group: " + groupId + " or group already inactive");
                return;
            }

            descriptor.mIsActive = false;
            updateActiveDevices(groupId, descriptor.mActiveContexts,
                    ACTIVE_CONTEXTS_NONE, descriptor.mIsActive);
            /* Clear lost devices */
            if (DBG) Log.d(TAG, "Clear for group: " + groupId);
            clearLostDevicesWhileStreaming(descriptor);
            notifyGroupStatusChanged(groupId, LeAudioStackEvent.GROUP_STATUS_INACTIVE);
        }
    }

    // Suppressed since this is part of a local process
    @SuppressLint("AndroidFrameworkRequiresPermission")
    void messageFromNative(LeAudioStackEvent stackEvent) {
@@ -1286,47 +1328,19 @@ public class LeAudioService extends ProfileService {
        } else if (stackEvent.type == LeAudioStackEvent.EVENT_TYPE_GROUP_STATUS_CHANGED) {
            int groupId = stackEvent.valueInt1;
            int groupStatus = stackEvent.valueInt2;
            boolean notifyGroupStatus = false;

            switch (groupStatus) {
                case LeAudioStackEvent.GROUP_STATUS_ACTIVE: {
                    LeAudioGroupDescriptor descriptor = getGroupDescriptor(groupId);
                    if (descriptor != null) {
                        if (!descriptor.mIsActive) {
                            descriptor.mIsActive = updateActiveDevices(groupId,
                                    ACTIVE_CONTEXTS_NONE, descriptor.mActiveContexts, true);
                            notifyGroupStatus = descriptor.mIsActive;
                        }
                    } else {
                        Log.e(TAG, "no descriptors for group: " + groupId);
                    }
                    handleGroupTransitToActive(groupId);
                    break;
                }
                case LeAudioStackEvent.GROUP_STATUS_INACTIVE: {
                    LeAudioGroupDescriptor descriptor = getGroupDescriptor(groupId);
                    if (descriptor != null) {
                        if (descriptor.mIsActive) {
                            descriptor.mIsActive = false;
                            updateActiveDevices(groupId, descriptor.mActiveContexts,
                                    ACTIVE_CONTEXTS_NONE, descriptor.mIsActive);
                            notifyGroupStatus = true;
                            /* Clear lost devices */
                            if (DBG) Log.d(TAG, "Clear for group: " + groupId);
                            clearLostDevicesWhileStreaming(descriptor);
                        }
                    } else {
                        Log.e(TAG, "no descriptors for group: " + groupId);
                    }
                    handleGroupTransitToInactive(groupId);
                    break;
                }
                default:
                    break;
            }

            if (notifyGroupStatus) {
                notifyGroupStatusChanged(groupId, groupStatus);
            }

        } else if (stackEvent.type == LeAudioStackEvent.EVENT_TYPE_BROADCAST_CREATED) {
            int broadcastId = stackEvent.valueInt1;
            boolean success = stackEvent.valueBool1;