From 4ed3658b7848891c014ba69c4c82693b30cb6e7f Mon Sep 17 00:00:00 2001 From: henrika Date: Thu, 10 Sep 2015 15:18:19 +0200 Subject: [PATCH] Avoids crashes in Java-based InitRecording(). This CL ensures that we return -1 in cases where InitRecording() fails. It ensures that we don't crash applications. BUG=b/22849644 R=magjed@webrtc.org Review URL: https://codereview.webrtc.org/1323243012 . Cr-Commit-Position: refs/heads/master@{#9918} --- .../webrtc/voiceengine/WebRtcAudioRecord.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 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 f81bab3e93..990c3d46b1 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 @@ -145,6 +145,10 @@ class WebRtcAudioRecord { Loge("RECORD_AUDIO permission is missing"); return -1; } + if (audioRecord != null) { + Loge("InitRecording() called twice without StopRecording()"); + return -1; + } final int bytesPerFrame = channels * (BITS_PER_SAMPLE / 8); final int framesPerBuffer = sampleRate / BUFFERS_PER_SECOND; byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer); @@ -164,11 +168,6 @@ class WebRtcAudioRecord { AudioFormat.ENCODING_PCM_16BIT); Logd("AudioRecord.getMinBufferSize: " + minBufferSize); - if (aec != null) { - aec.release(); - aec = null; - } - assertTrue(audioRecord == null); int bufferSizeInBytes = Math.max(byteBuffer.capacity(), minBufferSize); Logd("bufferSizeInBytes: " + bufferSizeInBytes); @@ -180,10 +179,14 @@ class WebRtcAudioRecord { bufferSizeInBytes); } catch (IllegalArgumentException e) { - Logd(e.getMessage()); + Loge(e.getMessage()); + return -1; + } + if (audioRecord == null || + audioRecord.getState() != AudioRecord.STATE_INITIALIZED) { + Loge("Failed to create a new AudioRecord instance"); return -1; } - assertTrue(audioRecord.getState() == AudioRecord.STATE_INITIALIZED); Logd("AudioRecord " + "session ID: " + audioRecord.getAudioSessionId() + ", " +