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

Commit 02785e5f authored by AlexandrKozlovskiy's avatar AlexandrKozlovskiy Committed by Michael Bestas
Browse files

FmService: Fix possible memory leak

* When we want to close the app, or recreate audiotrack/fmrecorder,
  we do not free the resources of these objects, so we create new
  objects, while not freeing the old, which causes a memory leak.

Change-Id: I1a59352d541b87a3b8be420d78df35c22a263906
parent 00763e01
Loading
Loading
Loading
Loading
+42 −25
Original line number Diff line number Diff line
@@ -409,18 +409,8 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan

       // need to create new audio record and audio play back track,
       // because input/output device may be changed.
       if (mAudioRecord != null) {
           if (mAudioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
               mAudioRecord.stop();
           }
           mAudioRecord.release();
           mAudioRecord = null;
       }
       if (mAudioTrack != null) {
           mAudioTrack.stop();
           mAudioTrack.release();
           mAudioTrack = null;
       }
       releaseAudioRecorder();
       releaseAudioTrack();
       initAudioRecordSink();

        mIsRender = true;
@@ -723,10 +713,7 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
            if (isRdsSupported()) {
                stopRdsThread();
            }

            if (mWakeLock.isHeld()) {
                mWakeLock.release();
            }
            releaseWakeLock();
            // Remove the notification in the title bar.
            removeNotification();
            return false;
@@ -737,11 +724,7 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
        if (isRdsSupported()) {
            stopRdsThread();
        }

        if (mWakeLock.isHeld()) {
            mWakeLock.release();
        }

        releaseWakeLock();
        // Remove the notification in the title bar.
        removeNotification();
        return true;
@@ -1329,10 +1312,15 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
    // Thread 1: onCreate() or startRender()
    // Thread 2: onAudioPatchListUpdate() or startRender()
    private synchronized void initAudioRecordSink() {
        if (mAudioRecord == null) {
            mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.RADIO_TUNER,
                    SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, RECORD_BUF_SIZE);
        }
        if (mAudioTrack == null) {
            mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, RECORD_BUF_SIZE, AudioTrack.MODE_STREAM);
                    SAMPLE_RATE, CHANNEL_CONFIG,
                    AUDIO_FORMAT, RECORD_BUF_SIZE, AudioTrack.MODE_STREAM);
        }
    }

    private synchronized int createAudioPatch() {
@@ -1442,6 +1430,12 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
        }
    }

    private synchronized void releaseWakeLock() {
        if (mWakeLock.isHeld()) {
            mWakeLock.release();
        }
    }

    private synchronized void releaseAudioPatch() {
        if (mAudioPatch != null) {
            Log.d(TAG, "releaseAudioPatch");
@@ -1452,6 +1446,26 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
        mAudioSink = null;
    }

    private synchronized void releaseAudioTrack() {
        if (mAudioTrack != null) {
            if (mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
                mAudioTrack.stop();
            }
            mAudioTrack.release();
            mAudioTrack = null;
        }
    }

    private synchronized void releaseAudioRecorder() {
        if (mAudioRecord != null) {
            if (mAudioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
                mAudioRecord.stop();
            }
            mAudioRecord.release();
            mAudioRecord = null;
        }
    }

    private void registerFmBroadcastReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(SOUND_POWER_DOWN_MSG);
@@ -1487,7 +1501,10 @@ public class FmService extends Service implements FmRecorder.OnRecorderStateChan
        }
        stopRender();
        exitRenderThread();
        releaseWakeLock();
        releaseAudioPatch();
        releaseAudioRecorder();
        releaseAudioTrack();
        unregisterAudioPortUpdateListener();
        super.onDestroy();
    }