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