Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2019, 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 | |
| 19 | #define LOG_TAG "AudioFlinger::DeviceEffectManager" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | #include <audio_utils/primitives.h> |
| 24 | |
| 25 | #include "AudioFlinger.h" |
| 26 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
| 27 | |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
| 30 | |
| 31 | namespace android { |
| 32 | |
Shunkai Yao | d7ea409 | 2022-12-12 00:44:33 +0000 | [diff] [blame^] | 33 | using detail::AudioHalVersionInfo; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 34 | using media::IEffectClient; |
| 35 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 36 | void AudioFlinger::DeviceEffectManager::createAudioPatch(audio_patch_handle_t handle, |
| 37 | const PatchPanel::Patch& patch) { |
| 38 | ALOGV("%s handle %d mHalHandle %d num sinks %d device sink %08x", |
| 39 | __func__, handle, patch.mHalHandle, |
| 40 | patch.mAudioPatch.num_sinks, |
| 41 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 42 | |
| 43 | mCommandThread->createAudioPatchCommand(handle, patch); |
| 44 | } |
| 45 | |
| 46 | void AudioFlinger::DeviceEffectManager::onCreateAudioPatch(audio_patch_handle_t handle, |
| 47 | const PatchPanel::Patch& patch) { |
| 48 | ALOGV("%s handle %d mHalHandle %d device sink %08x", |
| 49 | __func__, handle, patch.mHalHandle, |
| 50 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 51 | Mutex::Autolock _l(mLock); |
| 52 | for (auto& effect : mDeviceEffects) { |
| 53 | status_t status = effect.second->onCreatePatch(handle, patch); |
| 54 | ALOGV("%s Effect onCreatePatch status %d", __func__, status); |
| 55 | ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void AudioFlinger::DeviceEffectManager::releaseAudioPatch(audio_patch_handle_t handle) { |
| 60 | ALOGV("%s", __func__); |
| 61 | mCommandThread->releaseAudioPatchCommand(handle); |
| 62 | } |
| 63 | |
| 64 | void AudioFlinger::DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) { |
| 65 | ALOGV("%s", __func__); |
| 66 | Mutex::Autolock _l(mLock); |
| 67 | for (auto& effect : mDeviceEffects) { |
| 68 | effect.second->onReleasePatch(handle); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held |
| 73 | sp<AudioFlinger::EffectHandle> AudioFlinger::DeviceEffectManager::createEffect_l( |
| 74 | effect_descriptor_t *descriptor, |
| 75 | const AudioDeviceTypeAddr& device, |
| 76 | const sp<AudioFlinger::Client>& client, |
| 77 | const sp<IEffectClient>& effectClient, |
| 78 | const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches, |
| 79 | int *enabled, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 80 | status_t *status, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 81 | bool probe, |
| 82 | bool notifyFramesProcessed) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 83 | sp<DeviceEffectProxy> effect; |
| 84 | sp<EffectHandle> handle; |
| 85 | status_t lStatus; |
| 86 | |
| 87 | lStatus = checkEffectCompatibility(descriptor); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 88 | if (probe || lStatus != NO_ERROR) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 89 | *status = lStatus; |
| 90 | return handle; |
| 91 | } |
| 92 | |
| 93 | { |
| 94 | Mutex::Autolock _l(mLock); |
| 95 | auto iter = mDeviceEffects.find(device); |
| 96 | if (iter != mDeviceEffects.end()) { |
| 97 | effect = iter->second; |
| 98 | } else { |
| 99 | effect = new DeviceEffectProxy(device, mMyCallback, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 100 | descriptor, mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT), |
| 101 | notifyFramesProcessed); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 102 | } |
| 103 | // create effect handle and connect it to effect module |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 104 | handle = new EffectHandle(effect, client, effectClient, 0 /*priority*/, |
| 105 | notifyFramesProcessed); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 106 | lStatus = handle->initCheck(); |
| 107 | if (lStatus == NO_ERROR) { |
| 108 | lStatus = effect->addHandle(handle.get()); |
| 109 | if (lStatus == NO_ERROR) { |
Ram Mohan M | 2a05df2 | 2022-08-28 11:46:23 +0530 | [diff] [blame] | 110 | lStatus = effect->init(patches); |
| 111 | if (lStatus == NAME_NOT_FOUND) { |
| 112 | lStatus = NO_ERROR; |
| 113 | } |
| 114 | if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) { |
| 115 | mDeviceEffects.emplace(device, effect); |
| 116 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
| 120 | if (enabled != NULL) { |
| 121 | *enabled = (int)effect->isEnabled(); |
| 122 | } |
| 123 | *status = lStatus; |
| 124 | return handle; |
| 125 | } |
| 126 | |
| 127 | status_t AudioFlinger::DeviceEffectManager::checkEffectCompatibility( |
| 128 | const effect_descriptor_t *desc) { |
Eric Laurent | 9289bde | 2020-08-18 12:49:17 -0700 | [diff] [blame] | 129 | sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory(); |
| 130 | if (effectsFactory == nullptr) { |
| 131 | return BAD_VALUE; |
| 132 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 133 | |
Shunkai Yao | d7ea409 | 2022-12-12 00:44:33 +0000 | [diff] [blame^] | 134 | static AudioHalVersionInfo sMinDeviceEffectHalVersion = |
| 135 | AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0); |
| 136 | AudioHalVersionInfo halVersion = effectsFactory->getHalVersion(); |
Eric Laurent | 9289bde | 2020-08-18 12:49:17 -0700 | [diff] [blame] | 137 | |
Shunkai Yao | d7ea409 | 2022-12-12 00:44:33 +0000 | [diff] [blame^] | 138 | // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as |
| 139 | // long as the type, major and minor sequence doesn't change in the definition. |
Eric Laurent | 9289bde | 2020-08-18 12:49:17 -0700 | [diff] [blame] | 140 | if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC |
| 141 | && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) |
| 142 | || halVersion < sMinDeviceEffectHalVersion) { |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 143 | ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s", |
| 144 | __func__, desc->name, halVersion.toString().c_str()); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 145 | return BAD_VALUE; |
| 146 | } |
| 147 | |
| 148 | return NO_ERROR; |
| 149 | } |
| 150 | |
| 151 | status_t AudioFlinger::DeviceEffectManager::createEffectHal( |
| 152 | const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId, |
| 153 | sp<EffectHalInterface> *effect) { |
| 154 | status_t status = NO_INIT; |
| 155 | sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory(); |
| 156 | if (effectsFactory != 0) { |
| 157 | status = effectsFactory->createEffect( |
| 158 | pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect); |
| 159 | } |
| 160 | return status; |
| 161 | } |
| 162 | |
| 163 | void AudioFlinger::DeviceEffectManager::dump(int fd) { |
| 164 | const bool locked = dumpTryLock(mLock); |
| 165 | if (!locked) { |
| 166 | String8 result("DeviceEffectManager may be deadlocked\n"); |
| 167 | write(fd, result.string(), result.size()); |
| 168 | } |
| 169 | |
Phil Burk | 651d0a5 | 2020-05-08 14:00:58 -0700 | [diff] [blame] | 170 | String8 heading("\nDevice Effects:\n"); |
| 171 | write(fd, heading.string(), heading.size()); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 172 | for (const auto& iter : mDeviceEffects) { |
| 173 | String8 outStr; |
| 174 | outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "", |
| 175 | ::android::toString(iter.first.mType).c_str(), iter.first.getAddress()); |
| 176 | write(fd, outStr.string(), outStr.size()); |
| 177 | iter.second->dump(fd, 4); |
| 178 | } |
| 179 | |
| 180 | if (locked) { |
| 181 | mLock.unlock(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | size_t AudioFlinger::DeviceEffectManager::removeEffect(const sp<DeviceEffectProxy>& effect) |
| 187 | { |
| 188 | Mutex::Autolock _l(mLock); |
| 189 | mDeviceEffects.erase(effect->device()); |
| 190 | return mDeviceEffects.size(); |
| 191 | } |
| 192 | |
| 193 | bool AudioFlinger::DeviceEffectManagerCallback::disconnectEffectHandle( |
| 194 | EffectHandle *handle, bool unpinIfLast) { |
| 195 | sp<EffectBase> effectBase = handle->effect().promote(); |
| 196 | if (effectBase == nullptr) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | sp<DeviceEffectProxy> effect = effectBase->asDeviceEffectProxy(); |
| 201 | if (effect == nullptr) { |
| 202 | return false; |
| 203 | } |
| 204 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 205 | bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 206 | if (remove) { |
| 207 | mManager.removeEffect(effect); |
| 208 | if (handle->enabled()) { |
| 209 | effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/); |
| 210 | } |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | // ----------- DeviceEffectManager::CommandThread implementation ---------- |
| 216 | |
| 217 | |
| 218 | AudioFlinger::DeviceEffectManager::CommandThread::~CommandThread() |
| 219 | { |
| 220 | Mutex::Autolock _l(mLock); |
| 221 | mCommands.clear(); |
| 222 | } |
| 223 | |
| 224 | void AudioFlinger::DeviceEffectManager::CommandThread::onFirstRef() |
| 225 | { |
| 226 | run("DeviceEffectManage_CommandThread", ANDROID_PRIORITY_AUDIO); |
| 227 | } |
| 228 | |
| 229 | bool AudioFlinger::DeviceEffectManager::CommandThread::threadLoop() |
| 230 | { |
| 231 | mLock.lock(); |
| 232 | while (!exitPending()) |
| 233 | { |
| 234 | while (!mCommands.empty() && !exitPending()) { |
| 235 | sp<Command> command = mCommands.front(); |
| 236 | mCommands.pop_front(); |
| 237 | mLock.unlock(); |
| 238 | |
| 239 | switch (command->mCommand) { |
| 240 | case CREATE_AUDIO_PATCH: { |
| 241 | CreateAudioPatchData *data = (CreateAudioPatchData *)command->mData.get(); |
| 242 | ALOGV("CommandThread() processing create audio patch handle %d", data->mHandle); |
| 243 | mManager.onCreateAudioPatch(data->mHandle, data->mPatch); |
| 244 | } break; |
| 245 | case RELEASE_AUDIO_PATCH: { |
| 246 | ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mData.get(); |
| 247 | ALOGV("CommandThread() processing release audio patch handle %d", data->mHandle); |
| 248 | mManager.onReleaseAudioPatch(data->mHandle); |
| 249 | } break; |
| 250 | default: |
| 251 | ALOGW("CommandThread() unknown command %d", command->mCommand); |
| 252 | } |
| 253 | mLock.lock(); |
| 254 | } |
| 255 | |
| 256 | // At this stage we have either an empty command queue or the first command in the queue |
| 257 | // has a finite delay. So unless we are exiting it is safe to wait. |
| 258 | if (!exitPending()) { |
| 259 | ALOGV("CommandThread() going to sleep"); |
| 260 | mWaitWorkCV.wait(mLock); |
| 261 | } |
| 262 | } |
| 263 | mLock.unlock(); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | void AudioFlinger::DeviceEffectManager::CommandThread::sendCommand(sp<Command> command) { |
| 268 | Mutex::Autolock _l(mLock); |
| 269 | mCommands.push_back(command); |
| 270 | mWaitWorkCV.signal(); |
| 271 | } |
| 272 | |
| 273 | void AudioFlinger::DeviceEffectManager::CommandThread::createAudioPatchCommand( |
| 274 | audio_patch_handle_t handle, const PatchPanel::Patch& patch) |
| 275 | { |
| 276 | sp<Command> command = new Command(CREATE_AUDIO_PATCH, new CreateAudioPatchData(handle, patch)); |
| 277 | ALOGV("CommandThread() adding create patch handle %d mHalHandle %d.", handle, patch.mHalHandle); |
| 278 | sendCommand(command); |
| 279 | } |
| 280 | |
| 281 | void AudioFlinger::DeviceEffectManager::CommandThread::releaseAudioPatchCommand( |
| 282 | audio_patch_handle_t handle) |
| 283 | { |
| 284 | sp<Command> command = new Command(RELEASE_AUDIO_PATCH, new ReleaseAudioPatchData(handle)); |
| 285 | ALOGV("CommandThread() adding release patch"); |
| 286 | sendCommand(command); |
| 287 | } |
| 288 | |
| 289 | void AudioFlinger::DeviceEffectManager::CommandThread::exit() |
| 290 | { |
| 291 | ALOGV("CommandThread::exit"); |
| 292 | { |
| 293 | AutoMutex _l(mLock); |
| 294 | requestExit(); |
| 295 | mWaitWorkCV.signal(); |
| 296 | } |
| 297 | // Note that we can call it from the thread loop if all other references have been released |
| 298 | // but it will safely return WOULD_BLOCK in this case |
| 299 | requestExitAndWait(); |
| 300 | } |
| 301 | |
| 302 | } // namespace android |