Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oboe: improve DataCallbackResult::Stop handling #1351

Merged
merged 3 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/oboe/AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ class AudioStream : public AudioStreamBase {

std::atomic<bool> mDataCallbackEnabled{false};
std::atomic<bool> mErrorCallbackCalled{false};

std::atomic<bool> mStopThreadAllowed{false};
};

/**
Expand Down
12 changes: 4 additions & 8 deletions src/aaudio/AudioStreamAAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,12 @@ DataCallbackResult AudioStreamAAudio::callOnAudioReady(AAudioStream * /*stream*/
LOGE("Oboe callback returned unexpected value = %d", result);
}

if (getSdkVersion() <= __ANDROID_API_P__) {
// Returning Stop caused various problems before S. See #1230
if (OboeGlobals::areWorkaroundsEnabled() && getSdkVersion() <= __ANDROID_API_R__) {
launchStopThread();
if (isMMapUsed()) {
return DataCallbackResult::Stop;
} else {
// Legacy stream <= API_P cannot be restarted after returning Stop.
return DataCallbackResult::Continue;
}
return DataCallbackResult::Continue;
} else {
return DataCallbackResult::Stop; // OK >= API_Q
return DataCallbackResult::Stop; // OK >= API_S
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/common/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void AudioStream::checkScheduler() {
DataCallbackResult AudioStream::fireDataCallback(void *audioData, int32_t numFrames) {
if (!isDataCallbackEnabled()) {
LOGW("AudioStream::%s() called with data callback disabled!", __func__);
return DataCallbackResult::Stop; // We should not be getting called any more.
return DataCallbackResult::Stop; // Should not be getting called
}

DataCallbackResult result;
Expand Down Expand Up @@ -104,6 +104,7 @@ Result AudioStream::waitForStateTransition(StreamState startingState,

Result AudioStream::start(int64_t timeoutNanoseconds)
{
mStopThreadAllowed = true;
philburk marked this conversation as resolved.
Show resolved Hide resolved
Result result = requestStart();
if (result != Result::OK) return result;
if (timeoutNanoseconds <= 0) return result;
Expand Down Expand Up @@ -203,9 +204,12 @@ static void oboe_stop_thread_proc(AudioStream *oboeStream) {
}

void AudioStream::launchStopThread() {
// Stop this stream on a separate thread
std::thread t(oboe_stop_thread_proc, this);
t.detach();
// Prevent multiple stop threads from being launched.
if (mStopThreadAllowed.exchange(false)) {
// Stop this stream on a separate thread
std::thread t(oboe_stop_thread_proc, this);
t.detach();
}
}

} // namespace oboe