Commit 5fa3cdd3 authored by Thomas Guillem's avatar Thomas Guillem
Browse files

refactor Media/MediaList/MediaListPlayer

Prepare for new Media* class that follow libvlc API.

Rename Media to MediaWrapper since it's a wrapper of the future Media class,
and also a wrapper of MediaDatabase elements.

Move these classes from libvlc to vlc-android since these classes depends too
much of the vlc application. Indeed there are mainly used by MediaDatabase that
belongs to application and not the lib.
parent c0923bd3
......@@ -97,7 +97,7 @@ public class EventHandler {
private ArrayList<Handler> mEventHandler;
private static EventHandler mInstance;
EventHandler() {
public EventHandler() {
mEventHandler = new ArrayList<Handler>();
}
......
......@@ -796,6 +796,5 @@ public class LibVLC {
public native int setWindowSize(int width, int height);
/* MediaList */
protected native void loadPlaylist(String mrl, ArrayList<String> items);
protected native int expandMedia(ArrayList<String> children);
public native int expandMedia(ArrayList<String> children);
}
......@@ -31,8 +31,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.videolan.libvlc.Media;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
......@@ -478,7 +476,7 @@ public class MediaDatabase {
* Add a new media to the database. The picture can only added by update.
* @param media which you like to add to the database
*/
public synchronized void addMedia(Media media) {
public synchronized void addMedia(MediaWrapper media) {
ContentValues values = new ContentValues();
......@@ -556,7 +554,7 @@ public class MediaDatabase {
String[] queryColumns = new String[]{MEDIA_LOCATION, MEDIA_TITLE, MEDIA_ALBUM, MEDIA_ARTIST, MEDIA_TYPE};
String queryString = MEDIA_TITLE+" LIKE ? OR "+MEDIA_ALBUM+" LIKE ? OR "+MEDIA_ARTIST+" LIKE ?";
String [] queryArgs;
if (type != Media.TYPE_ALL) {
if (type != MediaWrapper.TYPE_ALL) {
queryString = "( " + queryString + " ) AND " + MEDIA_TYPE + "=?";
queryArgs = new String[]{"%"+filter+"%", "%"+filter+"%", "%"+filter+"%", String.valueOf(type)};
} else
......@@ -573,10 +571,10 @@ public class MediaDatabase {
return mediaList;
}
public synchronized HashMap<String, Media> getMedias() {
public synchronized HashMap<String, MediaWrapper> getMedias() {
Cursor cursor;
HashMap<String, Media> medias = new HashMap<String, Media>();
HashMap<String, MediaWrapper> medias = new HashMap<String, MediaWrapper>();
int chunk_count = 0;
int count = 0;
......@@ -606,7 +604,7 @@ public class MediaDatabase {
if (cursor.moveToFirst()) {
do {
String location = cursor.getString(14);
Media media = new Media(location,
MediaWrapper media = new MediaWrapper(location,
cursor.getLong(0), // MEDIA_TIME
cursor.getLong(1), // MEDIA_LENGTH
cursor.getInt(2), // MEDIA_TYPE
......@@ -650,7 +648,7 @@ public class MediaDatabase {
MEDIA_TIME, //1 long
MEDIA_TABLE_NAME,
MEDIA_TYPE,
Media.TYPE_VIDEO,
MediaWrapper.TYPE_VIDEO,
CHUNK_SIZE,
chunk_count * CHUNK_SIZE), null);
......@@ -670,10 +668,10 @@ public class MediaDatabase {
return times;
}
public synchronized Media getMedia(String location) {
public synchronized MediaWrapper getMedia(String location) {
Cursor cursor;
Media media = null;
MediaWrapper media = null;
try {
cursor = mDb.query(
......@@ -702,7 +700,7 @@ public class MediaDatabase {
return null;
}
if (cursor.moveToFirst()) {
media = new Media(location,
media = new MediaWrapper(location,
cursor.getLong(0),
cursor.getLong(1),
cursor.getInt(2),
......@@ -946,7 +944,7 @@ public class MediaDatabase {
mDb.delete(MEDIA_TABLE_NAME, null, null);
}
public static void setPicture(Media m, Bitmap p) {
public static void setPicture(MediaWrapper m, Bitmap p) {
Log.d(TAG, "Setting new picture for " + m.getTitle());
try {
getInstance().updateMedia(
......
......@@ -23,23 +23,22 @@ package org.videolan.vlc;
import java.util.ArrayList;
import java.util.List;
import org.videolan.libvlc.Media;
import org.videolan.vlc.util.BitmapUtil;
public class MediaGroup extends Media {
public class MediaGroup extends MediaWrapper {
public final static String TAG = "VLC/MediaGroup";
public final static int MIN_GROUP_LENGTH = 6;
private ArrayList<Media> mMedias;
private ArrayList<MediaWrapper> mMedias;
public MediaGroup(Media media)
public MediaGroup(MediaWrapper media)
{
super(media.getLocation(),
media.getTime(),
media.getLength(),
Media.TYPE_GROUP,
MediaWrapper.TYPE_GROUP,
BitmapUtil.getPictureFromCache(media),
media.getTitle(),
media.getArtist(),
......@@ -52,19 +51,19 @@ public class MediaGroup extends Media {
media.getAudioTrack(),
media.getSpuTrack(),
media.getTrackNumber());
mMedias = new ArrayList<Media>();
mMedias = new ArrayList<MediaWrapper>();
mMedias.add(media);
}
public void add(Media media) {
public void add(MediaWrapper media) {
mMedias.add(media);
}
public Media getMedia() {
public MediaWrapper getMedia() {
return mMedias.size() == 1 ? mMedias.get(0) : this;
}
public Media getFirstMedia() {
public MediaWrapper getFirstMedia() {
return mMedias.get(0);
}
......@@ -72,19 +71,19 @@ public class MediaGroup extends Media {
return mMedias.size();
}
public void merge(Media media, String title) {
public void merge(MediaWrapper media, String title) {
mMedias.add(media);
this.mTitle = title;
}
public static List<MediaGroup> group(List<Media> mediaList) {
public static List<MediaGroup> group(List<MediaWrapper> mediaList) {
ArrayList<MediaGroup> groups = new ArrayList<MediaGroup>();
for (Media media : mediaList)
for (MediaWrapper media : mediaList)
insertInto(groups, media);
return groups;
}
private static void insertInto(ArrayList<MediaGroup> groups, Media media)
private static void insertInto(ArrayList<MediaGroup> groups, MediaWrapper media)
{
for (MediaGroup mediaGroup : groups) {
String group = mediaGroup.getTitle();
......
......@@ -35,7 +35,6 @@ 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;
......@@ -55,7 +54,7 @@ public class MediaLibrary {
public static final int MEDIA_ITEMS_UPDATED = 100;
private static MediaLibrary mInstance;
private final ArrayList<Media> mItemList;
private final ArrayList<MediaWrapper> mItemList;
private final ArrayList<Handler> mUpdateHandler;
private final ReadWriteLock mItemListLock;
private boolean isStopping = false;
......@@ -83,7 +82,7 @@ public class MediaLibrary {
private MediaLibrary() {
mInstance = this;
mItemList = new ArrayList<Media>();
mItemList = new ArrayList<MediaWrapper>();
mUpdateHandler = new ArrayList<Handler>();
mItemListLock = new ReentrantReadWriteLock();
}
......@@ -134,8 +133,8 @@ public class MediaLibrary {
mUpdateHandler.remove(handler);
}
public ArrayList<Media> searchMedia(String query, int type){
ArrayList<Media> mediaList = new ArrayList<Media>();
public ArrayList<MediaWrapper> searchMedia(String query, int type){
ArrayList<MediaWrapper> mediaList = new ArrayList<MediaWrapper>();
ArrayList<String> pathList = MediaDatabase.getInstance().searchMedia(query, type);
if (!pathList.isEmpty()){
for (String path : pathList) {
......@@ -145,12 +144,12 @@ public class MediaLibrary {
return mediaList;
}
public ArrayList<Media> getVideoItems() {
ArrayList<Media> videoItems = new ArrayList<Media>();
public ArrayList<MediaWrapper> getVideoItems() {
ArrayList<MediaWrapper> videoItems = new ArrayList<MediaWrapper>();
mItemListLock.readLock().lock();
for (int i = 0; i < mItemList.size(); i++) {
Media item = mItemList.get(i);
if (item != null && item.getType() == Media.TYPE_VIDEO) {
MediaWrapper item = mItemList.get(i);
if (item != null && item.getType() == MediaWrapper.TYPE_VIDEO) {
videoItems.add(item);
}
}
......@@ -158,12 +157,12 @@ public class MediaLibrary {
return videoItems;
}
public ArrayList<Media> getAudioItems() {
ArrayList<Media> audioItems = new ArrayList<Media>();
public ArrayList<MediaWrapper> getAudioItems() {
ArrayList<MediaWrapper> audioItems = new ArrayList<MediaWrapper>();
mItemListLock.readLock().lock();
for (int i = 0; i < mItemList.size(); i++) {
Media item = mItemList.get(i);
if (item.getType() == Media.TYPE_AUDIO) {
MediaWrapper item = mItemList.get(i);
if (item.getType() == MediaWrapper.TYPE_AUDIO) {
audioItems.add(item);
}
}
......@@ -171,14 +170,14 @@ public class MediaLibrary {
return audioItems;
}
public ArrayList<Media> getMediaItems() {
public ArrayList<MediaWrapper> getMediaItems() {
return mItemList;
}
public Media getMediaItem(String location) {
public MediaWrapper getMediaItem(String location) {
mItemListLock.readLock().lock();
for (int i = 0; i < mItemList.size(); i++) {
Media item = mItemList.get(i);
MediaWrapper item = mItemList.get(i);
if (item.getLocation().equals(location)) {
mItemListLock.readLock().unlock();
return item;
......@@ -188,10 +187,10 @@ public class MediaLibrary {
return null;
}
public ArrayList<Media> getMediaItems(List<String> pathList) {
ArrayList<Media> items = new ArrayList<Media>();
public ArrayList<MediaWrapper> getMediaItems(List<String> pathList) {
ArrayList<MediaWrapper> items = new ArrayList<MediaWrapper>();
for (int i = 0; i < pathList.size(); i++) {
Media item = getMediaItem(pathList.get(i));
MediaWrapper item = getMediaItem(pathList.get(i));
items.add(item);
}
return items;
......@@ -234,7 +233,7 @@ public class MediaLibrary {
directories.addAll(mediaDirs);
// get all existing media items
HashMap<String, Media> existingMedias = DBManager.getMedias();
HashMap<String, MediaWrapper> existingMedias = DBManager.getMedias();
// list of all added files
HashSet<String> addedLocations = new HashSet<String>();
......@@ -320,7 +319,7 @@ public class MediaLibrary {
} else {
mItemListLock.writeLock().lock();
// create new media item
Media m = new Media(libVlcInstance, fileURI);
MediaWrapper m = new MediaWrapper(libVlcInstance, fileURI);
mItemList.add(m);
// Add this item to database
MediaDatabase db = MediaDatabase.getInstance();
......
/*****************************************************************************
* Media.aidl
* MediaWrapper.aidl
*****************************************************************************
* Copyright © 2015 VLC authors and VideoLAN
*
......@@ -18,6 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.libvlc;
package org.videolan.vlc;
parcelable Media;
parcelable MediaWrapper;
/*****************************************************************************
* Media.java
* MediaWrapper.java
*****************************************************************************
* Copyright © 2011-2013 VLC authors and VideoLAN
* Copyright © 2011-2015 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
......@@ -18,11 +18,14 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.libvlc;
package org.videolan.vlc;
import java.util.Locale;
import org.videolan.libvlc.util.Extensions;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcUtil;
import org.videolan.libvlc.TrackInfo;
import android.graphics.Bitmap;
import android.os.Parcel;
......@@ -30,8 +33,8 @@ import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
public class Media implements Parcelable {
public final static String TAG = "VLC/LibVLC/Media";
public class MediaWrapper implements Parcelable {
public final static String TAG = "VLC/MediaWrapper";
public final static int TYPE_ALL = -1;
public final static int TYPE_VIDEO = 0;
......@@ -100,7 +103,7 @@ public class Media implements Parcelable {
* @param libVLC A pointer to the libVLC instance. Should not be NULL
* @param URI The URI of the media.
*/
public Media(LibVLC libVLC, String URI) {
public MediaWrapper(LibVLC libVLC, String URI) {
if(libVLC == null)
throw new NullPointerException("libVLC was null");
......@@ -187,7 +190,7 @@ public class Media implements Parcelable {
mTrackNumber = trackNumber;
}
public Media(String location, long time, long length, int type,
public MediaWrapper(String location, long time, long length, int type,
Bitmap picture, String title, String artist, String genre, String album, String albumArtist,
int width, int height, String artworkURL, int audio, int spu, int trackNumber) {
mLocation = location;
......@@ -195,7 +198,7 @@ public class Media implements Parcelable {
width, height, artworkURL, audio, spu, trackNumber);
}
public Media(Parcel in) {
public MediaWrapper(Parcel in) {
mLocation = in.readString();
init(in.readLong(),
in.readLong(),
......@@ -279,7 +282,7 @@ public class Media implements Parcelable {
* Returns the raw picture object. Likely to be NULL in VLC for Android
* due to lazy-loading.
*
* Use {@link org.videolan.vlc.util.Bitmap#getPictureFromCache(Media)} instead.
* Use {@link org.videolan.vlc.util.Bitmap#getPictureFromCache(MediaWrapper)} instead.
*
* @return The raw picture or NULL
*/
......@@ -290,7 +293,7 @@ public class Media implements Parcelable {
/**
* Sets the raw picture object.
*
* In VLC for Android, use {@link org.videolan.vlc.MediaDatabase#setPicture(Media, Bitmap)} instead.
* In VLC for Android, use {@link org.videolan.vlc.MediaDatabase#setPicture(MediaWrapper, Bitmap)} instead.
*
* @param p
*/
......@@ -432,12 +435,12 @@ public class Media implements Parcelable {
dest.writeInt(getTrackNumber());
}
public static final Parcelable.Creator<Media> CREATOR = new Parcelable.Creator<Media>() {
public Media createFromParcel(Parcel in) {
return new Media(in);
public static final Parcelable.Creator<MediaWrapper> CREATOR = new Parcelable.Creator<MediaWrapper>() {
public MediaWrapper createFromParcel(Parcel in) {
return new MediaWrapper(in);
}
public Media[] newArray(int size) {
return new Media[size];
public MediaWrapper[] newArray(int size) {
return new MediaWrapper[size];
}
};
}
/*****************************************************************************
* MediaList.java
* MediaWrapperList.java
*****************************************************************************
* Copyright © 2013 VLC authors and VideoLAN
* Copyright © 2013-2015 VLC authors and VideoLAN
* Copyright © 2013 Edward Wang
*
* This program is free software; you can redistribute it and/or modify it
......@@ -18,31 +18,31 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.libvlc;
package org.videolan.vlc;
import java.util.ArrayList;
import org.videolan.libvlc.EventHandler;
import org.videolan.libvlc.LibVLC;
import android.os.Bundle;
/**
* Java/JNI wrapper for the libvlc_media_list_t structure.
*/
public class MediaList {
private static final String TAG = "VLC/LibVLC/MediaList";
public class MediaWrapperList {
private static final String TAG = "VLC/MediaWrapperList";
/* TODO: add locking */
private ArrayList<Media> mInternalList;
private ArrayList<MediaWrapper> mInternalList;
private LibVLC mLibVLC; // Used to create new objects that require a libvlc instance
private EventHandler mEventHandler;
public MediaList(LibVLC libVLC) {
public MediaWrapperList(LibVLC libVLC) {
mEventHandler = new EventHandler(); // used in init() below to fire events at the correct targets
mInternalList = new ArrayList<Media>();
mInternalList = new ArrayList<MediaWrapper>();
mLibVLC = libVLC;
}
public void add(Media media) {
public void add(MediaWrapper media) {
mInternalList.add(media);
}
......@@ -62,9 +62,9 @@ public class MediaList {
}
public void insert(int position, String mrl) {
insert(position, new Media(mLibVLC, mrl));
insert(position, new MediaWrapper(mLibVLC, mrl));
}
public void insert(int position, Media media) {
public void insert(int position, MediaWrapper media) {
mInternalList.add(position, media);
signal_list_event(EventHandler.CustomMediaListItemAdded, position, media.getLocation());
}
......@@ -81,7 +81,7 @@ public class MediaList {
&& endPosition >= 0 && endPosition <= mInternalList.size()))
throw new IndexOutOfBoundsException("Indexes out of range");
Media toMove = mInternalList.get(startPosition);
MediaWrapper toMove = mInternalList.get(startPosition);
mInternalList.remove(startPosition);
if (startPosition >= endPosition)
mInternalList.add(endPosition, toMove);
......@@ -116,7 +116,7 @@ public class MediaList {
return mInternalList.size();
}
public Media getMedia(int position) {
public MediaWrapper getMedia(int position) {
if (!isValid(position))
return null;
return mInternalList.get(position);
......
/*****************************************************************************
* MediaListPlayer.java
* MediaWrapperListPlayer.java
*****************************************************************************
* Copyright © 2015 VLC authors and VideoLAN
*
......@@ -18,23 +18,25 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.libvlc;
package org.videolan.vlc;
import java.util.ArrayList;
import org.videolan.libvlc.LibVLC;
public class MediaListPlayer {
public class MediaWrapperListPlayer {
private int mPlayerIndex = 0;
final private LibVLC mLibVLC;
final private MediaList mMediaList;
final private MediaWrapperList mMediaList;
public MediaListPlayer(LibVLC libVLC) {
public MediaWrapperListPlayer(LibVLC libVLC) {
mLibVLC = libVLC;
mMediaList = new MediaList(libVLC);
mMediaList = new MediaWrapperList(libVLC);
}
public MediaList getMediaList() {
public MediaWrapperList getMediaList() {
return mMediaList;
}
......@@ -48,7 +50,7 @@ public class MediaListPlayer {
if (mrl == null)
return;
final Media media = mMediaList.getMedia(position);
final MediaWrapper media = mMediaList.getMedia(position);
String[] options = mLibVLC.getMediaOptions(media != null ? media.getFlags() : 0);
mPlayerIndex = position;
mLibVLC.playMRL(mrl, options);
......
......@@ -31,7 +31,6 @@ import java.util.concurrent.locks.ReentrantLock;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcException;
import org.videolan.libvlc.Media;
import org.videolan.vlc.gui.video.VideoBrowserInterface;
import org.videolan.vlc.util.BitmapUtil;
import org.videolan.vlc.util.VLCInstance;
......@@ -48,7 +47,7 @@ public class Thumbnailer implements Runnable {
private VideoBrowserInterface mVideoBrowser;
private final Queue<Media> mItems = new LinkedList<Media>();
private final Queue<MediaWrapper> mItems = new LinkedList<MediaWrapper>();
private boolean isStopping = false;
private final Lock lock = new ReentrantLock();
......@@ -114,7 +113,7 @@ public class Thumbnailer implements Runnable {
* Add a new id of the file browser item to create its thumbnail.
* @param id the if of the file browser item.
*/
public void addJob(Media item) {
public void addJob(MediaWrapper item) {
if(BitmapUtil.getPictureFromCache(item) != null || item.isPictureParsed())
return;
lock.lock();
......@@ -160,7 +159,7 @@ public class Thumbnailer implements Runnable {
break;
}
total = totalCount;
Media item = mItems.poll();
MediaWrapper item = mItems.poll();
lock.unlock();
if (mVideoBrowser != null) {
......
......@@ -44,10 +44,10 @@ 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.libvlc.MediaList;
import org.videolan.libvlc.MediaListPlayer;
import org.videolan.vlc.MediaWrapper;
import org.videolan.vlc.MediaDatabase;
import org.videolan.vlc.MediaWrapperList;
import org.videolan.vlc.MediaWrapperListPlayer;
import org.videolan.vlc.R;
import org.videolan.vlc.RemoteControlClientReceiver;
import org.videolan.vlc.VLCApplication;
......@@ -123,7 +123,7 @@ public class AudioService extends Service {
public static final int NEXT_ITEM = 3;
private LibVLC mLibVLC;
private MediaListPlayer mMediaListPlayer;
private MediaWrapperListPlayer mMediaListPlayer;
private HashMap<IAudioServiceCallback, Integer> mCallback;
private EventHandler mEventHandler;
private OnAudioFocusChangeListener audioFocusListener;
......@@ -170,7 +170,7 @@ public class AudioService extends Service {
} catch (LibVlcException e) {
e.printStackTrace();