blob: 005ca96bb47b158c107b5f180d5489082e85c98e [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
Vlad Popa2900c0a2022-10-24 13:38:00 +020023#include <sounddose/SoundDoseManager.h>
Andy Hungba8b63b2023-07-18 18:55:52 -070024
25#include <mutex>
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:
34 virtual Mutex& mutex() const = 0;
35 virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0;
36 virtual sp<IAfThreadBase> checkOutputThread_l(audio_io_handle_t ioHandle) const = 0;
37};
38
Vlad Popab042ee62022-10-20 18:05:00 +020039/**
40 * Class for listening to new patches and starting the MEL computation. MelReporter is
41 * concealed within AudioFlinger, their lifetimes are the same.
42 */
43class MelReporter : public PatchCommandThread::PatchCommandListener {
44public:
Andy Hungb60a2c82023-07-17 14:02:52 -070045 explicit MelReporter(const sp<IAfMelReporterCallback>& afMelReporterCallback)
46 : mAfMelReporterCallback(afMelReporterCallback),
Vlad Popa1d5f0d52022-12-18 12:21:26 +010047 mSoundDoseManager(sp<SoundDoseManager>::make()) {}
Vlad Popab042ee62022-10-20 18:05:00 +020048
Vlad Popa1d5f0d52022-12-18 12:21:26 +010049 void onFirstRef() override;
Vlad Popab042ee62022-10-20 18:05:00 +020050
Vlad Popa1d5f0d52022-12-18 12:21:26 +010051 /**
52 * Activates the MEL reporting from the HAL sound dose interface. If the HAL
53 * does not support the sound dose interface for this module, the internal MEL
54 * calculation will be use.
55 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010056 * <p>If the device is using the audio AIDL HAL then this method will try to get the sound
57 * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used
58 * this method will be looking for the standalone sound dose implementation. It falls back to
59 * the internal MEL computation if no valid sound dose interface can be retrieved.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010060 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010061 * @return true if the MEL reporting will be done from any sound dose HAL interface
62 * implementation, false otherwise.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010063 */
Vlad Popa03bd5bc2023-01-17 16:16:51 +010064 bool activateHalSoundDoseComputation(const std::string& module,
65 const sp<DeviceHalInterface>& device);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010066
67 /**
68 * Activates the MEL reporting from internal framework values. These are used
69 * as a fallback when there is no sound dose interface implementation from HAL.
70 * Note: the internal CSD computation does not guarantee a certification with
71 * IEC62368-1 3rd edition or EN50332-3
72 */
73 void activateInternalSoundDoseComputation();
Vlad Popab042ee62022-10-20 18:05:00 +020074
Vlad Popae3fd1c22022-11-07 21:03:18 +010075 sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback);
Vlad Popa63f047e2022-11-05 14:09:19 +010076
Vlad Popab042ee62022-10-20 18:05:00 +020077 std::string dump();
78
79 // PatchCommandListener methods
80 void onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070081 const IAfPatchPanel::Patch& patch) final;
82 void onReleaseAudioPatch(audio_patch_handle_t handle) final;
François Gaffie58e73af2023-02-15 11:47:24 +010083 void onUpdateAudioPatch(audio_patch_handle_t oldHandle,
84 audio_patch_handle_t newHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070085 const IAfPatchPanel::Patch& patch) final;
Vlad Popab042ee62022-10-20 18:05:00 +020086
Vlad Popa7e81cea2023-01-19 16:34:16 +010087 /**
88 * The new metadata can determine whether we should compute MEL for the given thread.
89 * This is the case only if one of the tracks in the thread mix is using MEDIA or GAME.
90 * Otherwise, this method will disable CSD.
91 **/
92 void updateMetadataForCsd(audio_io_handle_t streamHandle,
93 const std::vector<playback_track_metadata_v7_t>& metadataVec);
Vlad Popab042ee62022-10-20 18:05:00 +020094private:
Vlad Popa7e81cea2023-01-19 16:34:16 +010095 struct ActiveMelPatch {
96 audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE};
97 std::vector<audio_port_handle_t> deviceHandles;
98 bool csdActive;
99 };
Vlad Popab042ee62022-10-20 18:05:00 +0200100
Vlad Popa7e81cea2023-01-19 16:34:16 +0100101 /** Returns true if we should compute MEL for the given device. */
102 bool shouldComputeMelForDeviceType(audio_devices_t device);
103
104 void stopInternalMelComputation();
105
106 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -0700107 void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100108
109 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -0700110 void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100111
Andy Hung920f6572022-10-06 12:09:49 -0700112 std::optional<audio_patch_handle_t>
113 activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100114
Vlad Popa69fbbee2023-04-25 12:23:24 +0200115 bool useHalSoundDoseInterface_l() REQUIRES(mLock);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100116
Andy Hungb60a2c82023-07-17 14:02:52 -0700117 const sp<IAfMelReporterCallback> mAfMelReporterCallback;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100118
119 sp<SoundDoseManager> mSoundDoseManager;
Vlad Popab042ee62022-10-20 18:05:00 +0200120
Vlad Popab042ee62022-10-20 18:05:00 +0200121 /**
122 * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
123 * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock.
124 */
125 std::mutex mLock;
Andy Hungad2faf72023-07-13 20:00:50 -0700126 std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches GUARDED_BY(mLock);
127 std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mLock);
128 bool mUseHalSoundDoseInterface GUARDED_BY(mLock) = false;
Vlad Popab042ee62022-10-20 18:05:00 +0200129};
Andy Hungad2faf72023-07-13 20:00:50 -0700130
131} // namespace android