1 /* 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 /* Automatic Gain Control implementation */ 18 #include "sles_allinclusive.h" 19 20 #include <media/EffectsFactoryApi.h> 21 22 #include <audio_effects/effect_agc.h> 23 24 /** 25 * returns true if this interface is not associated with an initialized AGC effect 26 */ 27 static inline bool NO_AUTOGAIN(IAndroidAutomaticGainControl* v) { 28 return (v->mAGCEffect == 0); 29 } 30 31 SLresult IAndroidAutomaticGainControl_SetEnabled(SLAndroidAutomaticGainControlItf self, 32 SLboolean enabled) 33 { 34 SL_ENTER_INTERFACE 35 36 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 37 interface_lock_exclusive(thiz); 38 thiz->mEnabled = (SLboolean) enabled; 39 if (NO_AUTOGAIN(thiz)) { 40 result = SL_RESULT_CONTROL_LOST; 41 } else { 42 android::status_t status = thiz->mAGCEffect->setEnabled((bool) thiz->mEnabled); 43 result = android_fx_statusToResult(status); 44 } 45 interface_unlock_exclusive(thiz); 46 47 SL_LEAVE_INTERFACE 48 } 49 50 SLresult IAndroidAutomaticGainControl_IsEnabled(SLAndroidAutomaticGainControlItf self, 51 SLboolean *pEnabled) 52 { 53 SL_ENTER_INTERFACE 54 55 if (NULL == pEnabled) { 56 result = SL_RESULT_PARAMETER_INVALID; 57 } else { 58 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 59 interface_lock_exclusive(thiz); 60 SLboolean enabled = thiz->mEnabled; 61 if (NO_AUTOGAIN(thiz)) { 62 result = SL_RESULT_CONTROL_LOST; 63 } else { 64 *pEnabled = (SLboolean) thiz->mAGCEffect->getEnabled(); 65 result = SL_RESULT_SUCCESS; 66 } 67 interface_unlock_exclusive(thiz); 68 } 69 SL_LEAVE_INTERFACE 70 } 71 72 SLresult IAndroidAutomaticGainControl_IsAvailable(SLAndroidAutomaticGainControlItf self, 73 SLboolean *pEnabled) 74 { 75 SL_ENTER_INTERFACE 76 77 *pEnabled = false; 78 79 uint32_t numEffects = 0; 80 int ret = EffectQueryNumberEffects(&numEffects); 81 if (ret != 0) { 82 ALOGE("IAndroidAutomaticGainControl_IsAvailable() error %d querying number of effects", 83 ret); 84 result = SL_RESULT_FEATURE_UNSUPPORTED; 85 } else { 86 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects); 87 88 effect_descriptor_t fxDesc; 89 for (uint32_t i = 0 ; i < numEffects ; i++) { 90 if (EffectQueryEffect(i, &fxDesc) == 0) { 91 ALOGV("effect %d is called %s", i, fxDesc.name); 92 if (memcmp(&fxDesc.type, SL_IID_ANDROIDAUTOMATICGAINCONTROL, 93 sizeof(effect_uuid_t)) == 0) { 94 ALOGI("found effect \"%s\" from %s", fxDesc.name, fxDesc.implementor); 95 *pEnabled = true; 96 break; 97 } 98 } 99 } 100 result = SL_RESULT_SUCCESS; 101 } 102 SL_LEAVE_INTERFACE 103 } 104 105 static const struct SLAndroidAutomaticGainControlItf_ IAndroidAutomaticGainControl_Itf = { 106 IAndroidAutomaticGainControl_SetEnabled, 107 IAndroidAutomaticGainControl_IsEnabled, 108 IAndroidAutomaticGainControl_IsAvailable 109 }; 110 111 void IAndroidAutomaticGainControl_init(void *self) 112 { 113 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 114 thiz->mItf = &IAndroidAutomaticGainControl_Itf; 115 thiz->mEnabled = SL_BOOLEAN_FALSE; 116 memset(&thiz->mAGCDescriptor, 0, sizeof(effect_descriptor_t)); 117 // placement new (explicit constructor) 118 (void) new (&thiz->mAGCEffect) android::sp<android::AudioEffect>(); 119 } 120 121 void IAndroidAutomaticGainControl_deinit(void *self) 122 { 123 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 124 // explicit destructor 125 thiz->mAGCEffect.~sp(); 126 } 127 128 bool IAndroidAutomaticGainControl_Expose(void *self) 129 { 130 IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self; 131 if (!android_fx_initEffectDescriptor(SL_IID_ANDROIDAUTOMATICGAINCONTROL, 132 &thiz->mAGCDescriptor)) { 133 SL_LOGE("Automatic Gain Control initialization failed."); 134 return false; 135 } 136 return true; 137 } 138