From 75ef37544246e529347bac6ce84ca4a3c8379b44 Mon Sep 17 00:00:00 2001 From: Hiroki KUMAZAKI Date: Thu, 24 Sep 2026 13:10:36 +0900 Subject: [PATCH] android: synchronize AAudio routing state --- internal/oboe/oboe_aaudio_AudioStreamAAudio_android.cpp | 8 +++++++- internal/oboe/oboe_aaudio_AudioStreamAAudio_android.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.cpp b/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.cpp index 8ea6afc..3490ae7 100644 --- a/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.cpp +++ b/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.cpp @@ -976,17 +976,20 @@ StreamState AudioStreamAAudio::getState() { } void AudioStreamAAudio::onRoutingChanged(std::vector deviceIds) { + std::lock_guard lock(mUpdatedDeviceIdsLock); int nextIdx = mUpdatedDeviceIds.idx.load() ^ 1; mUpdatedDeviceIds.deviceIds[nextIdx] = deviceIds; mUpdatedDeviceIds.idx.store(nextIdx); } int32_t AudioStreamAAudio::getDeviceId() const { + std::lock_guard lock(mUpdatedDeviceIdsLock); auto deviceIds = mUpdatedDeviceIds.deviceIds[mUpdatedDeviceIds.idx.load()]; return deviceIds.empty() ? kUnspecified : deviceIds[0]; } std::vector AudioStreamAAudio::getDeviceIds() const { + std::lock_guard lock(mUpdatedDeviceIdsLock); auto deviceIds = mUpdatedDeviceIds.deviceIds[mUpdatedDeviceIds.idx.load()]; return deviceIds; } @@ -1244,7 +1247,10 @@ void AudioStreamAAudio::updateDeviceIds() { mDeviceIds.push_back(deviceIds[i]); } } - mUpdatedDeviceIds.deviceIds[mUpdatedDeviceIds.idx.load()] = mDeviceIds; + { + std::lock_guard lock(mUpdatedDeviceIdsLock); + mUpdatedDeviceIds.deviceIds[mUpdatedDeviceIds.idx.load()] = mDeviceIds; + } // This should not happen in most cases. Please file a bug on Oboe if you see this happening. if (getDeviceIds().empty()) { diff --git a/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.h b/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.h index 45e0f64..91285c3 100644 --- a/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.h +++ b/internal/oboe/oboe_aaudio_AudioStreamAAudio_android.h @@ -212,6 +212,7 @@ class AudioStreamAAudio : public AudioStream { std::atomic idx{0}; std::vector> deviceIds{ {}, {} }; }; + mutable std::mutex mUpdatedDeviceIdsLock; UpdatedDeviceIds mUpdatedDeviceIds; };