frameworks: av: services: audiopolicy: Added AudioPolicyEffects

Manage pre- and postprocessing effects defined in audio_effects.conf

Change-Id: Idea1478e3d434f8ff66907adb3c77663db78d583
diff --git a/services/audiopolicy/AudioPolicyService.cpp b/services/audiopolicy/AudioPolicyService.cpp
old mode 100644
new mode 100755
index 93fab11..9435797
--- a/services/audiopolicy/AudioPolicyService.cpp
+++ b/services/audiopolicy/AudioPolicyService.cpp
@@ -40,8 +40,6 @@
 #include <system/audio.h>
 #include <system/audio_policy.h>
 #include <hardware/audio_policy.h>
-#include <audio_effects/audio_effects_conf.h>
-#include <media/AudioParameter.h>
 
 namespace android {
 
@@ -111,12 +109,8 @@
     mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
 #endif
 
-    // load audio pre processing modules
-    if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
-        loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
-    } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
-        loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
-    }
+    // load audio processing modules
+    mAudioPolicyEffects = new AudioPolicyEffects();
 }
 
 AudioPolicyService::~AudioPolicyService()
@@ -125,18 +119,6 @@
     mAudioCommandThread->exit();
     mOutputCommandThread->exit();
 
-    // release audio pre processing resources
-    for (size_t i = 0; i < mInputSources.size(); i++) {
-        delete mInputSources.valueAt(i);
-    }
-    mInputSources.clear();
-
-    for (size_t i = 0; i < mInputs.size(); i++) {
-        mInputs.valueAt(i)->mEffects.clear();
-        delete mInputs.valueAt(i);
-    }
-    mInputs.clear();
-
 #ifdef USE_LEGACY_AUDIO_POLICY
     if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
         mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
@@ -150,6 +132,7 @@
 #endif
 
     mNotificationClients.clear();
+    mAudioPolicyEffects.clear();
 }
 
 // A notification client is always registered by AudioSystem when the client process
@@ -353,14 +336,6 @@
     return NO_ERROR;
 }
 
