blob: f1f35b32e86b444c960c9b1a5b8385fe713405ee [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
Andy Hungad2faf72023-07-13 20:00:50 -070018#pragma once
Vlad Popab042ee62022-10-20 18:05:00 +020019
Andy Hungba8b63b2023-07-18 18:55:52 -070020#include "IAfPatchPanel.h"
21#include "PatchCommandThread.h"
22
Andy Hung954b9712023-08-28 18:36:53 -070023#include <audio_utils/mutex.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020024#include <sounddose/SoundDoseManager.h>
Andy Hungba8b63b2023-07-18 18:55:52 -070025
Vlad Popa2900c0a2022-10-24 13:38:00 +020026#include <unordered_map>
Vlad Popab042ee62022-10-20 18:05:00 +020027
Andy Hungad2faf72023-07-13 20:00:50 -070028namespace android {
29
Vlad Popab042ee62022-10-20 18:05:00 +020030constexpr static int kMaxTimestampDeltaInSec = 120;
31
Andy Hungb60a2c82023-07-17 14:02:52 -070032class IAfMelReporterCallback : public virtual RefBase {
33public:
Andy Hung0b2608c2023-08-31 15:30:19 -070034 virtual audio_utils::mutex& mutex() const
35 RETURN_CAPABILITY(audio_utils::AudioFlinger_Mutex) = 0;
Andy Hungb60a2c82023-07-17 14:02:52 -070036 virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0;
Andy Hung0b2608c2023-08-31 15:30:19 -070037 virtual sp<IAfThreadBase> checkOutputThread_l(audio_io_handle_t ioHandle) const
38 REQUIRES(mutex()) = 0;
Andy Hungb60a2c82023-07-17 14:02:52 -070039};
40
Vlad Popab042ee62022-10-20 18:05:00 +020041/**
42 * Class for listening to new patches and starting the MEL computation. MelReporter is
43 * concealed within AudioFlinger, their lifetimes are the same.
44 */
Vlad Popaf79f6ba2023-07-27 18:27:59 -070045class MelReporter : public PatchCommandThread::PatchCommandListener,
46 public IMelReporterCallback {
Vlad Popab042ee62022-10-20 18:05:00 +020047public:
Andy Hungb60a2c82023-07-17 14:02:52 -070048 explicit MelReporter(const sp<IAfMelReporterCallback>& afMelReporterCallback)
Vlad Popaf79f6ba2023-07-27 18:27:59 -070049 : mAfMelReporterCallback(afMelReporterCallback) {}
Vlad Popab042ee62022-10-20 18:05:00 +020050
Vlad Popa1d5f0d52022-12-18 12:21:26 +010051 void onFirstRef() override;
Vlad Popab042ee62022-10-20 18:05:00 +020052
Vlad Popa1d5f0d52022-12-18 12:21:26 +010053 /**
54 * Activates the MEL reporting from the HAL sound dose interface. If the HAL
55 * does not support the sound dose interface for this module, the internal MEL
56 * calculation will be use.
57 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010058 * <p>If the device is using the audio AIDL HAL then this method will try to get the sound
59 * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used
60 * this method will be looking for the standalone sound dose implementation. It falls back to
61 * the internal MEL computation if no valid sound dose interface can be retrieved.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010062 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010063 * @return true if the MEL reporting will be done from any sound dose HAL interface
64 * implementation, false otherwise.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010065 */
Vlad Popa03bd5bc2023-01-17 16:16:51 +010066 bool activateHalSoundDoseComputation(const std::string& module,
Andy Hung77448a52023-09-06 17:52:01 -070067 const sp<DeviceHalInterface>& device) EXCLUDES_MelReporter_Mutex;
Vlad Popa1d5f0d52022-12-18 12:21:26 +010068
69 /**
70 * Activates the MEL reporting from internal framework values. These are used
71 * as a fallback when there is no sound dose interface implementation from HAL.
72 * Note: the internal CSD computation does not guarantee a certification with
73 * IEC62368-1 3rd edition or EN50332-3
74 */
Andy Hung77448a52023-09-06 17:52:01 -070075 void activateInternalSoundDoseComputation() EXCLUDES_MelReporter_Mutex;
Vlad Popab042ee62022-10-20 18:05:00 +020076
Vlad Popae3fd1c22022-11-07 21:03:18 +010077 sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback);
Vlad Popa63f047e2022-11-05 14:09:19 +010078
Vlad Popab042ee62022-10-20 18:05:00 +020079 std::string dump();
80
Vlad Popaf79f6ba2023-07-27 18:27:59 -070081 // IMelReporterCallback methods
Andy Hung77448a52023-09-06 17:52:01 -070082 void stopMelComputationForDeviceId(audio_port_handle_t deviceId) final
83 EXCLUDES_MelReporter_Mutex;
84 void startMelComputationForDeviceId(audio_port_handle_t deviceId) final
85 EXCLUDES_MelReporter_Mutex;
Vlad Popaf79f6ba2023-07-27 18:27:59 -070086
Vlad Popab042ee62022-10-20 18:05:00 +020087 // PatchCommandListener methods
88 void onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hung77448a52023-09-06 17:52:01 -070089 const IAfPatchPanel::Patch& patch) final
90 EXCLUDES_AudioFlinger_Mutex;
91 void onReleaseAudioPatch(audio_patch_handle_t handle) final EXCLUDES_AudioFlinger_Mutex;
François Gaffie58e73af2023-02-15 11:47:24 +010092 void onUpdateAudioPatch(audio_patch_handle_t oldHandle,
93 audio_patch_handle_t newHandle,
Andy Hung77448a52023-09-06 17:52:01 -070094 const IAfPatchPanel::Patch& patch) final EXCLUDES_AudioFlinger_Mutex;
Vlad Popab042ee62022-10-20 18:05:00 +020095
Vlad Popa7e81cea2023-01-19 16:34:16 +010096 /**
97 * The new metadata can determine whether we should compute MEL for the given thread.
98 * This is the case only if one of the tracks in the thread mix is using MEDIA or GAME.
99 * Otherwise, this method will disable CSD.
100 **/
101 void updateMetadataForCsd(audio_io_handle_t streamHandle,
Andy Hung77448a52023-09-06 17:52:01 -0700102 const std::vector<playback_track_metadata_v7_t>& metadataVec)
103 EXCLUDES_AudioFlinger_Mutex;
104
Shraddha Basantwani94851202024-05-14 11:40:55 +0000105 void resetReferencesForTest();
106
Vlad Popab042ee62022-10-20 18:05:00 +0200107private:
Vlad Popa7e81cea2023-01-19 16:34:16 +0100108 struct ActiveMelPatch {
109 audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE};
Vlad Popaf79f6ba2023-07-27 18:27:59 -0700110 /**
111 * Stores device ids and whether they are compatible for CSD calculation.
112 * The boolean value can change since BT audio device types are user-configurable
113 * to headphones/headsets or other device types.
114 */
115 std::vector<std::pair<audio_port_handle_t,bool>> deviceStates;
Vlad Popa7e81cea2023-01-19 16:34:16 +0100116 bool csdActive;
117 };
Vlad Popab042ee62022-10-20 18:05:00 +0200118
Vlad Popa7e81cea2023-01-19 16:34:16 +0100119 void stopInternalMelComputation();
Andy Hung77448a52023-09-06 17:52:01 -0700120 audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::MelReporter_Mutex) {
121 return mMutex;
122 }
Vlad Popa7e81cea2023-01-19 16:34:16 +0100123
Andy Hung0169fbc2023-08-28 19:12:14 -0700124 /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
125 void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());
Vlad Popa7e81cea2023-01-19 16:34:16 +0100126
Andy Hung0169fbc2023-08-28 19:12:14 -0700127 /** Should be called with the following order of locks: mAudioFlinger.mutex() -> mutex(). */
128 void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mutex());
Vlad Popa7e81cea2023-01-19 16:34:16 +0100129
Andy Hung920f6572022-10-06 12:09:49 -0700130 std::optional<audio_patch_handle_t>
Andy Hung0169fbc2023-08-28 19:12:14 -0700131 activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mutex());
Vlad Popa7e81cea2023-01-19 16:34:16 +0100132
Andy Hung0169fbc2023-08-28 19:12:14 -0700133 bool useHalSoundDoseInterface_l() REQUIRES(mutex());
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100134
Shraddha Basantwani94851202024-05-14 11:40:55 +0000135 sp<IAfMelReporterCallback> mAfMelReporterCallback;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100136
Andy Hung0169fbc2023-08-28 19:12:14 -0700137 /* const */ sp<SoundDoseManager> mSoundDoseManager; // set onFirstRef
Vlad Popab042ee62022-10-20 18:05:00 +0200138
Vlad Popab042ee62022-10-20 18:05:00 +0200139 /**
140 * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
Andy Hung0169fbc2023-08-28 19:12:14 -0700141 * Locking order AudioFlinger::mutex() -> PatchCommandThread::mutex() -> MelReporter::mutex().
Vlad Popab042ee62022-10-20 18:05:00 +0200142 */
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700143 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kMelReporter_Mutex};
Andy Hung0169fbc2023-08-28 19:12:14 -0700144 std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches
145 GUARDED_BY(mutex());
146 std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mutex());
147 bool mUseHalSoundDoseInterface GUARDED_BY(mutex()) = false;
Vlad Popab042ee62022-10-20 18:05:00 +0200148};
Andy Hungad2faf72023-07-13 20:00:50 -0700149
150} // namespace android