[Android] libvlcjni: refactor expandMedia for MediaList

Edward Wang git at videolan.org
Wed Aug 28 16:34:20 CEST 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Wed Aug 28 12:16:04 2013 +0200| [2c789f3acaf34cd4024c7ff7df9e3c3c20c88fbd] | committer: Edward Wang

libvlcjni: refactor expandMedia for MediaList

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=2c789f3acaf34cd4024c7ff7df9e3c3c20c88fbd
---

 vlc-android/jni/libvlcjni-medialist.c              |   38 ++++++++++++++++++++
 vlc-android/jni/libvlcjni.c                        |   33 -----------------
 vlc-android/src/org/videolan/libvlc/LibVLC.java    |   11 +++---
 vlc-android/src/org/videolan/libvlc/MediaList.java |   10 ++++++
 vlc-android/src/org/videolan/vlc/AudioService.java |    4 +--
 5 files changed, 57 insertions(+), 39 deletions(-)

diff --git a/vlc-android/jni/libvlcjni-medialist.c b/vlc-android/jni/libvlcjni-medialist.c
index 55c7333..a516c71 100644
--- a/vlc-android/jni/libvlcjni-medialist.c
+++ b/vlc-android/jni/libvlcjni-medialist.c
@@ -102,6 +102,36 @@ end:
         (*myVm)->DetachCurrentThread(myVm);
 }
 
