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 <system/audio_effects/effect_agc.h>
21
22/**
23 * returns true if this interface is not associated with an initialized AGC effect
24 */
25static inline bool NO_AUTOGAIN(IAndroidAutomaticGainControl* v) {
26    return (v->mAGCEffect == 0);
27}
28
29SLresult IAndroidAutomaticGainControl_SetEnabled(SLAndroidAutomaticGainControlItf self,
30                                                 SLboolean enabled)
31{
32    SL_ENTER_INTERFACE
33
34    IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
35    interface_lock_exclusive(thiz);
36    thiz->mEnabled = (SLboolean) enabled;
37    if (NO_AUTOGAIN(thiz)) {
38        result = SL_RESULT_CONTROL_LOST;
39    } else {
40        android::status_t status = thiz->mAGCEffect->setEnabled((bool) thiz->mEnabled);
41        result = android_fx_statusToResult(status);
42    }
43    interface_unlock_exclusive(thiz);
44
45    SL_LEAVE_INTERFACE
46}
47
48SLresult IAndroidAutomaticGainControl_IsEnabled(SLAndroidAutomaticGainControlItf self,
49                                                SLboolean *pEnabled)
50{
51    SL_ENTER_INTERFACE
52
53    if (NULL == pEnabled) {
54        result = SL_RESULT_PARAMETER_INVALID;
55    } else {
56        IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
57        interface_lock_exclusive(thiz);
58        if (NO_AUTOGAIN(thiz)) {
59            result = SL_RESULT_CONTROL_LOST;
60        } else {
61            *pEnabled = (SLboolean) thiz->mAGCEffect->getEnabled();
62            result = SL_RESULT_SUCCESS;
63        }
64        interface_unlock_exclusive(thiz);
65    }
66    SL_LEAVE_INTERFACE
67}
68
69static const struct SLAndroidAutomaticGainControlItf_ IAndroidAutomaticGainControl_Itf = {
70    IAndroidAutomaticGainControl_SetEnabled,
71    IAndroidAutomaticGainControl_IsEnabled
72};
73
74void IAndroidAutomaticGainControl_init(void *self)
75{
76    IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
77    thiz->mItf = &IAndroidAutomaticGainControl_Itf;
78    thiz->mEnabled = SL_BOOLEAN_FALSE;
79    memset(&thiz->mAGCDescriptor, 0, sizeof(effect_descriptor_t));
80    // placement new (explicit constructor)
81    (void) new (&thiz->mAGCEffect) android::sp<android::AudioEffect>();
82}
83
84void IAndroidAutomaticGainControl_deinit(void *self)
85{
86    IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
87    // explicit destructor
88    thiz->mAGCEffect.~sp();
89}
90
91bool IAndroidAutomaticGainControl_Expose(void *self)
92{
93    IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
94    if (!android_fx_initEffectDescriptor(SL_IID_ANDROIDAUTOMATICGAINCONTROL,
95                                         &thiz->mAGCDescriptor)) {
96        SL_LOGE("Automatic Gain Control initialization failed.");
97        return false;
98    }
99    return true;
100}
101