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

Commit 8361fdc4 authored by Ram Periathiruvadi's avatar Ram Periathiruvadi Committed by Sanket Agarwal
Browse files

Fixing A2dpSinkStreamingStateMachine thread leak.

A2dpSinkStreamingStateMachine was being created on every Connection
leading to a leak. Fix is to Quit the A2dpSinkStreamingStateMachine
on A2dpSinkStateMachine's Quit() and create a new Streaming
StateMachine only if it is null.
Note that both the Sink and SinkStreaming State machines quit only
on Adapter off, not on BT disconnect.

Bug: b/32097580
Tested: On Mojave and Angler with bat_mas-userdebug

Change-Id: I7f2e3ac01ce5ff965afe407e3605cac00497af36
(cherry picked from commit 595694a05fe2544ce53389aa8c153a036a63f0e0)
parent 3cba0c18
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ import java.util.HashMap;
import java.util.Set;

final class A2dpSinkStateMachine extends StateMachine {
    private static final boolean DBG = true;
    private static final boolean DBG = false;

    static final int CONNECT = 1;
    static final int DISCONNECT = 2;
@@ -120,6 +120,7 @@ final class A2dpSinkStateMachine extends StateMachine {
    private BluetoothDevice mTargetDevice = null;
    private BluetoothDevice mIncomingDevice = null;
    private BluetoothDevice mPlayingDevice = null;
    private A2dpSinkStreamingStateMachine mStreaming = null;

    private final HashMap<BluetoothDevice,BluetoothAudioConfig> mAudioConfigs
            = new HashMap<BluetoothDevice,BluetoothAudioConfig>();
@@ -159,6 +160,15 @@ final class A2dpSinkStateMachine extends StateMachine {
    }

    public void doQuit() {
        if(DBG) {
            Log.d("A2dpSinkStateMachine", "Quit");
        }
        synchronized (A2dpSinkStateMachine.this) {
            if (mStreaming != null) {
                mStreaming.doQuit();
                mStreaming = null;
            }
        }
        quitNow();
    }

@@ -492,14 +502,21 @@ final class A2dpSinkStateMachine extends StateMachine {
    }

    private class Connected extends State {
        private A2dpSinkStreamingStateMachine mStreaming;
        @Override
        public void enter() {
            log("Enter Connected: " + getCurrentMessage().what);
            // Upon connected, the audio starts out as stopped
            broadcastAudioState(mCurrentDevice, BluetoothA2dpSink.STATE_NOT_PLAYING,
                                BluetoothA2dpSink.STATE_PLAYING);
            mStreaming = A2dpSinkStreamingStateMachine.make(A2dpSinkStateMachine.this, mContext);
            synchronized (A2dpSinkStateMachine.this) {
                if (mStreaming == null) {
                    if(DBG) {
                        log("Creating New A2dpSinkStreamingStateMachine");
                    }
                    mStreaming = A2dpSinkStreamingStateMachine.make(A2dpSinkStateMachine.this,
                            mContext);
                }
            }
        }

        @Override
+8 −1
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ import com.android.internal.util.StateMachine;
 *            2. On Audio focus enable streaming in Fluoride.
 */
final class A2dpSinkStreamingStateMachine extends StateMachine {
    private static final boolean DBG = true;
    private static final boolean DBG = false;
    private static final String TAG = "A2dpSinkStreamingStateMachine";
    private static final int ACT_PLAY_NUM_RETRIES = 5;
    private static final int ACT_PLAY_RETRY_DELAY = 2000; // millis.
@@ -158,6 +158,13 @@ final class A2dpSinkStreamingStateMachine extends StateMachine {
        return a2dpStrStateMachine;
    }

    public void doQuit(){
        if(DBG) {
            Log.d(TAG, "doQuit()");
        }
        quitNow();
    }

    /**
     * Utility functions that can be used by all states.
     */