blob: d5047b1b6df28cb9afb5d3b9480c9a2e4a827ecb [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;
Philip P. Moltmannbda45752020-07-17 16:41:18 -070045using media::permission::Identity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070046
47namespace {
48
49// Copy from a raw pointer + size into a vector of bytes.
50void appendToBuffer(const void* data,
51 size_t size,
52 std::vector<uint8_t>* buffer) {
53 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
54 buffer->insert(buffer->end(), p, p + size);
55}
56
57} // namespace
58
Eric Laurent801a1182010-06-09 00:17:29 -070059// ---------------------------------------------------------------------------
60
Philip P. Moltmannbda45752020-07-17 16:41:18 -070061AudioEffect::AudioEffect(const Identity& identity)
62 : mClientIdentity(identity)
Eric Laurent801a1182010-06-09 00:17:29 -070063{
64}
65
Eric Laurent801a1182010-06-09 00:17:29 -070066status_t AudioEffect::set(const effect_uuid_t *type,
67 const effect_uuid_t *uuid,
68 int32_t priority,
69 effect_callback_t cbf,
70 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080071 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080072 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070073 const AudioDeviceTypeAddr& device,
74 bool probe)
Eric Laurent801a1182010-06-09 00:17:29 -070075{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070076 sp<media::IEffect> iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -070077 sp<IMemory> cblk;
78 int enabled;
79
Steve Block3856b092011-10-20 11:56:00 +010080 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -070081
82 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +000083 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -070084 return INVALID_OPERATION;
85 }
86
Eric Laurent94876032019-11-13 12:45:28 -080087 if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) {
88 ALOGW("IO handle should not be specified for device effect");
89 return BAD_VALUE;
90 }
Eric Laurent801a1182010-06-09 00:17:29 -070091 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
92 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000093 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -070094 return NO_INIT;
95 }
96
97 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +000098 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -070099 return BAD_VALUE;
100 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700101 mProbe = probe;
Eric Laurent801a1182010-06-09 00:17:29 -0700102 mPriority = priority;
103 mCbf = cbf;
104 mUserData = user;
105 mSessionId = sessionId;
106
107 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -0800108 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
109 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700110
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700111 // TODO b/182392769: use identity util
Eric Laurent801a1182010-06-09 00:17:29 -0700112 mIEffectClient = new EffectClient(this);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700113 pid_t pid = IPCThreadState::self()->getCallingPid();
114 mClientIdentity.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(pid));
115 pid_t uid = IPCThreadState::self()->getCallingUid();
116 mClientIdentity.uid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid));
Eric Laurent801a1182010-06-09 00:17:29 -0700117
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800118 media::CreateEffectRequest request;
119 request.desc = VALUE_OR_RETURN_STATUS(
120 legacy2aidl_effect_descriptor_t_EffectDescriptor(mDescriptor));
121 request.client = mIEffectClient;
122 request.priority = priority;
123 request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
124 request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(mSessionId));
125 request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700126 request.identity = mClientIdentity;
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800127 request.probe = probe;
128
129 media::CreateEffectResponse response;
130
131 mStatus = audioFlinger->createEffect(request, &response);
132
133 if (mStatus == OK) {
134 mId = response.id;
135 enabled = response.enabled;
136 iEffect = response.effect;
137 mDescriptor = VALUE_OR_RETURN_STATUS(
138 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
139 }
Eric Laurent801a1182010-06-09 00:17:29 -0700140
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700141 // In probe mode, we stop here and return the status: the IEffect interface to
142 // audio flinger will not be retained. initCheck() will return the creation status
143 // but all other APIs will return invalid operation.
144 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000145 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700146 guidToString(type, typeBuffer, sizeof(typeBuffer));
147 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700148 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000149 type != nullptr ? typeBuffer : "NULL",
150 uuid != nullptr ? uuidBuffer : "NULL",
151 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700152 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700153 mStatus = NO_INIT;
154 }
Eric Laurent801a1182010-06-09 00:17:29 -0700155 return mStatus;
156 }
157
158 mEnabled = (volatile int32_t)enabled;
159
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700160 if (media::SharedFileRegion shmem;
161 !iEffect->getCblk(&shmem).isOk()
162 || !convertSharedFileRegionToIMemory(shmem, &cblk)
163 || cblk == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700164 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000165 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700166 return mStatus;
167 }
168
Eric Laurenteecd7652015-06-04 16:20:16 -0700169 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700170 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700171 // TODO: Using unsecurePointer() has some associated security pitfalls
172 // (see declaration for details).
173 // Either document why it is safe in this case or address the
174 // issue (e.g. by copying).
175 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700176 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
177 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
178
Marco Nelissen06b46062014-11-14 07:58:25 -0800179 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700180 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700181 mStatus, mEnabled, mClientIdentity.pid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700182
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800183 if (!audio_is_global_session(mSessionId)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700184 AudioSystem::acquireAudioSessionId(mSessionId, pid, uid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700185 }
Eric Laurent801a1182010-06-09 00:17:29 -0700186
187 return mStatus;
188}
189
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700190status_t AudioEffect::set(const char *typeStr,
191 const char *uuidStr,
192 int32_t priority,
193 effect_callback_t cbf,
194 void* user,
195 audio_session_t sessionId,
196 audio_io_handle_t io,
197 const AudioDeviceTypeAddr& device,
198 bool probe)
199{
200 effect_uuid_t type;
201 effect_uuid_t *pType = nullptr;
202 effect_uuid_t uuid;
203 effect_uuid_t *pUuid = nullptr;
204
205 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
206 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
207
208 if (stringToGuid(typeStr, &type) == NO_ERROR) {
209 pType = &type;
210 }
211 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
212 pUuid = &uuid;
213 }
214
215 return set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe);
216}
217
Eric Laurent801a1182010-06-09 00:17:29 -0700218
219AudioEffect::~AudioEffect()
220{
Steve Block3856b092011-10-20 11:56:00 +0100221 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700222
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700223 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800224 if (!audio_is_global_session(mSessionId)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700225 AudioSystem::releaseAudioSessionId(mSessionId,
226 VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mClientIdentity.pid)));
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700227 }
Eric Laurent801a1182010-06-09 00:17:29 -0700228 if (mIEffect != NULL) {
229 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800230 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700231 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700232 mIEffect.clear();
233 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700234 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700235 mIEffectClient.clear();
236 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700237}
238
239
240status_t AudioEffect::initCheck() const
241{
242 return mStatus;
243}
244
245// -------------------------------------------------------------------------
246
247effect_descriptor_t AudioEffect::descriptor() const
248{
249 return mDescriptor;
250}
251
Eric Laurentda7581b2010-07-02 08:12:41 -0700252bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700253{
254 return (mEnabled != 0);
255}
256
Eric Laurentda7581b2010-07-02 08:12:41 -0700257status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700258{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700259 if (mProbe) {
260 return INVALID_OPERATION;
261 }
Eric Laurent801a1182010-06-09 00:17:29 -0700262 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800263 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700264 }
Eric Laurent801a1182010-06-09 00:17:29 -0700265
Eric Laurentf5aafb22010-11-18 08:40:16 -0800266 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800267 AutoMutex lock(mLock);
268 if (enabled != mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700269 Status bs;
270
Eric Laurentf5aafb22010-11-18 08:40:16 -0800271 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100272 ALOGV("enable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700273 bs = mIEffect->enable(&status);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800274 } else {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("disable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700276 bs = mIEffect->disable(&status);
277 }
278 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800279 status = statusTFromBinderStatus(bs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700280 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800281 if (status == NO_ERROR) {
282 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700283 }
Eric Laurent801a1182010-06-09 00:17:29 -0700284 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800285 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700286}
287
Eric Laurent25f43952010-07-28 05:40:18 -0700288status_t AudioEffect::command(uint32_t cmdCode,
289 uint32_t cmdSize,
290 void *cmdData,
291 uint32_t *replySize,
292 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700293{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700294 if (mProbe) {
295 return INVALID_OPERATION;
296 }
Eric Laurent801a1182010-06-09 00:17:29 -0700297 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100298 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800299 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700300 }
301
Eric Laurentf5aafb22010-11-18 08:40:16 -0800302 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
303 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
304 return NO_ERROR;
305 }
306 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
307 return BAD_VALUE;
308 }
309 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700310 }
311
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700312 std::vector<uint8_t> data;
313 appendToBuffer(cmdData, cmdSize, &data);
314
315 status_t status;
316 std::vector<uint8_t> response;
317
318 Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status);
319 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800320 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700321 }
322 if (status == NO_ERROR) {
323 memcpy(replyData, response.data(), response.size());
324 *replySize = response.size();
325 }
Eric Laurent0fa449c2010-09-24 11:52:04 -0700326
327 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800328 if (status == NO_ERROR) {
329 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700330 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800331 if (status == NO_ERROR) {
332 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700333 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800334 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700335 }
336
Eric Laurent8569f0d2010-07-29 23:43:43 -0700337 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700338}
339
Eric Laurent801a1182010-06-09 00:17:29 -0700340status_t AudioEffect::setParameter(effect_param_t *param)
341{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700342 if (mProbe) {
343 return INVALID_OPERATION;
344 }
Eric Laurent801a1182010-06-09 00:17:29 -0700345 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800346 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700347 }
348
349 if (param == NULL || param->psize == 0 || param->vsize == 0) {
350 return BAD_VALUE;
351 }
352
Eric Laurent25f43952010-07-28 05:40:18 -0700353 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700354
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700355 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
356 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700357
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700358 std::vector<uint8_t> cmd;
359 appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd);
360 std::vector<uint8_t> response;
361 status_t status;
362 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM,
363 cmd,
364 sizeof(int),
365 &response,
366 &status);
367 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800368 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700369 return status;
370 }
371 assert(response.size() == sizeof(int));
372 memcpy(&param->status, response.data(), response.size());
373 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700374}
375
376status_t AudioEffect::setParameterDeferred(effect_param_t *param)
377{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700378 if (mProbe) {
379 return INVALID_OPERATION;
380 }
Eric Laurent801a1182010-06-09 00:17:29 -0700381 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800382 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700383 }
384
385 if (param == NULL || param->psize == 0 || param->vsize == 0) {
386 return BAD_VALUE;
387 }
388
389 Mutex::Autolock _l(mCblk->lock);
390
391 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
392 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
393
394 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
395 return NO_MEMORY;
396 }
397 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
398 *p++ = size;
399 memcpy(p, param, sizeof(effect_param_t) + psize);
400 mCblk->clientIndex += size;
401
402 return NO_ERROR;
403}
404
405status_t AudioEffect::setParameterCommit()
406{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700407 if (mProbe) {
408 return INVALID_OPERATION;
409 }
Eric Laurent801a1182010-06-09 00:17:29 -0700410 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800411 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700412 }
413
414 Mutex::Autolock _l(mCblk->lock);
415 if (mCblk->clientIndex == 0) {
416 return INVALID_OPERATION;
417 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700418 std::vector<uint8_t> cmd;
419 std::vector<uint8_t> response;
420 status_t status;
421 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT,
422 cmd,
423 0,
424 &response,
425 &status);
426 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800427 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700428 }
429 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700430}
431
432status_t AudioEffect::getParameter(effect_param_t *param)
433{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700434 if (mProbe) {
435 return INVALID_OPERATION;
436 }
Eric Laurent801a1182010-06-09 00:17:29 -0700437 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800438 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700439 }
440
441 if (param == NULL || param->psize == 0 || param->vsize == 0) {
442 return BAD_VALUE;
443 }
444
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700445 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
446 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700447
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700448 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
449 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700450
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700451 status_t status;
452 std::vector<uint8_t> cmd;
453 std::vector<uint8_t> response;
454 appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd);
455
456 Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status);
457 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800458 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700459 return status;
460 }
461 memcpy(param, response.data(), response.size());
462 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700463}
464
465
466// -------------------------------------------------------------------------
467
468void AudioEffect::binderDied()
469{
Steve Block5ff1dd52012-01-05 23:22:43 +0000470 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800471 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800472 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700473 status_t status = DEAD_OBJECT;
474 mCbf(EVENT_ERROR, mUserData, &status);
475 }
476 mIEffect.clear();
477}
478
479// -------------------------------------------------------------------------
480
481void AudioEffect::controlStatusChanged(bool controlGranted)
482{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700483 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
484 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700485 if (controlGranted) {
486 if (mStatus == ALREADY_EXISTS) {
487 mStatus = NO_ERROR;
488 }
489 } else {
490 if (mStatus == NO_ERROR) {
491 mStatus = ALREADY_EXISTS;
492 }
493 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800494 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700495 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
496 }
497}
498
499void AudioEffect::enableStatusChanged(bool enabled)
500{
Steve Block3856b092011-10-20 11:56:00 +0100501 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700502 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800503 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800504 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700505 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
506 }
507 }
508}
509
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700510void AudioEffect::commandExecuted(int32_t cmdCode,
511 const std::vector<uint8_t>& cmdData,
512 const std::vector<uint8_t>& replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700513{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700514 if (cmdData.empty() || replyData.empty()) {
Eric Laurent801a1182010-06-09 00:17:29 -0700515 return;
516 }
517
Glenn Kastena0d68332012-01-27 16:47:15 -0800518 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700519 std::vector<uint8_t> cmdDataCopy(cmdData);
520 effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data());
521 cmd->status = *reinterpret_cast<const int32_t *>(replyData.data());
Eric Laurent801a1182010-06-09 00:17:29 -0700522 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
523 }
524}
525
526// -------------------------------------------------------------------------
527
Eric Laurent801a1182010-06-09 00:17:29 -0700528status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
529{
530 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
531 if (af == 0) return PERMISSION_DENIED;
532 return af->queryNumberEffects(numEffects);
533}
534
Eric Laurentffe9c252010-06-23 17:38:20 -0700535status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700536{
537 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
538 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700539 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700540}
541
Glenn Kasten5e92a782012-01-30 07:40:52 -0800542status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700543 const effect_uuid_t *type,
544 uint32_t preferredTypeFlag,
545 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700546{
547 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
548 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700549 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700550}
551
Glenn Kastend848eb42016-03-08 13:42:11 -0800552status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700553 effect_descriptor_t *descriptors,
554 uint32_t *count)
555{
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800556 if (descriptors == nullptr || count == nullptr) {
557 return BAD_VALUE;
558 }
Eric Laurent57dae992011-07-24 13:36:09 -0700559 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
560 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800561
562 int32_t audioSessionAidl = VALUE_OR_RETURN_STATUS(
563 legacy2aidl_audio_session_t_int32_t(audioSession));
564 media::Int countAidl;
565 countAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*count));
566 std::vector<media::EffectDescriptor> retAidl;
567 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
568 aps->queryDefaultPreProcessing(audioSessionAidl, &countAidl, &retAidl)));
569 *count = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(countAidl.value));
570 RETURN_STATUS_IF_ERROR(convertRange(retAidl.begin(), retAidl.end(), descriptors,
571 aidl2legacy_EffectDescriptor_effect_descriptor_t));
572 return OK;
Eric Laurent57dae992011-07-24 13:36:09 -0700573}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700574
575status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
576{
577 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
578 if (af == 0) return PERMISSION_DENIED;
579 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
580 return NO_ERROR;
581}
582
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700583status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
584 const String16& opPackageName,
585 const char *uuidStr,
586 int32_t priority,
587 audio_source_t source,
588 audio_unique_id_t *id)
589{
590 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
591 if (aps == 0) return PERMISSION_DENIED;
592
593 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
594
595 // Convert type & uuid from string to effect_uuid_t.
596 effect_uuid_t type;
597 if (typeStr != NULL) {
598 status_t res = stringToGuid(typeStr, &type);
599 if (res != OK) return res;
600 } else {
601 type = *EFFECT_UUID_NULL;
602 }
603
604 effect_uuid_t uuid;
605 if (uuidStr != NULL) {
606 status_t res = stringToGuid(uuidStr, &uuid);
607 if (res != OK) return res;
608 } else {
609 uuid = *EFFECT_UUID_NULL;
610 }
611
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800612 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
613 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
614 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
615 legacy2aidl_String16_string(opPackageName));
616 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
617 legacy2aidl_audio_source_t_AudioSourceType(source));
618 int32_t retAidl;
619 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
620 aps->addSourceDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, sourceAidl,
621 &retAidl)));
622 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
623 return OK;
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700624}
625
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700626status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
627 const String16& opPackageName,
628 const char *uuidStr,
629 int32_t priority,
630 audio_usage_t usage,
631 audio_unique_id_t *id)
632{
633 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
634 if (aps == 0) return PERMISSION_DENIED;
635
636 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
637
638 // Convert type & uuid from string to effect_uuid_t.
639 effect_uuid_t type;
640 if (typeStr != NULL) {
641 status_t res = stringToGuid(typeStr, &type);
642 if (res != OK) return res;
643 } else {
644 type = *EFFECT_UUID_NULL;
645 }
646
647 effect_uuid_t uuid;
648 if (uuidStr != NULL) {
649 status_t res = stringToGuid(uuidStr, &uuid);
650 if (res != OK) return res;
651 } else {
652 uuid = *EFFECT_UUID_NULL;
653 }
654
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800655 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
656 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
657 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
658 legacy2aidl_String16_string(opPackageName));
659 media::AudioUsage usageAidl = VALUE_OR_RETURN_STATUS(
660 legacy2aidl_audio_usage_t_AudioUsage(usage));
661 int32_t retAidl;
662 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
663 aps->addStreamDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, usageAidl,
664 &retAidl)));
665 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
666 return OK;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700667}
668
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700669status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
670{
671 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
672 if (aps == 0) return PERMISSION_DENIED;
673
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800674 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
675 return statusTFromBinderStatus(aps->removeSourceDefaultEffect(idAidl));
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700676}
677
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700678status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
679{
680 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
681 if (aps == 0) return PERMISSION_DENIED;
682
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800683 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
684 return statusTFromBinderStatus(aps->removeStreamDefaultEffect(idAidl));
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700685}
686
Eric Laurent801a1182010-06-09 00:17:29 -0700687// -------------------------------------------------------------------------
688
689status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
690{
691 if (str == NULL || guid == NULL) {
692 return BAD_VALUE;
693 }
694
695 int tmp[10];
696
697 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
698 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
699 return BAD_VALUE;
700 }
701 guid->timeLow = (uint32_t)tmp[0];
702 guid->timeMid = (uint16_t)tmp[1];
703 guid->timeHiAndVersion = (uint16_t)tmp[2];
704 guid->clockSeq = (uint16_t)tmp[3];
705 guid->node[0] = (uint8_t)tmp[4];
706 guid->node[1] = (uint8_t)tmp[5];
707 guid->node[2] = (uint8_t)tmp[6];
708 guid->node[3] = (uint8_t)tmp[7];
709 guid->node[4] = (uint8_t)tmp[8];
710 guid->node[5] = (uint8_t)tmp[9];
711
712 return NO_ERROR;
713}
714
715status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
716{
717 if (guid == NULL || str == NULL) {
718 return BAD_VALUE;
719 }
720
721 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
722 guid->timeLow,
723 guid->timeMid,
724 guid->timeHiAndVersion,
725 guid->clockSeq,
726 guid->node[0],
727 guid->node[1],
728 guid->node[2],
729 guid->node[3],
730 guid->node[4],
731 guid->node[5]);
732
733 return NO_ERROR;
734}
735
736
Glenn Kasten40bc9062015-03-20 09:09:33 -0700737} // namespace android