blob: fbffd7d24001ab79722abaf580bd9e88e38f11bd [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 Hung81ce7602023-07-13 20:00:50 -070018#pragma once
Vlad Popab042ee62022-10-20 18:05:00 +020019
Vlad Popab042ee62022-10-20 18:05:00 +020020#include <mutex>
Vlad Popa2900c0a2022-10-24 13:38:00 +020021#include <sounddose/SoundDoseManager.h>
22#include <unordered_map>
Vlad Popab042ee62022-10-20 18:05:00 +020023
Andy Hung81ce7602023-07-13 20:00:50 -070024namespace android {
25
Vlad Popab042ee62022-10-20 18:05:00 +020026constexpr static int kMaxTimestampDeltaInSec = 120;
27
Andy Hungf9e248b2023-07-17 14:02:52 -070028class IAfMelReporterCallback : public virtual RefBase {
29public:
30 virtual Mutex& mutex() const = 0;
31 virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0;
32 virtual sp<IAfThreadBase> checkOutputThread_l(audio_io_handle_t ioHandle) const = 0;
33};
34
Vlad Popab042ee62022-10-20 18:05:00 +020035/**
36 * Class for listening to new patches and starting the MEL computation. MelReporter is
37 * concealed within AudioFlinger, their lifetimes are the same.
38 */
Vlad Popa197faf82023-07-27 18:27:59 -070039class MelReporter : public PatchCommandThread::PatchCommandListener,
40 public IMelReporterCallback {
Vlad Popab042ee62022-10-20 18:05:00 +020041public:
Andy Hungf9e248b2023-07-17 14:02:52 -070042 explicit MelReporter(const sp<IAfMelReporterCallback>& afMelReporterCallback)
43 : mAfMelReporterCallback(afMelReporterCallback) {}
Vlad Popab042ee62022-10-20 18:05:00 +020044
Vlad Popa1d5f0d52022-12-18 12:21:26 +010045 void onFirstRef() override;
Vlad Popab042ee62022-10-20 18:05:00 +020046
Vlad Popa1d5f0d52022-12-18 12:21:26 +010047 /**
48 * Activates the MEL reporting from the HAL sound dose interface. If the HAL
49 * does not support the sound dose interface for this module, the internal MEL
50 * calculation will be use.
51 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010052 * <p>If the device is using the audio AIDL HAL then this method will try to get the sound
53 * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used
54 * this method will be looking for the standalone sound dose implementation. It falls back to
55 * the internal MEL computation if no valid sound dose interface can be retrieved.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010056 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010057 * @return true if the MEL reporting will be done from any sound dose HAL interface
58 * implementation, false otherwise.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010059 */
Vlad Popa03bd5bc2023-01-17 16:16:51 +010060 bool activateHalSoundDoseComputation(const std::string& module,
61 const sp<DeviceHalInterface>& device);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010062
63 /**
64 * Activates the MEL reporting from internal framework values. These are used
65 * as a fallback when there is no sound dose interface implementation from HAL.
66 * Note: the internal CSD computation does not guarantee a certification with
67 * IEC62368-1 3rd edition or EN50332-3
68 */
69 void activateInternalSoundDoseComputation();
Vlad Popab042ee62022-10-20 18:05:00 +020070
Vlad Popae3fd1c22022-11-07 21:03:18 +010071 sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback);
Vlad Popa63f047e2022-11-05 14:09:19 +010072
Vlad Popab042ee62022-10-20 18:05:00 +020073 std::string dump();
74
Vlad Popa197faf82023-07-27 18:27:59 -070075 // IMelReporterCallback methods
76 void stopMelComputationForDeviceId(audio_port_handle_t deviceId) override;
77 void startMelComputationForDeviceId(audio_port_handle_t deviceId) override;
78
Vlad Popab042ee62022-10-20 18:05:00 +020079 // PatchCommandListener methods
80 void onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hungc0ab56b2023-07-13 18:11:33 -070081 const IAfPatchPanel::Patch& patch) final;
82 void onReleaseAudioPatch(audio_patch_handle_t handle) final;
Vlad Popab042ee62022-10-20 18:05:00 +020083
Vlad Popa7e81cea2023-01-19 16:34:16 +010084 /**
85 * The new metadata can determine whether we should compute MEL for the given thread.
86 * This is the case only if one of the tracks in the thread mix is using MEDIA or GAME.
87 * Otherwise, this method will disable CSD.
88 **/
89 void updateMetadataForCsd(audio_io_handle_t streamHandle,
90 const std::vector<playback_track_metadata_v7_t>& metadataVec);
Vlad Popab042ee62022-10-20 18:05:00 +020091private:
Vlad Popa7e81cea2023-01-19 16:34:16 +010092 struct ActiveMelPatch {
93 audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE};
Vlad Popa197faf82023-07-27 18:27:59 -070094 /**
95 * Stores device ids and whether they are compatible for CSD calculation.
96 * The boolean value can change since BT audio device types are user-configurable
97 * to headphones/headsets or other device types.
98 */
99 std::vector<std::pair<audio_port_handle_t,bool>> deviceStates;
Vlad Popa7e81cea2023-01-19 16:34:16 +0100100 bool csdActive;
101 };
Vlad Popab042ee62022-10-20 18:05:00 +0200102
Vlad Popa7e81cea2023-01-19 16:34:16 +0100103 void stopInternalMelComputation();
104
105 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -0700106 void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100107
108 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -0700109 void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100110
Andy Hung920f6572022-10-06 12:09:49 -0700111 std::optional<audio_patch_handle_t>
112 activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100113
Vlad Popa69fbbee2023-04-25 12:23:24 +0200114 bool useHalSoundDoseInterface_l() REQUIRES(mLock);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100115
Andy Hungf9e248b2023-07-17 14:02:52 -0700116 const sp<IAfMelReporterCallback> mAfMelReporterCallback;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100117
118 sp<SoundDoseManager> mSoundDoseManager;
Vlad Popab042ee62022-10-20 18:05:00 +0200119
Vlad Popab042ee62022-10-20 18:05:00 +0200120 /**
121 * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
122 * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock.
123 */
124 std::mutex mLock;
Andy Hung81ce7602023-07-13 20:00:50 -0700125 std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches GUARDED_BY(mLock);
126 std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mLock);
127 bool mUseHalSoundDoseInterface GUARDED_BY(mLock) = false;
Vlad Popab042ee62022-10-20 18:05:00 +0200128};
Andy Hung81ce7602023-07-13 20:00:50 -0700129
130} // namespace android