aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndreas Pehrson <[email protected]>2024-06-17 22:25:45 +0200
committerAndreas Pehrson <[email protected]>2024-06-24 11:29:24 +0200
commit1ed53cd69ab9ccd3172f446a697ddd3ece56bc3a (patch)
tree50c84e36cf63119274f2fcb6672cc8b226fe505e
parent220eb1978f8008127b00e58baef119e46e855913 (diff)
downloadcubeb-1ed53cd69ab9ccd3172f446a697ddd3ece56bc3a.tar.gz
cubeb-1ed53cd69ab9ccd3172f446a697ddd3ece56bc3a.zip
Split up reinitialize_stream
-rw-r--r--src/cubeb_aaudio.cpp66
1 files changed, 36 insertions, 30 deletions
diff --git a/src/cubeb_aaudio.cpp b/src/cubeb_aaudio.cpp
index 26f28f8..6176bcb 100644
--- a/src/cubeb_aaudio.cpp
+++ b/src/cubeb_aaudio.cpp
@@ -112,7 +112,7 @@ static int
aaudio_stream_start_locked(cubeb_stream * stm, lock_guard<mutex> & lock);
static void
-reinitialize_stream(cubeb_stream * stm);
+reinitialize_stream_async(cubeb_stream * stm);
enum class stream_state {
INIT = 0,
@@ -740,7 +740,7 @@ aaudio_duplex_data_cb(AAudioStream * astream, void * user_data,
if (in_num_frames == AAUDIO_STREAM_STATE_DISCONNECTED) {
LOG("AAudioStream_read: %s (reinitializing)",
WRAP(AAudio_convertResultToText)(in_num_frames));
- reinitialize_stream(stm);
+ reinitialize_stream_async(stm);
} else {
stm->state.store(stream_state::ERROR);
}
@@ -894,43 +894,49 @@ aaudio_input_data_cb(AAudioStream * astream, void * user_data,
}
static void
-reinitialize_stream(cubeb_stream * stm)
+reinitialize_stream_locked(cubeb_stream * stm, lock_guard<mutex> & lock)
{
- // This cannot be done from within the error callback, bounce to another
- // thread.
- // In this situation, the lock is acquired for the entire duration of the
- // function, so that this reinitialization period is atomic.
- std::thread([stm] {
- lock_guard lock(stm->mutex);
- stream_state state = stm->state.load();
- bool was_playing = state == stream_state::STARTED ||
- state == stream_state::STARTING ||
- state == stream_state::DRAINING;
- int err = aaudio_stream_stop_locked(stm, lock);
- // error ignored.
- aaudio_stream_destroy_locked(stm, lock);
- err = aaudio_stream_init_impl(stm, lock);
+ stream_state state = stm->state.load();
+ bool was_playing = state == stream_state::STARTED ||
+ state == stream_state::STARTING ||
+ state == stream_state::DRAINING;
+ int err = aaudio_stream_stop_locked(stm, lock);
+ // error ignored.
+ aaudio_stream_destroy_locked(stm, lock);
+ err = aaudio_stream_init_impl(stm, lock);
- assert(stm->in_use.load());
+ assert(stm->in_use.load());
+
+ if (err != CUBEB_OK) {
+ aaudio_stream_destroy_locked(stm, lock);
+ LOG("aaudio_stream_init_impl error while reiniting: %s",
+ WRAP(AAudio_convertResultToText)(err));
+ stm->state.store(stream_state::ERROR);
+ return;
+ }
+ if (was_playing) {
+ err = aaudio_stream_start_locked(stm, lock);
if (err != CUBEB_OK) {
aaudio_stream_destroy_locked(stm, lock);
- LOG("aaudio_stream_init_impl error while reiniting: %s",
+ LOG("aaudio_stream_start error while reiniting: %s",
WRAP(AAudio_convertResultToText)(err));
stm->state.store(stream_state::ERROR);
return;
}
+ }
+}
- if (was_playing) {
- err = aaudio_stream_start_locked(stm, lock);
- if (err != CUBEB_OK) {
- aaudio_stream_destroy_locked(stm, lock);
- LOG("aaudio_stream_start error while reiniting: %s",
- WRAP(AAudio_convertResultToText)(err));
- stm->state.store(stream_state::ERROR);
- return;
- }
- }
+static void
+reinitialize_stream_async(cubeb_stream * stm)
+{
+ // This cannot be done from within the error callback, bounce to another
+ // thread.
+ // In this situation, the lock is acquired for the entire duration of the
+ // function, so that this reinitialization period is atomic.
+ std::thread([stm] {
+ lock_guard lock(stm->mutex);
+ reinitialize_stream_locked(stm, lock);
}).detach();
}
@@ -943,7 +949,7 @@ aaudio_error_cb(AAudioStream * astream, void * user_data, aaudio_result_t error)
// Device change -- reinitialize on the new default device.
if (error == AAUDIO_ERROR_DISCONNECTED || error == AAUDIO_ERROR_TIMEOUT) {
LOG("Audio device change, reinitializing stream");
- reinitialize_stream(stm);
+ reinitialize_stream_async(stm);
return;
}