Skip to content

Commit 3565261

Browse files
committed
Make sure the stream is alive in data callback.
Fixes #2323.
1 parent dc38de3 commit 3565261

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/aaudio/AudioStreamAAudio.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ static aaudio_data_callback_result_t oboe_aaudio_data_callback_proc(
107107
int32_t numFrames) {
108108

109109
AudioStreamAAudio *oboeStream = reinterpret_cast<AudioStreamAAudio*>(userData);
110+
auto [isStreamAlive, sharedStream] =
111+
AAudioStreamCollection::getInstance().getStream(oboeStream);
112+
if (!isStreamAlive) {
113+
// Note that the stream is removed from the collection when close is called. However,
114+
// there can be callback fired until the framework fully close the stream. In that case,
115+
// logging a warning here and quick return to stop the stream.
116+
LOGW("%s data callback while stream is not longer alive", __func__);
117+
return static_cast<aaudio_data_callback_result_t>(DataCallbackResult::Stop);
118+
}
110119
if (oboeStream != nullptr) {
111120
return static_cast<aaudio_data_callback_result_t>(
112121
oboeStream->callOnAudioReady(stream, audioData, numFrames));
@@ -123,6 +132,15 @@ static int32_t oboe_aaudio_partial_data_callback_proc(
123132
void *audioData,
124133
int32_t numFrames) {
125134
AudioStreamAAudio *oboeStream = reinterpret_cast<AudioStreamAAudio*>(userData);
135+
auto [isStreamAlive, sharedStream] =
136+
AAudioStreamCollection::getInstance().getStream(oboeStream);
137+
if (!isStreamAlive) {
138+
// Note that the stream is removed from the collection when close is called. However,
139+
// there can be callback fired until the framework fully close the stream. In that case,
140+
// logging a warning here and quick return to stop the stream.
141+
LOGW("%s data callback while stream is not longer alive", __func__);
142+
return -1;
143+
}
126144
if (oboeStream != nullptr) {
127145
return oboeStream->callOnPartialAudioReady(stream, audioData, numFrames);
128146
} else {
@@ -613,6 +631,7 @@ Result AudioStreamAAudio::release() {
613631
}
614632

615633
Result AudioStreamAAudio::close() {
634+
LOGD("%s", __func__);
616635
// Always remove the stream from the collection before closing it as after closing, the client
617636
// will free the resource of the stream.
618637
AAudioStreamCollection::getInstance().removeStream(this);

0 commit comments

Comments
 (0)