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 | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame^] | 84 | if (mSoundDoseManager->isCsdDisabled()) { |
| 85 | ALOGV("%s csd is disabled", __func__); |
| 86 | return false; |
| 87 | } |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 88 | if (mSoundDoseManager->forceComputeCsdOnAllDevices()) { |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 92 | switch (device) { |
| 93 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 94 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 95 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 96 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: |
| 97 | case AUDIO_DEVICE_OUT_HEARING_AID: |
| 98 | case AUDIO_DEVICE_OUT_USB_HEADSET: |
| 99 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
| 100 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 101 | return true; |
| 102 | default: |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 107 | void AudioFlinger::MelReporter::updateMetadataForCsd(audio_io_handle_t streamHandle, |
| 108 | const std::vector<playback_track_metadata_v7_t>& metadataVec) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame^] | 109 | if (mSoundDoseManager->isCsdDisabled()) { |
| 110 | ALOGV("%s csd is disabled", __func__); |
| 111 | return; |
| 112 | } |
| 113 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 114 | std::lock_guard _laf(mAudioFlinger.mLock); |
| 115 | std::lock_guard _l(mLock); |
| 116 | auto activeMelPatchId = activePatchStreamHandle_l(streamHandle); |
| 117 | if (!activeMelPatchId) { |
| 118 | ALOGV("%s stream handle %d does not have an active patch", __func__, streamHandle); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | bool shouldActivateCsd = false; |
| 123 | for (const auto& metadata : metadataVec) { |
| 124 | if (metadata.base.usage == AUDIO_USAGE_GAME || metadata.base.usage == AUDIO_USAGE_MEDIA) { |
| 125 | shouldActivateCsd = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | auto activeMelPatchIt = mActiveMelPatches.find(activeMelPatchId.value()); |
| 130 | if (activeMelPatchIt != mActiveMelPatches.end() |
| 131 | && shouldActivateCsd != activeMelPatchIt->second.csdActive) { |
| 132 | if (activeMelPatchIt->second.csdActive) { |
| 133 | ALOGV("%s should not compute CSD for stream handle %d", __func__, streamHandle); |
| 134 | stopMelComputationForPatch_l(activeMelPatchIt->second); |
| 135 | } else { |
| 136 | ALOGV("%s should compute CSD for stream handle %d", __func__, streamHandle); |
| 137 | startMelComputationForActivePatch_l(activeMelPatchIt->second); |
| 138 | } |
| 139 | activeMelPatchIt->second.csdActive = shouldActivateCsd; |
| 140 | } |
| 141 | } |
| 142 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 143 | void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle, |
| 144 | const PatchPanel::Patch& patch) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame^] | 145 | if (mSoundDoseManager->isCsdDisabled()) { |
| 146 | ALOGV("%s csd is disabled", __func__); |
| 147 | return; |
| 148 | } |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 149 | if (useHalSoundDoseInterface()) { |
| 150 | ALOGV("%s using HAL sound dose, ignore new patch", __func__); |
| 151 | return; |
| 152 | } |
| 153 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 154 | ALOGV("%s: handle %d mHalHandle %d device sink %08x", |
| 155 | __func__, handle, patch.mHalHandle, |
| 156 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 157 | if (patch.mAudioPatch.num_sources == 0 |
| 158 | || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 159 | ALOGV("%s: patch does not contain any mix sources", __func__); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
| 163 | audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle; |
| 164 | ActiveMelPatch newPatch; |
| 165 | newPatch.streamHandle = streamHandle; |
| 166 | for (int i = 0; i < patch.mAudioPatch.num_sinks; ++ i) { |
| 167 | if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE |
| 168 | && shouldComputeMelForDeviceType(patch.mAudioPatch.sinks[i].ext.device.type)) { |
| 169 | audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id; |
| 170 | newPatch.deviceHandles.push_back(deviceId); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 171 | AudioDeviceTypeAddr adt{patch.mAudioPatch.sinks[i].ext.device.type, |
| 172 | patch.mAudioPatch.sinks[i].ext.device.address}; |
| 173 | mSoundDoseManager->mapAddressToDeviceId(adt, deviceId); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 177 | std::lock_guard _afl(mAudioFlinger.mLock); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 178 | std::lock_guard _l(mLock); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 179 | ALOGV("%s add patch handle %d to active devices", __func__, handle); |
| 180 | startMelComputationForActivePatch_l(newPatch); |
| 181 | newPatch.csdActive = true; |
| 182 | mActiveMelPatches[handle] = newPatch; |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 185 | void AudioFlinger::MelReporter::startMelComputationForActivePatch_l(const ActiveMelPatch& patch) { |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 186 | auto outputThread = mAudioFlinger.checkOutputThread_l(patch.streamHandle); |
| 187 | if (outputThread == nullptr) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 188 | ALOGE("%s cannot find thread for stream handle %d", __func__, patch.streamHandle); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | for (const auto& deviceHandle : patch.deviceHandles) { |
| 193 | ++mActiveDevices[deviceHandle]; |
| 194 | ALOGI("%s add stream %d that uses device %d for CSD, nr of streams: %d", __func__, |
| 195 | patch.streamHandle, deviceHandle, mActiveDevices[deviceHandle]); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 196 | |
| 197 | if (outputThread != nullptr) { |
| 198 | outputThread->startMelComputation_l(mSoundDoseManager->getOrCreateProcessorForDevice( |
| 199 | deviceHandle, |
| 200 | patch.streamHandle, |
| 201 | outputThread->mSampleRate, |
| 202 | outputThread->mChannelCount, |
| 203 | outputThread->mFormat)); |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 204 | } |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 205 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 208 | void AudioFlinger::MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) { |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame^] | 209 | if (mSoundDoseManager->isCsdDisabled()) { |
| 210 | ALOGV("%s csd is disabled", __func__); |
| 211 | return; |
| 212 | } |
| 213 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 214 | ActiveMelPatch melPatch; |
| 215 | { |
| 216 | std::lock_guard _l(mLock); |
| 217 | |
| 218 | auto patchIt = mActiveMelPatches.find(handle); |
| 219 | if (patchIt == mActiveMelPatches.end()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 220 | ALOGV("%s patch handle %d does not contain any mix sources with active MEL calculation", |
| 221 | __func__, handle); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
| 225 | melPatch = patchIt->second; |
| 226 | mActiveMelPatches.erase(patchIt); |
| 227 | } |
| 228 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 229 | std::lock_guard _afl(mAudioFlinger.mLock); |
| 230 | std::lock_guard _l(mLock); |
| 231 | stopMelComputationForPatch_l(melPatch); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 234 | sp<media::ISoundDose> AudioFlinger::MelReporter::getSoundDoseInterface( |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 235 | const sp<media::ISoundDoseCallback>& callback) { |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 236 | // no need to lock since getSoundDoseInterface is synchronized |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 237 | return mSoundDoseManager->getSoundDoseInterface(callback); |
| 238 | } |
| 239 | |
| 240 | void AudioFlinger::MelReporter::stopInternalMelComputation() { |
| 241 | ALOGV("%s", __func__); |
Vlad Popa | 870f6d6 | 2022-12-22 12:15:47 +0100 | [diff] [blame] | 242 | std::lock_guard _l(mLock); |
| 243 | mActiveMelPatches.clear(); |
| 244 | mUseHalSoundDoseInterface = true; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 247 | void AudioFlinger::MelReporter::stopMelComputationForPatch_l(const ActiveMelPatch& patch) { |
Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 248 | if (!patch.csdActive) { |
| 249 | // no need to stop CSD inactive patches |
| 250 | return; |
| 251 | } |
| 252 | |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 253 | auto outputThread = mAudioFlinger.checkOutputThread_l(patch.streamHandle); |
| 254 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 255 | ALOGV("%s: stop MEL for stream id: %d", __func__, patch.streamHandle); |
| 256 | for (const auto& deviceId : patch.deviceHandles) { |
| 257 | if (mActiveDevices[deviceId] > 0) { |
| 258 | --mActiveDevices[deviceId]; |
| 259 | if (mActiveDevices[deviceId] == 0) { |
| 260 | // no stream is using deviceId anymore |
| 261 | ALOGI("%s removing device %d from active CSD devices", __func__, deviceId); |
| 262 | mSoundDoseManager->clearMapDeviceIdEntries(deviceId); |
| 263 | } |
| 264 | } |
| 265 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 266 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 267 | mSoundDoseManager->removeStreamProcessor(patch.streamHandle); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 268 | if (outputThread != nullptr) { |
| 269 | outputThread->stopMelComputation_l(); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 270 | } |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 273 | |
| 274 | std::optional<audio_patch_handle_t> AudioFlinger::MelReporter::activePatchStreamHandle_l( |
| 275 | audio_io_handle_t streamHandle) { |
| 276 | for(const auto& patchIt : mActiveMelPatches) { |
| 277 | if (patchIt.second.streamHandle == streamHandle) { |
| 278 | return patchIt.first; |
| 279 | } |
| 280 | } |
| 281 | return std::nullopt; |
| 282 | } |
| 283 | |
| 284 | bool AudioFlinger::MelReporter::useHalSoundDoseInterface() { |
| 285 | bool useHalSoundDoseInterface = !mSoundDoseManager->forceUseFrameworkMel(); |
| 286 | { |
| 287 | std::lock_guard _l(mLock); |
| 288 | useHalSoundDoseInterface &= mUseHalSoundDoseInterface; |
| 289 | } |
| 290 | return useHalSoundDoseInterface; |
| 291 | } |
| 292 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 293 | std::string AudioFlinger::MelReporter::dump() { |
| 294 | std::lock_guard _l(mLock); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 295 | std::string output("\nSound Dose:\n"); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 296 | output.append(mSoundDoseManager->dump()); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 297 | return output; |
| 298 | } |
| 299 | |
| 300 | } // namespace android |