• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <audio_utils/string.h>
24 #include <jni.h>
25 #include <nativehelper/JNIPlatformHelp.h>
26 #include <nativehelper/ScopedUtfChars.h>
27 #include <android_runtime/AndroidRuntime.h>
28 #include "SoundPool.h"
29 
30 using namespace android;
31 
32 static struct fields_t {
33     jfieldID    mNativeContext;
34     jmethodID   mPostEvent;
35     jclass      mSoundPoolClass;
36 } fields;
37 
38 namespace {
39 
40 /**
41  * ObjectManager creates a native "object" on the heap and stores
42  * its pointer in a long field in a Java object.
43  *
44  * The type T must have 3 properties in the current implementation.
45  *    1) A T{} default constructor which represents a nullValue.
46  *    2) T::operator bool() const efficient detection of such a nullValue.
47  *    3) T must be copyable.
48  *
49  * Some examples of such a type T are std::shared_ptr<>, android::sp<>,
50  * std::optional, std::function<>, etc.
51  *
52  * Using set() with a nullValue T results in destroying the underlying native
53  * "object" if it exists.  A nullValue T is returned by get() if there is
54  * no underlying native Object.
55  *
56  * This class is thread safe for multiple access.
57  *
58  * Design notes:
59  * 1) For objects of type T that do not naturally have an "nullValue",
60  *    wrapping with
61  *           a) TOpt, where TOpt = std::optional<T>
62  *           b) TShared, where TShared = std::shared_ptr<T>
63  *
64  * 2) An overload for an explicit equality comparable nullValue such as
65  *    get(..., const T& nullValue) or set(..., const T& nullValue)
66  *    is omitted.  An alternative is to pass a fixed nullValue in the constructor.
67  */
68 template <typename T>
69 class ObjectManager
70 {
71 // Can a jlong hold a pointer?
72 static_assert(sizeof(jlong) >= sizeof(void*));
73 
74 public:
75     // fieldId is associated with a Java long member variable in the object.
76     // ObjectManager will store the native pointer in that field.
77     //
78     // If a native object is set() in that field, it
ObjectManager(jfieldID fieldId)79     explicit ObjectManager(jfieldID fieldId) : mFieldId(fieldId) {}
~ObjectManager()80     ~ObjectManager() {
81         ALOGE_IF(mObjectCount != 0, "%s: mObjectCount: %d should be zero on destruction",
82                 __func__, mObjectCount.load());
83         // Design note: it would be possible to keep a map of the outstanding allocated
84         // objects and force a delete on them on ObjectManager destruction.
85         // The consequences of that is probably worse than keeping them alive.
86     }
87 
88     // Retrieves the associated object, returns nullValue T if not available.
get(JNIEnv * env,jobject thiz)89     T get(JNIEnv *env, jobject thiz) {
90         std::lock_guard lg(mLock);
91         // NOLINTNEXTLINE(performance-no-int-to-ptr)
92         auto ptr = reinterpret_cast<T*>(env->GetLongField(thiz, mFieldId));
93         if (ptr != nullptr) {
94             return *ptr;
95         }
96         return {};
97     }
98 
99     // Sets the object and returns the old one.
100     //
101     // If the old object doesn't exist, then nullValue T is returned.
102     // If the new object is false by operator bool(), the internal object is destroyed.
103     // Note: The old object is returned so if T is a smart pointer, it can be held
104     // by the caller to be deleted outside of any external lock.
105     //
106     // Remember to call set(env, thiz, {}) to destroy the object in the Java
107     // object finalize to avoid orphaned objects on the heap.
set(JNIEnv * env,jobject thiz,const T & newObject)108     T set(JNIEnv *env, jobject thiz, const T& newObject) {
109         std::lock_guard lg(mLock);
110         // NOLINTNEXTLINE(performance-no-int-to-ptr)
111         auto ptr = reinterpret_cast<T*>(env->GetLongField(thiz, mFieldId));
112         if (ptr != nullptr) {
113             T old = std::move(*ptr);  // *ptr will be replaced or deleted.
114             if (newObject) {
115                 env->SetLongField(thiz, mFieldId, (jlong)0);
116                 delete ptr;
117                 --mObjectCount;
118             } else {
119                 *ptr = newObject;
120             }
121             return old;
122         } else {
123              if (newObject) {
124                  env->SetLongField(thiz, mFieldId, (jlong)new T(newObject));
125                  ++mObjectCount;
126              }
127              return {};
128         }
129     }
130 
131     // Returns the number of outstanding objects.
132     //
133     // This is purely for debugging purposes and tracks the number of active Java
134     // objects that have native T objects; hence represents the number of
135     // T heap allocations we have made.
136     //
137     // When all those Java objects have been finalized we expect this to go to 0.
getObjectCount() const138     int32_t getObjectCount() const {
139         return mObjectCount;
140     }
141 
142 private:
143     // NOLINTNEXTLINE(misc-misplaced-const)
144     const jfieldID mFieldId;  // '_jfieldID *const'
145 
146     // mObjectCount is the number of outstanding native T heap allocations we have
147     // made (and thus the number of active Java objects which are associated with them).
148     std::atomic_int32_t mObjectCount{};
149 
150     mutable std::mutex mLock;
151 };
152 
153 // We use SoundPoolManager to associate a native std::shared_ptr<SoundPool>
154 // object with a field in the Java object.
155 //
156 // We can then retrieve the std::shared_ptr<SoundPool> from the object.
157 //
158 // Design notes:
159 // 1) This is based on ObjectManager class.
160 // 2) An alternative that does not require a field in the Java object
161 //    is to create an associative map using as a key a NewWeakGlobalRef
162 //    to the Java object.
163 //    The problem of this method is that lookup is O(N) because comparison
164 //    between the WeakGlobalRef to a JNI jobject LocalRef must be done
165 //    through the JNI IsSameObject() call, hence iterative through the map.
166 //    One advantage of this method is that manual garbage collection
167 //    is possible by checking if the WeakGlobalRef is null equivalent.
168 
getSoundPoolManager()169 auto& getSoundPoolManager() {
170     static ObjectManager<std::shared_ptr<SoundPool>> soundPoolManager(fields.mNativeContext);
171     return soundPoolManager;
172 }
173 
getSoundPool(JNIEnv * env,jobject thiz)174 inline auto getSoundPool(JNIEnv *env, jobject thiz) {
175     return getSoundPoolManager().get(env, thiz);
176 }
177 
178 // Note: one must call setSoundPool(env, thiz, nullptr) to release any native resources
179 // somewhere in the Java object finalize().
setSoundPool(JNIEnv * env,jobject thiz,const std::shared_ptr<SoundPool> & soundPool)180 inline auto setSoundPool(
181         JNIEnv *env, jobject thiz, const std::shared_ptr<SoundPool>& soundPool) {
182     return getSoundPoolManager().set(env, thiz, soundPool);
183 }
184 
185 /**
186  * ConcurrentHashMap is a locked hash map
187  *
188  * As from the name, this class is thread_safe.
189  *
190  * The type V must have 3 properties in the current implementation.
191  *    1) A V{} default constructor which represents a nullValue.
192  *    2) V::operator bool() const efficient detection of such a nullValue.
193  *    3) V must be copyable.
194  *
195  * Note: The Key cannot be a Java LocalRef, as those change between JNI calls.
196  * The Key could be the raw native object pointer if one wanted to associate
197  * extra data with a native object.
198  *
199  * Using set() with a nullValue V results in erasing the key entry.
200  * A nullValue V is returned by get() if there is no underlying entry.
201  *
202  * Design notes:
203  * 1) For objects of type V that do not naturally have a "nullValue",
204  *    wrapping VOpt = std::optional<V> or VShared = std::shared<V> is recommended.
205  *
206  * 2) An overload for an explicit equality comparable nullValue such as
207  *    get(..., const V& nullValue) or set(..., const V& nullValue)
208  *    is omitted.  An alternative is to pass a fixed nullValue into a special
209  *    constructor (omitted) for equality comparisons and return value.
210  *
211  * 3) This ConcurrentHashMap currently allows only one thread at a time.
212  *    It is not optimized for heavy multi-threaded use.
213  */
214 template <typename K, typename V>
215 class ConcurrentHashMap
216 {
217 public:
218 
219     // Sets the value and returns the old one.
220     //
221     // If the old value doesn't exist, then nullValue V is returned.
222     // If the new value is false by operator bool(), the internal value is destroyed.
223     // Note: The old value is returned so if V is a smart pointer, it can be held
224     // by the caller to be deleted outside of any external lock.
225 
set(const K & key,const V & value)226     V set(const K& key, const V& value) {
227         std::lock_guard lg(mLock);
228         auto it = mMap.find(key);
229         if (it == mMap.end()) {
230             if (value) {
231                 mMap[key] = value;
232             }
233             return {};
234         }
235         V oldValue = std::move(it->second);
236         if (value) {
237             it->second = value;
238         } else {
239             mMap.erase(it);
240         }
241         return oldValue;
242     }
243 
244     // Retrieves the associated object, returns nullValue V if not available.
get(const K & key) const245     V get(const K& key) const {
246         std::lock_guard lg(mLock);
247         auto it = mMap.find(key);
248         return it != mMap.end() ? it->second : V{};
249     }
250 private:
251     mutable std::mutex mLock;
252     std::unordered_map<K, V> mMap GUARDED_BY(mLock);
253 };
254 
255 // *jobject is needed to fit the jobject into a std::shared_ptr.
256 // This is the Android type _jobject, but we derive this type as JObjectValue.
257 using JObjectValue = std::remove_pointer_t<jobject>; // _jobject
258 
259 // Check that jobject is really a pointer to JObjectValue.
260 // The JNI contract is that jobject is NULL comparable,
261 // so jobject is pointer equivalent; we check here to be sure.
262 // Note std::remove_ptr_t<NonPointerType> == NonPointerType.
263 static_assert(std::is_same_v<JObjectValue*, jobject>);
264 
265 // *jweak is needed to fit the jweak into a std::shared_ptr.
266 // This is the Android type _jobject, but we derive this type as JWeakValue.
267 using JWeakValue = std::remove_pointer_t<jweak>; // this is just _jobject
268 
269 // Check that jweak is really a pointer to JWeakValue.
270 static_assert(std::is_same_v<JWeakValue*, jweak>);
271 
272 // We store the ancillary data associated with a SoundPool object in a concurrent
273 // hash map indexed on the SoundPool native object pointer.
getSoundPoolJavaRefManager()274 auto& getSoundPoolJavaRefManager() {
275     // Note this can store shared_ptrs to either jweak and jobject,
276     // as the underlying type is identical.
277     static ConcurrentHashMap<SoundPool *, std::shared_ptr<JWeakValue>> concurrentHashMap;
278     return concurrentHashMap;
279 }
280 
281 // make_shared_globalref_from_localref() creates a sharable Java global
282 // reference from a Java local reference. The equivalent type is
283 // std::shared_ptr<_jobject> (where _jobject is JObjectValue,
284 // and _jobject * is jobject),
285 // and the jobject may be retrieved by .get() or pointer dereference.
286 // This encapsulation gives the benefit of std::shared_ptr
287 // ref counting, weak_ptr, etc.
288 //
289 // The Java global reference should be stable between JNI calls.  It is a limited
290 // quantity so sparingly use global references.
291 //
292 // The Android JNI implementation is described here:
293 // https://developer.android.com/training/articles/perf-jni
294 // https://android-developers.googleblog.com/2011/11/jni-local-reference-changes-in-ics.html
295 //
296 // Consider using a weak reference if this is self-referential.
297 [[maybe_unused]]
make_shared_globalref_from_localref(JNIEnv * env,jobject localRef)298 inline auto make_shared_globalref_from_localref(JNIEnv *env, jobject localRef) {
299     return std::shared_ptr<JObjectValue>(
300             env->NewGlobalRef(localRef),
301             [](JObjectValue* object) { // cannot cache env as don't know which thread we're on.
302                 if (object != nullptr) AndroidRuntime::getJNIEnv()->DeleteGlobalRef(object);
303             });
304 }
305 
306 // Create a weak global reference from local ref.
make_shared_weakglobalref_from_localref(JNIEnv * env,jobject localRef)307 inline auto make_shared_weakglobalref_from_localref(JNIEnv *env, jobject localRef) {
308     return std::shared_ptr<JWeakValue>(
309             env->NewWeakGlobalRef(localRef),
310             [](JWeakValue* weak) { // cannot cache env as don't know which thread we're on.
311                 if (weak != nullptr) AndroidRuntime::getJNIEnv()->DeleteWeakGlobalRef(weak);
312             });
313 }
314 
315 // std::unique_ptr<> does not store a type-erased deleter like std::shared_ptr<>.
316 // Define a lambda here to use for the std::unique_ptr<> type definition.
__anone82919020402(JObjectValue* object) 317 auto LocalRefDeleter = [](JObjectValue* object) {
318     if (object != nullptr) AndroidRuntime::getJNIEnv()->DeleteLocalRef(object);
319 };
320 
321 // Create a local reference from another reference.
322 // This is a unique_ptr to avoid the temptation of sharing with other threads.
323 //
324 // This can be used to promote a WeakGlobalRef jweak into a stable LocalRef jobject.
325 //
make_unique_localref_from_ref(JNIEnv * env,jobject object)326 inline auto make_unique_localref_from_ref(JNIEnv *env, jobject object) {
327     return std::unique_ptr<JObjectValue, decltype(LocalRefDeleter)>(
328             env->NewLocalRef(object),
329             LocalRefDeleter);
330 }
331 
332 } // namespace
333 
334 static const char* const kAudioAttributesClassPathName = "android/media/AudioAttributes";
335 struct audio_attributes_fields_t {
336     jfieldID  fieldUsage;        // AudioAttributes.mUsage
337     jfieldID  fieldContentType;  // AudioAttributes.mContentType
338     jfieldID  fieldFlags;        // AudioAttributes.mFlags
339     jfieldID  fieldFormattedTags;// AudioAttributes.mFormattedTags
340 };
341 static audio_attributes_fields_t javaAudioAttrFields;
342 
343 // ----------------------------------------------------------------------------
344 
345 static jint
android_media_SoundPool_load_FD(JNIEnv * env,jobject thiz,jobject fileDescriptor,jlong offset,jlong length,jint priority)346 android_media_SoundPool_load_FD(JNIEnv *env, jobject thiz, jobject fileDescriptor,
347         jlong offset, jlong length, jint priority)
348 {
349     ALOGV("android_media_SoundPool_load_FD");
350     auto soundPool = getSoundPool(env, thiz);
351     if (soundPool == nullptr) return 0;
352     return (jint) soundPool->load(jniGetFDFromFileDescriptor(env, fileDescriptor),
353             int64_t(offset), int64_t(length), int(priority));
354 }
355 
356 static jboolean
android_media_SoundPool_unload(JNIEnv * env,jobject thiz,jint sampleID)357 android_media_SoundPool_unload(JNIEnv *env, jobject thiz, jint sampleID) {
358     ALOGV("android_media_SoundPool_unload\n");
359     auto soundPool = getSoundPool(env, thiz);
360     if (soundPool == nullptr) return JNI_FALSE;
361     return soundPool->unload(sampleID) ? JNI_TRUE : JNI_FALSE;
362 }
363 
364 static jint
android_media_SoundPool_play(JNIEnv * env,jobject thiz,jint sampleID,jfloat leftVolume,jfloat rightVolume,jint priority,jint loop,jfloat rate,jint playerIId)365 android_media_SoundPool_play(JNIEnv *env, jobject thiz, jint sampleID,
366         jfloat leftVolume, jfloat rightVolume, jint priority, jint loop,
367         jfloat rate, jint playerIId)
368 {
369     ALOGV("android_media_SoundPool_play\n");
370     auto soundPool = getSoundPool(env, thiz);
371     if (soundPool == nullptr) return 0;
372 
373     return (jint) soundPool->play(sampleID,
374                                   leftVolume,
375                                   rightVolume,
376                                   priority,
377                                   loop,
378                                   rate,
379                                   playerIId);
380 }
381 
382 static void
android_media_SoundPool_pause(JNIEnv * env,jobject thiz,jint channelID)383 android_media_SoundPool_pause(JNIEnv *env, jobject thiz, jint channelID)
384 {
385     ALOGV("android_media_SoundPool_pause");
386     auto soundPool = getSoundPool(env, thiz);
387     if (soundPool == nullptr) return;
388     soundPool->pause(channelID);
389 }
390 
391 static void
android_media_SoundPool_resume(JNIEnv * env,jobject thiz,jint channelID)392 android_media_SoundPool_resume(JNIEnv *env, jobject thiz, jint channelID)
393 {
394     ALOGV("android_media_SoundPool_resume");
395     auto soundPool = getSoundPool(env, thiz);
396     if (soundPool == nullptr) return;
397     soundPool->resume(channelID);
398 }
399 
400 static void
android_media_SoundPool_autoPause(JNIEnv * env,jobject thiz)401 android_media_SoundPool_autoPause(JNIEnv *env, jobject thiz)
402 {
403     ALOGV("android_media_SoundPool_autoPause");
404     auto soundPool = getSoundPool(env, thiz);
405     if (soundPool == nullptr) return;
406     soundPool->autoPause();
407 }
408 
409 static void
android_media_SoundPool_autoResume(JNIEnv * env,jobject thiz)410 android_media_SoundPool_autoResume(JNIEnv *env, jobject thiz)
411 {
412     ALOGV("android_media_SoundPool_autoResume");
413     auto soundPool = getSoundPool(env, thiz);
414     if (soundPool == nullptr) return;
415     soundPool->autoResume();
416 }
417 
418 static void
android_media_SoundPool_stop(JNIEnv * env,jobject thiz,jint channelID)419 android_media_SoundPool_stop(JNIEnv *env, jobject thiz, jint channelID)
420 {
421     ALOGV("android_media_SoundPool_stop");
422     auto soundPool = getSoundPool(env, thiz);
423     if (soundPool == nullptr) return;
424     soundPool->stop(channelID);
425 }
426 
427 static void
android_media_SoundPool_setVolume(JNIEnv * env,jobject thiz,jint channelID,jfloat leftVolume,jfloat rightVolume)428 android_media_SoundPool_setVolume(JNIEnv *env, jobject thiz, jint channelID,
429         jfloat leftVolume, jfloat rightVolume)
430 {
431     ALOGV("android_media_SoundPool_setVolume");
432     auto soundPool = getSoundPool(env, thiz);
433     if (soundPool == nullptr) return;
434     soundPool->setVolume(channelID, (float) leftVolume, (float) rightVolume);
435 }
436 
437 static void
android_media_SoundPool_mute(JNIEnv * env,jobject thiz,jboolean muting)438 android_media_SoundPool_mute(JNIEnv *env, jobject thiz, jboolean muting)
439 {
440     ALOGV("android_media_SoundPool_mute(%d)", muting);
441     auto soundPool = getSoundPool(env, thiz);
442     if (soundPool == nullptr) return;
443     soundPool->mute(muting == JNI_TRUE);
444 }
445 
446 static void
android_media_SoundPool_setPriority(JNIEnv * env,jobject thiz,jint channelID,jint priority)447 android_media_SoundPool_setPriority(JNIEnv *env, jobject thiz, jint channelID,
448         jint priority)
449 {
450     ALOGV("android_media_SoundPool_setPriority");
451     auto soundPool = getSoundPool(env, thiz);
452     if (soundPool == nullptr) return;
453     soundPool->setPriority(channelID, (int) priority);
454 }
455 
456 static void
android_media_SoundPool_setLoop(JNIEnv * env,jobject thiz,jint channelID,int loop)457 android_media_SoundPool_setLoop(JNIEnv *env, jobject thiz, jint channelID,
458         int loop)
459 {
460     ALOGV("android_media_SoundPool_setLoop");
461     auto soundPool = getSoundPool(env, thiz);
462     if (soundPool == nullptr) return;
463     soundPool->setLoop(channelID, loop);
464 }
465 
466 static void
android_media_SoundPool_setRate(JNIEnv * env,jobject thiz,jint channelID,jfloat rate)467 android_media_SoundPool_setRate(JNIEnv *env, jobject thiz, jint channelID,
468        jfloat rate)
469 {
470     ALOGV("android_media_SoundPool_setRate");
471     auto soundPool = getSoundPool(env, thiz);
472     if (soundPool == nullptr) return;
473     soundPool->setRate(channelID, (float) rate);
474 }
475 
android_media_callback(SoundPoolEvent event,SoundPool * soundPool,void * user)476 static void android_media_callback(SoundPoolEvent event, SoundPool* soundPool, void* user)
477 {
478     ALOGV("callback: (%d, %d, %d, %p, %p)", event.mMsg, event.mArg1, event.mArg2, soundPool, user);
479     auto weakRef = getSoundPoolJavaRefManager().get(soundPool); // shared_ptr to WeakRef
480     if (weakRef == nullptr) {
481         ALOGD("%s: no weak ref, object released, ignoring callback", __func__);
482         return;
483     }
484     JNIEnv *env = AndroidRuntime::getJNIEnv();
485     // "promote" the WeakGlobalRef into a LocalRef.
486     auto javaSoundPool = make_unique_localref_from_ref(env, weakRef.get());
487     if (!javaSoundPool) {
488         ALOGW("%s: weak reference promotes to null (release() not called?), "
489                 "ignoring callback", __func__);
490         return;
491     }
492     env->CallVoidMethod(javaSoundPool.get(), fields.mPostEvent,
493             event.mMsg, event.mArg1, event.mArg2, nullptr /* object */);
494 
495     if (env->ExceptionCheck() != JNI_FALSE) {
496         ALOGE("%s: Uncaught exception returned from Java callback", __func__);
497         env->ExceptionDescribe();
498         env->ExceptionClear(); // Just clear it, hopefully all is ok.
499     }
500 }
501 
502 static jint
android_media_SoundPool_native_setup(JNIEnv * env,jobject thiz,jint maxChannels,jobject jaa,jstring opPackageName)503 android_media_SoundPool_native_setup(JNIEnv *env, jobject thiz,
504         jint maxChannels, jobject jaa, jstring opPackageName)
505 {
506     ALOGV("android_media_SoundPool_native_setup");
507     if (jaa == nullptr) {
508         ALOGE("Error creating SoundPool: invalid audio attributes");
509         return -1;
510     }
511 
512     // Use the AUDIO_ATTRIBUTES_INITIALIZER here to ensure all non-relevant fields are
513     // initialized properly. (note that .source is not explicitly initialized here).
514     audio_attributes_t audioAttributes = AUDIO_ATTRIBUTES_INITIALIZER;
515     // read the AudioAttributes values
516     const auto jtags =
517             (jstring) env->GetObjectField(jaa, javaAudioAttrFields.fieldFormattedTags);
518     const char* tags = env->GetStringUTFChars(jtags, nullptr);
519     // infers array size and guarantees zero termination (does not zero fill to the end).
520     audio_utils_strlcpy(audioAttributes.tags, tags);
521     env->ReleaseStringUTFChars(jtags, tags);
522     audioAttributes.usage =
523             (audio_usage_t) env->GetIntField(jaa, javaAudioAttrFields.fieldUsage);
524     audioAttributes.content_type =
525             (audio_content_type_t) env->GetIntField(jaa, javaAudioAttrFields.fieldContentType);
526     audioAttributes.flags =
527             (audio_flags_mask_t) env->GetIntField(jaa, javaAudioAttrFields.fieldFlags);
528     ScopedUtfChars opPackageNameStr(env, opPackageName);
529     auto soundPool = std::make_shared<SoundPool>(
530             maxChannels, audioAttributes, opPackageNameStr.c_str());
531     soundPool->setCallback(android_media_callback, nullptr /* user */);
532 
533     // register with SoundPoolManager.
534     auto oldSoundPool = setSoundPool(env, thiz, soundPool);
535     // register Java SoundPool WeakRef using native SoundPool * as the key, for the callback.
536     auto oldSoundPoolJavaRef = getSoundPoolJavaRefManager().set(
537             soundPool.get(), make_shared_weakglobalref_from_localref(env, thiz));
538 
539     ALOGW_IF(oldSoundPool != nullptr, "%s: Aliased SoundPool object %p",
540             __func__, oldSoundPool.get());
541     return 0;
542 }
543 
544 static void
android_media_SoundPool_release(JNIEnv * env,jobject thiz)545 android_media_SoundPool_release(JNIEnv *env, jobject thiz)
546 {
547     ALOGV("android_media_SoundPool_release");
548 
549     // Remove us from SoundPoolManager.
550 
551     auto oldSoundPool = setSoundPool(env, thiz, nullptr);
552     if (oldSoundPool != nullptr) {
553         // Note: setting the weak ref is thread safe in case there is a callback
554         // simultaneously occurring.
555         auto oldSoundPoolJavaRef = getSoundPoolJavaRefManager().set(oldSoundPool.get(), nullptr);
556     }
557     // destructor to oldSoundPool should occur at exit.
558 }
559 
560 // ----------------------------------------------------------------------------
561 
562 // Dalvik VM type signatures
563 static JNINativeMethod gMethods[] = {
564     {   "_load",
565         "(Ljava/io/FileDescriptor;JJI)I",
566         (void *)android_media_SoundPool_load_FD
567     },
568     {   "unload",
569         "(I)Z",
570         (void *)android_media_SoundPool_unload
571     },
572     {   "_play",
573         "(IFFIIFI)I",
574         (void *)android_media_SoundPool_play
575     },
576     {   "pause",
577         "(I)V",
578         (void *)android_media_SoundPool_pause
579     },
580     {   "resume",
581         "(I)V",
582         (void *)android_media_SoundPool_resume
583     },
584     {   "autoPause",
585         "()V",
586         (void *)android_media_SoundPool_autoPause
587     },
588     {   "autoResume",
589         "()V",
590         (void *)android_media_SoundPool_autoResume
591     },
592     {   "stop",
593         "(I)V",
594         (void *)android_media_SoundPool_stop
595     },
596     {   "_setVolume",
597         "(IFF)V",
598         (void *)android_media_SoundPool_setVolume
599     },
600     {   "_mute",
601         "(Z)V",
602         (void *)android_media_SoundPool_mute
603     },
604     {   "setPriority",
605         "(II)V",
606         (void *)android_media_SoundPool_setPriority
607     },
608     {   "setLoop",
609         "(II)V",
610         (void *)android_media_SoundPool_setLoop
611     },
612     {   "setRate",
613         "(IF)V",
614         (void *)android_media_SoundPool_setRate
615     },
616     {   "native_setup",
617         "(ILjava/lang/Object;Ljava/lang/String;)I",
618         (void*)android_media_SoundPool_native_setup
619     },
620     {   "native_release",
621         "()V",
622         (void*)android_media_SoundPool_release
623     }
624 };
625 
626 static const char* const kClassPathName = "android/media/SoundPool";
627 
JNI_OnLoad(JavaVM * vm,void *)628 jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
629 {
630     JNIEnv* env = nullptr;
631     jint result = -1;
632     jclass clazz;
633 
634     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
635         ALOGE("ERROR: GetEnv failed\n");
636         return result;
637     }
638     assert(env != nullptr);
639 
640     clazz = env->FindClass(kClassPathName);
641     if (clazz == nullptr) {
642         ALOGE("Can't find %s", kClassPathName);
643         return result;
644     }
645 
646     fields.mNativeContext = env->GetFieldID(clazz, "mNativeContext", "J");
647     if (fields.mNativeContext == nullptr) {
648         ALOGE("Can't find SoundPool.mNativeContext");
649         return result;
650     }
651 
652     fields.mPostEvent = env->GetMethodID(
653             clazz, "postEventFromNative", "(IIILjava/lang/Object;)V");
654     if (fields.mPostEvent == nullptr) {
655         ALOGE("Can't find android/media/SoundPool.postEventFromNative");
656         return result;
657     }
658 
659     // create a reference to class. Technically, we're leaking this reference
660     // since it's a static object.
661     fields.mSoundPoolClass = (jclass) env->NewGlobalRef(clazz);
662 
663     if (AndroidRuntime::registerNativeMethods(
664                 env, kClassPathName, gMethods, NELEM(gMethods)) < 0) {
665         return result;
666     }
667 
668     // Get the AudioAttributes class and fields
669     jclass audioAttrClass = env->FindClass(kAudioAttributesClassPathName);
670     if (audioAttrClass == nullptr) {
671         ALOGE("Can't find %s", kAudioAttributesClassPathName);
672         return result;
673     }
674     auto audioAttributesClassRef = (jclass)env->NewGlobalRef(audioAttrClass);
675     javaAudioAttrFields.fieldUsage = env->GetFieldID(audioAttributesClassRef, "mUsage", "I");
676     javaAudioAttrFields.fieldContentType
677                                    = env->GetFieldID(audioAttributesClassRef, "mContentType", "I");
678     javaAudioAttrFields.fieldFlags = env->GetFieldID(audioAttributesClassRef, "mFlags", "I");
679     javaAudioAttrFields.fieldFormattedTags =
680             env->GetFieldID(audioAttributesClassRef, "mFormattedTags", "Ljava/lang/String;");
681     env->DeleteGlobalRef(audioAttributesClassRef);
682     if (javaAudioAttrFields.fieldUsage == nullptr
683             || javaAudioAttrFields.fieldContentType == nullptr
684             || javaAudioAttrFields.fieldFlags == nullptr
685             || javaAudioAttrFields.fieldFormattedTags == nullptr) {
686         ALOGE("Can't initialize AudioAttributes fields");
687         return result;
688     }
689 
690     /* success -- return valid version number */
691     return JNI_VERSION_1_4;
692 }
693