Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.

Commit 42caedb

Browse files
committed
Restore the state when OpenSLESRecorder::CreateAudioRecorder fails
OpenSLESRecorder will be inconsistent if CreateAudioRecorder fails. For example, it is possible that recorder_object_ is set and simple_buffer_queue_ is not set at the same time. In that case, OpenSLESRecorder::DestroyAudioRecorder considers it audio recorder is created, and tries to use simple_buffer_queue_ to reset the state, which leads to segmentation fault. Restore the state when OpenSLESRecorder::CreateAudioRecorder fails to avoid such a complication.
1 parent aae7661 commit 42caedb

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

modules/audio_device/android/opensles_recorder.cc

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,60 +250,73 @@ bool OpenSLESRecorder::CreateAudioRecorder() {
250250
const SLInterfaceID interface_id[] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
251251
SL_IID_ANDROIDCONFIGURATION};
252252
const SLboolean interface_required[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
253+
SLAndroidConfigurationItf recorder_config;
254+
SLint32 stream_type = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
255+
253256
if (LOG_ON_ERROR((*engine_)->CreateAudioRecorder(
254257
engine_, recorder_object_.Receive(), &audio_source, &audio_sink,
255258
arraysize(interface_id), interface_id, interface_required))) {
256-
return false;
259+
goto fail;
257260
}
258261

259262
// Configure the audio recorder (before it is realized).
260-
SLAndroidConfigurationItf recorder_config;
261263
if (LOG_ON_ERROR((recorder_object_->GetInterface(recorder_object_.Get(),
262264
SL_IID_ANDROIDCONFIGURATION,
263265
&recorder_config)))) {
264-
return false;
266+
goto fail_recorder_object;
265267
}
266268

267269
// Uses the default microphone tuned for audio communication.
268270
// Note that, SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION leads to a fast
269271
// track but also excludes usage of required effects like AEC, AGC and NS.
270272
// SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION
271-
SLint32 stream_type = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
272273
if (LOG_ON_ERROR(((*recorder_config)
273274
->SetConfiguration(recorder_config,
274275
SL_ANDROID_KEY_RECORDING_PRESET,
275276
&stream_type, sizeof(SLint32))))) {
276-
return false;
277+
goto fail_recorder_object;
277278
}
278279

279280
// The audio recorder can now be realized (in synchronous mode).
280281
if (LOG_ON_ERROR((recorder_object_->Realize(recorder_object_.Get(),
281282
SL_BOOLEAN_FALSE)))) {
282-
return false;
283+
goto fail_recorder_object;
283284
}
284285

285286
// Get the implicit recorder interface (SL_IID_RECORD).
286287
if (LOG_ON_ERROR((recorder_object_->GetInterface(
287288
recorder_object_.Get(), SL_IID_RECORD, &recorder_)))) {
288-
return false;
289+
goto fail_recorder_object;
289290
}
290291

291292
// Get the simple buffer queue interface (SL_IID_ANDROIDSIMPLEBUFFERQUEUE).
292293
// It was explicitly requested.
293294
if (LOG_ON_ERROR((recorder_object_->GetInterface(
294295
recorder_object_.Get(), SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
295296
&simple_buffer_queue_)))) {
296-
return false;
297+
goto fail_recorder;
297298
}
298299

299300
// Register the input callback for the simple buffer queue.
300301
// This callback will be called when receiving new data from the device.
301302
if (LOG_ON_ERROR(((*simple_buffer_queue_)
302303
->RegisterCallback(simple_buffer_queue_,
303304
SimpleBufferQueueCallback, this)))) {
304-
return false;
305+
goto fail_simple_buffer_queue;
305306
}
306307
return true;
308+
309+
fail_simple_buffer_queue:
310+
simple_buffer_queue_ = nullptr;
311+
312+
fail_recorder:
313+
recorder_ = nullptr;
314+
315+
fail_recorder_object:
316+
recorder_object_.Reset();
317+
318+
fail:
319+
return false;
307320
}
308321

309322
void OpenSLESRecorder::DestroyAudioRecorder() {

0 commit comments

Comments
 (0)