Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2010, 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_NDEBUG 0 |
| 20 | #define LOG_TAG "AudioEffect" |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <limits.h> |
| 25 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 26 | #include <android/media/IAudioPolicyService.h> |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 28 | #include <media/AidlConversion.h> |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 29 | #include <media/AudioEffect.h> |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 30 | #include <media/PolicyAidlConversion.h> |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 31 | #include <media/ShmemCompat.h> |
| 32 | #include <private/media/AudioEffectShared.h> |
| 33 | #include <utils/Log.h> |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 36 | using aidl_utils::statusTFromBinderStatus; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 37 | using binder::Status; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 38 | using media::IAudioPolicyService; |
Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 39 | using media::audio::common::AudioSource; |
| 40 | using media::audio::common::AudioUuid; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 41 | |
| 42 | namespace { |
| 43 | |
| 44 | // Copy from a raw pointer + size into a vector of bytes. |
| 45 | void appendToBuffer(const void* data, |
| 46 | size_t size, |
| 47 | std::vector<uint8_t>* buffer) { |
| 48 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 49 | buffer->insert(buffer->end(), p, p + size); |
| 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 54 | // --------------------------------------------------------------------------- |
| 55 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 56 | AudioEffect::AudioEffect(const android::content::AttributionSourceState& attributionSource) |
| 57 | : mClientAttributionSource(attributionSource) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 61 | status_t AudioEffect::set(const effect_uuid_t *type, |
| 62 | const effect_uuid_t *uuid, |
| 63 | int32_t priority, |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 64 | const wp<IAudioEffectCallback>& callback, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 65 | audio_session_t sessionId, |
Eric Laurent | 9487603 | 2019-11-13 12:45:28 -0800 | [diff] [blame] | 66 | audio_io_handle_t io, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 67 | const AudioDeviceTypeAddr& device, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 68 | bool probe, |
| 69 | bool notifyFramesProcessed) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 70 | { |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 71 | sp<media::IEffect> iEffect; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 72 | sp<IMemory> cblk; |
| 73 | int enabled; |
| 74 | |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 75 | ALOGV("set %p uuid: %p timeLow %08x", this, type, type ? type->timeLow : 0); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 76 | |
| 77 | if (mIEffect != 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 78 | ALOGW("Effect already in use"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 79 | return INVALID_OPERATION; |
| 80 | } |
| 81 | |
Eric Laurent | 9487603 | 2019-11-13 12:45:28 -0800 | [diff] [blame] | 82 | if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) { |
| 83 | ALOGW("IO handle should not be specified for device effect"); |
| 84 | return BAD_VALUE; |
| 85 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 86 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 87 | if (audioFlinger == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 88 | ALOGE("set(): Could not get audioflinger"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 89 | return NO_INIT; |
| 90 | } |
| 91 | |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 92 | if (type == nullptr && uuid == nullptr) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 93 | ALOGW("Must specify at least type or uuid"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 94 | return BAD_VALUE; |
| 95 | } |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 96 | mProbe = probe; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 97 | mPriority = priority; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 98 | mSessionId = sessionId; |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 99 | mCallback = callback; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 100 | |
| 101 | memset(&mDescriptor, 0, sizeof(effect_descriptor_t)); |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 102 | mDescriptor.type = *(type != nullptr ? type : EFFECT_UUID_NULL); |
| 103 | mDescriptor.uuid = *(uuid != nullptr ? uuid : EFFECT_UUID_NULL); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 104 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 105 | // TODO b/182392769: use attribution source util |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 106 | mIEffectClient = new EffectClient(this); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 107 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 108 | mClientAttributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(pid)); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 109 | pid_t uid = IPCThreadState::self()->getCallingUid(); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 110 | mClientAttributionSource.uid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 111 | |
Ytai Ben-Tsvi | ce18294 | 2020-11-04 14:48:01 -0800 | [diff] [blame] | 112 | media::CreateEffectRequest request; |
| 113 | request.desc = VALUE_OR_RETURN_STATUS( |
| 114 | legacy2aidl_effect_descriptor_t_EffectDescriptor(mDescriptor)); |
| 115 | request.client = mIEffectClient; |
| 116 | request.priority = priority; |
| 117 | request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io)); |
| 118 | request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(mSessionId)); |
| 119 | request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device)); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 120 | request.attributionSource = mClientAttributionSource; |
Ytai Ben-Tsvi | ce18294 | 2020-11-04 14:48:01 -0800 | [diff] [blame] | 121 | request.probe = probe; |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 122 | request.notifyFramesProcessed = notifyFramesProcessed; |
Ytai Ben-Tsvi | ce18294 | 2020-11-04 14:48:01 -0800 | [diff] [blame] | 123 | |
| 124 | media::CreateEffectResponse response; |
| 125 | |
| 126 | mStatus = audioFlinger->createEffect(request, &response); |
| 127 | |
| 128 | if (mStatus == OK) { |
Eric Laurent | 4f1d4e9 | 2022-02-01 14:37:16 +0100 | [diff] [blame] | 129 | if (response.alreadyExists) { |
| 130 | mStatus = ALREADY_EXISTS; |
| 131 | } |
Ytai Ben-Tsvi | ce18294 | 2020-11-04 14:48:01 -0800 | [diff] [blame] | 132 | mId = response.id; |
| 133 | enabled = response.enabled; |
| 134 | iEffect = response.effect; |
| 135 | mDescriptor = VALUE_OR_RETURN_STATUS( |
| 136 | aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc)); |
| 137 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 138 | |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 139 | // In probe mode, we stop here and return the status: the IEffect interface to |
| 140 | // audio flinger will not be retained. initCheck() will return the creation status |
| 141 | // but all other APIs will return invalid operation. |
| 142 | if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) { |
Mikhail Naganov | abd6e9d | 2020-03-23 22:25:56 +0000 | [diff] [blame] | 143 | char typeBuffer[64] = {}, uuidBuffer[64] = {}; |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 144 | guidToString(type, typeBuffer, sizeof(typeBuffer)); |
| 145 | guidToString(uuid, uuidBuffer, sizeof(uuidBuffer)); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 146 | ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d", |
Mikhail Naganov | abd6e9d | 2020-03-23 22:25:56 +0000 | [diff] [blame] | 147 | type != nullptr ? typeBuffer : "NULL", |
| 148 | uuid != nullptr ? uuidBuffer : "NULL", |
| 149 | mStatus); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 150 | if (!probe && iEffect == 0) { |
Eric Laurent | eecd765 | 2015-06-04 16:20:16 -0700 | [diff] [blame] | 151 | mStatus = NO_INIT; |
| 152 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 153 | return mStatus; |
| 154 | } |
| 155 | |
| 156 | mEnabled = (volatile int32_t)enabled; |
| 157 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 158 | if (media::SharedFileRegion shmem; |
| 159 | !iEffect->getCblk(&shmem).isOk() |
| 160 | || !convertSharedFileRegionToIMemory(shmem, &cblk) |
| 161 | || cblk == 0) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 162 | mStatus = NO_INIT; |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 163 | ALOGE("Could not get control block"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 164 | return mStatus; |
| 165 | } |
| 166 | |
Eric Laurent | eecd765 | 2015-06-04 16:20:16 -0700 | [diff] [blame] | 167 | mIEffect = iEffect; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 168 | mCblkMemory = cblk; |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 169 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 170 | // (see declaration for details). |
| 171 | // Either document why it is safe in this case or address the |
| 172 | // issue (e.g. by copying). |
| 173 | mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer()); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 174 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 175 | mCblk->buffer = (uint8_t *)mCblk + bufOffset; |
| 176 | |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 177 | IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient); |
Jean-Michel Trivi | a0fd9ca | 2014-09-18 14:07:18 -0700 | [diff] [blame] | 178 | ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 179 | mStatus, mEnabled, mClientAttributionSource.pid); |
Jean-Michel Trivi | a0fd9ca | 2014-09-18 14:07:18 -0700 | [diff] [blame] | 180 | |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 181 | if (!audio_is_global_session(mSessionId)) { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 182 | AudioSystem::acquireAudioSessionId(mSessionId, pid, uid); |
Jean-Michel Trivi | a0fd9ca | 2014-09-18 14:07:18 -0700 | [diff] [blame] | 183 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 184 | |
| 185 | return mStatus; |
| 186 | } |
| 187 | |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 188 | namespace { |
| 189 | class LegacyCallbackWrapper : public AudioEffect::IAudioEffectCallback { |
| 190 | public: |
| 191 | LegacyCallbackWrapper(AudioEffect::legacy_callback_t callback, void* user): |
| 192 | mCallback(callback), mUser(user) {} |
| 193 | private: |
| 194 | void onControlStatusChanged(bool isGranted) override { |
| 195 | mCallback(AudioEffect::EVENT_CONTROL_STATUS_CHANGED, mUser, &isGranted); |
| 196 | } |
| 197 | |
| 198 | void onEnableStatusChanged(bool isEnabled) override { |
| 199 | mCallback(AudioEffect::EVENT_ENABLE_STATUS_CHANGED, mUser, &isEnabled); |
| 200 | } |
| 201 | |
| 202 | void onParameterChanged(std::vector<uint8_t> param) override { |
| 203 | mCallback(AudioEffect::EVENT_PARAMETER_CHANGED, mUser, param.data()); |
| 204 | } |
| 205 | |
| 206 | void onError(status_t errorCode) override { |
| 207 | mCallback(AudioEffect::EVENT_ERROR, mUser, &errorCode); |
| 208 | } |
| 209 | |
| 210 | void onFramesProcessed(int32_t framesProcessed) override { |
| 211 | mCallback(AudioEffect::EVENT_FRAMES_PROCESSED, mUser, &framesProcessed); |
| 212 | } |
| 213 | |
| 214 | const AudioEffect::legacy_callback_t mCallback; |
| 215 | void* const mUser; |
| 216 | }; |
| 217 | } // namespace |
| 218 | |
| 219 | status_t AudioEffect::set(const effect_uuid_t *type, |
| 220 | const effect_uuid_t *uuid, |
| 221 | int32_t priority, |
| 222 | legacy_callback_t cbf, |
| 223 | void* user, |
| 224 | audio_session_t sessionId, |
| 225 | audio_io_handle_t io, |
| 226 | const AudioDeviceTypeAddr& device, |
| 227 | bool probe, |
| 228 | bool notifyFramesProcessed) |
| 229 | { |
| 230 | if (cbf != nullptr) { |
| 231 | mLegacyWrapper = sp<LegacyCallbackWrapper>::make(cbf, user); |
| 232 | } else if (user != nullptr) { |
| 233 | LOG_ALWAYS_FATAL("%s: User provided without callback", __func__); |
| 234 | } |
| 235 | return set(type, uuid, priority, mLegacyWrapper, sessionId, io, device, probe, |
| 236 | notifyFramesProcessed); |
| 237 | } |
Mikhail Naganov | 416fffe | 2020-07-31 17:36:08 -0700 | [diff] [blame] | 238 | status_t AudioEffect::set(const char *typeStr, |
| 239 | const char *uuidStr, |
| 240 | int32_t priority, |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 241 | const wp<IAudioEffectCallback>& callback, |
Mikhail Naganov | 416fffe | 2020-07-31 17:36:08 -0700 | [diff] [blame] | 242 | audio_session_t sessionId, |
| 243 | audio_io_handle_t io, |
| 244 | const AudioDeviceTypeAddr& device, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 245 | bool probe, |
| 246 | bool notifyFramesProcessed) |
Mikhail Naganov | 416fffe | 2020-07-31 17:36:08 -0700 | [diff] [blame] | 247 | { |
| 248 | effect_uuid_t type; |
| 249 | effect_uuid_t *pType = nullptr; |
| 250 | effect_uuid_t uuid; |
| 251 | effect_uuid_t *pUuid = nullptr; |
| 252 | |
| 253 | ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s", |
| 254 | typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr"); |
| 255 | |
| 256 | if (stringToGuid(typeStr, &type) == NO_ERROR) { |
| 257 | pType = &type; |
| 258 | } |
| 259 | if (stringToGuid(uuidStr, &uuid) == NO_ERROR) { |
| 260 | pUuid = &uuid; |
| 261 | } |
| 262 | |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 263 | return set(pType, pUuid, priority, callback, sessionId, io, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 264 | device, probe, notifyFramesProcessed); |
Mikhail Naganov | 416fffe | 2020-07-31 17:36:08 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 267 | status_t AudioEffect::set(const char *typeStr, |
| 268 | const char *uuidStr, |
| 269 | int32_t priority, |
| 270 | legacy_callback_t cbf, |
| 271 | void* user, |
| 272 | audio_session_t sessionId, |
| 273 | audio_io_handle_t io, |
| 274 | const AudioDeviceTypeAddr& device, |
| 275 | bool probe, |
| 276 | bool notifyFramesProcessed) |
| 277 | { |
| 278 | if (cbf != nullptr) { |
| 279 | mLegacyWrapper = sp<LegacyCallbackWrapper>::make(cbf, user); |
| 280 | } else if (user != nullptr) { |
| 281 | LOG_ALWAYS_FATAL("%s: User provided without callback", __func__); |
| 282 | } |
| 283 | return set(typeStr, uuidStr, priority, mLegacyWrapper, sessionId, io, device, probe, |
| 284 | notifyFramesProcessed); |
| 285 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 286 | AudioEffect::~AudioEffect() |
| 287 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 288 | ALOGV("Destructor %p", this); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 289 | |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 290 | if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 291 | if (!audio_is_global_session(mSessionId)) { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 292 | AudioSystem::releaseAudioSessionId(mSessionId, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 293 | VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mClientAttributionSource.pid))); |
Jean-Michel Trivi | a0fd9ca | 2014-09-18 14:07:18 -0700 | [diff] [blame] | 294 | } |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 295 | if (mIEffect != nullptr) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 296 | mIEffect->disconnect(); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 297 | IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 298 | } |
Eric Laurent | eecd765 | 2015-06-04 16:20:16 -0700 | [diff] [blame] | 299 | mIEffect.clear(); |
| 300 | mCblkMemory.clear(); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 301 | } |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 302 | mIEffectClient.clear(); |
| 303 | IPCThreadState::self()->flushCommands(); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | status_t AudioEffect::initCheck() const |
| 308 | { |
| 309 | return mStatus; |
| 310 | } |
| 311 | |
| 312 | // ------------------------------------------------------------------------- |
| 313 | |
| 314 | effect_descriptor_t AudioEffect::descriptor() const |
| 315 | { |
| 316 | return mDescriptor; |
| 317 | } |
| 318 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 319 | bool AudioEffect::getEnabled() const |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 320 | { |
| 321 | return (mEnabled != 0); |
| 322 | } |
| 323 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 324 | status_t AudioEffect::setEnabled(bool enabled) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 325 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 326 | if (mProbe) { |
| 327 | return INVALID_OPERATION; |
| 328 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 329 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 330 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 331 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 332 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 333 | status_t status = NO_ERROR; |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 334 | AutoMutex lock(mLock); |
| 335 | if (enabled != mEnabled) { |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 336 | Status bs; |
| 337 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 338 | if (enabled) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 339 | ALOGV("enable %p", this); |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 340 | bs = mIEffect->enable(&status); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 341 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 342 | ALOGV("disable %p", this); |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 343 | bs = mIEffect->disable(&status); |
| 344 | } |
| 345 | if (!bs.isOk()) { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 346 | status = statusTFromBinderStatus(bs); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 347 | } |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 348 | if (status == NO_ERROR) { |
| 349 | mEnabled = enabled; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 350 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 351 | } |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 352 | return status; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 355 | status_t AudioEffect::command(uint32_t cmdCode, |
| 356 | uint32_t cmdSize, |
| 357 | void *cmdData, |
| 358 | uint32_t *replySize, |
| 359 | void *replyData) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 360 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 361 | if (mProbe) { |
| 362 | return INVALID_OPERATION; |
| 363 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 364 | if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 365 | ALOGV("command() bad status %d", mStatus); |
John Grossman | af7d818 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 366 | return mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 367 | } |
| 368 | |
zhanglina1 | a0de6c6 | 2023-11-30 15:18:34 +0800 | [diff] [blame] | 369 | std::unique_lock ul(mLock, std::defer_lock); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 370 | if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) { |
zhanglina1 | a0de6c6 | 2023-11-30 15:18:34 +0800 | [diff] [blame] | 371 | ul.lock(); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 372 | if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) { |
| 373 | return NO_ERROR; |
| 374 | } |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 375 | if (replySize == nullptr || *replySize != sizeof(status_t) || replyData == nullptr) { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 376 | return BAD_VALUE; |
| 377 | } |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 380 | std::vector<uint8_t> data; |
| 381 | appendToBuffer(cmdData, cmdSize, &data); |
| 382 | |
| 383 | status_t status; |
| 384 | std::vector<uint8_t> response; |
| 385 | |
| 386 | Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status); |
| 387 | if (!bs.isOk()) { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 388 | status = statusTFromBinderStatus(bs); |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 389 | } |
| 390 | if (status == NO_ERROR) { |
| 391 | memcpy(replyData, response.data(), response.size()); |
| 392 | *replySize = response.size(); |
| 393 | } |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 394 | |
| 395 | if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 396 | if (status == NO_ERROR) { |
| 397 | status = *(status_t *)replyData; |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 398 | } |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 399 | if (status == NO_ERROR) { |
| 400 | mEnabled = (cmdCode == EFFECT_CMD_ENABLE); |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 401 | } |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 404 | return status; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 407 | status_t AudioEffect::setParameter(effect_param_t *param) |
| 408 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 409 | if (mProbe) { |
| 410 | return INVALID_OPERATION; |
| 411 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 412 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 413 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 416 | if (param == nullptr || param->psize == 0 || param->vsize == 0) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 417 | return BAD_VALUE; |
| 418 | } |
| 419 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 420 | uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 421 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 422 | ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data, |
| 423 | (param->psize == 8) ? *((int *)param->data + 1): -1); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 424 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 425 | std::vector<uint8_t> cmd; |
| 426 | appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd); |
| 427 | std::vector<uint8_t> response; |
| 428 | status_t status; |
| 429 | Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM, |
| 430 | cmd, |
| 431 | sizeof(int), |
| 432 | &response, |
| 433 | &status); |
| 434 | if (!bs.isOk()) { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 435 | status = statusTFromBinderStatus(bs); |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 436 | return status; |
| 437 | } |
| 438 | assert(response.size() == sizeof(int)); |
| 439 | memcpy(¶m->status, response.data(), response.size()); |
| 440 | return status; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | status_t AudioEffect::setParameterDeferred(effect_param_t *param) |
| 444 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 445 | if (mProbe) { |
| 446 | return INVALID_OPERATION; |
| 447 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 448 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 449 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 450 | } |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 451 | if (param == nullptr || param->psize == 0 || param->vsize == 0) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 452 | return BAD_VALUE; |
| 453 | } |
| 454 | |
| 455 | Mutex::Autolock _l(mCblk->lock); |
| 456 | |
| 457 | int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize; |
| 458 | int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int); |
| 459 | |
| 460 | if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) { |
| 461 | return NO_MEMORY; |
| 462 | } |
| 463 | int *p = (int *)(mCblk->buffer + mCblk->clientIndex); |
| 464 | *p++ = size; |
| 465 | memcpy(p, param, sizeof(effect_param_t) + psize); |
| 466 | mCblk->clientIndex += size; |
| 467 | |
| 468 | return NO_ERROR; |
| 469 | } |
| 470 | |
| 471 | status_t AudioEffect::setParameterCommit() |
| 472 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 473 | if (mProbe) { |
| 474 | return INVALID_OPERATION; |
| 475 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 476 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 477 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | Mutex::Autolock _l(mCblk->lock); |
| 481 | if (mCblk->clientIndex == 0) { |
| 482 | return INVALID_OPERATION; |
| 483 | } |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 484 | std::vector<uint8_t> cmd; |
| 485 | std::vector<uint8_t> response; |
| 486 | status_t status; |
| 487 | Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, |
| 488 | cmd, |
| 489 | 0, |
| 490 | &response, |
| 491 | &status); |
| 492 | if (!bs.isOk()) { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 493 | status = statusTFromBinderStatus(bs); |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 494 | } |
| 495 | return status; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | status_t AudioEffect::getParameter(effect_param_t *param) |
| 499 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 500 | if (mProbe) { |
| 501 | return INVALID_OPERATION; |
| 502 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 503 | if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) { |
John Grossman | af7d818 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 504 | return mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 505 | } |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 506 | if (param == nullptr || param->psize == 0 || param->vsize == 0) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 507 | return BAD_VALUE; |
| 508 | } |
| 509 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 510 | ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data, |
| 511 | (param->psize == 8) ? *((int *)param->data + 1): -1); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 512 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 513 | uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 514 | param->vsize; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 515 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 516 | status_t status; |
| 517 | std::vector<uint8_t> cmd; |
| 518 | std::vector<uint8_t> response; |
| 519 | appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd); |
| 520 | |
| 521 | Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status); |
| 522 | if (!bs.isOk()) { |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 523 | status = statusTFromBinderStatus(bs); |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 524 | return status; |
| 525 | } |
| 526 | memcpy(param, response.data(), response.size()); |
| 527 | return status; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Mikhail Naganov | 59984db | 2022-04-19 21:21:23 +0000 | [diff] [blame] | 530 | status_t AudioEffect::getConfigs( |
| 531 | audio_config_base_t *inputCfg, audio_config_base_t *outputCfg) |
| 532 | { |
| 533 | if (mProbe) { |
| 534 | return INVALID_OPERATION; |
| 535 | } |
| 536 | if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) { |
| 537 | return mStatus; |
| 538 | } |
| 539 | if (inputCfg == NULL || outputCfg == NULL) { |
| 540 | return BAD_VALUE; |
| 541 | } |
| 542 | status_t status; |
| 543 | media::EffectConfig cfg; |
| 544 | Status bs = mIEffect->getConfig(&cfg, &status); |
| 545 | if (!bs.isOk()) { |
| 546 | status = statusTFromBinderStatus(bs); |
| 547 | ALOGW("%s received status %d from binder transaction", __func__, status); |
| 548 | return status; |
| 549 | } |
| 550 | if (status == NO_ERROR) { |
| 551 | *inputCfg = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioConfigBase_audio_config_base_t( |
| 552 | cfg.inputCfg, cfg.isOnInputStream)); |
| 553 | *outputCfg = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioConfigBase_audio_config_base_t( |
| 554 | cfg.outputCfg, cfg.isOnInputStream)); |
| 555 | } else { |
| 556 | ALOGW("%s received status %d from the effect", __func__, status); |
| 557 | } |
| 558 | return status; |
| 559 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 560 | |
| 561 | // ------------------------------------------------------------------------- |
| 562 | |
| 563 | void AudioEffect::binderDied() |
| 564 | { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 565 | ALOGW("IEffect died"); |
John Grossman | af7d818 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 566 | mStatus = DEAD_OBJECT; |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 567 | auto cb = mCallback.promote(); |
| 568 | if (cb != nullptr) { |
| 569 | cb->onError(mStatus); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 570 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // ------------------------------------------------------------------------- |
| 574 | |
| 575 | void AudioEffect::controlStatusChanged(bool controlGranted) |
| 576 | { |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 577 | auto cb = mCallback.promote(); |
| 578 | ALOGV("controlStatusChanged %p control %d callback %p", this, controlGranted, cb.get()); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 579 | if (controlGranted) { |
| 580 | if (mStatus == ALREADY_EXISTS) { |
| 581 | mStatus = NO_ERROR; |
| 582 | } |
| 583 | } else { |
| 584 | if (mStatus == NO_ERROR) { |
| 585 | mStatus = ALREADY_EXISTS; |
| 586 | } |
| 587 | } |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 588 | if (cb != nullptr) { |
| 589 | cb->onControlStatusChanged(controlGranted); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | void AudioEffect::enableStatusChanged(bool enabled) |
| 594 | { |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 595 | auto cb = mCallback.promote(); |
| 596 | ALOGV("enableStatusChanged %p enabled %d mCallback %p", this, enabled, cb.get()); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 597 | if (mStatus == ALREADY_EXISTS) { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 598 | mEnabled = enabled; |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 599 | if (cb != nullptr) { |
| 600 | cb->onEnableStatusChanged(enabled); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 605 | void AudioEffect::commandExecuted(int32_t cmdCode, |
| 606 | const std::vector<uint8_t>& cmdData, |
| 607 | const std::vector<uint8_t>& replyData) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 608 | { |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 609 | if (cmdData.empty() || replyData.empty()) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 610 | return; |
| 611 | } |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 612 | auto cb = mCallback.promote(); |
| 613 | if (cb != nullptr && cmdCode == EFFECT_CMD_SET_PARAM) { |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 614 | std::vector<uint8_t> cmdDataCopy(cmdData); |
| 615 | effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data()); |
| 616 | cmd->status = *reinterpret_cast<const int32_t *>(replyData.data()); |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 617 | cb->onParameterChanged(std::move(cmdDataCopy)); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 621 | void AudioEffect::framesProcessed(int32_t frames) |
| 622 | { |
Atneya Nair | 4715607 | 2021-12-02 19:05:52 -0500 | [diff] [blame] | 623 | auto cb = mCallback.promote(); |
| 624 | if (cb != nullptr) { |
| 625 | cb->onFramesProcessed(frames); |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 629 | // ------------------------------------------------------------------------- |
| 630 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 631 | status_t AudioEffect::queryNumberEffects(uint32_t *numEffects) |
| 632 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 633 | if (numEffects == nullptr) { |
| 634 | return BAD_VALUE; |
| 635 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 636 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 637 | if (af == 0) return PERMISSION_DENIED; |
| 638 | return af->queryNumberEffects(numEffects); |
| 639 | } |
| 640 | |
Eric Laurent | ffe9c25 | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 641 | status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 642 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 643 | if (descriptor == nullptr) { |
| 644 | return BAD_VALUE; |
| 645 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 646 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 647 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | ffe9c25 | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 648 | return af->queryEffect(index, descriptor); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 651 | status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid, |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 652 | const effect_uuid_t *type, |
| 653 | uint32_t preferredTypeFlag, |
| 654 | effect_descriptor_t *descriptor) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 655 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 656 | if (uuid == nullptr || type == nullptr || descriptor == nullptr) { |
| 657 | return BAD_VALUE; |
| 658 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 659 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 660 | if (af == 0) return PERMISSION_DENIED; |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 661 | return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 664 | status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession, |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 665 | effect_descriptor_t *descriptors, |
| 666 | uint32_t *count) |
| 667 | { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 668 | if (descriptors == nullptr || count == nullptr) { |
| 669 | return BAD_VALUE; |
| 670 | } |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 671 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 672 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 673 | |
| 674 | int32_t audioSessionAidl = VALUE_OR_RETURN_STATUS( |
| 675 | legacy2aidl_audio_session_t_int32_t(audioSession)); |
Mikhail Naganov | 0078ee5 | 2021-09-30 23:06:20 +0000 | [diff] [blame] | 676 | media::audio::common::Int countAidl; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 677 | countAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*count)); |
| 678 | std::vector<media::EffectDescriptor> retAidl; |
| 679 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 680 | aps->queryDefaultPreProcessing(audioSessionAidl, &countAidl, &retAidl))); |
| 681 | *count = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(countAidl.value)); |
| 682 | RETURN_STATUS_IF_ERROR(convertRange(retAidl.begin(), retAidl.end(), descriptors, |
| 683 | aidl2legacy_EffectDescriptor_effect_descriptor_t)); |
| 684 | return OK; |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 685 | } |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 686 | |
| 687 | status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id) |
| 688 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 689 | if (id == nullptr) { |
| 690 | return BAD_VALUE; |
| 691 | } |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 692 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 693 | if (af == 0) return PERMISSION_DENIED; |
| 694 | *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
| 695 | return NO_ERROR; |
| 696 | } |
| 697 | |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 698 | status_t AudioEffect::addSourceDefaultEffect(const char *typeStr, |
| 699 | const String16& opPackageName, |
| 700 | const char *uuidStr, |
| 701 | int32_t priority, |
| 702 | audio_source_t source, |
| 703 | audio_unique_id_t *id) |
| 704 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 705 | if ((typeStr == nullptr && uuidStr == nullptr) || id == nullptr) { |
| 706 | return BAD_VALUE; |
| 707 | } |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 708 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 709 | if (aps == 0) return PERMISSION_DENIED; |
| 710 | |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 711 | // Convert type & uuid from string to effect_uuid_t. |
| 712 | effect_uuid_t type; |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 713 | if (typeStr != nullptr) { |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 714 | status_t res = stringToGuid(typeStr, &type); |
| 715 | if (res != OK) return res; |
| 716 | } else { |
| 717 | type = *EFFECT_UUID_NULL; |
| 718 | } |
| 719 | |
| 720 | effect_uuid_t uuid; |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 721 | if (uuidStr != nullptr) { |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 722 | status_t res = stringToGuid(uuidStr, &uuid); |
| 723 | if (res != OK) return res; |
| 724 | } else { |
| 725 | uuid = *EFFECT_UUID_NULL; |
| 726 | } |
| 727 | |
Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 728 | AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type)); |
| 729 | AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid)); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 730 | std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS( |
| 731 | legacy2aidl_String16_string(opPackageName)); |
Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 732 | AudioSource sourceAidl = VALUE_OR_RETURN_STATUS( |
| 733 | legacy2aidl_audio_source_t_AudioSource(source)); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 734 | int32_t retAidl; |
| 735 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 736 | aps->addSourceDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, sourceAidl, |
| 737 | &retAidl))); |
| 738 | *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl)); |
| 739 | return OK; |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 742 | status_t AudioEffect::addStreamDefaultEffect(const char *typeStr, |
| 743 | const String16& opPackageName, |
| 744 | const char *uuidStr, |
| 745 | int32_t priority, |
| 746 | audio_usage_t usage, |
| 747 | audio_unique_id_t *id) |
| 748 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 749 | if ((typeStr == nullptr && uuidStr == nullptr) || id == nullptr) { |
| 750 | return BAD_VALUE; |
| 751 | } |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 752 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 753 | if (aps == 0) return PERMISSION_DENIED; |
| 754 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 755 | // Convert type & uuid from string to effect_uuid_t. |
| 756 | effect_uuid_t type; |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 757 | if (typeStr != nullptr) { |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 758 | status_t res = stringToGuid(typeStr, &type); |
| 759 | if (res != OK) return res; |
| 760 | } else { |
| 761 | type = *EFFECT_UUID_NULL; |
| 762 | } |
| 763 | |
| 764 | effect_uuid_t uuid; |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 765 | if (uuidStr != nullptr) { |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 766 | status_t res = stringToGuid(uuidStr, &uuid); |
| 767 | if (res != OK) return res; |
| 768 | } else { |
| 769 | uuid = *EFFECT_UUID_NULL; |
| 770 | } |
| 771 | |
Mikhail Naganov | ddceecc | 2021-09-03 13:58:56 -0700 | [diff] [blame] | 772 | AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type)); |
| 773 | AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid)); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 774 | std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS( |
| 775 | legacy2aidl_String16_string(opPackageName)); |
Mikhail Naganov | dbf0364 | 2021-08-25 18:15:32 -0700 | [diff] [blame] | 776 | media::audio::common::AudioUsage usageAidl = VALUE_OR_RETURN_STATUS( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 777 | legacy2aidl_audio_usage_t_AudioUsage(usage)); |
| 778 | int32_t retAidl; |
| 779 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 780 | aps->addStreamDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, usageAidl, |
| 781 | &retAidl))); |
| 782 | *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl)); |
| 783 | return OK; |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 786 | status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id) |
| 787 | { |
| 788 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 789 | if (aps == 0) return PERMISSION_DENIED; |
| 790 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 791 | int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id)); |
| 792 | return statusTFromBinderStatus(aps->removeSourceDefaultEffect(idAidl)); |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 795 | status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id) |
| 796 | { |
| 797 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 798 | if (aps == 0) return PERMISSION_DENIED; |
| 799 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 800 | int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id)); |
| 801 | return statusTFromBinderStatus(aps->removeStreamDefaultEffect(idAidl)); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 804 | // ------------------------------------------------------------------------- |
| 805 | |
| 806 | status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid) |
| 807 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 808 | if (str == nullptr || guid == nullptr) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 809 | return BAD_VALUE; |
| 810 | } |
| 811 | |
| 812 | int tmp[10]; |
| 813 | |
| 814 | if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x", |
| 815 | tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) { |
| 816 | return BAD_VALUE; |
| 817 | } |
| 818 | guid->timeLow = (uint32_t)tmp[0]; |
| 819 | guid->timeMid = (uint16_t)tmp[1]; |
| 820 | guid->timeHiAndVersion = (uint16_t)tmp[2]; |
| 821 | guid->clockSeq = (uint16_t)tmp[3]; |
| 822 | guid->node[0] = (uint8_t)tmp[4]; |
| 823 | guid->node[1] = (uint8_t)tmp[5]; |
| 824 | guid->node[2] = (uint8_t)tmp[6]; |
| 825 | guid->node[3] = (uint8_t)tmp[7]; |
| 826 | guid->node[4] = (uint8_t)tmp[8]; |
| 827 | guid->node[5] = (uint8_t)tmp[9]; |
| 828 | |
| 829 | return NO_ERROR; |
| 830 | } |
| 831 | |
| 832 | status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen) |
| 833 | { |
Mikhail Naganov | 097e3b9 | 2022-04-27 22:30:22 +0000 | [diff] [blame] | 834 | if (guid == nullptr || str == nullptr) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 835 | return BAD_VALUE; |
| 836 | } |
| 837 | |
| 838 | snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x", |
| 839 | guid->timeLow, |
| 840 | guid->timeMid, |
| 841 | guid->timeHiAndVersion, |
| 842 | guid->clockSeq, |
| 843 | guid->node[0], |
| 844 | guid->node[1], |
| 845 | guid->node[2], |
| 846 | guid->node[3], |
| 847 | guid->node[4], |
| 848 | guid->node[5]); |
| 849 | |
| 850 | return NO_ERROR; |
| 851 | } |
| 852 | |
| 853 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 854 | } // namespace android |