blob: 8c645c3ecc057df609f2a1072dbb3dd4422fe054 [file] [log] [blame]
Eric Laurent801a1182010-06-09 00:17:29 -07001/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioEffect"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080026#include <android/media/IAudioPolicyService.h>
Eric Laurent801a1182010-06-09 00:17:29 -070027#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080028#include <media/AidlConversion.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070029#include <media/AudioEffect.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080030#include <media/PolicyAidlConversion.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070031#include <media/ShmemCompat.h>
32#include <private/media/AudioEffectShared.h>
33#include <utils/Log.h>
Eric Laurent801a1182010-06-09 00:17:29 -070034
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080035#define RETURN_STATUS_IF_ERROR(x) \
36 { \
37 auto _tmp = (x); \
38 if (_tmp != OK) return _tmp; \
39 }
40
Eric Laurent801a1182010-06-09 00:17:29 -070041namespace android {
Andy Hung1131b6e2020-12-08 20:47:45 -080042using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070043using binder::Status;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080044using media::IAudioPolicyService;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070045
46namespace {
47
48// Copy from a raw pointer + size into a vector of bytes.
49void appendToBuffer(const void* data,
50 size_t size,
51 std::vector<uint8_t>* buffer) {
52 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
53 buffer->insert(buffer->end(), p, p + size);
54}
55
56} // namespace
57
Eric Laurent801a1182010-06-09 00:17:29 -070058// ---------------------------------------------------------------------------
59
Svet Ganov33761132021-05-13 22:51:08 +000060AudioEffect::AudioEffect(const android::content::AttributionSourceState& attributionSource)
61 : mClientAttributionSource(attributionSource)
Eric Laurent801a1182010-06-09 00:17:29 -070062{
63}
64
Eric Laurent801a1182010-06-09 00:17:29 -070065status_t AudioEffect::set(const effect_uuid_t *type,
66 const effect_uuid_t *uuid,
67 int32_t priority,
68 effect_callback_t cbf,
69 void* user,
Glenn Kastend848eb42016-03-08 13:42:11 -080070 audio_session_t sessionId,
Eric Laurent94876032019-11-13 12:45:28 -080071 audio_io_handle_t io,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070072 const AudioDeviceTypeAddr& device,
Eric Laurentde8caf42021-08-11 17:19:25 +020073 bool probe,
74 bool notifyFramesProcessed)
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
Mikhail Naganov097e3b92022-04-27 22:30:22 +000097 if (type == nullptr && uuid == nullptr) {
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));
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000108 mDescriptor.type = *(type != nullptr ? type : EFFECT_UUID_NULL);
109 mDescriptor.uuid = *(uuid != nullptr ? uuid : EFFECT_UUID_NULL);
Eric Laurent801a1182010-06-09 00:17:29 -0700110
Svet Ganov33761132021-05-13 22:51:08 +0000111 // TODO b/182392769: use attribution source 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();
Svet Ganov33761132021-05-13 22:51:08 +0000114 mClientAttributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_pid_t_int32_t(pid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700115 pid_t uid = IPCThreadState::self()->getCallingUid();
Svet Ganov33761132021-05-13 22:51:08 +0000116 mClientAttributionSource.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));
Svet Ganov33761132021-05-13 22:51:08 +0000126 request.attributionSource = mClientAttributionSource;
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800127 request.probe = probe;
Eric Laurentde8caf42021-08-11 17:19:25 +0200128 request.notifyFramesProcessed = notifyFramesProcessed;
Ytai Ben-Tsvice182942020-11-04 14:48:01 -0800129
130 media::CreateEffectResponse response;
131
132 mStatus = audioFlinger->createEffect(request, &response);
133
134 if (mStatus == OK) {
135 mId = response.id;
136 enabled = response.enabled;
137 iEffect = response.effect;
138 mDescriptor = VALUE_OR_RETURN_STATUS(
139 aidl2legacy_EffectDescriptor_effect_descriptor_t(response.desc));
140 }
Eric Laurent801a1182010-06-09 00:17:29 -0700141
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700142 // In probe mode, we stop here and return the status: the IEffect interface to
143 // audio flinger will not be retained. initCheck() will return the creation status
144 // but all other APIs will return invalid operation.
145 if (probe || iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000146 char typeBuffer[64] = {}, uuidBuffer[64] = {};
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700147 guidToString(type, typeBuffer, sizeof(typeBuffer));
148 guidToString(uuid, uuidBuffer, sizeof(uuidBuffer));
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700149 ALOGE_IF(!probe, "set(): AudioFlinger could not create effect %s / %s, status: %d",
Mikhail Naganovabd6e9d2020-03-23 22:25:56 +0000150 type != nullptr ? typeBuffer : "NULL",
151 uuid != nullptr ? uuidBuffer : "NULL",
152 mStatus);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700153 if (!probe && iEffect == 0) {
Eric Laurenteecd7652015-06-04 16:20:16 -0700154 mStatus = NO_INIT;
155 }
Eric Laurent801a1182010-06-09 00:17:29 -0700156 return mStatus;
157 }
158
159 mEnabled = (volatile int32_t)enabled;
160
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700161 if (media::SharedFileRegion shmem;
162 !iEffect->getCblk(&shmem).isOk()
163 || !convertSharedFileRegionToIMemory(shmem, &cblk)
164 || cblk == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700165 mStatus = NO_INIT;
Steve Block29357bc2012-01-06 19:20:56 +0000166 ALOGE("Could not get control block");
Eric Laurent801a1182010-06-09 00:17:29 -0700167 return mStatus;
168 }
169
Eric Laurenteecd7652015-06-04 16:20:16 -0700170 mIEffect = iEffect;
Eric Laurent801a1182010-06-09 00:17:29 -0700171 mCblkMemory = cblk;
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700172 // TODO: Using unsecurePointer() has some associated security pitfalls
173 // (see declaration for details).
174 // Either document why it is safe in this case or address the
175 // issue (e.g. by copying).
176 mCblk = static_cast<effect_param_cblk_t*>(cblk->unsecurePointer());
Eric Laurent801a1182010-06-09 00:17:29 -0700177 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
178 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
179
Marco Nelissen06b46062014-11-14 07:58:25 -0800180 IInterface::asBinder(iEffect)->linkToDeath(mIEffectClient);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700181 ALOGV("set() %p OK effect: %s id: %d status %d enabled %d pid %d", this, mDescriptor.name, mId,
Svet Ganov33761132021-05-13 22:51:08 +0000182 mStatus, mEnabled, mClientAttributionSource.pid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700183
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800184 if (!audio_is_global_session(mSessionId)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700185 AudioSystem::acquireAudioSessionId(mSessionId, pid, uid);
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700186 }
Eric Laurent801a1182010-06-09 00:17:29 -0700187
188 return mStatus;
189}
190
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700191status_t AudioEffect::set(const char *typeStr,
192 const char *uuidStr,
193 int32_t priority,
194 effect_callback_t cbf,
195 void* user,
196 audio_session_t sessionId,
197 audio_io_handle_t io,
198 const AudioDeviceTypeAddr& device,
Eric Laurentde8caf42021-08-11 17:19:25 +0200199 bool probe,
200 bool notifyFramesProcessed)
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700201{
202 effect_uuid_t type;
203 effect_uuid_t *pType = nullptr;
204 effect_uuid_t uuid;
205 effect_uuid_t *pUuid = nullptr;
206
207 ALOGV("AudioEffect::set string\n - type: %s\n - uuid: %s",
208 typeStr ? typeStr : "nullptr", uuidStr ? uuidStr : "nullptr");
209
210 if (stringToGuid(typeStr, &type) == NO_ERROR) {
211 pType = &type;
212 }
213 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
214 pUuid = &uuid;
215 }
216
Eric Laurentde8caf42021-08-11 17:19:25 +0200217 return set(pType, pUuid, priority, cbf, user, sessionId, io,
218 device, probe, notifyFramesProcessed);
Mikhail Naganov416fffe2020-07-31 17:36:08 -0700219}
220
Eric Laurent801a1182010-06-09 00:17:29 -0700221
222AudioEffect::~AudioEffect()
223{
Steve Block3856b092011-10-20 11:56:00 +0100224 ALOGV("Destructor %p", this);
Eric Laurent801a1182010-06-09 00:17:29 -0700225
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700226 if (!mProbe && (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS)) {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800227 if (!audio_is_global_session(mSessionId)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700228 AudioSystem::releaseAudioSessionId(mSessionId,
Svet Ganov33761132021-05-13 22:51:08 +0000229 VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mClientAttributionSource.pid)));
Jean-Michel Trivia0fd9ca2014-09-18 14:07:18 -0700230 }
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000231 if (mIEffect != nullptr) {
Eric Laurent801a1182010-06-09 00:17:29 -0700232 mIEffect->disconnect();
Marco Nelissen06b46062014-11-14 07:58:25 -0800233 IInterface::asBinder(mIEffect)->unlinkToDeath(mIEffectClient);
Eric Laurent801a1182010-06-09 00:17:29 -0700234 }
Eric Laurenteecd7652015-06-04 16:20:16 -0700235 mIEffect.clear();
236 mCblkMemory.clear();
Eric Laurent801a1182010-06-09 00:17:29 -0700237 }
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700238 mIEffectClient.clear();
239 IPCThreadState::self()->flushCommands();
Eric Laurent801a1182010-06-09 00:17:29 -0700240}
241
242
243status_t AudioEffect::initCheck() const
244{
245 return mStatus;
246}
247
248// -------------------------------------------------------------------------
249
250effect_descriptor_t AudioEffect::descriptor() const
251{
252 return mDescriptor;
253}
254
Eric Laurentda7581b2010-07-02 08:12:41 -0700255bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700256{
257 return (mEnabled != 0);
258}
259
Eric Laurentda7581b2010-07-02 08:12:41 -0700260status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700261{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700262 if (mProbe) {
263 return INVALID_OPERATION;
264 }
Eric Laurent801a1182010-06-09 00:17:29 -0700265 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800266 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700267 }
Eric Laurent801a1182010-06-09 00:17:29 -0700268
Eric Laurentf5aafb22010-11-18 08:40:16 -0800269 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800270 AutoMutex lock(mLock);
271 if (enabled != mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700272 Status bs;
273
Eric Laurentf5aafb22010-11-18 08:40:16 -0800274 if (enabled) {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("enable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700276 bs = mIEffect->enable(&status);
Eric Laurentf5aafb22010-11-18 08:40:16 -0800277 } else {
Steve Block3856b092011-10-20 11:56:00 +0100278 ALOGV("disable %p", this);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700279 bs = mIEffect->disable(&status);
280 }
281 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800282 status = statusTFromBinderStatus(bs);
Eric Laurentda7581b2010-07-02 08:12:41 -0700283 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800284 if (status == NO_ERROR) {
285 mEnabled = enabled;
Eric Laurentda7581b2010-07-02 08:12:41 -0700286 }
Eric Laurent801a1182010-06-09 00:17:29 -0700287 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800288 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700289}
290
Eric Laurent25f43952010-07-28 05:40:18 -0700291status_t AudioEffect::command(uint32_t cmdCode,
292 uint32_t cmdSize,
293 void *cmdData,
294 uint32_t *replySize,
295 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700296{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700297 if (mProbe) {
298 return INVALID_OPERATION;
299 }
Eric Laurent801a1182010-06-09 00:17:29 -0700300 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
Steve Block3856b092011-10-20 11:56:00 +0100301 ALOGV("command() bad status %d", mStatus);
John Grossmanaf7d8182012-01-11 12:23:42 -0800302 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700303 }
304
Eric Laurentf5aafb22010-11-18 08:40:16 -0800305 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
306 if (mEnabled == (cmdCode == EFFECT_CMD_ENABLE)) {
307 return NO_ERROR;
308 }
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000309 if (replySize == nullptr || *replySize != sizeof(status_t) || replyData == nullptr) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800310 return BAD_VALUE;
311 }
312 mLock.lock();
Eric Laurent0fa449c2010-09-24 11:52:04 -0700313 }
314
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700315 std::vector<uint8_t> data;
316 appendToBuffer(cmdData, cmdSize, &data);
317
318 status_t status;
319 std::vector<uint8_t> response;
320
321 Status bs = mIEffect->command(cmdCode, data, *replySize, &response, &status);
322 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800323 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700324 }
325 if (status == NO_ERROR) {
326 memcpy(replyData, response.data(), response.size());
327 *replySize = response.size();
328 }
Eric Laurent0fa449c2010-09-24 11:52:04 -0700329
330 if (cmdCode == EFFECT_CMD_ENABLE || cmdCode == EFFECT_CMD_DISABLE) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800331 if (status == NO_ERROR) {
332 status = *(status_t *)replyData;
Eric Laurent0fa449c2010-09-24 11:52:04 -0700333 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800334 if (status == NO_ERROR) {
335 mEnabled = (cmdCode == EFFECT_CMD_ENABLE);
Eric Laurent0fa449c2010-09-24 11:52:04 -0700336 }
Eric Laurentf5aafb22010-11-18 08:40:16 -0800337 mLock.unlock();
Eric Laurent8569f0d2010-07-29 23:43:43 -0700338 }
339
Eric Laurent8569f0d2010-07-29 23:43:43 -0700340 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700341}
342
Eric Laurent801a1182010-06-09 00:17:29 -0700343status_t AudioEffect::setParameter(effect_param_t *param)
344{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700345 if (mProbe) {
346 return INVALID_OPERATION;
347 }
Eric Laurent801a1182010-06-09 00:17:29 -0700348 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800349 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700350 }
351
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000352 if (param == nullptr || param->psize == 0 || param->vsize == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700353 return BAD_VALUE;
354 }
355
Eric Laurent25f43952010-07-28 05:40:18 -0700356 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700357
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700358 ALOGV("setParameter: param: %d, param2: %d", *(int *)param->data,
359 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700360
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700361 std::vector<uint8_t> cmd;
362 appendToBuffer(param, sizeof(effect_param_t) + psize, &cmd);
363 std::vector<uint8_t> response;
364 status_t status;
365 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM,
366 cmd,
367 sizeof(int),
368 &response,
369 &status);
370 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800371 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700372 return status;
373 }
374 assert(response.size() == sizeof(int));
375 memcpy(&param->status, response.data(), response.size());
376 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700377}
378
379status_t AudioEffect::setParameterDeferred(effect_param_t *param)
380{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700381 if (mProbe) {
382 return INVALID_OPERATION;
383 }
Eric Laurent801a1182010-06-09 00:17:29 -0700384 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800385 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700386 }
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000387 if (param == nullptr || param->psize == 0 || param->vsize == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700388 return BAD_VALUE;
389 }
390
391 Mutex::Autolock _l(mCblk->lock);
392
393 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
394 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
395
396 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
397 return NO_MEMORY;
398 }
399 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
400 *p++ = size;
401 memcpy(p, param, sizeof(effect_param_t) + psize);
402 mCblk->clientIndex += size;
403
404 return NO_ERROR;
405}
406
407status_t AudioEffect::setParameterCommit()
408{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700409 if (mProbe) {
410 return INVALID_OPERATION;
411 }
Eric Laurent801a1182010-06-09 00:17:29 -0700412 if (mStatus != NO_ERROR) {
Glenn Kastenf063b492012-02-17 16:24:10 -0800413 return (mStatus == ALREADY_EXISTS) ? (status_t) INVALID_OPERATION : mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700414 }
415
416 Mutex::Autolock _l(mCblk->lock);
417 if (mCblk->clientIndex == 0) {
418 return INVALID_OPERATION;
419 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700420 std::vector<uint8_t> cmd;
421 std::vector<uint8_t> response;
422 status_t status;
423 Status bs = mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT,
424 cmd,
425 0,
426 &response,
427 &status);
428 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800429 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700430 }
431 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700432}
433
434status_t AudioEffect::getParameter(effect_param_t *param)
435{
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700436 if (mProbe) {
437 return INVALID_OPERATION;
438 }
Eric Laurent801a1182010-06-09 00:17:29 -0700439 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
John Grossmanaf7d8182012-01-11 12:23:42 -0800440 return mStatus;
Eric Laurent801a1182010-06-09 00:17:29 -0700441 }
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000442 if (param == nullptr || param->psize == 0 || param->vsize == 0) {
Eric Laurent801a1182010-06-09 00:17:29 -0700443 return BAD_VALUE;
444 }
445
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700446 ALOGV("getParameter: param: %d, param2: %d", *(int *)param->data,
447 (param->psize == 8) ? *((int *)param->data + 1): -1);
Eric Laurent801a1182010-06-09 00:17:29 -0700448
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700449 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
450 param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700451
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700452 status_t status;
453 std::vector<uint8_t> cmd;
454 std::vector<uint8_t> response;
455 appendToBuffer(param, sizeof(effect_param_t) + param->psize, &cmd);
456
457 Status bs = mIEffect->command(EFFECT_CMD_GET_PARAM, cmd, psize, &response, &status);
458 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -0800459 status = statusTFromBinderStatus(bs);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700460 return status;
461 }
462 memcpy(param, response.data(), response.size());
463 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700464}
465
466
467// -------------------------------------------------------------------------
468
469void AudioEffect::binderDied()
470{
Steve Block5ff1dd52012-01-05 23:22:43 +0000471 ALOGW("IEffect died");
John Grossmanaf7d8182012-01-11 12:23:42 -0800472 mStatus = DEAD_OBJECT;
Glenn Kastena0d68332012-01-27 16:47:15 -0800473 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700474 status_t status = DEAD_OBJECT;
475 mCbf(EVENT_ERROR, mUserData, &status);
476 }
477 mIEffect.clear();
478}
479
480// -------------------------------------------------------------------------
481
482void AudioEffect::controlStatusChanged(bool controlGranted)
483{
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700484 ALOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf,
485 mUserData);
Eric Laurent801a1182010-06-09 00:17:29 -0700486 if (controlGranted) {
487 if (mStatus == ALREADY_EXISTS) {
488 mStatus = NO_ERROR;
489 }
490 } else {
491 if (mStatus == NO_ERROR) {
492 mStatus = ALREADY_EXISTS;
493 }
494 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800495 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700496 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
497 }
498}
499
500void AudioEffect::enableStatusChanged(bool enabled)
501{
Steve Block3856b092011-10-20 11:56:00 +0100502 ALOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700503 if (mStatus == ALREADY_EXISTS) {
Eric Laurentf5aafb22010-11-18 08:40:16 -0800504 mEnabled = enabled;
Glenn Kastena0d68332012-01-27 16:47:15 -0800505 if (mCbf != NULL) {
Eric Laurent801a1182010-06-09 00:17:29 -0700506 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
507 }
508 }
509}
510
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700511void AudioEffect::commandExecuted(int32_t cmdCode,
512 const std::vector<uint8_t>& cmdData,
513 const std::vector<uint8_t>& replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700514{
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700515 if (cmdData.empty() || replyData.empty()) {
Eric Laurent801a1182010-06-09 00:17:29 -0700516 return;
517 }
518
Glenn Kastena0d68332012-01-27 16:47:15 -0800519 if (mCbf != NULL && cmdCode == EFFECT_CMD_SET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700520 std::vector<uint8_t> cmdDataCopy(cmdData);
521 effect_param_t* cmd = reinterpret_cast<effect_param_t *>(cmdDataCopy.data());
522 cmd->status = *reinterpret_cast<const int32_t *>(replyData.data());
Eric Laurent801a1182010-06-09 00:17:29 -0700523 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
524 }
525}
526
Eric Laurentde8caf42021-08-11 17:19:25 +0200527void AudioEffect::framesProcessed(int32_t frames)
528{
529 if (mCbf != NULL) {
530 mCbf(EVENT_FRAMES_PROCESSED, mUserData, &frames);
531 }
532}
533
Eric Laurent801a1182010-06-09 00:17:29 -0700534// -------------------------------------------------------------------------
535
Eric Laurent801a1182010-06-09 00:17:29 -0700536status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
537{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000538 if (numEffects == nullptr) {
539 return BAD_VALUE;
540 }
Eric Laurent801a1182010-06-09 00:17:29 -0700541 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
542 if (af == 0) return PERMISSION_DENIED;
543 return af->queryNumberEffects(numEffects);
544}
545
Eric Laurentffe9c252010-06-23 17:38:20 -0700546status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700547{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000548 if (descriptor == nullptr) {
549 return BAD_VALUE;
550 }
Eric Laurent801a1182010-06-09 00:17:29 -0700551 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
552 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700553 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700554}
555
Glenn Kasten5e92a782012-01-30 07:40:52 -0800556status_t AudioEffect::getEffectDescriptor(const effect_uuid_t *uuid,
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700557 const effect_uuid_t *type,
558 uint32_t preferredTypeFlag,
559 effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700560{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000561 if (uuid == nullptr || type == nullptr || descriptor == nullptr) {
562 return BAD_VALUE;
563 }
Eric Laurent801a1182010-06-09 00:17:29 -0700564 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
565 if (af == 0) return PERMISSION_DENIED;
Ari Hausman-Cohen2046ec72018-04-24 14:00:55 -0700566 return af->getEffectDescriptor(uuid, type, preferredTypeFlag, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700567}
568
Glenn Kastend848eb42016-03-08 13:42:11 -0800569status_t AudioEffect::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700570 effect_descriptor_t *descriptors,
571 uint32_t *count)
572{
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800573 if (descriptors == nullptr || count == nullptr) {
574 return BAD_VALUE;
575 }
Eric Laurent57dae992011-07-24 13:36:09 -0700576 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
577 if (aps == 0) return PERMISSION_DENIED;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800578
579 int32_t audioSessionAidl = VALUE_OR_RETURN_STATUS(
580 legacy2aidl_audio_session_t_int32_t(audioSession));
581 media::Int countAidl;
582 countAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*count));
583 std::vector<media::EffectDescriptor> retAidl;
584 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
585 aps->queryDefaultPreProcessing(audioSessionAidl, &countAidl, &retAidl)));
586 *count = VALUE_OR_RETURN_STATUS(convertIntegral<uint32_t>(countAidl.value));
587 RETURN_STATUS_IF_ERROR(convertRange(retAidl.begin(), retAidl.end(), descriptors,
588 aidl2legacy_EffectDescriptor_effect_descriptor_t));
589 return OK;
Eric Laurent57dae992011-07-24 13:36:09 -0700590}
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700591
592status_t AudioEffect::newEffectUniqueId(audio_unique_id_t* id)
593{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000594 if (id == nullptr) {
595 return BAD_VALUE;
596 }
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700597 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
598 if (af == 0) return PERMISSION_DENIED;
599 *id = af->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
600 return NO_ERROR;
601}
602
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700603status_t AudioEffect::addSourceDefaultEffect(const char *typeStr,
604 const String16& opPackageName,
605 const char *uuidStr,
606 int32_t priority,
607 audio_source_t source,
608 audio_unique_id_t *id)
609{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000610 if ((typeStr == nullptr && uuidStr == nullptr) || id == nullptr) {
611 return BAD_VALUE;
612 }
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700613 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
614 if (aps == 0) return PERMISSION_DENIED;
615
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700616 // Convert type & uuid from string to effect_uuid_t.
617 effect_uuid_t type;
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000618 if (typeStr != nullptr) {
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700619 status_t res = stringToGuid(typeStr, &type);
620 if (res != OK) return res;
621 } else {
622 type = *EFFECT_UUID_NULL;
623 }
624
625 effect_uuid_t uuid;
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000626 if (uuidStr != nullptr) {
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700627 status_t res = stringToGuid(uuidStr, &uuid);
628 if (res != OK) return res;
629 } else {
630 uuid = *EFFECT_UUID_NULL;
631 }
632
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800633 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
634 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
635 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
636 legacy2aidl_String16_string(opPackageName));
637 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
638 legacy2aidl_audio_source_t_AudioSourceType(source));
639 int32_t retAidl;
640 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
641 aps->addSourceDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, sourceAidl,
642 &retAidl)));
643 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
644 return OK;
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700645}
646
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700647status_t AudioEffect::addStreamDefaultEffect(const char *typeStr,
648 const String16& opPackageName,
649 const char *uuidStr,
650 int32_t priority,
651 audio_usage_t usage,
652 audio_unique_id_t *id)
653{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000654 if ((typeStr == nullptr && uuidStr == nullptr) || id == nullptr) {
655 return BAD_VALUE;
656 }
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700657 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
658 if (aps == 0) return PERMISSION_DENIED;
659
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700660 // Convert type & uuid from string to effect_uuid_t.
661 effect_uuid_t type;
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000662 if (typeStr != nullptr) {
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700663 status_t res = stringToGuid(typeStr, &type);
664 if (res != OK) return res;
665 } else {
666 type = *EFFECT_UUID_NULL;
667 }
668
669 effect_uuid_t uuid;
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000670 if (uuidStr != nullptr) {
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700671 status_t res = stringToGuid(uuidStr, &uuid);
672 if (res != OK) return res;
673 } else {
674 uuid = *EFFECT_UUID_NULL;
675 }
676
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800677 media::AudioUuid typeAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(type));
678 media::AudioUuid uuidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_uuid_t_AudioUuid(uuid));
679 std::string opPackageNameAidl = VALUE_OR_RETURN_STATUS(
680 legacy2aidl_String16_string(opPackageName));
681 media::AudioUsage usageAidl = VALUE_OR_RETURN_STATUS(
682 legacy2aidl_audio_usage_t_AudioUsage(usage));
683 int32_t retAidl;
684 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
685 aps->addStreamDefaultEffect(typeAidl, opPackageNameAidl, uuidAidl, priority, usageAidl,
686 &retAidl)));
687 *id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_unique_id_t(retAidl));
688 return OK;
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700689}
690
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700691status_t AudioEffect::removeSourceDefaultEffect(audio_unique_id_t id)
692{
693 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
694 if (aps == 0) return PERMISSION_DENIED;
695
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800696 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
697 return statusTFromBinderStatus(aps->removeSourceDefaultEffect(idAidl));
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700698}
699
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700700status_t AudioEffect::removeStreamDefaultEffect(audio_unique_id_t id)
701{
702 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
703 if (aps == 0) return PERMISSION_DENIED;
704
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800705 int32_t idAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(id));
706 return statusTFromBinderStatus(aps->removeStreamDefaultEffect(idAidl));
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700707}
708
Eric Laurent801a1182010-06-09 00:17:29 -0700709// -------------------------------------------------------------------------
710
711status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
712{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000713 if (str == nullptr || guid == nullptr) {
Eric Laurent801a1182010-06-09 00:17:29 -0700714 return BAD_VALUE;
715 }
716
717 int tmp[10];
718
719 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
720 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
721 return BAD_VALUE;
722 }
723 guid->timeLow = (uint32_t)tmp[0];
724 guid->timeMid = (uint16_t)tmp[1];
725 guid->timeHiAndVersion = (uint16_t)tmp[2];
726 guid->clockSeq = (uint16_t)tmp[3];
727 guid->node[0] = (uint8_t)tmp[4];
728 guid->node[1] = (uint8_t)tmp[5];
729 guid->node[2] = (uint8_t)tmp[6];
730 guid->node[3] = (uint8_t)tmp[7];
731 guid->node[4] = (uint8_t)tmp[8];
732 guid->node[5] = (uint8_t)tmp[9];
733
734 return NO_ERROR;
735}
736
737status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
738{
Mikhail Naganov097e3b92022-04-27 22:30:22 +0000739 if (guid == nullptr || str == nullptr) {
Eric Laurent801a1182010-06-09 00:17:29 -0700740 return BAD_VALUE;
741 }
742
743 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
744 guid->timeLow,
745 guid->timeMid,
746 guid->timeHiAndVersion,
747 guid->clockSeq,
748 guid->node[0],
749 guid->node[1],
750 guid->node[2],
751 guid->node[3],
752 guid->node[4],
753 guid->node[5]);
754
755 return NO_ERROR;
756}
757
758
Glenn Kasten40bc9062015-03-20 09:09:33 -0700759} // namespace android