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

Commit 85803145 authored by Keith Mok's avatar Keith Mok
Browse files

FmService: Fix crash when plug-in/out headset

There is race condition between the RenderThread
and main thread using the object mAudioTrack and
mAudioRecord. Stop and re-create the RenderThread
when we need to create new instance of mAudioTrack
and mAudioRecord to avoid the race condition.

CYNGNOS-2775 FEIJ-1227

Change-Id: I919010fdcc8aad76f1fc5debe6729b4d2a6954dc
(cherry picked from commit 4da63b53)
parent 04d03f5e
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -389,6 +389,8 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
    private synchronized void startRender() {
        Log.d(TAG, "startRender " + AudioSystem.getForceUse(FOR_PROPRIETARY));

        exitRenderThread();

       // need to create new audio record and audio play back track,
       // because input/output device may be changed.
       if (mAudioRecord != null) {
@@ -404,6 +406,7 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
       initAudioRecordSink();

        mIsRender = true;
        createRenderThread();
        synchronized (mRenderLock) {
            mRenderLock.notify();
        }
@@ -425,8 +428,12 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
    }

    private synchronized void exitRenderThread() {
        stopRender();
        mRenderThread.interrupt();
        try {
            mRenderThread.join();
        } catch (InterruptedException ie) {
            Log.e(TAG, "Failed to join render thread");
        }
        mRenderThread = null;
    }

@@ -1451,6 +1458,7 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
        if (null != mFmRecorder) {
            mFmRecorder = null;
        }
        stopRender();
        exitRenderThread();
        releaseAudioPatch();
        unregisterAudioPortUpdateListener();