[Android] [PATCH 3/3] MediaWrapper: remove constructor with LibVLC

Thomas Guillem thomas at gllm.fr
Fri Jan 23 10:41:05 CET 2015


This constructor was parsing a media and could take a long time. Replace it
with a mrl constructor that don't parse a media.

Indeed, MediaWrapper is only a wrapper and is often used in the UI Thread. It
shouldn't do any parsing (but it still fetch data that is already parsed when
constructed from a media).

Add "// FIXME" for all media that are parsed in UI Thread.
---
 vlc-android/src/org/videolan/vlc/MediaLibrary.java | 10 ++++++---
 vlc-android/src/org/videolan/vlc/MediaWrapper.java | 17 ++++++---------
 .../src/org/videolan/vlc/MediaWrapperList.java     |  4 +---
 .../org/videolan/vlc/MediaWrapperListPlayer.java   |  6 +++++-
 .../src/org/videolan/vlc/audio/AudioService.java   | 25 ++++++++++++++--------
 .../src/org/videolan/vlc/gui/DirectoryAdapter.java | 10 ++++++---
 .../vlc/gui/video/VideoPlayerActivity.java         |  6 +++++-
 vlc-android/src/org/videolan/vlc/util/Util.java    |  9 ++++++--
 8 files changed, 54 insertions(+), 33 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
index bc94389..4a3775b 100644
--- a/vlc-android/src/org/videolan/vlc/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
@@ -35,6 +35,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.LibVlcException;
+import org.videolan.libvlc.Media;
 import org.videolan.libvlc.util.Extensions;
 import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.util.AndroidDevices;
