Skip to content
Merged
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
5 changes: 5 additions & 0 deletions android/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ add_subdirectory(
)

find_package(oboe REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)
find_package(fbjni REQUIRED CONFIG)

add_library(tunerengine SHARED
TunerEngineJni.cpp
Expand All @@ -28,4 +30,7 @@ target_link_libraries(tunerengine
oboe::oboe
android
log
ReactAndroid::jsi
ReactAndroid::reactnative
fbjni::fbjni
)
15 changes: 10 additions & 5 deletions android/src/main/cpp/OboeAudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ bool OboeAudioSource::start() {
}

void OboeAudioSource::stop() {
stopped_.store(true, std::memory_order_relaxed);
if (restartThread_.joinable()) {
restartThread_.join();
// Use seq_cst so onErrorAfterClose() on another thread is guaranteed to
// see stopped_=true before we try to join the restart thread.
stopped_.store(true, std::memory_order_seq_cst);
{
std::lock_guard<std::mutex> lock(restartMutex_);
if (restartThread_.joinable()) restartThread_.join();
}
if (stream_) {
stream_->requestStop();
Expand All @@ -90,8 +93,10 @@ oboe::DataCallbackResult OboeAudioSource::onAudioReady(

void OboeAudioSource::onErrorAfterClose(oboe::AudioStream* /*stream*/, oboe::Result error) {
LOGE("Oboe stream error after close: %s — attempting restart", oboe::convertToText(error));
if (stopped_.load(std::memory_order_relaxed)) return;
// Restart on a separate thread to avoid deadlock; join in stop()/destructor
std::lock_guard<std::mutex> lock(restartMutex_);
// seq_cst load pairs with the seq_cst store in stop() — guarantees we
// see stopped_=true if stop() ran before we acquired the mutex.
if (stopped_.load(std::memory_order_seq_cst)) return;
if (restartThread_.joinable()) restartThread_.join();
restartThread_ = std::thread([this]() {
if (!stopped_.load(std::memory_order_relaxed)) {
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/cpp/OboeAudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <functional>
#include <memory>
#include <atomic>
#include <mutex>
#include <thread>

// Oboe-based microphone capture.
Expand Down Expand Up @@ -37,5 +38,6 @@ class OboeAudioSource : public oboe::AudioStreamCallback,
std::shared_ptr<oboe::AudioStream> stream_;
float sampleRate_{48000.0f};
std::atomic<bool> stopped_{false};
std::mutex restartMutex_;
std::thread restartThread_;
};
Loading
Loading