+static int expand_media_internal(libvlc_media_list_t* p_mlist, int position) {
+    libvlc_media_t* p_md = libvlc_media_list_item_at_index(p_mlist, position);
+    if(!p_md) {
+        return -1;
+    }
+    libvlc_media_list_t* p_subitems = libvlc_media_subitems(p_md);
+    libvlc_media_release(p_md);
+    if(p_subitems) {
+        // Expand any subitems if needed
+        int subitem_count = libvlc_media_list_count(p_subitems);
+        if(subitem_count > 0) {
+            LOGD("Found %d subitems, expanding", subitem_count);
+            for(int i = subitem_count - 1; i >= 0; i--) {
+                libvlc_media_t* p_subitem = libvlc_media_list_item_at_index(p_subitems, i);
+                libvlc_media_list_insert_media(p_mlist, p_subitem, position+1);
+                libvlc_media_release(p_subitem);
+            }
+            libvlc_media_list_remove_index(p_mlist, position);
+        }
+        libvlc_media_list_release(p_subitems);
+        if(subitem_count > 0) {
+            return 0;
+        } else {
+            return -1;
+        }
+    } else {
+        return -1;
+    }
+}
+
 jlong Java_org_videolan_libvlc_MediaList_init(JNIEnv *env, jobject thiz, jobject libvlcJava) {
     libvlc_media_list_t* p_ml = libvlc_media_list_new((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"));
     if(!p_ml) {
@@ -135,6 +165,14 @@ void Java_org_videolan_libvlc_MediaList_nativeDestroy(JNIEnv *env, jobject thiz)
     (*env)->DeleteGlobalRef(env, (jobject)(intptr_t)getLong(env, thiz, "mEventHanderGlobalRef"));
 }
 
+jint Java_org_videolan_libvlc_MediaList_expandMedia(JNIEnv *env, jobject thiz, jint position) {
+    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
+    libvlc_media_list_lock(p_ml);
+    jint ret = (jint)expand_media_internal(p_ml, position);
+    libvlc_media_list_unlock(p_ml);
+    return ret;
+}
+
 void Java_org_videolan_libvlc_MediaList_remove(JNIEnv *env, jobject thiz, jint position) {
     libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
     libvlc_media_list_lock(p_ml);
diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index 1ff013a..3cf890d 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -362,39 +362,6 @@ static void create_player_and_play(JNIEnv* env, jobject thiz,
     libvlc_media_player_play(mp);
 }
 
-jint Java_org_videolan_libvlc_LibVLC_expandMedia(JNIEnv *env, jobject thiz)
-{
-    int current_position = getInt(env, thiz, "mInternalMediaPlayerIndex");
-    libvlc_media_list_t* p_mlist = getMediaList(env, thiz);
-    libvlc_media_list_lock(p_mlist);
-    libvlc_media_t* p_md = libvlc_media_list_item_at_index(p_mlist, current_position);
-    libvlc_media_list_unlock(p_mlist);
-    libvlc_media_list_t* p_subitems = libvlc_media_subitems(p_md);
-    libvlc_media_release(p_md);
-    if(p_subitems) {
-        // Expand any subitems if needed
-        int subitem_count = libvlc_media_list_count(p_subitems);
-        if(subitem_count > 0) {
-            LOGD("Found %d subitems, expanding", subitem_count);
-            libvlc_media_list_lock(p_mlist);
-            for(int i = subitem_count - 1; i >= 0; i--) {
-                libvlc_media_list_insert_media(p_mlist, libvlc_media_list_item_at_index(p_subitems, i), current_position+1);
-            }
-            libvlc_media_list_remove_index(p_mlist, current_position);
-            libvlc_media_list_unlock(p_mlist);
-        }
-        libvlc_media_list_release(p_subitems);
-        if(subitem_count > 0) {
-            create_player_and_play(env, thiz,
-                getLong(env, thiz, "mLibVlcInstance"), current_position);
-            return (jint)current_position;
-        } else
-            return -1;
-    } else {
-        return -1;
-    }
-}
-
 jint Java_org_videolan_libvlc_LibVLC_readMedia(JNIEnv *env, jobject thiz,
                                             jlong instance, jstring mrl, jboolean novideo)
 {
diff --git a/vlc-android/src/org/videolan/libvlc/LibVLC.java b/vlc-android/src/org/videolan/libvlc/LibVLC.java
index ec0f809..972a2db 100644
--- a/vlc-android/src/org/videolan/libvlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/libvlc/LibVLC.java
@@ -611,13 +611,16 @@ public class LibVLC {
     public native void getMediaListItems(ArrayList<String> arl);
 
      /**
-      * A function to flatten the playlist. This function checks the
-      * currently playing media. If there are any subitems, it will
-      * expand them and replace the current media.
+      * Expand and continue playing the current media.
       *
       * @return the index of the media was expanded, and -1 if no media was expanded
       */
-    public native int expandMedia();
+    public int expandAndPlay() {
+        int r = mMediaList.expandMedia(mInternalMediaPlayerIndex);
+        if(r == 0)
+            this.playIndex(mInternalMediaPlayerIndex);
+        return r;
+    }
 
     private native void setEventHandler(EventHandler eventHandler);
 
diff --git a/vlc-android/src/org/videolan/libvlc/MediaList.java b/vlc-android/src/org/videolan/libvlc/MediaList.java
index 80ad13c..a994dd8 100644
--- a/vlc-android/src/org/videolan/libvlc/MediaList.java
+++ b/vlc-android/src/org/videolan/libvlc/MediaList.java
@@ -63,6 +63,16 @@ public class MediaList {
     }
     private native void add(LibVLC libvlc_instance, String mrl);
 
+    /**
+     * This function checks the currently playing media for subitems at the given
+     * position, and if any exist, it will expand them at the same position
+     * and replace the current media.
+     *
+     * @param position The position to expand
+     * @return -1 if no subitems were found, 0 if subitems were expanded
+     */
+    public native int expandMedia(int position);
+
     public void insert(int position, String mrl) {
         insert(mLibVLC, position, mrl);
     }
diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index 28a6465..415d84b 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -653,8 +653,8 @@ public class AudioService extends Service {
 
     private void next() {
         // Try to expand any items present
-        int pos = mLibVLC.expandMedia();
-        if(pos >= 0) {
+        int pos = mLibVLC.expandAndPlay();
+        if(pos == 0) {
             Log.d(TAG, "Found subitems, updating media display");
             ArrayList<String> mediaPathList = new ArrayList<String>();
             mLibVLC.getMediaListItems(mediaPathList);



More information about the Android mailing list