Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 1 | /* |
| 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 | // #define LOG_NDEBUG 0 |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 19 | #define LOG_TAG "MelReporter" |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 20 | |
Andy Hung | ba8b63b | 2023-07-18 18:55:52 -0700 | [diff] [blame] | 21 | #include "MelReporter.h" |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 22 | |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 23 | #include <android/media/ISoundDoseCallback.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 24 | #include <audio_utils/power.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
Vlad Popa | debe0a7 | 2022-12-28 16:55:13 +0100 | [diff] [blame] | 27 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 28 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 31 | bool MelReporter::activateHalSoundDoseComputation(const std::string& module, |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 32 | const sp<DeviceHalInterface>& device) { |
Vlad Popa | eb8036a | 2023-09-29 18:20:04 -0700 | [diff] [blame] | 33 | if (mSoundDoseManager->isFrameworkMelForced()) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 34 | ALOGD("%s: Forcing use of internal MEL computation.", __func__); |
| 35 | activateInternalSoundDoseComputation(); |
| 36 | return false; |
| 37 | } |
| 38 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 39 | ndk::SpAIBinder soundDoseBinder; |
| 40 | if (device->getSoundDoseInterface(module, &soundDoseBinder) != OK) { |
Vlad Popa | b1c5378 | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 41 | ALOGW("%s: HAL cannot provide sound dose interface for module %s", |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 42 | __func__, module.c_str()); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 46 | if (soundDoseBinder == nullptr) { |
Vlad Popa | b1c5378 | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 47 | ALOGW("%s: HAL doesn't implement a sound dose interface for module %s", |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 48 | __func__, module.c_str()); |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
| 52 | std::shared_ptr<ISoundDose> soundDoseInterface = ISoundDose::fromBinder(soundDoseBinder); |
| 53 | |
Vlad Popa | b1c5378 | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 54 | if (!mSoundDoseManager->setHalSoundDoseInterface(module, soundDoseInterface)) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 55 | ALOGW("%s: cannot activate HAL MEL reporting for module %s", __func__, module.c_str()); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 59 | stopInternalMelComputation(); |
| 60 | return true; |
| 61 | } |
| 62 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 63 | void MelReporter::activateInternalSoundDoseComputation() { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 64 | { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 65 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 66 | if (!mUseHalSoundDoseInterface) { |
| 67 | // no need to start internal MEL on active patches |
| 68 | return; |
| 69 | } |
| 70 | mUseHalSoundDoseInterface = false; |
| 71 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 72 | |
Vlad Popa | b1c5378 | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 73 | // reset the HAL interfaces and use internal MELs |
| 74 | mSoundDoseManager->resetHalSoundDoseInterfaces(); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 77 | void MelReporter::onFirstRef() { |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 78 | mAfMelReporterCallback->getPatchCommandThread()->addListener(this); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 79 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 80 | mSoundDoseManager = sp<SoundDoseManager>::make(sp<IMelReporterCallback>::fromExisting(this)); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 83 | void MelReporter::updateMetadataForCsd(audio_io_handle_t streamHandle, |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 84 | const std::vector<playback_track_metadata_v7_t>& metadataVec) { |
Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 85 | if (!mSoundDoseManager->isCsdEnabled()) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 86 | ALOGV("%s csd is disabled", __func__); |
| 87 | return; |
| 88 | } |
| 89 | |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 90 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); // AudioFlinger_Mutex |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 91 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 92 | auto activeMelPatchId = activePatchStreamHandle_l(streamHandle); |
| 93 | if (!activeMelPatchId) { |
| 94 | ALOGV("%s stream handle %d does not have an active patch", __func__, streamHandle); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | bool shouldActivateCsd = false; |
| 99 | for (const auto& metadata : metadataVec) { |
| 100 | if (metadata.base.usage == AUDIO_USAGE_GAME || metadata.base.usage == AUDIO_USAGE_MEDIA) { |
| 101 | shouldActivateCsd = true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | auto activeMelPatchIt = mActiveMelPatches.find(activeMelPatchId.value()); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 106 | if (activeMelPatchIt != mActiveMelPatches.end()) { |
| 107 | if (shouldActivateCsd != activeMelPatchIt->second.csdActive) { |
| 108 | if (activeMelPatchIt->second.csdActive) { |
| 109 | ALOGV("%s should not compute CSD for stream handle %d", __func__, streamHandle); |
| 110 | stopMelComputationForPatch_l(activeMelPatchIt->second); |
| 111 | } else { |
| 112 | ALOGV("%s should compute CSD for stream handle %d", __func__, streamHandle); |
| 113 | startMelComputationForActivePatch_l(activeMelPatchIt->second); |
| 114 | } |
| 115 | activeMelPatchIt->second.csdActive = shouldActivateCsd; |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 116 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Shraddha Basantwani | fd414a8 | 2024-06-06 16:28:52 +0000 | [diff] [blame^] | 120 | void MelReporter::resetReferencesForTest() { |
| 121 | mAfMelReporterCallback.clear(); |
| 122 | mSoundDoseManager->resetReferencesForTest(); |
| 123 | } |
| 124 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 125 | void MelReporter::onCreateAudioPatch(audio_patch_handle_t handle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 126 | const IAfPatchPanel::Patch& patch) { |
Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 127 | if (!mSoundDoseManager->isCsdEnabled()) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 128 | ALOGV("%s csd is disabled", __func__); |
| 129 | return; |
| 130 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 131 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 132 | ALOGV("%s: handle %d mHalHandle %d device sink %08x", |
| 133 | __func__, handle, patch.mHalHandle, |
| 134 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 135 | if (patch.mAudioPatch.num_sources == 0 |
| 136 | || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 137 | ALOGV("%s: patch does not contain any mix sources", __func__); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
| 141 | audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle; |
| 142 | ActiveMelPatch newPatch; |
| 143 | newPatch.streamHandle = streamHandle; |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 144 | newPatch.csdActive = false; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 145 | for (size_t i = 0; i < patch.mAudioPatch.num_sinks; ++i) { |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 146 | if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE && |
| 147 | mSoundDoseManager->shouldComputeCsdForDeviceType( |
| 148 | patch.mAudioPatch.sinks[i].ext.device.type)) { |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 149 | audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id; |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 150 | bool shouldComputeCsd = mSoundDoseManager->shouldComputeCsdForDeviceWithAddress( |
| 151 | patch.mAudioPatch.sinks[i].ext.device.type, |
| 152 | patch.mAudioPatch.sinks[i].ext.device.address); |
| 153 | newPatch.deviceStates.push_back({deviceId, shouldComputeCsd}); |
| 154 | newPatch.csdActive |= shouldComputeCsd; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 155 | AudioDeviceTypeAddr adt{patch.mAudioPatch.sinks[i].ext.device.type, |
| 156 | patch.mAudioPatch.sinks[i].ext.device.address}; |
| 157 | mSoundDoseManager->mapAddressToDeviceId(adt, deviceId); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 161 | if (!newPatch.deviceStates.empty() && newPatch.csdActive) { |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 162 | audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex()); // AudioFlinger_Mutex |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 163 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 164 | ALOGV("%s add patch handle %d to active devices", __func__, handle); |
| 165 | startMelComputationForActivePatch_l(newPatch); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 166 | mActiveMelPatches[handle] = newPatch; |
| 167 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 170 | void MelReporter::startMelComputationForActivePatch_l(const ActiveMelPatch& patch) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 171 | NO_THREAD_SAFETY_ANALYSIS // access of AudioFlinger::checkOutputThread_l |
| 172 | { |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 173 | auto outputThread = mAfMelReporterCallback->checkOutputThread_l(patch.streamHandle); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 174 | if (outputThread == nullptr) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 175 | ALOGE("%s cannot find thread for stream handle %d", __func__, patch.streamHandle); |
| 176 | return; |
| 177 | } |
| 178 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 179 | for (const auto& device : patch.deviceStates) { |
| 180 | if (device.second) { |
| 181 | ++mActiveDevices[device.first]; |
| 182 | ALOGI("%s add stream %d that uses device %d for CSD, nr of streams: %d", __func__, |
| 183 | patch.streamHandle, device.first, mActiveDevices[device.first]); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 184 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 185 | if (outputThread != nullptr && !useHalSoundDoseInterface_l()) { |
| 186 | outputThread->startMelComputation_l( |
| 187 | mSoundDoseManager->getOrCreateProcessorForDevice( |
| 188 | device.first, |
| 189 | patch.streamHandle, |
| 190 | outputThread->sampleRate(), |
| 191 | outputThread->channelCount(), |
| 192 | outputThread->format())); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void MelReporter::startMelComputationForDeviceId(audio_port_handle_t deviceId) { |
| 199 | ALOGV("%s(%d)", __func__, deviceId); |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 200 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); |
| 201 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 202 | |
| 203 | for (auto& activeMelPatch : mActiveMelPatches) { |
| 204 | bool csdActive = false; |
| 205 | for (auto& device: activeMelPatch.second.deviceStates) { |
| 206 | if (device.first == deviceId && !device.second) { |
| 207 | device.second = true; |
| 208 | } |
| 209 | csdActive |= device.second; |
| 210 | } |
| 211 | if (csdActive && !activeMelPatch.second.csdActive) { |
| 212 | activeMelPatch.second.csdActive = csdActive; |
| 213 | startMelComputationForActivePatch_l(activeMelPatch.second); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 214 | } |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 215 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 218 | void MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) { |
Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 219 | if (!mSoundDoseManager->isCsdEnabled()) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 220 | ALOGV("%s csd is disabled", __func__); |
| 221 | return; |
| 222 | } |
| 223 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 224 | ActiveMelPatch melPatch; |
| 225 | { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 226 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 227 | |
| 228 | auto patchIt = mActiveMelPatches.find(handle); |
| 229 | if (patchIt == mActiveMelPatches.end()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 230 | ALOGV("%s patch handle %d does not contain any mix sources with active MEL calculation", |
| 231 | __func__, handle); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | melPatch = patchIt->second; |
| 236 | mActiveMelPatches.erase(patchIt); |
| 237 | } |
| 238 | |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 239 | audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex()); // AudioFlinger_Mutex |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 240 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 241 | if (melPatch.csdActive) { |
| 242 | // only need to stop if patch was active |
| 243 | melPatch.csdActive = false; |
| 244 | stopMelComputationForPatch_l(melPatch); |
| 245 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 248 | void MelReporter::onUpdateAudioPatch(audio_patch_handle_t oldHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 249 | audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) { |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 250 | onReleaseAudioPatch(oldHandle); |
| 251 | onCreateAudioPatch(newHandle, patch); |
| 252 | } |
| 253 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 254 | sp<media::ISoundDose> MelReporter::getSoundDoseInterface( |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 255 | const sp<media::ISoundDoseCallback>& callback) { |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 256 | // no need to lock since getSoundDoseInterface is synchronized |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 257 | return mSoundDoseManager->getSoundDoseInterface(callback); |
| 258 | } |
| 259 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 260 | void MelReporter::stopInternalMelComputation() { |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 261 | ALOGV("%s", __func__); |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 262 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | b1c5378 | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 263 | if (mUseHalSoundDoseInterface) { |
| 264 | return; |
| 265 | } |
Vlad Popa | 870f6d6 | 2022-12-22 12:15:47 +0100 | [diff] [blame] | 266 | mActiveMelPatches.clear(); |
| 267 | mUseHalSoundDoseInterface = true; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 270 | void MelReporter::stopMelComputationForPatch_l(const ActiveMelPatch& patch) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 271 | NO_THREAD_SAFETY_ANALYSIS // access of AudioFlinger::checkOutputThread_l |
| 272 | { |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 273 | auto outputThread = mAfMelReporterCallback->checkOutputThread_l(patch.streamHandle); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 274 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 275 | ALOGV("%s: stop MEL for stream id: %d", __func__, patch.streamHandle); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 276 | for (const auto& device : patch.deviceStates) { |
| 277 | if (mActiveDevices[device.first] > 0) { |
| 278 | --mActiveDevices[device.first]; |
| 279 | if (mActiveDevices[device.first] == 0) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 280 | // no stream is using deviceId anymore |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 281 | ALOGI("%s removing device %d from active CSD devices", __func__, device.first); |
| 282 | mSoundDoseManager->clearMapDeviceIdEntries(device.first); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 286 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 287 | mSoundDoseManager->removeStreamProcessor(patch.streamHandle); |
Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame] | 288 | if (outputThread != nullptr && !useHalSoundDoseInterface_l()) { |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 289 | outputThread->stopMelComputation_l(); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 290 | } |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 293 | void MelReporter::stopMelComputationForDeviceId(audio_port_handle_t deviceId) { |
| 294 | ALOGV("%s(%d)", __func__, deviceId); |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 295 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); |
| 296 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 297 | |
| 298 | for (auto& activeMelPatch : mActiveMelPatches) { |
| 299 | bool csdActive = false; |
| 300 | for (auto& device: activeMelPatch.second.deviceStates) { |
| 301 | if (device.first == deviceId && device.second) { |
| 302 | device.second = false; |
| 303 | } |
| 304 | csdActive |= device.second; |
| 305 | } |
| 306 | |
| 307 | if (!csdActive && activeMelPatch.second.csdActive) { |
| 308 | activeMelPatch.second.csdActive = csdActive; |
| 309 | stopMelComputationForPatch_l(activeMelPatch.second); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 314 | |
Vlad Popa | 1c67ad6 | 2024-01-19 17:53:42 -0800 | [diff] [blame] | 315 | void MelReporter::applyAllAudioPatches() { |
| 316 | ALOGV("%s", __func__); |
| 317 | |
| 318 | std::vector<IAfPatchPanel::Patch> patchesCopy; |
| 319 | { |
| 320 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); |
| 321 | for (const auto& patch : mAfPatchPanel->patches_l()) { |
| 322 | patchesCopy.emplace_back(patch.second); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | for (const auto& patch : patchesCopy) { |
| 327 | onCreateAudioPatch(patch.mHalHandle, patch); |
| 328 | } |
| 329 | } |
| 330 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 331 | std::optional<audio_patch_handle_t> MelReporter::activePatchStreamHandle_l( |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 332 | audio_io_handle_t streamHandle) { |
| 333 | for(const auto& patchIt : mActiveMelPatches) { |
| 334 | if (patchIt.second.streamHandle == streamHandle) { |
| 335 | return patchIt.first; |
| 336 | } |
| 337 | } |
| 338 | return std::nullopt; |
| 339 | } |
| 340 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 341 | bool MelReporter::useHalSoundDoseInterface_l() { |
Vlad Popa | eb8036a | 2023-09-29 18:20:04 -0700 | [diff] [blame] | 342 | return !mSoundDoseManager->isFrameworkMelForced() & mUseHalSoundDoseInterface; |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 345 | std::string MelReporter::dump() { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 346 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 347 | std::string output("\nSound Dose:\n"); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 348 | output.append(mSoundDoseManager->dump()); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 349 | return output; |
| 350 | } |
| 351 | |
| 352 | } // namespace android |