blob: ad8ad7b4c8182c5566d78e2042478cf949b9c48d [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 Ganovbe71aa22015-04-28 12:06:02 -070060AudioEffect::AudioEffect(const String16& opPackageName)
Mikhail Naganov416fffe2020-07-31 17:36:08 -070061 : mOpPackageName(opPackageName)
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
110 mIEffectClient = new EffectClient(this);
Eric Laurentb6436272016-12-07 19:24:50 -0800111 mClientPid = IPCThreadState::self()->getCallingPid();
Andy Hung8b0bfd92019-12-23 13:11:11 -0800112 mClientUid = IPCThreadState::self()->getCallingUid();
Eric Laurent801a1182010-06-09 00:17:29 -0700113
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800114 media::CreateEffectRequest request;
115 request.desc = VALUE_OR_RETURN_STATUS(
116 legacy2aidl_effect_descriptor_t_EffectDescriptor(mDescriptor));
117 request.client = mIEffectClient;
118 request.priority = priority;
119 request.output = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io));
120 request.sessionId = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(mSessionId));
121 request.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(device));
122 request.opPackageName = VALUE_OR_RETURN_STATUS(legacy2aidl_String16_string(mOpPackageName));
123 request.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(mClientPid));
124 request.probe = probe;
125
126 media::CreateEffectResponse response;
127
128 mStatus = audioFlinger->createEffect(request, &response);
129
130 if (mStatus == OK) {
131 mId = response.id;
132 enabled = response.enabled;
133 iEffect = response.effect;
134 mDescriptor = VALUE_OR_RETURN_STATUS(
135 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
136 }
Eric Laurent801a1182010-06-09 00:17:29 -0700137
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700138 // In probe mode, we stop here and return the status: the IEffect interface to
139 // audio flinger will not be retained. initCheck() will return the creation status
140 // but all other APIs will return invalid operation.
141 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000142 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700143 guidToString(type, typeBuffer, sizeof(typeBuffer));
144 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700145 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000146 type != nullptr ? typeBuffer : "NULL",
147 uuid != nullptr ? uuidBuffer : "NULL",
148 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700149 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700150 mStatus = NO_INIT;
151 }
Eric Laurent801a1182010-06-09 00:17:29 -0700152 return mStatus;
153 }
154
155 mEnabled = (volatile int32_t)enabled;
156
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700157 if (media::SharedFileRegion shmem;
158 !iEffect->getCblk(&shmem).isOk()
159 || !convertSharedFileRegionToIMemory(shmem, &cblk)
160 || cblk == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700161 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000162 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700163 return mStatus;
164 }
165
Eric Laurenteecd7652015-06-04 16:20:16 -0700166 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700167 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700168 // TODO: Using unsecurePointer() has some associated security pitfalls
169 // (see declaration for details).
170 // Either document why it is safe in this case or address the
171 // issue (e.g. by copying).
172 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700173 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
174 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
175
Marco Nelissen06b46062014-11-14 07:58:25 -0800176 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700177 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
178 mStatus, mEnabled, mClientPid);
179
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800180 if (!audio_is_global_session(mSessionId)) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800181 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700182 }
Eric Laurent801a1182010-06-09 00:17:29 -0700183
184 return mStatus;
185}
186
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700187status_t AudioEffect::set(const char *typeStr,
188 const char *uuidStr,
189 int32_t priority,
190 effect_callback_t cbf,
191 void* user,
192 audio_session_t sessionId,
193 audio_io_handle_t io,
194 const AudioDeviceTypeAddr& device,
195 bool probe)
196{
197 effect_uuid_t type;
198 effect_uuid_t *pType = nullptr;
199 effect_uuid_t uuid;
200 effect_uuid_t *pUuid = nullptr;
201
202 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
203 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
204
205 if (stringToGuid(typeStr, &type) == NO_ERROR) {
206 pType = &type;
207 }
208 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
209 pUuid = &uuid;
210 }
211
212 return set(pType, pUuid, priority, cbf, user, sessionId, io, device, probe);
213}
214
Eric Laurent801a1182010-06-09 00:17:29 -0700215
216AudioEffect::~AudioEffect()
217{
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700219
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700220 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800221 if (!audio_is_global_session(mSessionId)) {
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700222 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
223 }
Eric Laurent801a1182010-06-09 00:17:29 -0700224 if (mIEffect != NULL) {
225 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800226 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700227 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700228 mIEffect.clear();
229 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700230 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700231 mIEffectClient.clear();
232 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700233}
234
235
236status_t AudioEffect::initCheck() const
237{
238 return mStatus;
239}
240
241// -------------------------------------------------------------------------
242
243effect_descriptor_t AudioEffect::descriptor() const
244{
245 return mDescriptor;
246}
247
Eric Laurentda7581b2010-07-02 08:12:41 -0700248bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700249{
250 return (mEnabled != 0);
251}
252
Eric Laurentda7581b2010-07-02 08:12:41 -0700253status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700254{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700255 if (mProbe) {
256 return INVALID_OPERATION;
257 }
Eric Laurent801a1182010-06-09 00:17:29 -0700258 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800259 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700260 }
Eric Laurent801a1182010-06-09 00:17:29 -0700261
Eric Laurentf5aafb22010-11-18 08:40:16 -0800262 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800263 AutoMutex lock(mLock);
264 if (enabled != mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700265 Status bs;
266
Eric Laurentf5aafb22010-11-18 08:40:16 -0800267 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("enable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700269 bs = mIEffect->enable(&status);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800270 } else {
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("disable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700272 bs = mIEffect->disable(&status);
273 }
274 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800275 status = statusTFromBinderStatus(bs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700276 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800277 if (status == NO_ERROR) {
278 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700279 }
Eric Laurent801a1182010-06-09 00:17:29 -0700280 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800281 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700282}
283
Eric Laurent25f43952010-07-28 05:40:18 -0700284status_t AudioEffect::command(uint32_t cmdCode,
285 uint32_t cmdSize,
286 void *cmdData,
287 uint32_t *replySize,
288 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700289{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700290 if (mProbe) {
291 return INVALID_OPERATION;
292 }
Eric Laurent801a1182010-06-09 00:17:29 -0700293 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100294 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800295 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700296 }
297
Eric Laurentf5aafb22010-11-18 08:40:16 -0800298 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
299 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
300 return NO_ERROR;
301 }
302 if (replySize == NULL || *replySize != sizeof(status_t) || replyData == NULL) {
303 return BAD_VALUE;
304 }
305 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700306 }
307
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700308 std::vector<uint8_t> data;
309 appendToBuffer(cmdData, cmdSize, &data);
310
311 status_t status;
312 std::vector<uint8_t> response;
313
314 Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status);
315 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800316 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700317 }
318 if (status == NO_ERROR) {
319 memcpy(replyData, response.data(), response.size());
320 *replySize = response.size();
321 }
Eric Laurent0fa449c2010-09-24 11:52:04 -0700322
323 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800324 if (status == NO_ERROR) {
325 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700326 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800327 if (status == NO_ERROR) {
328 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700329 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800330 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700331 }
332
Eric Laurent8569f0d2010-07-29 23:43:43 -0700333 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700334}
335
Eric Laurent801a1182010-06-09 00:17:29 -0700336status_t AudioEffect::setParameter(effect_param_t *param)
337{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700338 if (mProbe) {
339 return INVALID_OPERATION;
340 }
Eric Laurent801a1182010-06-09 00:17:29 -0700341 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800342 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700343 }
344
345 if (param == NULL || param->psize == 0 || param->vsize == 0) {
346 return BAD_VALUE;
347 }
348
Eric Laurent25f43952010-07-28 05:40:18 -0700349 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700350
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700351 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
352 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700353
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700354 std::vector<uint8_t> cmd;
355 appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd);
356 std::vector<uint8_t> response;
357 status_t status;
358 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM,
359 cmd,
360 sizeof(int),
361 &response,
362 &status);
363 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800364 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700365 return status;
366 }
367 assert(response.size() == sizeof(int));
368 memcpy(&param->status, response.data(), response.size());
369 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700370}
371
372status_t AudioEffect::setParameterDeferred(effect_param_t *param)
373{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700374 if (mProbe) {
375 return INVALID_OPERATION;
376 }
Eric Laurent801a1182010-06-09 00:17:29 -0700377 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800378 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700379 }
380
381 if (param == NULL || param->psize == 0 || param->vsize == 0) {
382 return BAD_VALUE;
383 }
384
385 Mutex::Autolock _l(mCblk->lock);
386
387 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
388 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
389
390 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
391 return NO_MEMORY;
392 }
393 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
394 *p++ = size;
395 memcpy(p, param, sizeof(effect_param_t) + psize);
396 mCblk->clientIndex += size;
397
398 return NO_ERROR;
399}
400
401status_t AudioEffect::setParameterCommit()
402{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700403 if (mProbe) {
404 return INVALID_OPERATION;
405 }
Eric Laurent801a1182010-06-09 00:17:29 -0700406 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800407 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700408 }
409
410 Mutex::Autolock _l(mCblk->lock);
411 if (mCblk->clientIndex == 0) {
412 return INVALID_OPERATION;
413 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700414 std::vector<uint8_t> cmd;
415 std::vector<uint8_t> response;
416 status_t status;
417 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT,
418 cmd,
419 0,
420 &response,
421 &status);
422 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800423 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700424 }
425 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700426}
427
428status_t AudioEffect::getParameter(effect_param_t *param)
429{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700430 if (mProbe) {
431 return INVALID_OPERATION;
432 }
Eric Laurent801a1182010-06-09 00:17:29 -0700433 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800434 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700435 }
436
437 if (param == NULL || param->psize == 0 || param->vsize == 0) {
438 return BAD_VALUE;
439 }
440
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700441 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
442 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700443
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700444 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
445 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700446
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700447 status_t status;
448 std::vector<uint8_t> cmd;
449 std::vector<uint8_t> response;
450 appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd);
451
452 Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status);
453 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800454 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700455 return status;
456 }
457 memcpy(param, response.data(), response.size());
458 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700459}
460
461
462// -------------------------------------------------------------------------
463
464void AudioEffect::binderDied()
465{
Steve Block5ff1dd52012-01-05 23:22:43 +0000466 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800467 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800468 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700469 status_t status = DEAD_OBJECT;
470 mCbf(EVENT_ERROR, mUserData, &status);
471 }
472 mIEffect.clear();
473}
474
475// -------------------------------------------------------------------------
476
477void AudioEffect::controlStatusChanged(bool controlGranted)
478{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700479 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
480 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700481 if (controlGranted) {
482 if (mStatus == ALREADY_EXISTS) {
483 mStatus = NO_ERROR;
484 }
485 } else {
486 if (mStatus == NO_ERROR) {
487 mStatus = ALREADY_EXISTS;
488 }
489 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800490 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700491 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
492 }
493}
494
495void AudioEffect::enableStatusChanged(bool enabled)
496{
Steve Block3856b092011-10-20 11:56:00 +0100497 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700498 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800499 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800500 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700501 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
502 }
503 }
504}
505
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700506void AudioEffect::commandExecuted(int32_t cmdCode,
507 const std::vector<uint8_t>& cmdData,
508 const std::vector<uint8_t>& replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700509{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700510 if (cmdData.empty() || replyData.empty()) {
Eric Laurent801a1182010-06-09 00:17:29 -0700511 return;
512 }
513
Glenn Kastena0d68332012-01-27 16:47:15 -0800514 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700515 std::vector<uint8_t> cmdDataCopy(cmdData);
516 effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data());
517 cmd->status = *reinterpret_cast<const int32_t *>(replyData.data());
Eric Laurent801a1182010-06-09 00:17:29 -0700518 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
519 }
520}
521
522// -------------------------------------------------------------------------
523
Eric Laurent801a1182010-06-09 00:17:29 -0700524status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
525{
526 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
527 if (af == 0) return PERMISSION_DENIED;
528 return af->queryNumberEffects(numEffects);
529}
530
Eric Laurentffe9c252010-06-23 17:38:20 -0700531status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700532{
533 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
534 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700535 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700536}
537
Glenn Kasten5e92a782012-01-30 07:40:52 -0800538status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700539 const effect_uuid_t *type,
540 uint32_t preferredTypeFlag,
541 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700542{
543 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
544 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700545 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700546}
547
Glenn Kastend848eb42016-03-08 13:42:11 -0800548status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700549 effect_descriptor_t *descriptors,
550 uint32_t *count)
551{
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800552 if (descriptors == nullptr || count == nullptr) {
553 return BAD_VALUE;
554 }
Eric Laurent57dae992011-07-24 13:36:09 -0700555 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
556 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800557
558 int32_t audioSessionAidl = VALUE_OR_RETURN_STATUS(
559 legacy2aidl_audio_session_t_int32_t(audioSession));
560 media::Int countAidl;
561 countAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*count));
562 std::vector<media::EffectDescriptor> retAidl;
563 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
564 aps->queryDefaultPreProcessing(audioSessionAidl, &countAidl, &retAidl)));
565 *count = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(countAidl.value));
566 RETURN_STATUS_IF_ERROR(convertRange(retAidl.begin(), retAidl.end(), descriptors,
567 aidl2legacy_EffectDescriptor_effect_descriptor_t));
568 return OK;
Eric Laurent57dae992011-07-24 13:36:09 -0700569}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700570
571status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
572{
573 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
574 if (af == 0) return PERMISSION_DENIED;
575 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
576 return NO_ERROR;
577}
578
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700579status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
580 const String16& opPackageName,
581 const char *uuidStr,
582 int32_t priority,
583 audio_source_t source,
584 audio_unique_id_t *id)
585{
586 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
587 if (aps == 0) return PERMISSION_DENIED;
588
589 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
590
591 // Convert type & uuid from string to effect_uuid_t.
592 effect_uuid_t type;
593 if (typeStr != NULL) {
594 status_t res = stringToGuid(typeStr, &type);
595 if (res != OK) return res;
596 } else {
597 type = *EFFECT_UUID_NULL;
598 }
599
600 effect_uuid_t uuid;
601 if (uuidStr != NULL) {
602 status_t res = stringToGuid(uuidStr, &uuid);
603 if (res != OK) return res;
604 } else {
605 uuid = *EFFECT_UUID_NULL;
606 }
607
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800608 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
609 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
610 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
611 legacy2aidl_String16_string(opPackageName));
612 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
613 legacy2aidl_audio_source_t_AudioSourceType(source));
614 int32_t retAidl;
615 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
616 aps->addSourceDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, sourceAidl,
617 &retAidl)));
618 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
619 return OK;
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700620}
621
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700622status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
623 const String16& opPackageName,
624 const char *uuidStr,
625 int32_t priority,
626 audio_usage_t usage,
627 audio_unique_id_t *id)
628{
629 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
630 if (aps == 0) return PERMISSION_DENIED;
631
632 if (typeStr == NULL && uuidStr == NULL) return BAD_VALUE;
633
634 // Convert type & uuid from string to effect_uuid_t.
635 effect_uuid_t type;
636 if (typeStr != NULL) {
637 status_t res = stringToGuid(typeStr, &type);
638 if (res != OK) return res;
639 } else {
640 type = *EFFECT_UUID_NULL;
641 }
642
643 effect_uuid_t uuid;
644 if (uuidStr != NULL) {
645 status_t res = stringToGuid(uuidStr, &uuid);
646 if (res != OK) return res;
647 } else {
648 uuid = *EFFECT_UUID_NULL;
649 }
650
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800651 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
652 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
653 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
654 legacy2aidl_String16_string(opPackageName));
655 media::AudioUsage usageAidl = VALUE_OR_RETURN_STATUS(
656 legacy2aidl_audio_usage_t_AudioUsage(usage));
657 int32_t retAidl;
658 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
659 aps->addStreamDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, usageAidl,
660 &retAidl)));
661 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
662 return OK;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700663}
664
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700665status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
666{
667 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
668 if (aps == 0) return PERMISSION_DENIED;
669
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800670 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
671 return statusTFromBinderStatus(aps->removeSourceDefaultEffect(idAidl));
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700672}
673
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700674status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
675{
676 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
677 if (aps == 0) return PERMISSION_DENIED;
678
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800679 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
680 return statusTFromBinderStatus(aps->removeStreamDefaultEffect(idAidl));
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700681}
682
Eric Laurent801a1182010-06-09 00:17:29 -0700683// -------------------------------------------------------------------------
684
685status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
686{
687 if (str == NULL || guid == NULL) {
688 return BAD_VALUE;
689 }
690
691 int tmp[10];
692
693 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
694 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
695 return BAD_VALUE;
696 }
697 guid->timeLow = (uint32_t)tmp[0];
698 guid->timeMid = (uint16_t)tmp[1];
699 guid->timeHiAndVersion = (uint16_t)tmp[2];
700 guid->clockSeq = (uint16_t)tmp[3];
701 guid->node[0] = (uint8_t)tmp[4];
702 guid->node[1] = (uint8_t)tmp[5];
703 guid->node[2] = (uint8_t)tmp[6];
704 guid->node[3] = (uint8_t)tmp[7];
705 guid->node[4] = (uint8_t)tmp[8];
706 guid->node[5] = (uint8_t)tmp[9];
707
708 return NO_ERROR;
709}
710
711status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
712{
713 if (guid == NULL || str == NULL) {
714 return BAD_VALUE;
715 }
716
717 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
718 guid->timeLow,
719 guid->timeMid,
720 guid->timeHiAndVersion,
721 guid->clockSeq,
722 guid->node[0],
723 guid->node[1],
724 guid->node[2],
725 guid->node[3],
726 guid->node[4],
727 guid->node[5]);
728
729 return NO_ERROR;
730}
731
732
Glenn Kasten40bc9062015-03-20 09:09:33 -0700733} // namespace android