[Android] LibVLC: add Slaves API

Thomas Guillem git at videolan.org
Thu May 19 15:58:33 CEST 2016


vlc-android | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed May 18 16:54:42 2016 +0200| [b934f3b6fca88c8314b9e12f2d36397524dc922c] | committer: Thomas Guillem

LibVLC: add Slaves API

> https://code.videolan.org/videolan/vlc-android/commit/b934f3b6fca88c8314b9e12f2d36397524dc922c
---

 libvlc/jni/libvlcjni-media.c              | 77 +++++++++++++++++++++++++++++++
 libvlc/jni/libvlcjni.c                    |  9 ++++
 libvlc/jni/utils.h                        |  5 ++
 libvlc/src/org/videolan/libvlc/Media.java | 64 +++++++++++++++++++++++++
 4 files changed, 155 insertions(+)

diff --git a/libvlc/jni/libvlcjni-media.c b/libvlc/jni/libvlcjni-media.c
index 8889633..bfb7d87 100644
--- a/libvlc/jni/libvlcjni-media.c
+++ b/libvlc/jni/libvlcjni-media.c
@@ -478,3 +478,80 @@ Java_org_videolan_libvlc_Media_nativeAddOption(JNIEnv *env, jobject thiz,
 
     (*env)->ReleaseStringUTFChars(env, joption, p_option);
 }
+
+void
+Java_org_videolan_libvlc_Media_nativeAddSlave(JNIEnv *env, jobject thiz,
+                                              jint type, jint priority,
+                                              jstring juri)
+{
+    const char *psz_uri;
+    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
+
+    if (!p_obj)
+        return;
+
+    if (!juri || !(psz_uri = (*env)->GetStringUTFChars(env, juri, 0)))
+    {
+        throw_IllegalArgumentException(env, "uri invalid");
+        return;
+    }
+
+    int i_ret = libvlc_media_slaves_add(p_obj->u.p_m, type, priority, psz_uri);
+
+    (*env)->ReleaseStringUTFChars(env, juri, psz_uri);
+    if (i_ret != 0)
+        throw_IllegalStateException(env, "can't add slaves to libvlc_media");
+}
+
+void
+Java_org_videolan_libvlc_Media_nativeClearSlaves(JNIEnv *env, jobject thiz)
+{
+    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
+
+    if (!p_obj)
+        return;
+
+    libvlc_media_slaves_clear(p_obj->u.p_m);
+}
+
+unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
+                                      libvlc_media_slave_t ***ppp_slaves );
+void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
+                                  unsigned int i_count );
+jobject
+Java_org_videolan_libvlc_Media_nativeGetSlaves(JNIEnv *env, jobject thiz)
+{
+    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
+    libvlc_media_slave_t **pp_slaves;
+    unsigned int i_slaves;
+    jobjectArray array;
+
+    if (!p_obj)
+        return NULL;
+
+    i_slaves = libvlc_media_slaves_get(p_obj->u.p_m, &pp_slaves);
+    if (i_slaves == 0)
+        return NULL;
+
+    array = (*env)->NewObjectArray(env, i_slaves, fields.Media.Slave.clazz, NULL);
+    if (array == NULL)
+        goto error;
+
+    for (unsigned int i = 0; i < i_slaves; ++i)
+    {
+        libvlc_media_slave_t *p_slave = pp_slaves[i];
+        jstring juri = (*env)->NewStringUTF(env, p_slave->psz_uri);
+
+        jobject jslave =
+            (*env)->CallStaticObjectMethod(env, fields.Media.clazz,
+                                           fields.Media.createSlaveFromNativeID,
+                                           p_slave->i_type, p_slave->i_priority,
+                                           juri);
+        (*env)->SetObjectArrayElement(env, array, i, jslave);
+    }
+
+error:
+    if (pp_slaves)
+        libvlc_media_slaves_release(pp_slaves, i_slaves);
+    return array;
+}
diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index bb5dd54..cdf8782 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -169,6 +169,8 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
               "org/videolan/libvlc/Media", true);
     GET_CLASS(fields.Media.Track.clazz,
               "org/videolan/libvlc/Media$Track", true);
