blob: b3d564aaf3a21c13118ff95b8c0735c7214d7653 [file] [log] [blame]
bryant_liuba2b4392014-06-11 16:49:30 +08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyEffects"
Eric Laurent897a4082014-07-11 16:51:53 -070018//#define LOG_NDEBUG 0
bryant_liuba2b4392014-06-11 16:49:30 +080019
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
Kevin Rocard4fb615c2017-06-26 10:28:13 -070023#include <memory>
bryant_liuba2b4392014-06-11 16:49:30 +080024#include <cutils/misc.h>
25#include <media/AudioEffect.h>
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070026#include <media/AudioPolicyHelper.h>
Kevin Rocard4fb615c2017-06-26 10:28:13 -070027#include <media/EffectsConfig.h>
Andy Hungab7ef302018-05-15 19:35:29 -070028#include <mediautils/ServiceUtilities.h>
bryant_liuba2b4392014-06-11 16:49:30 +080029#include <system/audio.h>
Mikhail Naganovc2f710f2016-10-17 18:05:36 -070030#include <system/audio_effects/audio_effects_conf.h>
bryant_liuba2b4392014-06-11 16:49:30 +080031#include <utils/Vector.h>
32#include <utils/SortedVector.h>
33#include <cutils/config_utils.h>
Eric Laurentb6436272016-12-07 19:24:50 -080034#include <binder/IPCThreadState.h>
bryant_liuba2b4392014-06-11 16:49:30 +080035#include "AudioPolicyEffects.h"
bryant_liuba2b4392014-06-11 16:49:30 +080036
37namespace android {
38
39// ----------------------------------------------------------------------------
40// AudioPolicyEffects Implementation
41// ----------------------------------------------------------------------------
42
43AudioPolicyEffects::AudioPolicyEffects()
44{
Kevin Rocard4fb615c2017-06-26 10:28:13 -070045 status_t loadResult = loadAudioEffectXmlConfig();
46 if (loadResult < 0) {
47 ALOGW("Failed to load XML effect configuration, fallback to .conf");
48 // load automatic audio effect modules
49 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
50 loadAudioEffectConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
51 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
52 loadAudioEffectConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
53 }
54 } else if (loadResult > 0) {
55 ALOGE("Effect config is partially invalid, skipped %d elements", loadResult);
bryant_liuba2b4392014-06-11 16:49:30 +080056 }
57}
58
59
60AudioPolicyEffects::~AudioPolicyEffects()
61{
62 size_t i = 0;
63 // release audio input processing resources
64 for (i = 0; i < mInputSources.size(); i++) {
65 delete mInputSources.valueAt(i);
66 }
67 mInputSources.clear();
68
Eric Laurentfb66dd92016-01-28 18:32:03 -080069 for (i = 0; i < mInputSessions.size(); i++) {
70 mInputSessions.valueAt(i)->mEffects.clear();
71 delete mInputSessions.valueAt(i);
bryant_liuba2b4392014-06-11 16:49:30 +080072 }
Eric Laurentfb66dd92016-01-28 18:32:03 -080073 mInputSessions.clear();
bryant_liuba2b4392014-06-11 16:49:30 +080074
75 // release audio output processing resources
76 for (i = 0; i < mOutputStreams.size(); i++) {
77 delete mOutputStreams.valueAt(i);
78 }
79 mOutputStreams.clear();
80
81 for (i = 0; i < mOutputSessions.size(); i++) {
82 mOutputSessions.valueAt(i)->mEffects.clear();
83 delete mOutputSessions.valueAt(i);
84 }
85 mOutputSessions.clear();
86}
87
88
89status_t AudioPolicyEffects::addInputEffects(audio_io_handle_t input,
90 audio_source_t inputSource,
Eric Laurentfb66dd92016-01-28 18:32:03 -080091 audio_session_t audioSession)
bryant_liuba2b4392014-06-11 16:49:30 +080092{
93 status_t status = NO_ERROR;
94
95 // create audio pre processors according to input source
96 audio_source_t aliasSource = (inputSource == AUDIO_SOURCE_HOTWORD) ?
97 AUDIO_SOURCE_VOICE_RECOGNITION : inputSource;
98
Eric Laurent8b1e80b2014-10-07 09:08:47 -070099 Mutex::Autolock _l(mLock);
bryant_liuba2b4392014-06-11 16:49:30 +0800100 ssize_t index = mInputSources.indexOfKey(aliasSource);
101 if (index < 0) {
102 ALOGV("addInputEffects(): no processing needs to be attached to this source");
103 return status;
104 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800105 ssize_t idx = mInputSessions.indexOfKey(audioSession);
106 EffectVector *sessionDesc;
bryant_liuba2b4392014-06-11 16:49:30 +0800107 if (idx < 0) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800108 sessionDesc = new EffectVector(audioSession);
109 mInputSessions.add(audioSession, sessionDesc);
bryant_liuba2b4392014-06-11 16:49:30 +0800110 } else {
bryant_liu890a5632014-08-20 18:06:13 +0800111 // EffectVector is existing and we just need to increase ref count
Eric Laurentfb66dd92016-01-28 18:32:03 -0800112 sessionDesc = mInputSessions.valueAt(idx);
bryant_liuba2b4392014-06-11 16:49:30 +0800113 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800114 sessionDesc->mRefCount++;
bryant_liu890a5632014-08-20 18:06:13 +0800115
Eric Laurentfb66dd92016-01-28 18:32:03 -0800116 ALOGV("addInputEffects(): input: %d, refCount: %d", input, sessionDesc->mRefCount);
117 if (sessionDesc->mRefCount == 1) {
Eric Laurentb6436272016-12-07 19:24:50 -0800118 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent7de5ac12014-10-21 09:07:11 -0700119 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
120 for (size_t i = 0; i < effects.size(); i++) {
121 EffectDesc *effect = effects[i];
Svet Ganovbe71aa22015-04-28 12:06:02 -0700122 sp<AudioEffect> fx = new AudioEffect(NULL, String16("android"), &effect->mUuid, -1, 0,
123 0, audioSession, input);
Eric Laurent7de5ac12014-10-21 09:07:11 -0700124 status_t status = fx->initCheck();
125 if (status != NO_ERROR && status != ALREADY_EXISTS) {
126 ALOGW("addInputEffects(): failed to create Fx %s on source %d",
127 effect->mName, (int32_t)aliasSource);
128 // fx goes out of scope and strong ref on AudioEffect is released
129 continue;
130 }
131 for (size_t j = 0; j < effect->mParams.size(); j++) {
132 fx->setParameter(effect->mParams[j]);
133 }
134 ALOGV("addInputEffects(): added Fx %s on source: %d",
bryant_liuba2b4392014-06-11 16:49:30 +0800135 effect->mName, (int32_t)aliasSource);
Eric Laurentfb66dd92016-01-28 18:32:03 -0800136 sessionDesc->mEffects.add(fx);
bryant_liuba2b4392014-06-11 16:49:30 +0800137 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800138 sessionDesc->setProcessorEnabled(true);
Eric Laurentb6436272016-12-07 19:24:50 -0800139 IPCThreadState::self()->restoreCallingIdentity(token);
bryant_liuba2b4392014-06-11 16:49:30 +0800140 }
bryant_liuba2b4392014-06-11 16:49:30 +0800141 return status;
142}
143
144
Eric Laurentfb66dd92016-01-28 18:32:03 -0800145status_t AudioPolicyEffects::releaseInputEffects(audio_io_handle_t input,
146 audio_session_t audioSession)
bryant_liuba2b4392014-06-11 16:49:30 +0800147{
148 status_t status = NO_ERROR;
149
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700150 Mutex::Autolock _l(mLock);
Eric Laurentfb66dd92016-01-28 18:32:03 -0800151 ssize_t index = mInputSessions.indexOfKey(audioSession);
bryant_liuba2b4392014-06-11 16:49:30 +0800152 if (index < 0) {
153 return status;
154 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800155 EffectVector *sessionDesc = mInputSessions.valueAt(index);
156 sessionDesc->mRefCount--;
157 ALOGV("releaseInputEffects(): input: %d, refCount: %d", input, sessionDesc->mRefCount);
158 if (sessionDesc->mRefCount == 0) {
159 sessionDesc->setProcessorEnabled(false);
160 delete sessionDesc;
161 mInputSessions.removeItemsAt(index);
bryant_liu890a5632014-08-20 18:06:13 +0800162 ALOGV("releaseInputEffects(): all effects released");
163 }
bryant_liuba2b4392014-06-11 16:49:30 +0800164 return status;
165}
166
Eric Laurentfb66dd92016-01-28 18:32:03 -0800167status_t AudioPolicyEffects::queryDefaultInputEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +0800168 effect_descriptor_t *descriptors,
169 uint32_t *count)
170{
171 status_t status = NO_ERROR;
172
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700173 Mutex::Autolock _l(mLock);
bryant_liuba2b4392014-06-11 16:49:30 +0800174 size_t index;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800175 for (index = 0; index < mInputSessions.size(); index++) {
176 if (mInputSessions.valueAt(index)->mSessionId == audioSession) {
bryant_liuba2b4392014-06-11 16:49:30 +0800177 break;
178 }
179 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800180 if (index == mInputSessions.size()) {
bryant_liuba2b4392014-06-11 16:49:30 +0800181 *count = 0;
182 return BAD_VALUE;
183 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800184 Vector< sp<AudioEffect> > effects = mInputSessions.valueAt(index)->mEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800185
186 for (size_t i = 0; i < effects.size(); i++) {
187 effect_descriptor_t desc = effects[i]->descriptor();
188 if (i < *count) {
189 descriptors[i] = desc;
190 }
191 }
192 if (effects.size() > *count) {
193 status = NO_MEMORY;
194 }
195 *count = effects.size();
196 return status;
197}
198
199
Eric Laurentfb66dd92016-01-28 18:32:03 -0800200status_t AudioPolicyEffects::queryDefaultOutputSessionEffects(audio_session_t audioSession,
bryant_liuba2b4392014-06-11 16:49:30 +0800201 effect_descriptor_t *descriptors,
202 uint32_t *count)
203{
204 status_t status = NO_ERROR;
205
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700206 Mutex::Autolock _l(mLock);
bryant_liuba2b4392014-06-11 16:49:30 +0800207 size_t index;
208 for (index = 0; index < mOutputSessions.size(); index++) {
209 if (mOutputSessions.valueAt(index)->mSessionId == audioSession) {
210 break;
211 }
212 }
213 if (index == mOutputSessions.size()) {
214 *count = 0;
215 return BAD_VALUE;
216 }
217 Vector< sp<AudioEffect> > effects = mOutputSessions.valueAt(index)->mEffects;
218
219 for (size_t i = 0; i < effects.size(); i++) {
220 effect_descriptor_t desc = effects[i]->descriptor();
221 if (i < *count) {
222 descriptors[i] = desc;
223 }
224 }
225 if (effects.size() > *count) {
226 status = NO_MEMORY;
227 }
228 *count = effects.size();
229 return status;
230}
231
232
233status_t AudioPolicyEffects::addOutputSessionEffects(audio_io_handle_t output,
234 audio_stream_type_t stream,
Eric Laurentfb66dd92016-01-28 18:32:03 -0800235 audio_session_t audioSession)
bryant_liuba2b4392014-06-11 16:49:30 +0800236{
237 status_t status = NO_ERROR;
238
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700239 Mutex::Autolock _l(mLock);
bryant_liuba2b4392014-06-11 16:49:30 +0800240 // create audio processors according to stream
Eric Laurent223fd5c2014-11-11 13:43:36 -0800241 // FIXME: should we have specific post processing settings for internal streams?
242 // default to media for now.
243 if (stream >= AUDIO_STREAM_PUBLIC_CNT) {
244 stream = AUDIO_STREAM_MUSIC;
245 }
bryant_liuba2b4392014-06-11 16:49:30 +0800246 ssize_t index = mOutputStreams.indexOfKey(stream);
247 if (index < 0) {
248 ALOGV("addOutputSessionEffects(): no output processing needed for this stream");
249 return NO_ERROR;
250 }
251
252 ssize_t idx = mOutputSessions.indexOfKey(audioSession);
253 EffectVector *procDesc;
254 if (idx < 0) {
255 procDesc = new EffectVector(audioSession);
256 mOutputSessions.add(audioSession, procDesc);
257 } else {
bryant_liu890a5632014-08-20 18:06:13 +0800258 // EffectVector is existing and we just need to increase ref count
bryant_liuba2b4392014-06-11 16:49:30 +0800259 procDesc = mOutputSessions.valueAt(idx);
260 }
bryant_liu890a5632014-08-20 18:06:13 +0800261 procDesc->mRefCount++;
262
Eric Laurent7de5ac12014-10-21 09:07:11 -0700263 ALOGV("addOutputSessionEffects(): session: %d, refCount: %d",
264 audioSession, procDesc->mRefCount);
265 if (procDesc->mRefCount == 1) {
Eric Laurentb6436272016-12-07 19:24:50 -0800266 // make sure effects are associated to audio server even if we are executing a binder call
267 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent7de5ac12014-10-21 09:07:11 -0700268 Vector <EffectDesc *> effects = mOutputStreams.valueAt(index)->mEffects;
269 for (size_t i = 0; i < effects.size(); i++) {
270 EffectDesc *effect = effects[i];
Svet Ganovbe71aa22015-04-28 12:06:02 -0700271 sp<AudioEffect> fx = new AudioEffect(NULL, String16("android"), &effect->mUuid, 0, 0, 0,
Eric Laurent7de5ac12014-10-21 09:07:11 -0700272 audioSession, output);
273 status_t status = fx->initCheck();
274 if (status != NO_ERROR && status != ALREADY_EXISTS) {
275 ALOGE("addOutputSessionEffects(): failed to create Fx %s on session %d",
276 effect->mName, audioSession);
277 // fx goes out of scope and strong ref on AudioEffect is released
278 continue;
279 }
280 ALOGV("addOutputSessionEffects(): added Fx %s on session: %d for stream: %d",
281 effect->mName, audioSession, (int32_t)stream);
282 procDesc->mEffects.add(fx);
bryant_liuba2b4392014-06-11 16:49:30 +0800283 }
Eric Laurent7de5ac12014-10-21 09:07:11 -0700284
285 procDesc->setProcessorEnabled(true);
Eric Laurentb6436272016-12-07 19:24:50 -0800286 IPCThreadState::self()->restoreCallingIdentity(token);
bryant_liuba2b4392014-06-11 16:49:30 +0800287 }
bryant_liuba2b4392014-06-11 16:49:30 +0800288 return status;
289}
290
291status_t AudioPolicyEffects::releaseOutputSessionEffects(audio_io_handle_t output,
292 audio_stream_type_t stream,
Eric Laurentfb66dd92016-01-28 18:32:03 -0800293 audio_session_t audioSession)
bryant_liuba2b4392014-06-11 16:49:30 +0800294{
295 status_t status = NO_ERROR;
296 (void) output; // argument not used for now
297 (void) stream; // argument not used for now
298
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700299 Mutex::Autolock _l(mLock);
bryant_liuba2b4392014-06-11 16:49:30 +0800300 ssize_t index = mOutputSessions.indexOfKey(audioSession);
301 if (index < 0) {
302 ALOGV("releaseOutputSessionEffects: no output processing was attached to this stream");
303 return NO_ERROR;
304 }
305
306 EffectVector *procDesc = mOutputSessions.valueAt(index);
bryant_liu890a5632014-08-20 18:06:13 +0800307 procDesc->mRefCount--;
Eric Laurent7de5ac12014-10-21 09:07:11 -0700308 ALOGV("releaseOutputSessionEffects(): session: %d, refCount: %d",
309 audioSession, procDesc->mRefCount);
bryant_liu890a5632014-08-20 18:06:13 +0800310 if (procDesc->mRefCount == 0) {
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700311 procDesc->setProcessorEnabled(false);
bryant_liu890a5632014-08-20 18:06:13 +0800312 procDesc->mEffects.clear();
313 delete procDesc;
314 mOutputSessions.removeItemsAt(index);
315 ALOGV("releaseOutputSessionEffects(): output processing released from session: %d",
316 audioSession);
317 }
bryant_liuba2b4392014-06-11 16:49:30 +0800318 return status;
319}
320
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700321status_t AudioPolicyEffects::addSourceDefaultEffect(const effect_uuid_t *type,
322 const String16& opPackageName,
323 const effect_uuid_t *uuid,
324 int32_t priority,
325 audio_source_t source,
326 audio_unique_id_t* id)
327{
328 if (uuid == NULL || type == NULL) {
329 ALOGE("addSourceDefaultEffect(): Null uuid or type uuid pointer");
330 return BAD_VALUE;
331 }
332
333 // HOTWORD and FM_TUNER are two special case sources > MAX.
334 if (source < AUDIO_SOURCE_DEFAULT ||
335 (source > AUDIO_SOURCE_MAX &&
336 source != AUDIO_SOURCE_HOTWORD &&
337 source != AUDIO_SOURCE_FM_TUNER)) {
338 ALOGE("addSourceDefaultEffect(): Unsupported source type %d", source);
339 return BAD_VALUE;
340 }
341
342 // Check that |uuid| or |type| corresponds to an effect on the system.
343 effect_descriptor_t descriptor = {};
344 status_t res = AudioEffect::getEffectDescriptor(
345 uuid, type, EFFECT_FLAG_TYPE_PRE_PROC, &descriptor);
346 if (res != OK) {
347 ALOGE("addSourceDefaultEffect(): Failed to find effect descriptor matching uuid/type.");
348 return res;
349 }
350
351 // Only pre-processing effects can be added dynamically as source defaults.
352 if ((descriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) {
353 ALOGE("addSourceDefaultEffect(): Desired effect cannot be attached "
354 "as a source default effect.");
355 return BAD_VALUE;
356 }
357
358 Mutex::Autolock _l(mLock);
359
360 // Find the EffectDescVector for the given source type, or create a new one if necessary.
361 ssize_t index = mInputSources.indexOfKey(source);
362 EffectDescVector *desc = NULL;
363 if (index < 0) {
364 // No effects for this source type yet.
365 desc = new EffectDescVector();
366 mInputSources.add(source, desc);
367 } else {
368 desc = mInputSources.valueAt(index);
369 }
370
371 // Create a new effect and add it to the vector.
372 res = AudioEffect::newEffectUniqueId(id);
373 if (res != OK) {
374 ALOGE("addSourceDefaultEffect(): failed to get new unique id.");
375 return res;
376 }
377 EffectDesc *effect = new EffectDesc(
378 descriptor.name, *type, opPackageName, *uuid, priority, *id);
379 desc->mEffects.add(effect);
380 // TODO(b/71813697): Support setting params as well.
381
382 // TODO(b/71814300): Retroactively attach to any existing sources of the given type.
383 // This requires tracking the source type of each session id in addition to what is
384 // already being tracked.
385
386 return NO_ERROR;
387}
388
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700389status_t AudioPolicyEffects::addStreamDefaultEffect(const effect_uuid_t *type,
390 const String16& opPackageName,
391 const effect_uuid_t *uuid,
392 int32_t priority,
393 audio_usage_t usage,
394 audio_unique_id_t* id)
395{
396 if (uuid == NULL || type == NULL) {
397 ALOGE("addStreamDefaultEffect(): Null uuid or type uuid pointer");
398 return BAD_VALUE;
399 }
400
401 audio_stream_type_t stream = audio_usage_to_stream_type(usage);
402
403 if (stream < AUDIO_STREAM_MIN || stream >= AUDIO_STREAM_PUBLIC_CNT) {
404 ALOGE("addStreamDefaultEffect(): Unsupported stream type %d", stream);
405 return BAD_VALUE;
406 }
407
408 // Check that |uuid| or |type| corresponds to an effect on the system.
409 effect_descriptor_t descriptor = {};
410 status_t res = AudioEffect::getEffectDescriptor(
411 uuid, type, EFFECT_FLAG_TYPE_INSERT, &descriptor);
412 if (res != OK) {
413 ALOGE("addStreamDefaultEffect(): Failed to find effect descriptor matching uuid/type.");
414 return res;
415 }
416
417 // Only insert effects can be added dynamically as stream defaults.
418 if ((descriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_INSERT) {
419 ALOGE("addStreamDefaultEffect(): Desired effect cannot be attached "
420 "as a stream default effect.");
421 return BAD_VALUE;
422 }
423
424 Mutex::Autolock _l(mLock);
425
426 // Find the EffectDescVector for the given stream type, or create a new one if necessary.
427 ssize_t index = mOutputStreams.indexOfKey(stream);
428 EffectDescVector *desc = NULL;
429 if (index < 0) {
430 // No effects for this stream type yet.
431 desc = new EffectDescVector();
432 mOutputStreams.add(stream, desc);
433 } else {
434 desc = mOutputStreams.valueAt(index);
435 }
436
437 // Create a new effect and add it to the vector.
438 res = AudioEffect::newEffectUniqueId(id);
439 if (res != OK) {
440 ALOGE("addStreamDefaultEffect(): failed to get new unique id.");
441 return res;
442 }
443 EffectDesc *effect = new EffectDesc(
444 descriptor.name, *type, opPackageName, *uuid, priority, *id);
445 desc->mEffects.add(effect);
446 // TODO(b/71813697): Support setting params as well.
447
448 // TODO(b/71814300): Retroactively attach to any existing streams of the given type.
449 // This requires tracking the stream type of each session id in addition to what is
450 // already being tracked.
451
452 return NO_ERROR;
453}
454
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700455status_t AudioPolicyEffects::removeSourceDefaultEffect(audio_unique_id_t id)
456{
457 if (id == AUDIO_UNIQUE_ID_ALLOCATE) {
458 // ALLOCATE is not a unique identifier, but rather a reserved value indicating
459 // a real id has not been assigned. For default effects, this value is only used
460 // by system-owned defaults from the loaded config, which cannot be removed.
461 return BAD_VALUE;
462 }
463
464 Mutex::Autolock _l(mLock);
465
466 // Check each source type.
467 size_t numSources = mInputSources.size();
468 for (size_t i = 0; i < numSources; ++i) {
469 // Check each effect for each source.
470 EffectDescVector* descVector = mInputSources[i];
471 for (auto desc = descVector->mEffects.begin(); desc != descVector->mEffects.end(); ++desc) {
472 if ((*desc)->mId == id) {
473 // Found it!
474 // TODO(b/71814300): Remove from any sources the effect was attached to.
475 descVector->mEffects.erase(desc);
476 // Handles are unique; there can only be one match, so return early.
477 return NO_ERROR;
478 }
479 }
480 }
481
482 // Effect wasn't found, so it's been trivially removed successfully.
483 return NO_ERROR;
484}
485
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700486status_t AudioPolicyEffects::removeStreamDefaultEffect(audio_unique_id_t id)
487{
488 if (id == AUDIO_UNIQUE_ID_ALLOCATE) {
489 // ALLOCATE is not a unique identifier, but rather a reserved value indicating
490 // a real id has not been assigned. For default effects, this value is only used
491 // by system-owned defaults from the loaded config, which cannot be removed.
492 return BAD_VALUE;
493 }
494
495 Mutex::Autolock _l(mLock);
496
497 // Check each stream type.
498 size_t numStreams = mOutputStreams.size();
499 for (size_t i = 0; i < numStreams; ++i) {
500 // Check each effect for each stream.
501 EffectDescVector* descVector = mOutputStreams[i];
502 for (auto desc = descVector->mEffects.begin(); desc != descVector->mEffects.end(); ++desc) {
503 if ((*desc)->mId == id) {
504 // Found it!
505 // TODO(b/71814300): Remove from any streams the effect was attached to.
506 descVector->mEffects.erase(desc);
507 // Handles are unique; there can only be one match, so return early.
508 return NO_ERROR;
509 }
510 }
511 }
512
513 // Effect wasn't found, so it's been trivially removed successfully.
514 return NO_ERROR;
515}
bryant_liuba2b4392014-06-11 16:49:30 +0800516
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700517void AudioPolicyEffects::EffectVector::setProcessorEnabled(bool enabled)
bryant_liuba2b4392014-06-11 16:49:30 +0800518{
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700519 for (size_t i = 0; i < mEffects.size(); i++) {
520 mEffects.itemAt(i)->setEnabled(enabled);
bryant_liuba2b4392014-06-11 16:49:30 +0800521 }
522}
523
524
525// ----------------------------------------------------------------------------
526// Audio processing configuration
527// ----------------------------------------------------------------------------
528
529/*static*/ const char * const AudioPolicyEffects::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
530 MIC_SRC_TAG,
531 VOICE_UL_SRC_TAG,
532 VOICE_DL_SRC_TAG,
533 VOICE_CALL_SRC_TAG,
534 CAMCORDER_SRC_TAG,
535 VOICE_REC_SRC_TAG,
rago8a397d52015-12-02 11:27:57 -0800536 VOICE_COMM_SRC_TAG,
537 UNPROCESSED_SRC_TAG
bryant_liuba2b4392014-06-11 16:49:30 +0800538};
539
540// returns the audio_source_t enum corresponding to the input source name or
541// AUDIO_SOURCE_CNT is no match found
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700542/*static*/ audio_source_t AudioPolicyEffects::inputSourceNameToEnum(const char *name)
bryant_liuba2b4392014-06-11 16:49:30 +0800543{
544 int i;
545 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
546 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
547 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
548 break;
549 }
550 }
551 return (audio_source_t)i;
552}
553
Eric Laurent223fd5c2014-11-11 13:43:36 -0800554const char *AudioPolicyEffects::kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1] = {
bryant_liuba2b4392014-06-11 16:49:30 +0800555 AUDIO_STREAM_DEFAULT_TAG,
556 AUDIO_STREAM_VOICE_CALL_TAG,
557 AUDIO_STREAM_SYSTEM_TAG,
558 AUDIO_STREAM_RING_TAG,
559 AUDIO_STREAM_MUSIC_TAG,
560 AUDIO_STREAM_ALARM_TAG,
561 AUDIO_STREAM_NOTIFICATION_TAG,
562 AUDIO_STREAM_BLUETOOTH_SCO_TAG,
563 AUDIO_STREAM_ENFORCED_AUDIBLE_TAG,
564 AUDIO_STREAM_DTMF_TAG,
565 AUDIO_STREAM_TTS_TAG
566};
567
568// returns the audio_stream_t enum corresponding to the output stream name or
Eric Laurent223fd5c2014-11-11 13:43:36 -0800569// AUDIO_STREAM_PUBLIC_CNT is no match found
bryant_liuba2b4392014-06-11 16:49:30 +0800570audio_stream_type_t AudioPolicyEffects::streamNameToEnum(const char *name)
571{
572 int i;
Eric Laurent223fd5c2014-11-11 13:43:36 -0800573 for (i = AUDIO_STREAM_DEFAULT; i < AUDIO_STREAM_PUBLIC_CNT; i++) {
bryant_liuba2b4392014-06-11 16:49:30 +0800574 if (strcmp(name, kStreamNames[i - AUDIO_STREAM_DEFAULT]) == 0) {
575 ALOGV("streamNameToEnum found stream %s %d", name, i);
576 break;
577 }
578 }
579 return (audio_stream_type_t)i;
580}
581
582// ----------------------------------------------------------------------------
583// Audio Effect Config parser
584// ----------------------------------------------------------------------------
585
Eric Laurent138ed172016-02-10 10:40:44 -0800586size_t AudioPolicyEffects::growParamSize(char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800587 size_t size,
588 size_t *curSize,
589 size_t *totSize)
590{
591 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
592 size_t pos = ((*curSize - 1 ) / size + 1) * size;
593
594 if (pos + size > *totSize) {
595 while (pos + size > *totSize) {
596 *totSize += ((*totSize + 7) / 8) * 4;
597 }
George Burgess IV31225682018-02-12 11:08:38 -0800598 char *newParam = (char *)realloc(*param, *totSize);
599 if (newParam == NULL) {
Eric Laurent138ed172016-02-10 10:40:44 -0800600 ALOGE("%s realloc error for size %zu", __func__, *totSize);
601 return 0;
602 }
George Burgess IV31225682018-02-12 11:08:38 -0800603 *param = newParam;
bryant_liuba2b4392014-06-11 16:49:30 +0800604 }
605 *curSize = pos + size;
606 return pos;
607}
608
Eric Laurent138ed172016-02-10 10:40:44 -0800609
bryant_liuba2b4392014-06-11 16:49:30 +0800610size_t AudioPolicyEffects::readParamValue(cnode *node,
Eric Laurent138ed172016-02-10 10:40:44 -0800611 char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800612 size_t *curSize,
613 size_t *totSize)
614{
Eric Laurent138ed172016-02-10 10:40:44 -0800615 size_t len = 0;
616 size_t pos;
617
bryant_liuba2b4392014-06-11 16:49:30 +0800618 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
Eric Laurent138ed172016-02-10 10:40:44 -0800619 pos = growParamSize(param, sizeof(short), curSize, totSize);
620 if (pos == 0) {
621 goto exit;
bryant_liuba2b4392014-06-11 16:49:30 +0800622 }
Eric Laurent138ed172016-02-10 10:40:44 -0800623 *(short *)(*param + pos) = (short)atoi(node->value);
624 ALOGV("readParamValue() reading short %d", *(short *)(*param + pos));
625 len = sizeof(short);
626 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
627 pos = growParamSize(param, sizeof(int), curSize, totSize);
628 if (pos == 0) {
629 goto exit;
630 }
631 *(int *)(*param + pos) = atoi(node->value);
632 ALOGV("readParamValue() reading int %d", *(int *)(*param + pos));
633 len = sizeof(int);
634 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
635 pos = growParamSize(param, sizeof(float), curSize, totSize);
636 if (pos == 0) {
637 goto exit;
638 }
639 *(float *)(*param + pos) = (float)atof(node->value);
640 ALOGV("readParamValue() reading float %f",*(float *)(*param + pos));
641 len = sizeof(float);
642 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
643 pos = growParamSize(param, sizeof(bool), curSize, totSize);
644 if (pos == 0) {
645 goto exit;
646 }
647 if (strncmp(node->value, "true", strlen("true") + 1) == 0) {
648 *(bool *)(*param + pos) = true;
649 } else {
650 *(bool *)(*param + pos) = false;
651 }
652 ALOGV("readParamValue() reading bool %s",
653 *(bool *)(*param + pos) ? "true" : "false");
654 len = sizeof(bool);
bryant_liuba2b4392014-06-11 16:49:30 +0800655 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
Eric Laurent138ed172016-02-10 10:40:44 -0800656 len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
bryant_liuba2b4392014-06-11 16:49:30 +0800657 if (*curSize + len + 1 > *totSize) {
658 *totSize = *curSize + len + 1;
Eric Laurent138ed172016-02-10 10:40:44 -0800659 *param = (char *)realloc(*param, *totSize);
660 if (*param == NULL) {
661 len = 0;
662 ALOGE("%s realloc error for string len %zu", __func__, *totSize);
663 goto exit;
664 }
bryant_liuba2b4392014-06-11 16:49:30 +0800665 }
Eric Laurent138ed172016-02-10 10:40:44 -0800666 strncpy(*param + *curSize, node->value, len);
bryant_liuba2b4392014-06-11 16:49:30 +0800667 *curSize += len;
Eric Laurent138ed172016-02-10 10:40:44 -0800668 (*param)[*curSize] = '\0';
669 ALOGV("readParamValue() reading string %s", *param + *curSize - len);
670 } else {
671 ALOGW("readParamValue() unknown param type %s", node->name);
bryant_liuba2b4392014-06-11 16:49:30 +0800672 }
Eric Laurent138ed172016-02-10 10:40:44 -0800673exit:
674 return len;
bryant_liuba2b4392014-06-11 16:49:30 +0800675}
676
677effect_param_t *AudioPolicyEffects::loadEffectParameter(cnode *root)
678{
679 cnode *param;
680 cnode *value;
681 size_t curSize = sizeof(effect_param_t);
682 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
683 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
684
Eric Laurent138ed172016-02-10 10:40:44 -0800685 if (fx_param == NULL) {
686 ALOGE("%s malloc error for effect structure of size %zu",
687 __func__, totSize);
688 return NULL;
689 }
690
bryant_liuba2b4392014-06-11 16:49:30 +0800691 param = config_find(root, PARAM_TAG);
692 value = config_find(root, VALUE_TAG);
693 if (param == NULL && value == NULL) {
694 // try to parse simple parameter form {int int}
695 param = root->first_child;
696 if (param != NULL) {
697 // Note: that a pair of random strings is read as 0 0
698 int *ptr = (int *)fx_param->data;
Eric Laurent138ed172016-02-10 10:40:44 -0800699#if LOG_NDEBUG == 0
bryant_liuba2b4392014-06-11 16:49:30 +0800700 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Eric Laurent138ed172016-02-10 10:40:44 -0800701 ALOGV("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
702#endif
bryant_liuba2b4392014-06-11 16:49:30 +0800703 *ptr++ = atoi(param->name);
704 *ptr = atoi(param->value);
705 fx_param->psize = sizeof(int);
706 fx_param->vsize = sizeof(int);
707 return fx_param;
708 }
709 }
710 if (param == NULL || value == NULL) {
Eric Laurent138ed172016-02-10 10:40:44 -0800711 ALOGW("loadEffectParameter() invalid parameter description %s",
712 root->name);
bryant_liuba2b4392014-06-11 16:49:30 +0800713 goto error;
714 }
715
716 fx_param->psize = 0;
717 param = param->first_child;
718 while (param) {
719 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent138ed172016-02-10 10:40:44 -0800720 size_t size =
721 readParamValue(param, (char **)&fx_param, &curSize, &totSize);
bryant_liuba2b4392014-06-11 16:49:30 +0800722 if (size == 0) {
723 goto error;
724 }
725 fx_param->psize += size;
726 param = param->next;
727 }
728
729 // align start of value field on 32 bit boundary
730 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
731
732 fx_param->vsize = 0;
733 value = value->first_child;
734 while (value) {
735 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent138ed172016-02-10 10:40:44 -0800736 size_t size =
737 readParamValue(value, (char **)&fx_param, &curSize, &totSize);
bryant_liuba2b4392014-06-11 16:49:30 +0800738 if (size == 0) {
739 goto error;
740 }
741 fx_param->vsize += size;
742 value = value->next;
743 }
744
745 return fx_param;
746
747error:
Eric Laurent138ed172016-02-10 10:40:44 -0800748 free(fx_param);
bryant_liuba2b4392014-06-11 16:49:30 +0800749 return NULL;
750}
751
752void AudioPolicyEffects::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
753{
754 cnode *node = root->first_child;
755 while (node) {
756 ALOGV("loadEffectParameters() loading param %s", node->name);
757 effect_param_t *param = loadEffectParameter(node);
Eric Laurent138ed172016-02-10 10:40:44 -0800758 if (param != NULL) {
759 params.add(param);
bryant_liuba2b4392014-06-11 16:49:30 +0800760 }
bryant_liuba2b4392014-06-11 16:49:30 +0800761 node = node->next;
762 }
763}
764
765
766AudioPolicyEffects::EffectDescVector *AudioPolicyEffects::loadEffectConfig(
767 cnode *root,
768 const Vector <EffectDesc *>& effects)
769{
770 cnode *node = root->first_child;
771 if (node == NULL) {
772 ALOGW("loadInputSource() empty element %s", root->name);
773 return NULL;
774 }
775 EffectDescVector *desc = new EffectDescVector();
776 while (node) {
777 size_t i;
Eric Laurent138ed172016-02-10 10:40:44 -0800778
bryant_liuba2b4392014-06-11 16:49:30 +0800779 for (i = 0; i < effects.size(); i++) {
780 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
781 ALOGV("loadEffectConfig() found effect %s in list", node->name);
782 break;
783 }
784 }
785 if (i == effects.size()) {
786 ALOGV("loadEffectConfig() effect %s not in list", node->name);
787 node = node->next;
788 continue;
789 }
790 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
791 loadEffectParameters(node, effect->mParams);
792 ALOGV("loadEffectConfig() adding effect %s uuid %08x",
793 effect->mName, effect->mUuid.timeLow);
794 desc->mEffects.add(effect);
795 node = node->next;
796 }
797 if (desc->mEffects.size() == 0) {
798 ALOGW("loadEffectConfig() no valid effects found in config %s", root->name);
799 delete desc;
800 return NULL;
801 }
802 return desc;
803}
804
805status_t AudioPolicyEffects::loadInputEffectConfigurations(cnode *root,
806 const Vector <EffectDesc *>& effects)
807{
808 cnode *node = config_find(root, PREPROCESSING_TAG);
809 if (node == NULL) {
810 return -ENOENT;
811 }
812 node = node->first_child;
813 while (node) {
814 audio_source_t source = inputSourceNameToEnum(node->name);
815 if (source == AUDIO_SOURCE_CNT) {
816 ALOGW("loadInputSources() invalid input source %s", node->name);
817 node = node->next;
818 continue;
819 }
820 ALOGV("loadInputSources() loading input source %s", node->name);
821 EffectDescVector *desc = loadEffectConfig(node, effects);
822 if (desc == NULL) {
823 node = node->next;
824 continue;
825 }
826 mInputSources.add(source, desc);
827 node = node->next;
828 }
829 return NO_ERROR;
830}
831
832status_t AudioPolicyEffects::loadStreamEffectConfigurations(cnode *root,
833 const Vector <EffectDesc *>& effects)
834{
835 cnode *node = config_find(root, OUTPUT_SESSION_PROCESSING_TAG);
836 if (node == NULL) {
837 return -ENOENT;
838 }
839 node = node->first_child;
840 while (node) {
841 audio_stream_type_t stream = streamNameToEnum(node->name);
Eric Laurent223fd5c2014-11-11 13:43:36 -0800842 if (stream == AUDIO_STREAM_PUBLIC_CNT) {
bryant_liuba2b4392014-06-11 16:49:30 +0800843 ALOGW("loadStreamEffectConfigurations() invalid output stream %s", node->name);
844 node = node->next;
845 continue;
846 }
847 ALOGV("loadStreamEffectConfigurations() loading output stream %s", node->name);
848 EffectDescVector *desc = loadEffectConfig(node, effects);
849 if (desc == NULL) {
850 node = node->next;
851 continue;
852 }
853 mOutputStreams.add(stream, desc);
854 node = node->next;
855 }
856 return NO_ERROR;
857}
858
859AudioPolicyEffects::EffectDesc *AudioPolicyEffects::loadEffect(cnode *root)
860{
861 cnode *node = config_find(root, UUID_TAG);
862 if (node == NULL) {
863 return NULL;
864 }
865 effect_uuid_t uuid;
866 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
867 ALOGW("loadEffect() invalid uuid %s", node->value);
868 return NULL;
869 }
870 return new EffectDesc(root->name, uuid);
871}
872
873status_t AudioPolicyEffects::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
874{
875 cnode *node = config_find(root, EFFECTS_TAG);
876 if (node == NULL) {
877 return -ENOENT;
878 }
879 node = node->first_child;
880 while (node) {
881 ALOGV("loadEffects() loading effect %s", node->name);
882 EffectDesc *effect = loadEffect(node);
883 if (effect == NULL) {
884 node = node->next;
885 continue;
886 }
887 effects.add(effect);
888 node = node->next;
889 }
890 return NO_ERROR;
891}
892
Kevin Rocard4fb615c2017-06-26 10:28:13 -0700893status_t AudioPolicyEffects::loadAudioEffectXmlConfig() {
894 auto result = effectsConfig::parse();
895 if (result.parsedConfig == nullptr) {
896 return -ENOENT;
897 }
898
899 auto loadProcessingChain = [](auto& processingChain, auto& streams) {
900 for (auto& stream : processingChain) {
901 auto effectDescs = std::make_unique<EffectDescVector>();
902 for (auto& effect : stream.effects) {
903 effectDescs->mEffects.add(
904 new EffectDesc{effect.get().name.c_str(), effect.get().uuid});
905 }
906 streams.add(stream.type, effectDescs.release());
907 }
908 };
909 loadProcessingChain(result.parsedConfig->preprocess, mInputSources);
910 loadProcessingChain(result.parsedConfig->postprocess, mOutputStreams);
911 // Casting from ssize_t to status_t is probably safe, there should not be more than 2^31 errors
912 return result.nbSkippedElement;
913}
914
bryant_liuba2b4392014-06-11 16:49:30 +0800915status_t AudioPolicyEffects::loadAudioEffectConfig(const char *path)
916{
917 cnode *root;
918 char *data;
919
920 data = (char *)load_file(path, NULL);
921 if (data == NULL) {
922 return -ENODEV;
923 }
924 root = config_node("", "");
925 config_load(root, data);
926
927 Vector <EffectDesc *> effects;
928 loadEffects(root, effects);
929 loadInputEffectConfigurations(root, effects);
930 loadStreamEffectConfigurations(root, effects);
931
Eric Laurent182c2f52015-01-15 14:29:19 -0800932 for (size_t i = 0; i < effects.size(); i++) {
933 delete effects[i];
934 }
935
bryant_liuba2b4392014-06-11 16:49:30 +0800936 config_free(root);
937 free(root);
938 free(data);
939
940 return NO_ERROR;
941}
942
943
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800944} // namespace android