From ac8d5164f0187d21da54905ed1143e6b04f5a7ab Mon Sep 17 00:00:00 2001 From: henrika Date: Thu, 15 Dec 2016 15:42:56 +0100 Subject: [PATCH] Improves release of allocated audio resources on Android. BUG=webrtc:6890 R=magjed@webrtc.org, solenberg@webrtc.org Review-Url: https://codereview.webrtc.org/2574053003 . Cr-Commit-Position: refs/heads/master@{#15637} --- .../webrtc/voiceengine/WebRtcAudioRecord.java | 13 ++++++++++-- .../webrtc/voiceengine/WebRtcAudioTrack.java | 20 +++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java index 136cce45e0..23b2c60c9c 100644 --- a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java +++ b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java @@ -189,10 +189,12 @@ public class WebRtcAudioRecord { AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes); } catch (IllegalArgumentException e) { Logging.e(TAG, e.getMessage()); + releaseAudioResources(); return -1; } if (audioRecord == null || audioRecord.getState() != AudioRecord.STATE_INITIALIZED) { Logging.e(TAG, "Failed to create a new AudioRecord instance"); + releaseAudioResources(); return -1; } if (effects != null) { @@ -233,8 +235,7 @@ public class WebRtcAudioRecord { if (effects != null) { effects.release(); } - audioRecord.release(); - audioRecord = null; + releaseAudioResources(); return true; } @@ -274,4 +275,12 @@ public class WebRtcAudioRecord { Logging.w(TAG, "setMicrophoneMute(" + mute + ")"); microphoneMute = mute; } + + // Releases the native AudioRecord resources. + private void releaseAudioResources() { + if (audioRecord != null) { + audioRecord.release(); + audioRecord = null; + } + } } diff --git a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java index 5ea647a44b..7c3a672576 100644 --- a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java +++ b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioTrack.java @@ -77,6 +77,7 @@ public class WebRtcAudioTrack { assertTrue(audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING); } catch (IllegalStateException e) { Logging.e(TAG, "AudioTrack.play failed: " + e.getMessage()); + releaseAudioResources(); return; } @@ -202,14 +203,16 @@ public class WebRtcAudioTrack { AudioFormat.ENCODING_PCM_16BIT, minBufferSizeInBytes, AudioTrack.MODE_STREAM); } catch (IllegalArgumentException e) { Logging.d(TAG, e.getMessage()); + releaseAudioResources(); return false; } // It can happen that an AudioTrack is created but it was not successfully // initialized upon creation. Seems to be the case e.g. when the maximum // number of globally available audio tracks is exceeded. - if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) { + if (audioTrack == null || audioTrack.getState() != AudioTrack.STATE_INITIALIZED) { Logging.e(TAG, "Initialization of audio track failed."); + releaseAudioResources(); return false; } logMainParameters(); @@ -222,7 +225,7 @@ public class WebRtcAudioTrack { assertTrue(audioTrack != null); assertTrue(audioThread == null); if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) { - Logging.e(TAG, "Audio track is not successfully initialized."); + Logging.e(TAG, "AudioTrack instance is not successfully initialized."); return false; } audioThread = new AudioTrackThread("AudioTrackJavaThread"); @@ -236,10 +239,7 @@ public class WebRtcAudioTrack { logUnderrunCount(); audioThread.joinThread(); audioThread = null; - if (audioTrack != null) { - audioTrack.release(); - audioTrack = null; - } + releaseAudioResources(); return true; } @@ -330,4 +330,12 @@ public class WebRtcAudioTrack { Logging.w(TAG, "setSpeakerMute(" + mute + ")"); speakerMute = mute; } + + // Releases the native AudioTrack resources. + private void releaseAudioResources() { + if (audioTrack != null) { + audioTrack.release(); + audioTrack = null; + } + } }