blob: 2858aadb2d0302aa4f45772a568e7fdda16afb17 [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-Cohen433722e2018-04-24 14:25:22 -0700321status_t AudioPolicyEffects::addStreamDefaultEffect(const effect_uuid_t *type,
322 const String16& opPackageName,
323 const effect_uuid_t *uuid,
324 int32_t priority,
325 audio_usage_t usage,
326 audio_unique_id_t* id)
327{
328 if (uuid == NULL || type == NULL) {
329 ALOGE("addStreamDefaultEffect(): Null uuid or type uuid pointer");
330 return BAD_VALUE;
331 }
332
333 audio_stream_type_t stream = audio_usage_to_stream_type(usage);
334
335 if (stream < AUDIO_STREAM_MIN || stream >= AUDIO_STREAM_PUBLIC_CNT) {
336 ALOGE("addStreamDefaultEffect(): Unsupported stream type %d", stream);
337 return BAD_VALUE;
338 }
339
340 // Check that |uuid| or |type| corresponds to an effect on the system.
341 effect_descriptor_t descriptor = {};
342 status_t res = AudioEffect::getEffectDescriptor(
343 uuid, type, EFFECT_FLAG_TYPE_INSERT, &descriptor);
344 if (res != OK) {
345 ALOGE("addStreamDefaultEffect(): Failed to find effect descriptor matching uuid/type.");
346 return res;
347 }
348
349 // Only insert effects can be added dynamically as stream defaults.
350 if ((descriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_INSERT) {
351 ALOGE("addStreamDefaultEffect(): Desired effect cannot be attached "
352 "as a stream default effect.");
353 return BAD_VALUE;
354 }
355
356 Mutex::Autolock _l(mLock);
357
358 // Find the EffectDescVector for the given stream type, or create a new one if necessary.
359 ssize_t index = mOutputStreams.indexOfKey(stream);
360 EffectDescVector *desc = NULL;
361 if (index < 0) {
362 // No effects for this stream type yet.
363 desc = new EffectDescVector();
364 mOutputStreams.add(stream, desc);
365 } else {
366 desc = mOutputStreams.valueAt(index);
367 }
368
369 // Create a new effect and add it to the vector.
370 res = AudioEffect::newEffectUniqueId(id);
371 if (res != OK) {
372 ALOGE("addStreamDefaultEffect(): failed to get new unique id.");
373 return res;
374 }
375 EffectDesc *effect = new EffectDesc(
376 descriptor.name, *type, opPackageName, *uuid, priority, *id);
377 desc->mEffects.add(effect);
378 // TODO(b/71813697): Support setting params as well.
379
380 // TODO(b/71814300): Retroactively attach to any existing streams of the given type.
381 // This requires tracking the stream type of each session id in addition to what is
382 // already being tracked.
383
384 return NO_ERROR;
385}
386
387status_t AudioPolicyEffects::removeStreamDefaultEffect(audio_unique_id_t id)
388{
389 if (id == AUDIO_UNIQUE_ID_ALLOCATE) {
390 // ALLOCATE is not a unique identifier, but rather a reserved value indicating
391 // a real id has not been assigned. For default effects, this value is only used
392 // by system-owned defaults from the loaded config, which cannot be removed.
393 return BAD_VALUE;
394 }
395
396 Mutex::Autolock _l(mLock);
397
398 // Check each stream type.
399 size_t numStreams = mOutputStreams.size();
400 for (size_t i = 0; i < numStreams; ++i) {
401 // Check each effect for each stream.
402 EffectDescVector* descVector = mOutputStreams[i];
403 for (auto desc = descVector->mEffects.begin(); desc != descVector->mEffects.end(); ++desc) {
404 if ((*desc)->mId == id) {
405 // Found it!
406 // TODO(b/71814300): Remove from any streams the effect was attached to.
407 descVector->mEffects.erase(desc);
408 // Handles are unique; there can only be one match, so return early.
409 return NO_ERROR;
410 }
411 }
412 }
413
414 // Effect wasn't found, so it's been trivially removed successfully.
415 return NO_ERROR;
416}
bryant_liuba2b4392014-06-11 16:49:30 +0800417
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700418void AudioPolicyEffects::EffectVector::setProcessorEnabled(bool enabled)
bryant_liuba2b4392014-06-11 16:49:30 +0800419{
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700420 for (size_t i = 0; i < mEffects.size(); i++) {
421 mEffects.itemAt(i)->setEnabled(enabled);
bryant_liuba2b4392014-06-11 16:49:30 +0800422 }
423}
424
425
426// ----------------------------------------------------------------------------
427// Audio processing configuration
428// ----------------------------------------------------------------------------
429
430/*static*/ const char * const AudioPolicyEffects::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
431 MIC_SRC_TAG,
432 VOICE_UL_SRC_TAG,
433 VOICE_DL_SRC_TAG,
434 VOICE_CALL_SRC_TAG,
435 CAMCORDER_SRC_TAG,
436 VOICE_REC_SRC_TAG,
rago8a397d52015-12-02 11:27:57 -0800437 VOICE_COMM_SRC_TAG,
438 UNPROCESSED_SRC_TAG
bryant_liuba2b4392014-06-11 16:49:30 +0800439};
440
441// returns the audio_source_t enum corresponding to the input source name or
442// AUDIO_SOURCE_CNT is no match found
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700443/*static*/ audio_source_t AudioPolicyEffects::inputSourceNameToEnum(const char *name)
bryant_liuba2b4392014-06-11 16:49:30 +0800444{
445 int i;
446 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
447 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
448 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
449 break;
450 }
451 }
452 return (audio_source_t)i;
453}
454
Eric Laurent223fd5c2014-11-11 13:43:36 -0800455const char *AudioPolicyEffects::kStreamNames[AUDIO_STREAM_PUBLIC_CNT+1] = {
bryant_liuba2b4392014-06-11 16:49:30 +0800456 AUDIO_STREAM_DEFAULT_TAG,
457 AUDIO_STREAM_VOICE_CALL_TAG,
458 AUDIO_STREAM_SYSTEM_TAG,
459 AUDIO_STREAM_RING_TAG,
460 AUDIO_STREAM_MUSIC_TAG,
461 AUDIO_STREAM_ALARM_TAG,
462 AUDIO_STREAM_NOTIFICATION_TAG,
463 AUDIO_STREAM_BLUETOOTH_SCO_TAG,
464 AUDIO_STREAM_ENFORCED_AUDIBLE_TAG,
465 AUDIO_STREAM_DTMF_TAG,
466 AUDIO_STREAM_TTS_TAG
467};
468
469// returns the audio_stream_t enum corresponding to the output stream name or
Eric Laurent223fd5c2014-11-11 13:43:36 -0800470// AUDIO_STREAM_PUBLIC_CNT is no match found
bryant_liuba2b4392014-06-11 16:49:30 +0800471audio_stream_type_t AudioPolicyEffects::streamNameToEnum(const char *name)
472{
473 int i;
Eric Laurent223fd5c2014-11-11 13:43:36 -0800474 for (i = AUDIO_STREAM_DEFAULT; i < AUDIO_STREAM_PUBLIC_CNT; i++) {
bryant_liuba2b4392014-06-11 16:49:30 +0800475 if (strcmp(name, kStreamNames[i - AUDIO_STREAM_DEFAULT]) == 0) {
476 ALOGV("streamNameToEnum found stream %s %d", name, i);
477 break;
478 }
479 }
480 return (audio_stream_type_t)i;
481}
482
483// ----------------------------------------------------------------------------
484// Audio Effect Config parser
485// ----------------------------------------------------------------------------
486
Eric Laurent138ed172016-02-10 10:40:44 -0800487size_t AudioPolicyEffects::growParamSize(char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800488 size_t size,
489 size_t *curSize,
490 size_t *totSize)
491{
492 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
493 size_t pos = ((*curSize - 1 ) / size + 1) * size;
494
495 if (pos + size > *totSize) {
496 while (pos + size > *totSize) {
497 *totSize += ((*totSize + 7) / 8) * 4;
498 }
George Burgess IV31225682018-02-12 11:08:38 -0800499 char *newParam = (char *)realloc(*param, *totSize);
500 if (newParam == NULL) {
Eric Laurent138ed172016-02-10 10:40:44 -0800501 ALOGE("%s realloc error for size %zu", __func__, *totSize);
502 return 0;
503 }
George Burgess IV31225682018-02-12 11:08:38 -0800504 *param = newParam;
bryant_liuba2b4392014-06-11 16:49:30 +0800505 }
506 *curSize = pos + size;
507 return pos;
508}
509
Eric Laurent138ed172016-02-10 10:40:44 -0800510
bryant_liuba2b4392014-06-11 16:49:30 +0800511size_t AudioPolicyEffects::readParamValue(cnode *node,
Eric Laurent138ed172016-02-10 10:40:44 -0800512 char **param,
bryant_liuba2b4392014-06-11 16:49:30 +0800513 size_t *curSize,
514 size_t *totSize)
515{
Eric Laurent138ed172016-02-10 10:40:44 -0800516 size_t len = 0;
517 size_t pos;
518
bryant_liuba2b4392014-06-11 16:49:30 +0800519 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
Eric Laurent138ed172016-02-10 10:40:44 -0800520 pos = growParamSize(param, sizeof(short), curSize, totSize);
521 if (pos == 0) {
522 goto exit;
bryant_liuba2b4392014-06-11 16:49:30 +0800523 }
Eric Laurent138ed172016-02-10 10:40:44 -0800524 *(short *)(*param + pos) = (short)atoi(node->value);
525 ALOGV("readParamValue() reading short %d", *(short *)(*param + pos));
526 len = sizeof(short);
527 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
528 pos = growParamSize(param, sizeof(int), curSize, totSize);
529 if (pos == 0) {
530 goto exit;
531 }
532 *(int *)(*param + pos) = atoi(node->value);
533 ALOGV("readParamValue() reading int %d", *(int *)(*param + pos));
534 len = sizeof(int);
535 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
536 pos = growParamSize(param, sizeof(float), curSize, totSize);
537 if (pos == 0) {
538 goto exit;
539 }
540 *(float *)(*param + pos) = (float)atof(node->value);
541 ALOGV("readParamValue() reading float %f",*(float *)(*param + pos));
542 len = sizeof(float);
543 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
544 pos = growParamSize(param, sizeof(bool), curSize, totSize);
545 if (pos == 0) {
546 goto exit;
547 }
548 if (strncmp(node->value, "true", strlen("true") + 1) == 0) {
549 *(bool *)(*param + pos) = true;
550 } else {
551 *(bool *)(*param + pos) = false;
552 }
553 ALOGV("readParamValue() reading bool %s",
554 *(bool *)(*param + pos) ? "true" : "false");
555 len = sizeof(bool);
bryant_liuba2b4392014-06-11 16:49:30 +0800556 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
Eric Laurent138ed172016-02-10 10:40:44 -0800557 len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
bryant_liuba2b4392014-06-11 16:49:30 +0800558 if (*curSize + len + 1 > *totSize) {
559 *totSize = *curSize + len + 1;
Eric Laurent138ed172016-02-10 10:40:44 -0800560 *param = (char *)realloc(*param, *totSize);
561 if (*param == NULL) {
562 len = 0;
563 ALOGE("%s realloc error for string len %zu", __func__, *totSize);
564 goto exit;
565 }
bryant_liuba2b4392014-06-11 16:49:30 +0800566 }
Eric Laurent138ed172016-02-10 10:40:44 -0800567 strncpy(*param + *curSize, node->value, len);
bryant_liuba2b4392014-06-11 16:49:30 +0800568 *curSize += len;
Eric Laurent138ed172016-02-10 10:40:44 -0800569 (*param)[*curSize] = '\0';
570 ALOGV("readParamValue() reading string %s", *param + *curSize - len);
571 } else {
572 ALOGW("readParamValue() unknown param type %s", node->name);
bryant_liuba2b4392014-06-11 16:49:30 +0800573 }
Eric Laurent138ed172016-02-10 10:40:44 -0800574exit:
575 return len;
bryant_liuba2b4392014-06-11 16:49:30 +0800576}
577
578effect_param_t *AudioPolicyEffects::loadEffectParameter(cnode *root)
579{
580 cnode *param;
581 cnode *value;
582 size_t curSize = sizeof(effect_param_t);
583 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
584 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
585
Eric Laurent138ed172016-02-10 10:40:44 -0800586 if (fx_param == NULL) {
587 ALOGE("%s malloc error for effect structure of size %zu",
588 __func__, totSize);
589 return NULL;
590 }
591
bryant_liuba2b4392014-06-11 16:49:30 +0800592 param = config_find(root, PARAM_TAG);
593 value = config_find(root, VALUE_TAG);
594 if (param == NULL && value == NULL) {
595 // try to parse simple parameter form {int int}
596 param = root->first_child;
597 if (param != NULL) {
598 // Note: that a pair of random strings is read as 0 0
599 int *ptr = (int *)fx_param->data;
Eric Laurent138ed172016-02-10 10:40:44 -0800600#if LOG_NDEBUG == 0
bryant_liuba2b4392014-06-11 16:49:30 +0800601 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Eric Laurent138ed172016-02-10 10:40:44 -0800602 ALOGV("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
603#endif
bryant_liuba2b4392014-06-11 16:49:30 +0800604 *ptr++ = atoi(param->name);
605 *ptr = atoi(param->value);
606 fx_param->psize = sizeof(int);
607 fx_param->vsize = sizeof(int);
608 return fx_param;
609 }
610 }
611 if (param == NULL || value == NULL) {
Eric Laurent138ed172016-02-10 10:40:44 -0800612 ALOGW("loadEffectParameter() invalid parameter description %s",
613 root->name);
bryant_liuba2b4392014-06-11 16:49:30 +0800614 goto error;
615 }
616
617 fx_param->psize = 0;
618 param = param->first_child;
619 while (param) {
620 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent138ed172016-02-10 10:40:44 -0800621 size_t size =
622 readParamValue(param, (char **)&fx_param, &curSize, &totSize);
bryant_liuba2b4392014-06-11 16:49:30 +0800623 if (size == 0) {
624 goto error;
625 }
626 fx_param->psize += size;
627 param = param->next;
628 }
629
630 // align start of value field on 32 bit boundary
631 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
632
633 fx_param->vsize = 0;
634 value = value->first_child;
635 while (value) {
636 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent138ed172016-02-10 10:40:44 -0800637 size_t size =
638 readParamValue(value, (char **)&fx_param, &curSize, &totSize);
bryant_liuba2b4392014-06-11 16:49:30 +0800639 if (size == 0) {
640 goto error;
641 }
642 fx_param->vsize += size;
643 value = value->next;
644 }
645
646 return fx_param;
647
648error:
Eric Laurent138ed172016-02-10 10:40:44 -0800649 free(fx_param);
bryant_liuba2b4392014-06-11 16:49:30 +0800650 return NULL;
651}
652
653void AudioPolicyEffects::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
654{
655 cnode *node = root->first_child;
656 while (node) {
657 ALOGV("loadEffectParameters() loading param %s", node->name);
658 effect_param_t *param = loadEffectParameter(node);
Eric Laurent138ed172016-02-10 10:40:44 -0800659 if (param != NULL) {
660 params.add(param);
bryant_liuba2b4392014-06-11 16:49:30 +0800661 }
bryant_liuba2b4392014-06-11 16:49:30 +0800662 node = node->next;
663 }
664}
665
666
667AudioPolicyEffects::EffectDescVector *AudioPolicyEffects::loadEffectConfig(
668 cnode *root,
669 const Vector <EffectDesc *>& effects)
670{
671 cnode *node = root->first_child;
672 if (node == NULL) {
673 ALOGW("loadInputSource() empty element %s", root->name);
674 return NULL;
675 }
676 EffectDescVector *desc = new EffectDescVector();
677 while (node) {
678 size_t i;
Eric Laurent138ed172016-02-10 10:40:44 -0800679
bryant_liuba2b4392014-06-11 16:49:30 +0800680 for (i = 0; i < effects.size(); i++) {
681 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
682 ALOGV("loadEffectConfig() found effect %s in list", node->name);
683 break;
684 }
685 }
686 if (i == effects.size()) {
687 ALOGV("loadEffectConfig() effect %s not in list", node->name);
688 node = node->next;
689 continue;
690 }
691 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
692 loadEffectParameters(node, effect->mParams);
693 ALOGV("loadEffectConfig() adding effect %s uuid %08x",
694 effect->mName, effect->mUuid.timeLow);
695 desc->mEffects.add(effect);
696 node = node->next;
697 }
698 if (desc->mEffects.size() == 0) {
699 ALOGW("loadEffectConfig() no valid effects found in config %s", root->name);
700 delete desc;
701 return NULL;
702 }
703 return desc;
704}
705
706status_t AudioPolicyEffects::loadInputEffectConfigurations(cnode *root,
707 const Vector <EffectDesc *>& effects)
708{
709 cnode *node = config_find(root, PREPROCESSING_TAG);
710 if (node == NULL) {
711 return -ENOENT;
712 }
713 node = node->first_child;
714 while (node) {
715 audio_source_t source = inputSourceNameToEnum(node->name);
716 if (source == AUDIO_SOURCE_CNT) {
717 ALOGW("loadInputSources() invalid input source %s", node->name);
718 node = node->next;
719 continue;
720 }
721 ALOGV("loadInputSources() loading input source %s", node->name);
722 EffectDescVector *desc = loadEffectConfig(node, effects);
723 if (desc == NULL) {
724 node = node->next;
725 continue;
726 }
727 mInputSources.add(source, desc);
728 node = node->next;
729 }
730 return NO_ERROR;
731}
732
733status_t AudioPolicyEffects::loadStreamEffectConfigurations(cnode *root,
734 const Vector <EffectDesc *>& effects)
735{
736 cnode *node = config_find(root, OUTPUT_SESSION_PROCESSING_TAG);
737 if (node == NULL) {
738 return -ENOENT;
739 }
740 node = node->first_child;
741 while (node) {
742 audio_stream_type_t stream = streamNameToEnum(node->name);
Eric Laurent223fd5c2014-11-11 13:43:36 -0800743 if (stream == AUDIO_STREAM_PUBLIC_CNT) {
bryant_liuba2b4392014-06-11 16:49:30 +0800744 ALOGW("loadStreamEffectConfigurations() invalid output stream %s", node->name);
745 node = node->next;
746 continue;
747 }
748 ALOGV("loadStreamEffectConfigurations() loading output stream %s", node->name);
749 EffectDescVector *desc = loadEffectConfig(node, effects);
750 if (desc == NULL) {
751 node = node->next;
752 continue;
753 }
754 mOutputStreams.add(stream, desc);
755 node = node->next;
756 }
757 return NO_ERROR;
758}
759
760AudioPolicyEffects::EffectDesc *AudioPolicyEffects::loadEffect(cnode *root)
761{
762 cnode *node = config_find(root, UUID_TAG);
763 if (node == NULL) {
764 return NULL;
765 }
766 effect_uuid_t uuid;
767 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
768 ALOGW("loadEffect() invalid uuid %s", node->value);
769 return NULL;
770 }
771 return new EffectDesc(root->name, uuid);
772}
773
774status_t AudioPolicyEffects::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
775{
776 cnode *node = config_find(root, EFFECTS_TAG);
777 if (node == NULL) {
778 return -ENOENT;
779 }
780 node = node->first_child;
781 while (node) {
782 ALOGV("loadEffects() loading effect %s", node->name);
783 EffectDesc *effect = loadEffect(node);
784 if (effect == NULL) {
785 node = node->next;
786 continue;
787 }
788 effects.add(effect);
789 node = node->next;
790 }
791 return NO_ERROR;
792}
793
Kevin Rocard4fb615c2017-06-26 10:28:13 -0700794status_t AudioPolicyEffects::loadAudioEffectXmlConfig() {
795 auto result = effectsConfig::parse();
796 if (result.parsedConfig == nullptr) {
797 return -ENOENT;
798 }
799
800 auto loadProcessingChain = [](auto& processingChain, auto& streams) {
801 for (auto& stream : processingChain) {
802 auto effectDescs = std::make_unique<EffectDescVector>();
803 for (auto& effect : stream.effects) {
804 effectDescs->mEffects.add(
805 new EffectDesc{effect.get().name.c_str(), effect.get().uuid});
806 }
807 streams.add(stream.type, effectDescs.release());
808 }
809 };
810 loadProcessingChain(result.parsedConfig->preprocess, mInputSources);
811 loadProcessingChain(result.parsedConfig->postprocess, mOutputStreams);
812 // Casting from ssize_t to status_t is probably safe, there should not be more than 2^31 errors
813 return result.nbSkippedElement;
814}
815
bryant_liuba2b4392014-06-11 16:49:30 +0800816status_t AudioPolicyEffects::loadAudioEffectConfig(const char *path)
817{
818 cnode *root;
819 char *data;
820
821 data = (char *)load_file(path, NULL);
822 if (data == NULL) {
823 return -ENODEV;
824 }
825 root = config_node("", "");
826 config_load(root, data);
827
828 Vector <EffectDesc *> effects;
829 loadEffects(root, effects);
830 loadInputEffectConfigurations(root, effects);
831 loadStreamEffectConfigurations(root, effects);
832
Eric Laurent182c2f52015-01-15 14:29:19 -0800833 for (size_t i = 0; i < effects.size(); i++) {
834 delete effects[i];
835 }
836
bryant_liuba2b4392014-06-11 16:49:30 +0800837 config_free(root);
838 free(root);
839 free(data);
840
841 return NO_ERROR;
842}
843
844
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800845} // namespace android