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