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     if ((nDescriptor->flags & EFFECT_FLAG_TYPE_MASK)
43         == EFFECT_FLAG_TYPE_AUXILIARY) {
44         jConnect = env->NewStringUTF("Auxiliary");
45     } else if ((nDescriptor->flags & EFFECT_FLAG_TYPE_MASK)
46         == EFFECT_FLAG_TYPE_INSERT) {
47         jConnect = env->NewStringUTF("Insert");
48     } else if ((nDescriptor->flags & EFFECT_FLAG_TYPE_MASK)
49         == EFFECT_FLAG_TYPE_PRE_PROC) {
50         jConnect = env->NewStringUTF("Pre Processing");
51     } else {
52         return (jint) AUDIO_JAVA_BAD_VALUE;
53     }
54 
55     AudioEffect::guidToString(&nDescriptor->type, str, EFFECT_STRING_LEN_MAX);
56     jType = env->NewStringUTF(str);
57 
58     AudioEffect::guidToString(&nDescriptor->uuid, str, EFFECT_STRING_LEN_MAX);
59     jUuid = env->NewStringUTF(str);
60 
61     jName = env->NewStringUTF(nDescriptor->name);
62     jImplementor = env->NewStringUTF(nDescriptor->implementor);
63 
64     *jDescriptor = env->NewObject(gAudioEffectDescriptorClass,
65                                   gAudioEffectDescriptorCstor,
66                                   jType,
67                                   jUuid,
68                                   jConnect,
69                                   jName,
70                                   jImplementor);
71     env->DeleteLocalRef(jType);
72     env->DeleteLocalRef(jUuid);
73     env->DeleteLocalRef(jConnect);
74     env->DeleteLocalRef(jName);
75     env->DeleteLocalRef(jImplementor);
76 
77     return (jint) AUDIO_JAVA_SUCCESS;
78 }
79 
convertAudioEffectDescriptorVectorFromNative(JNIEnv * env,jobjectArray * jDescriptors,const std::vector<effect_descriptor_t> & nDescriptors)80 void convertAudioEffectDescriptorVectorFromNative(JNIEnv *env, jobjectArray *jDescriptors,
81         const std::vector<effect_descriptor_t>& nDescriptors)
82 {
83     jobjectArray temp = env->NewObjectArray(nDescriptors.size(),
84                                             audioEffectDescriptorClass(), NULL);
85     size_t actualSize = 0;
86     for (size_t i = 0; i < nDescriptors.size(); i++) {
87         jobject jdesc;
88         if (convertAudioEffectDescriptorFromNative(env,
89                                                    &jdesc,
90                                                    &nDescriptors[i])
91             != AUDIO_JAVA_SUCCESS) {
92             continue;
93         }
94 
95         env->SetObjectArrayElement(temp, actualSize++, jdesc);
96         env->DeleteLocalRef(jdesc);
97     }
98 
99     *jDescriptors = env->NewObjectArray(actualSize, audioEffectDescriptorClass(), NULL);
100     for (size_t i = 0; i < actualSize; i++) {
101         env->SetObjectArrayElement(*jDescriptors,
102                                    i,
103                                    env->GetObjectArrayElement(temp, i));
104     }
105     env->DeleteLocalRef(temp);
106 }
107 
108 }; // namespace android
109 
register_android_media_AudioEffectDescriptor(JNIEnv * env)110 int register_android_media_AudioEffectDescriptor(JNIEnv* env) {
111     jclass audioEffectDescriptorClass =
112         FindClassOrDie(env, "android/media/audiofx/AudioEffect$Descriptor");
113     gAudioEffectDescriptorClass =
114         MakeGlobalRefOrDie(env, audioEffectDescriptorClass);
115     gAudioEffectDescriptorCstor =
116         GetMethodIDOrDie(env,
117                          audioEffectDescriptorClass,
118                          "<init>",
119                          "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
120 
121     env->DeleteLocalRef(audioEffectDescriptorClass);
122     return 0;
123 }
124