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