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