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