1948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent/*
2948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * Copyright (C) 2010 The Android Open Source Project
3948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent *
4948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
5948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * you may not use this file except in compliance with the License.
6948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * You may obtain a copy of the License at
7948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent *
8948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
9948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent *
10948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * Unless required by applicable law or agreed to in writing, software
11948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
12948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * See the License for the specific language governing permissions and
14948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent * limitations under the License.
15948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent */
16948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
17948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#include <stdio.h>
18948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
19948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent//#define LOG_NDEBUG 0
20948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define LOG_TAG "AudioEffects-JNI"
21948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
22948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#include <utils/Log.h>
23948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#include <nativehelper/jni.h>
24948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#include <nativehelper/JNIHelp.h>
25948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#include <android_runtime/AndroidRuntime.h>
26948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#include "media/AudioEffect.h"
27948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
28fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov#include <ScopedUtfChars.h>
29fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov
30948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentusing namespace android;
31948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
32948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_SUCCESS                      0
33948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_ERROR                       -1
34948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_ERROR_ALREADY_EXISTS        -2
35948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_ERROR_NO_INIT               -3
36948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_ERROR_BAD_VALUE             -4
37948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_ERROR_INVALID_OPERATION     -5
38948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_ERROR_NO_MEMORY             -6
39948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent#define AUDIOEFFECT_ERROR_DEAD_OBJECT           -7
40948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
41948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
421a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurentstatic const char* const kClassPathName = "android/media/audiofx/AudioEffect";
43948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
44948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstruct fields_t {
45948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // these fields provide access from C++ to the...
46948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jclass    clazzEffect;          // AudioEffect class
47948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jmethodID midPostNativeEvent;   // event post callback method
48948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jfieldID  fidNativeAudioEffect; // stores in Java the native AudioEffect object
49948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jfieldID  fidJniData;           // stores in Java additional resources used by the native AudioEffect
50948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jclass    clazzDesc;            // AudioEffect.Descriptor class
51948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jmethodID midDescCstor;         // AudioEffect.Descriptor class constructor
52948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent};
53948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic fields_t fields;
54948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
55948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstruct effect_callback_cookie {
56948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jclass      audioEffect_class;  // AudioEffect class
57948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jobject     audioEffect_ref;    // AudioEffect object instance
58948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent };
59948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
60948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
61948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentclass AudioEffectJniStorage {
62948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    public:
63948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        effect_callback_cookie mCallbackData;
64948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
65948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    AudioEffectJniStorage() {
66948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
67948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
68948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    ~AudioEffectJniStorage() {
69948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
70948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
71948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent};
72948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
73948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
74948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jint translateError(int code) {
75948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    switch(code) {
76948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case NO_ERROR:
77948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_SUCCESS;
78948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case ALREADY_EXISTS:
79948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_ALREADY_EXISTS;
80948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case NO_INIT:
81948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_NO_INIT;
82948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case BAD_VALUE:
83948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_BAD_VALUE;
84948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case INVALID_OPERATION:
85948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_INVALID_OPERATION;
86948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case NO_MEMORY:
87948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_NO_MEMORY;
88948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case DEAD_OBJECT:
89948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_DEAD_OBJECT;
90948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    default:
91948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR;
92948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
93948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
94948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
9576f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurentstatic Mutex sLock;
96948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
97948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
98948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic void effectCallback(int event, void* user, void *info) {
99948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
100948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    effect_param_t *p;
101948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    int arg1 = 0;
102948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    int arg2 = 0;
103948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jobject obj = NULL;
104948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyteArray array = NULL;
105948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyte *bytes;
106948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    bool param;
107948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    size_t size;
108948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
109948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    effect_callback_cookie *callbackInfo = (effect_callback_cookie *)user;
110948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
111948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
11271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("effectCallback: callbackInfo %p, audioEffect_ref %p audioEffect_class %p",
113948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            callbackInfo,
114948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            callbackInfo->audioEffect_ref,
115948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            callbackInfo->audioEffect_class);
116948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
117948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (!user || !env) {
1188564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("effectCallback error user %p, env %p", user, env);
119948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return;
120948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
121948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
122948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    switch (event) {
123948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case AudioEffect::EVENT_CONTROL_STATUS_CHANGED:
124948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (info == 0) {
1258564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("EVENT_CONTROL_STATUS_CHANGED info == NULL");
126948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto effectCallback_Exit;
127948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
128948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        param = *(bool *)info;
129948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        arg1 = (int)param;
13071f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("EVENT_CONTROL_STATUS_CHANGED");
131948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        break;
132948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case AudioEffect::EVENT_ENABLE_STATUS_CHANGED:
133948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (info == 0) {
1348564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("EVENT_ENABLE_STATUS_CHANGED info == NULL");
135948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto effectCallback_Exit;
136948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
137948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        param = *(bool *)info;
138948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        arg1 = (int)param;
13971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("EVENT_ENABLE_STATUS_CHANGED");
140948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        break;
141948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case AudioEffect::EVENT_PARAMETER_CHANGED:
142948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (info == 0) {
1438564c8da817a845353d213acd8636b76f567b234Steve Block            ALOGW("EVENT_PARAMETER_CHANGED info == NULL");
144948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto effectCallback_Exit;
145948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
146948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        p = (effect_param_t *)info;
147948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (p->psize == 0 || p->vsize == 0) {
148948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto effectCallback_Exit;
149948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
150948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        // arg1 contains offset of parameter value from start of byte array
151948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        arg1 = sizeof(effect_param_t) + ((p->psize - 1) / sizeof(int) + 1) * sizeof(int);
152948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        size = arg1 + p->vsize;
153948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        array = env->NewByteArray(size);
154948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (array == NULL) {
1553762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("effectCallback: Couldn't allocate byte array for parameter data");
156948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto effectCallback_Exit;
157948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
158948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        bytes = env->GetByteArrayElements(array, NULL);
159948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        memcpy(bytes, p, size);
160948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleaseByteArrayElements(array, bytes, 0);
161948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        obj = array;
16271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("EVENT_PARAMETER_CHANGED");
163948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent       break;
164948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    case AudioEffect::EVENT_ERROR:
1658564c8da817a845353d213acd8636b76f567b234Steve Block        ALOGW("EVENT_ERROR");
166948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        break;
167948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
168948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
169948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->CallStaticVoidMethod(
170948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        callbackInfo->audioEffect_class,
171948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        fields.midPostNativeEvent,
172948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        callbackInfo->audioEffect_ref, event, arg1, arg2, obj);
173948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
174948235c06ed0d49190b2f49d9299b473c4dd61a9Eric LaurenteffectCallback_Exit:
175948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (array) {
176948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->DeleteLocalRef(array);
177948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
178948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
179948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (env->ExceptionCheck()) {
180948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ExceptionDescribe();
181948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ExceptionClear();
182948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
183948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
184948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
185948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
18676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent
18776f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurentstatic sp<AudioEffect> getAudioEffect(JNIEnv* env, jobject thiz)
18876f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent{
18976f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    Mutex::Autolock l(sLock);
19076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    AudioEffect* const ae =
19176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent            (AudioEffect*)env->GetLongField(thiz, fields.fidNativeAudioEffect);
19276f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    return sp<AudioEffect>(ae);
19376f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent}
19476f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent
19576f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurentstatic sp<AudioEffect> setAudioEffect(JNIEnv* env, jobject thiz,
19676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent                                    const sp<AudioEffect>& ae)
19776f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent{
19876f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    Mutex::Autolock l(sLock);
19976f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    sp<AudioEffect> old =
20076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent            (AudioEffect*)env->GetLongField(thiz, fields.fidNativeAudioEffect);
20176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (ae.get()) {
20276f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent        ae->incStrong((void*)setAudioEffect);
20376f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    }
20476f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (old != 0) {
20576f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent        old->decStrong((void*)setAudioEffect);
20676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    }
20776f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    env->SetLongField(thiz, fields.fidNativeAudioEffect, (jlong)ae.get());
20876f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    return old;
20976f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent}
21076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent
21176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent// ----------------------------------------------------------------------------
212948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// This function gets some field IDs, which in turn causes class initialization.
213948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// It is called from a static block in AudioEffect, which won't run until the
214948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// first time an instance of this class is used.
215948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic void
216948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentandroid_media_AudioEffect_native_init(JNIEnv *env)
217948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
218948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
21971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("android_media_AudioEffect_native_init");
220948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
221948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.clazzEffect = NULL;
222948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.clazzDesc = NULL;
223948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
224948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // Get the AudioEffect class
225948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jclass clazz = env->FindClass(kClassPathName);
226948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (clazz == NULL) {
2273762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find %s", kClassPathName);
228948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return;
229948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
230948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
231948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.clazzEffect = (jclass)env->NewGlobalRef(clazz);
232948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
233948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // Get the postEvent method
234948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.midPostNativeEvent = env->GetStaticMethodID(
235948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            fields.clazzEffect,
236948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            "postEventFromNative", "(Ljava/lang/Object;IIILjava/lang/Object;)V");
237948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (fields.midPostNativeEvent == NULL) {
2383762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find AudioEffect.%s", "postEventFromNative");
239948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return;
240948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
241948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
242948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // Get the variables fields
243948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    //      nativeTrackInJavaObj
244948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.fidNativeAudioEffect = env->GetFieldID(
245948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            fields.clazzEffect,
246ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat            "mNativeAudioEffect", "J");
247948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (fields.fidNativeAudioEffect == NULL) {
2483762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find AudioEffect.%s", "mNativeAudioEffect");
249948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return;
250948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
251948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    //      fidJniData;
252948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.fidJniData = env->GetFieldID(
253948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            fields.clazzEffect,
254ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat            "mJniData", "J");
255948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (fields.fidJniData == NULL) {
2563762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find AudioEffect.%s", "mJniData");
257948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return;
258948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
259948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
2601a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent    clazz = env->FindClass("android/media/audiofx/AudioEffect$Descriptor");
261948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (clazz == NULL) {
2623762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find android/media/audiofx/AudioEffect$Descriptor class");
263948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return;
264948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
265948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.clazzDesc = (jclass)env->NewGlobalRef(clazz);
266948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
267948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    fields.midDescCstor
268948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            = env->GetMethodID(
269948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                    fields.clazzDesc,
270948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                    "<init>",
271948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                    "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
272948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (fields.midDescCstor == NULL) {
2733762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find android/media/audiofx/AudioEffect$Descriptor class constructor");
274948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return;
275948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
276948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
277948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
278948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
279948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jint
280948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentandroid_media_AudioEffect_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
281fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov        jstring type, jstring uuid, jint priority, jint sessionId, jintArray jId,
282fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov        jobjectArray javadesc, jstring opPackageName)
283948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
28471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("android_media_AudioEffect_native_setup");
285948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    AudioEffectJniStorage* lpJniStorage = NULL;
286948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    int lStatus = AUDIOEFFECT_ERROR_NO_MEMORY;
28776f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    sp<AudioEffect> lpAudioEffect;
288948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jint* nId = NULL;
289948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    const char *typeStr = NULL;
290948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    const char *uuidStr = NULL;
291948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    effect_descriptor_t desc;
292948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jobject jdesc;
293948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    char str[EFFECT_STRING_LEN_MAX];
294948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescType;
295948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescUuid;
296948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescConnect;
297948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescName;
298948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescImplementor;
299948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
300fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov    ScopedUtfChars opPackageNameStr(env, opPackageName);
301fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov
30276f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    setAudioEffect(env, thiz, 0);
30376f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent
304948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (type != NULL) {
305948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        typeStr = env->GetStringUTFChars(type, NULL);
306948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (typeStr == NULL) {  // Out of memory
307948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
308948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto setup_failure;
309948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
310948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
311948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
312948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (uuid != NULL) {
313948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        uuidStr = env->GetStringUTFChars(uuid, NULL);
314948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (uuidStr == NULL) {  // Out of memory
315948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
316948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto setup_failure;
317948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
318948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
319948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
320948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (typeStr == NULL && uuidStr == NULL) {
321948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        lStatus = AUDIOEFFECT_ERROR_BAD_VALUE;
322948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setup_failure;
323948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
324948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
325948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpJniStorage = new AudioEffectJniStorage();
326948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpJniStorage == NULL) {
3273762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("setup: Error creating JNI Storage");
328948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setup_failure;
329948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
330948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
331948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpJniStorage->mCallbackData.audioEffect_class = (jclass)env->NewGlobalRef(fields.clazzEffect);
332948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // we use a weak reference so the AudioEffect object can be garbage collected.
333948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpJniStorage->mCallbackData.audioEffect_ref = env->NewGlobalRef(weak_this);
334948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
33571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("setup: lpJniStorage: %p audioEffect_ref %p audioEffect_class %p, &mCallbackData %p",
336948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            lpJniStorage,
337948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            lpJniStorage->mCallbackData.audioEffect_ref,
338948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            lpJniStorage->mCallbackData.audioEffect_class,
339948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            &lpJniStorage->mCallbackData);
340948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
3412fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    if (jId == NULL) {
3423762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("setup: NULL java array for id pointer");
343948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        lStatus = AUDIOEFFECT_ERROR_BAD_VALUE;
344948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setup_failure;
345948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
346948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
347948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // create the native AudioEffect object
348948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpAudioEffect = new AudioEffect(typeStr,
349fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov                                    String16(opPackageNameStr.c_str()),
350948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                                    uuidStr,
351948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                                    priority,
352948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                                    effectCallback,
353948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                                    &lpJniStorage->mCallbackData,
35433b840444f5a481dd31e129079d3c0cf3acdf80eGlenn Kasten                                    (audio_session_t) sessionId,
3554cb15cf15dfc9d9c6c8dc34911a4ed6bf53e8e4cJean-Michel Trivi                                    0);
35676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (lpAudioEffect == 0) {
3573762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioEffect");
358948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setup_failure;
359948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
360948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
361948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lStatus = translateError(lpAudioEffect->initCheck());
362948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lStatus != AUDIOEFFECT_SUCCESS && lStatus != AUDIOEFFECT_ERROR_ALREADY_EXISTS) {
3633762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("AudioEffect initCheck failed %d", lStatus);
364948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setup_failure;
365948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
366948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
3672fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    nId = (jint *) env->GetPrimitiveArrayCritical(jId, NULL);
3682fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    if (nId == NULL) {
3693762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("setup: Error retrieving id pointer");
3702fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent        lStatus = AUDIOEFFECT_ERROR_BAD_VALUE;
3712fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent        goto setup_failure;
3722fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    }
373948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    nId[0] = lpAudioEffect->id();
374948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->ReleasePrimitiveArrayCritical(jId, nId, 0);
375948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    nId = NULL;
376948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
377948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (typeStr) {
378948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleaseStringUTFChars(type, typeStr);
379948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        typeStr = NULL;
380948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
381948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
382948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (uuidStr) {
383948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleaseStringUTFChars(uuid, uuidStr);
384948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        uuidStr = NULL;
385948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
386948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
387948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // get the effect descriptor
388948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    desc = lpAudioEffect->descriptor();
389948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
390948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    AudioEffect::guidToString(&desc.type, str, EFFECT_STRING_LEN_MAX);
391948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jdescType = env->NewStringUTF(str);
392948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
393948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    AudioEffect::guidToString(&desc.uuid, str, EFFECT_STRING_LEN_MAX);
394948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jdescUuid = env->NewStringUTF(str);
395948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
396948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
397948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jdescConnect = env->NewStringUTF("Auxiliary");
39809f1735fadf985f321abef5fe696d0b80c6b46c7Eric Laurent    } else if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
39909f1735fadf985f321abef5fe696d0b80c6b46c7Eric Laurent        jdescConnect = env->NewStringUTF("Pre Processing");
400948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    } else {
401948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jdescConnect = env->NewStringUTF("Insert");
402948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
403948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
404948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jdescName = env->NewStringUTF(desc.name);
405948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jdescImplementor = env->NewStringUTF(desc.implementor);
406948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
407948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jdesc = env->NewObject(fields.clazzDesc,
408948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                           fields.midDescCstor,
409948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                           jdescType,
410948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                           jdescUuid,
411948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                           jdescConnect,
412948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                           jdescName,
413948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                           jdescImplementor);
414948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->DeleteLocalRef(jdescType);
415948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->DeleteLocalRef(jdescUuid);
416948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->DeleteLocalRef(jdescConnect);
417948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->DeleteLocalRef(jdescName);
418948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->DeleteLocalRef(jdescImplementor);
419948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (jdesc == NULL) {
4203762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("env->NewObject(fields.clazzDesc, fields.midDescCstor)");
421948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setup_failure;
422948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
423948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
424948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    env->SetObjectArrayElement(javadesc, 0, jdesc);
425948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
42676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    setAudioEffect(env, thiz, lpAudioEffect);
427948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
428ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    env->SetLongField(thiz, fields.fidJniData, (jlong)lpJniStorage);
429948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
430ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    return (jint) AUDIOEFFECT_SUCCESS;
431948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
432948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // failures:
433948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentsetup_failure:
434948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
435948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (nId != NULL) {
436948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleasePrimitiveArrayCritical(jId, nId, 0);
437948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
438948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
439948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpJniStorage) {
44076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent        env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioEffect_class);
44176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent        env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioEffect_ref);
442948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        delete lpJniStorage;
443948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
444ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    env->SetLongField(thiz, fields.fidJniData, 0);
445948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
446948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (uuidStr != NULL) {
447948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleaseStringUTFChars(uuid, uuidStr);
448948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
449948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
450948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (typeStr != NULL) {
451948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleaseStringUTFChars(type, typeStr);
452948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
453948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
454ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    return (jint)lStatus;
455948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
456948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
457948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
458948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
45976f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurentstatic void android_media_AudioEffect_native_release(JNIEnv *env,  jobject thiz) {
46076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    sp<AudioEffect> lpAudioEffect = setAudioEffect(env, thiz, 0);
46176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (lpAudioEffect == 0) {
46276f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent        return;
463948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
464948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
465948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // delete the JNI data
46676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    AudioEffectJniStorage* lpJniStorage =
46776f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent        (AudioEffectJniStorage *)env->GetLongField(thiz, fields.fidJniData);
46876f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent
46976f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    // reset the native resources in the Java object so any attempt to access
47076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    // them after a call to release fails.
47176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    env->SetLongField(thiz, fields.fidJniData, 0);
47276f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent
473948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpJniStorage) {
474ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        ALOGV("deleting pJniStorage: %p\n", lpJniStorage);
475077999844f306d31d3ea9593b7e7b2f06e7f7483Eric Laurent        env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioEffect_class);
476077999844f306d31d3ea9593b7e7b2f06e7f7483Eric Laurent        env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioEffect_ref);
477948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        delete lpJniStorage;
478948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
479948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
480948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
481948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
48276f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurentstatic void android_media_AudioEffect_native_finalize(JNIEnv *env,  jobject thiz) {
48376f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    ALOGV("android_media_AudioEffect_native_finalize jobject: %p\n", thiz);
48476f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    android_media_AudioEffect_native_release(env, thiz);
485948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
486948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
487948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jint
488df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurentandroid_media_AudioEffect_native_setEnabled(JNIEnv *env, jobject thiz, jboolean enabled)
489948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
49076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz);
49176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (lpAudioEffect == 0) {
492948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
493948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            "Unable to retrieve AudioEffect pointer for enable()");
494948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_NO_INIT;
495948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
496948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
497ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    return (jint) translateError(lpAudioEffect->setEnabled(enabled));
498948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
499948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
500948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jboolean
501df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurentandroid_media_AudioEffect_native_getEnabled(JNIEnv *env, jobject thiz)
502948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
50376f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent  sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz);
50476f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent  if (lpAudioEffect == 0) {
505948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
506948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            "Unable to retrieve AudioEffect pointer for getEnabled()");
507ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        return JNI_FALSE;
508948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
509948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
510ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    if (lpAudioEffect->getEnabled()) {
511ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        return JNI_TRUE;
512ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    } else {
513ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        return JNI_FALSE;
514ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    }
515948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
516948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
517948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
518948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jboolean
519948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentandroid_media_AudioEffect_native_hasControl(JNIEnv *env, jobject thiz)
520948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
52176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent  sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz);
52276f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent  if (lpAudioEffect == 0) {
523948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
524df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent            "Unable to retrieve AudioEffect pointer for hasControl()");
525ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        return JNI_FALSE;
526948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
527948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
528948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpAudioEffect->initCheck() == NO_ERROR) {
529ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        return JNI_TRUE;
530948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    } else {
531ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        return JNI_FALSE;
532948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
533948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
534948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
535948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jint android_media_AudioEffect_native_setParameter(JNIEnv *env,
536ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat        jobject thiz, jint psize, jbyteArray pJavaParam, jint vsize,
537948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jbyteArray pJavaValue) {
538948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // retrieve the AudioEffect object
539948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyte* lpValue = NULL;
540948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyte* lpParam = NULL;
541948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jint lStatus = AUDIOEFFECT_ERROR_BAD_VALUE;
542948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    effect_param_t *p;
543948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    int voffset;
544948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
54576f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz);
54676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (lpAudioEffect == 0) {
547948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
548948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                "Unable to retrieve AudioEffect pointer for setParameter()");
549948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_NO_INIT;
550948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
551948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
552948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (psize == 0 || vsize == 0 || pJavaParam == NULL || pJavaValue == NULL) {
553948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_BAD_VALUE;
554948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
555948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
556948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // get the pointer for the param from the java array
557948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpParam = (jbyte *) env->GetPrimitiveArrayCritical(pJavaParam, NULL);
558948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpParam == NULL) {
5593762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("setParameter: Error retrieving param pointer");
560948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setParameter_Exit;
561948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
562948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
563948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // get the pointer for the value from the java array
564948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpValue = (jbyte *) env->GetPrimitiveArrayCritical(pJavaValue, NULL);
565948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpValue == NULL) {
5663762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("setParameter: Error retrieving value pointer");
567948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto setParameter_Exit;
568948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
569948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
570948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    voffset = ((psize - 1) / sizeof(int) + 1) * sizeof(int);
571948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    p = (effect_param_t *) malloc(sizeof(effect_param_t) + voffset + vsize);
572948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    memcpy(p->data, lpParam, psize);
573948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    p->psize = psize;
574ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    memcpy(p->data + voffset, lpValue, vsize);
575948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    p->vsize = vsize;
576948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
577948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lStatus = lpAudioEffect->setParameter(p);
578948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lStatus == NO_ERROR) {
579948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        lStatus = p->status;
580948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
581948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
582948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    free(p);
583948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
584948235c06ed0d49190b2f49d9299b473c4dd61a9Eric LaurentsetParameter_Exit:
585948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
586948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpParam != NULL) {
587948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleasePrimitiveArrayCritical(pJavaParam, lpParam, 0);
588948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
589948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpValue != NULL) {
590948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleasePrimitiveArrayCritical(pJavaValue, lpValue, 0);
591948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
592ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    return (jint) translateError(lStatus);
593948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
594948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
595948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jint
596948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentandroid_media_AudioEffect_native_getParameter(JNIEnv *env,
597602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent        jobject thiz, jint psize, jbyteArray pJavaParam,
598602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent        jint vsize, jbyteArray pJavaValue) {
599948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // retrieve the AudioEffect object
600948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyte* lpParam = NULL;
601948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyte* lpValue = NULL;
602948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jint lStatus = AUDIOEFFECT_ERROR_BAD_VALUE;
603948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    effect_param_t *p;
604948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    int voffset;
605948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
60676f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz);
60776f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (lpAudioEffect == 0) {
608948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
609948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                "Unable to retrieve AudioEffect pointer for getParameter()");
610948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_NO_INIT;
611948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
612948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
613602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    if (psize == 0 || vsize == 0 || pJavaParam == NULL || pJavaValue == NULL) {
614948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_BAD_VALUE;
615948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
616948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
617948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // get the pointer for the param from the java array
618948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpParam = (jbyte *) env->GetPrimitiveArrayCritical(pJavaParam, NULL);
619948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpParam == NULL) {
6203762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("getParameter: Error retrieving param pointer");
621948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto getParameter_Exit;
622948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
623948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
624948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // get the pointer for the value from the java array
625948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lpValue = (jbyte *) env->GetPrimitiveArrayCritical(pJavaValue, NULL);
626948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpValue == NULL) {
6273762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("getParameter: Error retrieving value pointer");
628948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto getParameter_Exit;
629948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
630948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
631948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    voffset = ((psize - 1) / sizeof(int) + 1) * sizeof(int);
632602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    p = (effect_param_t *) malloc(sizeof(effect_param_t) + voffset + vsize);
633948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    memcpy(p->data, lpParam, psize);
634948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    p->psize = psize;
635602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    p->vsize = vsize;
636948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
637948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    lStatus = lpAudioEffect->getParameter(p);
638948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lStatus == NO_ERROR) {
639948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        lStatus = p->status;
640948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (lStatus == NO_ERROR) {
641948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            memcpy(lpValue, p->data + voffset, p->vsize);
642602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent            vsize = p->vsize;
643948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
644948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
645948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
646948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    free(p);
647948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
648948235c06ed0d49190b2f49d9299b473c4dd61a9Eric LaurentgetParameter_Exit:
649948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
650948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpParam != NULL) {
651948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleasePrimitiveArrayCritical(pJavaParam, lpParam, 0);
652948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
653948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (lpValue != NULL) {
654948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleasePrimitiveArrayCritical(pJavaValue, lpValue, 0);
655948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
656948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
657602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    if (lStatus == NO_ERROR) {
658602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent        return vsize;
659602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    }
660ea7861c918567d17d40a762b38f97c053d88b839Ashok Bhat    return (jint) translateError(lStatus);
661948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
662948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
663948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jint android_media_AudioEffect_native_command(JNIEnv *env, jobject thiz,
664602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent        jint cmdCode, jint cmdSize, jbyteArray jCmdData, jint replySize,
665948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jbyteArray jReplyData) {
666948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyte* pCmdData = NULL;
667948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jbyte* pReplyData = NULL;
668948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jint lStatus = AUDIOEFFECT_ERROR_BAD_VALUE;
669948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
67076f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz);
67176f81331886c0972e5f7432e4edd8c36ef1048e6Eric Laurent    if (lpAudioEffect == 0) {
672948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
673948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                "Unable to retrieve AudioEffect pointer for setParameter()");
674948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_NO_INIT;
675948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
676948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
677602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    if ((cmdSize != 0 && jCmdData == NULL) || (replySize != 0 && jReplyData == NULL)) {
678948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        return AUDIOEFFECT_ERROR_BAD_VALUE;
679948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
680948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
681948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // get the pointer for the command from the java array
682948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (cmdSize != 0) {
683948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        pCmdData = (jbyte *) env->GetPrimitiveArrayCritical(jCmdData, NULL);
684948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (pCmdData == NULL) {
6853762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("setParameter: Error retrieving command pointer");
686948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto command_Exit;
687948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
688948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
689948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
690948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    // get the pointer for the reply from the java array
691602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    if (replySize != 0 && jReplyData != NULL) {
692948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        pReplyData = (jbyte *) env->GetPrimitiveArrayCritical(jReplyData, NULL);
693948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (pReplyData == NULL) {
6943762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("setParameter: Error retrieving reply pointer");
695948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto command_Exit;
696948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
697948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
698948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
699a4c72acfbc6c06588dd26cf41e67a834fc0a54f9Eric Laurent    lStatus = translateError(lpAudioEffect->command((uint32_t)cmdCode,
700a4c72acfbc6c06588dd26cf41e67a834fc0a54f9Eric Laurent                                                    (uint32_t)cmdSize,
701a4c72acfbc6c06588dd26cf41e67a834fc0a54f9Eric Laurent                                                    pCmdData,
702602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent                                                    (uint32_t *)&replySize,
703a4c72acfbc6c06588dd26cf41e67a834fc0a54f9Eric Laurent                                                    pReplyData));
704948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
705948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentcommand_Exit:
706948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
707948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (pCmdData != NULL) {
708948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleasePrimitiveArrayCritical(jCmdData, pCmdData, 0);
709948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
710948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (pReplyData != NULL) {
711948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->ReleasePrimitiveArrayCritical(jReplyData, pReplyData, 0);
712948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
713948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
714602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    if (lStatus == NO_ERROR) {
715602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent        return replySize;
716602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    }
717948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    return lStatus;
718948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
719948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
720948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentstatic jobjectArray
7214fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurentandroid_media_AudioEffect_native_queryEffects(JNIEnv *env, jclass clazz __unused)
722948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
723948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    effect_descriptor_t desc;
724948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    char str[EFFECT_STRING_LEN_MAX];
7254fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    uint32_t totalEffectsCount = 0;
7264fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    uint32_t returnedEffectsCount = 0;
727948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    uint32_t i = 0;
728948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescType;
729948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescUuid;
730948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescConnect;
731948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescName;
732948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jstring jdescImplementor;
733948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jobject jdesc;
7344fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    jobjectArray ret;
735948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
7364fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    if (AudioEffect::queryNumberEffects(&totalEffectsCount) != NO_ERROR) {
7374526f0de0a5698832719f16158bed57ad209d4f2Peter Karlsson        return NULL;
7384526f0de0a5698832719f16158bed57ad209d4f2Peter Karlsson    }
7394526f0de0a5698832719f16158bed57ad209d4f2Peter Karlsson
7404fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    jobjectArray temp = env->NewObjectArray(totalEffectsCount, fields.clazzDesc, NULL);
7414fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    if (temp == NULL) {
7424fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent        return temp;
743948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
744948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
7454fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    ALOGV("queryEffects() totalEffectsCount: %d", totalEffectsCount);
746948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
7474fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    for (i = 0; i < totalEffectsCount; i++) {
74853334cdb81bab4a4dfd0a41d2ef50709015a36c8Eric Laurent        if (AudioEffect::queryEffect(i, &desc) != NO_ERROR) {
749948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto queryEffects_failure;
750948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
751948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
7520f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7530f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent            jdescConnect = env->NewStringUTF("Auxiliary");
7540f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        } else if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT) {
7550f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent            jdescConnect = env->NewStringUTF("Insert");
7560f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        } else if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
7570f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent            jdescConnect = env->NewStringUTF("Pre Processing");
7580f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        } else {
7590f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent            continue;
7600f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        }
7610f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
762948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        AudioEffect::guidToString(&desc.type, str, EFFECT_STRING_LEN_MAX);
763948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jdescType = env->NewStringUTF(str);
764948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
765948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        AudioEffect::guidToString(&desc.uuid, str, EFFECT_STRING_LEN_MAX);
766948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jdescUuid = env->NewStringUTF(str);
767948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
768948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jdescName = env->NewStringUTF(desc.name);
769948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jdescImplementor = env->NewStringUTF(desc.implementor);
770948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
771948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        jdesc = env->NewObject(fields.clazzDesc,
772948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                               fields.midDescCstor,
773948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                               jdescType,
774948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                               jdescUuid,
775948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                               jdescConnect,
776948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                               jdescName,
777948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                               jdescImplementor);
778948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->DeleteLocalRef(jdescType);
779948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->DeleteLocalRef(jdescUuid);
780948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->DeleteLocalRef(jdescConnect);
781948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->DeleteLocalRef(jdescName);
782948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        env->DeleteLocalRef(jdescImplementor);
783948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        if (jdesc == NULL) {
7843762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("env->NewObject(fields.clazzDesc, fields.midDescCstor)");
785948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent            goto queryEffects_failure;
786948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        }
787948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
7884fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent        env->SetObjectArrayElement(temp, returnedEffectsCount++, jdesc);
789948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent   }
790948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
7914fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    if (returnedEffectsCount == 0) {
7924fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent        goto queryEffects_failure;
7934fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    }
7944fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    ret = env->NewObjectArray(returnedEffectsCount, fields.clazzDesc, NULL);
7954fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    if (ret == NULL) {
7964fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent        goto queryEffects_failure;
7974fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    }
7984fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    for (i = 0; i < returnedEffectsCount; i++) {
7994fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent        env->SetObjectArrayElement(ret, i, env->GetObjectArrayElement(temp, i));
8004fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    }
8014fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    env->DeleteLocalRef(temp);
802948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    return ret;
803948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
804948235c06ed0d49190b2f49d9299b473c4dd61a9Eric LaurentqueryEffects_failure:
805948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
8064fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent    if (temp != NULL) {
8074fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent        env->DeleteLocalRef(temp);
808948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
809948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    return NULL;
810948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
811948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
812948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
8130f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8140f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8150f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurentstatic jobjectArray
8164fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurentandroid_media_AudioEffect_native_queryPreProcessings(JNIEnv *env, jclass clazz __unused,
8174fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurent                                                     jint audioSession)
8180f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent{
819b27a8a5bcc40054f6d775d070bc2de6eb996d1c2Eric Laurent    effect_descriptor_t *descriptors = new effect_descriptor_t[AudioEffect::kMaxPreProcessing];
820b27a8a5bcc40054f6d775d070bc2de6eb996d1c2Eric Laurent    uint32_t numEffects = AudioEffect::kMaxPreProcessing;
8210f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
82233b840444f5a481dd31e129079d3c0cf3acdf80eGlenn Kasten    status_t status = AudioEffect::queryDefaultPreProcessing((audio_session_t) audioSession,
8230f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                                           descriptors,
8240f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                                           &numEffects);
8250f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    if (status != NO_ERROR || numEffects == 0) {
8260f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        delete[] descriptors;
8270f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        return NULL;
8280f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    }
82971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("queryDefaultPreProcessing() got %d effects", numEffects);
8300f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8310f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    jobjectArray ret = env->NewObjectArray(numEffects, fields.clazzDesc, NULL);
8320f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    if (ret == NULL) {
8330f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        delete[] descriptors;
8340f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        return ret;
8350f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    }
8360f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8370f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    char str[EFFECT_STRING_LEN_MAX];
8380f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    jstring jdescType;
8390f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    jstring jdescUuid;
8400f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    jstring jdescConnect;
8410f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    jstring jdescName;
8420f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    jstring jdescImplementor;
8430f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    jobject jdesc;
8440f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8450f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    for (uint32_t i = 0; i < numEffects; i++) {
8460f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8470f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        AudioEffect::guidToString(&descriptors[i].type, str, EFFECT_STRING_LEN_MAX);
8480f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        jdescType = env->NewStringUTF(str);
8490f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        AudioEffect::guidToString(&descriptors[i].uuid, str, EFFECT_STRING_LEN_MAX);
8500f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        jdescUuid = env->NewStringUTF(str);
8510f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        jdescConnect = env->NewStringUTF("Pre Processing");
8520f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        jdescName = env->NewStringUTF(descriptors[i].name);
8530f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        jdescImplementor = env->NewStringUTF(descriptors[i].implementor);
8540f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8550f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        jdesc = env->NewObject(fields.clazzDesc,
8560f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                               fields.midDescCstor,
8570f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                               jdescType,
8580f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                               jdescUuid,
8590f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                               jdescConnect,
8600f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                               jdescName,
8610f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent                               jdescImplementor);
8620f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        env->DeleteLocalRef(jdescType);
8630f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        env->DeleteLocalRef(jdescUuid);
8640f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        env->DeleteLocalRef(jdescConnect);
8650f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        env->DeleteLocalRef(jdescName);
8660f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        env->DeleteLocalRef(jdescImplementor);
8670f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        if (jdesc == NULL) {
8683762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("env->NewObject(fields.clazzDesc, fields.midDescCstor)");
8690f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent            env->DeleteLocalRef(ret);
8700f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent            return NULL;;
8710f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        }
8720f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8730f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent        env->SetObjectArrayElement(ret, i, jdesc);
8740f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent   }
8750f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
8760f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent   return ret;
8770f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent}
8780f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent
879948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
880948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
881948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// Dalvik VM type signatures
88276f6a86de25e1bf74717e047e55fd44b089673f3Daniel Micaystatic const JNINativeMethod gMethods[] = {
883948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    {"native_init",          "()V",      (void *)android_media_AudioEffect_native_init},
884fa5ecdc4ac6d7a8db2bb9e4a6a60a3189025df30Svet Ganov    {"native_setup",         "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;II[I[Ljava/lang/Object;Ljava/lang/String;)I",
885948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent                                         (void *)android_media_AudioEffect_native_setup},
886948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    {"native_finalize",      "()V",      (void *)android_media_AudioEffect_native_finalize},
887948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    {"native_release",       "()V",      (void *)android_media_AudioEffect_native_release},
888df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent    {"native_setEnabled",    "(Z)I",      (void *)android_media_AudioEffect_native_setEnabled},
889df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent    {"native_getEnabled",    "()Z",      (void *)android_media_AudioEffect_native_getEnabled},
890948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    {"native_hasControl",    "()Z",      (void *)android_media_AudioEffect_native_hasControl},
891948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    {"native_setParameter",  "(I[BI[B)I",  (void *)android_media_AudioEffect_native_setParameter},
892602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    {"native_getParameter",  "(I[BI[B)I",  (void *)android_media_AudioEffect_native_getParameter},
893602b3286ffe7da6e70bf2d9e4861a5d74ff7c473Eric Laurent    {"native_command",       "(II[BI[B)I", (void *)android_media_AudioEffect_native_command},
894948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    {"native_query_effects", "()[Ljava/lang/Object;", (void *)android_media_AudioEffect_native_queryEffects},
8950f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent    {"native_query_pre_processing", "(I)[Ljava/lang/Object;",
8960f7f4ece1b6b73caf608d533d833a8cdc11c8131Eric Laurent            (void *)android_media_AudioEffect_native_queryPreProcessings},
897948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent};
898948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
899948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
900948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent// ----------------------------------------------------------------------------
901948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
902df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurentextern int register_android_media_visualizer(JNIEnv *env);
903df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent
904948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentint register_android_media_AudioEffect(JNIEnv *env)
905948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
906948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
907948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
908948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
9094fc28d78cb925c2780abf02c6826e77f8f8a230aEric Laurentjint JNI_OnLoad(JavaVM* vm, void* reserved __unused)
910948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent{
911948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
912948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    JNIEnv* env = NULL;
913948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    jint result = -1;
914948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
915948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
9163762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("ERROR: GetEnv failed\n");
917948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto bail;
918948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
919948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    assert(env != NULL);
920948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
921948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    if (register_android_media_AudioEffect(env) < 0) {
9223762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("ERROR: AudioEffect native registration failed\n");
923948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent        goto bail;
924948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    }
925948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
926df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent    if (register_android_media_visualizer(env) < 0) {
9273762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("ERROR: Visualizer native registration failed\n");
928df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent        goto bail;
929df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent    }
930df9b81ced437b11f8a3fcf4ba3ea6af703d121e2Eric Laurent
931948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    /* success -- return valid version number */
932948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    result = JNI_VERSION_1_4;
933948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
934948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurentbail:
935948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent    return result;
936948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent}
937948235c06ed0d49190b2f49d9299b473c4dd61a9Eric Laurent
938