Commit b6f0e73d authored by Edward Wang's avatar Edward Wang
Browse files

libvlcjni: factorize media list support

Eventually this allows for having multiple independent media lists
parent 1044f6a5
......@@ -3,7 +3,7 @@ include $(CLEAR_VARS)
LOCAL_MODULE := libvlcjni
LOCAL_SRC_FILES := libvlcjni.c libvlcjni-util.c libvlcjni-track.c aout.c vout.c libvlcjni-equalizer.c
LOCAL_SRC_FILES := libvlcjni.c libvlcjni-util.c libvlcjni-track.c libvlcjni-medialist.c aout.c vout.c libvlcjni-equalizer.c
LOCAL_SRC_FILES += thumbnailer.c pthread-condattr.c pthread-rwlocks.c pthread-once.c eventfd.c sem.c
LOCAL_SRC_FILES += pipe2.c
LOCAL_SRC_FILES += wchar/wcpcpy.c
......
/*****************************************************************************
* libvlcjni-medialist.c
*****************************************************************************
* Copyright © 2013 VLC authors and VideoLAN
* Copyright © 2013 Edward Wang
*
* 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
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <jni.h>
#include <vlc/vlc.h>
#include <vlc/libvlc_media_list.h>
#include "utils.h"
#define LOG_TAG "VLC/JNI/MediaList"
#include "log.h"
libvlc_media_list_t* getMediaList(JNIEnv *env, jobject thiz) {
return (libvlc_media_list_t*)(intptr_t)getLong(env, thiz, "mMediaListInstance");
}
jlong Java_org_videolan_libvlc_MediaList_init(JNIEnv *env, jobject thiz, jobject libvlcJava) {
libvlc_media_list_t* p_ml = libvlc_media_list_new((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"));
if(!p_ml) {
jclass exc = (*env)->FindClass(env, "org/videolan/libvlc/LibVlcException");
(*env)->ThrowNew(env, exc, "Unable to create LibVLC media list");
return (jlong)(intptr_t)NULL;
}
return (jlong)(intptr_t)p_ml;
}
void Java_org_videolan_libvlc_MediaList_nativeDestroy(JNIEnv *env, jobject thiz) {
libvlc_media_list_t* p_ml = getMediaList(env, thiz);
libvlc_media_list_release(p_ml);
}
void Java_org_videolan_libvlc_MediaList_remove(JNIEnv *env, jobject thiz, jint position) {
libvlc_media_list_t* p_ml = getMediaList(env, thiz);
libvlc_media_list_lock(p_ml);
libvlc_media_list_remove_index(p_ml, position);
libvlc_media_list_unlock(p_ml);
}
void Java_org_videolan_libvlc_MediaList_add(JNIEnv *env, jobject thiz, jobject libvlcInstance, jstring mrl) {
libvlc_media_list_t* p_ml = getMediaList(env, thiz);
const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, NULL);
libvlc_media_t *p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)getLong(env, libvlcInstance, "mLibVlcInstance"), p_mrl);
libvlc_media_list_lock(p_ml);
libvlc_media_list_add_media(p_ml, p_md);
libvlc_media_list_unlock(p_ml);
libvlc_media_release(p_md);
(*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
}
void Java_org_videolan_libvlc_MediaList_insert(JNIEnv *env, jobject thiz, jobject libvlcInstance, jint position, jstring mrl) {
libvlc_media_list_t* p_ml = getMediaList(env, thiz);
const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, NULL);
libvlc_media_t *p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)getLong(env, libvlcInstance, "mLibVlcInstance"), p_mrl);
libvlc_media_list_lock(p_ml);
libvlc_media_list_insert_media(p_ml, p_md, position);
libvlc_media_list_unlock(p_ml);
libvlc_media_release(p_md);
(*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
}
jint Java_org_videolan_libvlc_MediaList_size(JNIEnv *env, jobject thiz) {
libvlc_media_list_t* p_ml = getMediaList(env, thiz);
libvlc_media_list_lock(p_ml);
int count = libvlc_media_list_count(p_ml);
libvlc_media_list_unlock(p_ml);
return count;
}
jstring Java_org_videolan_libvlc_MediaList_getMRL(JNIEnv *env, jobject thiz, jint position) {
libvlc_media_list_t* p_ml = getMediaList(env, thiz);
libvlc_media_list_lock(p_ml);
libvlc_media_t* p_md = libvlc_media_list_item_at_index(p_ml, position);
libvlc_media_list_unlock(p_ml);
if(p_md) {
char* p_mrl = libvlc_media_get_mrl(p_md);
libvlc_media_release(p_md);
return (*env)->NewStringUTF(env, p_mrl);
} else
return NULL;
}
......@@ -277,14 +277,6 @@ void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
libvlc_log_set(instance, debug_log, &verbosity);
/* Initialize media list (a.k.a. playlist/history) */
libvlc_media_list_t* pointer = libvlc_media_list_new( instance );
if(!pointer) {
jclass exc = (*env)->FindClass(env, "org/videolan/libvlc/LibVlcException");
(*env)->ThrowNew(env, exc, "Unable to create LibVLC media list");
return;
}
/* Connect the event manager */
libvlc_event_manager_t *ev = libvlc_media_list_event_manager(pointer);
static const libvlc_event_type_t mp_events[] = {
......@@ -293,8 +285,6 @@ void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
};
for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)
libvlc_event_attach(ev, mp_events[i], vlc_event_callback, myVm);
setLong(env, thiz, "mMediaListInstance", (jlong)(intptr_t)pointer);
}
void Java_org_videolan_libvlc_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
......@@ -464,10 +454,6 @@ void Java_org_videolan_libvlc_LibVLC_getMediaListItems(
libvlc_media_list_unlock( p_mlist );
}
void Java_org_videolan_libvlc_LibVLC_removeIndex(JNIEnv *env, jobject thiz, jlong instance, jint position) {
libvlc_media_list_remove_index((libvlc_media_list_t*)(intptr_t)instance, position);
}
jfloat Java_org_videolan_libvlc_LibVLC_getRate(JNIEnv *env, jobject thiz) {
libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
if(mp)
......
......@@ -41,8 +41,8 @@ public class LibVLC {
/** libvlc_media_player pointer and index */
private int mInternalMediaPlayerIndex = 0; // Read-only, reserved for JNI
private long mInternalMediaPlayerInstance = 0; // Read-only, reserved for JNI
/** libvlc_media_list_t pointer */
private long mMediaListInstance = 0; // Read-only, reserved for JNI
private MediaList mMediaList;
/** Buffer for VLC messages */
private StringBuffer mDebugLogBuffer;
......@@ -281,6 +281,7 @@ public class LibVLC {
throw new LibVlcException();
}
nativeInit();
mMediaList = new MediaList(this);
setEventHandler(EventHandler.getInstance());
mIsInitialized = true;
}
......@@ -412,12 +413,6 @@ public class LibVLC {
return mIsBufferingLog;
}
/** Media list functions **/
public void removeIndex(int position) {
removeIndex(mMediaListInstance, position);
}
private native void removeIndex(long media_list_instance, int position);
/**
* Read a media
* @param instance: the instance of libVLC
......
/*****************************************************************************
* MediaList.java
*****************************************************************************
* Copyright © 2013 VLC authors and VideoLAN
* Copyright © 2013 Edward Wang
*
* 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
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* 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;
/**
* Java/JNI wrapper for the libvlc_media_list_t structure.
*/
public class MediaList {
private static final String TAG = "VLC/LibVLC/MediaList";
private long mMediaListInstance = 0; // Read-only, reserved for JNI
private LibVLC mLibVLC; // Used to create new objects that require a libvlc instance
private boolean destroyed = false;
public MediaList(LibVLC libVLC) {
mMediaListInstance = init(libVLC);
mLibVLC = libVLC;
}
private native long init(LibVLC libvlc_instance);
@Override
public void finalize() {
if(!destroyed) destroy();
}
/**
* Releases the media list.
*
* The object should be considered released after this and must not be used.
*/
public void destroy() {
nativeDestroy();
mMediaListInstance = 0;
mLibVLC = null;
destroyed = true;
}
private native void nativeDestroy();
public void add(String mrl) {
add(mLibVLC, mrl);
}
private native void add(LibVLC libvlc_instance, String mrl);
public void insert(int position, String mrl) {
insert(mLibVLC, position, mrl);
}
private native void insert(LibVLC libvlc_instance, int position, String mrl);
public native void remove(int position);
public native int size();
/**
* @param position The index of the media in the list
* @return null if not found
*/
public native String getMRL(int position);
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("LibVLC Media List: {");
for(int i = 0; i < size(); i++) {
sb.append(((Integer)i).toString());
sb.append(": ");
sb.append(getMRL(i));
sb.append(", ");
}
sb.append("}");
return sb.toString();
}
}
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