| 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 | 81ce760 | 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 | 4f27495 | 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 | 81ce760 | 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 | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 33 | if (mSoundDoseManager->forceUseFrameworkMel()) { | 
|  | 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 | 63c48dd | 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 | 63c48dd | 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 | 63c48dd | 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 | 81ce760 | 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 | d65f1d8 | 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 | 63c48dd | 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 | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 77 | void MelReporter::onFirstRef() { | 
| Andy Hung | f9e248b | 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 | 197faf8 | 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 | 81ce760 | 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 | cd88bfe | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 90 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex());  // AudioFlinger_Mutex | 
| Andy Hung | d65f1d8 | 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 | 197faf8 | 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 | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 120 | void MelReporter::onCreateAudioPatch(audio_patch_handle_t handle, | 
| Andy Hung | c0ab56b | 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 | 197faf8 | 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 | 197faf8 | 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 | 197faf8 | 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 | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 156 | if (!newPatch.deviceStates.empty() && newPatch.csdActive) { | 
| Andy Hung | cd88bfe | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 157 | audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());  // AudioFlinger_Mutex | 
| Andy Hung | d65f1d8 | 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 | 81ce760 | 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 | f9e248b | 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 | 197faf8 | 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 | 197faf8 | 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, | 
| Andy Hung | 0c1e11e | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 185 | outputThread->sampleRate(), | 
|  | 186 | outputThread->channelCount(), | 
|  | 187 | outputThread->format())); | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 188 | } | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 193 | void MelReporter::startMelComputationForDeviceId(audio_port_handle_t deviceId) { | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 194 | ALOGV("%s(%d)", __func__, deviceId); | 
| Andy Hung | d65f1d8 | 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 | 197faf8 | 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 | 81ce760 | 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 | d65f1d8 | 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 | cd88bfe | 2023-09-06 17:52:01 -0700 | [diff] [blame] | 234 | audio_utils::lock_guard _afl(mAfMelReporterCallback->mutex());  // AudioFlinger_Mutex | 
| Andy Hung | d65f1d8 | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 235 | audio_utils::lock_guard _l(mutex()); | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 236 | stopMelComputationForPatch_l(melPatch); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 239 | sp<media::ISoundDose> MelReporter::getSoundDoseInterface( | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 240 | const sp<media::ISoundDoseCallback>& callback) { | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 241 | // no need to lock since getSoundDoseInterface is synchronized | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 242 | return mSoundDoseManager->getSoundDoseInterface(callback); | 
|  | 243 | } | 
|  | 244 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 245 | void MelReporter::stopInternalMelComputation() { | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 246 | ALOGV("%s", __func__); | 
| Andy Hung | d65f1d8 | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 247 | audio_utils::lock_guard _l(mutex()); | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 248 | if (mUseHalSoundDoseInterface) { | 
|  | 249 | return; | 
|  | 250 | } | 
| Vlad Popa | 870f6d6 | 2022-12-22 12:15:47 +0100 | [diff] [blame] | 251 | mActiveMelPatches.clear(); | 
|  | 252 | mUseHalSoundDoseInterface = true; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 253 | } | 
|  | 254 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 255 | void MelReporter::stopMelComputationForPatch_l(const ActiveMelPatch& patch) | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 256 | NO_THREAD_SAFETY_ANALYSIS  // access of AudioFlinger::checkOutputThread_l | 
|  | 257 | { | 
| Andy Hung | f9e248b | 2023-07-17 14:02:52 -0700 | [diff] [blame] | 258 | auto outputThread = mAfMelReporterCallback->checkOutputThread_l(patch.streamHandle); | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 259 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 260 | ALOGV("%s: stop MEL for stream id: %d", __func__, patch.streamHandle); | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 261 | for (const auto& device : patch.deviceStates) { | 
|  | 262 | if (mActiveDevices[device.first] > 0) { | 
|  | 263 | --mActiveDevices[device.first]; | 
|  | 264 | if (mActiveDevices[device.first] == 0) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 265 | // no stream is using deviceId anymore | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 266 | ALOGI("%s removing device %d from active CSD devices", __func__, device.first); | 
|  | 267 | mSoundDoseManager->clearMapDeviceIdEntries(device.first); | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 268 | } | 
|  | 269 | } | 
|  | 270 | } | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 271 |  | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 272 | mSoundDoseManager->removeStreamProcessor(patch.streamHandle); | 
| Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame] | 273 | if (outputThread != nullptr && !useHalSoundDoseInterface_l()) { | 
| Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 274 | outputThread->stopMelComputation_l(); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 275 | } | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 278 | void MelReporter::stopMelComputationForDeviceId(audio_port_handle_t deviceId) { | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 279 | ALOGV("%s(%d)", __func__, deviceId); | 
| Andy Hung | d65f1d8 | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 280 | audio_utils::lock_guard _laf(mAfMelReporterCallback->mutex()); | 
|  | 281 | audio_utils::lock_guard _l(mutex()); | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 282 |  | 
|  | 283 | for (auto& activeMelPatch : mActiveMelPatches) { | 
|  | 284 | bool csdActive = false; | 
|  | 285 | for (auto& device: activeMelPatch.second.deviceStates) { | 
|  | 286 | if (device.first == deviceId && device.second) { | 
|  | 287 | device.second = false; | 
|  | 288 | } | 
|  | 289 | csdActive |= device.second; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | if (!csdActive && activeMelPatch.second.csdActive) { | 
|  | 293 | activeMelPatch.second.csdActive = csdActive; | 
|  | 294 | stopMelComputationForPatch_l(activeMelPatch.second); | 
|  | 295 | } | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | } | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 299 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 300 | std::optional<audio_patch_handle_t> MelReporter::activePatchStreamHandle_l( | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 301 | audio_io_handle_t streamHandle) { | 
|  | 302 | for(const auto& patchIt : mActiveMelPatches) { | 
|  | 303 | if (patchIt.second.streamHandle == streamHandle) { | 
|  | 304 | return patchIt.first; | 
|  | 305 | } | 
|  | 306 | } | 
|  | 307 | return std::nullopt; | 
|  | 308 | } | 
|  | 309 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 310 | bool MelReporter::useHalSoundDoseInterface_l() { | 
| Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame] | 311 | return !mSoundDoseManager->forceUseFrameworkMel() & mUseHalSoundDoseInterface; | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 312 | } | 
|  | 313 |  | 
| Andy Hung | 81ce760 | 2023-07-13 20:00:50 -0700 | [diff] [blame] | 314 | std::string MelReporter::dump() { | 
| Andy Hung | d65f1d8 | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 315 | audio_utils::lock_guard _l(mutex()); | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 316 | std::string output("\nSound Dose:\n"); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 317 | output.append(mSoundDoseManager->dump()); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 318 | return output; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | }  // namespace android |