blob: 6ad548364c96bc31d59fee1477c5a385217b0e08 [file] [log] [blame]
Eric Laurent801a1182010-06-09 00:17:29 -07001/*
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-Tsvi0a4904a2021-01-06 12:57:05 -080026#include <android/media/IAudioPolicyService.h>
Eric Laurent801a1182010-06-09 00:17:29 -070027#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080028#include <media/AidlConversion.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070029#include <media/AudioEffect.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080030#include <media/PolicyAidlConversion.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070031#include <media/ShmemCompat.h>
32#include <private/media/AudioEffectShared.h>
33#include <utils/Log.h>
Eric Laurent801a1182010-06-09 00:17:29 -070034
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080035#define RETURN_STATUS_IF_ERROR(x) \
36 { \
37 auto _tmp = (x); \
38 if (_tmp != OK) return _tmp; \
39 }
40
Eric Laurent801a1182010-06-09 00:17:29 -070041namespace android {
Andy Hung1131b6e2020-12-08 20:47:45 -080042using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070043using binder::Status;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080044using media::IAudioPolicyService;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070045
46namespace {
47
48// Copy from a raw pointer + size into a vector of bytes.
49void 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 Laurent801a1182010-06-09 00:17:29 -070058// ---------------------------------------------------------------------------
59
Svet Ganov33761132021-05-13 22:51:08 +000060AudioEffect::AudioEffect(const android::content::AttributionSourceState& attributionSource)
61 : mClientAttributionSource(attributionSource)
Eric Laurent801a1182010-06-09 00:17:29 -070062{
63}
64
Eric Laurent801a1182010-06-09 00:17:29 -070065status_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 Kastend848eb42016-03-08 13:42:11 -080070 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080071 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070072 const AudioDeviceTypeAddr& device,
73 bool probe)
Eric Laurent801a1182010-06-09 00:17:29 -070074{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070075 sp<media::IEffect> iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -070076 sp<IMemory> cblk;
77 int enabled;
78
Steve Block3856b092011-10-20 11:56:00 +010079 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -070080
81 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +000082 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -070083 return INVALID_OPERATION;
84 }
85
Eric Laurent94876032019-11-13 12:45:28 -080086 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 Laurent801a1182010-06-09 00:17:29 -070090 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
91 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000092 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -070093 return NO_INIT;
94 }
95
96 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +000097 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -070098 return BAD_VALUE;
99 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700100 mProbe = probe;
Eric Laurent801a1182010-06-09 00:17:29 -0700101 mPriority = priority;
102 mCbf = cbf;
103 mUserData = user;
104 mSessionId = sessionId;
105
106 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -0800107 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
108 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700109
Svet Ganov33761132021-05-13 22:51:08 +0000110 // TODO b/182392769: use attribution source util
Eric Laurent801a1182010-06-09 00:17:29 -0700111 mIEffectClient = new EffectClient(this);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700112 pid_t pid = IPCThreadState::self()->getCallingPid();
Svet Ganov33761132021-05-13 22:51:08 +0000113 mClientAttributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(pid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700114 pid_t uid = IPCThreadState::self()->getCallingUid();
Svet Ganov33761132021-05-13 22:51:08 +0000115 mClientAttributionSource.uid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Eric Laurent801a1182010-06-09 00:17:29 -0700116
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800117 media::CreateEffectRequest request;
118 request.desc = VALUE_OR_RETURN_STATUS(
119 legacy2aidl_effect_descriptor_t_EffectDescriptor(mDescriptor));
120 request.client = mIEffectClient;
121 request.priority = priority;
122 request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
123 request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(mSessionId));
124 request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device));
Svet Ganov33761132021-05-13 22:51:08 +0000125 request.attributionSource = mClientAttributionSource;
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800126 request.probe = probe;
127
128 media::CreateEffectResponse response;
129
130 mStatus = audioFlinger->createEffect(request, &response);
131
132 if (mStatus == OK) {
133 mId = response.id;
134 enabled = response.enabled;
135 iEffect = response.effect;
136 mDescriptor = VALUE_OR_RETURN_STATUS(
137 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
138 }
Eric Laurent801a1182010-06-09 00:17:29 -0700139
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700140 // In probe mode, we stop here and return the status: the IEffect interface to
141 // audio flinger will not be retained. initCheck() will return the creation status
142 // but all other APIs will return invalid operation.
143 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000144 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700145 guidToString(type, typeBuffer, sizeof(typeBuffer));
146 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700147 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000148 type != nullptr ? typeBuffer : "NULL",
149 uuid != nullptr ? uuidBuffer : "NULL",
150 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700151 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700152 mStatus = NO_INIT;
153 }
Eric Laurent801a1182010-06-09 00:17:29 -0700154 return mStatus;
155 }
156
157 mEnabled = (volatile int32_t)enabled;
158
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700159 if (media::SharedFileRegion shmem;
160 !iEffect->getCblk(&shmem).isOk()
161 || !convertSharedFileRegionToIMemory(shmem, &cblk)
162 || cblk == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700163 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000164 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700165 return mStatus;
166 }
167
Eric Laurenteecd7652015-06-04 16:20:16 -0700168 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700169 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700170 // TODO: Using unsecurePointer() has some associated security pitfalls
171 // (see declaration for details).
172 // Either document why it is safe in this case or address the
173 // issue (e.g. by copying).
174 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700175 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
176 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
177
Marco Nelissen06b46062014-11-14 07:58:25 -0800178 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700179 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
Svet Ganov33761132021-05-13 22:51:08 +0000180 mStatus, mEnabled, mClientAttributionSource.pid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700181
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800182 if (!audio_is_global_session(mSessionId)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700183 AudioSystem::acquireAudioSessionId(mSessionId, pid, uid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700184 }
Eric Laurent801a1182010-06-09 00:17:29 -0700185
186 return mStatus;
187}
188
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700189status_t AudioEffect::set(const char *typeStr,
190 const char *uuidStr,
191 int32_t priority,
192 effect_callback_t cbf,
193 void* user,
194 audio_session_t sessionId,
195 audio_io_handle_t io,
196 const AudioDeviceTypeAddr& device,
197 bool probe)
198{
199 effect_uuid_t type;
200 effect_uuid_t *pType = nullptr;
201 effect_uuid_t uuid;
202 effect_uuid_t *pUuid = nullptr;
203
204 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
205 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
206
207 if (stringToGuid(typeStr, &type) == NO_ERROR) {
208 pType = &type;
209 }
210 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
211 pUuid = &uuid;
212 }
213
214 return set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe);
215}
216
Eric Laurent801a1182010-06-09 00:17:29 -0700217
218AudioEffect::~AudioEffect()
219{
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700221
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700222 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800223 if (!audio_is_global_session(mSessionId)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700224 AudioSystem::releaseAudioSessionId(mSessionId,
Svet Ganov33761132021-05-13 22:51:08 +0000225 VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mClientAttributionSource.pid)));
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700226 }
Eric Laurent801a1182010-06-09 00:17:29 -0700227 if (mIEffect != NULL) {
228 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800229 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700230 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700231 mIEffect.clear();
232 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700233 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700234 mIEffectClient.clear();
235 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700236}
237
238
239status_t AudioEffect::initCheck() const
240{
241 return mStatus;
242}
243
244// -------------------------------------------------------------------------
245
246effect_descriptor_t AudioEffect::descriptor() const
247{
248 return mDescriptor;
249}
250
Eric Laurentda7581b2010-07-02 08:12:41 -0700251bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700252{
253 return (mEnabled != 0);
254}
255
Eric Laurentda7581b2010-07-02 08:12:41 -0700256status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700257{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700258 if (mProbe) {
259 return INVALID_OPERATION;
260 }
Eric Laurent801a1182010-06-09 00:17:29 -0700261 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800262 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700263 }
Eric Laurent801a1182010-06-09 00:17:29 -0700264
Eric Laurentf5aafb22010-11-18 08:40:16 -0800265 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800266 AutoMutex lock(mLock);
267 if (enabled != mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700268 Status bs;
269
Eric Laurentf5aafb22010-11-18 08:40:16 -0800270 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("enable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700272 bs = mIEffect->enable(&status);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800273 } else {
Steve Block3856b092011-10-20 11:56:00 +0100274 ALOGV("disable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700275 bs = mIEffect->disable(&status);
276 }
277 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800278 status = statusTFromBinderStatus(bs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700279 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800280 if (status == NO_ERROR) {
281 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700282 }
Eric Laurent801a1182010-06-09 00:17:29 -0700283 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800284 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700285}
286
Eric Laurent25f43952010-07-28 05:40:18 -0700287status_t AudioEffect::command(uint32_t cmdCode,
288 uint32_t cmdSize,
289 void *cmdData,
290 uint32_t *replySize,
291 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700292{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700293 if (mProbe) {
294 return INVALID_OPERATION;
295 }
Eric Laurent801a1182010-06-09 00:17:29 -0700296 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800298 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700299 }
300
Eric Laurentf5aafb22010-11-18 08:40:16 -0800301 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
302 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
303 return NO_ERROR;
304 }
305 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
306 return BAD_VALUE;
307 }
308 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700309 }
310
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700311 std::vector<uint8_t> data;
312 appendToBuffer(cmdData, cmdSize, &data);
313
314 status_t status;
315 std::vector<uint8_t> response;
316
317 Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status);
318 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800319 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700320 }
321 if (status == NO_ERROR) {
322 memcpy(replyData, response.data(), response.size());
323 *replySize = response.size();
324 }
Eric Laurent0fa449c2010-09-24 11:52:04 -0700325
326 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800327 if (status == NO_ERROR) {
328 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700329 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800330 if (status == NO_ERROR) {
331 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700332 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800333 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700334 }
335
Eric Laurent8569f0d2010-07-29 23:43:43 -0700336 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700337}
338
Eric Laurent801a1182010-06-09 00:17:29 -0700339status_t AudioEffect::setParameter(effect_param_t *param)
340{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700341 if (mProbe) {
342 return INVALID_OPERATION;
343 }
Eric Laurent801a1182010-06-09 00:17:29 -0700344 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800345 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700346 }
347
348 if (param == NULL || param->psize == 0 || param->vsize == 0) {
349 return BAD_VALUE;
350 }
351
Eric Laurent25f43952010-07-28 05:40:18 -0700352 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700353
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700354 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
355 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700356
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700357 std::vector<uint8_t> cmd;
358 appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd);
359 std::vector<uint8_t> response;
360 status_t status;
361 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM,
362 cmd,
363 sizeof(int),
364 &response,
365 &status);
366 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800367 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700368 return status;
369 }
370 assert(response.size() == sizeof(int));
371 memcpy(&param->status, response.data(), response.size());
372 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700373}
374
375status_t AudioEffect::setParameterDeferred(effect_param_t *param)
376{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700377 if (mProbe) {
378 return INVALID_OPERATION;
379 }
Eric Laurent801a1182010-06-09 00:17:29 -0700380 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800381 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700382 }
383
384 if (param == NULL || param->psize == 0 || param->vsize == 0) {
385 return BAD_VALUE;
386 }
387
388 Mutex::Autolock _l(mCblk->lock);
389
390 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
391 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
392
393 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
394 return NO_MEMORY;
395 }
396 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
397 *p++ = size;
398 memcpy(p, param, sizeof(effect_param_t) + psize);
399 mCblk->clientIndex += size;
400
401 return NO_ERROR;
402}
403
404status_t AudioEffect::setParameterCommit()
405{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700406 if (mProbe) {
407 return INVALID_OPERATION;
408 }
Eric Laurent801a1182010-06-09 00:17:29 -0700409 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800410 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700411 }
412
413 Mutex::Autolock _l(mCblk->lock);
414 if (mCblk->clientIndex == 0) {
415 return INVALID_OPERATION;
416 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700417 std::vector<uint8_t> cmd;
418 std::vector<uint8_t> response;
419 status_t status;
420 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT,
421 cmd,
422 0,
423 &response,
424 &status);
425 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800426 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700427 }
428 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700429}
430
431status_t AudioEffect::getParameter(effect_param_t *param)
432{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700433 if (mProbe) {
434 return INVALID_OPERATION;
435 }
Eric Laurent801a1182010-06-09 00:17:29 -0700436 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800437 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700438 }
439
440 if (param == NULL || param->psize == 0 || param->vsize == 0) {
441 return BAD_VALUE;
442 }
443
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700444 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
445 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700446
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700447 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
448 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700449
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700450 status_t status;
451 std::vector<uint8_t> cmd;
452 std::vector<uint8_t> response;
453 appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd);
454
455 Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status);
456 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800457 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700458 return status;
459 }
460 memcpy(param, response.data(), response.size());
461 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700462}
463
464
465// -------------------------------------------------------------------------
466
467void AudioEffect::binderDied()
468{
Steve Block5ff1dd52012-01-05 23:22:43 +0000469 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800470 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800471 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700472 status_t status = DEAD_OBJECT;
473 mCbf(EVENT_ERROR, mUserData, &status);
474 }
475 mIEffect.clear();
476}
477
478// -------------------------------------------------------------------------
479
480void AudioEffect::controlStatusChanged(bool controlGranted)
481{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700482 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
483 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700484 if (controlGranted) {
485 if (mStatus == ALREADY_EXISTS) {
486 mStatus = NO_ERROR;
487 }
488 } else {
489 if (mStatus == NO_ERROR) {
490 mStatus = ALREADY_EXISTS;
491 }
492 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800493 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700494 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
495 }
496}
497
498void AudioEffect::enableStatusChanged(bool enabled)
499{
Steve Block3856b092011-10-20 11:56:00 +0100500 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700501 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800502 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800503 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700504 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
505 }
506 }
507}
508
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700509void AudioEffect::commandExecuted(int32_t cmdCode,
510 const std::vector<uint8_t>& cmdData,
511 const std::vector<uint8_t>& replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700512{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700513 if (cmdData.empty() || replyData.empty()) {
Eric Laurent801a1182010-06-09 00:17:29 -0700514 return;
515 }
516
Glenn Kastena0d68332012-01-27 16:47:15 -0800517 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700518 std::vector<uint8_t> cmdDataCopy(cmdData);
519 effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data());
520 cmd->status = *reinterpret_cast<const int32_t *>(replyData.data());
Eric Laurent801a1182010-06-09 00:17:29 -0700521 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
522 }
523}
524
525// -------------------------------------------------------------------------
526
Eric Laurent801a1182010-06-09 00:17:29 -0700527status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
528{
529 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
530 if (af == 0) return PERMISSION_DENIED;
531 return af->queryNumberEffects(numEffects);
532}
533
Eric Laurentffe9c252010-06-23 17:38:20 -0700534status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700535{
536 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
537 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700538 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700539}
540
Glenn Kasten5e92a782012-01-30 07:40:52 -0800541status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700542 const effect_uuid_t *type,
543 uint32_t preferredTypeFlag,
544 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700545{
546 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
547 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700548 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700549}
550
Glenn Kastend848eb42016-03-08 13:42:11 -0800551status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700552 effect_descriptor_t *descriptors,
553 uint32_t *count)
554{
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800555 if (descriptors == nullptr || count == nullptr) {
556 return BAD_VALUE;
557 }
Eric Laurent57dae992011-07-24 13:36:09 -0700558 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
559 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800560
561 int32_t audioSessionAidl = VALUE_OR_RETURN_STATUS(
562 legacy2aidl_audio_session_t_int32_t(audioSession));
563 media::Int countAidl;
564 countAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*count));
565 std::vector<media::EffectDescriptor> retAidl;
566 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
567 aps->queryDefaultPreProcessing(audioSessionAidl, &countAidl, &retAidl)));
568 *count = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(countAidl.value));
569 RETURN_STATUS_IF_ERROR(convertRange(retAidl.begin(), retAidl.end(), descriptors,
570 aidl2legacy_EffectDescriptor_effect_descriptor_t));
571 return OK;
Eric Laurent57dae992011-07-24 13:36:09 -0700572}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700573
574status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
575{
576 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
577 if (af == 0) return PERMISSION_DENIED;
578 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
579 return NO_ERROR;
580}
581
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700582status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
583 const String16& opPackageName,
584 const char *uuidStr,
585 int32_t priority,
586 audio_source_t source,
587 audio_unique_id_t *id)
588{
589 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
590 if (aps == 0) return PERMISSION_DENIED;
591
592 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
593
594 // Convert type & uuid from string to effect_uuid_t.
595 effect_uuid_t type;
596 if (typeStr != NULL) {
597 status_t res = stringToGuid(typeStr, &type);
598 if (res != OK) return res;
599 } else {
600 type = *EFFECT_UUID_NULL;
601 }
602
603 effect_uuid_t uuid;
604 if (uuidStr != NULL) {
605 status_t res = stringToGuid(uuidStr, &uuid);
606 if (res != OK) return res;
607 } else {
608 uuid = *EFFECT_UUID_NULL;
609 }
610
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800611 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
612 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
613 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
614 legacy2aidl_String16_string(opPackageName));
615 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
616 legacy2aidl_audio_source_t_AudioSourceType(source));
617 int32_t retAidl;
618 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
619 aps->addSourceDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, sourceAidl,
620 &retAidl)));
621 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
622 return OK;
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700623}
624
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700625status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
626 const String16& opPackageName,
627 const char *uuidStr,
628 int32_t priority,
629 audio_usage_t usage,
630 audio_unique_id_t *id)
631{
632 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
633 if (aps == 0) return PERMISSION_DENIED;
634
635 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
636
637 // Convert type & uuid from string to effect_uuid_t.
638 effect_uuid_t type;
639 if (typeStr != NULL) {
640 status_t res = stringToGuid(typeStr, &type);
641 if (res != OK) return res;
642 } else {
643 type = *EFFECT_UUID_NULL;
644 }
645
646 effect_uuid_t uuid;
647 if (uuidStr != NULL) {
648 status_t res = stringToGuid(uuidStr, &uuid);
649 if (res != OK) return res;
650 } else {
651 uuid = *EFFECT_UUID_NULL;
652 }
653
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800654 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
655 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
656 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
657 legacy2aidl_String16_string(opPackageName));
658 media::AudioUsage usageAidl = VALUE_OR_RETURN_STATUS(
659 legacy2aidl_audio_usage_t_AudioUsage(usage));
660 int32_t retAidl;
661 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
662 aps->addStreamDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, usageAidl,
663 &retAidl)));
664 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
665 return OK;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700666}
667
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700668status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
669{
670 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
671 if (aps == 0) return PERMISSION_DENIED;
672
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800673 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
674 return statusTFromBinderStatus(aps->removeSourceDefaultEffect(idAidl));
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700675}
676
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700677status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
678{
679 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
680 if (aps == 0) return PERMISSION_DENIED;
681
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800682 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
683 return statusTFromBinderStatus(aps->removeStreamDefaultEffect(idAidl));
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700684}
685
Eric Laurent801a1182010-06-09 00:17:29 -0700686// -------------------------------------------------------------------------
687
688status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
689{
690 if (str == NULL || guid == NULL) {
691 return BAD_VALUE;
692 }
693
694 int tmp[10];
695
696 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
697 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
698 return BAD_VALUE;
699 }
700 guid->timeLow = (uint32_t)tmp[0];
701 guid->timeMid = (uint16_t)tmp[1];
702 guid->timeHiAndVersion = (uint16_t)tmp[2];
703 guid->clockSeq = (uint16_t)tmp[3];
704 guid->node[0] = (uint8_t)tmp[4];
705 guid->node[1] = (uint8_t)tmp[5];
706 guid->node[2] = (uint8_t)tmp[6];
707 guid->node[3] = (uint8_t)tmp[7];
708 guid->node[4] = (uint8_t)tmp[8];
709 guid->node[5] = (uint8_t)tmp[9];
710
711 return NO_ERROR;
712}
713
714status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
715{
716 if (guid == NULL || str == NULL) {
717 return BAD_VALUE;
718 }
719
720 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
721 guid->timeLow,
722 guid->timeMid,
723 guid->timeHiAndVersion,
724 guid->clockSeq,
725 guid->node[0],
726 guid->node[1],
727 guid->node[2],
728 guid->node[3],
729 guid->node[4],
730 guid->node[5]);
731
732 return NO_ERROR;
733}
734
735
Glenn Kasten40bc9062015-03-20 09:09:33 -0700736} // namespace android