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