@@ -319,11 +320,14 @@ public class MediaLibrary {
                     } else {
                         mItemListLock.writeLock().lock();
                         // create new media item
-                        MediaWrapper m = new MediaWrapper(libVlcInstance, fileURI);
-                        mItemList.add(m);
+                        final Media media = new Media(libVlcInstance, fileURI);
+                        media.parse();
+                        media.release();
+                        MediaWrapper mw = new MediaWrapper(media);
+                        mItemList.add(mw);
                         // Add this item to database
                         MediaDatabase db = MediaDatabase.getInstance();
-                        db.addMedia(m);
+                        db.addMedia(mw);
                         mItemListLock.writeLock().unlock();
                     }
                     if (isStopping) {
diff --git a/vlc-android/src/org/videolan/vlc/MediaWrapper.java b/vlc-android/src/org/videolan/vlc/MediaWrapper.java
index a391632..de1a3e9 100644
--- a/vlc-android/src/org/videolan/vlc/MediaWrapper.java
+++ b/vlc-android/src/org/videolan/vlc/MediaWrapper.java
@@ -74,21 +74,16 @@ public class MediaWrapper implements Parcelable {
     private boolean mIsPictureParsed;
     private int mFlags = 0;
 
-
     /**
      * Create a new MediaWrapper
-     * @param libVLC A pointer to the libVLC instance. Should not be NULL
-     * @param URI The URI of the media.
+     * @param mrl Should not be null.
      */
-    public MediaWrapper(LibVLC libVLC, String mrl) {
-        if(libVLC == null)
-            throw new NullPointerException("libVLC was null");
+    public MediaWrapper(String mrl) {
+        if (mrl == null)
+            throw new NullPointerException("mrl was null");
 
-        final Media media = new Media(libVLC, mrl);
-        media.parse();
-        media.release();
-        mLocation = media.getMrl();
-        init(media);
+        mLocation = mrl;
+        init(null);
     }
 
     /**
diff --git a/vlc-android/src/org/videolan/vlc/MediaWrapperList.java b/vlc-android/src/org/videolan/vlc/MediaWrapperList.java
index e28fe89..f955820 100644
--- a/vlc-android/src/org/videolan/vlc/MediaWrapperList.java
+++ b/vlc-android/src/org/videolan/vlc/MediaWrapperList.java
@@ -39,13 +39,11 @@ public class MediaWrapperList {
 
     /* TODO: add locking */
     private ArrayList<MediaWrapper> mInternalList;
-    private LibVLC mLibVLC; // Used to create new objects that require a libvlc instance
     private ArrayList<EventListener> mEventListenerList;
 
     public MediaWrapperList(LibVLC libVLC) {
         mEventListenerList = new ArrayList<EventListener>();
         mInternalList = new ArrayList<MediaWrapper>();
-        mLibVLC = libVLC;
     }
 
     public void add(MediaWrapper media) {
@@ -92,7 +90,7 @@ public class MediaWrapperList {
     }
 
     public void insert(int position, String mrl) {
-        insert(position, new MediaWrapper(mLibVLC, mrl));
+        insert(position, new MediaWrapper(mrl));
     }
     public void insert(int position, MediaWrapper media) {
         mInternalList.add(position, media);
diff --git a/vlc-android/src/org/videolan/vlc/MediaWrapperListPlayer.java b/vlc-android/src/org/videolan/vlc/MediaWrapperListPlayer.java
index 0cde4f2..e8a70b7 100644
--- a/vlc-android/src/org/videolan/vlc/MediaWrapperListPlayer.java
+++ b/vlc-android/src/org/videolan/vlc/MediaWrapperListPlayer.java
@@ -23,6 +23,7 @@ package org.videolan.vlc;
 import java.util.ArrayList;
 
 import org.videolan.libvlc.LibVLC;
+import org.videolan.libvlc.Media;
 
 
 public class MediaWrapperListPlayer {
@@ -86,7 +87,10 @@ public class MediaWrapperListPlayer {
        if(ret == 0) {
            mMediaList.remove(mPlayerIndex);
            for(String mrl : children) {
-               mMediaList.insert(mPlayerIndex, mrl);
+               final Media media = new Media(mLibVLC, mrl);
+               media.parse(); // FIXME: parse should be done asynchronously
+               media.release();
+               mMediaList.insert(mPlayerIndex, new MediaWrapper(media));
            }
        }
        return ret;
diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
index 0da3b32..d39b345 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
@@ -36,6 +36,7 @@ import org.videolan.libvlc.EventHandler;
 import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.LibVlcException;
 import org.videolan.libvlc.LibVlcUtil;
+import org.videolan.libvlc.Media;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.MediaDatabase;
 import org.videolan.vlc.MediaWrapperList;
@@ -1169,19 +1170,22 @@ public class AudioService extends Service {
             MediaDatabase db = MediaDatabase.getInstance();
             for (int i = 0; i < mediaPathList.size(); i++) {
                 String location = mediaPathList.get(i);
-                MediaWrapper media = db.getMedia(location);
-                if(media == null) {
+                MediaWrapper mediaWrapper = db.getMedia(location);
+                if(mediaWrapper == null) {
                     if(!validateLocation(location)) {
                         Log.w(TAG, "Invalid location " + location);
                         showToast(getResources().getString(R.string.invalid_location, location), Toast.LENGTH_SHORT);
                         continue;
                     }
                     Log.v(TAG, "Creating on-the-fly Media object for " + location);
-                    media = new MediaWrapper(mLibVLC, location);
+                    final Media media = new Media(mLibVLC, location);
+                    media.parse(); // FIXME: parse should be done asynchronously
+                    media.release();
+                    mediaWrapper = new MediaWrapper(media);
                 }
                 if (noVideo)
-                    media.addFlags(LibVLC.MEDIA_NO_VIDEO);
-                mediaList.add(media);
+                    mediaWrapper.addFlags(LibVLC.MEDIA_NO_VIDEO);
+                mediaList.add(mediaWrapper);
             }
 
             if (mMediaListPlayer.getMediaList().size() == 0) {
@@ -1278,16 +1282,19 @@ public class AudioService extends Service {
             MediaDatabase db = MediaDatabase.getInstance();
             for (int i = 0; i < mediaLocationList.size(); i++) {
                 String location = mediaLocationList.get(i);
-                MediaWrapper media = db.getMedia(location);
-                if(media == null) {
+                MediaWrapper mediaWrapper = db.getMedia(location);
+                if(mediaWrapper == null) {
                     if (!validateLocation(location)) {
                         showToast(getResources().getString(R.string.invalid_location, location), Toast.LENGTH_SHORT);
                         continue;
                     }
                     Log.v(TAG, "Creating on-the-fly Media object for " + location);
-                    media = new MediaWrapper(mLibVLC, location);
+                    final Media media = new Media(mLibVLC, location);
+                    media.parse(); // FIXME: parse should'nt be done asynchronously
+                    media.release();
+                    mediaWrapper = new MediaWrapper(media);
                 }
-                mMediaListPlayer.getMediaList().add(media);
+                mMediaListPlayer.getMediaList().add(mediaWrapper);
             }
             AudioService.this.saveMediaList();
             determinePrevAndNextIndices();
diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
index 9488a95..90b11e5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
@@ -29,6 +29,7 @@ import java.util.ListIterator;
 import java.util.regex.Pattern;
 
 import org.videolan.libvlc.LibVLC;
+import org.videolan.libvlc.Media;
 import org.videolan.libvlc.util.Extensions;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.R;
@@ -344,9 +345,12 @@ public class DirectoryAdapter extends BaseAdapter {
         String holderText = "";
         if(selectedNode.isFile()) {
             Log.d(TAG, "Loading media " + selectedNode.name);
-            MediaWrapper m = new MediaWrapper(LibVLC.getExistingInstance(), getMediaLocation(position));
-            holder.title.setText(m.getTitle());
-            holderText = Util.getMediaSubtitle(context, m);
+            final Media media = new Media(LibVLC.getExistingInstance(), getMediaLocation(position));
+            media.parse(); // FIXME: parse should'nt be done asynchronously
+            media.release();
+            MediaWrapper mw = new MediaWrapper(media);
+            holder.title.setText(mw.getTitle());
+            holderText = Util.getMediaSubtitle(context, mw);
         } else
             holder.title.setText(selectedNode.getVisibleName());
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index b401c7c..604d015 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -43,6 +43,7 @@ import org.videolan.libvlc.IVideoPlayer;
 import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.LibVlcException;
 import org.videolan.libvlc.LibVlcUtil;
+import org.videolan.libvlc.Media;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.MediaDatabase;
 import org.videolan.vlc.MediaWrapperListPlayer;
@@ -2379,7 +2380,10 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         } else if (mLocation != null && mLocation.length() > 0) {
             AudioServiceController.getInstance().stop(); // Stop the previous playback.
             mMediaListPlayer.getMediaList().clear();
-            mMediaListPlayer.getMediaList().add(new MediaWrapper(mLibVLC, mLocation));
+            final Media media = new Media(mLibVLC, mLocation);
+            media.parse(); // FIXME: parse should'nt be done asynchronously
+            media.release();
+            mMediaListPlayer.getMediaList().add(new MediaWrapper(media));
             savedIndexPosition = mMediaListPlayer.getMediaList().size() - 1;
             mMediaListPlayer.playIndex(savedIndexPosition);
         }
diff --git a/vlc-android/src/org/videolan/vlc/util/Util.java b/vlc-android/src/org/videolan/vlc/util/Util.java
index 0db056b..06f72e9 100644
--- a/vlc-android/src/org/videolan/vlc/util/Util.java
+++ b/vlc-android/src/org/videolan/vlc/util/Util.java
@@ -29,6 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.LibVlcUtil;
+import org.videolan.libvlc.Media;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
@@ -106,8 +107,12 @@ public class Util {
      */
     public static MediaWrapper getOrCreateMedia(LibVLC libVLC, String mrl) {
         MediaWrapper mlItem = MediaLibrary.getInstance().getMediaItem(mrl);
-        if(mlItem == null)
-            mlItem = new MediaWrapper(libVLC, mrl);
+        if(mlItem == null) {
+            final Media media = new Media(libVLC, mrl);
+            media.parse(); // FIXME: parse should'nt be done asynchronously
+            media.release();
+            mlItem = new MediaWrapper(media);
+        }
         return mlItem;
     }
 
-- 
2.1.3



More information about the Android mailing list