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 | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 35 | : mAudioFlinger(audioFlinger) {} |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 36 | |
| 37 | void onFirstRef() override { |
| 38 | mAudioFlinger.mPatchCommandThread->addListener(this); |
| 39 | } |
| 40 | |
| 41 | /** Returns true if we should compute MEL for the given device. */ |
| 42 | static bool shouldComputeMelForDeviceType(audio_devices_t device); |
| 43 | |
| 44 | // For now only support internal MelReporting |
| 45 | [[nodiscard]] bool isHalReportingEnabled() const { return false; } |
| 46 | |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame^] | 47 | void registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback); |
| 48 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 49 | std::string dump(); |
| 50 | |
| 51 | // PatchCommandListener methods |
| 52 | void onCreateAudioPatch(audio_patch_handle_t handle, |
| 53 | const PatchPanel::Patch& patch) override; |
| 54 | void onReleaseAudioPatch(audio_patch_handle_t handle) override; |
| 55 | |
| 56 | private: |
| 57 | AudioFlinger& mAudioFlinger; // does not own the object |
| 58 | |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 59 | SoundDoseManager mSoundDoseManager; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 60 | |
| 61 | struct ActiveMelPatch { |
| 62 | audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE}; |
| 63 | std::vector<audio_port_handle_t> deviceHandles; |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock. |
| 68 | * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock. |
| 69 | */ |
| 70 | std::mutex mLock; |
| 71 | std::unordered_map<audio_patch_handle_t, ActiveMelPatch> |
| 72 | mActiveMelPatches GUARDED_BY(AudioFlinger::MelReporter::mLock); |
| 73 | }; |