Skip to content

Commit

Permalink
opensles: clean up after failed open
Browse files Browse the repository at this point in the history
There were resource leaks if the open failed.
So now it calls close() internally if open fails.

Fixes #1541
  • Loading branch information
philburk committed May 26, 2022
1 parent 220df11 commit e360841
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/opensles/AudioInputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ Result AudioInputStreamOpenSLES::open() {
return Result::OK;

error:
close(); // Clean up various OpenSL objects and prevent resource leaks.
return Result::ErrorInternal; // TODO convert error from SLES to OBOE
}

Expand All @@ -225,7 +226,7 @@ Result AudioInputStreamOpenSLES::close() {
if (getState() == StreamState::Closed){
result = Result::ErrorClosed;
} else {
requestStop_l();
(void) requestStop_l();
// invalidate any interfaces
mRecordInterface = nullptr;
result = AudioStreamOpenSLES::close_l();
Expand All @@ -238,7 +239,7 @@ Result AudioInputStreamOpenSLES::setRecordState_l(SLuint32 newState) {
Result result = Result::OK;

if (mRecordInterface == nullptr) {
LOGE("AudioInputStreamOpenSLES::%s() mRecordInterface is null", __func__);
LOGW("AudioInputStreamOpenSLES::%s() mRecordInterface is null", __func__);
return Result::ErrorInvalidState;
}
SLresult slResult = (*mRecordInterface)->SetRecordState(mRecordInterface, newState);
Expand Down Expand Up @@ -308,6 +309,7 @@ Result AudioInputStreamOpenSLES::requestStop_l() {
case StreamState::Stopping:
case StreamState::Stopped:
return Result::OK;
case StreamState::Uninitialized:
case StreamState::Closed:
return Result::ErrorClosed;
default:
Expand Down
5 changes: 4 additions & 1 deletion src/opensles/AudioOutputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ Result AudioOutputStreamOpenSLES::open() {
return Result::OK;

error:
close(); // Clean up various OpenSL objects and prevent resource leaks.
return Result::ErrorInternal; // TODO convert error from SLES to OBOE
}

Expand All @@ -249,7 +250,7 @@ Result AudioOutputStreamOpenSLES::close() {
if (getState() == StreamState::Closed){
result = Result::ErrorClosed;
} else {
requestPause_l();
(void) requestPause_l();
// invalidate any interfaces
mPlayInterface = nullptr;
result = AudioStreamOpenSLES::close_l();
Expand Down Expand Up @@ -326,6 +327,7 @@ Result AudioOutputStreamOpenSLES::requestPause_l() {
case StreamState::Pausing:
case StreamState::Paused:
return Result::OK;
case StreamState::Uninitialized:
case StreamState::Closed:
return Result::ErrorClosed;
default:
Expand Down Expand Up @@ -383,6 +385,7 @@ Result AudioOutputStreamOpenSLES::requestStop() {
case StreamState::Stopping:
case StreamState::Stopped:
return Result::OK;
case StreamState::Uninitialized:
case StreamState::Closed:
return Result::ErrorClosed;
default:
Expand Down
4 changes: 2 additions & 2 deletions src/opensles/AudioStreamBuffered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ void AudioStreamBuffered::allocateFifo() {
capacityFrames = numBursts * getFramesPerBurst();
}
}
// TODO consider using std::make_unique if we require c++14
mFifoBuffer.reset(new FifoBuffer(getBytesPerFrame(), capacityFrames));

mFifoBuffer = std::make_unique<FifoBuffer>(getBytesPerFrame(), capacityFrames);
mBufferCapacityInFrames = capacityFrames;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/opensles/AudioStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Result AudioStreamOpenSLES::open() {

Result oboeResult = AudioStreamBuffered::open();
if (oboeResult != Result::OK) {
EngineOpenSLES::getInstance().close();
return oboeResult;
}
// Convert to defaults if UNSPECIFIED
Expand Down

0 comments on commit e360841

Please sign in to comment.