Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ bool AudioPlayer::openAudioStream() {
return true;
}

bool AudioPlayer::rebuildStream() {
cleanup();
return openAudioStream();
}

bool AudioPlayer::start() {
std::scoped_lock lock(streamMutex_);
if (!isInitialized_.load(std::memory_order_acquire)) {
Expand Down Expand Up @@ -158,10 +163,19 @@ AudioPlayer::onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numF
}

void AudioPlayer::onErrorAfterClose(oboe::AudioStream *stream, oboe::Result error) {
if (error != oboe::Result::ErrorDisconnected || driverMutex_ == nullptr) {
if (driverMutex_ == nullptr) {
return;
}

switch (error) {
case oboe::Result::ErrorDisconnected:
case oboe::Result::ErrorTimeout:
case oboe::Result::ErrorInternal:
break;
default:
return;
}

// Serialize with start()/resume()/suspend()/close() on the JS / promise-pool threads.
std::scoped_lock lock(*driverMutex_, streamMutex_);

Expand All @@ -176,9 +190,18 @@ void AudioPlayer::onErrorAfterClose(oboe::AudioStream *stream, oboe::Result erro
return;
}

cleanup();
if (openAudioStream()) {
resume();
// Check if the stream was expected to be running when the error occurred
const bool wasRunning = isRunning_.load(std::memory_order_acquire);

if (!rebuildStream()) {
isRunning_.store(false, std::memory_order_release);
return;
}

Comment on lines +194 to +200

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If rebuildStream fails, isRunning flag may stay true.

// Restart the stream if it was expected to be running when the error occurred
if (wasRunning && mStream_ != nullptr) {
const bool started = mStream_->requestStart() == oboe::Result::OK;
isRunning_.store(started, std::memory_order_release);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class AudioPlayer : public CommonPlayer,
std::weak_ptr<AudioContext> context_;

bool openAudioStream();
bool rebuildStream();
};

} // namespace audioapi