-void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
-{
-    const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
-    for (size_t i = 0; i < fxVector.size(); i++) {
-        fxVector.itemAt(i)->setEnabled(enabled);
-    }
-}
-
 status_t AudioPolicyService::onTransact(
         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
 {
@@ -936,304 +911,6 @@
     return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
 }
 
-// ----------------------------------------------------------------------------
-// Audio pre-processing configuration
-// ----------------------------------------------------------------------------
-
-/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
-    MIC_SRC_TAG,
-    VOICE_UL_SRC_TAG,
-    VOICE_DL_SRC_TAG,
-    VOICE_CALL_SRC_TAG,
-    CAMCORDER_SRC_TAG,
-    VOICE_REC_SRC_TAG,
-    VOICE_COMM_SRC_TAG
-};
-
-// returns the audio_source_t enum corresponding to the input source name or
-// AUDIO_SOURCE_CNT is no match found
-audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
-{
-    int i;
-    for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
-        if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
-            ALOGV("inputSourceNameToEnum found source %s %d", name, i);
-            break;
-        }
-    }
-    return (audio_source_t)i;
-}
-
-size_t AudioPolicyService::growParamSize(char *param,
-                                         size_t size,
-                                         size_t *curSize,
-                                         size_t *totSize)
-{
-    // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
-    size_t pos = ((*curSize - 1 ) / size + 1) * size;
-
-    if (pos + size > *totSize) {
-        while (pos + size > *totSize) {
-            *totSize += ((*totSize + 7) / 8) * 4;
-        }
-        param = (char *)realloc(param, *totSize);
-    }
-    *curSize = pos + size;
-    return pos;
-}
-
-size_t AudioPolicyService::readParamValue(cnode *node,
-                                          char *param,
-                                          size_t *curSize,
-                                          size_t *totSize)
-{
-    if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
-        size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
-        *(short *)((char *)param + pos) = (short)atoi(node->value);
-        ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
-        return sizeof(short);
-    } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
-        size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
-        *(int *)((char *)param + pos) = atoi(node->value);
-        ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
-        return sizeof(int);
-    } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
-        size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
-        *(float *)((char *)param + pos) = (float)atof(node->value);
-        ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
-        return sizeof(float);
-    } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
-        size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
-        if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
-            *(bool *)((char *)param + pos) = false;
-        } else {
-            *(bool *)((char *)param + pos) = true;
-        }
-        ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
-        return sizeof(bool);
-    } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
-        size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
-        if (*curSize + len + 1 > *totSize) {
-            *totSize = *curSize + len + 1;
-            param = (char *)realloc(param, *totSize);
-        }
-        strncpy(param + *curSize, node->value, len);
-        *curSize += len;
-        param[*curSize] = '\0';
-        ALOGV("readParamValue() reading string %s", param + *curSize - len);
-        return len;
-    }
-    ALOGW("readParamValue() unknown param type %s", node->name);
-    return 0;
-}
-
-effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
-{
-    cnode *param;
-    cnode *value;
-    size_t curSize = sizeof(effect_param_t);
-    size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
-    effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
-
-    param = config_find(root, PARAM_TAG);
-    value = config_find(root, VALUE_TAG);
-    if (param == NULL && value == NULL) {
-        // try to parse simple parameter form {int int}
-        param = root->first_child;
-        if (param != NULL) {
-            // Note: that a pair of random strings is read as 0 0
-            int *ptr = (int *)fx_param->data;
-            int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
-            ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
-            *ptr++ = atoi(param->name);
-            *ptr = atoi(param->value);
-            fx_param->psize = sizeof(int);
-            fx_param->vsize = sizeof(int);
-            return fx_param;
-        }
-    }
-    if (param == NULL || value == NULL) {
-        ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
-        goto error;
-    }
-
-    fx_param->psize = 0;
-    param = param->first_child;
-    while (param) {
-        ALOGV("loadEffectParameter() reading param of type %s", param->name);
-        size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
-        if (size == 0) {
-            goto error;
-        }
-        fx_param->psize += size;
-        param = param->next;
-    }
-
-    // align start of value field on 32 bit boundary
-    curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
-
-    fx_param->vsize = 0;
-    value = value->first_child;
-    while (value) {
-        ALOGV("loadEffectParameter() reading value of type %s", value->name);
-        size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
-        if (size == 0) {
-            goto error;
-        }
-        fx_param->vsize += size;
-        value = value->next;
-    }
-
-    return fx_param;
-
-error:
-    free(fx_param);
-    return NULL;
-}
-
-void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
-{
-    cnode *node = root->first_child;
-    while (node) {
-        ALOGV("loadEffectParameters() loading param %s", node->name);
-        effect_param_t *param = loadEffectParameter(node);
-        if (param == NULL) {
-            node = node->next;
-            continue;
-        }
-        params.add(param);
-        node = node->next;
-    }
-}
-
-AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
-                                                            cnode *root,
-                                                            const Vector <EffectDesc *>& effects)
-{
-    cnode *node = root->first_child;
-    if (node == NULL) {
-        ALOGW("loadInputSource() empty element %s", root->name);
-        return NULL;
-    }
-    InputSourceDesc *source = new InputSourceDesc();
-    while (node) {
-        size_t i;
-        for (i = 0; i < effects.size(); i++) {
-            if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
-                ALOGV("loadInputSource() found effect %s in list", node->name);
-                break;
-            }
-        }
-        if (i == effects.size()) {
-            ALOGV("loadInputSource() effect %s not in list", node->name);
-            node = node->next;
-            continue;
-        }
-        EffectDesc *effect = new EffectDesc(*effects[i]);   // deep copy
-        loadEffectParameters(node, effect->mParams);
-        ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
-        source->mEffects.add(effect);
-        node = node->next;
-    }
-    if (source->mEffects.size() == 0) {
-        ALOGW("loadInputSource() no valid effects found in source %s", root->name);
-        delete source;
-        return NULL;
-    }
-    return source;
-}
-
-status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
-{
-    cnode *node = config_find(root, PREPROCESSING_TAG);
-    if (node == NULL) {
-        return -ENOENT;
-    }
-    node = node->first_child;
-    while (node) {
-        audio_source_t source = inputSourceNameToEnum(node->name);
-        if (source == AUDIO_SOURCE_CNT) {
-            ALOGW("loadInputSources() invalid input source %s", node->name);
-            node = node->next;
-            continue;
-        }
-        ALOGV("loadInputSources() loading input source %s", node->name);
-        InputSourceDesc *desc = loadInputSource(node, effects);
-        if (desc == NULL) {
-            node = node->next;
-            continue;
-        }
-        mInputSources.add(source, desc);
-        node = node->next;
-    }
-    return NO_ERROR;
-}
-
-AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
-{
-    cnode *node = config_find(root, UUID_TAG);
-    if (node == NULL) {
-        return NULL;
-    }
-    effect_uuid_t uuid;
-    if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
-        ALOGW("loadEffect() invalid uuid %s", node->value);
-        return NULL;
-    }
-    return new EffectDesc(root->name, uuid);
-}
-
-status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
-{
-    cnode *node = config_find(root, EFFECTS_TAG);
-    if (node == NULL) {
-        return -ENOENT;
-    }
-    node = node->first_child;
-    while (node) {
-        ALOGV("loadEffects() loading effect %s", node->name);
-        EffectDesc *effect = loadEffect(node);
-        if (effect == NULL) {
-            node = node->next;
-            continue;
-        }
-        effects.add(effect);
-        node = node->next;
-    }
-    return NO_ERROR;
-}
-
-status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
-{
-    cnode *root;
-    char *data;
-
-    data = (char *)load_file(path, NULL);
-    if (data == NULL) {
-        return -ENODEV;
-    }
-    root = config_node("", "");
-    config_load(root, data);
-
-    Vector <EffectDesc *> effects;
-    loadEffects(root, effects);
-    loadInputSources(root, effects);
-
-    // delete effects to fix memory leak.
-    // as effects is local var and valgrind would treat this as memory leak
-    // and although it only did in mediaserver init, but free it in case mediaserver reboot
-    size_t i;
-    for (i = 0; i < effects.size(); i++) {
-      delete effects[i];
-    }
-
-    config_free(root);
-    free(root);
-    free(data);
-
-    return NO_ERROR;
-}
-
 extern "C" {
 audio_module_handle_t aps_load_hw_module(void *service __unused,
                                              const char *name);