AudioFlinger: Fix haptic race condition
Test: compiles
Bug: 131715193
Change-Id: I57f136903b9ecc40ecd10642c0bfaad0134495f4
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index fd29f31..5c6f353 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -3268,6 +3268,7 @@
cpuStats.sample(myName);
Vector< sp<EffectChain> > effectChains;
+ audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE;
// If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency.
//
@@ -3541,6 +3542,19 @@
// during mixing and effect process as the audio buffers could be deleted
// or modified if an effect is created or deleted
lockEffectChains_l(effectChains);
+
+ // Determine which session to pick up haptic data.
+ // This must be done under the same lock as prepareTracks_l().
+ // TODO: Write haptic data directly to sink buffer when mixing.
+ if (mHapticChannelCount > 0 && effectChains.size() > 0) {
+ for (const auto& track : mActiveTracks) {
+ if (track->getHapticPlaybackEnabled()) {
+ activeHapticSessionId = track->sessionId();
+ break;
+ }
+ }
+ }
+
} // mLock scope ends
if (mBytesRemaining == 0) {
@@ -3613,20 +3627,11 @@
// only process effects if we're going to write
if (mSleepTimeUs == 0 && mType != OFFLOAD) {
- audio_session_t activeHapticId = AUDIO_SESSION_NONE;
- if (mHapticChannelCount > 0 && effectChains.size() > 0) {
- for (auto track : mActiveTracks) {
- if (track->getHapticPlaybackEnabled()) {
- activeHapticId = track->sessionId();
- break;
- }
- }
- }
for (size_t i = 0; i < effectChains.size(); i ++) {
effectChains[i]->process_l();
// TODO: Write haptic data directly to sink buffer when mixing.
- if (activeHapticId != AUDIO_SESSION_NONE
- && activeHapticId == effectChains[i]->sessionId()) {
+ if (activeHapticSessionId != AUDIO_SESSION_NONE
+ && activeHapticSessionId == effectChains[i]->sessionId()) {
// Haptic data is active in this case, copy it directly from
// in buffer to out buffer.
const size_t audioBufferSize = mNormalFrameCount