[Android] [PATCH 3/3] MediaBrowser: change network discovery

Thomas Guillem thomas at gllm.fr
Mon Feb 2 18:33:24 CET 2015


Use discoverNetworks to discover networks. More than one MediaDiscoverer can be
used. See DISCOVER_LIST.
---
 .../src/org/videolan/libvlc/util/MediaBrowser.java | 79 +++++++++++++++-------
 1 file changed, 54 insertions(+), 25 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
index d5b1353..4371823 100644
--- a/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
+++ b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
@@ -20,6 +20,8 @@
 
 package org.videolan.libvlc.util;
 
+import java.util.ArrayList;
+
 import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.Media;
 import org.videolan.libvlc.MediaDiscoverer;
@@ -29,8 +31,15 @@ import org.videolan.libvlc.VLCObject;
 public class MediaBrowser {
     private static final String TAG = "LibVLC/util/MediaBrowser";
 
+    private static final String[] DISCOVERER_LIST = {
+        "dsm", // Netbios discovery via libdsm
+        // "bonjour",
+        //  "mdns"
+    };
+
     private LibVLC mLibVlc;
-    private MediaDiscoverer mMediaDiscoverer;
+    private ArrayList<MediaDiscoverer> mMediaDiscoverers = new ArrayList<MediaDiscoverer>();
+    private ArrayList<Media> mMediaArray = new ArrayList<Media>();
     private MediaList mMediaList;
     private Media mMedia;
     private EventListener mEventListener;
@@ -39,11 +48,22 @@ public class MediaBrowser {
      * Listener called when medias are added or removed.
      */
     public interface EventListener {
+        /**
+         * Received when a new media is added.
+         * @param index
+         * @param media
+         */
         public void onMediaAdded(int index, Media media);
-        public void onMediaRemoved(int index);
+        /**
+         * Received when a media is removed (Happens only when you discover networks)
+         * @param index
+         * @param media Released media, but cached attributes are still
+         * available (like media.getMrl())
+         */
+        public void onMediaRemoved(int index, Media media);
         /**
          * Called when browse ended.
-         * It won't be called when you browse a service discovery.
+         * It won't be called when you discover networks
          */
         public void onBrowseEnd();
     }
@@ -54,10 +74,10 @@ public class MediaBrowser {
     }
 
     private synchronized void reset() {
-        if (mMediaDiscoverer != null) {
-            mMediaDiscoverer.release();
-            mMediaDiscoverer = null;
-        }
+        for (MediaDiscoverer md : mMediaDiscoverers)
+            md.release();
+        mMediaDiscoverers.clear();
+        mMediaArray.clear();
         if (mMedia != null) {
             mMedia.release();
             mMedia = null;
@@ -74,26 +94,25 @@ public class MediaBrowser {
         reset();
     }
 
+    public synchronized void discoverNetworks() {
+        reset();
+        for (String discovererName : DISCOVERER_LIST) {
+            MediaDiscoverer md = new MediaDiscoverer(mLibVlc, discovererName);
+            mMediaDiscoverers.add(md);
+            final MediaList ml = md.getMediaList();
+            ml.setEventListener(mMediaListEventListener);
+            md.start();
+        }
+    }
     /**
      * Browse to the specified mrl.
      *
      * @param mrl
      */
     public synchronized void browse(String mrl) {
-        if (!mrl.contains("://") && !mrl.startsWith("/")) {
-            reset();
-            if (mrl.equals("smb"))
-                mMediaDiscoverer = new MediaDiscoverer(mLibVlc, "dsm");
-            else
-                mMediaDiscoverer = new MediaDiscoverer(mLibVlc, mrl);
-            mMediaList = mMediaDiscoverer.getMediaList();
-            mMediaList.setEventListener(mMediaListEventListener);
-            mMediaDiscoverer.start();
-        } else {
-            final Media media = new Media(mLibVlc, mrl);
-            browse(media);
-            media.release();
-        }
+        final Media media = new Media(mLibVlc, mrl);
+        browse(media);
+        media.release();
     }
 
     /**
@@ -117,14 +136,14 @@ public class MediaBrowser {
      * Get the number or media.
      */
     public synchronized int getMediaCount() {
-        return mMediaList != null ? mMediaList.getCount() : 0;
+        return mMediaArray.size();
     }
 
     /**
      * Get a media at a specified index.
      */
     public synchronized Media getMediaAt(int index) {
-        return mMediaList != null ? mMediaList.getMediaAt(index) : null;
+        return index >= 0 && index < mMediaArray.size() ? mMediaArray.get(index) : null;
     }
 
     private MediaList.EventListener mMediaListEventListener = new MediaList.EventListener() {
@@ -133,13 +152,23 @@ public class MediaBrowser {
             if (mEventListener == null)
                 return;
             final MediaList.Event mlEvent = (MediaList.Event) event;
+            int index;
 
+            /*
+             * We use an intermediate array here since more than one MediaDiscoverer can be used
+             */
             switch (mlEvent.type) {
             case MediaList.Events.MediaListItemAdded:
-                mEventListener.onMediaAdded(mlEvent.index, mlEvent.media);
+                mMediaArray.add(mlEvent.media);
+                index = mMediaArray.size() - 1;
+                mEventListener.onMediaAdded(index, mlEvent.media);
                 break;
             case MediaList.Events.MediaListItemDeleted:
-                mEventListener.onMediaRemoved(mlEvent.index);
+                index = mMediaArray.indexOf(mlEvent.media);
+                if (index != -1) {
+                    mMediaArray.remove(index);
+                    mEventListener.onMediaRemoved(index, mlEvent.media);
+                }
                 break;
             case MediaList.Events.MediaListEndReached:
                 mEventListener.onBrowseEnd();
-- 
2.1.3



More information about the Android mailing list