blob: acbc8ed64bb9b0e705f295e5d738519102c74795 [file] [log] [blame]
Vlad Popab042ee62022-10-20 18:05:00 +02001/*
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 Popab042ee62022-10-20 18:05:00 +020022#include <mutex>
Vlad Popa2900c0a2022-10-24 13:38:00 +020023#include <sounddose/SoundDoseManager.h>
24#include <unordered_map>
Vlad Popab042ee62022-10-20 18:05:00 +020025
26constexpr 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 */
32class MelReporter : public PatchCommandThread::PatchCommandListener {
33public:
34 explicit MelReporter(AudioFlinger& audioFlinger)
Vlad Popa1d5f0d52022-12-18 12:21:26 +010035 : mAudioFlinger(audioFlinger),
36 mSoundDoseManager(sp<SoundDoseManager>::make()) {}
Vlad Popab042ee62022-10-20 18:05:00 +020037
Vlad Popa1d5f0d52022-12-18 12:21:26 +010038 void onFirstRef() override;
Vlad Popab042ee62022-10-20 18:05:00 +020039
40 /** Returns true if we should compute MEL for the given device. */
Vlad Popa91930462022-12-20 22:42:48 +010041 bool shouldComputeMelForDeviceType(audio_devices_t device);
Vlad Popab042ee62022-10-20 18:05:00 +020042
Vlad Popa1d5f0d52022-12-18 12:21:26 +010043 /**
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 *
48 * For now support internal MelReporting only if the sound dose standalone HAL
49 * is not implemented
50 *
51 * @return true if the MEL reporting will be done from the sound dose HAL
52 * interface
53 */
54 bool activateHalSoundDoseComputation(const std::string& module);
55
56 /**
57 * Activates the MEL reporting from internal framework values. These are used
58 * as a fallback when there is no sound dose interface implementation from HAL.
59 * Note: the internal CSD computation does not guarantee a certification with
60 * IEC62368-1 3rd edition or EN50332-3
61 */
62 void activateInternalSoundDoseComputation();
Vlad Popab042ee62022-10-20 18:05:00 +020063
Vlad Popae3fd1c22022-11-07 21:03:18 +010064 sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback);
Vlad Popa63f047e2022-11-05 14:09:19 +010065
Vlad Popab042ee62022-10-20 18:05:00 +020066 std::string dump();
67
68 // PatchCommandListener methods
69 void onCreateAudioPatch(audio_patch_handle_t handle,
70 const PatchPanel::Patch& patch) override;
71 void onReleaseAudioPatch(audio_patch_handle_t handle) override;
72
73private:
Vlad Popa1d5f0d52022-12-18 12:21:26 +010074 void stopInternalMelComputation();
75 void stopInternalMelComputationForStream(audio_io_handle_t streamId);
Vlad Popab042ee62022-10-20 18:05:00 +020076
Vlad Popa1d5f0d52022-12-18 12:21:26 +010077 void startMelComputationForNewPatch(audio_io_handle_t streamHandle,
78 audio_port_handle_t deviceId);
79
80 AudioFlinger& mAudioFlinger; // does not own the object
81 std::shared_ptr<::aidl::android::hardware::audio::sounddose::ISoundDoseFactory>
82 mSoundDoseFactory;
83
84 sp<SoundDoseManager> mSoundDoseManager;
Vlad Popab042ee62022-10-20 18:05:00 +020085
86 struct ActiveMelPatch {
87 audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE};
88 std::vector<audio_port_handle_t> deviceHandles;
89 };
90
91 /**
92 * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
93 * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock.
94 */
95 std::mutex mLock;
96 std::unordered_map<audio_patch_handle_t, ActiveMelPatch>
97 mActiveMelPatches GUARDED_BY(AudioFlinger::MelReporter::mLock);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010098 bool mUseHalSoundDoseInterface GUARDED_BY(AudioFlinger::MelReporter::mLock) = false;
Vlad Popab042ee62022-10-20 18:05:00 +020099};