CSD: report the new csd values to AudioService

We are reporting with every new callback from the MelProcessor the
values that lead to a change of at least 1% of CSD.

Test: manual, logs
Bug: 257238734
Change-Id: I3f0c39c9fda62bfccd3401e065586ca8621627ca
diff --git a/services/audioflinger/sounddose/SoundDoseManager.cpp b/services/audioflinger/sounddose/SoundDoseManager.cpp
index 86fb3a4..474d0ce 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.cpp
+++ b/services/audioflinger/sounddose/SoundDoseManager.cpp
@@ -21,9 +21,10 @@
 #include "SoundDoseManager.h"
 
 #include <android-base/stringprintf.h>
-#include <cinttypes>
-#include <utils/Log.h>
 #include <time.h>
+#include <utils/Log.h>
+#include <cinttypes>
+#include "android/media/SoundDoseRecord.h"
 
 namespace android {
 
@@ -41,38 +42,27 @@
 }  // namespace
 
 sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
-        audio_port_handle_t deviceId,
-        audio_io_handle_t streamHandle,
-        uint32_t sampleRate,
-        size_t channelCount,
-        audio_format_t format)
-{
+        audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate,
+        size_t channelCount, audio_format_t format) {
     std::lock_guard _l(mLock);
 
     auto streamProcessor = mActiveProcessors.find(streamHandle);
     sp<audio_utils::MelProcessor> processor;
-    if (streamProcessor != mActiveProcessors.end()
-            && (processor = streamProcessor->second.promote())) {
+    if (streamProcessor != mActiveProcessors.end() &&
+            (processor = streamProcessor->second.promote())) {
         ALOGV("%s: found callback for stream %d", __func__, streamHandle);
         processor->setDeviceId(deviceId);
         return processor;
     } else {
         ALOGV("%s: creating new callback for device %d", __func__, streamHandle);
-        sp<audio_utils::MelProcessor> melProcessor =
-            sp<audio_utils::MelProcessor>::make(sampleRate,
-                                                channelCount,
-                                                format,
-                                                *this,
-                                                deviceId,
-                                                mRs2Value);
+        sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make(
+                sampleRate, channelCount, format, *this, deviceId, mRs2Value);
         mActiveProcessors[streamHandle] = melProcessor;
         return melProcessor;
     }
 }
 
-
-void SoundDoseManager::setOutputRs2(float rs2Value)
-{
+void SoundDoseManager::setOutputRs2(float rs2Value) {
     ALOGV("%s", __func__);
     std::lock_guard _l(mLock);
 
@@ -81,48 +71,60 @@
         if (processor != nullptr) {
             status_t result = processor->setOutputRs2(rs2Value);
             if (result != NO_ERROR) {
-                ALOGW("%s: could not set RS2 value %f for stream %d",
-                      __func__,
-                      rs2Value,
+                ALOGW("%s: could not set RS2 value %f for stream %d", __func__, rs2Value,
                       streamProcessor.first);
             }
         }
     }
 }
 
-void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle)
-{
+void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) {
     std::lock_guard _l(mLock);
     auto callbackToRemove = mActiveProcessors.find(streamHandle);
-    if(callbackToRemove != mActiveProcessors.end()) {
+    if (callbackToRemove != mActiveProcessors.end()) {
         mActiveProcessors.erase(callbackToRemove);
     }
 }
 
-void SoundDoseManager::onNewMelValues(const std::vector<float>& mels,
-                                      size_t offset,
-                                      size_t length,
-                                      audio_port_handle_t deviceId) const
-{
+void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
+                                      audio_port_handle_t deviceId) const {
     ALOGV("%s", __func__);
-    std::lock_guard _l(mLock);
 
-    int64_t timestampSec = getMonotonicSecond();
+    sp<media::ISoundDoseCallback> soundDoseCallback;
+    std::vector<audio_utils::CsdRecord> records;
+    float currentCsd;
+    {
+        std::lock_guard _l(mLock);
 
-    // only for internal callbacks
-    mMelAggregator->aggregateAndAddNewMelRecord(
-        audio_utils::MelRecord(deviceId, std::vector<float>(
-                                   mels.begin() + offset,
-                                   mels.begin() + offset + length),
-                               timestampSec - length));
+        int64_t timestampSec = getMonotonicSecond();
+
+        // only for internal callbacks
+        records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord(
+                deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length),
+                timestampSec - length));
+
+        currentCsd = mMelAggregator->getCsd();
+    }
+
+    soundDoseCallback = getSoundDoseCallback();
+
+    if (records.size() > 0 && soundDoseCallback != nullptr) {
+        std::vector<media::SoundDoseRecord> newRecordsToReport;
+        for (const auto& record : records) {
+            newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record));
+        }
+
+        soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport);
+    }
 }
 
-void SoundDoseManager::onMomentaryExposure(float currentMel,
-                                           audio_port_handle_t deviceId) const {
-    ALOGV("%s: Momentary exposure for device %d triggered: %f MEL",
-          __func__,
-          deviceId,
-          currentMel);
+sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
+    std::lock_guard _l(mLock);
+    return mSoundDoseCallback;
+}
+
+void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const {
+    ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);
 
     sp<media::ISoundDoseCallback> soundDoseCallback;
     {
@@ -142,15 +144,12 @@
     mSoundDoseCallback = callback;
 }
 
-std::string SoundDoseManager::dump() const
-{
+std::string SoundDoseManager::dump() const {
     std::string output;
     mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) {
         base::StringAppendF(&output,
                             "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]",
-                            csdRecord.value,
-                            csdRecord.averageMel,
-                            csdRecord.timestamp,
+                            csdRecord.value, csdRecord.averageMel, csdRecord.timestamp,
                             csdRecord.timestamp + csdRecord.duration);
         base::StringAppendF(&output, "\n");
     });
@@ -173,4 +172,14 @@
     return mMelAggregator->getCachedMelRecordsSize();
 }
 
+media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord(
+        const audio_utils::CsdRecord& legacy) {
+    media::SoundDoseRecord soundDoseRecord{};
+    soundDoseRecord.timestamp = legacy.timestamp;
+    soundDoseRecord.duration = legacy.duration;
+    soundDoseRecord.value = legacy.value;
+    soundDoseRecord.averageMel = legacy.averageMel;
+    return soundDoseRecord;
+}
+
 }  // namespace android