CSD: fix race condition when releasing patch

For short streams it can happen that the updateMetadata which
invalidates the active stream is sent before the patch is released. This
leads to a double calling of stopMelComputation for the same stream,
which has an effect of lowering the counter of mActiveDeviceTypes twice.

Also improved the logging for debugging.

Test: logs and dumpsys
Bug: 265928284
Change-Id: I1f1e585c974632d015d318fe616364009cefdd96
Merged-In: I1f1e585c974632d015d318fe616364009cefdd96
(cherry picked from commit 2750604b0c8e5ada62ad45b93153dfd48fe7d2d8)
diff --git a/services/audioflinger/MelReporter.cpp b/services/audioflinger/MelReporter.cpp
index 5dbcb33..cfa02bb 100644
--- a/services/audioflinger/MelReporter.cpp
+++ b/services/audioflinger/MelReporter.cpp
@@ -224,6 +224,11 @@
 }
 
 void AudioFlinger::MelReporter::stopMelComputationForPatch_l(const ActiveMelPatch& patch) {
+    if (!patch.csdActive) {
+        // no need to stop CSD inactive patches
+        return;
+    }
+
     auto thread = mAudioFlinger.checkPlaybackThread_l(patch.streamHandle);
     ALOGV("%s: stop MEL for stream id: %d", __func__, patch.streamHandle);
     for (const auto& deviceId : patch.deviceHandles) {
diff --git a/services/audioflinger/sounddose/SoundDoseManager.cpp b/services/audioflinger/sounddose/SoundDoseManager.cpp
index 3dbe8d9..03a14d0 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.cpp
+++ b/services/audioflinger/sounddose/SoundDoseManager.cpp
@@ -63,7 +63,7 @@
     sp<audio_utils::MelProcessor> processor;
     if (streamProcessor != mActiveProcessors.end() &&
             (processor = streamProcessor->second.promote())) {
-        ALOGV("%s: found callback for stream %d", __func__, streamHandle);
+        ALOGV("%s: found callback for stream id %d", __func__, streamHandle);
         const auto activeTypeIt = mActiveDeviceTypes.find(deviceId);
         if (activeTypeIt != mActiveDeviceTypes.end()) {
             processor->setAttenuation(mMelAttenuationDB[activeTypeIt->second]);
@@ -72,7 +72,7 @@
         processor->setOutputRs2(mRs2Value);
         return processor;
     } else {
-        ALOGV("%s: creating new callback for device %d", __func__, streamHandle);
+        ALOGV("%s: creating new callback for stream id %d", __func__, streamHandle);
         sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make(
                 sampleRate, channelCount, format, *this, deviceId, mRs2Value);
         const auto activeTypeIt = mActiveDeviceTypes.find(deviceId);
@@ -180,7 +180,7 @@
 void SoundDoseManager::mapAddressToDeviceId(const AudioDeviceTypeAddr& adt,
                                             const audio_port_handle_t deviceId) {
     std::lock_guard _l(mLock);
-    ALOGI("%s: map address: %s to device id: %d", __func__, adt.toString().c_str(), deviceId);
+    ALOGI("%s: map address: %d to device id: %d", __func__, adt.mType, deviceId);
     mActiveDevices[adt] = deviceId;
     mActiveDeviceTypes[deviceId] = adt.mType;
 }
@@ -189,8 +189,8 @@
     std::lock_guard _l(mLock);
     for (auto activeDevice = mActiveDevices.begin(); activeDevice != mActiveDevices.end();) {
         if (activeDevice->second == deviceId) {
-            ALOGI("%s: clear mapping addr: %s to deviceId: %d",
-                  __func__, activeDevice->first.toString().c_str(), deviceId);
+            ALOGI("%s: clear mapping type: %d to deviceId: %d",
+                  __func__, activeDevice->first.mType, deviceId);
             activeDevice = mActiveDevices.erase(activeDevice);
             continue;
         }
@@ -331,7 +331,7 @@
 
 void SoundDoseManager::updateAttenuation(float attenuationDB, audio_devices_t deviceType) {
     std::lock_guard _l(mLock);
-    ALOGV("%s: updating MEL processor attenuation for device %d to %f",
+    ALOGV("%s: updating MEL processor attenuation for device type %d to %f",
             __func__, deviceType, attenuationDB);
     mMelAttenuationDB[deviceType] = attenuationDB;
     for (const auto& mp : mActiveProcessors) {
@@ -339,7 +339,7 @@
         if (melProcessor != nullptr) {
             auto deviceId = melProcessor->getDeviceId();
             if (mActiveDeviceTypes[deviceId] == deviceType) {
-                ALOGV("%s: updating MEL processor attenuation for deviceId %d to %f",
+                ALOGV("%s: set attenuation for deviceId %d to %f",
                         __func__, deviceId, attenuationDB);
                 melProcessor->setAttenuation(attenuationDB);
             }