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
diff --git a/src/com/android/fmradio/FmService.java b/src/com/android/fmradio/FmService.java
index b3d7715..9638208 100644
--- a/src/com/android/fmradio/FmService.java
+++ b/src/com/android/fmradio/FmService.java
@@ -389,6 +389,8 @@
     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 @@
        initAudioRecordSink();
 
         mIsRender = true;
+        createRenderThread();
         synchronized (mRenderLock) {
             mRenderLock.notify();
         }
@@ -425,8 +428,12 @@
     }
 
     private synchronized void exitRenderThread() {
-        stopRender();
         mRenderThread.interrupt();
+        try {
+            mRenderThread.join();
+        } catch (InterruptedException ie) {
+            Log.e(TAG, "Failed to join render thread");
+        }
         mRenderThread = null;
     }
 
@@ -1448,6 +1455,7 @@
         if (null != mFmRecorder) {
             mFmRecorder = null;
         }
+        stopRender();
         exitRenderThread();
         releaseAudioPatch();
         unregisterAudioPortUpdateListener();