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 | |
| 18 | #ifndef INCLUDING_FROM_AUDIOFLINGER_H |
| 19 | #error This header file should only be included from AudioFlinger.h |
| 20 | #endif |
| 21 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 22 | #include <mutex> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 23 | #include <sounddose/SoundDoseManager.h> |
| 24 | #include <unordered_map> |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 25 | |
| 26 | constexpr static int kMaxTimestampDeltaInSec = 120; |
| 27 | |
| 28 | /** |
| 29 | * Class for listening to new patches and starting the MEL computation. MelReporter is |
| 30 | * concealed within AudioFlinger, their lifetimes are the same. |
| 31 | */ |
| 32 | class MelReporter : public PatchCommandThread::PatchCommandListener { |
| 33 | public: |
| 34 | explicit MelReporter(AudioFlinger& audioFlinger) |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 35 | : mAudioFlinger(audioFlinger), |
| 36 | mSoundDoseManager(sp<SoundDoseManager>::make()) {} |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 37 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 38 | void onFirstRef() override; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 39 | |
| 40 | /** Returns true if we should compute MEL for the given device. */ |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 41 | bool shouldComputeMelForDeviceType(audio_devices_t device); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 42 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 43 | /** |
| 44 | * Activates the MEL reporting from the HAL sound dose interface. If the HAL |
| 45 | * does not support the sound dose interface for this module, the internal MEL |
| 46 | * calculation will be use. |
| 47 | * |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame^] | 48 | * <p>If the device is using the audio AIDL HAL then this method will try to get the sound |
| 49 | * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used |
| 50 | * this method will be looking for the standalone sound dose implementation. It falls back to |
| 51 | * 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] | 52 | * |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame^] | 53 | * @return true if the MEL reporting will be done from any sound dose HAL interface |
| 54 | * implementation, false otherwise. |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 55 | */ |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame^] | 56 | bool activateHalSoundDoseComputation(const std::string& module, |
| 57 | const sp<DeviceHalInterface>& device); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Activates the MEL reporting from internal framework values. These are used |
| 61 | * as a fallback when there is no sound dose interface implementation from HAL. |
| 62 | * Note: the internal CSD computation does not guarantee a certification with |
| 63 | * IEC62368-1 3rd edition or EN50332-3 |
| 64 | */ |
| 65 | void activateInternalSoundDoseComputation(); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 66 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 67 | sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 68 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 69 | std::string dump(); |
| 70 | |
| 71 | // PatchCommandListener methods |
| 72 | void onCreateAudioPatch(audio_patch_handle_t handle, |
| 73 | const PatchPanel::Patch& patch) override; |
| 74 | void onReleaseAudioPatch(audio_patch_handle_t handle) override; |
| 75 | |
| 76 | private: |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 77 | void stopInternalMelComputation(); |
| 78 | void stopInternalMelComputationForStream(audio_io_handle_t streamId); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 79 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 80 | void startMelComputationForNewPatch(audio_io_handle_t streamHandle, |
| 81 | audio_port_handle_t deviceId); |
| 82 | |
| 83 | AudioFlinger& mAudioFlinger; // does not own the object |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 84 | |
| 85 | sp<SoundDoseManager> mSoundDoseManager; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 86 | |
| 87 | struct ActiveMelPatch { |
| 88 | audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE}; |
| 89 | std::vector<audio_port_handle_t> deviceHandles; |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock. |
| 94 | * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock. |
| 95 | */ |
| 96 | std::mutex mLock; |
| 97 | std::unordered_map<audio_patch_handle_t, ActiveMelPatch> |
| 98 | mActiveMelPatches GUARDED_BY(AudioFlinger::MelReporter::mLock); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 99 | bool mUseHalSoundDoseInterface GUARDED_BY(AudioFlinger::MelReporter::mLock) = false; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 100 | }; |