Skip to content

Commit e3482a9

Browse files
PierceLBrooksakien-mga
authored andcommitted
Android: Ensure cleanup of all subobjects in the OpenSL audio driver
1 parent 3e0c10d commit e3482a9

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

platform/android/audio_driver_opensl.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ Error AudioDriverOpenSL::init_input_device() {
268268
}
269269

270270
Error AudioDriverOpenSL::input_start() {
271+
if (recordItf || recordBufferQueueItf) {
272+
return ERR_ALREADY_IN_USE;
273+
}
274+
271275
if (OS::get_singleton()->request_permission("RECORD_AUDIO")) {
272276
return init_input_device();
273277
}
@@ -277,6 +281,10 @@ Error AudioDriverOpenSL::input_start() {
277281
}
278282

279283
Error AudioDriverOpenSL::input_stop() {
284+
if (!recordItf || !recordBufferQueueItf) {
285+
return ERR_CANT_OPEN;
286+
}
287+
280288
SLuint32 state;
281289
SLresult res = (*recordItf)->GetRecordState(recordItf, &state);
282290
ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
@@ -313,13 +321,36 @@ void AudioDriverOpenSL::unlock() {
313321
}
314322

315323
void AudioDriverOpenSL::finish() {
316-
(*sl)->Destroy(sl);
324+
if (recordItf) {
325+
(*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
326+
recordItf = nullptr;
327+
}
328+
if (recorder) {
329+
(*recorder)->Destroy(recorder);
330+
recorder = nullptr;
331+
}
332+
if (playItf) {
333+
(*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
334+
playItf = nullptr;
335+
}
336+
if (player) {
337+
(*player)->Destroy(player);
338+
player = nullptr;
339+
}
340+
if (OutputMix) {
341+
(*OutputMix)->Destroy(OutputMix);
342+
OutputMix = nullptr;
343+
}
344+
if (sl) {
345+
(*sl)->Destroy(sl);
346+
sl = nullptr;
347+
}
317348
}
318349

319350
void AudioDriverOpenSL::set_pause(bool p_pause) {
320351
pause = p_pause;
321352

322-
if (active) {
353+
if (active && playItf) {
323354
if (pause) {
324355
(*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
325356
} else {

platform/android/audio_driver_opensl.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ class AudioDriverOpenSL : public AudioDriver {
5454

5555
Vector<int16_t> rec_buffer;
5656

57-
SLPlayItf playItf;
58-
SLRecordItf recordItf;
59-
SLObjectItf sl;
60-
SLEngineItf EngineItf;
61-
SLObjectItf OutputMix;
62-
SLObjectItf player;
63-
SLObjectItf recorder;
64-
SLAndroidSimpleBufferQueueItf bufferQueueItf;
65-
SLAndroidSimpleBufferQueueItf recordBufferQueueItf;
57+
SLPlayItf playItf = nullptr;
58+
SLRecordItf recordItf = nullptr;
59+
SLObjectItf sl = nullptr;
60+
SLEngineItf EngineItf = nullptr;
61+
SLObjectItf OutputMix = nullptr;
62+
SLObjectItf player = nullptr;
63+
SLObjectItf recorder = nullptr;
64+
SLAndroidSimpleBufferQueueItf bufferQueueItf = nullptr;
65+
SLAndroidSimpleBufferQueueItf recordBufferQueueItf = nullptr;
6666
SLDataSource audioSource;
6767
SLDataFormat_PCM pcm;
6868
SLDataSink audioSink;

0 commit comments

Comments
 (0)