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> |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 25 | #include <android/binder_manager.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
Vlad Popa | debe0a7 | 2022-12-28 16:55:13 +0100 | [diff] [blame^] | 28 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 29 | using aidl::android::hardware::audio::sounddose::ISoundDoseFactory; |
| 30 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 33 | constexpr std::string_view kSoundDoseInterfaceModule = "/default"; |
| 34 | |
| 35 | bool AudioFlinger::MelReporter::activateHalSoundDoseComputation(const std::string& module) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 36 | if (mSoundDoseManager->forceUseFrameworkMel()) { |
| 37 | ALOGD("%s: Forcing use of internal MEL computation.", __func__); |
| 38 | activateInternalSoundDoseComputation(); |
| 39 | return false; |
| 40 | } |
| 41 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 42 | if (mSoundDoseFactory == nullptr) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 43 | ALOGW("%s: sound dose HAL reporting not available", __func__); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 44 | activateInternalSoundDoseComputation(); |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | std::shared_ptr<ISoundDose> soundDoseInterface; |
| 49 | auto result = mSoundDoseFactory->getSoundDose(module, &soundDoseInterface); |
| 50 | if (!result.isOk()) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 51 | ALOGW("%s: HAL cannot provide sound dose interface for module %s", |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 52 | __func__, module.c_str()); |
| 53 | activateInternalSoundDoseComputation(); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | if (!mSoundDoseManager->setHalSoundDoseInterface(soundDoseInterface)) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 58 | 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] | 59 | activateInternalSoundDoseComputation(); |
| 60 | return false; |
| 61 | } |
| 62 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 63 | stopInternalMelComputation(); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void AudioFlinger::MelReporter::activateInternalSoundDoseComputation() { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 68 | { |
| 69 | std::lock_guard _l(mLock); |
| 70 | if (!mUseHalSoundDoseInterface) { |
| 71 | // no need to start internal MEL on active patches |
| 72 | return; |
| 73 | } |
| 74 | mUseHalSoundDoseInterface = false; |
| 75 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 76 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 77 | mSoundDoseManager->setHalSoundDoseInterface(nullptr); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void AudioFlinger::MelReporter::onFirstRef() { |
| 81 | mAudioFlinger.mPatchCommandThread->addListener(this); |
| 82 | |
| 83 | std::string interface = |
| 84 | std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data(); |
| 85 | AIBinder* binder = AServiceManager_checkService(interface.c_str()); |
| 86 | if (binder == nullptr) { |
| 87 | ALOGW("%s service %s doesn't exist", __func__, interface.c_str()); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | mSoundDoseFactory = ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder)); |
| 92 | } |
| 93 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 94 | bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t device) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 95 | if (mSoundDoseManager->forceComputeCsdOnAllDevices()) { |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 99 | switch (device) { |
| 100 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 101 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 102 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 103 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: |
| 104 | case AUDIO_DEVICE_OUT_HEARING_AID: |
| 105 | case AUDIO_DEVICE_OUT_USB_HEADSET: |
| 106 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
| 107 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 108 | return true; |
| 109 | default: |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle, |
| 115 | const PatchPanel::Patch& patch) { |
| 116 | ALOGV("%s: handle %d mHalHandle %d device sink %08x", |
| 117 | __func__, handle, patch.mHalHandle, |
| 118 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 119 | if (patch.mAudioPatch.num_sources == 0 |
| 120 | || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) { |
| 121 | ALOGW("%s: patch does not contain any mix sources", __func__); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle; |
| 126 | ActiveMelPatch newPatch; |
| 127 | newPatch.streamHandle = streamHandle; |
| 128 | for (int i = 0; i < patch.mAudioPatch.num_sinks; ++ i) { |
| 129 | if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE |
| 130 | && shouldComputeMelForDeviceType(patch.mAudioPatch.sinks[i].ext.device.type)) { |
| 131 | audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id; |
| 132 | newPatch.deviceHandles.push_back(deviceId); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 133 | AudioDeviceTypeAddr adt{patch.mAudioPatch.sinks[i].ext.device.type, |
| 134 | patch.mAudioPatch.sinks[i].ext.device.address}; |
| 135 | mSoundDoseManager->mapAddressToDeviceId(adt, deviceId); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 136 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 137 | bool useHalSoundDoseInterface = !mSoundDoseManager->forceUseFrameworkMel(); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 138 | { |
| 139 | std::lock_guard _l(mLock); |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 140 | useHalSoundDoseInterface &= mUseHalSoundDoseInterface; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 141 | } |
| 142 | if (!useHalSoundDoseInterface) { |
| 143 | startMelComputationForNewPatch(streamHandle, deviceId); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | std::lock_guard _l(mLock); |
| 149 | mActiveMelPatches[patch.mAudioPatch.id] = newPatch; |
| 150 | } |
| 151 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 152 | void AudioFlinger::MelReporter::startMelComputationForNewPatch( |
| 153 | audio_io_handle_t streamHandle, audio_port_handle_t deviceId) { |
| 154 | // Start the MEL calculation in the PlaybackThread |
| 155 | std::lock_guard _lAf(mAudioFlinger.mLock); |
| 156 | auto thread = mAudioFlinger.checkPlaybackThread_l(streamHandle); |
| 157 | if (thread != nullptr) { |
| 158 | thread->startMelComputation(mSoundDoseManager->getOrCreateProcessorForDevice( |
| 159 | deviceId, |
| 160 | streamHandle, |
| 161 | thread->mSampleRate, |
| 162 | thread->mChannelCount, |
| 163 | thread->mFormat)); |
| 164 | } |
| 165 | } |
| 166 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 167 | void AudioFlinger::MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) { |
| 168 | ALOGV("%s", __func__); |
| 169 | |
| 170 | ActiveMelPatch melPatch; |
| 171 | { |
| 172 | std::lock_guard _l(mLock); |
| 173 | |
| 174 | auto patchIt = mActiveMelPatches.find(handle); |
| 175 | if (patchIt == mActiveMelPatches.end()) { |
| 176 | ALOGW( |
| 177 | "%s patch does not contain any mix sources with active MEL calculation", |
| 178 | __func__); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | melPatch = patchIt->second; |
| 183 | mActiveMelPatches.erase(patchIt); |
| 184 | } |
| 185 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 186 | for (const auto& deviceId : melPatch.deviceHandles) { |
| 187 | mSoundDoseManager->clearMapDeviceIdEntries(deviceId); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 188 | } |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 189 | stopInternalMelComputationForStream(melPatch.streamHandle); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 192 | sp<media::ISoundDose> AudioFlinger::MelReporter::getSoundDoseInterface( |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 193 | const sp<media::ISoundDoseCallback>& callback) { |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 194 | // no need to lock since getSoundDoseInterface is synchronized |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 195 | return mSoundDoseManager->getSoundDoseInterface(callback); |
| 196 | } |
| 197 | |
| 198 | void AudioFlinger::MelReporter::stopInternalMelComputation() { |
| 199 | ALOGV("%s", __func__); |
Vlad Popa | 870f6d6 | 2022-12-22 12:15:47 +0100 | [diff] [blame] | 200 | std::lock_guard _l(mLock); |
| 201 | mActiveMelPatches.clear(); |
| 202 | mUseHalSoundDoseInterface = true; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void AudioFlinger::MelReporter::stopInternalMelComputationForStream(audio_io_handle_t streamId) { |
| 206 | ALOGV("%s: stop internal mel for stream id: %d", __func__, streamId); |
| 207 | |
| 208 | std::lock_guard _lAf(mAudioFlinger.mLock); |
| 209 | mSoundDoseManager->removeStreamProcessor(streamId); |
| 210 | auto thread = mAudioFlinger.checkPlaybackThread_l(streamId); |
| 211 | if (thread != nullptr) { |
| 212 | thread->stopMelComputation(); |
| 213 | } |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 216 | std::string AudioFlinger::MelReporter::dump() { |
| 217 | std::lock_guard _l(mLock); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 218 | std::string output("\nSound Dose:\n"); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 219 | output.append(mSoundDoseManager->dump()); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 220 | return output; |
| 221 | } |
| 222 | |
| 223 | } // namespace android |