[Android] [PATCH 1/2] LibVLC: MediaDiscoverer: add getServices

Thomas Guillem thomas at gllm.fr
Tue Feb 9 15:18:32 CET 2016


---
 libvlc/jni/libvlcjni-mediadiscoverer.c             | 54 ++++++++++++++++++++++
 libvlc/jni/libvlcjni.c                             | 11 +++++
 libvlc/jni/utils.h                                 |  7 +++
 .../src/org/videolan/libvlc/MediaDiscoverer.java   | 38 +++++++++++++++
 4 files changed, 110 insertions(+)

diff --git a/libvlc/jni/libvlcjni-mediadiscoverer.c b/libvlc/jni/libvlcjni-mediadiscoverer.c
index ae35cf2..2a3787d 100644
--- a/libvlc/jni/libvlcjni-mediadiscoverer.c
+++ b/libvlc/jni/libvlcjni-mediadiscoverer.c
@@ -88,3 +88,57 @@ Java_org_videolan_libvlc_MediaDiscoverer_nativeStop(JNIEnv *env, jobject thiz)
 
     libvlc_media_discoverer_stop(p_obj->u.p_md);
 }
+
+
+static jobject
+service_to_object(JNIEnv *env, libvlc_media_discoverer_service *p_service)
+{
+    jstring jname = NULL;
+    jstring jlongName = NULL;
+
+    jname = (*env)->NewStringUTF(env, p_service->psz_name);
+    jlongName = (*env)->NewStringUTF(env, p_service->psz_longname);
+
+    return (*env)->CallStaticObjectMethod(env, fields.MediaDiscoverer.clazz,
+                        fields.MediaDiscoverer.createServiceFromNativeID,
+                        jname, jlongName, p_service->i_cat);
+}
+
+jobject
+Java_org_videolan_libvlc_MediaDiscoverer_nativeGetServices(JNIEnv *env,
+                                                           jobject thiz,
+                                                           jobject libVlc,
+                                                           jint i_category)
+{
+    vlcjni_object *p_lib_obj = VLCJniObject_getInstance(env, libVlc);
+    libvlc_instance_t *p_libvlc = p_lib_obj->u.p_libvlc;
+    libvlc_media_discoverer_service **pp_services = NULL;
+    unsigned int i_nb_services = 0;
+    jobjectArray array;
+
+    if (!p_lib_obj)
+        return NULL;
+
+    i_nb_services =
+        libvlc_media_discoverer_services_get( p_libvlc, i_category,
+                                              &pp_services);
+    if (i_nb_services == 0)
+        return NULL;
+
+    array = (*env)->NewObjectArray(env, i_nb_services,
+                                   fields.MediaDiscoverer.Service.clazz, NULL);
+    if (!array)
+        goto error;
+
+    for (int i = 0; i < i_nb_services; ++i)
+    {
+        jobject jservice = service_to_object(env, pp_services[i]);
+
+        (*env)->SetObjectArrayElement(env, array, i, jservice);
+    }
+
+error:
+    if (pp_services)
+        libvlc_media_discoverer_services_release(pp_services, i_nb_services);
+    return array;
+}
diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index 235a4cb..0262482 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -186,6 +186,10 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
               "org/videolan/libvlc/MediaPlayer$TrackDescription", true);
     GET_CLASS(fields.MediaPlayer.Equalizer.clazz,
               "org/videolan/libvlc/MediaPlayer$Equalizer", true);
+    GET_CLASS(fields.MediaDiscoverer.clazz,
+              "org/videolan/libvlc/MediaDiscoverer", true);
+    GET_CLASS(fields.MediaDiscoverer.Service.clazz,
+              "org/videolan/libvlc/MediaDiscoverer$Service", true);
     GET_CLASS(fields.Dialog.clazz,
               "org/videolan/libvlc/Dialog", true);
 
@@ -273,6 +277,13 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
            "(ILjava/lang/String;)Lorg/videolan/libvlc/MediaPlayer$TrackDescription;");
 
     GET_ID(GetStaticMethodID,
+           fields.MediaDiscoverer.createServiceFromNativeID,
+           fields.MediaDiscoverer.clazz,
+           "createServiceFromNative",
+           "(Ljava/lang/String;Ljava/lang/String;I)"
+           "Lorg/videolan/libvlc/MediaDiscoverer$Service;");
+
+    GET_ID(GetStaticMethodID,
            fields.Dialog.displayErrorFromNativeID,
            fields.Dialog.clazz,
            "displayErrorFromNative",
diff --git a/libvlc/jni/utils.h b/libvlc/jni/utils.h
index 7a9232f..6544169 100644
--- a/libvlc/jni/utils.h
+++ b/libvlc/jni/utils.h
@@ -83,6 +83,13 @@ struct fields {
         jmethodID createTrackDescriptionFromNativeID;
     } MediaPlayer;
     struct {
+        struct {
+            jclass clazz;
+        } Service;
+        jclass clazz;
+        jmethodID createServiceFromNativeID;
+    } MediaDiscoverer;
+    struct {
         jclass clazz;
         jmethodID displayErrorFromNativeID;
         jmethodID displayLoginFromNativeID;
diff --git a/libvlc/src/org/videolan/libvlc/MediaDiscoverer.java b/libvlc/src/org/videolan/libvlc/MediaDiscoverer.java
index 9989a87..810f4c0 100644
--- a/libvlc/src/org/videolan/libvlc/MediaDiscoverer.java
+++ b/libvlc/src/org/videolan/libvlc/MediaDiscoverer.java
@@ -34,6 +34,35 @@ public class MediaDiscoverer extends VLCObject<MediaDiscoverer.Event> {
         }
     }
 
+    public static class Service {
+        public static class Category {
+            /** devices, like portable music player */
+            public static final int Devices = 0;
+            /** LAN/WAN services, like Upnp, SMB, or SAP */
+            public static final int Lan = 1;
+            /** Internet of Website channels services */
+            public static final int Internet = 2;
+            /** Computer services, like Discs or Apps */
+            public static final int MyComputer = 3;
+        }
+        public final String name;
+        public final String longName;
+        public final int category;
+
+        private Service(String name, String longName, int category)
+        {
+            this.name = name;
+            this.longName = longName;
+            this.category = category;
+        }
+    }
+
+    @SuppressWarnings("unused") /* Used from JNI */
+    private static Service createServiceFromNative(String name, String longName, int category)
+    {
+        return new Service(name, longName, category);
+    }
+
     public interface EventListener extends VLCEvent.Listener<MediaDiscoverer.Event> {}
 
     private MediaList mMediaList = null;
@@ -111,9 +140,18 @@ public class MediaDiscoverer extends VLCObject<MediaDiscoverer.Event> {
         nativeRelease();
     }
 
+    /**
+     * Get media discoverer services by category
+     * @param category see {@link MediaDiscoverer.Service.Category}
+     */
+    public static Service[] getServices(LibVLC libVLC, int category) {
+        return nativeGetServices(libVLC, category);
+    }
+
     /* JNI */
     private native void nativeNew(LibVLC libVLC, String name);
     private native void nativeRelease();
     private native boolean nativeStart();
     private native void nativeStop();
+    private static native Service[] nativeGetServices(LibVLC libVLC, int category);
 }
-- 
2.7.0.rc3



More information about the Android mailing list