1 /*
2  * Copyright (C) 2008 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 <stdio.h>
18 
19 //#define LOG_NDEBUG 0
20 #define LOG_TAG "SoundPool-JNI"
21 
22 #include <utils/Log.h>
23 #include <jni.h>
24 #include <nativehelper/JNIHelp.h>
25 #include <android_runtime/AndroidRuntime.h>
26 #include "SoundPool.h"
27 
28 using namespace android;
29 
30 static struct fields_t {
31     jfieldID    mNativeContext;
32     jmethodID   mPostEvent;
33     jclass      mSoundPoolClass;
34 } fields;
MusterSoundPool(JNIEnv * env,jobject thiz)35 static inline SoundPool* MusterSoundPool(JNIEnv *env, jobject thiz) {
36     return (SoundPool*)env->GetLongField(thiz, fields.mNativeContext);
37 }
38 static const char* const kAudioAttributesClassPathName = "android/media/AudioAttributes";
39 struct audio_attributes_fields_t {
40     jfieldID  fieldUsage;        // AudioAttributes.mUsage
41     jfieldID  fieldContentType;  // AudioAttributes.mContentType
42     jfieldID  fieldFlags;        // AudioAttributes.mFlags
43     jfieldID  fieldFormattedTags;// AudioAttributes.mFormattedTags
44 };
45 static audio_attributes_fields_t javaAudioAttrFields;
46 
47 // ----------------------------------------------------------------------------
48 
49 static jint
android_media_SoundPool_load_FD(JNIEnv * env,jobject thiz,jobject fileDescriptor,jlong offset,jlong length,jint priority)50 android_media_SoundPool_load_FD(JNIEnv *env, jobject thiz, jobject fileDescriptor,
51         jlong offset, jlong length, jint priority)
52 {
53     ALOGV("android_media_SoundPool_load_FD");
54     SoundPool *ap = MusterSoundPool(env, thiz);
55     if (ap == nullptr) return 0;
56     return (jint) ap->load(jniGetFDFromFileDescriptor(env, fileDescriptor),
57             int64_t(offset), int64_t(length), int(priority));
58 }
59 
60 static jboolean
android_media_SoundPool_unload(JNIEnv * env,jobject thiz,jint sampleID)61 android_media_SoundPool_unload(JNIEnv *env, jobject thiz, jint sampleID) {
62     ALOGV("android_media_SoundPool_unload\n");
63     SoundPool *ap = MusterSoundPool(env, thiz);
64     if (ap == nullptr) return JNI_FALSE;
65     return ap->unload(sampleID) ? JNI_TRUE : JNI_FALSE;
66 }
67 
68 static jint
android_media_SoundPool_play(JNIEnv * env,jobject thiz,jint sampleID,jfloat leftVolume,jfloat rightVolume,jint priority,jint loop,jfloat rate)69 android_media_SoundPool_play(JNIEnv *env, jobject thiz, jint sampleID,
70         jfloat leftVolume, jfloat rightVolume, jint priority, jint loop,
71         jfloat rate)
72 {
73     ALOGV("android_media_SoundPool_play\n");
74     SoundPool *ap = MusterSoundPool(env, thiz);
75     if (ap == nullptr) return 0;
76     return (jint) ap->play(sampleID, leftVolume, rightVolume, priority, loop, rate);
77 }
78 
79 static void
android_media_SoundPool_pause(JNIEnv * env,jobject thiz,jint channelID)80 android_media_SoundPool_pause(JNIEnv *env, jobject thiz, jint channelID)
81 {
82     ALOGV("android_media_SoundPool_pause");
83     SoundPool *ap = MusterSoundPool(env, thiz);
84     if (ap == nullptr) return;
85     ap->pause(channelID);
86 }
87 
88 static void
android_media_SoundPool_resume(JNIEnv * env,jobject thiz,jint channelID)89 android_media_SoundPool_resume(JNIEnv *env, jobject thiz, jint channelID)
90 {
91     ALOGV("android_media_SoundPool_resume");
92     SoundPool *ap = MusterSoundPool(env, thiz);
93     if (ap == nullptr) return;
94     ap->resume(channelID);
95 }
96 
97 static void
android_media_SoundPool_autoPause(JNIEnv * env,jobject thiz)98 android_media_SoundPool_autoPause(JNIEnv *env, jobject thiz)
99 {
100     ALOGV("android_media_SoundPool_autoPause");
101     SoundPool *ap = MusterSoundPool(env, thiz);
102     if (ap == nullptr) return;
103     ap->autoPause();
104 }
105 
106 static void
android_media_SoundPool_autoResume(JNIEnv * env,jobject thiz)107 android_media_SoundPool_autoResume(JNIEnv *env, jobject thiz)
108 {
109     ALOGV("android_media_SoundPool_autoResume");
110     SoundPool *ap = MusterSoundPool(env, thiz);
111     if (ap == nullptr) return;
112     ap->autoResume();
113 }
114 
115 static void
android_media_SoundPool_stop(JNIEnv * env,jobject thiz,jint channelID)116 android_media_SoundPool_stop(JNIEnv *env, jobject thiz, jint channelID)
117 {
118     ALOGV("android_media_SoundPool_stop");
119     SoundPool *ap = MusterSoundPool(env, thiz);
120     if (ap == nullptr) return;
121     ap->stop(channelID);
122 }
123 
124 static void
android_media_SoundPool_setVolume(JNIEnv * env,jobject thiz,jint channelID,jfloat leftVolume,jfloat rightVolume)125 android_media_SoundPool_setVolume(JNIEnv *env, jobject thiz, jint channelID,
126         jfloat leftVolume, jfloat rightVolume)
127 {
128     ALOGV("android_media_SoundPool_setVolume");
129     SoundPool *ap = MusterSoundPool(env, thiz);
130     if (ap == nullptr) return;
131     ap->setVolume(channelID, (float) leftVolume, (float) rightVolume);
132 }
133 
134 static void
android_media_SoundPool_mute(JNIEnv * env,jobject thiz,jboolean muting)135 android_media_SoundPool_mute(JNIEnv *env, jobject thiz, jboolean muting)
136 {
137     ALOGV("android_media_SoundPool_mute(%d)", muting);
138     SoundPool *ap = MusterSoundPool(env, thiz);
139     if (ap == nullptr) return;
140     ap->mute(muting == JNI_TRUE);
141 }
142 
143 static void
android_media_SoundPool_setPriority(JNIEnv * env,jobject thiz,jint channelID,jint priority)144 android_media_SoundPool_setPriority(JNIEnv *env, jobject thiz, jint channelID,
145         jint priority)
146 {
147     ALOGV("android_media_SoundPool_setPriority");
148     SoundPool *ap = MusterSoundPool(env, thiz);
149     if (ap == nullptr) return;
150     ap->setPriority(channelID, (int) priority);
151 }
152 
153 static void
android_media_SoundPool_setLoop(JNIEnv * env,jobject thiz,jint channelID,int loop)154 android_media_SoundPool_setLoop(JNIEnv *env, jobject thiz, jint channelID,
155         int loop)
156 {
157     ALOGV("android_media_SoundPool_setLoop");
158     SoundPool *ap = MusterSoundPool(env, thiz);
159     if (ap == nullptr) return;
160     ap->setLoop(channelID, loop);
161 }
162 
163 static void
android_media_SoundPool_setRate(JNIEnv * env,jobject thiz,jint channelID,jfloat rate)164 android_media_SoundPool_setRate(JNIEnv *env, jobject thiz, jint channelID,
165        jfloat rate)
166 {
167     ALOGV("android_media_SoundPool_setRate");
168     SoundPool *ap = MusterSoundPool(env, thiz);
169     if (ap == nullptr) return;
170     ap->setRate(channelID, (float) rate);
171 }
172 
android_media_callback(SoundPoolEvent event,SoundPool * soundPool,void * user)173 static void android_media_callback(SoundPoolEvent event, SoundPool* soundPool, void* user)
174 {
175     ALOGV("callback: (%d, %d, %d, %p, %p)", event.mMsg, event.mArg1, event.mArg2, soundPool, user);
176     JNIEnv *env = AndroidRuntime::getJNIEnv();
177     env->CallStaticVoidMethod(
178             fields.mSoundPoolClass, fields.mPostEvent, user, event.mMsg, event.mArg1, event.mArg2,
179             nullptr /* object */);
180 }
181 
182 static jint
android_media_SoundPool_native_setup(JNIEnv * env,jobject thiz,jobject weakRef,jint maxChannels,jobject jaa)183 android_media_SoundPool_native_setup(JNIEnv *env, jobject thiz, jobject weakRef,
184         jint maxChannels, jobject jaa)
185 {
186     if (jaa == nullptr) {
187         ALOGE("Error creating SoundPool: invalid audio attributes");
188         return -1;
189     }
190 
191     audio_attributes_t *paa = nullptr;
192     // read the AudioAttributes values
193     paa = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
194     const auto jtags =
195             (jstring) env->GetObjectField(jaa, javaAudioAttrFields.fieldFormattedTags);
196     const char* tags = env->GetStringUTFChars(jtags, nullptr);
197     // copying array size -1, char array for tags was calloc'd, no need to NULL-terminate it
198     strncpy(paa->tags, tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1);
199     env->ReleaseStringUTFChars(jtags, tags);
200     paa->usage = (audio_usage_t) env->GetIntField(jaa, javaAudioAttrFields.fieldUsage);
201     paa->content_type =
202             (audio_content_type_t) env->GetIntField(jaa, javaAudioAttrFields.fieldContentType);
203     paa->flags = env->GetIntField(jaa, javaAudioAttrFields.fieldFlags);
204 
205     ALOGV("android_media_SoundPool_native_setup");
206     auto *ap = new SoundPool(maxChannels, paa);
207     if (ap == nullptr) {
208         return -1;
209     }
210 
211     // save pointer to SoundPool C++ object in opaque field in Java object
212     env->SetLongField(thiz, fields.mNativeContext, (jlong) ap);
213 
214     // set callback with weak reference
215     jobject globalWeakRef = env->NewGlobalRef(weakRef);
216     ap->setCallback(android_media_callback, globalWeakRef);
217 
218     // audio attributes were copied in SoundPool creation
219     free(paa);
220 
221     return 0;
222 }
223 
224 static void
android_media_SoundPool_release(JNIEnv * env,jobject thiz)225 android_media_SoundPool_release(JNIEnv *env, jobject thiz)
226 {
227     ALOGV("android_media_SoundPool_release");
228     SoundPool *ap = MusterSoundPool(env, thiz);
229     if (ap != nullptr) {
230 
231         // release weak reference and clear callback
232         auto weakRef = (jobject) ap->getUserData();
233         ap->setCallback(nullptr /* callback */, nullptr /* user */);
234         if (weakRef != nullptr) {
235             env->DeleteGlobalRef(weakRef);
236         }
237 
238         // clear native context
239         env->SetLongField(thiz, fields.mNativeContext, 0);
240         delete ap;
241     }
242 }
243 
244 // ----------------------------------------------------------------------------
245 
246 // Dalvik VM type signatures
247 static JNINativeMethod gMethods[] = {
248     {   "_load",
249         "(Ljava/io/FileDescriptor;JJI)I",
250         (void *)android_media_SoundPool_load_FD
251     },
252     {   "unload",
253         "(I)Z",
254         (void *)android_media_SoundPool_unload
255     },
256     {   "_play",
257         "(IFFIIF)I",
258         (void *)android_media_SoundPool_play
259     },
260     {   "pause",
261         "(I)V",
262         (void *)android_media_SoundPool_pause
263     },
264     {   "resume",
265         "(I)V",
266         (void *)android_media_SoundPool_resume
267     },
268     {   "autoPause",
269         "()V",
270         (void *)android_media_SoundPool_autoPause
271     },
272     {   "autoResume",
273         "()V",
274         (void *)android_media_SoundPool_autoResume
275     },
276     {   "stop",
277         "(I)V",
278         (void *)android_media_SoundPool_stop
279     },
280     {   "_setVolume",
281         "(IFF)V",
282         (void *)android_media_SoundPool_setVolume
283     },
284     {   "_mute",
285         "(Z)V",
286         (void *)android_media_SoundPool_mute
287     },
288     {   "setPriority",
289         "(II)V",
290         (void *)android_media_SoundPool_setPriority
291     },
292     {   "setLoop",
293         "(II)V",
294         (void *)android_media_SoundPool_setLoop
295     },
296     {   "setRate",
297         "(IF)V",
298         (void *)android_media_SoundPool_setRate
299     },
300     {   "native_setup",
301         "(Ljava/lang/Object;ILjava/lang/Object;)I",
302         (void*)android_media_SoundPool_native_setup
303     },
304     {   "native_release",
305         "()V",
306         (void*)android_media_SoundPool_release
307     }
308 };
309 
310 static const char* const kClassPathName = "android/media/SoundPool";
311 
JNI_OnLoad(JavaVM * vm,void *)312 jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
313 {
314     JNIEnv* env = nullptr;
315     jint result = -1;
316     jclass clazz;
317 
318     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
319         ALOGE("ERROR: GetEnv failed\n");
320         return result;
321     }
322     assert(env != nullptr);
323 
324     clazz = env->FindClass(kClassPathName);
325     if (clazz == nullptr) {
326         ALOGE("Can't find %s", kClassPathName);
327         return result;
328     }
329 
330     fields.mNativeContext = env->GetFieldID(clazz, "mNativeContext", "J");
331     if (fields.mNativeContext == nullptr) {
332         ALOGE("Can't find SoundPool.mNativeContext");
333         return result;
334     }
335 
336     fields.mPostEvent = env->GetStaticMethodID(clazz, "postEventFromNative",
337                                                "(Ljava/lang/Object;IIILjava/lang/Object;)V");
338     if (fields.mPostEvent == nullptr) {
339         ALOGE("Can't find android/media/SoundPool.postEventFromNative");
340         return result;
341     }
342 
343     // create a reference to class. Technically, we're leaking this reference
344     // since it's a static object.
345     fields.mSoundPoolClass = (jclass) env->NewGlobalRef(clazz);
346 
347     if (AndroidRuntime::registerNativeMethods(
348                 env, kClassPathName, gMethods, NELEM(gMethods)) < 0) {
349         return result;
350     }
351 
352     // Get the AudioAttributes class and fields
353     jclass audioAttrClass = env->FindClass(kAudioAttributesClassPathName);
354     if (audioAttrClass == nullptr) {
355         ALOGE("Can't find %s", kAudioAttributesClassPathName);
356         return result;
357     }
358     auto audioAttributesClassRef = (jclass)env->NewGlobalRef(audioAttrClass);
359     javaAudioAttrFields.fieldUsage = env->GetFieldID(audioAttributesClassRef, "mUsage", "I");
360     javaAudioAttrFields.fieldContentType
361                                    = env->GetFieldID(audioAttributesClassRef, "mContentType", "I");
362     javaAudioAttrFields.fieldFlags = env->GetFieldID(audioAttributesClassRef, "mFlags", "I");
363     javaAudioAttrFields.fieldFormattedTags =
364             env->GetFieldID(audioAttributesClassRef, "mFormattedTags", "Ljava/lang/String;");
365     env->DeleteGlobalRef(audioAttributesClassRef);
366     if (javaAudioAttrFields.fieldUsage == nullptr
367             || javaAudioAttrFields.fieldContentType == nullptr
368             || javaAudioAttrFields.fieldFlags == nullptr
369             || javaAudioAttrFields.fieldFormattedTags == nullptr) {
370         ALOGE("Can't initialize AudioAttributes fields");
371         return result;
372     }
373 
374     /* success -- return valid version number */
375     return JNI_VERSION_1_4;
376 }
377