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 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 120 | void MelReporter::onCreateAudioPatch(audio_patch_handle_t handle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 121 | const IAfPatchPanel::Patch& patch) { |
Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 122 | if (!mSoundDoseManager->isCsdEnabled()) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 123 | ALOGV("%s csd is disabled", __func__); |
| 124 | return; |
| 125 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 126 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 127 | ALOGV("%s: handle %d mHalHandle %d device sink %08x", |
| 128 | __func__, handle, patch.mHalHandle, |
| 129 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 130 | if (patch.mAudioPatch.num_sources == 0 |
| 131 | || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 132 | ALOGV("%s: patch does not contain any mix sources", __func__); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
| 136 | audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle; |
| 137 | ActiveMelPatch newPatch; |
| 138 | newPatch.streamHandle = streamHandle; |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 139 | newPatch.csdActive = false; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 140 | for (size_t i = 0; i < patch.mAudioPatch.num_sinks; ++i) { |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 141 | if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE && |
| 142 | mSoundDoseManager->shouldComputeCsdForDeviceType( |
| 143 | patch.mAudioPatch.sinks[i].ext.device.type)) { |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 144 | audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id; |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 145 | bool shouldComputeCsd = mSoundDoseManager->shouldComputeCsdForDeviceWithAddress( |
| 146 | patch.mAudioPatch.sinks[i].ext.device.type, |
| 147 | patch.mAudioPatch.sinks[i].ext.device.address); |
| 148 | newPatch.deviceStates.push_back({deviceId, shouldComputeCsd}); |
| 149 | newPatch.csdActive |= shouldComputeCsd; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 150 | AudioDeviceTypeAddr adt{patch.mAudioPatch.sinks[i].ext.device.type, |
| 151 | patch.mAudioPatch.sinks[i].ext.device.address}; |
| 152 | mSoundDoseManager->mapAddressToDeviceId(adt, deviceId); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 156 | if (!newPatch.deviceStates.empty() && newPatch.csdActive) { |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 157 | audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex()); // AudioFlinger_Mutex |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 158 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 159 | ALOGV("%s add patch handle %d to active devices", __func__, handle); |
| 160 | startMelComputationForActivePatch_l(newPatch); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 161 | mActiveMelPatches[handle] = newPatch; |
| 162 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 165 | void MelReporter::startMelComputationForActivePatch_l(const ActiveMelPatch& patch) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 166 | NO_THREAD_SAFETY_ANALYSIS // access of AudioFlinger::checkOutputThread_l |
| 167 | { |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 168 | auto outputThread = mAfMelReporterCallback->checkOutputThread_l(patch.streamHandle); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 169 | if (outputThread == nullptr) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 170 | ALOGE("%s cannot find thread for stream handle %d", __func__, patch.streamHandle); |
| 171 | return; |
| 172 | } |
| 173 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 174 | for (const auto& device : patch.deviceStates) { |
| 175 | if (device.second) { |
| 176 | ++mActiveDevices[device.first]; |
| 177 | ALOGI("%s add stream %d that uses device %d for CSD, nr of streams: %d", __func__, |
| 178 | patch.streamHandle, device.first, mActiveDevices[device.first]); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 179 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 180 | if (outputThread != nullptr && !useHalSoundDoseInterface_l()) { |
| 181 | outputThread->startMelComputation_l( |
| 182 | mSoundDoseManager->getOrCreateProcessorForDevice( |
| 183 | device.first, |
| 184 | patch.streamHandle, |
| 185 | outputThread->sampleRate(), |
| 186 | outputThread->channelCount(), |
| 187 | outputThread->format())); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void MelReporter::startMelComputationForDeviceId(audio_port_handle_t deviceId) { |
| 194 | ALOGV("%s(%d)", __func__, deviceId); |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 195 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); |
| 196 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 197 | |
| 198 | for (auto& activeMelPatch : mActiveMelPatches) { |
| 199 | bool csdActive = false; |
| 200 | for (auto& device: activeMelPatch.second.deviceStates) { |
| 201 | if (device.first == deviceId && !device.second) { |
| 202 | device.second = true; |
| 203 | } |
| 204 | csdActive |= device.second; |
| 205 | } |
| 206 | if (csdActive && !activeMelPatch.second.csdActive) { |
| 207 | activeMelPatch.second.csdActive = csdActive; |
| 208 | startMelComputationForActivePatch_l(activeMelPatch.second); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 209 | } |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 210 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 213 | void MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) { |
Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 214 | if (!mSoundDoseManager->isCsdEnabled()) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 215 | ALOGV("%s csd is disabled", __func__); |
| 216 | return; |
| 217 | } |
| 218 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 219 | ActiveMelPatch melPatch; |
| 220 | { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 221 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 222 | |
| 223 | auto patchIt = mActiveMelPatches.find(handle); |
| 224 | if (patchIt == mActiveMelPatches.end()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 225 | ALOGV("%s patch handle %d does not contain any mix sources with active MEL calculation", |
| 226 | __func__, handle); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
| 230 | melPatch = patchIt->second; |
| 231 | mActiveMelPatches.erase(patchIt); |
| 232 | } |
| 233 | |
Andy Hung | 77448a5 | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 234 | audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex()); // AudioFlinger_Mutex |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 235 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 236 | if (melPatch.csdActive) { |
| 237 | // only need to stop if patch was active |
| 238 | melPatch.csdActive = false; |
| 239 | stopMelComputationForPatch_l(melPatch); |
| 240 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 243 | void MelReporter::onUpdateAudioPatch(audio_patch_handle_t oldHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 244 | audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) { |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 245 | onReleaseAudioPatch(oldHandle); |
| 246 | onCreateAudioPatch(newHandle, patch); |
| 247 | } |
| 248 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 249 | sp<media::ISoundDose> MelReporter::getSoundDoseInterface( |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 250 | const sp<media::ISoundDoseCallback>& callback) { |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 251 | // no need to lock since getSoundDoseInterface is synchronized |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 252 | return mSoundDoseManager->getSoundDoseInterface(callback); |
| 253 | } |
| 254 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 255 | void MelReporter::stopInternalMelComputation() { |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 256 | ALOGV("%s", __func__); |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 257 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | b1c5378 | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 258 | if (mUseHalSoundDoseInterface) { |
| 259 | return; |
| 260 | } |
Vlad Popa | 870f6d6 | 2022-12-22 12:15:47 +0100 | [diff] [blame] | 261 | mActiveMelPatches.clear(); |
| 262 | mUseHalSoundDoseInterface = true; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 265 | void MelReporter::stopMelComputationForPatch_l(const ActiveMelPatch& patch) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 266 | NO_THREAD_SAFETY_ANALYSIS // access of AudioFlinger::checkOutputThread_l |
| 267 | { |
Andy Hung | b60a2c8 | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 268 | auto outputThread = mAfMelReporterCallback->checkOutputThread_l(patch.streamHandle); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 269 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 270 | ALOGV("%s: stop MEL for stream id: %d", __func__, patch.streamHandle); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 271 | for (const auto& device : patch.deviceStates) { |
| 272 | if (mActiveDevices[device.first] > 0) { |
| 273 | --mActiveDevices[device.first]; |
| 274 | if (mActiveDevices[device.first] == 0) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 275 | // no stream is using deviceId anymore |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 276 | ALOGI("%s removing device %d from active CSD devices", __func__, device.first); |
| 277 | mSoundDoseManager->clearMapDeviceIdEntries(device.first); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 281 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 282 | mSoundDoseManager->removeStreamProcessor(patch.streamHandle); |
Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame] | 283 | if (outputThread != nullptr && !useHalSoundDoseInterface_l()) { |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 284 | outputThread->stopMelComputation_l(); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 285 | } |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 288 | void MelReporter::stopMelComputationForDeviceId(audio_port_handle_t deviceId) { |
| 289 | ALOGV("%s(%d)", __func__, deviceId); |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 290 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); |
| 291 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | f79f6ba | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 292 | |
| 293 | for (auto& activeMelPatch : mActiveMelPatches) { |
| 294 | bool csdActive = false; |
| 295 | for (auto& device: activeMelPatch.second.deviceStates) { |
| 296 | if (device.first == deviceId && device.second) { |
| 297 | device.second = false; |
| 298 | } |
| 299 | csdActive |= device.second; |
| 300 | } |
| 301 | |
| 302 | if (!csdActive && activeMelPatch.second.csdActive) { |
| 303 | activeMelPatch.second.csdActive = csdActive; |
| 304 | stopMelComputationForPatch_l(activeMelPatch.second); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 309 | |
Vlad Popa | 1c67ad6 | 2024-01-19 17:53:42 -0800 | [diff] [blame] | 310 | void MelReporter::applyAllAudioPatches() { |
| 311 | ALOGV("%s", __func__); |
| 312 | |
| 313 | std::vector<IAfPatchPanel::Patch> patchesCopy; |
| 314 | { |
| 315 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); |
| 316 | for (const auto& patch : mAfPatchPanel->patches_l()) { |
| 317 | patchesCopy.emplace_back(patch.second); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | for (const auto& patch : patchesCopy) { |
| 322 | onCreateAudioPatch(patch.mHalHandle, patch); |
| 323 | } |
| 324 | } |
| 325 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 326 | std::optional<audio_patch_handle_t> MelReporter::activePatchStreamHandle_l( |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 327 | audio_io_handle_t streamHandle) { |
| 328 | for(const auto& patchIt : mActiveMelPatches) { |
| 329 | if (patchIt.second.streamHandle == streamHandle) { |
| 330 | return patchIt.first; |
| 331 | } |
| 332 | } |
| 333 | return std::nullopt; |
| 334 | } |
| 335 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 336 | bool MelReporter::useHalSoundDoseInterface_l() { |
Vlad Popa | eb8036a | 2023-09-29 18:20:04 -0700 | [diff] [blame] | 337 | return !mSoundDoseManager->isFrameworkMelForced() & mUseHalSoundDoseInterface; |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Andy Hung | ad2faf7 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 340 | std::string MelReporter::dump() { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 341 | audio_utils::lock_guard _l(mutex()); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 342 | std::string output("\nSound Dose:\n"); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 343 | output.append(mSoundDoseManager->dump()); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 344 | return output; |
| 345 | } |
| 346 | |
| 347 | } // namespace android |