Skip to content

Commit

Permalink
When an error happens, wait for the streams to be stopped before disp…
Browse files Browse the repository at this point in the history
…atching the error callback
  • Loading branch information
padenot committed Apr 18, 2023
1 parent ce1dbde commit f721bfe
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/cubeb_aaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ struct AutoInCallback {
cubeb_stream * stm;
};

// Returns when aaudio_stream's state is equal to desired_state
static int
wait_for_state_change(AAudioStream * aaudio_stream,
aaudio_stream_state_t desired_state)
{
aaudio_stream_state_t new_state;
do {
aaudio_result_t res = WRAP(AAudioStream_waitForStateChange)(
aaudio_stream, AAUDIO_STREAM_STATE_UNKNOWN, &new_state, 0);
if (res != AAUDIO_OK) {
LOG("AAudioStream_waitForStateChanged: %s",
WRAP(AAudio_convertResultToText)(res));
return CUBEB_ERROR;
}
} while (new_state != desired_state);

LOG("wait_for_state_change: current state now: %s",
cubeb_AAudio_convertStreamStateToText(new_state));

return CUBEB_OK;
}

// Only allowed from state thread, while mutex on stm is locked
static void
shutdown_with_error(cubeb_stream * stm)
Expand All @@ -203,6 +225,14 @@ shutdown_with_error(cubeb_stream * stm)
WRAP(AAudioStream_requestStop)(stm->ostream);
}

if (stm->istream) {
wait_for_state_change(stm->istream, AAUDIO_STREAM_STATE_STOPPED);
}
if (stm->ostream) {
wait_for_state_change(stm->ostream, AAUDIO_STREAM_STATE_STOPPED);
}

assert(!stm->in_data_callback.load());
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
stm->state.store(stream_state::SHUTDOWN);
}
Expand Down

0 comments on commit f721bfe

Please sign in to comment.