blob: d851e8e3b67bc00e46012757c63538bd6edded83 [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
26#include <mutex>
Vlad Popa2900c0a2022-10-24 13:38:00 +020027#include <unordered_map>
Vlad Popab042ee62022-10-20 18:05:00 +020028
Andy Hungad2faf72023-07-13 20:00:50 -070029namespace android {
30
Vlad Popab042ee62022-10-20 18:05:00 +020031constexpr static int kMaxTimestampDeltaInSec = 120;
32
Andy Hungb60a2c82023-07-17 14:02:52 -070033class IAfMelReporterCallback : public virtual RefBase {
34public:
Andy Hung954b9712023-08-28 18:36:53 -070035 virtual audio_utils::mutex& mutex() const = 0;
Andy Hungb60a2c82023-07-17 14:02:52 -070036 virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0;
37 virtual sp<IAfThreadBase> checkOutputThread_l(audio_io_handle_t ioHandle) const = 0;
38};
39
Vlad Popab042ee62022-10-20 18:05:00 +020040/**
41 * Class for listening to new patches and starting the MEL computation. MelReporter is
42 * concealed within AudioFlinger, their lifetimes are the same.
43 */
Vlad Popaf79f6ba2023-07-27 18:27:59 -070044class MelReporter : public PatchCommandThread::PatchCommandListener,
45 public IMelReporterCallback {
Vlad Popab042ee62022-10-20 18:05:00 +020046public:
Andy Hungb60a2c82023-07-17 14:02:52 -070047 explicit MelReporter(const sp<IAfMelReporterCallback>& afMelReporterCallback)
Vlad Popaf79f6ba2023-07-27 18:27:59 -070048 : mAfMelReporterCallback(afMelReporterCallback) {}
Vlad Popab042ee62022-10-20 18:05:00 +020049
Vlad Popa1d5f0d52022-12-18 12:21:26 +010050 void onFirstRef() override;
Vlad Popab042ee62022-10-20 18:05:00 +020051
Vlad Popa1d5f0d52022-12-18 12:21:26 +010052 /**
53 * Activates the MEL reporting from the HAL sound dose interface. If the HAL
54 * does not support the sound dose interface for this module, the internal MEL
55 * calculation will be use.
56 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010057 * <p>If the device is using the audio AIDL HAL then this method will try to get the sound
58 * dose interface from IModule#getSoundDose(). Otherwise, if the legacy audio HIDL HAL is used
59 * this method will be looking for the standalone sound dose implementation. It falls back to
60 * the internal MEL computation if no valid sound dose interface can be retrieved.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010061 *
Vlad Popa03bd5bc2023-01-17 16:16:51 +010062 * @return true if the MEL reporting will be done from any sound dose HAL interface
63 * implementation, false otherwise.
Vlad Popa1d5f0d52022-12-18 12:21:26 +010064 */
Vlad Popa03bd5bc2023-01-17 16:16:51 +010065 bool activateHalSoundDoseComputation(const std::string& module,
66 const sp<DeviceHalInterface>& device);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010067
68 /**
69 * Activates the MEL reporting from internal framework values. These are used
70 * as a fallback when there is no sound dose interface implementation from HAL.
71 * Note: the internal CSD computation does not guarantee a certification with
72 * IEC62368-1 3rd edition or EN50332-3
73 */
74 void activateInternalSoundDoseComputation();
Vlad Popab042ee62022-10-20 18:05:00 +020075
Vlad Popae3fd1c22022-11-07 21:03:18 +010076 sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback);
Vlad Popa63f047e2022-11-05 14:09:19 +010077
Vlad Popab042ee62022-10-20 18:05:00 +020078 std::string dump();
79
Vlad Popaf79f6ba2023-07-27 18:27:59 -070080 // IMelReporterCallback methods
81 void stopMelComputationForDeviceId(audio_port_handle_t deviceId) override;
82 void startMelComputationForDeviceId(audio_port_handle_t deviceId) override;
83
Vlad Popab042ee62022-10-20 18:05:00 +020084 // PatchCommandListener methods
85 void onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070086 const IAfPatchPanel::Patch& patch) final;
87 void onReleaseAudioPatch(audio_patch_handle_t handle) final;
François Gaffie58e73af2023-02-15 11:47:24 +010088 void onUpdateAudioPatch(audio_patch_handle_t oldHandle,
89 audio_patch_handle_t newHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070090 const IAfPatchPanel::Patch& patch) final;
Vlad Popab042ee62022-10-20 18:05:00 +020091
Vlad Popa7e81cea2023-01-19 16:34:16 +010092 /**
93 * The new metadata can determine whether we should compute MEL for the given thread.
94 * This is the case only if one of the tracks in the thread mix is using MEDIA or GAME.
95 * Otherwise, this method will disable CSD.
96 **/
97 void updateMetadataForCsd(audio_io_handle_t streamHandle,
98 const std::vector<playback_track_metadata_v7_t>& metadataVec);
Vlad Popab042ee62022-10-20 18:05:00 +020099private:
Vlad Popa7e81cea2023-01-19 16:34:16 +0100100 struct ActiveMelPatch {
101 audio_io_handle_t streamHandle{AUDIO_IO_HANDLE_NONE};
Vlad Popaf79f6ba2023-07-27 18:27:59 -0700102 /**
103 * Stores device ids and whether they are compatible for CSD calculation.
104 * The boolean value can change since BT audio device types are user-configurable
105 * to headphones/headsets or other device types.
106 */
107 std::vector<std::pair<audio_port_handle_t,bool>> deviceStates;
Vlad Popa7e81cea2023-01-19 16:34:16 +0100108 bool csdActive;
109 };
Vlad Popab042ee62022-10-20 18:05:00 +0200110
Vlad Popa7e81cea2023-01-19 16:34:16 +0100111 void stopInternalMelComputation();
112
113 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -0700114 void stopMelComputationForPatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100115
116 /** Should be called with the following order of locks: mAudioFlinger.mLock -> mLock. */
Andy Hung920f6572022-10-06 12:09:49 -0700117 void startMelComputationForActivePatch_l(const ActiveMelPatch& patch) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100118
Andy Hung920f6572022-10-06 12:09:49 -0700119 std::optional<audio_patch_handle_t>
120 activePatchStreamHandle_l(audio_io_handle_t streamHandle) REQUIRES(mLock);
Vlad Popa7e81cea2023-01-19 16:34:16 +0100121
Vlad Popa69fbbee2023-04-25 12:23:24 +0200122 bool useHalSoundDoseInterface_l() REQUIRES(mLock);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100123
Andy Hungb60a2c82023-07-17 14:02:52 -0700124 const sp<IAfMelReporterCallback> mAfMelReporterCallback;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100125
126 sp<SoundDoseManager> mSoundDoseManager;
Vlad Popab042ee62022-10-20 18:05:00 +0200127
Vlad Popab042ee62022-10-20 18:05:00 +0200128 /**
129 * Lock for protecting the active mel patches. Do not mix with the AudioFlinger lock.
130 * Locking order AudioFlinger::mLock -> PatchCommandThread::mLock -> MelReporter::mLock.
131 */
132 std::mutex mLock;
Andy Hungad2faf72023-07-13 20:00:50 -0700133 std::unordered_map<audio_patch_handle_t, ActiveMelPatch> mActiveMelPatches GUARDED_BY(mLock);
134 std::unordered_map<audio_port_handle_t, int> mActiveDevices GUARDED_BY(mLock);
135 bool mUseHalSoundDoseInterface GUARDED_BY(mLock) = false;
Vlad Popab042ee62022-10-20 18:05:00 +0200136};
Andy Hungad2faf72023-07-13 20:00:50 -0700137
138} // namespace android