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