Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2022, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 18 | #pragma once |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 19 | |
Andy Hung | ba8b63b | 2023-07-18 18:55:52 -0700 | [diff] [blame] | 20 | #include "IAfPatchPanel.h" |
| 21 | #include "PatchCommandThread.h" |
| 22 | |
Andy Hung | 954b971 | 2023-08-28 18:36:53 -0700 | [diff] [blame] | 23 | #include <audio_utils/mutex.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 24 | #include <sounddose/SoundDoseManager.h> |
Andy Hung | ba8b63b | 2023-07-18 18:55:52 -0700 | [diff] [blame] | 25 | |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 26 | #include <unordered_map> |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 27 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 30 | class IAfMelReporterCallback : public virtual RefBase { |
| 31 | public: |
Andy Hung | 0b2608c | 2023-08-31 15:30:19 -0700 | [diff] [blame] | 32 | virtual audio_utils::mutex& mutex() const |
| 33 | RETURN_CAPABILITY(audio_utils::AudioFlinger_Mutex) = 0; |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 34 | virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0; |
Andy Hung | 0b2608c | 2023-08-31 15:30:19 -0700 | [diff] [blame] | 35 | virtual sp<IAfThreadBase> checkOutputThread_l(audio_io_handle_t ioHandle) const |
| 36 | REQUIRES(mutex()) = 0; |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 39 | /** |
| 40 | * Class for listening to new patches and starting the MEL computation. MelReporter is |
| 41 | * concealed within AudioFlinger, their lifetimes are the same. |
| 42 | */ |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 43 | class MelReporter : public PatchCommandThread::PatchCommandListener, |
| 44 | public IMelReporterCallback { |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 45 | public: |
Vlad Popa | 1c67ad6 | 2024-01-19 17:53:42 -0800 | [diff] [blame] | 46 | MelReporter(const sp<IAfMelReporterCallback>& afMelReporterCallback, |
| 47 | const sp<IAfPatchPanel>& afPatchPanel) |
| 48 | : mAfMelReporterCallback(afMelReporterCallback), |
| 49 | mAfPatchPanel(afPatchPanel) {} |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 50 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 51 | void onFirstRef() override; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 52 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 53 | /** |
| 54 | * Activates the MEL reporting from the HAL sound dose interface. If the HAL |
| 55 | * does not support the sound dose interface for this module, the internal MEL |
| 56 | * calculation will be use. |
| 57 | * |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 58 | * <p>If the device is using the audio AIDL HAL then this method will try to get the sound |
| 59 | * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used |
| 60 | * this method will be looking for the standalone sound dose implementation. It falls back to |
| 61 | * the internal MEL computation if no valid sound dose interface can be retrieved. |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 62 | * |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 63 | * @return true if the MEL reporting will be done from any sound dose HAL interface |
| 64 | * implementation, false otherwise. |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 65 | */ |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 66 | bool activateHalSoundDoseComputation(const std::string& module, |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 67 | const sp<DeviceHalInterface>& device) EXCLUDES_MelReporter_Mutex; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Activates the MEL reporting from internal framework values. These are used |
| 71 | * as a fallback when there is no sound dose interface implementation from HAL. |
| 72 | * Note: the internal CSD computation does not guarantee a certification with |
| 73 | * IEC62368-1 3rd edition or EN50332-3 |
| 74 | */ |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 75 | void activateInternalSoundDoseComputation() EXCLUDES_MelReporter_Mutex; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 76 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 77 | sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 78 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 79 | std::string dump(); |
| 80 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 81 | // IMelReporterCallback methods |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 82 | void stopMelComputationForDeviceId(audio_port_handle_t deviceId) final |
Vlad Popa | 1c67ad6 | 2024-01-19 17:53:42 -0800 | [diff] [blame] | 83 | EXCLUDES_AudioFlinger_Mutex EXCLUDES_MelReporter_Mutex; |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 84 | void startMelComputationForDeviceId(audio_port_handle_t deviceId) final |
Vlad Popa | 1c67ad6 | 2024-01-19 17:53:42 -0800 | [diff] [blame] | 85 | EXCLUDES_AudioFlinger_Mutex EXCLUDES_MelReporter_Mutex; |
| 86 | void applyAllAudioPatches() final EXCLUDES_AudioFlinger_Mutex EXCLUDES_MelReporter_Mutex; |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 87 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 88 | // PatchCommandListener methods |
| 89 | void onCreateAudioPatch(audio_patch_handle_t handle, |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 90 | const IAfPatchPanel::Patch& patch) final |
| 91 | EXCLUDES_AudioFlinger_Mutex; |
| 92 | void onReleaseAudioPatch(audio_patch_handle_t handle) final EXCLUDES_AudioFlinger_Mutex; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 93 | void onUpdateAudioPatch(audio_patch_handle_t oldHandle, |
| 94 | audio_patch_handle_t newHandle, |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 95 | const IAfPatchPanel::Patch& patch) final EXCLUDES_AudioFlinger_Mutex; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 96 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 97 | /** |
| 98 | * The new metadata can determine whether we should compute MEL for the given thread. |
| 99 | * This is the case only if one of the tracks in the thread mix is using MEDIA or GAME. |
| 100 | * Otherwise, this method will disable CSD. |
| 101 | **/ |
| 102 | void updateMetadataForCsd(audio_io_handle_t streamHandle, |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 103 | const std::vector<playback_track_metadata_v7_t>& metadataVec) |
| 104 | EXCLUDES_AudioFlinger_Mutex; |
| 105 | |
Shraddha Basantwani | fd414a8 | 2024-06-06 16:28:52 +0000 | [diff] [blame^] | 106 | void resetReferencesForTest(); |
| 107 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 108 | private: |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 109 | struct ActiveMelPatch { |
| 110 | audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE}; |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Stores device ids and whether they are compatible for CSD calculation. |
| 113 | * The boolean value can change since BT audio device types are user-configurable |
| 114 | * to headphones/headsets or other device types. |
| 115 | */ |
| 116 | std::vector<std::pair<audio_port_handle_t,bool>> deviceStates; |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 117 | bool csdActive; |
| 118 | }; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 119 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 120 | void stopInternalMelComputation(); |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 121 | audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::MelReporter_Mutex) { |
| 122 | return mMutex; |
| 123 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 124 | |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 125 | /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */ |
| 126 | void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mutex()); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 127 | |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 128 | /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */ |
| 129 | void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mutex()); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 130 | |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 131 | std::optional<audio_patch_handle_t> |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 132 | activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mutex()); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 133 | |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 134 | bool useHalSoundDoseInterface_l() REQUIRES(mutex()); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 135 | |
Shraddha Basantwani | fd414a8 | 2024-06-06 16:28:52 +0000 | [diff] [blame^] | 136 | sp<IAfMelReporterCallback> mAfMelReporterCallback; |
Vlad Popa | 1c67ad6 | 2024-01-19 17:53:42 -0800 | [diff] [blame] | 137 | const sp<IAfPatchPanel> mAfPatchPanel; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 138 | |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 139 | /* const */ sp<SoundDoseManager> mSoundDoseManager; // set onFirstRef |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 140 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 141 | /** |
| 142 | * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock. |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 143 | * Locking order AudioFlinger::mutex() -> PatchCommandThread::mutex() -> MelReporter::mutex(). |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 144 | */ |
Andy Hung | a6f1cbb | 2023-10-12 18:18:13 -0700 | [diff] [blame] | 145 | mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kMelReporter_Mutex}; |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 146 | std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches |
| 147 | GUARDED_BY(mutex()); |
| 148 | std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mutex()); |
| 149 | bool mUseHalSoundDoseInterface GUARDED_BY(mutex()) = false; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 150 | }; |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 151 | |
| 152 | } // namespace android |