+    GET_CLASS(fields.Media.Slave.clazz,
+              "org/videolan/libvlc/Media$Slave", true);
     GET_CLASS(fields.MediaPlayer.clazz,
               "org/videolan/libvlc/MediaPlayer", true);
     GET_CLASS(fields.MediaPlayer.Title.clazz,
@@ -252,6 +254,13 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
            "Lorg/videolan/libvlc/Media$Track;");
 
     GET_ID(GetStaticMethodID,
+           fields.Media.createSlaveFromNativeID,
+           fields.Media.clazz,
+           "createSlaveFromNative",
+           "(IILjava/lang/String;)"
+           "Lorg/videolan/libvlc/Media$Slave;");
+
+    GET_ID(GetStaticMethodID,
            fields.MediaPlayer.createTitleFromNativeID,
            fields.MediaPlayer.clazz,
            "createTitleFromNative",
diff --git a/libvlc/jni/utils.h b/libvlc/jni/utils.h
index ce14de1..7df7c08 100644
--- a/libvlc/jni/utils.h
+++ b/libvlc/jni/utils.h
@@ -56,11 +56,16 @@ struct fields {
             jclass clazz;
         } Track;
 
+        struct {
+            jclass clazz;
+        } Slave;
+
         jclass clazz;
         jmethodID createAudioTrackFromNativeID;
         jmethodID createVideoTrackFromNativeID;
         jmethodID createSubtitleTrackFromNativeID;
         jmethodID createUnknownTrackFromNativeID;
+        jmethodID createSlaveFromNativeID;
     } Media;
     struct {
         struct {
diff --git a/libvlc/src/org/videolan/libvlc/Media.java b/libvlc/src/org/videolan/libvlc/Media.java
index 2743cff..f277441 100644
--- a/libvlc/src/org/videolan/libvlc/Media.java
+++ b/libvlc/src/org/videolan/libvlc/Media.java
@@ -21,6 +21,7 @@
 package org.videolan.libvlc;
 
 import android.net.Uri;
+import android.support.annotation.Nullable;
 
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.libvlc.util.HWDecoderUtil;
@@ -258,6 +259,33 @@ public class Media extends VLCObject<Media.Event> {
                 level, bitrate, language, description);
     }
 
+    /**
+     * see libvlc_media_slave_t
+     */
+    public static class Slave {
+        public static class Type {
+            public static final int Subtitle = 0;
+            public static final int Audio = 1;
+        }
+
+        /** @see Type */
+        public final int type;
+        /** From 0 (low priority) to 4 (high priority) */
+        public final int priority;
+        public final String uri;
+
+        public Slave(int type, int priority, String uri) {
+            this.type = type;
+            this.priority = priority;
+            this.uri = uri;
+        }
+    }
+
+    @SuppressWarnings("unused") /* Used from JNI */
+    private static Slave createSlaveFromNative(int type, int priority, String uri) {
+        return new Slave(type, priority, uri);
+    }
+
     private static final int PARSE_STATUS_INIT = 0x00;
     private static final int PARSE_STATUS_PARSING = 0x01;
     private static final int PARSE_STATUS_PARSED = 0x02;
@@ -691,6 +719,39 @@ public class Media extends VLCObject<Media.Event> {
         nativeAddOption(option);
     }
 
+
+    /**
+     * Add a slave to the current media.
+     *
+     * A slave is an external input source that may contains an additional subtitle
+     * track (like a .srt) or an additional audio track (like a .ac3).
+     *
+     * This function must be called before the media is parsed (via {@link #parseAsync(int)}} or
+     * before the media is played (via {@link MediaPlayer#play()})
+     */
+    public void addSlave(Slave slave) {
+        nativeAddSlave(slave.type, slave.priority, slave.uri);
+    }
+
+    /**
+     * Clear all slaves previously added by {@link #addSlave(Slave)} or internally.
+     */
+    public void clearSlaves() {
+        nativeClearSlaves();
+    }
+
+    /**
+     * Get a media's slave list
+     *
+     * The list will contain slaves parsed by VLC or previously added by
+     * {@link #addSlave(Slave)}. The typical use case of this function is to save
+     * a list of slave in a database for a later use.
+     */
+    @Nullable
+    public Slave[] getSlaves() {
+        return nativeGetSlaves();
+    }
+
     @Override
     protected void onReleaseNative() {
         if (mSubItems != null)
@@ -713,4 +774,7 @@ public class Media extends VLCObject<Media.Event> {
     private native long nativeGetDuration();
     private native int nativeGetType();
     private native void nativeAddOption(String option);
+    private native void nativeAddSlave(int type, int priority, String uri);
+    private native void nativeClearSlaves();
+    private native Slave[] nativeGetSlaves();
 }
\ No newline at end of file



More information about the Android mailing list