The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_media_MediaPlayer.cpp |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaPlayer-JNI" |
| 20 | #include "utils/Log.h" |
| 21 | |
| 22 | #include <media/mediaplayer.h> |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 23 | #include <media/MediaPlayerInterface.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <assert.h> |
| 26 | #include <limits.h> |
| 27 | #include <unistd.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <utils/threads.h> |
| 30 | #include "jni.h" |
| 31 | #include "JNIHelp.h" |
| 32 | #include "android_runtime/AndroidRuntime.h" |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 33 | #include "utils/Errors.h" // for status_t |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 34 | #include "utils/KeyedVector.h" |
| 35 | #include "utils/String8.h" |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 36 | #include "android_util_Binder.h" |
| 37 | #include <binder/Parcel.h> |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 38 | #include <gui/SurfaceTexture.h> |
| 39 | #include <gui/ISurfaceTexture.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 40 | #include <surfaceflinger/Surface.h> |
Gloria Wang | d211f41 | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 41 | #include <binder/IPCThreadState.h> |
| 42 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | using namespace android; |
| 47 | |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
| 50 | struct fields_t { |
| 51 | jfieldID context; |
| 52 | jfieldID surface; |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 53 | jfieldID surfaceTexture; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | /* actually in android.view.Surface XXX */ |
| 55 | jfieldID surface_native; |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 56 | // actually in android.graphics.SurfaceTexture |
| 57 | jfieldID surfaceTexture_native; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 59 | jmethodID post_event; |
| 60 | }; |
| 61 | static fields_t fields; |
| 62 | |
| 63 | static Mutex sLock; |
| 64 | |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | // ref-counted object for callbacks |
| 67 | class JNIMediaPlayerListener: public MediaPlayerListener |
| 68 | { |
| 69 | public: |
| 70 | JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz); |
| 71 | ~JNIMediaPlayerListener(); |
| 72 | void notify(int msg, int ext1, int ext2); |
| 73 | private: |
| 74 | JNIMediaPlayerListener(); |
| 75 | jclass mClass; // Reference to MediaPlayer class |
| 76 | jobject mObject; // Weak ref to MediaPlayer Java object to call on |
| 77 | }; |
| 78 | |
| 79 | JNIMediaPlayerListener::JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz) |
| 80 | { |
| 81 | |
| 82 | // Hold onto the MediaPlayer class for use in calling the static method |
| 83 | // that posts events to the application thread. |
| 84 | jclass clazz = env->GetObjectClass(thiz); |
| 85 | if (clazz == NULL) { |
| 86 | LOGE("Can't find android/media/MediaPlayer"); |
| 87 | jniThrowException(env, "java/lang/Exception", NULL); |
| 88 | return; |
| 89 | } |
| 90 | mClass = (jclass)env->NewGlobalRef(clazz); |
| 91 | |
| 92 | // We use a weak reference so the MediaPlayer object can be garbage collected. |
| 93 | // The reference is only used as a proxy for callbacks. |
| 94 | mObject = env->NewGlobalRef(weak_thiz); |
| 95 | } |
| 96 | |
| 97 | JNIMediaPlayerListener::~JNIMediaPlayerListener() |
| 98 | { |
| 99 | // remove global references |
| 100 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 101 | env->DeleteGlobalRef(mObject); |
| 102 | env->DeleteGlobalRef(mClass); |
| 103 | } |
| 104 | |
| 105 | void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2) |
| 106 | { |
| 107 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 108 | env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0); |
| 109 | } |
| 110 | |
| 111 | // ---------------------------------------------------------------------------- |
| 112 | |
Marco Nelissen | 0fc736f32 | 2009-07-10 09:34:59 -0700 | [diff] [blame] | 113 | static Surface* get_surface(JNIEnv* env, jobject clazz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | { |
Marco Nelissen | 0fc736f32 | 2009-07-10 09:34:59 -0700 | [diff] [blame] | 115 | return (Surface*)env->GetIntField(clazz, fields.surface_native); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 118 | sp<ISurfaceTexture> getSurfaceTexture(JNIEnv* env, jobject clazz) |
| 119 | { |
| 120 | sp<ISurfaceTexture> surfaceTexture( |
| 121 | (ISurfaceTexture*)env->GetIntField(clazz, fields.surfaceTexture_native)); |
| 122 | return surfaceTexture; |
| 123 | } |
| 124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | static sp<MediaPlayer> getMediaPlayer(JNIEnv* env, jobject thiz) |
| 126 | { |
| 127 | Mutex::Autolock l(sLock); |
| 128 | MediaPlayer* const p = (MediaPlayer*)env->GetIntField(thiz, fields.context); |
| 129 | return sp<MediaPlayer>(p); |
| 130 | } |
| 131 | |
| 132 | static sp<MediaPlayer> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer>& player) |
| 133 | { |
| 134 | Mutex::Autolock l(sLock); |
| 135 | sp<MediaPlayer> old = (MediaPlayer*)env->GetIntField(thiz, fields.context); |
| 136 | if (player.get()) { |
| 137 | player->incStrong(thiz); |
| 138 | } |
| 139 | if (old != 0) { |
| 140 | old->decStrong(thiz); |
| 141 | } |
| 142 | env->SetIntField(thiz, fields.context, (int)player.get()); |
| 143 | return old; |
| 144 | } |
| 145 | |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 146 | // If exception is NULL and opStatus is not OK, this method sends an error |
| 147 | // event to the client application; otherwise, if exception is not NULL and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | // opStatus is not OK, this method throws the given exception to the client |
| 149 | // application. |
| 150 | static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message) |
| 151 | { |
| 152 | if (exception == NULL) { // Don't throw exception. Instead, send an event. |
| 153 | if (opStatus != (status_t) OK) { |
| 154 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 155 | if (mp != 0) mp->notify(MEDIA_ERROR, opStatus, 0); |
| 156 | } |
| 157 | } else { // Throw exception! |
| 158 | if ( opStatus == (status_t) INVALID_OPERATION ) { |
| 159 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 160 | } else if ( opStatus != (status_t) OK ) { |
| 161 | if (strlen(message) > 230) { |
| 162 | // if the message is too long, don't bother displaying the status code |
| 163 | jniThrowException( env, exception, message); |
| 164 | } else { |
| 165 | char msg[256]; |
| 166 | // append the status code to the message |
| 167 | sprintf(msg, "%s: status=0x%X", message, opStatus); |
| 168 | jniThrowException( env, exception, msg); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static void |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 175 | android_media_MediaPlayer_setDataSourceAndHeaders( |
| 176 | JNIEnv *env, jobject thiz, jstring path, jobject headers) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 178 | if (mp == NULL ) { |
| 179 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | if (path == NULL) { |
| 184 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 189 | if (pathStr == NULL) { // Out of memory |
| 190 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 191 | return; |
| 192 | } |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 193 | |
| 194 | // headers is a Map<String, String>. |
| 195 | // We build a similar KeyedVector out of it. |
| 196 | KeyedVector<String8, String8> headersVector; |
| 197 | if (headers) { |
| 198 | // Get the Map's entry Set. |
| 199 | jclass mapClass = env->FindClass("java/util/Map"); |
| 200 | |
| 201 | jmethodID entrySet = |
| 202 | env->GetMethodID(mapClass, "entrySet", "()Ljava/util/Set;"); |
| 203 | |
| 204 | jobject set = env->CallObjectMethod(headers, entrySet); |
| 205 | // Obtain an iterator over the Set |
| 206 | jclass setClass = env->FindClass("java/util/Set"); |
| 207 | |
| 208 | jmethodID iterator = |
| 209 | env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;"); |
| 210 | |
| 211 | jobject iter = env->CallObjectMethod(set, iterator); |
| 212 | // Get the Iterator method IDs |
| 213 | jclass iteratorClass = env->FindClass("java/util/Iterator"); |
| 214 | jmethodID hasNext = env->GetMethodID(iteratorClass, "hasNext", "()Z"); |
| 215 | |
| 216 | jmethodID next = |
| 217 | env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;"); |
| 218 | |
| 219 | // Get the Entry class method IDs |
| 220 | jclass entryClass = env->FindClass("java/util/Map$Entry"); |
| 221 | |
| 222 | jmethodID getKey = |
| 223 | env->GetMethodID(entryClass, "getKey", "()Ljava/lang/Object;"); |
| 224 | |
| 225 | jmethodID getValue = |
| 226 | env->GetMethodID(entryClass, "getValue", "()Ljava/lang/Object;"); |
| 227 | |
| 228 | // Iterate over the entry Set |
| 229 | while (env->CallBooleanMethod(iter, hasNext)) { |
| 230 | jobject entry = env->CallObjectMethod(iter, next); |
| 231 | jstring key = (jstring) env->CallObjectMethod(entry, getKey); |
| 232 | jstring value = (jstring) env->CallObjectMethod(entry, getValue); |
| 233 | |
| 234 | const char* keyStr = env->GetStringUTFChars(key, NULL); |
| 235 | if (!keyStr) { // Out of memory |
| 236 | jniThrowException( |
| 237 | env, "java/lang/RuntimeException", "Out of memory"); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | const char* valueStr = env->GetStringUTFChars(value, NULL); |
| 242 | if (!valueStr) { // Out of memory |
| 243 | jniThrowException( |
| 244 | env, "java/lang/RuntimeException", "Out of memory"); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | headersVector.add(String8(keyStr), String8(valueStr)); |
| 249 | |
| 250 | env->DeleteLocalRef(entry); |
| 251 | env->ReleaseStringUTFChars(key, keyStr); |
| 252 | env->DeleteLocalRef(key); |
| 253 | env->ReleaseStringUTFChars(value, valueStr); |
| 254 | env->DeleteLocalRef(value); |
| 255 | } |
| 256 | |
| 257 | env->DeleteLocalRef(entryClass); |
| 258 | env->DeleteLocalRef(iteratorClass); |
| 259 | env->DeleteLocalRef(iter); |
| 260 | env->DeleteLocalRef(setClass); |
| 261 | env->DeleteLocalRef(set); |
| 262 | env->DeleteLocalRef(mapClass); |
| 263 | } |
| 264 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 265 | LOGV("setDataSource: path %s", pathStr); |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 266 | status_t opStatus = |
| 267 | mp->setDataSource( |
| 268 | String8(pathStr), |
| 269 | headers ? &headersVector : NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | |
| 271 | // Make sure that local ref is released before a potential exception |
| 272 | env->ReleaseStringUTFChars(path, pathStr); |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 273 | |
| 274 | process_media_player_call( |
| 275 | env, thiz, opStatus, "java/io/IOException", |
| 276 | "setDataSource failed." ); |
| 277 | } |
| 278 | |
| 279 | static void |
| 280 | android_media_MediaPlayer_setDataSource(JNIEnv *env, jobject thiz, jstring path) |
| 281 | { |
| 282 | android_media_MediaPlayer_setDataSourceAndHeaders(env, thiz, path, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void |
| 286 | android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) |
| 287 | { |
| 288 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 289 | if (mp == NULL ) { |
| 290 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | if (fileDescriptor == NULL) { |
| 295 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 296 | return; |
| 297 | } |
| 298 | int fd = getParcelFileDescriptorFD(env, fileDescriptor); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 299 | LOGV("setDataSourceFD: fd %d", fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." ); |
| 301 | } |
| 302 | |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 303 | static void setVideoSurfaceOrSurfaceTexture( |
| 304 | const sp<MediaPlayer>& mp, JNIEnv *env, jobject thiz, const char *prefix) |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 305 | { |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 306 | // The Java MediaPlayer class makes sure that at most one of mSurface and |
| 307 | // mSurfaceTexture is non-null. But just in case, we give priority to |
| 308 | // mSurface over mSurfaceTexture. |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 309 | jobject surface = env->GetObjectField(thiz, fields.surface); |
| 310 | if (surface != NULL) { |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 311 | sp<Surface> native_surface(get_surface(env, surface)); |
| 312 | LOGV("%s: surface=%p (id=%d)", prefix, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 313 | native_surface.get(), native_surface->getIdentity()); |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 314 | mp->setVideoSurface(native_surface); |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 315 | } else { |
| 316 | jobject surfaceTexture = env->GetObjectField(thiz, fields.surfaceTexture); |
| 317 | if (surfaceTexture != NULL) { |
| 318 | sp<ISurfaceTexture> native_surfaceTexture( |
| 319 | getSurfaceTexture(env, surfaceTexture)); |
| 320 | LOGV("%s: texture=%p (id=%d)", prefix, |
| 321 | native_surfaceTexture.get(), native_surfaceTexture->getIdentity()); |
| 322 | mp->setVideoSurfaceTexture(native_surfaceTexture); |
| 323 | } |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | static void |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 328 | android_media_MediaPlayer_setVideoSurfaceOrSurfaceTexture(JNIEnv *env, jobject thiz) |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 329 | { |
| 330 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 331 | if (mp == NULL ) { |
| 332 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 333 | return; |
| 334 | } |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 335 | setVideoSurfaceOrSurfaceTexture(mp, env, thiz, |
| 336 | "_setVideoSurfaceOrSurfaceTexture"); |
Dave Sparks | 8b0b174 | 2009-05-29 09:01:20 -0700 | [diff] [blame] | 337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | |
| 339 | static void |
| 340 | android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz) |
| 341 | { |
| 342 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 343 | if (mp == NULL ) { |
| 344 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 345 | return; |
| 346 | } |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 347 | setVideoSurfaceOrSurfaceTexture(mp, env, thiz, "prepare"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." ); |
| 349 | } |
| 350 | |
| 351 | static void |
| 352 | android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz) |
| 353 | { |
| 354 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 355 | if (mp == NULL ) { |
| 356 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 357 | return; |
| 358 | } |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 359 | setVideoSurfaceOrSurfaceTexture(mp, env, thiz, "prepareAsync"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." ); |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | android_media_MediaPlayer_start(JNIEnv *env, jobject thiz) |
| 365 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 366 | LOGV("start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 368 | if (mp == NULL ) { |
| 369 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 370 | return; |
| 371 | } |
| 372 | process_media_player_call( env, thiz, mp->start(), NULL, NULL ); |
| 373 | } |
| 374 | |
| 375 | static void |
| 376 | android_media_MediaPlayer_stop(JNIEnv *env, jobject thiz) |
| 377 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 378 | LOGV("stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 380 | if (mp == NULL ) { |
| 381 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 382 | return; |
| 383 | } |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 384 | process_media_player_call( env, thiz, mp->stop(), NULL, NULL ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static void |
| 388 | android_media_MediaPlayer_pause(JNIEnv *env, jobject thiz) |
| 389 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 390 | LOGV("pause"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 392 | if (mp == NULL ) { |
| 393 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 394 | return; |
| 395 | } |
| 396 | process_media_player_call( env, thiz, mp->pause(), NULL, NULL ); |
| 397 | } |
| 398 | |
| 399 | static jboolean |
| 400 | android_media_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz) |
| 401 | { |
| 402 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 403 | if (mp == NULL ) { |
| 404 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 405 | return false; |
| 406 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 407 | const jboolean is_playing = mp->isPlaying(); |
| 408 | |
| 409 | LOGV("isPlaying: %d", is_playing); |
| 410 | return is_playing; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void |
| 414 | android_media_MediaPlayer_seekTo(JNIEnv *env, jobject thiz, int msec) |
| 415 | { |
| 416 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 417 | if (mp == NULL ) { |
| 418 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 419 | return; |
| 420 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 421 | LOGV("seekTo: %d(msec)", msec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | process_media_player_call( env, thiz, mp->seekTo(msec), NULL, NULL ); |
| 423 | } |
| 424 | |
| 425 | static int |
| 426 | android_media_MediaPlayer_getVideoWidth(JNIEnv *env, jobject thiz) |
| 427 | { |
| 428 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 429 | if (mp == NULL ) { |
| 430 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 431 | return 0; |
| 432 | } |
| 433 | int w; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 434 | if (0 != mp->getVideoWidth(&w)) { |
| 435 | LOGE("getVideoWidth failed"); |
| 436 | w = 0; |
| 437 | } |
| 438 | LOGV("getVideoWidth: %d", w); |
| 439 | return w; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static int |
| 443 | android_media_MediaPlayer_getVideoHeight(JNIEnv *env, jobject thiz) |
| 444 | { |
| 445 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 446 | if (mp == NULL ) { |
| 447 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 448 | return 0; |
| 449 | } |
| 450 | int h; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 451 | if (0 != mp->getVideoHeight(&h)) { |
| 452 | LOGE("getVideoHeight failed"); |
| 453 | h = 0; |
| 454 | } |
| 455 | LOGV("getVideoHeight: %d", h); |
| 456 | return h; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | static int |
| 461 | android_media_MediaPlayer_getCurrentPosition(JNIEnv *env, jobject thiz) |
| 462 | { |
| 463 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 464 | if (mp == NULL ) { |
| 465 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 466 | return 0; |
| 467 | } |
| 468 | int msec; |
| 469 | process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL ); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 470 | LOGV("getCurrentPosition: %d (msec)", msec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | return msec; |
| 472 | } |
| 473 | |
| 474 | static int |
| 475 | android_media_MediaPlayer_getDuration(JNIEnv *env, jobject thiz) |
| 476 | { |
| 477 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 478 | if (mp == NULL ) { |
| 479 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 480 | return 0; |
| 481 | } |
| 482 | int msec; |
| 483 | process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL ); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 484 | LOGV("getDuration: %d (msec)", msec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | return msec; |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | android_media_MediaPlayer_reset(JNIEnv *env, jobject thiz) |
| 490 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 491 | LOGV("reset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 493 | if (mp == NULL ) { |
| 494 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 495 | return; |
| 496 | } |
| 497 | process_media_player_call( env, thiz, mp->reset(), NULL, NULL ); |
| 498 | } |
| 499 | |
| 500 | static void |
| 501 | android_media_MediaPlayer_setAudioStreamType(JNIEnv *env, jobject thiz, int streamtype) |
| 502 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 503 | LOGV("setAudioStreamType: %d", streamtype); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 505 | if (mp == NULL ) { |
| 506 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 507 | return; |
| 508 | } |
| 509 | process_media_player_call( env, thiz, mp->setAudioStreamType(streamtype) , NULL, NULL ); |
| 510 | } |
| 511 | |
| 512 | static void |
| 513 | android_media_MediaPlayer_setLooping(JNIEnv *env, jobject thiz, jboolean looping) |
| 514 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 515 | LOGV("setLooping: %d", looping); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 517 | if (mp == NULL ) { |
| 518 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 519 | return; |
| 520 | } |
| 521 | process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL ); |
| 522 | } |
| 523 | |
| 524 | static jboolean |
| 525 | android_media_MediaPlayer_isLooping(JNIEnv *env, jobject thiz) |
| 526 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 527 | LOGV("isLooping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 529 | if (mp == NULL ) { |
| 530 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 531 | return false; |
| 532 | } |
| 533 | return mp->isLooping(); |
| 534 | } |
| 535 | |
| 536 | static void |
| 537 | android_media_MediaPlayer_setVolume(JNIEnv *env, jobject thiz, float leftVolume, float rightVolume) |
| 538 | { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 539 | LOGV("setVolume: left %f right %f", leftVolume, rightVolume); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 541 | if (mp == NULL ) { |
| 542 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 543 | return; |
| 544 | } |
| 545 | process_media_player_call( env, thiz, mp->setVolume(leftVolume, rightVolume), NULL, NULL ); |
| 546 | } |
| 547 | |
| 548 | // FIXME: deprecated |
| 549 | static jobject |
| 550 | android_media_MediaPlayer_getFrameAt(JNIEnv *env, jobject thiz, jint msec) |
| 551 | { |
| 552 | return NULL; |
| 553 | } |
| 554 | |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 555 | |
| 556 | // Sends the request and reply parcels to the media player via the |
| 557 | // binder interface. |
| 558 | static jint |
| 559 | android_media_MediaPlayer_invoke(JNIEnv *env, jobject thiz, |
| 560 | jobject java_request, jobject java_reply) |
| 561 | { |
| 562 | sp<MediaPlayer> media_player = getMediaPlayer(env, thiz); |
| 563 | if (media_player == NULL ) { |
| 564 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 565 | return UNKNOWN_ERROR; |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
| 569 | Parcel *request = parcelForJavaObject(env, java_request); |
| 570 | Parcel *reply = parcelForJavaObject(env, java_reply); |
| 571 | |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 572 | // Don't use process_media_player_call which use the async loop to |
| 573 | // report errors, instead returns the status. |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 574 | return media_player->invoke(*request, reply); |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 577 | // Sends the new filter to the client. |
| 578 | static jint |
| 579 | android_media_MediaPlayer_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request) |
| 580 | { |
| 581 | sp<MediaPlayer> media_player = getMediaPlayer(env, thiz); |
| 582 | if (media_player == NULL ) { |
| 583 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 584 | return UNKNOWN_ERROR; |
| 585 | } |
| 586 | |
| 587 | Parcel *filter = parcelForJavaObject(env, request); |
| 588 | |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 589 | if (filter == NULL ) { |
| 590 | jniThrowException(env, "java/lang/RuntimeException", "Filter is null"); |
| 591 | return UNKNOWN_ERROR; |
| 592 | } |
| 593 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 594 | return media_player->setMetadataFilter(*filter); |
| 595 | } |
| 596 | |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 597 | static jboolean |
| 598 | android_media_MediaPlayer_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only, |
| 599 | jboolean apply_filter, jobject reply) |
| 600 | { |
| 601 | sp<MediaPlayer> media_player = getMediaPlayer(env, thiz); |
| 602 | if (media_player == NULL ) { |
| 603 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | Parcel *metadata = parcelForJavaObject(env, reply); |
| 608 | |
| 609 | if (metadata == NULL ) { |
| 610 | jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null"); |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | metadata->freeData(); |
| 615 | // On return metadata is positioned at the beginning of the |
| 616 | // metadata. Note however that the parcel actually starts with the |
| 617 | // return code so you should not rewind the parcel using |
| 618 | // setDataPosition(0). |
| 619 | return media_player->getMetadata(update_only, apply_filter, metadata) == OK; |
| 620 | } |
| 621 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 622 | // This function gets some field IDs, which in turn causes class initialization. |
| 623 | // It is called from a static block in MediaPlayer, which won't run until the |
| 624 | // first time an instance of this class is used. |
| 625 | static void |
| 626 | android_media_MediaPlayer_native_init(JNIEnv *env) |
| 627 | { |
| 628 | jclass clazz; |
| 629 | |
| 630 | clazz = env->FindClass("android/media/MediaPlayer"); |
| 631 | if (clazz == NULL) { |
| 632 | jniThrowException(env, "java/lang/RuntimeException", "Can't find android/media/MediaPlayer"); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 637 | if (fields.context == NULL) { |
| 638 | jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaPlayer.mNativeContext"); |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 643 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 644 | if (fields.post_event == NULL) { |
| 645 | jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaPlayer.postEventFromNative"); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;"); |
| 650 | if (fields.surface == NULL) { |
| 651 | jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaPlayer.mSurface"); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | jclass surface = env->FindClass("android/view/Surface"); |
| 656 | if (surface == NULL) { |
| 657 | jniThrowException(env, "java/lang/RuntimeException", "Can't find android/view/Surface"); |
| 658 | return; |
| 659 | } |
| 660 | |
Mathias Agopian | 8b13832 | 2010-04-12 16:22:15 -0700 | [diff] [blame] | 661 | fields.surface_native = env->GetFieldID(surface, ANDROID_VIEW_SURFACE_JNI_ID, "I"); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 662 | if (fields.surface_native == NULL) { |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 663 | jniThrowException(env, "java/lang/RuntimeException", |
| 664 | "Can't find Surface." ANDROID_VIEW_SURFACE_JNI_ID); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 665 | return; |
| 666 | } |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 667 | |
| 668 | fields.surfaceTexture = env->GetFieldID(clazz, "mSurfaceTexture", |
| 669 | "Landroid/graphics/SurfaceTexture;"); |
| 670 | if (fields.surfaceTexture == NULL) { |
| 671 | jniThrowException(env, "java/lang/RuntimeException", |
| 672 | "Can't find MediaPlayer.mSurfaceTexture"); |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | jclass surfaceTexture = env->FindClass("android/graphics/SurfaceTexture"); |
| 677 | if (surfaceTexture == NULL) { |
| 678 | jniThrowException(env, "java/lang/RuntimeException", |
| 679 | "Can't find android/graphics/SurfaceTexture"); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | fields.surfaceTexture_native = env->GetFieldID(surfaceTexture, |
| 684 | ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I"); |
| 685 | if (fields.surfaceTexture_native == NULL) { |
| 686 | jniThrowException(env, "java/lang/RuntimeException", |
| 687 | "Can't find SurfaceTexture." ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID); |
| 688 | return; |
| 689 | } |
| 690 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 691 | } |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | static void |
| 694 | android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this) |
| 695 | { |
| 696 | LOGV("native_setup"); |
| 697 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 698 | if (mp == NULL) { |
| 699 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | // create new listener and give it to MediaPlayer |
| 704 | sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this); |
| 705 | mp->setListener(listener); |
| 706 | |
| 707 | // Stow our new C++ MediaPlayer in an opaque field in the Java object. |
| 708 | setMediaPlayer(env, thiz, mp); |
| 709 | } |
| 710 | |
| 711 | static void |
| 712 | android_media_MediaPlayer_release(JNIEnv *env, jobject thiz) |
| 713 | { |
| 714 | LOGV("release"); |
| 715 | sp<MediaPlayer> mp = setMediaPlayer(env, thiz, 0); |
| 716 | if (mp != NULL) { |
| 717 | // this prevents native callbacks after the object is released |
| 718 | mp->setListener(0); |
| 719 | mp->disconnect(); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | static void |
| 724 | android_media_MediaPlayer_native_finalize(JNIEnv *env, jobject thiz) |
| 725 | { |
| 726 | LOGV("native_finalize"); |
| 727 | android_media_MediaPlayer_release(env, thiz); |
| 728 | } |
| 729 | |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 730 | static void android_media_MediaPlayer_set_audio_session_id(JNIEnv *env, jobject thiz, jint sessionId) { |
| 731 | LOGV("set_session_id(): %d", sessionId); |
| 732 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 733 | if (mp == NULL ) { |
| 734 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 735 | return; |
| 736 | } |
| 737 | process_media_player_call( env, thiz, mp->setAudioSessionId(sessionId), NULL, NULL ); |
| 738 | } |
| 739 | |
| 740 | static jint android_media_MediaPlayer_get_audio_session_id(JNIEnv *env, jobject thiz) { |
| 741 | LOGV("get_session_id()"); |
| 742 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 743 | if (mp == NULL ) { |
| 744 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | return mp->getAudioSessionId(); |
| 749 | } |
| 750 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 751 | static void |
| 752 | android_media_MediaPlayer_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level) |
| 753 | { |
| 754 | LOGV("setAuxEffectSendLevel: level %f", level); |
| 755 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 756 | if (mp == NULL ) { |
| 757 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 758 | return; |
| 759 | } |
| 760 | process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL ); |
| 761 | } |
| 762 | |
| 763 | static void android_media_MediaPlayer_attachAuxEffect(JNIEnv *env, jobject thiz, jint effectId) { |
Eric Laurent | b3bdf3f | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 764 | LOGV("attachAuxEffect(): %d", effectId); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 765 | sp<MediaPlayer> mp = getMediaPlayer(env, thiz); |
| 766 | if (mp == NULL ) { |
| 767 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 768 | return; |
| 769 | } |
| 770 | process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL ); |
| 771 | } |
| 772 | |
Gloria Wang | d211f41 | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 773 | static jint |
| 774 | android_media_MediaPlayer_pullBatteryData(JNIEnv *env, jobject thiz, jobject java_reply) |
| 775 | { |
| 776 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.player")); |
| 777 | sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); |
| 778 | if (service.get() == NULL) { |
| 779 | jniThrowException(env, "java/lang/RuntimeException", "cannot get MediaPlayerService"); |
| 780 | return UNKNOWN_ERROR; |
| 781 | } |
| 782 | |
| 783 | Parcel *reply = parcelForJavaObject(env, java_reply); |
| 784 | |
| 785 | return service->pullBatteryData(reply); |
| 786 | } |
| 787 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | // ---------------------------------------------------------------------------- |
| 789 | |
| 790 | static JNINativeMethod gMethods[] = { |
| 791 | {"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaPlayer_setDataSource}, |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 792 | {"setDataSource", "(Ljava/lang/String;Ljava/util/Map;)V",(void *)android_media_MediaPlayer_setDataSourceAndHeaders}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD}, |
Glenn Kasten | cc562a3 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 794 | {"_setVideoSurfaceOrSurfaceTexture", "()V", (void *)android_media_MediaPlayer_setVideoSurfaceOrSurfaceTexture}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | {"prepare", "()V", (void *)android_media_MediaPlayer_prepare}, |
| 796 | {"prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync}, |
| 797 | {"_start", "()V", (void *)android_media_MediaPlayer_start}, |
| 798 | {"_stop", "()V", (void *)android_media_MediaPlayer_stop}, |
| 799 | {"getVideoWidth", "()I", (void *)android_media_MediaPlayer_getVideoWidth}, |
| 800 | {"getVideoHeight", "()I", (void *)android_media_MediaPlayer_getVideoHeight}, |
| 801 | {"seekTo", "(I)V", (void *)android_media_MediaPlayer_seekTo}, |
| 802 | {"_pause", "()V", (void *)android_media_MediaPlayer_pause}, |
| 803 | {"isPlaying", "()Z", (void *)android_media_MediaPlayer_isPlaying}, |
| 804 | {"getCurrentPosition", "()I", (void *)android_media_MediaPlayer_getCurrentPosition}, |
| 805 | {"getDuration", "()I", (void *)android_media_MediaPlayer_getDuration}, |
| 806 | {"_release", "()V", (void *)android_media_MediaPlayer_release}, |
| 807 | {"_reset", "()V", (void *)android_media_MediaPlayer_reset}, |
| 808 | {"setAudioStreamType", "(I)V", (void *)android_media_MediaPlayer_setAudioStreamType}, |
| 809 | {"setLooping", "(Z)V", (void *)android_media_MediaPlayer_setLooping}, |
| 810 | {"isLooping", "()Z", (void *)android_media_MediaPlayer_isLooping}, |
| 811 | {"setVolume", "(FF)V", (void *)android_media_MediaPlayer_setVolume}, |
| 812 | {"getFrameAt", "(I)Landroid/graphics/Bitmap;", (void *)android_media_MediaPlayer_getFrameAt}, |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 813 | {"native_invoke", "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer_invoke}, |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 814 | {"native_setMetadataFilter", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_setMetadataFilter}, |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 815 | {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer_getMetadata}, |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 816 | {"native_init", "()V", (void *)android_media_MediaPlayer_native_init}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer_native_setup}, |
| 818 | {"native_finalize", "()V", (void *)android_media_MediaPlayer_native_finalize}, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 819 | {"getAudioSessionId", "()I", (void *)android_media_MediaPlayer_get_audio_session_id}, |
| 820 | {"setAudioSessionId", "(I)V", (void *)android_media_MediaPlayer_set_audio_session_id}, |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 821 | {"setAuxEffectSendLevel", "(F)V", (void *)android_media_MediaPlayer_setAuxEffectSendLevel}, |
| 822 | {"attachAuxEffect", "(I)V", (void *)android_media_MediaPlayer_attachAuxEffect}, |
Gloria Wang | d211f41 | 2011-02-19 18:37:57 -0800 | [diff] [blame] | 823 | {"native_pullBatteryData", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_pullBatteryData}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | }; |
| 825 | |
| 826 | static const char* const kClassPathName = "android/media/MediaPlayer"; |
| 827 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 828 | // This function only registers the native methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | static int register_android_media_MediaPlayer(JNIEnv *env) |
| 830 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | return AndroidRuntime::registerNativeMethods(env, |
| 832 | "android/media/MediaPlayer", gMethods, NELEM(gMethods)); |
| 833 | } |
| 834 | |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 835 | extern int register_android_media_MediaMetadataRetriever(JNIEnv *env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | extern int register_android_media_MediaRecorder(JNIEnv *env); |
| 837 | extern int register_android_media_MediaScanner(JNIEnv *env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | extern int register_android_media_ResampleInputStream(JNIEnv *env); |
James Dong | c371194 | 2010-01-19 17:45:38 -0800 | [diff] [blame] | 839 | extern int register_android_media_MediaProfiles(JNIEnv *env); |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 840 | extern int register_android_media_AmrInputStream(JNIEnv *env); |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 841 | extern int register_android_mtp_MtpDatabase(JNIEnv *env); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 842 | extern int register_android_mtp_MtpDevice(JNIEnv *env); |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 843 | extern int register_android_mtp_MtpServer(JNIEnv *env); |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 844 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | jint JNI_OnLoad(JavaVM* vm, void* reserved) |
| 846 | { |
| 847 | JNIEnv* env = NULL; |
| 848 | jint result = -1; |
| 849 | |
| 850 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 851 | LOGE("ERROR: GetEnv failed\n"); |
| 852 | goto bail; |
| 853 | } |
| 854 | assert(env != NULL); |
| 855 | |
| 856 | if (register_android_media_MediaPlayer(env) < 0) { |
| 857 | LOGE("ERROR: MediaPlayer native registration failed\n"); |
| 858 | goto bail; |
| 859 | } |
| 860 | |
| 861 | if (register_android_media_MediaRecorder(env) < 0) { |
| 862 | LOGE("ERROR: MediaRecorder native registration failed\n"); |
| 863 | goto bail; |
| 864 | } |
| 865 | |
| 866 | if (register_android_media_MediaScanner(env) < 0) { |
| 867 | LOGE("ERROR: MediaScanner native registration failed\n"); |
| 868 | goto bail; |
| 869 | } |
| 870 | |
| 871 | if (register_android_media_MediaMetadataRetriever(env) < 0) { |
| 872 | LOGE("ERROR: MediaMetadataRetriever native registration failed\n"); |
| 873 | goto bail; |
| 874 | } |
| 875 | |
| 876 | if (register_android_media_AmrInputStream(env) < 0) { |
| 877 | LOGE("ERROR: AmrInputStream native registration failed\n"); |
| 878 | goto bail; |
| 879 | } |
| 880 | |
| 881 | if (register_android_media_ResampleInputStream(env) < 0) { |
| 882 | LOGE("ERROR: ResampleInputStream native registration failed\n"); |
| 883 | goto bail; |
| 884 | } |
| 885 | |
James Dong | c371194 | 2010-01-19 17:45:38 -0800 | [diff] [blame] | 886 | if (register_android_media_MediaProfiles(env) < 0) { |
| 887 | LOGE("ERROR: MediaProfiles native registration failed"); |
| 888 | goto bail; |
| 889 | } |
| 890 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 891 | if (register_android_mtp_MtpDatabase(env) < 0) { |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 892 | LOGE("ERROR: MtpDatabase native registration failed"); |
| 893 | goto bail; |
| 894 | } |
| 895 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 896 | if (register_android_mtp_MtpDevice(env) < 0) { |
| 897 | LOGE("ERROR: MtpDevice native registration failed"); |
| 898 | goto bail; |
| 899 | } |
| 900 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 901 | if (register_android_mtp_MtpServer(env) < 0) { |
Mike Lockwood | 81ea83d | 2010-06-30 17:49:41 -0400 | [diff] [blame] | 902 | LOGE("ERROR: MtpServer native registration failed"); |
| 903 | goto bail; |
| 904 | } |
| 905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | /* success -- return valid version number */ |
| 907 | result = JNI_VERSION_1_4; |
| 908 | |
| 909 | bail: |
| 910 | return result; |
| 911 | } |
| 912 | |
| 913 | // KTHXBYE |