1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "core_jni_helpers.h"
18 #include "android_media_AudioErrors.h"
19 #include "media/AudioEffect.h"
20 
21 using namespace android;
22 
23 static jclass gAudioEffectDescriptorClass;
24 static jmethodID gAudioEffectDescriptorCstor;
25 
26 namespace android {
27 
audioEffectDescriptorClass()28 jclass audioEffectDescriptorClass() {
29     return gAudioEffectDescriptorClass;
30 }
31 
convertAudioEffectDescriptorFromNative(JNIEnv * env,jobject * jDescriptor,const effect_descriptor_t * nDescriptor)32 jint convertAudioEffectDescriptorFromNative(JNIEnv* env, jobject* jDescriptor,
33         const effect_descriptor_t* nDescriptor)
34 {
35     jstring jType;
36     jstring jUuid;
37     jstring jConnect;
38     jstring jName;
39     jstring jImplementor;
40     char str[EFFECT_STRING_LEN_MAX];
41 
42     switch (nDescriptor->flags & EFFECT_FLAG_TYPE_MASK) {
43         case EFFECT_FLAG_TYPE_AUXILIARY:
44             jConnect = env->NewStringUTF("Auxiliary");
45             break;
46         case EFFECT_FLAG_TYPE_INSERT:
47             jConnect = env->NewStringUTF("Insert");
48             break;
49         case EFFECT_FLAG_TYPE_PRE_PROC:
50             jConnect = env->NewStringUTF("Pre Processing");
51             break;
52         case EFFECT_FLAG_TYPE_POST_PROC:
53             jConnect = env->NewStringUTF("Post Processing");
54             break;
55         default:
56             return (jint)AUDIO_JAVA_BAD_VALUE;
57     }
58 
59     AudioEffect::guidToString(&nDescriptor->type, str, EFFECT_STRING_LEN_MAX);
60     jType = env->NewStringUTF(str);
61 
62     AudioEffect::guidToString(&nDescriptor->uuid, str, EFFECT_STRING_LEN_MAX);
63     jUuid = env->NewStringUTF(str);
64 
65     jName = env->NewStringUTF(nDescriptor->name);
66     jImplementor = env->NewStringUTF(nDescriptor->implementor);
67 
68     *jDescriptor = env->NewObject(gAudioEffectDescriptorClass,
69                                   gAudioEffectDescriptorCstor,
70                                   jType,
71                                   jUuid,
72                                   jConnect,
73                                   jName,
74                                   jImplementor);
75     env->DeleteLocalRef(jType);
76     env->DeleteLocalRef(jUuid);
77     env->DeleteLocalRef(jConnect);
78     env->DeleteLocalRef(jName);
79     env->DeleteLocalRef(jImplementor);
80 
81     return (jint) AUDIO_JAVA_SUCCESS;
82 }
83 
convertAudioEffectDescriptorVectorFromNative(JNIEnv * env,jobjectArray * jDescriptors,const std::vector<effect_descriptor_t> & nDescriptors)84 void convertAudioEffectDescriptorVectorFromNative(JNIEnv *env, jobjectArray *jDescriptors,
85         const std::vector<effect_descriptor_t>& nDescriptors)
86 {
87     jobjectArray temp = env->NewObjectArray(nDescriptors.size(),
88                                             audioEffectDescriptorClass(), NULL);
89     size_t actualSize = 0;
90     for (size_t i = 0; i < nDescriptors.size(); i++) {
91         jobject jdesc;
92         if (convertAudioEffectDescriptorFromNative(env,
93                                                    &jdesc,
94                                                    &nDescriptors[i])
95             != AUDIO_JAVA_SUCCESS) {
96             continue;
97         }
98 
99         env->SetObjectArrayElement(temp, actualSize++, jdesc);
100         env->DeleteLocalRef(jdesc);
101     }
102 
103     *jDescriptors = env->NewObjectArray(actualSize, audioEffectDescriptorClass(), NULL);
104     for (size_t i = 0; i < actualSize; i++) {
105         jobject jdesc = env->GetObjectArrayElement(temp, i);
106         env->SetObjectArrayElement(*jDescriptors, i, jdesc);
107         env->DeleteLocalRef(jdesc);
108     }
109     env->DeleteLocalRef(temp);
110 }
111 
112 }; // namespace android
113 
register_android_media_AudioEffectDescriptor(JNIEnv * env)114 int register_android_media_AudioEffectDescriptor(JNIEnv* env) {
115     jclass audioEffectDescriptorClass =
116         FindClassOrDie(env, "android/media/audiofx/AudioEffect$Descriptor");
117     gAudioEffectDescriptorClass =
118         MakeGlobalRefOrDie(env, audioEffectDescriptorClass);
119     gAudioEffectDescriptorCstor =
120         GetMethodIDOrDie(env,
121                          audioEffectDescriptorClass,
122                          "<init>",
123                          "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
124 
125     env->DeleteLocalRef(audioEffectDescriptorClass);
126     return 0;
127 }
128