Commit 9880ab8a authored by Thomas Guillem's avatar Thomas Guillem

MediaWrapper: remove constructor with LibVLC

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.
parent ae6d2191
......@@ -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) {
......
......@@ -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);
}
/**
......
......@@ -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);
......
......@@ -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;
......
......@@ -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();
......
......@@ -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());
......
......@@ -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);
}
......
......@@ -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;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment