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}
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 f81bab3..990c3d4 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 @@
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 @@
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 @@
bufferSizeInBytes);
} catch (IllegalArgumentException e) {
- Logd(e.getMessage());
+ Loge(e.getMessage());
return -1;
}
- assertTrue(audioRecord.getState() == AudioRecord.STATE_INITIALIZED);
+ if (audioRecord == null ||
+ audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
+ Loge("Failed to create a new AudioRecord instance");
+ return -1;
+ }
Logd("AudioRecord " +
"session ID: " + audioRecord.getAudioSessionId() + ", " +