Add helper methods for the sound dose test API

Added methods for:
 * getOutputRs2: returns the currently used RS2 value by the audioserver
 * getCsd: returns the current CSD value from the audioserver
 * forceUseFrameworkMel: will enable/disable the computation of the
   MEL values by the framework/HAL
 * forceComputeCsdOnAllDevice: computes CSD for all output playback
   devices.

Test: TestApi
Bug: 248565894
Change-Id: Id34e7e26e57d47ee8ab5a45d08e46b7a3f298ddb
diff --git a/media/libaudioclient/aidl/android/media/ISoundDose.aidl b/media/libaudioclient/aidl/android/media/ISoundDose.aidl
index f31f091..7310160 100644
--- a/media/libaudioclient/aidl/android/media/ISoundDose.aidl
+++ b/media/libaudioclient/aidl/android/media/ISoundDose.aidl
@@ -22,9 +22,9 @@
  * Interface used to push the sound dose related information from the
  * AudioService#SoundDoseHelper to the audio server
  */
-oneway interface ISoundDose {
+interface ISoundDose {
     /** Set a new RS2 value used for momentary exposure warnings. */
-    void setOutputRs2(float rs2Value);
+    oneway void setOutputRs2(float rs2Value);
 
     /**
      * Resets the native CSD values. This can happen after a crash in the
@@ -33,5 +33,15 @@
      * dosage values and MELs together with their timestamps that lead to this
      * CSD.
      */
-    void resetCsd(float currentCsd, in SoundDoseRecord[] records);
+    oneway void resetCsd(float currentCsd, in SoundDoseRecord[] records);
+
+    /* -------------------------- Test API methods --------------------------
+    /** Get the currently used RS2 value. */
+    float getOutputRs2();
+    /** Get the current CSD from audioserver. */
+    float getCsd();
+    /** Enables/Disables MEL computations from framework. */
+    oneway void forceUseFrameworkMel(boolean useFrameworkMel);
+    /** Enables/Disables the computation of CSD on all devices. */
+    oneway void forceComputeCsdOnAllDevices(boolean computeCsdOnAllDevices);
 }
diff --git a/services/audioflinger/MelReporter.cpp b/services/audioflinger/MelReporter.cpp
index 8cc7eab..b2e8027 100644
--- a/services/audioflinger/MelReporter.cpp
+++ b/services/audioflinger/MelReporter.cpp
@@ -27,6 +27,10 @@
 namespace android {
 
 bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t device) {
+    if (mSoundDoseManager.computeCsdOnAllDevices()) {
+        return true;
+    }
+
     switch (device) {
         case AUDIO_DEVICE_OUT_WIRED_HEADSET:
         case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
diff --git a/services/audioflinger/MelReporter.h b/services/audioflinger/MelReporter.h
index f73b3d6..b1abc59 100644
--- a/services/audioflinger/MelReporter.h
+++ b/services/audioflinger/MelReporter.h
@@ -39,7 +39,7 @@
     }
 
     /** Returns true if we should compute MEL for the given device. */
-    static bool shouldComputeMelForDeviceType(audio_devices_t device);
+    bool shouldComputeMelForDeviceType(audio_devices_t device);
 
     // For now only support internal MelReporting
     [[nodiscard]] bool isHalReportingEnabled() const { return false; }
diff --git a/services/audioflinger/sounddose/SoundDoseManager.cpp b/services/audioflinger/sounddose/SoundDoseManager.cpp
index 46f310c..61f27cb 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.cpp
+++ b/services/audioflinger/sounddose/SoundDoseManager.cpp
@@ -117,6 +117,64 @@
     return binder::Status::ok();
 }
 
+binder::Status SoundDoseManager::SoundDose::getOutputRs2(float* value) {
+    ALOGV("%s", __func__);
+    auto soundDoseManager = mSoundDoseManager.promote();
+    if (soundDoseManager != nullptr) {
+        std::lock_guard _l(soundDoseManager->mLock);
+        *value = soundDoseManager->mRs2Value;
+    }
+    return binder::Status::ok();
+}
+
+binder::Status SoundDoseManager::SoundDose::getCsd(float* value) {
+    ALOGV("%s", __func__);
+    auto soundDoseManager = mSoundDoseManager.promote();
+    if (soundDoseManager != nullptr) {
+        *value = soundDoseManager->mMelAggregator->getCsd();
+    }
+    return binder::Status::ok();
+}
+
+binder::Status SoundDoseManager::SoundDose::forceUseFrameworkMel(bool useFrameworkMel) {
+    ALOGV("%s", __func__);
+    auto soundDoseManager = mSoundDoseManager.promote();
+    if (soundDoseManager != nullptr) {
+        soundDoseManager->setUseFrameworkMel(useFrameworkMel);
+    }
+    return binder::Status::ok();
+}
+
+binder::Status SoundDoseManager::SoundDose::forceComputeCsdOnAllDevices(
+        bool computeCsdOnAllDevices) {
+    ALOGV("%s", __func__);
+    auto soundDoseManager = mSoundDoseManager.promote();
+    if (soundDoseManager != nullptr) {
+        soundDoseManager->setComputeCsdOnAllDevices(computeCsdOnAllDevices);
+    }
+    return binder::Status::ok();
+}
+
+void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) {
+    std::lock_guard _l(mLock);
+    mUseFrameworkMel = useFrameworkMel;
+}
+
+bool SoundDoseManager::useFrameworkMel() const {
+    std::lock_guard _l(mLock);
+    return mUseFrameworkMel;
+}
+
+void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) {
+    std::lock_guard _l(mLock);
+    mComputeCsdOnAllDevices = computeCsdOnAllDevices;
+}
+
+bool SoundDoseManager::computeCsdOnAllDevices() const {
+    std::lock_guard _l(mLock);
+    return mComputeCsdOnAllDevices;
+}
+
 void SoundDoseManager::resetSoundDose() {
     std::lock_guard lock(mLock);
     mSoundDose = nullptr;
diff --git a/services/audioflinger/sounddose/SoundDoseManager.h b/services/audioflinger/sounddose/SoundDoseManager.h
index b0aa5d6..eb5fa49 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.h
+++ b/services/audioflinger/sounddose/SoundDoseManager.h
@@ -82,6 +82,9 @@
 
     // used for testing
     size_t getCachedMelRecordsSize() const;
+    bool useFrameworkMel() const;
+    bool computeCsdOnAllDevices() const;
+
 
     /** Method for converting from audio_utils::CsdRecord to media::SoundDoseRecord. */
     static media::SoundDoseRecord csdRecordToSoundDoseRecord(const audio_utils::CsdRecord& legacy);
@@ -107,6 +110,10 @@
         binder::Status setOutputRs2(float value) override;
         binder::Status resetCsd(float currentCsd,
                                 const std::vector<media::SoundDoseRecord>& records) override;
+        binder::Status getOutputRs2(float* value);
+        binder::Status getCsd(float* value);
+        binder::Status forceUseFrameworkMel(bool useFrameworkMel);
+        binder::Status forceComputeCsdOnAllDevices(bool computeCsdOnAllDevices);
 
         wp<SoundDoseManager> mSoundDoseManager;
         const sp<media::ISoundDoseCallback> mSoundDoseCallback;
@@ -117,7 +124,10 @@
     void resetCsd(float currentCsd, const std::vector<media::SoundDoseRecord>& records);
 
     sp<media::ISoundDoseCallback> getSoundDoseCallback() const;
-    
+
+    void setUseFrameworkMel(bool useFrameworkMel);
+    void setComputeCsdOnAllDevices(bool computeCsdOnAllDevices);
+
     mutable std::mutex mLock;
 
     // no need for lock since MelAggregator is thread-safe
@@ -129,6 +139,9 @@
     float mRs2Value GUARDED_BY(mLock);
 
     sp<SoundDose> mSoundDose GUARDED_BY(mLock);
+
+    bool mUseFrameworkMel GUARDED_BY(mLock);
+    bool mComputeCsdOnAllDevices GUARDED_BY(mLock);
 };
 
 }  // namespace android