Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioRecord.java

Issue 1323243012: Avoids crashes in Java-based InitRecording() (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 } 138 }
139 139
140 private int InitRecording(int sampleRate, int channels) { 140 private int InitRecording(int sampleRate, int channels) {
141 Logd("InitRecording(sampleRate=" + sampleRate + ", channels=" + 141 Logd("InitRecording(sampleRate=" + sampleRate + ", channels=" +
142 channels + ")"); 142 channels + ")");
143 if (!WebRtcAudioUtils.hasPermission( 143 if (!WebRtcAudioUtils.hasPermission(
144 context, android.Manifest.permission.RECORD_AUDIO)) { 144 context, android.Manifest.permission.RECORD_AUDIO)) {
145 Loge("RECORD_AUDIO permission is missing"); 145 Loge("RECORD_AUDIO permission is missing");
146 return -1; 146 return -1;
147 } 147 }
148 if (audioRecord != null) {
149 Loge("InitRecording() called twice without StopRecording()");
150 return -1;
151 }
152 if (aec != null) {
magjed_webrtc 2015/09/10 07:23:08 When can this ever happen? Doesn't |aec| have the
henrika_webrtc 2015/09/10 11:27:07 You are correct. They are correlated. Should be en
153 aec.release();
154 aec = null;
155 }
148 final int bytesPerFrame = channels * (BITS_PER_SAMPLE / 8); 156 final int bytesPerFrame = channels * (BITS_PER_SAMPLE / 8);
149 final int framesPerBuffer = sampleRate / BUFFERS_PER_SECOND; 157 final int framesPerBuffer = sampleRate / BUFFERS_PER_SECOND;
150 byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer); 158 byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer);
151 Logd("byteBuffer.capacity: " + byteBuffer.capacity()); 159 Logd("byteBuffer.capacity: " + byteBuffer.capacity());
152 // Rather than passing the ByteBuffer with every callback (requiring 160 // Rather than passing the ByteBuffer with every callback (requiring
153 // the potentially expensive GetDirectBufferAddress) we simply have the 161 // the potentially expensive GetDirectBufferAddress) we simply have the
154 // the native class cache the address to the memory once. 162 // the native class cache the address to the memory once.
155 nativeCacheDirectBufferAddress(byteBuffer, nativeAudioRecord); 163 nativeCacheDirectBufferAddress(byteBuffer, nativeAudioRecord);
156 164
157 // Get the minimum buffer size required for the successful creation of 165 // Get the minimum buffer size required for the successful creation of
158 // an AudioRecord object, in byte units. 166 // an AudioRecord object, in byte units.
159 // Note that this size doesn't guarantee a smooth recording under load. 167 // Note that this size doesn't guarantee a smooth recording under load.
160 // TODO(henrika): Do we need to make this larger to avoid underruns? 168 // TODO(henrika): Do we need to make this larger to avoid underruns?
161 int minBufferSize = AudioRecord.getMinBufferSize( 169 int minBufferSize = AudioRecord.getMinBufferSize(
162 sampleRate, 170 sampleRate,
163 AudioFormat.CHANNEL_IN_MONO, 171 AudioFormat.CHANNEL_IN_MONO,
164 AudioFormat.ENCODING_PCM_16BIT); 172 AudioFormat.ENCODING_PCM_16BIT);
165 Logd("AudioRecord.getMinBufferSize: " + minBufferSize); 173 Logd("AudioRecord.getMinBufferSize: " + minBufferSize);
166 174
167 if (aec != null) {
168 aec.release();
169 aec = null;
170 }
171 assertTrue(audioRecord == null);
172 175
173 int bufferSizeInBytes = Math.max(byteBuffer.capacity(), minBufferSize); 176 int bufferSizeInBytes = Math.max(byteBuffer.capacity(), minBufferSize);
174 Logd("bufferSizeInBytes: " + bufferSizeInBytes); 177 Logd("bufferSizeInBytes: " + bufferSizeInBytes);
175 try { 178 try {
176 audioRecord = new AudioRecord(AudioSource.VOICE_COMMUNICATION, 179 audioRecord = new AudioRecord(AudioSource.VOICE_COMMUNICATION,
177 sampleRate, 180 sampleRate,
178 AudioFormat.CHANNEL_IN_MONO, 181 AudioFormat.CHANNEL_IN_MONO,
179 AudioFormat.ENCODING_PCM_16BIT, 182 AudioFormat.ENCODING_PCM_16BIT,
180 bufferSizeInBytes); 183 bufferSizeInBytes);
181 184
182 } catch (IllegalArgumentException e) { 185 } catch (IllegalArgumentException e) {
183 Logd(e.getMessage()); 186 Loge(e.getMessage());
184 return -1; 187 return -1;
185 } 188 }
186 assertTrue(audioRecord.getState() == AudioRecord.STATE_INITIALIZED); 189 if (audioRecord == null ||
190 audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
191 Loge("Failed to create a new AudioRecord instance");
192 return -1;
193 }
187 194
188 Logd("AudioRecord " + 195 Logd("AudioRecord " +
189 "session ID: " + audioRecord.getAudioSessionId() + ", " + 196 "session ID: " + audioRecord.getAudioSessionId() + ", " +
190 "audio format: " + audioRecord.getAudioFormat() + ", " + 197 "audio format: " + audioRecord.getAudioFormat() + ", " +
191 "channels: " + audioRecord.getChannelCount() + ", " + 198 "channels: " + audioRecord.getChannelCount() + ", " +
192 "sample rate: " + audioRecord.getSampleRate()); 199 "sample rate: " + audioRecord.getSampleRate());
193 Logd("AcousticEchoCanceler.isAvailable: " + builtInAECIsAvailable()); 200 Logd("AcousticEchoCanceler.isAvailable: " + builtInAECIsAvailable());
194 if (!builtInAECIsAvailable()) { 201 if (!builtInAECIsAvailable()) {
195 return framesPerBuffer; 202 return framesPerBuffer;
196 } 203 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 280
274 private static void Loge(String msg) { 281 private static void Loge(String msg) {
275 Log.e(TAG, msg); 282 Log.e(TAG, msg);
276 } 283 }
277 284
278 private native void nativeCacheDirectBufferAddress( 285 private native void nativeCacheDirectBufferAddress(
279 ByteBuffer byteBuffer, long nativeAudioRecord); 286 ByteBuffer byteBuffer, long nativeAudioRecord);
280 287
281 private native void nativeDataIsRecorded(int bytes, long nativeAudioRecord); 288 private native void nativeDataIsRecorded(int bytes, long nativeAudioRecord);
282 } 289 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698