CSD: forward the native MEL exposure to AudioService

Added new aidl interface for the communication between the
SoundDoseManager and the SoundDoseHelper. Currently only the momentary
exposure warning is reported.

Test: manual
Bug: 257238734
Change-Id: I61560f81fedd31c30c39d676b7adf0ce087b495c
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index d5100fe..0719838 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -229,7 +229,7 @@
 BINDER_METHOD_ENTRY(setDeviceConnectedState) \
 BINDER_METHOD_ENTRY(setRequestedLatencyMode) \
 BINDER_METHOD_ENTRY(getSupportedLatencyModes) \
-
+BINDER_METHOD_ENTRY(registerSoundDoseCallback) \
 
 // singleton for Binder Method Statistics for IAudioFlinger
 static auto& getIAudioFlingerStatistics() {
@@ -1657,6 +1657,11 @@
     return thread->getSupportedLatencyModes(modes);
 }
 
+status_t AudioFlinger::registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback) {
+    mMelReporter->registerSoundDoseCallback(callback);
+    return NO_ERROR;
+}
+
 status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
 {
     // check calling permissions
@@ -4613,6 +4618,7 @@
         case TransactionCode::SET_MASTER_VOLUME:
         case TransactionCode::SET_MASTER_MUTE:
         case TransactionCode::MASTER_MUTE:
+        case TransactionCode::REGISTER_SOUND_DOSE_CALLBACK:
         case TransactionCode::SET_MODE:
         case TransactionCode::SET_MIC_MUTE:
         case TransactionCode::SET_LOW_RAM_DEVICE:
@@ -4624,7 +4630,7 @@
                 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
                       __func__, code, IPCThreadState::self()->getCallingPid(),
                       IPCThreadState::self()->getCallingUid());
-                // return status only for non void methods
+                // return status only for non-void methods
                 switch (code) {
                     case TransactionCode::SYSTEM_READY:
                         break;
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index d2a033e..a2cde70 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -123,6 +123,7 @@
 class EffectsFactoryHalInterface;
 class FastMixer;
 class IAudioManager;
+class ISoundDoseCallback;
 class PassthruBufferProvider;
 class RecordBufferConverter;
 class ServerProxy;
@@ -305,6 +306,8 @@
     virtual status_t getSupportedLatencyModes(audio_io_handle_t output,
             std::vector<audio_latency_mode_t>* modes);
 
+    virtual status_t registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback);
+
     status_t onTransactWrapper(TransactionCode code, const Parcel& data, uint32_t flags,
         const std::function<status_t()>& delegate) override;
 
diff --git a/services/audioflinger/MelReporter.cpp b/services/audioflinger/MelReporter.cpp
index 6fc756b..279f30d 100644
--- a/services/audioflinger/MelReporter.cpp
+++ b/services/audioflinger/MelReporter.cpp
@@ -20,6 +20,7 @@
 
 #include "AudioFlinger.h"
 
+#include <android/media/ISoundDoseCallback.h>
 #include <audio_utils/power.h>
 #include <utils/Log.h>
 
@@ -107,6 +108,12 @@
     }
 }
 
+void AudioFlinger::MelReporter::registerSoundDoseCallback(
+        const sp<media::ISoundDoseCallback>& callback) {
+    // no need to lock since registerSoundDoseCallback is synchronized
+    mSoundDoseManager.registerSoundDoseCallback(callback);
+}
+
 std::string AudioFlinger::MelReporter::dump() {
     std::lock_guard _l(mLock);
     std::string output("\nSound Dose:\n");
diff --git a/services/audioflinger/MelReporter.h b/services/audioflinger/MelReporter.h
index 8a78f6e..3625a86 100644
--- a/services/audioflinger/MelReporter.h
+++ b/services/audioflinger/MelReporter.h
@@ -44,6 +44,8 @@
     // For now only support internal MelReporting
     [[nodiscard]] bool isHalReportingEnabled() const { return false; }
 
+    void registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback);
+
     std::string dump();
 
     // PatchCommandListener methods
diff --git a/services/audioflinger/sounddose/Android.bp b/services/audioflinger/sounddose/Android.bp
index 5c72fba..6149472 100644
--- a/services/audioflinger/sounddose/Android.bp
+++ b/services/audioflinger/sounddose/Android.bp
@@ -17,6 +17,7 @@
     ],
 
     shared_libs: [
+        "audioflinger-aidl-cpp",
         "libaudioutils",
         "libbase",
         "liblog",
diff --git a/services/audioflinger/sounddose/SoundDoseManager.cpp b/services/audioflinger/sounddose/SoundDoseManager.cpp
index 0a69c52..86fb3a4 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.cpp
+++ b/services/audioflinger/sounddose/SoundDoseManager.cpp
@@ -123,6 +123,23 @@
           __func__,
           deviceId,
           currentMel);
+
+    sp<media::ISoundDoseCallback> soundDoseCallback;
+    {
+        std::lock_guard _l(mLock);
+        soundDoseCallback = mSoundDoseCallback;
+    }
+
+    if (soundDoseCallback != nullptr) {
+        mSoundDoseCallback->onMomentaryExposure(currentMel, deviceId);
+    }
+}
+
+void SoundDoseManager::registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback) {
+    ALOGV("%s: Register ISoundDoseCallback", __func__);
+
+    std::lock_guard _l(mLock);
+    mSoundDoseCallback = callback;
 }
 
 std::string SoundDoseManager::dump() const
diff --git a/services/audioflinger/sounddose/SoundDoseManager.h b/services/audioflinger/sounddose/SoundDoseManager.h
index 5934d2e..754b569 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.h
+++ b/services/audioflinger/sounddose/SoundDoseManager.h
@@ -17,6 +17,7 @@
 
 #pragma once
 
+#include <android/media/ISoundDoseCallback.h>
 #include <audio_utils/MelProcessor.h>
 #include <audio_utils/MelAggregator.h>
 #include <mutex>
@@ -71,6 +72,9 @@
 
     std::string dump() const;
 
+    /** \brief Registers the interface for passing callbacks to the AudioService. */
+    void registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback);
+
     // used for testing
     size_t getCachedMelRecordsSize() const;
 
@@ -91,6 +95,8 @@
                        wp<audio_utils::MelProcessor>> mActiveProcessors GUARDED_BY(mLock);
 
     float mRs2Value GUARDED_BY(mLock);
+
+    sp<media::ISoundDoseCallback> mSoundDoseCallback GUARDED_BY(mLock);
 };
 
 }  // namespace android
diff --git a/services/audioflinger/sounddose/tests/Android.bp b/services/audioflinger/sounddose/tests/Android.bp
index d1779c2..a886663 100644
--- a/services/audioflinger/sounddose/tests/Android.bp
+++ b/services/audioflinger/sounddose/tests/Android.bp
@@ -15,6 +15,7 @@
     ],
 
     shared_libs: [
+        "audioflinger-aidl-cpp",
         "libaudioutils",
         "libbase",
         "liblog",