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 | |
| 26 | #include <private/media/AudioEffectShared.h> |
| 27 | #include <media/AudioEffect.h> |
| 28 | |
| 29 | #include <utils/Log.h> |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
| 31 | |
| 32 | |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | // --------------------------------------------------------------------------- |
| 37 | |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 38 | AudioEffect::AudioEffect(const String16& opPackageName) |
Mikhail Naganov | c8e17ee | 2020-07-31 17:36:08 -0700 | [diff] [blame] | 39 | : mOpPackageName(opPackageName) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 43 | status_t AudioEffect::set(const effect_uuid_t *type, |
| 44 | const effect_uuid_t *uuid, |
| 45 | int32_t priority, |
| 46 | effect_callback_t cbf, |
| 47 | void* user, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 48 | audio_session_t sessionId, |
Eric Laurent | 9487603 | 2019-11-13 12:45:28 -0800 | [diff] [blame] | 49 | audio_io_handle_t io, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 50 | const AudioDeviceTypeAddr& device, |
| 51 | bool probe) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 52 | { |
| 53 | sp<IEffect> iEffect; |
| 54 | sp<IMemory> cblk; |
| 55 | int enabled; |
| 56 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 57 | 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] | 58 | |
| 59 | if (mIEffect != 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 60 | ALOGW("Effect already in use"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 61 | return INVALID_OPERATION; |
| 62 | } |
| 63 | |
Eric Laurent | 9487603 | 2019-11-13 12:45:28 -0800 | [diff] [blame] | 64 | if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) { |
| 65 | ALOGW("IO handle should not be specified for device effect"); |
| 66 | return BAD_VALUE; |
| 67 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 68 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 69 | if (audioFlinger == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 70 | ALOGE("set(): Could not get audioflinger"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 71 | return NO_INIT; |
| 72 | } |
| 73 | |
| 74 | if (type == NULL && uuid == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 75 | ALOGW("Must specify at least type or uuid"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 76 | return BAD_VALUE; |
| 77 | } |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 78 | mProbe = probe; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 79 | mPriority = priority; |
| 80 | mCbf = cbf; |
| 81 | mUserData = user; |
| 82 | mSessionId = sessionId; |
| 83 | |
| 84 | memset(&mDescriptor, 0, sizeof(effect_descriptor_t)); |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 85 | mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL); |
| 86 | mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 87 | |
| 88 | mIEffectClient = new EffectClient(this); |
Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 89 | mClientPid = IPCThreadState::self()->getCallingPid(); |
Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 90 | mClientUid = IPCThreadState::self()->getCallingUid(); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 91 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 92 | iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor, |
Eric Laurent | 9487603 | 2019-11-13 12:45:28 -0800 | [diff] [blame] | 93 | mIEffectClient, priority, io, mSessionId, device, mOpPackageName, mClientPid, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 94 | probe, &mStatus, &mId, &enabled); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 95 | |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 96 | // In probe mode, we stop here and return the status: the IEffect interface to |
| 97 | // audio flinger will not be retained. initCheck() will return the creation status |
| 98 | // but all other APIs will return invalid operation. |
| 99 | if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) { |
Mikhail Naganov | abd6e9d | 2020-03-23 22:25:56 +0000 | [diff] [blame] | 100 | char typeBuffer[64] = {}, uuidBuffer[64] = {}; |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 101 | guidToString(type, typeBuffer, sizeof(typeBuffer)); |
| 102 | guidToString(uuid, uuidBuffer, sizeof(uuidBuffer)); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 103 | 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] | 104 | type != nullptr ? typeBuffer : "NULL", |
| 105 | uuid != nullptr ? uuidBuffer : "NULL", |
| 106 | mStatus); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 107 | if (!probe && iEffect == 0) { |
Eric Laurent | eecd765 | 2015-06-04 16:20:16 -0700 | [diff] [blame] | 108 | mStatus = NO_INIT; |
| 109 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 110 | return mStatus; |
| 111 | } |
| 112 | |
| 113 | mEnabled = (volatile int32_t)enabled; |
| 114 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 115 | cblk = iEffect->getCblk(); |
| 116 | if (cblk == 0) { |
| 117 | mStatus = NO_INIT; |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 118 | ALOGE("Could not get control block"); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 119 | return mStatus; |
| 120 | } |
| 121 | |
Eric Laurent | eecd765 | 2015-06-04 16:20:16 -0700 | [diff] [blame] | 122 | mIEffect = iEffect; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 123 | mCblkMemory = cblk; |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 124 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 125 | // (see declaration for details). |
| 126 | // Either document why it is safe in this case or address the |
| 127 | // issue (e.g. by copying). |
| 128 | mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer()); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 129 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 130 | mCblk->buffer = (uint8_t *)mCblk + bufOffset; |
| 131 | |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 132 | IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient); |
Jean-Michel Trivi | a0fd9ca | 2014-09-18 14:07:18 -0700 | [diff] [blame] | 133 | ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId, |
| 134 | mStatus, mEnabled, mClientPid); |
| 135 | |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 136 | if (!audio_is_global_session(mSessionId)) { |
Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 137 | AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid); |
Jean-Michel Trivi | a0fd9ca | 2014-09-18 14:07:18 -0700 | [diff] [blame] | 138 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 139 | |
| 140 | return mStatus; |
| 141 | } |
| 142 | |
Mikhail Naganov | c8e17ee | 2020-07-31 17:36:08 -0700 | [diff] [blame] | 143 | status_t AudioEffect::set(const char *typeStr, |
| 144 | const char *uuidStr, |
| 145 | int32_t priority, |
| 146 | effect_callback_t cbf, |
| 147 | void* user, |
| 148 | audio_session_t sessionId, |
| 149 | audio_io_handle_t io, |
| 150 | const AudioDeviceTypeAddr& device, |
| 151 | bool probe) |
| 152 | { |
| 153 | effect_uuid_t type; |
| 154 | effect_uuid_t *pType = nullptr; |
| 155 | effect_uuid_t uuid; |
| 156 | effect_uuid_t *pUuid = nullptr; |
| 157 | |
| 158 | ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s", |
| 159 | typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr"); |
| 160 | |
| 161 | if (stringToGuid(typeStr, &type) == NO_ERROR) { |
| 162 | pType = &type; |
| 163 | } |
| 164 | if (stringToGuid(uuidStr, &uuid) == NO_ERROR) { |
| 165 | pUuid = &uuid; |
| 166 | } |
| 167 | |
| 168 | return set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe); |
| 169 | } |
| 170 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 171 | |
| 172 | AudioEffect::~AudioEffect() |
| 173 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 174 | ALOGV("Destructor %p", this); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 175 | |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 176 | if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) { |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 177 | if (!audio_is_global_session(mSessionId)) { |
Jean-Michel Trivi | a0fd9ca | 2014-09-18 14:07:18 -0700 | [diff] [blame] | 178 | AudioSystem::releaseAudioSessionId(mSessionId, mClientPid); |
| 179 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 180 | if (mIEffect != NULL) { |
| 181 | mIEffect->disconnect(); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 182 | IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 183 | } |
Eric Laurent | eecd765 | 2015-06-04 16:20:16 -0700 | [diff] [blame] | 184 | mIEffect.clear(); |
| 185 | mCblkMemory.clear(); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 186 | } |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 187 | mIEffectClient.clear(); |
| 188 | IPCThreadState::self()->flushCommands(); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | status_t AudioEffect::initCheck() const |
| 193 | { |
| 194 | return mStatus; |
| 195 | } |
| 196 | |
| 197 | // ------------------------------------------------------------------------- |
| 198 | |
| 199 | effect_descriptor_t AudioEffect::descriptor() const |
| 200 | { |
| 201 | return mDescriptor; |
| 202 | } |
| 203 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 204 | bool AudioEffect::getEnabled() const |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 205 | { |
| 206 | return (mEnabled != 0); |
| 207 | } |
| 208 | |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 209 | status_t AudioEffect::setEnabled(bool enabled) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 210 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 211 | if (mProbe) { |
| 212 | return INVALID_OPERATION; |
| 213 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 214 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 215 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 216 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 217 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 218 | status_t status = NO_ERROR; |
| 219 | |
| 220 | AutoMutex lock(mLock); |
| 221 | if (enabled != mEnabled) { |
| 222 | if (enabled) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 223 | ALOGV("enable %p", this); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 224 | status = mIEffect->enable(); |
| 225 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 226 | ALOGV("disable %p", this); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 227 | status = mIEffect->disable(); |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 228 | } |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 229 | if (status == NO_ERROR) { |
| 230 | mEnabled = enabled; |
Eric Laurent | da7581b | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 231 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 232 | } |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 233 | return status; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 236 | status_t AudioEffect::command(uint32_t cmdCode, |
| 237 | uint32_t cmdSize, |
| 238 | void *cmdData, |
| 239 | uint32_t *replySize, |
| 240 | void *replyData) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 241 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 242 | if (mProbe) { |
| 243 | return INVALID_OPERATION; |
| 244 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 245 | if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 246 | ALOGV("command() bad status %d", mStatus); |
John Grossman | af7d818 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 247 | return mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 250 | if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) { |
| 251 | if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) { |
| 252 | return NO_ERROR; |
| 253 | } |
| 254 | if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) { |
| 255 | return BAD_VALUE; |
| 256 | } |
| 257 | mLock.lock(); |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 260 | status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData); |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 261 | |
| 262 | if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 263 | if (status == NO_ERROR) { |
| 264 | status = *(status_t *)replyData; |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 265 | } |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 266 | if (status == NO_ERROR) { |
| 267 | mEnabled = (cmdCode == EFFECT_CMD_ENABLE); |
Eric Laurent | 0fa449c | 2010-09-24 11:52:04 -0700 | [diff] [blame] | 268 | } |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 269 | mLock.unlock(); |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 272 | return status; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | status_t AudioEffect::setParameter(effect_param_t *param) |
| 277 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 278 | if (mProbe) { |
| 279 | return INVALID_OPERATION; |
| 280 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 281 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 282 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (param == NULL || param->psize == 0 || param->vsize == 0) { |
| 286 | return BAD_VALUE; |
| 287 | } |
| 288 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 289 | uint32_t size = sizeof(int); |
| 290 | 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] | 291 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 292 | ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data, |
| 293 | (param->psize == 8) ? *((int *)param->data + 1): -1); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 294 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 295 | return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size, |
| 296 | ¶m->status); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | status_t AudioEffect::setParameterDeferred(effect_param_t *param) |
| 300 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 301 | if (mProbe) { |
| 302 | return INVALID_OPERATION; |
| 303 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 304 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 305 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (param == NULL || param->psize == 0 || param->vsize == 0) { |
| 309 | return BAD_VALUE; |
| 310 | } |
| 311 | |
| 312 | Mutex::Autolock _l(mCblk->lock); |
| 313 | |
| 314 | int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize; |
| 315 | int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int); |
| 316 | |
| 317 | if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) { |
| 318 | return NO_MEMORY; |
| 319 | } |
| 320 | int *p = (int *)(mCblk->buffer + mCblk->clientIndex); |
| 321 | *p++ = size; |
| 322 | memcpy(p, param, sizeof(effect_param_t) + psize); |
| 323 | mCblk->clientIndex += size; |
| 324 | |
| 325 | return NO_ERROR; |
| 326 | } |
| 327 | |
| 328 | status_t AudioEffect::setParameterCommit() |
| 329 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 330 | if (mProbe) { |
| 331 | return INVALID_OPERATION; |
| 332 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 333 | if (mStatus != NO_ERROR) { |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 334 | return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | Mutex::Autolock _l(mCblk->lock); |
| 338 | if (mCblk->clientIndex == 0) { |
| 339 | return INVALID_OPERATION; |
| 340 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 341 | uint32_t size = 0; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 342 | return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL); |
| 343 | } |
| 344 | |
| 345 | status_t AudioEffect::getParameter(effect_param_t *param) |
| 346 | { |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 347 | if (mProbe) { |
| 348 | return INVALID_OPERATION; |
| 349 | } |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 350 | if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) { |
John Grossman | af7d818 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 351 | return mStatus; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | if (param == NULL || param->psize == 0 || param->vsize == 0) { |
| 355 | return BAD_VALUE; |
| 356 | } |
| 357 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 358 | ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data, |
| 359 | (param->psize == 8) ? *((int *)param->data + 1): -1); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 360 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 361 | uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 362 | param->vsize; |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 363 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 364 | return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param, |
| 365 | &psize, param); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
| 369 | // ------------------------------------------------------------------------- |
| 370 | |
| 371 | void AudioEffect::binderDied() |
| 372 | { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 373 | ALOGW("IEffect died"); |
John Grossman | af7d818 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 374 | mStatus = DEAD_OBJECT; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 375 | if (mCbf != NULL) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 376 | status_t status = DEAD_OBJECT; |
| 377 | mCbf(EVENT_ERROR, mUserData, &status); |
| 378 | } |
| 379 | mIEffect.clear(); |
| 380 | } |
| 381 | |
| 382 | // ------------------------------------------------------------------------- |
| 383 | |
| 384 | void AudioEffect::controlStatusChanged(bool controlGranted) |
| 385 | { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 386 | ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf, |
| 387 | mUserData); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 388 | if (controlGranted) { |
| 389 | if (mStatus == ALREADY_EXISTS) { |
| 390 | mStatus = NO_ERROR; |
| 391 | } |
| 392 | } else { |
| 393 | if (mStatus == NO_ERROR) { |
| 394 | mStatus = ALREADY_EXISTS; |
| 395 | } |
| 396 | } |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 397 | if (mCbf != NULL) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 398 | mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void AudioEffect::enableStatusChanged(bool enabled) |
| 403 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 404 | ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 405 | if (mStatus == ALREADY_EXISTS) { |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 406 | mEnabled = enabled; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 407 | if (mCbf != NULL) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 408 | mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 413 | void AudioEffect::commandExecuted(uint32_t cmdCode, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 414 | uint32_t cmdSize __unused, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 415 | void *cmdData, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 416 | uint32_t replySize __unused, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 417 | void *replyData) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 418 | { |
| 419 | if (cmdData == NULL || replyData == NULL) { |
| 420 | return; |
| 421 | } |
| 422 | |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 423 | if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) { |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 424 | effect_param_t *cmd = (effect_param_t *)cmdData; |
| 425 | cmd->status = *(int32_t *)replyData; |
| 426 | mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // ------------------------------------------------------------------------- |
| 431 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 432 | status_t AudioEffect::queryNumberEffects(uint32_t *numEffects) |
| 433 | { |
| 434 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 435 | if (af == 0) return PERMISSION_DENIED; |
| 436 | return af->queryNumberEffects(numEffects); |
| 437 | } |
| 438 | |
Eric Laurent | ffe9c25 | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 439 | status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 440 | { |
| 441 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 442 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | ffe9c25 | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 443 | return af->queryEffect(index, descriptor); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 446 | status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid, |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 447 | const effect_uuid_t *type, |
| 448 | uint32_t preferredTypeFlag, |
| 449 | effect_descriptor_t *descriptor) |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 450 | { |
| 451 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 452 | if (af == 0) return PERMISSION_DENIED; |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 453 | return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor); |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 456 | status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession, |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 457 | effect_descriptor_t *descriptors, |
| 458 | uint32_t *count) |
| 459 | { |
| 460 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 461 | if (aps == 0) return PERMISSION_DENIED; |
| 462 | return aps->queryDefaultPreProcessing(audioSession, descriptors, count); |
| 463 | } |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 464 | |
| 465 | status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id) |
| 466 | { |
| 467 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 468 | if (af == 0) return PERMISSION_DENIED; |
| 469 | *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
| 470 | return NO_ERROR; |
| 471 | } |
| 472 | |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 473 | status_t AudioEffect::addSourceDefaultEffect(const char *typeStr, |
| 474 | const String16& opPackageName, |
| 475 | const char *uuidStr, |
| 476 | int32_t priority, |
| 477 | audio_source_t source, |
| 478 | audio_unique_id_t *id) |
| 479 | { |
| 480 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 481 | if (aps == 0) return PERMISSION_DENIED; |
| 482 | |
| 483 | if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE; |
| 484 | |
| 485 | // Convert type & uuid from string to effect_uuid_t. |
| 486 | effect_uuid_t type; |
| 487 | if (typeStr != NULL) { |
| 488 | status_t res = stringToGuid(typeStr, &type); |
| 489 | if (res != OK) return res; |
| 490 | } else { |
| 491 | type = *EFFECT_UUID_NULL; |
| 492 | } |
| 493 | |
| 494 | effect_uuid_t uuid; |
| 495 | if (uuidStr != NULL) { |
| 496 | status_t res = stringToGuid(uuidStr, &uuid); |
| 497 | if (res != OK) return res; |
| 498 | } else { |
| 499 | uuid = *EFFECT_UUID_NULL; |
| 500 | } |
| 501 | |
| 502 | return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id); |
| 503 | } |
| 504 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 505 | status_t AudioEffect::addStreamDefaultEffect(const char *typeStr, |
| 506 | const String16& opPackageName, |
| 507 | const char *uuidStr, |
| 508 | int32_t priority, |
| 509 | audio_usage_t usage, |
| 510 | audio_unique_id_t *id) |
| 511 | { |
| 512 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 513 | if (aps == 0) return PERMISSION_DENIED; |
| 514 | |
| 515 | if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE; |
| 516 | |
| 517 | // Convert type & uuid from string to effect_uuid_t. |
| 518 | effect_uuid_t type; |
| 519 | if (typeStr != NULL) { |
| 520 | status_t res = stringToGuid(typeStr, &type); |
| 521 | if (res != OK) return res; |
| 522 | } else { |
| 523 | type = *EFFECT_UUID_NULL; |
| 524 | } |
| 525 | |
| 526 | effect_uuid_t uuid; |
| 527 | if (uuidStr != NULL) { |
| 528 | status_t res = stringToGuid(uuidStr, &uuid); |
| 529 | if (res != OK) return res; |
| 530 | } else { |
| 531 | uuid = *EFFECT_UUID_NULL; |
| 532 | } |
| 533 | |
| 534 | return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id); |
| 535 | } |
| 536 | |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 537 | status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id) |
| 538 | { |
| 539 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 540 | if (aps == 0) return PERMISSION_DENIED; |
| 541 | |
| 542 | return aps->removeSourceDefaultEffect(id); |
| 543 | } |
| 544 | |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 545 | status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id) |
| 546 | { |
| 547 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 548 | if (aps == 0) return PERMISSION_DENIED; |
| 549 | |
| 550 | return aps->removeStreamDefaultEffect(id); |
| 551 | } |
| 552 | |
Eric Laurent | 801a118 | 2010-06-09 00:17:29 -0700 | [diff] [blame] | 553 | // ------------------------------------------------------------------------- |
| 554 | |
| 555 | status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid) |
| 556 | { |
| 557 | if (str == NULL || guid == NULL) { |
| 558 | return BAD_VALUE; |
| 559 | } |
| 560 | |
| 561 | int tmp[10]; |
| 562 | |
| 563 | if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x", |
| 564 | tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) { |
| 565 | return BAD_VALUE; |
| 566 | } |
| 567 | guid->timeLow = (uint32_t)tmp[0]; |
| 568 | guid->timeMid = (uint16_t)tmp[1]; |
| 569 | guid->timeHiAndVersion = (uint16_t)tmp[2]; |
| 570 | guid->clockSeq = (uint16_t)tmp[3]; |
| 571 | guid->node[0] = (uint8_t)tmp[4]; |
| 572 | guid->node[1] = (uint8_t)tmp[5]; |
| 573 | guid->node[2] = (uint8_t)tmp[6]; |
| 574 | guid->node[3] = (uint8_t)tmp[7]; |
| 575 | guid->node[4] = (uint8_t)tmp[8]; |
| 576 | guid->node[5] = (uint8_t)tmp[9]; |
| 577 | |
| 578 | return NO_ERROR; |
| 579 | } |
| 580 | |
| 581 | status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen) |
| 582 | { |
| 583 | if (guid == NULL || str == NULL) { |
| 584 | return BAD_VALUE; |
| 585 | } |
| 586 | |
| 587 | snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x", |
| 588 | guid->timeLow, |
| 589 | guid->timeMid, |
| 590 | guid->timeHiAndVersion, |
| 591 | guid->clockSeq, |
| 592 | guid->node[0], |
| 593 | guid->node[1], |
| 594 | guid->node[2], |
| 595 | guid->node[3], |
| 596 | guid->node[4], |
| 597 | guid->node[5]); |
| 598 | |
| 599 | return NO_ERROR; |
| 600 | } |
| 601 | |
| 602 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 603 | } // namespace android |