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 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 20 | #include <mutex> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 21 | #include <sounddose/SoundDoseManager.h> |
| 22 | #include <unordered_map> |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 23 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 26 | constexpr static int kMaxTimestampDeltaInSec = 120; |
| 27 | |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame^] | 28 | class IAfMelReporterCallback : public virtual RefBase { |
| 29 | public: |
| 30 | virtual Mutex& mutex() const = 0; |
| 31 | virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0; |
| 32 | virtual sp<IAfThreadBase> checkOutputThread_l(audio_io_handle_t ioHandle) const = 0; |
| 33 | }; |
| 34 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 35 | /** |
| 36 | * Class for listening to new patches and starting the MEL computation. MelReporter is |
| 37 | * concealed within AudioFlinger, their lifetimes are the same. |
| 38 | */ |
| 39 | class MelReporter : public PatchCommandThread::PatchCommandListener { |
| 40 | public: |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame^] | 41 | explicit MelReporter(const sp<IAfMelReporterCallback>& afMelReporterCallback) |
| 42 | : mAfMelReporterCallback(afMelReporterCallback), |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 43 | mSoundDoseManager(sp<SoundDoseManager>::make()) {} |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 44 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 45 | void onFirstRef() override; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 46 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 47 | /** |
| 48 | * Activates the MEL reporting from the HAL sound dose interface. If the HAL |
| 49 | * does not support the sound dose interface for this module, the internal MEL |
| 50 | * calculation will be use. |
| 51 | * |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 52 | * <p>If the device is using the audio AIDL HAL then this method will try to get the sound |
| 53 | * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used |
| 54 | * this method will be looking for the standalone sound dose implementation. It falls back to |
| 55 | * 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] | 56 | * |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 57 | * @return true if the MEL reporting will be done from any sound dose HAL interface |
| 58 | * implementation, false otherwise. |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 59 | */ |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 60 | bool activateHalSoundDoseComputation(const std::string& module, |
| 61 | const sp<DeviceHalInterface>& device); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Activates the MEL reporting from internal framework values. These are used |
| 65 | * as a fallback when there is no sound dose interface implementation from HAL. |
| 66 | * Note: the internal CSD computation does not guarantee a certification with |
| 67 | * IEC62368-1 3rd edition or EN50332-3 |
| 68 | */ |
| 69 | void activateInternalSoundDoseComputation(); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 70 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 71 | sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 72 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 73 | std::string dump(); |
| 74 | |
| 75 | // PatchCommandListener methods |
| 76 | void onCreateAudioPatch(audio_patch_handle_t handle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 77 | const IAfPatchPanel::Patch& patch) final; |
| 78 | void onReleaseAudioPatch(audio_patch_handle_t handle) final; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 79 | void onUpdateAudioPatch(audio_patch_handle_t oldHandle, |
| 80 | audio_patch_handle_t newHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 81 | const IAfPatchPanel::Patch& patch) final; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 82 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 83 | /** |
| 84 | * The new metadata can determine whether we should compute MEL for the given thread. |
| 85 | * This is the case only if one of the tracks in the thread mix is using MEDIA or GAME. |
| 86 | * Otherwise, this method will disable CSD. |
| 87 | **/ |
| 88 | void updateMetadataForCsd(audio_io_handle_t streamHandle, |
| 89 | const std::vector<playback_track_metadata_v7_t>& metadataVec); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 90 | private: |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 91 | struct ActiveMelPatch { |
| 92 | audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE}; |
| 93 | std::vector<audio_port_handle_t> deviceHandles; |
| 94 | bool csdActive; |
| 95 | }; |
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 | /** Returns true if we should compute MEL for the given device. */ |
| 98 | bool shouldComputeMelForDeviceType(audio_devices_t device); |
| 99 | |
| 100 | void stopInternalMelComputation(); |
| 101 | |
| 102 | /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */ |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 103 | void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 104 | |
| 105 | /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */ |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 106 | void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 107 | |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 108 | std::optional<audio_patch_handle_t> |
| 109 | activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mLock); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 110 | |
Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame] | 111 | bool useHalSoundDoseInterface_l() REQUIRES(mLock); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 112 | |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame^] | 113 | const sp<IAfMelReporterCallback> mAfMelReporterCallback; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 114 | |
| 115 | sp<SoundDoseManager> mSoundDoseManager; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 116 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 117 | /** |
| 118 | * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock. |
| 119 | * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock. |
| 120 | */ |
| 121 | std::mutex mLock; |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 122 | std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches GUARDED_BY(mLock); |
| 123 | std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mLock); |
| 124 | bool mUseHalSoundDoseInterface GUARDED_BY(mLock) = false; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 125 | }; |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 126 | |
| 127 | } // namespace android |