[Android] [PATCH] jni: uniformize how to fetch libvlc_instance_t

Thomas Guillem thomas at gllm.fr
Tue Nov 25 17:36:16 CET 2014


---
 libvlc/jni/libvlcjni-medialist.c              | 16 ++++++-------
 libvlc/jni/libvlcjni-track.c                  |  8 +++----
 libvlc/jni/libvlcjni.c                        | 18 ++++++++++----
 libvlc/jni/thumbnailer.c                      |  6 ++---
 libvlc/jni/utils.h                            |  4 +++-
 libvlc/src/org/videolan/libvlc/LibVLC.java    | 34 ++++++++-------------------
 libvlc/src/org/videolan/libvlc/MediaList.java |  6 ++---
 7 files changed, 43 insertions(+), 49 deletions(-)

diff --git a/libvlc/jni/libvlcjni-medialist.c b/libvlc/jni/libvlcjni-medialist.c
index 257fadc..8b22b3e 100644
--- a/libvlc/jni/libvlcjni-medialist.c
+++ b/libvlc/jni/libvlcjni-medialist.c
@@ -75,18 +75,18 @@ static int expand_media_internal(JNIEnv *env, libvlc_instance_t* p_instance, job
     }
 }
 
-jint Java_org_videolan_libvlc_MediaList_expandMedia(JNIEnv *env, jobject thiz, jobject libvlcJava, jint position, jobject children) {
+jint Java_org_videolan_libvlc_expandMedia(JNIEnv *env, jobject thiz, jint position, jobject children) {
     return (jint)expand_media_internal(env,
-        (libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"),
+        getLibVlcInstance(env, thiz),
         children,
-        (libvlc_media_t*)libvlc_media_player_get_media((libvlc_media_player_t*)(intptr_t)getLong(env, libvlcJava, "mInternalMediaPlayerInstance"))
-        );
+        (libvlc_media_t*)libvlc_media_player_get_media(getMediaPlayer(env, thiz)));
 }
 
-void Java_org_videolan_libvlc_MediaList_loadPlaylist(JNIEnv *env, jobject thiz, jobject libvlcJava, jstring mrl, jobject items) {
+void Java_org_videolan_libvlc_loadPlaylist(JNIEnv *env, jobject thiz, jstring mrl, jobject items) {
     const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, NULL);
+    libvlc_instance_t *p_instance = getLibVlcInstance(env, thiz);
 
-    libvlc_media_t *p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"), p_mrl);
+    libvlc_media_t *p_md = libvlc_media_new_location(p_instance, p_mrl);
     libvlc_media_add_option(p_md, ":demux=playlist,none");
     libvlc_media_add_option(p_md, ":run-time=1");
 
@@ -96,7 +96,7 @@ void Java_org_videolan_libvlc_MediaList_loadPlaylist(JNIEnv *env, jobject thiz,
     monitor->stopped = false;
     pthread_mutex_lock(&monitor->doneMutex);
 
-    libvlc_media_player_t* p_mp = libvlc_media_player_new((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"));
+    libvlc_media_player_t* p_mp = libvlc_media_player_new(p_instance);
     libvlc_media_player_set_video_title_display(p_mp, libvlc_position_disable, 0);
     libvlc_event_manager_t* ev = libvlc_media_player_event_manager(p_mp);
     libvlc_event_attach(ev, libvlc_MediaPlayerEndReached, stopped_callback, monitor);
@@ -118,7 +118,7 @@ void Java_org_videolan_libvlc_MediaList_loadPlaylist(JNIEnv *env, jobject thiz,
 
     libvlc_media_player_release(p_mp);
 
-    expand_media_internal(env, (libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"), items, p_md);
+    expand_media_internal(env, p_instance, items, p_md);
 
     (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
 }
diff --git a/libvlc/jni/libvlcjni-track.c b/libvlc/jni/libvlcjni-track.c
index 53ea64c..cfc70ce 100644
--- a/libvlc/jni/libvlcjni-track.c
+++ b/libvlc/jni/libvlcjni-track.c
@@ -45,10 +45,10 @@ static void length_changed_callback(const libvlc_event_t *ev, void *data)
 }
 
 jboolean Java_org_videolan_libvlc_LibVLC_hasVideoTrack(JNIEnv *env, jobject thiz,
-                                                       jlong i_instance, jstring fileLocation)
+                                                       jstring fileLocation)
 {
     /* Create a new item and assign it to the media player. */
-    libvlc_media_t *p_m = new_media(i_instance, env, thiz, fileLocation, false, false);
+    libvlc_media_t *p_m = new_media(env, thiz, fileLocation, false, false);
     if (p_m == NULL)
     {
         LOGE("Could not create the media!");
@@ -183,10 +183,10 @@ jobjectArray read_track_info_internal(JNIEnv *env, jobject thiz, libvlc_media_t*
 }
 
 jobjectArray Java_org_videolan_libvlc_LibVLC_readTracksInfo(JNIEnv *env, jobject thiz,
-                                                            jlong instance, jstring mrl)
+                                                            jstring mrl)
 {
     /* Create a new item and assign it to the media player. */
-    libvlc_media_t *p_m = new_media(instance, env, thiz, mrl, false, false);
+    libvlc_media_t *p_m = new_media(env, thiz, mrl, false, false);
     if (p_m == NULL)
     {
         LOGE("Could not create the media!");
diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index b310e96..b2c0cce 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -72,9 +72,9 @@ static void add_media_options(libvlc_media_t *p_md, JNIEnv *env, jobjectArray me
     }
 }
 
-libvlc_media_t *new_media(jlong instance, JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo)
+libvlc_media_t *new_media(JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo)
 {
-    libvlc_instance_t *libvlc = (libvlc_instance_t*)(intptr_t)instance;
+    libvlc_instance_t *libvlc = getLibVlcInstance(env, thiz);
     jboolean isCopy;
     const char *psz_location = (*env)->GetStringUTFChars(env, fileLocation, &isCopy);
     libvlc_media_t *p_md = libvlc_media_new_location(libvlc, psz_location);
@@ -96,11 +96,17 @@ libvlc_media_t *new_media(jlong instance, JNIEnv *env, jobject thiz, jstring fil
     return p_md;
 }
 
+libvlc_instance_t *getLibVlcInstance(JNIEnv *env, jobject thiz)
+{
+    return (libvlc_instance_t*)(intptr_t)getLong(env, thiz, "mLibVlcInstance");
+}
+
 libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz)
 {
     return (libvlc_media_player_t*)(intptr_t)getLong(env, thiz, "mInternalMediaPlayerInstance");
 }
 
+
 static void releaseMediaPlayer(JNIEnv *env, jobject thiz)
 {
     libvlc_media_player_t* p_mp = getMediaPlayer(env, thiz);
@@ -399,14 +405,16 @@ void Java_org_videolan_libvlc_LibVLC_setEventHandler(JNIEnv *env, jobject thiz,
     eventHandlerInstance = getEventHandlerReference(env, thiz, eventHandler);
 }
 
-void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz, jlong instance,
+void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz,
                                              jstring mrl, jobjectArray mediaOptions)
 {
     /* Release previous media player, if any */
     releaseMediaPlayer(env, thiz);
 
+    libvlc_instance_t *p_instance = getLibVlcInstance(env, thiz);
+
     /* Create a media player playing environment */
-    libvlc_media_player_t *mp = libvlc_media_player_new((libvlc_instance_t*)(intptr_t)instance);
+    libvlc_media_player_t *mp = libvlc_media_player_new(p_instance);
     libvlc_media_player_set_video_title_display(mp, libvlc_position_disable, 0);
     jobject myJavaLibVLC = (*env)->NewGlobalRef(env, thiz);
 
@@ -444,7 +452,7 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz, jlong in
 
     const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, 0);
 
-    libvlc_media_t* p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)instance, p_mrl);
+    libvlc_media_t* p_md = libvlc_media_new_location(p_instance, p_mrl);
     /* media options */
     if (mediaOptions != NULL)
         add_media_options(p_md, env, mediaOptions);
diff --git a/libvlc/jni/thumbnailer.c b/libvlc/jni/thumbnailer.c
index 2a95d21..0b8aae3 100644
--- a/libvlc/jni/thumbnailer.c
+++ b/libvlc/jni/thumbnailer.c
@@ -139,10 +139,10 @@ static void thumbnailer_unlock(void *opaque, void *picture, void *const *pixels)
  * return null if the thumbail generation failed.
  **/
 jbyteArray Java_org_videolan_libvlc_LibVLC_getThumbnail(JNIEnv *env, jobject thiz,
-                                                        jlong instance, jstring filePath,
+                                                        jstring filePath,
                                                         const jint frameWidth, const jint frameHeight)
 {
-    libvlc_instance_t *libvlc = (libvlc_instance_t *)(intptr_t)instance;
+    libvlc_instance_t *libvlc = getLibVlcInstance(env, thiz);
     jbyteArray byteArray = NULL;
 
     /* Create the thumbnailer data structure */
@@ -161,7 +161,7 @@ jbyteArray Java_org_videolan_libvlc_LibVLC_getThumbnail(JNIEnv *env, jobject thi
     libvlc_media_player_t *mp = libvlc_media_player_new(libvlc);
     libvlc_media_player_set_video_title_display(mp, libvlc_position_disable, 0);
 
-    libvlc_media_t *m = new_media(instance, env, thiz, filePath, true, false);
+    libvlc_media_t *m = new_media(env, thiz, filePath, true, false);
     if (m == NULL)
     {
         LOGE("Could not create the media to play!");
diff --git a/libvlc/jni/utils.h b/libvlc/jni/utils.h
index f8502d9..5da3514 100644
--- a/libvlc/jni/utils.h
+++ b/libvlc/jni/utils.h
@@ -21,7 +21,9 @@
 #ifndef LIBVLCJNI_UTILS_H
 #define LIBVLCJNI_UTILS_H
 
-libvlc_media_t *new_media(jlong instance, JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo);
+libvlc_media_t *new_media(JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo);
+
+libvlc_instance_t *getLibVlcInstance(JNIEnv *env, jobject thiz);
 
 libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz);
 
diff --git a/libvlc/src/org/videolan/libvlc/LibVLC.java b/libvlc/src/org/videolan/libvlc/LibVLC.java
index 4d4f67a..5d1f830 100644
--- a/libvlc/src/org/videolan/libvlc/LibVLC.java
+++ b/libvlc/src/org/videolan/libvlc/LibVLC.java
@@ -600,7 +600,7 @@ public class LibVLC {
             return;
         String[] options = mMediaList.getMediaOptions(position);
         mInternalMediaPlayerIndex = position;
-        playMRL(mLibVlcInstance, mrl, options);
+        playMRL(mrl, options);
     }
 
     /**
@@ -612,25 +612,7 @@ public class LibVLC {
         // index=-1 will return options from libvlc instance without relying on MediaList
         String[] options = mMediaList.getMediaOptions(-1);
         mInternalMediaPlayerIndex = 0;
-        playMRL(mLibVlcInstance, mrl, options);
-    }
-
-    public TrackInfo[] readTracksInfo(String mrl) {
-        return readTracksInfo(mLibVlcInstance, mrl);
-    }
-
-    /**
-     * Get a media thumbnail.
-     */
-    public byte[] getThumbnail(String mrl, int i_width, int i_height) {
-        return getThumbnail(mLibVlcInstance, mrl, i_width, i_height);
-    }
-
-    /**
-     * Return true if there is a video track in the file
-     */
-    public boolean hasVideoTrack(String mrl) throws java.io.IOException {
-        return hasVideoTrack(mLibVlcInstance, mrl);
+        playMRL(mrl, options);
     }
 
     /**
@@ -677,7 +659,7 @@ public class LibVLC {
     /**
      * Play an mrl
      */
-    private native void playMRL(long instance, String mrl, String[] mediaOptions);
+    private native void playMRL(String mrl, String[] mediaOptions);
 
     /**
      * Returns true if any media is playing
@@ -773,14 +755,14 @@ public class LibVLC {
      * Get a media thumbnail.
      * @return a bytearray with the RGBA thumbnail data inside.
      */
-    private native byte[] getThumbnail(long instance, String mrl, int i_width, int i_height);
+    public native byte[] getThumbnail(String mrl, int i_width, int i_height);
 
     /**
      * Return true if there is a video track in the file
      */
-    private native boolean hasVideoTrack(long instance, String mrl);
+    public native boolean hasVideoTrack(String mrl) throws java.io.IOException;
 
-    private native TrackInfo[] readTracksInfo(long instance, String mrl);
+    public native TrackInfo[] readTracksInfo(String mrl);
 
     public native TrackInfo[] readTracksInfoInternal();
 
@@ -884,4 +866,8 @@ public class LibVLC {
     public native String getMeta(int meta);
 
     public native int setWindowSize(int width, int height);
+
+    /* MediaList */
+    protected native void loadPlaylist(String mrl, ArrayList<String> items);
+    protected native int expandMedia(int position, ArrayList<String> children);
 }
diff --git a/libvlc/src/org/videolan/libvlc/MediaList.java b/libvlc/src/org/videolan/libvlc/MediaList.java
index 666568e..7888f6f 100644
--- a/libvlc/src/org/videolan/libvlc/MediaList.java
+++ b/libvlc/src/org/videolan/libvlc/MediaList.java
@@ -103,7 +103,7 @@ public class MediaList {
      */
     public int expandMedia(int position) {
         ArrayList<String> children = new ArrayList<String>();
-        int ret = expandMedia(mLibVLC, position, children);
+        int ret = mLibVLC.expandMedia(position, children);
         if(ret == 0) {
             mEventHandler.callback(EventHandler.CustomMediaListExpanding, new Bundle());
             this.remove(position);
@@ -114,17 +114,15 @@ public class MediaList {
         }
         return ret;
     }
-    private native int expandMedia(LibVLC libvlc_instance, int position, ArrayList<String> children);
 
     public void loadPlaylist(String mrl) {
         ArrayList<String> items = new ArrayList<String>();
-        loadPlaylist(mLibVLC, mrl, items);
+        mLibVLC.loadPlaylist(mrl, items);
         this.clear();
         for(String item : items) {
             this.add(item);
         }
     }
-    private native void loadPlaylist(LibVLC libvlc_instance, String mrl, ArrayList<String> items);
 
     public void insert(int position, String mrl) {
         insert(position, new Media(mLibVLC, mrl));
-- 
2.1.1



More information about the Android mailing list