| 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 | 
|  | 19 | #define LOG_TAG "AudioFlinger::MelReporter" | 
|  | 20 |  | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 21 | #include "AudioFlinger.h" | 
|  | 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 |  | 
| Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 31 | bool AudioFlinger::MelReporter::activateHalSoundDoseComputation(const std::string& module, | 
|  | 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) { | 
|  | 41 | ALOGW("%s: HAL cannot provide sound dose interface for module %s, use internal MEL", | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 42 | __func__, module.c_str()); | 
|  | 43 | activateInternalSoundDoseComputation(); | 
|  | 44 | return false; | 
|  | 45 | } | 
|  | 46 |  | 
| Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 47 | if (soundDoseBinder == nullptr) { | 
|  | 48 | ALOGW("%s: HAL doesn't implement a sound dose interface for module %s, use internal MEL", | 
|  | 49 | __func__, module.c_str()); | 
|  | 50 | activateInternalSoundDoseComputation(); | 
|  | 51 | return false; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | std::shared_ptr<ISoundDose> soundDoseInterface = ISoundDose::fromBinder(soundDoseBinder); | 
|  | 55 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 56 | if (!mSoundDoseManager->setHalSoundDoseInterface(soundDoseInterface)) { | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 57 | 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] | 58 | activateInternalSoundDoseComputation(); | 
|  | 59 | return false; | 
|  | 60 | } | 
|  | 61 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 62 | stopInternalMelComputation(); | 
|  | 63 | return true; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | void AudioFlinger::MelReporter::activateInternalSoundDoseComputation() { | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 67 | { | 
|  | 68 | std::lock_guard _l(mLock); | 
|  | 69 | if (!mUseHalSoundDoseInterface) { | 
|  | 70 | // no need to start internal MEL on active patches | 
|  | 71 | return; | 
|  | 72 | } | 
|  | 73 | mUseHalSoundDoseInterface = false; | 
|  | 74 | } | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 75 |  | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 76 | mSoundDoseManager->setHalSoundDoseInterface(nullptr); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | void AudioFlinger::MelReporter::onFirstRef() { | 
|  | 80 | mAudioFlinger.mPatchCommandThread->addListener(this); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 83 | bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t device) { | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 84 | if (mSoundDoseManager->forceComputeCsdOnAllDevices()) { | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 85 | return true; | 
|  | 86 | } | 
|  | 87 |  | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 88 | switch (device) { | 
|  | 89 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: | 
|  | 90 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: | 
|  | 91 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: | 
|  | 92 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: | 
|  | 93 | case AUDIO_DEVICE_OUT_HEARING_AID: | 
|  | 94 | case AUDIO_DEVICE_OUT_USB_HEADSET: | 
|  | 95 | case AUDIO_DEVICE_OUT_BLE_HEADSET: | 
|  | 96 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: | 
|  | 97 | return true; | 
|  | 98 | default: | 
|  | 99 | return false; | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 103 | void AudioFlinger::MelReporter::updateMetadataForCsd(audio_io_handle_t streamHandle, | 
|  | 104 | const std::vector<playback_track_metadata_v7_t>& metadataVec) { | 
|  | 105 | std::lock_guard _laf(mAudioFlinger.mLock); | 
|  | 106 | std::lock_guard _l(mLock); | 
|  | 107 | auto activeMelPatchId = activePatchStreamHandle_l(streamHandle); | 
|  | 108 | if (!activeMelPatchId) { | 
|  | 109 | ALOGV("%s stream handle %d does not have an active patch", __func__, streamHandle); | 
|  | 110 | return; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | bool shouldActivateCsd = false; | 
|  | 114 | for (const auto& metadata : metadataVec) { | 
|  | 115 | if (metadata.base.usage == AUDIO_USAGE_GAME || metadata.base.usage == AUDIO_USAGE_MEDIA) { | 
|  | 116 | shouldActivateCsd = true; | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | auto activeMelPatchIt = mActiveMelPatches.find(activeMelPatchId.value()); | 
|  | 121 | if (activeMelPatchIt != mActiveMelPatches.end() | 
|  | 122 | && shouldActivateCsd != activeMelPatchIt->second.csdActive) { | 
|  | 123 | if (activeMelPatchIt->second.csdActive) { | 
|  | 124 | ALOGV("%s should not compute CSD for stream handle %d", __func__, streamHandle); | 
|  | 125 | stopMelComputationForPatch_l(activeMelPatchIt->second); | 
|  | 126 | } else { | 
|  | 127 | ALOGV("%s should compute CSD for stream handle %d", __func__, streamHandle); | 
|  | 128 | startMelComputationForActivePatch_l(activeMelPatchIt->second); | 
|  | 129 | } | 
|  | 130 | activeMelPatchIt->second.csdActive = shouldActivateCsd; | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 |  | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 134 | void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle, | 
|  | 135 | const PatchPanel::Patch& patch) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 136 | if (useHalSoundDoseInterface()) { | 
|  | 137 | ALOGV("%s using HAL sound dose, ignore new patch", __func__); | 
|  | 138 | return; | 
|  | 139 | } | 
|  | 140 |  | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 141 | ALOGV("%s: handle %d mHalHandle %d device sink %08x", | 
|  | 142 | __func__, handle, patch.mHalHandle, | 
|  | 143 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); | 
|  | 144 | if (patch.mAudioPatch.num_sources == 0 | 
|  | 145 | || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 146 | ALOGV("%s: patch does not contain any mix sources", __func__); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 147 | return; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle; | 
|  | 151 | ActiveMelPatch newPatch; | 
|  | 152 | newPatch.streamHandle = streamHandle; | 
|  | 153 | for (int i = 0; i < patch.mAudioPatch.num_sinks; ++ i) { | 
|  | 154 | if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE | 
|  | 155 | && shouldComputeMelForDeviceType(patch.mAudioPatch.sinks[i].ext.device.type)) { | 
|  | 156 | audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id; | 
|  | 157 | newPatch.deviceHandles.push_back(deviceId); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 158 | AudioDeviceTypeAddr adt{patch.mAudioPatch.sinks[i].ext.device.type, | 
|  | 159 | patch.mAudioPatch.sinks[i].ext.device.address}; | 
|  | 160 | mSoundDoseManager->mapAddressToDeviceId(adt, deviceId); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 161 | } | 
|  | 162 | } | 
|  | 163 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 164 | std::lock_guard _afl(mAudioFlinger.mLock); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 165 | std::lock_guard _l(mLock); | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 166 | ALOGV("%s add patch handle %d to active devices", __func__, handle); | 
|  | 167 | startMelComputationForActivePatch_l(newPatch); | 
|  | 168 | newPatch.csdActive = true; | 
|  | 169 | mActiveMelPatches[handle] = newPatch; | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 172 | void AudioFlinger::MelReporter::startMelComputationForActivePatch_l(const ActiveMelPatch& patch) { | 
|  | 173 | auto thread = mAudioFlinger.checkPlaybackThread_l(patch.streamHandle); | 
|  | 174 | if (thread == nullptr) { | 
|  | 175 | ALOGE("%s cannot find thread for stream handle %d", __func__, patch.streamHandle); | 
|  | 176 | return; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | for (const auto& deviceHandle : patch.deviceHandles) { | 
|  | 180 | ++mActiveDevices[deviceHandle]; | 
|  | 181 | ALOGI("%s add stream %d that uses device %d for CSD, nr of streams: %d", __func__, | 
|  | 182 | patch.streamHandle, deviceHandle, mActiveDevices[deviceHandle]); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 183 | thread->startMelComputation(mSoundDoseManager->getOrCreateProcessorForDevice( | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 184 | deviceHandle, | 
|  | 185 | patch.streamHandle, | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 186 | thread->mSampleRate, | 
|  | 187 | thread->mChannelCount, | 
|  | 188 | thread->mFormat)); | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 189 | } | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 192 | void AudioFlinger::MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) { | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 193 | ActiveMelPatch melPatch; | 
|  | 194 | { | 
|  | 195 | std::lock_guard _l(mLock); | 
|  | 196 |  | 
|  | 197 | auto patchIt = mActiveMelPatches.find(handle); | 
|  | 198 | if (patchIt == mActiveMelPatches.end()) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 199 | ALOGV("%s patch handle %d does not contain any mix sources with active MEL calculation", | 
|  | 200 | __func__, handle); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 201 | return; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | melPatch = patchIt->second; | 
|  | 205 | mActiveMelPatches.erase(patchIt); | 
|  | 206 | } | 
|  | 207 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 208 | std::lock_guard _afl(mAudioFlinger.mLock); | 
|  | 209 | std::lock_guard _l(mLock); | 
|  | 210 | stopMelComputationForPatch_l(melPatch); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 213 | sp<media::ISoundDose> AudioFlinger::MelReporter::getSoundDoseInterface( | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 214 | const sp<media::ISoundDoseCallback>& callback) { | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 215 | // no need to lock since getSoundDoseInterface is synchronized | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 216 | return mSoundDoseManager->getSoundDoseInterface(callback); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | void AudioFlinger::MelReporter::stopInternalMelComputation() { | 
|  | 220 | ALOGV("%s", __func__); | 
| Vlad Popa | 870f6d6 | 2022-12-22 12:15:47 +0100 | [diff] [blame] | 221 | std::lock_guard _l(mLock); | 
|  | 222 | mActiveMelPatches.clear(); | 
|  | 223 | mUseHalSoundDoseInterface = true; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 226 | void AudioFlinger::MelReporter::stopMelComputationForPatch_l(const ActiveMelPatch& patch) { | 
|  | 227 | auto thread = mAudioFlinger.checkPlaybackThread_l(patch.streamHandle); | 
|  | 228 | ALOGV("%s: stop MEL for stream id: %d", __func__, patch.streamHandle); | 
|  | 229 | for (const auto& deviceId : patch.deviceHandles) { | 
|  | 230 | if (mActiveDevices[deviceId] > 0) { | 
|  | 231 | --mActiveDevices[deviceId]; | 
|  | 232 | if (mActiveDevices[deviceId] == 0) { | 
|  | 233 | // no stream is using deviceId anymore | 
|  | 234 | ALOGI("%s removing device %d from active CSD devices", __func__, deviceId); | 
|  | 235 | mSoundDoseManager->clearMapDeviceIdEntries(deviceId); | 
|  | 236 | } | 
|  | 237 | } | 
|  | 238 | } | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 239 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 240 | mSoundDoseManager->removeStreamProcessor(patch.streamHandle); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 241 | if (thread != nullptr) { | 
|  | 242 | thread->stopMelComputation(); | 
|  | 243 | } | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 246 |  | 
|  | 247 | std::optional<audio_patch_handle_t> AudioFlinger::MelReporter::activePatchStreamHandle_l( | 
|  | 248 | audio_io_handle_t streamHandle) { | 
|  | 249 | for(const auto& patchIt : mActiveMelPatches) { | 
|  | 250 | if (patchIt.second.streamHandle == streamHandle) { | 
|  | 251 | return patchIt.first; | 
|  | 252 | } | 
|  | 253 | } | 
|  | 254 | return std::nullopt; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | bool AudioFlinger::MelReporter::useHalSoundDoseInterface() { | 
|  | 258 | bool useHalSoundDoseInterface = !mSoundDoseManager->forceUseFrameworkMel(); | 
|  | 259 | { | 
|  | 260 | std::lock_guard _l(mLock); | 
|  | 261 | useHalSoundDoseInterface &= mUseHalSoundDoseInterface; | 
|  | 262 | } | 
|  | 263 | return useHalSoundDoseInterface; | 
|  | 264 | } | 
|  | 265 |  | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 266 | std::string AudioFlinger::MelReporter::dump() { | 
|  | 267 | std::lock_guard _l(mLock); | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 268 | std::string output("\nSound Dose:\n"); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 269 | output.append(mSoundDoseManager->dump()); | 
| Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 270 | return output; | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | }  // namespace android |