blob: e020173cd5b88f7f1a9f1dec1ea44ff8e6e295d6 [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 */
Vlad Popa197faf82023-07-27 18:27:59 -070032class MelReporter : public PatchCommandThread::PatchCommandListener,
33 public IMelReporterCallback {
Vlad Popab042ee62022-10-20 18:05:00 +020034public:
35 explicit MelReporter(AudioFlinger& audioFlinger)
Vlad Popa197faf82023-07-27 18:27:59 -070036 : mAudioFlinger(audioFlinger) {}
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
Vlad Popa1d5f0d52022-12-18 12:21:26 +010040 /**
41 * Activates the MEL reporting from the HAL sound dose interface. If the HAL
42 * does not support the sound dose interface for this module, the internal MEL
43 * calculation will be use.
44 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010045 * <p>If the device is using the audio AIDL HAL then this method will try to get the sound
46 * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used
47 * this method will be looking for the standalone sound dose implementation. It falls back to
48 * the internal MEL computation if no valid sound dose interface can be retrieved.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010049 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010050 * @return true if the MEL reporting will be done from any sound dose HAL interface
51 * implementation, false otherwise.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010052 */
Vlad Popa03bd5bc2023-01-17 16:16:51 +010053 bool activateHalSoundDoseComputation(const std::string& module,
54 const sp<DeviceHalInterface>& device);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010055
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
Vlad Popa197faf82023-07-27 18:27:59 -070068 // IMelReporterCallback methods
69 void stopMelComputationForDeviceId(audio_port_handle_t deviceId) override;
70 void startMelComputationForDeviceId(audio_port_handle_t deviceId) override;
71
Vlad Popab042ee62022-10-20 18:05:00 +020072 // PatchCommandListener methods
73 void onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hungc0ab56b2023-07-13 18:11:33 -070074 const IAfPatchPanel::Patch& patch) final;
75 void onReleaseAudioPatch(audio_patch_handle_t handle) final;
Vlad Popab042ee62022-10-20 18:05:00 +020076
Vlad Popa7e81cea2023-01-19 16:34:16 +010077 /**
78 * The new metadata can determine whether we should compute MEL for the given thread.
79 * This is the case only if one of the tracks in the thread mix is using MEDIA or GAME.
80 * Otherwise, this method will disable CSD.
81 **/
82 void updateMetadataForCsd(audio_io_handle_t streamHandle,
83 const std::vector<playback_track_metadata_v7_t>& metadataVec);
Vlad Popab042ee62022-10-20 18:05:00 +020084private:
Vlad Popa7e81cea2023-01-19 16:34:16 +010085 struct ActiveMelPatch {
86 audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE};
Vlad Popa197faf82023-07-27 18:27:59 -070087 /**
88 * Stores device ids and whether they are compatible for CSD calculation.
89 * The boolean value can change since BT audio device types are user-configurable
90 * to headphones/headsets or other device types.
91 */
92 std::vector<std::pair<audio_port_handle_t,bool>> deviceStates;
Vlad Popa7e81cea2023-01-19 16:34:16 +010093 bool csdActive;
94 };
Vlad Popab042ee62022-10-20 18:05:00 +020095
Vlad Popa7e81cea2023-01-19 16:34:16 +010096 void stopInternalMelComputation();
97
98 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -070099 void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100100
101 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -0700102 void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100103
Andy Hung920f6572022-10-06 12:09:49 -0700104 std::optional<audio_patch_handle_t>
105 activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100106
Vlad Popa69fbbee2023-04-25 12:23:24 +0200107 bool useHalSoundDoseInterface_l() REQUIRES(mLock);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100108
109 AudioFlinger& mAudioFlinger; // does not own the object
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100110
111 sp<SoundDoseManager> mSoundDoseManager;
Vlad Popab042ee62022-10-20 18:05:00 +0200112
Vlad Popab042ee62022-10-20 18:05:00 +0200113 /**
114 * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
115 * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock.
116 */
117 std::mutex mLock;
118 std::unordered_map<audio_patch_handle_t, ActiveMelPatch>
119 mActiveMelPatches GUARDED_BY(AudioFlinger::MelReporter::mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100120 std::unordered_map<audio_port_handle_t, int>
121 mActiveDevices GUARDED_BY(AudioFlinger::MelReporter::mLock);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100122 bool mUseHalSoundDoseInterface GUARDED_BY(AudioFlinger::MelReporter::mLock) = false;
Vlad Popab042ee62022-10-20 18:05:00 +0200123};