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