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