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