blob: e5a7029841bb08f2a497dcbc31ed9c8e8e282703 [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
Eric Laurent801a1182010-06-09 00:17:29 -070026#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070027#include <media/AudioEffect.h>
28#include <media/ShmemCompat.h>
29#include <private/media/AudioEffectShared.h>
30#include <utils/Log.h>
Eric Laurent801a1182010-06-09 00:17:29 -070031
Ytai Ben-Tsvice182942020-11-04 14:48:01 -080032#define VALUE_OR_RETURN_STATUS(exp) \
33 ({ \
34 auto _tmp = (exp); \
35 if (!_tmp.ok()) return _tmp.error(); \
36 std::move(_tmp.value()); \
37 })
38
Eric Laurent801a1182010-06-09 00:17:29 -070039namespace android {
40
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070041using binder::Status;
42
43namespace {
44
45// Copy from a raw pointer + size into a vector of bytes.
46void appendToBuffer(const void* data,
47 size_t size,
48 std::vector<uint8_t>* buffer) {
49 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
50 buffer->insert(buffer->end(), p, p + size);
51}
52
53} // namespace
54
Eric Laurent801a1182010-06-09 00:17:29 -070055// ---------------------------------------------------------------------------
56
Svet Ganovbe71aa22015-04-28 12:06:02 -070057AudioEffect::AudioEffect(const String16& opPackageName)
Mikhail Naganov416fffe2020-07-31 17:36:08 -070058 : mOpPackageName(opPackageName)
Eric Laurent801a1182010-06-09 00:17:29 -070059{
60}
61
Eric Laurent801a1182010-06-09 00:17:29 -070062status_t AudioEffect::set(const effect_uuid_t *type,
63 const effect_uuid_t *uuid,
64 int32_t priority,
65 effect_callback_t cbf,
66 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080067 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080068 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070069 const AudioDeviceTypeAddr& device,
70 bool probe)
Eric Laurent801a1182010-06-09 00:17:29 -070071{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070072 sp<media::IEffect> iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -070073 sp<IMemory> cblk;
74 int enabled;
75
Steve Block3856b092011-10-20 11:56:00 +010076 ALOGV("set %p mUserData: %p uuid: %p timeLow %08x", this, user, type, type ? type->timeLow : 0);
Eric Laurent801a1182010-06-09 00:17:29 -070077
78 if (mIEffect != 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +000079 ALOGW("Effect already in use");
Eric Laurent801a1182010-06-09 00:17:29 -070080 return INVALID_OPERATION;
81 }
82
Eric Laurent94876032019-11-13 12:45:28 -080083 if (sessionId == AUDIO_SESSION_DEVICE && io != AUDIO_IO_HANDLE_NONE) {
84 ALOGW("IO handle should not be specified for device effect");
85 return BAD_VALUE;
86 }
Eric Laurent801a1182010-06-09 00:17:29 -070087 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
88 if (audioFlinger == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000089 ALOGE("set(): Could not get audioflinger");
Eric Laurent801a1182010-06-09 00:17:29 -070090 return NO_INIT;
91 }
92
93 if (type == NULL && uuid == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +000094 ALOGW("Must specify at least type or uuid");
Eric Laurent801a1182010-06-09 00:17:29 -070095 return BAD_VALUE;
96 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -070097 mProbe = probe;
Eric Laurent801a1182010-06-09 00:17:29 -070098 mPriority = priority;
99 mCbf = cbf;
100 mUserData = user;
101 mSessionId = sessionId;
102
103 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
Glenn Kastena189a682012-02-20 12:16:30 -0800104 mDescriptor.type = *(type != NULL ? type : EFFECT_UUID_NULL);
105 mDescriptor.uuid = *(uuid != NULL ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700106
107 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -0800108 mClientPid = IPCThreadState::self()->getCallingPid();
Andy Hung8b0bfd92019-12-23 13:11:11 -0800109 mClientUid = IPCThreadState::self()->getCallingUid();
Eric Laurent801a1182010-06-09 00:17:29 -0700110
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800111 media::CreateEffectRequest request;
112 request.desc = VALUE_OR_RETURN_STATUS(
113 legacy2aidl_effect_descriptor_t_EffectDescriptor(mDescriptor));
114 request.client = mIEffectClient;
115 request.priority = priority;
116 request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
117 request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(mSessionId));
118 request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device));
119 request.opPackageName = VALUE_OR_RETURN_STATUS(legacy2aidl_String16_string(mOpPackageName));
120 request.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(mClientPid));
121 request.probe = probe;
122
123 media::CreateEffectResponse response;
124
125 mStatus = audioFlinger->createEffect(request, &response);
126
127 if (mStatus == OK) {
128 mId = response.id;
129 enabled = response.enabled;
130 iEffect = response.effect;
131 mDescriptor = VALUE_OR_RETURN_STATUS(
132 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
133 }
Eric Laurent801a1182010-06-09 00:17:29 -0700134
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700135 // In probe mode, we stop here and return the status: the IEffect interface to
136 // audio flinger will not be retained. initCheck() will return the creation status
137 // but all other APIs will return invalid operation.
138 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000139 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700140 guidToString(type, typeBuffer, sizeof(typeBuffer));
141 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700142 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000143 type != nullptr ? typeBuffer : "NULL",
144 uuid != nullptr ? uuidBuffer : "NULL",
145 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700146 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700147 mStatus = NO_INIT;
148 }
Eric Laurent801a1182010-06-09 00:17:29 -0700149 return mStatus;
150 }
151
152 mEnabled = (volatile int32_t)enabled;
153
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700154 if (media::SharedFileRegion shmem;
155 !iEffect->getCblk(&shmem).isOk()
156 || !convertSharedFileRegionToIMemory(shmem, &cblk)
157 || cblk == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700158 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000159 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700160 return mStatus;
161 }
162
Eric Laurenteecd7652015-06-04 16:20:16 -0700163 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700164 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700165 // TODO: Using unsecurePointer() has some associated security pitfalls
166 // (see declaration for details).
167 // Either document why it is safe in this case or address the
168 // issue (e.g. by copying).
169 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700170 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
171 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
172
Marco Nelissen06b46062014-11-14 07:58:25 -0800173 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700174 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
175 mStatus, mEnabled, mClientPid);
176
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800177 if (!audio_is_global_session(mSessionId)) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800178 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700179 }
Eric Laurent801a1182010-06-09 00:17:29 -0700180
181 return mStatus;
182}
183
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700184status_t AudioEffect::set(const char *typeStr,
185 const char *uuidStr,
186 int32_t priority,
187 effect_callback_t cbf,
188 void* user,
189 audio_session_t sessionId,
190 audio_io_handle_t io,
191 const AudioDeviceTypeAddr& device,
192 bool probe)
193{
194 effect_uuid_t type;
195 effect_uuid_t *pType = nullptr;
196 effect_uuid_t uuid;
197 effect_uuid_t *pUuid = nullptr;
198
199 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
200 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
201
202 if (stringToGuid(typeStr, &type) == NO_ERROR) {
203 pType = &type;
204 }
205 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
206 pUuid = &uuid;
207 }
208
209 return set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe);
210}
211
Eric Laurent801a1182010-06-09 00:17:29 -0700212
213AudioEffect::~AudioEffect()
214{
Steve Block3856b092011-10-20 11:56:00 +0100215 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700216
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700217 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800218 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700219 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
220 }
Eric Laurent801a1182010-06-09 00:17:29 -0700221 if (mIEffect != NULL) {
222 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800223 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700224 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700225 mIEffect.clear();
226 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700227 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700228 mIEffectClient.clear();
229 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700230}
231
232
233status_t AudioEffect::initCheck() const
234{
235 return mStatus;
236}
237
238// -------------------------------------------------------------------------
239
240effect_descriptor_t AudioEffect::descriptor() const
241{
242 return mDescriptor;
243}
244
Eric Laurentda7581b2010-07-02 08:12:41 -0700245bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700246{
247 return (mEnabled != 0);
248}
249
Eric Laurentda7581b2010-07-02 08:12:41 -0700250status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700251{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700252 if (mProbe) {
253 return INVALID_OPERATION;
254 }
Eric Laurent801a1182010-06-09 00:17:29 -0700255 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800256 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700257 }
Eric Laurent801a1182010-06-09 00:17:29 -0700258
Eric Laurentf5aafb22010-11-18 08:40:16 -0800259 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800260 AutoMutex lock(mLock);
261 if (enabled != mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700262 Status bs;
263
Eric Laurentf5aafb22010-11-18 08:40:16 -0800264 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("enable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700266 bs = mIEffect->enable(&status);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800267 } else {
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("disable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700269 bs = mIEffect->disable(&status);
270 }
271 if (!bs.isOk()) {
272 status = bs.transactionError();
Eric Laurentda7581b2010-07-02 08:12:41 -0700273 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800274 if (status == NO_ERROR) {
275 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700276 }
Eric Laurent801a1182010-06-09 00:17:29 -0700277 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800278 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700279}
280
Eric Laurent25f43952010-07-28 05:40:18 -0700281status_t AudioEffect::command(uint32_t cmdCode,
282 uint32_t cmdSize,
283 void *cmdData,
284 uint32_t *replySize,
285 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700286{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700287 if (mProbe) {
288 return INVALID_OPERATION;
289 }
Eric Laurent801a1182010-06-09 00:17:29 -0700290 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100291 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800292 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700293 }
294
Eric Laurentf5aafb22010-11-18 08:40:16 -0800295 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
296 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
297 return NO_ERROR;
298 }
299 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
300 return BAD_VALUE;
301 }
302 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700303 }
304
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700305 std::vector<uint8_t> data;
306 appendToBuffer(cmdData, cmdSize, &data);
307
308 status_t status;
309 std::vector<uint8_t> response;
310
311 Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status);
312 if (!bs.isOk()) {
313 status = bs.transactionError();
314 }
315 if (status == NO_ERROR) {
316 memcpy(replyData, response.data(), response.size());
317 *replySize = response.size();
318 }
Eric Laurent0fa449c2010-09-24 11:52:04 -0700319
320 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800321 if (status == NO_ERROR) {
322 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700323 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800324 if (status == NO_ERROR) {
325 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700326 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800327 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700328 }
329
Eric Laurent8569f0d2010-07-29 23:43:43 -0700330 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700331}
332
Eric Laurent801a1182010-06-09 00:17:29 -0700333status_t AudioEffect::setParameter(effect_param_t *param)
334{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700335 if (mProbe) {
336 return INVALID_OPERATION;
337 }
Eric Laurent801a1182010-06-09 00:17:29 -0700338 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800339 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700340 }
341
342 if (param == NULL || param->psize == 0 || param->vsize == 0) {
343 return BAD_VALUE;
344 }
345
Eric Laurent25f43952010-07-28 05:40:18 -0700346 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700347
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700348 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
349 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700350
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700351 std::vector<uint8_t> cmd;
352 appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd);
353 std::vector<uint8_t> response;
354 status_t status;
355 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM,
356 cmd,
357 sizeof(int),
358 &response,
359 &status);
360 if (!bs.isOk()) {
361 status = bs.transactionError();
362 return status;
363 }
364 assert(response.size() == sizeof(int));
365 memcpy(&param->status, response.data(), response.size());
366 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700367}
368
369status_t AudioEffect::setParameterDeferred(effect_param_t *param)
370{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700371 if (mProbe) {
372 return INVALID_OPERATION;
373 }
Eric Laurent801a1182010-06-09 00:17:29 -0700374 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800375 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700376 }
377
378 if (param == NULL || param->psize == 0 || param->vsize == 0) {
379 return BAD_VALUE;
380 }
381
382 Mutex::Autolock _l(mCblk->lock);
383
384 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
385 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
386
387 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
388 return NO_MEMORY;
389 }
390 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
391 *p++ = size;
392 memcpy(p, param, sizeof(effect_param_t) + psize);
393 mCblk->clientIndex += size;
394
395 return NO_ERROR;
396}
397
398status_t AudioEffect::setParameterCommit()
399{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700400 if (mProbe) {
401 return INVALID_OPERATION;
402 }
Eric Laurent801a1182010-06-09 00:17:29 -0700403 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800404 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700405 }
406
407 Mutex::Autolock _l(mCblk->lock);
408 if (mCblk->clientIndex == 0) {
409 return INVALID_OPERATION;
410 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700411 std::vector<uint8_t> cmd;
412 std::vector<uint8_t> response;
413 status_t status;
414 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT,
415 cmd,
416 0,
417 &response,
418 &status);
419 if (!bs.isOk()) {
420 status = bs.transactionError();
421 }
422 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700423}
424
425status_t AudioEffect::getParameter(effect_param_t *param)
426{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700427 if (mProbe) {
428 return INVALID_OPERATION;
429 }
Eric Laurent801a1182010-06-09 00:17:29 -0700430 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800431 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700432 }
433
434 if (param == NULL || param->psize == 0 || param->vsize == 0) {
435 return BAD_VALUE;
436 }
437
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700438 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
439 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700440
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700441 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
442 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700443
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700444 status_t status;
445 std::vector<uint8_t> cmd;
446 std::vector<uint8_t> response;
447 appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd);
448
449 Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status);
450 if (!bs.isOk()) {
451 status = bs.transactionError();
452 return status;
453 }
454 memcpy(param, response.data(), response.size());
455 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700456}
457
458
459// -------------------------------------------------------------------------
460
461void AudioEffect::binderDied()
462{
Steve Block5ff1dd52012-01-05 23:22:43 +0000463 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800464 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800465 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700466 status_t status = DEAD_OBJECT;
467 mCbf(EVENT_ERROR, mUserData, &status);
468 }
469 mIEffect.clear();
470}
471
472// -------------------------------------------------------------------------
473
474void AudioEffect::controlStatusChanged(bool controlGranted)
475{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700476 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
477 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700478 if (controlGranted) {
479 if (mStatus == ALREADY_EXISTS) {
480 mStatus = NO_ERROR;
481 }
482 } else {
483 if (mStatus == NO_ERROR) {
484 mStatus = ALREADY_EXISTS;
485 }
486 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800487 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700488 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
489 }
490}
491
492void AudioEffect::enableStatusChanged(bool enabled)
493{
Steve Block3856b092011-10-20 11:56:00 +0100494 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700495 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800496 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800497 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700498 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
499 }
500 }
501}
502
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700503void AudioEffect::commandExecuted(int32_t cmdCode,
504 const std::vector<uint8_t>& cmdData,
505 const std::vector<uint8_t>& replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700506{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700507 if (cmdData.empty() || replyData.empty()) {
Eric Laurent801a1182010-06-09 00:17:29 -0700508 return;
509 }
510
Glenn Kastena0d68332012-01-27 16:47:15 -0800511 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700512 std::vector<uint8_t> cmdDataCopy(cmdData);
513 effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data());
514 cmd->status = *reinterpret_cast<const int32_t *>(replyData.data());
Eric Laurent801a1182010-06-09 00:17:29 -0700515 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
516 }
517}
518
519// -------------------------------------------------------------------------
520
Eric Laurent801a1182010-06-09 00:17:29 -0700521status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
522{
523 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
524 if (af == 0) return PERMISSION_DENIED;
525 return af->queryNumberEffects(numEffects);
526}
527
Eric Laurentffe9c252010-06-23 17:38:20 -0700528status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700529{
530 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
531 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700532 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700533}
534
Glenn Kasten5e92a782012-01-30 07:40:52 -0800535status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700536 const effect_uuid_t *type,
537 uint32_t preferredTypeFlag,
538 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700539{
540 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
541 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700542 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700543}
544
Glenn Kastend848eb42016-03-08 13:42:11 -0800545status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700546 effect_descriptor_t *descriptors,
547 uint32_t *count)
548{
549 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
550 if (aps == 0) return PERMISSION_DENIED;
551 return aps->queryDefaultPreProcessing(audioSession, descriptors, count);
552}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700553
554status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
555{
556 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
557 if (af == 0) return PERMISSION_DENIED;
558 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
559 return NO_ERROR;
560}
561
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700562status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
563 const String16& opPackageName,
564 const char *uuidStr,
565 int32_t priority,
566 audio_source_t source,
567 audio_unique_id_t *id)
568{
569 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
570 if (aps == 0) return PERMISSION_DENIED;
571
572 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
573
574 // Convert type & uuid from string to effect_uuid_t.
575 effect_uuid_t type;
576 if (typeStr != NULL) {
577 status_t res = stringToGuid(typeStr, &type);
578 if (res != OK) return res;
579 } else {
580 type = *EFFECT_UUID_NULL;
581 }
582
583 effect_uuid_t uuid;
584 if (uuidStr != NULL) {
585 status_t res = stringToGuid(uuidStr, &uuid);
586 if (res != OK) return res;
587 } else {
588 uuid = *EFFECT_UUID_NULL;
589 }
590
591 return aps->addSourceDefaultEffect(&type, opPackageName, &uuid, priority, source, id);
592}
593
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700594status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
595 const String16& opPackageName,
596 const char *uuidStr,
597 int32_t priority,
598 audio_usage_t usage,
599 audio_unique_id_t *id)
600{
601 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
602 if (aps == 0) return PERMISSION_DENIED;
603
604 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
605
606 // Convert type & uuid from string to effect_uuid_t.
607 effect_uuid_t type;
608 if (typeStr != NULL) {
609 status_t res = stringToGuid(typeStr, &type);
610 if (res != OK) return res;
611 } else {
612 type = *EFFECT_UUID_NULL;
613 }
614
615 effect_uuid_t uuid;
616 if (uuidStr != NULL) {
617 status_t res = stringToGuid(uuidStr, &uuid);
618 if (res != OK) return res;
619 } else {
620 uuid = *EFFECT_UUID_NULL;
621 }
622
623 return aps->addStreamDefaultEffect(&type, opPackageName, &uuid, priority, usage, id);
624}
625
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700626status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
627{
628 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
629 if (aps == 0) return PERMISSION_DENIED;
630
631 return aps->removeSourceDefaultEffect(id);
632}
633
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700634status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
635{
636 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
637 if (aps == 0) return PERMISSION_DENIED;
638
639 return aps->removeStreamDefaultEffect(id);
640}
641
Eric Laurent801a1182010-06-09 00:17:29 -0700642// -------------------------------------------------------------------------
643
644status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
645{
646 if (str == NULL || guid == NULL) {
647 return BAD_VALUE;
648 }
649
650 int tmp[10];
651
652 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
653 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
654 return BAD_VALUE;
655 }
656 guid->timeLow = (uint32_t)tmp[0];
657 guid->timeMid = (uint16_t)tmp[1];
658 guid->timeHiAndVersion = (uint16_t)tmp[2];
659 guid->clockSeq = (uint16_t)tmp[3];
660 guid->node[0] = (uint8_t)tmp[4];
661 guid->node[1] = (uint8_t)tmp[5];
662 guid->node[2] = (uint8_t)tmp[6];
663 guid->node[3] = (uint8_t)tmp[7];
664 guid->node[4] = (uint8_t)tmp[8];
665 guid->node[5] = (uint8_t)tmp[9];
666
667 return NO_ERROR;
668}
669
670status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
671{
672 if (guid == NULL || str == NULL) {
673 return BAD_VALUE;
674 }
675
676 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
677 guid->timeLow,
678 guid->timeMid,
679 guid->timeHiAndVersion,
680 guid->clockSeq,
681 guid->node[0],
682 guid->node[1],
683 guid->node[2],
684 guid->node[3],
685 guid->node[4],
686 guid->node[5]);
687
688 return NO_ERROR;
689}
690
691
Glenn Kasten40bc9062015-03-20 09:09:33 -0700692} // namespace android