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