1 /*
2 * Copyright (C) 2010 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
18 #include "sles_allinclusive.h"
19 #include "math.h"
20 #include "utils/RefBase.h"
21 #include "utils/String16.h"
22
23 #include <system/audio_effects/effect_bassboost.h>
24 #include <system/audio_effects/effect_equalizer.h>
25 #include <system/audio_effects/effect_environmentalreverb.h>
26 #include <system/audio_effects/effect_presetreverb.h>
27 #include <system/audio_effects/effect_virtualizer.h>
28
29 #include <system/audio_effects/effect_aec.h>
30 #include <system/audio_effects/effect_agc.h>
31 #include <system/audio_effects/effect_ns.h>
32
33 #include <system/audio.h>
34
35 using android::content::AttributionSourceState;
36
37 static const int EQUALIZER_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t)
38 + EFFECT_STRING_LEN_MAX;
39
40 static const int BASSBOOST_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
41
42 static const int VIRTUALIZER_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
43
44 static const int ENVREVERB_PARAM_SIZE_MAX_SINGLE = sizeof(effect_param_t) + 2 * sizeof(int32_t);
45
46 static const int ENVREVERB_PARAM_SIZE_MAX_ALL = sizeof(effect_param_t) + sizeof(int32_t)
47 + sizeof(s_reverb_settings);
48
49 static const int PRESETREVERB_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
50
KEY_FROM_GUID(SLInterfaceID pUuid)51 static inline SLuint32 KEY_FROM_GUID(SLInterfaceID pUuid) {
52 return pUuid->time_low;
53 }
54
55
56 //-----------------------------------------------------------------------------
57 static
eq_paramSize(int32_t param)58 uint32_t eq_paramSize(int32_t param) {
59 uint32_t size;
60
61 switch (param) {
62 case EQ_PARAM_NUM_BANDS:
63 case EQ_PARAM_LEVEL_RANGE:
64 case EQ_PARAM_CUR_PRESET:
65 case EQ_PARAM_GET_NUM_OF_PRESETS:
66 size = sizeof(int32_t);
67 break;
68 case EQ_PARAM_BAND_LEVEL:
69 case EQ_PARAM_CENTER_FREQ:
70 case EQ_PARAM_BAND_FREQ_RANGE:
71 case EQ_PARAM_GET_BAND:
72 case EQ_PARAM_GET_PRESET_NAME:
73 size = 2 * sizeof(int32_t);
74 break;
75 default:
76 size = 2 * sizeof(int32_t);
77 SL_LOGE("Trying to use an unknown EQ parameter %d", param);
78 break;
79 }
80 return size;
81 }
82
83 static
eq_valueSize(int32_t param)84 uint32_t eq_valueSize(int32_t param) {
85 uint32_t size;
86
87 switch (param) {
88 case EQ_PARAM_NUM_BANDS:
89 case EQ_PARAM_CUR_PRESET:
90 case EQ_PARAM_GET_NUM_OF_PRESETS:
91 case EQ_PARAM_BAND_LEVEL:
92 case EQ_PARAM_GET_BAND:
93 size = sizeof(int16_t);
94 break;
95 case EQ_PARAM_LEVEL_RANGE:
96 size = 2 * sizeof(int16_t);
97 break;
98 case EQ_PARAM_CENTER_FREQ:
99 size = sizeof(int32_t);
100 break;
101 case EQ_PARAM_BAND_FREQ_RANGE:
102 size = 2 * sizeof(int32_t);
103 break;
104 case EQ_PARAM_GET_PRESET_NAME:
105 size = EFFECT_STRING_LEN_MAX;
106 break;
107 default:
108 size = sizeof(int32_t);
109 SL_LOGE("Trying to access an unknown EQ parameter %d", param);
110 break;
111 }
112 return size;
113 }
114
115 //-----------------------------------------------------------------------------
116 /**
117 * returns the size in bytes of the value of each bass boost parameter
118 */
119 static
bb_valueSize(int32_t param)120 uint32_t bb_valueSize(int32_t param) {
121 uint32_t size;
122
123 switch (param) {
124 case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
125 size = sizeof(int32_t);
126 break;
127 case BASSBOOST_PARAM_STRENGTH:
128 size = sizeof(int16_t);
129 break;
130 default:
131 size = sizeof(int32_t);
132 SL_LOGE("Trying to access an unknown BassBoost parameter %d", param);
133 break;
134 }
135
136 return size;
137 }
138
139 //-----------------------------------------------------------------------------
140 /**
141 * returns the size in bytes of the value of each virtualizer parameter
142 */
143 static
virt_valueSize(int32_t param)144 uint32_t virt_valueSize(int32_t param) {
145 uint32_t size;
146
147 switch (param) {
148 case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
149 size = sizeof(int32_t);
150 break;
151 case VIRTUALIZER_PARAM_STRENGTH:
152 size = sizeof(int16_t);
153 break;
154 default:
155 size = sizeof(int32_t);
156 SL_LOGE("Trying to access an unknown Virtualizer parameter %d", param);
157 break;
158 }
159
160 return size;
161 }
162
163 //-----------------------------------------------------------------------------
164 /**
165 * returns the size in bytes of the value of each environmental reverb parameter
166 */
167 static
erev_valueSize(int32_t param)168 uint32_t erev_valueSize(int32_t param) {
169 uint32_t size;
170
171 switch (param) {
172 case REVERB_PARAM_ROOM_LEVEL:
173 case REVERB_PARAM_ROOM_HF_LEVEL:
174 case REVERB_PARAM_REFLECTIONS_LEVEL:
175 case REVERB_PARAM_REVERB_LEVEL:
176 size = sizeof(int16_t); // millibel
177 break;
178 case REVERB_PARAM_DECAY_TIME:
179 case REVERB_PARAM_REFLECTIONS_DELAY:
180 case REVERB_PARAM_REVERB_DELAY:
181 size = sizeof(uint32_t); // milliseconds
182 break;
183 case REVERB_PARAM_DECAY_HF_RATIO:
184 case REVERB_PARAM_DIFFUSION:
185 case REVERB_PARAM_DENSITY:
186 size = sizeof(int16_t); // permille
187 break;
188 case REVERB_PARAM_PROPERTIES:
189 size = sizeof(s_reverb_settings); // struct of all reverb properties
190 break;
191 default:
192 size = sizeof(int32_t);
193 SL_LOGE("Trying to access an unknown Environmental Reverb parameter %d", param);
194 break;
195 }
196
197 return size;
198 }
199
200 //-----------------------------------------------------------------------------
android_eq_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,int32_t param2,void * pValue)201 android::status_t android_eq_getParam(const android::sp<android::AudioEffect>& pFx,
202 int32_t param, int32_t param2, void *pValue)
203 {
204 android::status_t status;
205 uint32_t buf32[(EQUALIZER_PARAM_SIZE_MAX - 1) / sizeof(uint32_t) + 1];
206 effect_param_t *p = (effect_param_t *)buf32;
207
208 p->psize = eq_paramSize(param);
209 *(int32_t *)p->data = param;
210 if (p->psize == 2 * sizeof(int32_t)) {
211 *((int32_t *)p->data + 1) = param2;
212 }
213 p->vsize = eq_valueSize(param);
214 status = pFx->getParameter(p);
215 if (android::NO_ERROR == status) {
216 status = p->status;
217 if (android::NO_ERROR == status) {
218 memcpy(pValue, p->data + p->psize, p->vsize);
219 }
220 }
221
222 return status;
223 }
224
225
226 //-----------------------------------------------------------------------------
android_eq_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,int32_t param2,void * pValue)227 android::status_t android_eq_setParam(const android::sp<android::AudioEffect>& pFx,
228 int32_t param, int32_t param2, void *pValue)
229 {
230 android::status_t status;
231 uint32_t buf32[(EQUALIZER_PARAM_SIZE_MAX - 1) / sizeof(uint32_t) + 1];
232 effect_param_t *p = (effect_param_t *)buf32;
233
234 p->psize = eq_paramSize(param);
235 *(int32_t *)p->data = param;
236 if (p->psize == 2 * sizeof(int32_t)) {
237 *((int32_t *)p->data + 1) = param2;
238 }
239 p->vsize = eq_valueSize(param);
240 memcpy(p->data + p->psize, pValue, p->vsize);
241 status = pFx->setParameter(p);
242 if (android::NO_ERROR == status) {
243 status = p->status;
244 }
245
246 return status;
247 }
248
249 //-----------------------------------------------------------------------------
android_bb_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)250 android::status_t android_bb_setParam(const android::sp<android::AudioEffect>& pFx,
251 int32_t param, void *pValue) {
252
253 return android_fx_setParam(pFx, param, BASSBOOST_PARAM_SIZE_MAX,
254 pValue, bb_valueSize(param));
255 }
256
257 //-----------------------------------------------------------------------------
android_bb_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)258 android::status_t android_bb_getParam(const android::sp<android::AudioEffect>& pFx,
259 int32_t param, void *pValue) {
260
261 return android_fx_getParam(pFx, param, BASSBOOST_PARAM_SIZE_MAX,
262 pValue, bb_valueSize(param));
263 }
264
265 //-----------------------------------------------------------------------------
android_bb_init(audio_session_t sessionId,IBassBoost * ibb)266 void android_bb_init(audio_session_t sessionId, IBassBoost* ibb) {
267 SL_LOGV("session %d", sessionId);
268
269 if (!android_fx_initEffectObj(sessionId, ibb->mBassBoostEffect,
270 &ibb->mBassBoostDescriptor.type))
271 {
272 SL_LOGE("BassBoost effect initialization failed");
273 return;
274 }
275
276 // initialize strength
277 int16_t strength;
278 if (android::NO_ERROR == android_bb_getParam(ibb->mBassBoostEffect,
279 BASSBOOST_PARAM_STRENGTH, &strength)) {
280 ibb->mStrength = (SLpermille) strength;
281 }
282 }
283
284
285 //-----------------------------------------------------------------------------
android_eq_init(audio_session_t sessionId,IEqualizer * ieq)286 void android_eq_init(audio_session_t sessionId, IEqualizer* ieq) {
287 SL_LOGV("android_eq_init on session %d", sessionId);
288
289 if (!android_fx_initEffectObj(sessionId, ieq->mEqEffect, &ieq->mEqDescriptor.type)) {
290 SL_LOGE("Equalizer effect initialization failed");
291 return;
292 }
293
294 // initialize number of bands, band level range, and number of presets
295 uint16_t num = 0;
296 if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect, EQ_PARAM_NUM_BANDS, 0, &num)) {
297 ieq->mNumBands = num;
298 }
299 int16_t range[2] = {0, 0};
300 if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect, EQ_PARAM_LEVEL_RANGE, 0, range)) {
301 ieq->mBandLevelRangeMin = range[0];
302 ieq->mBandLevelRangeMax = range[1];
303 }
304
305 SL_LOGV(" EQ init: num bands = %u, band range=[%d %d]mB", num, range[0], range[1]);
306
307 // FIXME don't store presets names, they can be queried each time they're needed
308 // initialize preset number and names, store in IEngine
309 uint16_t numPresets = 0;
310 if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect,
311 EQ_PARAM_GET_NUM_OF_PRESETS, 0, &numPresets)) {
312 ieq->mThis->mEngine->mEqNumPresets = numPresets;
313 ieq->mNumPresets = numPresets;
314 }
315
316 object_lock_exclusive(&ieq->mThis->mEngine->mObject);
317 char name[EFFECT_STRING_LEN_MAX];
318 if ((0 < numPresets) && (NULL == ieq->mThis->mEngine->mEqPresetNames)) {
319 ieq->mThis->mEngine->mEqPresetNames = (char **)new char *[numPresets];
320 for(uint32_t i = 0 ; i < numPresets ; i++) {
321 if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect,
322 EQ_PARAM_GET_PRESET_NAME, i, name)) {
323 ieq->mThis->mEngine->mEqPresetNames[i] = new char[strlen(name) + 1];
324 strcpy(ieq->mThis->mEngine->mEqPresetNames[i], name);
325 SL_LOGV(" EQ init: presets = %u is %s", i, ieq->mThis->mEngine->mEqPresetNames[i]);
326 }
327 }
328 }
329 object_unlock_exclusive(&ieq->mThis->mEngine->mObject);
330
331 }
332
333
334 //-----------------------------------------------------------------------------
android_virt_init(audio_session_t sessionId,IVirtualizer * ivi)335 void android_virt_init(audio_session_t sessionId, IVirtualizer* ivi) {
336 SL_LOGV("android_virt_init on session %d", sessionId);
337
338 if (!android_fx_initEffectObj(sessionId, ivi->mVirtualizerEffect,
339 &ivi->mVirtualizerDescriptor.type)) {
340 SL_LOGE("Virtualizer effect initialization failed");
341 return;
342 }
343
344 // initialize strength
345 int16_t strength;
346 if (android::NO_ERROR == android_virt_getParam(ivi->mVirtualizerEffect,
347 VIRTUALIZER_PARAM_STRENGTH, &strength)) {
348 ivi->mStrength = (SLpermille) strength;
349 }
350 }
351
352 //-----------------------------------------------------------------------------
android_virt_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)353 android::status_t android_virt_setParam(const android::sp<android::AudioEffect>& pFx,
354 int32_t param, void *pValue) {
355
356 return android_fx_setParam(pFx, param, VIRTUALIZER_PARAM_SIZE_MAX,
357 pValue, virt_valueSize(param));
358 }
359
360 //-----------------------------------------------------------------------------
android_virt_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)361 android::status_t android_virt_getParam(const android::sp<android::AudioEffect>& pFx,
362 int32_t param, void *pValue) {
363
364 return android_fx_getParam(pFx, param, VIRTUALIZER_PARAM_SIZE_MAX,
365 pValue, virt_valueSize(param));
366 }
367
368
369 //-----------------------------------------------------------------------------
android_prev_init(IPresetReverb * ipr)370 void android_prev_init(IPresetReverb* ipr) {
371 SL_LOGV("session is implicitly %d (aux effect)", AUDIO_SESSION_OUTPUT_MIX);
372
373 if (!android_fx_initEffectObj(AUDIO_SESSION_OUTPUT_MIX /*sessionId*/,
374 ipr->mPresetReverbEffect, &ipr->mPresetReverbDescriptor.type)) {
375 SL_LOGE("PresetReverb effect initialization failed");
376 return;
377 }
378
379 // initialize preset
380 uint16_t preset;
381 if (android::NO_ERROR == android_prev_getPreset(ipr->mPresetReverbEffect, &preset)) {
382 ipr->mPreset = preset;
383 // enable the effect if it has a preset loaded
384 ipr->mPresetReverbEffect->setEnabled(SL_REVERBPRESET_NONE != preset);
385 }
386 }
387
388 //-----------------------------------------------------------------------------
android_prev_setPreset(const android::sp<android::AudioEffect> & pFx,uint16_t preset)389 android::status_t android_prev_setPreset(const android::sp<android::AudioEffect>& pFx,
390 uint16_t preset) {
391 android::status_t status = android_fx_setParam(pFx, REVERB_PARAM_PRESET,
392 PRESETREVERB_PARAM_SIZE_MAX, &preset, sizeof(uint16_t));
393 // enable the effect if the preset is different from SL_REVERBPRESET_NONE
394 pFx->setEnabled(SL_REVERBPRESET_NONE != preset);
395 return status;
396 }
397
398 //-----------------------------------------------------------------------------
android_prev_getPreset(const android::sp<android::AudioEffect> & pFx,uint16_t * preset)399 android::status_t android_prev_getPreset(const android::sp<android::AudioEffect>& pFx,
400 uint16_t* preset) {
401 return android_fx_getParam(pFx, REVERB_PARAM_PRESET, PRESETREVERB_PARAM_SIZE_MAX, preset,
402 sizeof(uint16_t));
403 }
404
405
406 //-----------------------------------------------------------------------------
android_erev_init(IEnvironmentalReverb * ier)407 void android_erev_init(IEnvironmentalReverb* ier) {
408 SL_LOGV("session is implicitly %d (aux effect)", AUDIO_SESSION_OUTPUT_MIX);
409
410 if (!android_fx_initEffectObj(AUDIO_SESSION_OUTPUT_MIX /*sessionId*/,
411 ier->mEnvironmentalReverbEffect, &ier->mEnvironmentalReverbDescriptor.type)) {
412 SL_LOGE("EnvironmentalReverb effect initialization failed");
413 return;
414 }
415
416 // enable env reverb: other SL ES effects have an explicit SetEnabled() function, and the
417 // preset reverb state depends on the selected preset.
418 ier->mEnvironmentalReverbEffect->setEnabled(true);
419
420 // initialize reverb properties
421 SLEnvironmentalReverbSettings properties;
422 if (android::NO_ERROR == android_erev_getParam(ier->mEnvironmentalReverbEffect,
423 REVERB_PARAM_PROPERTIES, &properties)) {
424 ier->mProperties = properties;
425 }
426 }
427
428 //-----------------------------------------------------------------------------
android_erev_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)429 android::status_t android_erev_setParam(const android::sp<android::AudioEffect>& pFx,
430 int32_t param, void *pValue) {
431
432 // given the size difference between a single reverb property and the whole set of reverb
433 // properties, select which max size to pass to avoid allocating too much memory
434 if (param == REVERB_PARAM_PROPERTIES) {
435 return android_fx_setParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_ALL,
436 pValue, erev_valueSize(param));
437 } else {
438 return android_fx_setParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_SINGLE,
439 pValue, erev_valueSize(param));
440 }
441 }
442
443 //-----------------------------------------------------------------------------
android_erev_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)444 android::status_t android_erev_getParam(const android::sp<android::AudioEffect>& pFx,
445 int32_t param, void *pValue) {
446
447 // given the size difference between a single reverb property and the whole set of reverb
448 // properties, select which max size to pass to avoid allocating too much memory
449 if (param == REVERB_PARAM_PROPERTIES) {
450 return android_fx_getParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_ALL,
451 pValue, erev_valueSize(param));
452 } else {
453 return android_fx_getParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_SINGLE,
454 pValue, erev_valueSize(param));
455 }
456 }
457
458 //-----------------------------------------------------------------------------
android_aec_init(audio_session_t sessionId,IAndroidAcousticEchoCancellation * iaec)459 void android_aec_init(audio_session_t sessionId, IAndroidAcousticEchoCancellation* iaec) {
460 SL_LOGV("android_aec_init on session %d", sessionId);
461
462 if (!android_fx_initEffectObj(sessionId, iaec->mAECEffect,
463 &iaec->mAECDescriptor.type)) {
464 SL_LOGE("AEC effect initialization failed");
465 return;
466 }
467 }
468
469 //-----------------------------------------------------------------------------
android_agc_init(audio_session_t sessionId,IAndroidAutomaticGainControl * iagc)470 void android_agc_init(audio_session_t sessionId, IAndroidAutomaticGainControl* iagc) {
471 SL_LOGV("android_agc_init on session %d", sessionId);
472
473 if (!android_fx_initEffectObj(sessionId, iagc->mAGCEffect,
474 &iagc->mAGCDescriptor.type)) {
475 SL_LOGE("AGC effect initialization failed");
476 return;
477 }
478 }
479
480 //-----------------------------------------------------------------------------
android_ns_init(audio_session_t sessionId,IAndroidNoiseSuppression * ins)481 void android_ns_init(audio_session_t sessionId, IAndroidNoiseSuppression* ins) {
482 SL_LOGV("android_ns_init on session %d", sessionId);
483
484 if (!android_fx_initEffectObj(sessionId, ins->mNSEffect,
485 &ins->mNSDescriptor.type)) {
486 SL_LOGE("NS effect initialization failed");
487 return;
488 }
489 }
490
491 //-----------------------------------------------------------------------------
492 /**
493 * pre-condition:
494 * ap != NULL
495 * for media players:
496 * ap->mAPlayer != 0
497 * ap->mTrackPlayer->mAudioTrack == 0
498 * for buffer queue players:
499 * ap->mAPlayer == 0
500 * ap->mTrackPlayer->mAudioTrack != 0 is optional; if no track yet then the setting is deferred
501 */
android_fxSend_attach(CAudioPlayer * ap,bool attach,const android::sp<android::AudioEffect> & pFx,SLmillibel sendLevel)502 android::status_t android_fxSend_attach(CAudioPlayer* ap, bool attach,
503 const android::sp<android::AudioEffect>& pFx, SLmillibel sendLevel) {
504
505 if (pFx == 0) {
506 return android::INVALID_OPERATION;
507 }
508
509 // There are 3 cases:
510 // mAPlayer != 0 && mAudioTrack == 0 means playing decoded audio
511 // mAPlayer == 0 && mAudioTrack != 0 means playing PCM audio
512 // mAPlayer == 0 && mAudioTrack == 0 means player not fully configured yet
513 // The asserts document and verify this.
514 auto audioTrack = ap->mTrackPlayer->getAudioTrack();
515 if (ap->mAPlayer != 0) {
516 assert(audioTrack == 0);
517 if (attach) {
518 ap->mAPlayer->attachAuxEffect(pFx->id());
519 ap->mAPlayer->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) );
520 } else {
521 ap->mAPlayer->attachAuxEffect(0);
522 }
523 return android::NO_ERROR;
524 }
525
526 if (audioTrack == 0) {
527 // the player doesn't have an AudioTrack at the moment, so store this info to use it
528 // when the AudioTrack becomes available
529 if (attach) {
530 ap->mAuxEffect = pFx;
531 } else {
532 ap->mAuxEffect.clear();
533 }
534 // we keep track of the send level, independently of the current audio player level
535 ap->mAuxSendLevel = sendLevel - ap->mVolume.mLevel;
536 return android::NO_ERROR;
537 }
538
539 if (attach) {
540 android::status_t status = audioTrack->attachAuxEffect(pFx->id());
541 //SL_LOGV("attachAuxEffect(%d) returned %d", pFx->id(), status);
542 if (android::NO_ERROR == status) {
543 status = audioTrack->setAuxEffectSendLevel(
544 sles_to_android_amplification(sendLevel) );
545 }
546 return status;
547 } else {
548 return audioTrack->attachAuxEffect(0);
549 }
550 }
551
552 //-----------------------------------------------------------------------------
553 /**
554 * pre-condition:
555 * ap != NULL
556 * ap->mOutputMix != NULL
557 */
android_fxSend_attachToAux(CAudioPlayer * ap,SLInterfaceID pUuid,SLboolean attach,SLmillibel sendLevel)558 SLresult android_fxSend_attachToAux(CAudioPlayer* ap, SLInterfaceID pUuid, SLboolean attach,
559 SLmillibel sendLevel) {
560 COutputMix *outputMix = CAudioPlayer_GetOutputMix(ap);
561 ssize_t index = outputMix->mAndroidEffect.mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
562
563 if (0 > index) {
564 SL_LOGE("invalid effect ID: no such effect attached to the OutputMix");
565 return SL_RESULT_PARAMETER_INVALID;
566 }
567
568 android::sp<android::AudioEffect> pFx =
569 outputMix->mAndroidEffect.mEffects->valueAt(index);
570 if (pFx == 0) {
571 return SL_RESULT_RESOURCE_ERROR;
572 }
573 if (android::NO_ERROR == android_fxSend_attach( ap, (bool) attach, pFx, sendLevel) ) {
574 return SL_RESULT_SUCCESS;
575 } else {
576 return SL_RESULT_RESOURCE_ERROR;
577 }
578
579 }
580
581 //-----------------------------------------------------------------------------
582 /**
583 * pre-condition:
584 * ap != NULL
585 * for media players:
586 * ap->mAPlayer != 0
587 * ap->mTrackPlayer->mAudioTrack == 0
588 * for buffer queue players:
589 * ap->mAPlayer == 0
590 * ap->mTrackPlayer->mAudioTrack != 0 is optional; if no track yet then the setting is deferred
591 */
android_fxSend_setSendLevel(CAudioPlayer * ap,SLmillibel sendLevel)592 android::status_t android_fxSend_setSendLevel(CAudioPlayer* ap, SLmillibel sendLevel) {
593 // we keep track of the send level, independently of the current audio player level
594 ap->mAuxSendLevel = sendLevel - ap->mVolume.mLevel;
595
596 auto audioTrack = ap->mTrackPlayer->getAudioTrack();
597 if (ap->mAPlayer != 0) {
598 assert(audioTrack == 0);
599 ap->mAPlayer->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) );
600 return android::NO_ERROR;
601 }
602
603 if (audioTrack == 0) {
604 return android::NO_ERROR;
605 }
606
607 return audioTrack->setAuxEffectSendLevel(
608 sles_to_android_amplification(sendLevel) );
609 }
610
611 //-----------------------------------------------------------------------------
android_fx_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,uint32_t paramSizeMax,void * pValue,uint32_t valueSize)612 android::status_t android_fx_setParam(const android::sp<android::AudioEffect>& pFx,
613 int32_t param, uint32_t paramSizeMax, void *pValue, uint32_t valueSize)
614 {
615
616 android::status_t status;
617 uint32_t buf32[(paramSizeMax - 1) / sizeof(uint32_t) + 1];
618 effect_param_t *p = (effect_param_t *)buf32;
619
620 p->psize = sizeof(int32_t);
621 *(int32_t *)p->data = param;
622 p->vsize = valueSize;
623 memcpy(p->data + p->psize, pValue, p->vsize);
624 status = pFx->setParameter(p);
625 if (android::NO_ERROR == status) {
626 status = p->status;
627 }
628 return status;
629 }
630
631
632 //-----------------------------------------------------------------------------
android_fx_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,uint32_t paramSizeMax,void * pValue,uint32_t valueSize)633 android::status_t android_fx_getParam(const android::sp<android::AudioEffect>& pFx,
634 int32_t param, uint32_t paramSizeMax, void *pValue, uint32_t valueSize)
635 {
636 android::status_t status;
637 uint32_t buf32[(paramSizeMax - 1) / sizeof(uint32_t) + 1];
638 effect_param_t *p = (effect_param_t *)buf32;
639
640 p->psize = sizeof(int32_t);
641 *(int32_t *)p->data = param;
642 p->vsize = valueSize;
643 status = pFx->getParameter(p);
644 if (android::NO_ERROR == status) {
645 status = p->status;
646 if (android::NO_ERROR == status) {
647 memcpy(pValue, p->data + p->psize, p->vsize);
648 }
649 }
650
651 return status;
652 }
653
654
655 //-----------------------------------------------------------------------------
android_fx_statusToResult(android::status_t status)656 SLresult android_fx_statusToResult(android::status_t status) {
657
658 if ((android::INVALID_OPERATION == status) || (android::DEAD_OBJECT == status)) {
659 return SL_RESULT_CONTROL_LOST;
660 } else {
661 return SL_RESULT_SUCCESS;
662 }
663 }
664
665
666 //-----------------------------------------------------------------------------
android_fx_initEffectObj(audio_session_t sessionId,android::sp<android::AudioEffect> & effect,const effect_uuid_t * type)667 bool android_fx_initEffectObj(audio_session_t sessionId, android::sp<android::AudioEffect>& effect,
668 const effect_uuid_t *type) {
669 //SL_LOGV("android_fx_initEffectObj on session %d", sessionId);
670
671 // TODO b/182392769: use attribution source util
672 AttributionSourceState attributionSource;
673 attributionSource.uid = VALUE_OR_FATAL(android::legacy2aidl_uid_t_int32_t(getuid()));
674 attributionSource.pid = VALUE_OR_FATAL(android::legacy2aidl_pid_t_int32_t(getpid()));
675 attributionSource.token = android::sp<android::BBinder>::make();
676
677 effect = android::sp<android::AudioEffect>::make(attributionSource);
678
679 effect->set(type, EFFECT_UUID_NULL,
680 0,// priority
681 nullptr,// effect callback
682 sessionId,// session ID
683 0); // output
684
685 android::status_t status = effect->initCheck();
686 if (android::NO_ERROR != status) {
687 effect.clear();
688 SL_LOGE("Effect initCheck() returned %d", status);
689 return false;
690 }
691
692 return true;
693 }
694
695
696 //-----------------------------------------------------------------------------
android_fx_initEffectDescriptor(const SLInterfaceID effectId,effect_descriptor_t * fxDescrLoc)697 bool android_fx_initEffectDescriptor(const SLInterfaceID effectId,
698 effect_descriptor_t* fxDescrLoc) {
699 uint32_t numEffects = 0;
700 effect_descriptor_t descriptor;
701 bool foundEffect = false;
702
703 // any effects?
704 android::status_t res = android::AudioEffect::queryNumberEffects(&numEffects);
705 if (android::NO_ERROR != res) {
706 SL_LOGE("unable to find any effects.");
707 goto effectError;
708 }
709
710 // request effect in the effects?
711 for (uint32_t i=0 ; i < numEffects ; i++) {
712 res = android::AudioEffect::queryEffect(i, &descriptor);
713 if ((android::NO_ERROR == res) &&
714 (0 == memcmp(effectId, &descriptor.type, sizeof(effect_uuid_t)))) {
715 SL_LOGV("found effect %d %s", i, descriptor.name);
716 foundEffect = true;
717 break;
718 }
719 }
720 if (foundEffect) {
721 memcpy(fxDescrLoc, &descriptor, sizeof(effect_descriptor_t));
722 } else {
723 SL_LOGE("unable to find an implementation for the requested effect.");
724 goto effectError;
725 }
726
727 return true;
728
729 effectError:
730 // the requested effect wasn't found
731 memset(fxDescrLoc, 0, sizeof(effect_descriptor_t));
732
733 return false;
734 }
735
736 //-----------------------------------------------------------------------------
android_genericFx_queryNumEffects(SLuint32 * pNumSupportedAudioEffects)737 SLresult android_genericFx_queryNumEffects(SLuint32 *pNumSupportedAudioEffects) {
738
739 if (NULL == pNumSupportedAudioEffects) {
740 return SL_RESULT_PARAMETER_INVALID;
741 }
742
743 android::status_t status =
744 android::AudioEffect::queryNumberEffects((uint32_t*)pNumSupportedAudioEffects);
745
746 SLresult result = SL_RESULT_SUCCESS;
747 switch (status) {
748 case android::NO_ERROR:
749 result = SL_RESULT_SUCCESS;
750 break;
751 case android::PERMISSION_DENIED:
752 result = SL_RESULT_PERMISSION_DENIED;
753 break;
754 case android::NO_INIT:
755 result = SL_RESULT_RESOURCE_ERROR;
756 break;
757 case android::BAD_VALUE:
758 result = SL_RESULT_PARAMETER_INVALID;
759 break;
760 default:
761 result = SL_RESULT_INTERNAL_ERROR;
762 SL_LOGE("received invalid status %d from AudioEffect::queryNumberEffects()", status);
763 break;
764 }
765 return result;
766 }
767
768
769 //-----------------------------------------------------------------------------
android_genericFx_queryEffect(SLuint32 index,effect_descriptor_t * pDescriptor)770 SLresult android_genericFx_queryEffect(SLuint32 index, effect_descriptor_t* pDescriptor) {
771
772 if (NULL == pDescriptor) {
773 return SL_RESULT_PARAMETER_INVALID;
774 }
775
776 android::status_t status =
777 android::AudioEffect::queryEffect(index, pDescriptor);
778
779 SLresult result = SL_RESULT_SUCCESS;
780 if (android::NO_ERROR != status) {
781 switch (status) {
782 case android::PERMISSION_DENIED:
783 result = SL_RESULT_PERMISSION_DENIED;
784 break;
785 case android::NO_INIT:
786 case android::INVALID_OPERATION:
787 result = SL_RESULT_RESOURCE_ERROR;
788 break;
789 case android::BAD_VALUE:
790 result = SL_RESULT_PARAMETER_INVALID;
791 break;
792 default:
793 result = SL_RESULT_INTERNAL_ERROR;
794 SL_LOGE("received invalid status %d from AudioEffect::queryNumberEffects()", status);
795 break;
796 }
797 // an error occurred, reset the effect descriptor
798 memset(pDescriptor, 0, sizeof(effect_descriptor_t));
799 }
800
801 return result;
802 }
803
804
805 //-----------------------------------------------------------------------------
android_genericFx_createEffect(IAndroidEffect * iae,SLInterfaceID pUuid,audio_session_t sessionId)806 SLresult android_genericFx_createEffect(IAndroidEffect* iae, SLInterfaceID pUuid,
807 audio_session_t sessionId)
808 {
809
810 SLresult result = SL_RESULT_SUCCESS;
811
812 // does this effect already exist?
813 if (0 <= iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid))) {
814 return result;
815 }
816
817 // create new effect
818 // TODO b/182392769: use attribution source util
819 AttributionSourceState attributionSource;
820 attributionSource.uid = VALUE_OR_FATAL(android::legacy2aidl_uid_t_int32_t(getuid()));
821 attributionSource.pid = VALUE_OR_FATAL(android::legacy2aidl_pid_t_int32_t(getpid()));
822 attributionSource.token = android::sp<android::BBinder>::make();
823
824 const auto pFx = android::sp<android::AudioEffect>::make(attributionSource);
825
826 pFx->set(NULL, // not using type to create effect
827 (const effect_uuid_t*)pUuid,
828 0,// priority
829 nullptr,// effect callback
830 sessionId,
831 0 );// output
832
833 // verify effect was successfully created before storing it
834 android::status_t status = pFx->initCheck();
835 if (android::NO_ERROR != status) {
836 SL_LOGE("AudioEffect initCheck() returned %d, effect will not be stored", status);
837 result = SL_RESULT_RESOURCE_ERROR;
838 } else {
839 SL_LOGV("AudioEffect successfully created on session %d", sessionId);
840 iae->mEffects->add(KEY_FROM_GUID(pUuid), pFx);
841 }
842
843 return result;
844 }
845
846
847 //-----------------------------------------------------------------------------
android_genericFx_releaseEffect(IAndroidEffect * iae,SLInterfaceID pUuid)848 SLresult android_genericFx_releaseEffect(IAndroidEffect* iae, SLInterfaceID pUuid) {
849
850 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
851
852 if (0 > index) {
853 return SL_RESULT_PARAMETER_INVALID;
854 } else {
855 iae->mEffects->removeItem(index);
856 return SL_RESULT_SUCCESS;
857 }
858 }
859
860
861 //-----------------------------------------------------------------------------
android_genericFx_setEnabled(IAndroidEffect * iae,SLInterfaceID pUuid,SLboolean enabled)862 SLresult android_genericFx_setEnabled(IAndroidEffect* iae, SLInterfaceID pUuid, SLboolean enabled) {
863
864 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
865
866 if (0 > index) {
867 return SL_RESULT_PARAMETER_INVALID;
868 } else {
869 android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index);
870 android::status_t status = pFx->setEnabled(SL_BOOLEAN_TRUE == enabled);
871 return android_fx_statusToResult(status);
872 }
873 }
874
875
876 //-----------------------------------------------------------------------------
android_genericFx_isEnabled(IAndroidEffect * iae,SLInterfaceID pUuid,SLboolean * pEnabled)877 SLresult android_genericFx_isEnabled(IAndroidEffect* iae, SLInterfaceID pUuid, SLboolean *pEnabled)
878 {
879 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
880
881 if (0 > index) {
882 return SL_RESULT_PARAMETER_INVALID;
883 } else {
884 android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index);
885 *pEnabled = (SLboolean) pFx->getEnabled();
886 return SL_RESULT_SUCCESS;
887 }
888 }
889
890
891 //-----------------------------------------------------------------------------
android_genericFx_sendCommand(IAndroidEffect * iae,SLInterfaceID pUuid,SLuint32 command,SLuint32 commandSize,void * pCommandData,SLuint32 * replySize,void * pReplyData)892 SLresult android_genericFx_sendCommand(IAndroidEffect* iae, SLInterfaceID pUuid,
893 SLuint32 command, SLuint32 commandSize, void* pCommandData,
894 SLuint32 *replySize, void *pReplyData) {
895
896 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
897
898 if (0 > index) {
899 return SL_RESULT_PARAMETER_INVALID;
900 } else {
901 android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index);
902 android::status_t status = pFx->command(
903 (uint32_t) command,
904 (uint32_t) commandSize,
905 pCommandData,
906 (uint32_t*)replySize,
907 pReplyData);
908 if (android::BAD_VALUE == status) {
909 return SL_RESULT_PARAMETER_INVALID;
910 } else {
911 return SL_RESULT_SUCCESS;
912 }
913 }
914 }
915
916 //-----------------------------------------------------------------------------
917 /**
918 * returns true if the given effect id is present in the AndroidEffect interface
919 */
android_genericFx_hasEffect(IAndroidEffect * iae,SLInterfaceID pUuid)920 bool android_genericFx_hasEffect(IAndroidEffect* iae, SLInterfaceID pUuid) {
921 return( 0 <= iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid)));
922 }
923
924 //-----------------------------------------------------------------------------
925 static const int AEC_PARAM_SIZE_MAX = sizeof(effect_param_t) + (2 * sizeof(int32_t));
926 /**
927 * returns the size in bytes of the value of each acoustic echo cancellation parameter
928 */
aec_valueSize(int32_t param)929 uint32_t aec_valueSize(int32_t param) {
930 uint32_t size;
931 switch (param) {
932 case AEC_PARAM_ECHO_DELAY:
933 size = sizeof(int32_t);
934 break;
935 default:
936 size = sizeof(int32_t);
937 SL_LOGE("Trying to access an unknown Acoustic Echo Cancellation parameter %d", param);
938 break;
939 }
940
941 return size;
942 }
943
android_aec_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)944 android::status_t android_aec_setParam(const android::sp<android::AudioEffect>& pFx,
945 int32_t param, void *pValue) {
946 return android_fx_setParam(pFx, param, AEC_PARAM_SIZE_MAX,
947 pValue, aec_valueSize(param));
948 }
949
android_aec_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)950 android::status_t android_aec_getParam(const android::sp<android::AudioEffect>& pFx,
951 int32_t param, void *pValue) {
952 return android_fx_getParam(pFx, param, AEC_PARAM_SIZE_MAX,
953 pValue, aec_valueSize(param));
954 }
955
956 //-----------------------------------------------------------------------------
957 static const int AGC_PARAM_SIZE_MAX = sizeof(effect_param_t) + (2 * sizeof(int16_t)) + sizeof(bool);
958 /**
959 * returns the size in bytes of the value of each automatic gain control parameter
960 */
agc_valueSize(int32_t param)961 uint32_t agc_valueSize(int32_t param) {
962 uint32_t size;
963 switch (param) {
964 case AGC_PARAM_TARGET_LEVEL:
965 case AGC_PARAM_COMP_GAIN:
966 size = sizeof(int16_t);
967 break;
968 case AGC_PARAM_LIMITER_ENA:
969 size = sizeof(bool);
970 break;
971 default:
972 size = sizeof(int32_t);
973 SL_LOGE("Trying to access an unknown Automatic Gain Control parameter %d", param);
974 break;
975 }
976
977 return size;
978 }
979
android_agc_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)980 android::status_t android_agc_setParam(const android::sp<android::AudioEffect>& pFx,
981 int32_t param, void *pValue) {
982 return android_fx_setParam(pFx, param, AGC_PARAM_SIZE_MAX,
983 pValue, agc_valueSize(param));
984 }
985
android_agc_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)986 android::status_t android_agc_getParam(const android::sp<android::AudioEffect>& pFx,
987 int32_t param, void *pValue) {
988 return android_fx_getParam(pFx, param, AGC_PARAM_SIZE_MAX,
989 pValue, agc_valueSize(param));
990 }
991
992 //-----------------------------------------------------------------------------
993 static const int NS_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
994 /**
995 * returns the size in bytes of the value of each noise suppression parameter
996 */
ns_valueSize(int32_t param)997 uint32_t ns_valueSize(int32_t param) {
998 uint32_t size;
999 switch (param) {
1000 case NS_PARAM_LEVEL:
1001 size = sizeof(int32_t);
1002 break;
1003 default:
1004 size = sizeof(int32_t);
1005 SL_LOGE("Trying to access an unknown Noise suppression parameter %d", param);
1006 break;
1007 }
1008
1009 return size;
1010 }
1011
android_ns_setParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)1012 android::status_t android_ns_setParam(const android::sp<android::AudioEffect>& pFx,
1013 int32_t param, void *pValue)
1014 {
1015 return android_fx_setParam(pFx, param, NS_PARAM_SIZE_MAX,
1016 pValue, ns_valueSize(param));
1017 }
1018
android_ns_getParam(const android::sp<android::AudioEffect> & pFx,int32_t param,void * pValue)1019 android::status_t android_ns_getParam(const android::sp<android::AudioEffect>& pFx,
1020 int32_t param, void *pValue)
1021 {
1022 return android_fx_getParam(pFx, param, NS_PARAM_SIZE_MAX,
1023 pValue, ns_valueSize(param));
1024 }
1025