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