diff options
Diffstat (limited to 'media/jni/android_media_MediaPlayer.cpp')
| -rw-r--r-- | media/jni/android_media_MediaPlayer.cpp | 178 |
1 files changed, 79 insertions, 99 deletions
diff --git a/media/jni/android_media_MediaPlayer.cpp b/media/jni/android_media_MediaPlayer.cpp index 6d074e9..b03aa38 100644 --- a/media/jni/android_media_MediaPlayer.cpp +++ b/media/jni/android_media_MediaPlayer.cpp @@ -1,4 +1,4 @@ -/* //device/libs/android_runtime/android_media_MediaPlayer.cpp +/* ** ** Copyright 2007, The Android Open Source Project ** @@ -33,6 +33,8 @@ #include "utils/Errors.h" // for status_t #include "utils/KeyedVector.h" #include "utils/String8.h" +#include "android_media_Utils.h" + #include "android_util_Binder.h" #include <binder/Parcel.h> #include <gui/SurfaceTexture.h> @@ -69,7 +71,7 @@ class JNIMediaPlayerListener: public MediaPlayerListener public: JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz); ~JNIMediaPlayerListener(); - void notify(int msg, int ext1, int ext2); + virtual void notify(int msg, int ext1, int ext2, const Parcel *obj = NULL); private: JNIMediaPlayerListener(); jclass mClass; // Reference to MediaPlayer class @@ -102,10 +104,23 @@ JNIMediaPlayerListener::~JNIMediaPlayerListener() env->DeleteGlobalRef(mClass); } -void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2) +void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2, const Parcel *obj) { JNIEnv *env = AndroidRuntime::getJNIEnv(); - env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0); + if (obj && obj->dataSize() > 0) { + jbyteArray jArray = env->NewByteArray(obj->dataSize()); + if (jArray != NULL) { + jbyte *nArray = env->GetByteArrayElements(jArray, NULL); + memcpy(nArray, obj->data(), obj->dataSize()); + env->ReleaseByteArrayElements(jArray, nArray, 0); + env->CallStaticVoidMethod(mClass, fields.post_event, mObject, + msg, ext1, ext2, jArray); + env->DeleteLocalRef(jArray); + } + } else { + env->CallStaticVoidMethod(mClass, fields.post_event, mObject, + msg, ext1, ext2, NULL); + } } // ---------------------------------------------------------------------------- @@ -173,7 +188,9 @@ static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStat static void android_media_MediaPlayer_setDataSourceAndHeaders( - JNIEnv *env, jobject thiz, jstring path, jobject headers) { + JNIEnv *env, jobject thiz, jstring path, + jobjectArray keys, jobjectArray values) { + sp<MediaPlayer> mp = getMediaPlayer(env, thiz); if (mp == NULL ) { jniThrowException(env, "java/lang/IllegalStateException", NULL); @@ -185,91 +202,27 @@ android_media_MediaPlayer_setDataSourceAndHeaders( return; } - const char *pathStr = env->GetStringUTFChars(path, NULL); - if (pathStr == NULL) { // Out of memory - jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); + const char *tmp = env->GetStringUTFChars(path, NULL); + if (tmp == NULL) { // Out of memory return; } - // headers is a Map<String, String>. - // We build a similar KeyedVector out of it. - KeyedVector<String8, String8> headersVector; - if (headers) { - // Get the Map's entry Set. - jclass mapClass = env->FindClass("java/util/Map"); - - jmethodID entrySet = - env->GetMethodID(mapClass, "entrySet", "()Ljava/util/Set;"); - - jobject set = env->CallObjectMethod(headers, entrySet); - // Obtain an iterator over the Set - jclass setClass = env->FindClass("java/util/Set"); - - jmethodID iterator = - env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;"); - - jobject iter = env->CallObjectMethod(set, iterator); - // Get the Iterator method IDs - jclass iteratorClass = env->FindClass("java/util/Iterator"); - jmethodID hasNext = env->GetMethodID(iteratorClass, "hasNext", "()Z"); - - jmethodID next = - env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;"); - - // Get the Entry class method IDs - jclass entryClass = env->FindClass("java/util/Map$Entry"); + String8 pathStr(tmp); + env->ReleaseStringUTFChars(path, tmp); + tmp = NULL; - jmethodID getKey = - env->GetMethodID(entryClass, "getKey", "()Ljava/lang/Object;"); - - jmethodID getValue = - env->GetMethodID(entryClass, "getValue", "()Ljava/lang/Object;"); - - // Iterate over the entry Set - while (env->CallBooleanMethod(iter, hasNext)) { - jobject entry = env->CallObjectMethod(iter, next); - jstring key = (jstring) env->CallObjectMethod(entry, getKey); - jstring value = (jstring) env->CallObjectMethod(entry, getValue); - - const char* keyStr = env->GetStringUTFChars(key, NULL); - if (!keyStr) { // Out of memory - jniThrowException( - env, "java/lang/RuntimeException", "Out of memory"); - return; - } - - const char* valueStr = env->GetStringUTFChars(value, NULL); - if (!valueStr) { // Out of memory - jniThrowException( - env, "java/lang/RuntimeException", "Out of memory"); - return; - } - - headersVector.add(String8(keyStr), String8(valueStr)); - - env->DeleteLocalRef(entry); - env->ReleaseStringUTFChars(key, keyStr); - env->DeleteLocalRef(key); - env->ReleaseStringUTFChars(value, valueStr); - env->DeleteLocalRef(value); - } - - env->DeleteLocalRef(entryClass); - env->DeleteLocalRef(iteratorClass); - env->DeleteLocalRef(iter); - env->DeleteLocalRef(setClass); - env->DeleteLocalRef(set); - env->DeleteLocalRef(mapClass); + // We build a KeyedVector out of the key and val arrays + KeyedVector<String8, String8> headersVector; + if (!ConvertKeyValueArraysToKeyedVector( + env, keys, values, &headersVector)) { + return; } LOGV("setDataSource: path %s", pathStr); status_t opStatus = mp->setDataSource( - String8(pathStr), - headers ? &headersVector : NULL); - - // Make sure that local ref is released before a potential exception - env->ReleaseStringUTFChars(path, pathStr); + pathStr, + headersVector.size() > 0? &headersVector : NULL); process_media_player_call( env, thiz, opStatus, "java/io/IOException", @@ -279,7 +232,7 @@ android_media_MediaPlayer_setDataSourceAndHeaders( static void android_media_MediaPlayer_setDataSource(JNIEnv *env, jobject thiz, jstring path) { - android_media_MediaPlayer_setDataSourceAndHeaders(env, thiz, path, 0); + android_media_MediaPlayer_setDataSourceAndHeaders(env, thiz, path, NULL, NULL); } static void @@ -295,7 +248,7 @@ android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fil jniThrowException(env, "java/lang/IllegalArgumentException", NULL); return; } - int fd = getParcelFileDescriptorFD(env, fileDescriptor); + int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); LOGV("setDataSourceFD: fd %d", fd); process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." ); } @@ -317,8 +270,7 @@ static void setVideoSurfaceOrSurfaceTexture( if (surfaceTexture != NULL) { sp<ISurfaceTexture> native_surfaceTexture( getSurfaceTexture(env, surfaceTexture)); - LOGV("%s: texture=%p (id=%d)", prefix, - native_surfaceTexture.get(), native_surfaceTexture->getIdentity()); + LOGV("%s: texture=%p", prefix, native_surfaceTexture.get()); mp->setVideoSurfaceTexture(native_surfaceTexture); } } @@ -629,62 +581,49 @@ android_media_MediaPlayer_native_init(JNIEnv *env) clazz = env->FindClass("android/media/MediaPlayer"); if (clazz == NULL) { - jniThrowException(env, "java/lang/RuntimeException", "Can't find android/media/MediaPlayer"); return; } fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); if (fields.context == NULL) { - jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaPlayer.mNativeContext"); return; } fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", "(Ljava/lang/Object;IIILjava/lang/Object;)V"); if (fields.post_event == NULL) { - jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaPlayer.postEventFromNative"); return; } fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;"); if (fields.surface == NULL) { - jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaPlayer.mSurface"); return; } jclass surface = env->FindClass("android/view/Surface"); if (surface == NULL) { - jniThrowException(env, "java/lang/RuntimeException", "Can't find android/view/Surface"); return; } fields.surface_native = env->GetFieldID(surface, ANDROID_VIEW_SURFACE_JNI_ID, "I"); if (fields.surface_native == NULL) { - jniThrowException(env, "java/lang/RuntimeException", - "Can't find Surface." ANDROID_VIEW_SURFACE_JNI_ID); return; } fields.surfaceTexture = env->GetFieldID(clazz, "mSurfaceTexture", "Landroid/graphics/SurfaceTexture;"); if (fields.surfaceTexture == NULL) { - jniThrowException(env, "java/lang/RuntimeException", - "Can't find MediaPlayer.mSurfaceTexture"); return; } jclass surfaceTexture = env->FindClass("android/graphics/SurfaceTexture"); if (surfaceTexture == NULL) { - jniThrowException(env, "java/lang/RuntimeException", - "Can't find android/graphics/SurfaceTexture"); return; } fields.surfaceTexture_native = env->GetFieldID(surfaceTexture, ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I"); if (fields.surfaceTexture_native == NULL) { - jniThrowException(env, "java/lang/RuntimeException", - "Can't find SurfaceTexture." ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID); return; } @@ -785,11 +724,50 @@ android_media_MediaPlayer_pullBatteryData(JNIEnv *env, jobject thiz, jobject jav return service->pullBatteryData(reply); } +static jboolean +android_media_MediaPlayer_setParameter(JNIEnv *env, jobject thiz, jint key, jobject java_request) +{ + LOGV("setParameter: key %d", key); + sp<MediaPlayer> mp = getMediaPlayer(env, thiz); + if (mp == NULL ) { + jniThrowException(env, "java/lang/IllegalStateException", NULL); + return false; + } + + Parcel *request = parcelForJavaObject(env, java_request); + status_t err = mp->setParameter(key, *request); + if (err == OK) { + return true; + } else { + return false; + } +} + +static void +android_media_MediaPlayer_getParameter(JNIEnv *env, jobject thiz, jint key, jobject java_reply) +{ + LOGV("getParameter: key %d", key); + sp<MediaPlayer> mp = getMediaPlayer(env, thiz); + if (mp == NULL ) { + jniThrowException(env, "java/lang/IllegalStateException", NULL); + return; + } + + Parcel *reply = parcelForJavaObject(env, java_reply); + process_media_player_call(env, thiz, mp->getParameter(key, reply), NULL, NULL ); +} + // ---------------------------------------------------------------------------- static JNINativeMethod gMethods[] = { {"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaPlayer_setDataSource}, - {"setDataSource", "(Ljava/lang/String;Ljava/util/Map;)V",(void *)android_media_MediaPlayer_setDataSourceAndHeaders}, + + { + "_setDataSource", + "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V", + (void *)android_media_MediaPlayer_setDataSourceAndHeaders + }, + {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD}, {"_setVideoSurfaceOrSurfaceTexture", "()V", (void *)android_media_MediaPlayer_setVideoSurfaceOrSurfaceTexture}, {"prepare", "()V", (void *)android_media_MediaPlayer_prepare}, @@ -821,6 +799,8 @@ static JNINativeMethod gMethods[] = { {"setAuxEffectSendLevel", "(F)V", (void *)android_media_MediaPlayer_setAuxEffectSendLevel}, {"attachAuxEffect", "(I)V", (void *)android_media_MediaPlayer_attachAuxEffect}, {"native_pullBatteryData", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_pullBatteryData}, + {"setParameter", "(ILandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer_setParameter}, + {"getParameter", "(ILandroid/os/Parcel;)V", (void *)android_media_MediaPlayer_getParameter}, }; static const char* const kClassPathName = "android/media/MediaPlayer"; |
