Commit 3a30e205 authored by Rafaël Carré's avatar Rafaël Carré

Add a new utility function to create a libvlc media

- Declare it in shared utils.h for thumbnailer.c
- Fix a memleak related to jstring->string in getLengthFromFile()
- Create a java helper to get iomx preference and use it

Code removed:
- add_media_codec_options() : merged into new function
- static bool : we fetch the preference directly
- No need to give iomx preference to nativeInit()
- No need to reinit libvlc on iomx preference change
- No need to monitor those changes
- No need for reinit() function

Notes:

We could add a new boolean field, continue to monitor preference changes
and update that field; then access this field from JNI instead of a method.

Since android works well with URIs we should use media_new_location()
(Need to check all code paths to this function)
parent 0c5e1205
......@@ -29,15 +29,24 @@
#include "libvlcjni.h"
#include "aout.h"
#include "utils.h"
#define LOG_TAG "VLC/JNI/main"
#include "log.h"
static bool iomx_enabled;
void add_media_codec_options(libvlc_media_t *p_md)
libvlc_media_t *new_media(jint instance, JNIEnv *env, jobject thiz, jstring filePath)
{
if (iomx_enabled) {
libvlc_instance_t *libvlc = (libvlc_instance_t*)instance;
jboolean isCopy;
const char *psz_path = (*env)->GetStringUTFChars(env, filePath, &isCopy);
libvlc_media_t *p_md = libvlc_media_new_path(libvlc, psz_path);
(*env)->ReleaseStringUTFChars(env, filePath, psz_path);
if (!p_md)
return NULL;
jclass cls = (*env)->GetObjectClass(env, thiz);
jmethodID methodId = (*env)->GetMethodID(env, cls, "useIOMX", "()Z");
if ((*env)->CallBooleanMethod(env, thiz, methodId)) {
/*
* Set higher caching values if using iomx decoding, since some omx
* decoders have a very high latency, and if the preroll data isn't
......@@ -51,6 +60,7 @@ void add_media_codec_options(libvlc_media_t *p_md)
libvlc_media_add_option(p_md, ":network-caching=1500");
libvlc_media_add_option(p_md, ":codec=iomx,all");
}
return p_md;
}
static libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz)
......@@ -204,9 +214,8 @@ void Java_org_videolan_vlc_android_LibVLC_detachSurface(JNIEnv *env, jobject thi
pthread_mutex_unlock(&vout_android_lock);
}
void Java_org_videolan_vlc_android_LibVLC_nativeInit(JNIEnv *env, jobject thiz, jboolean enable_iomx)
void Java_org_videolan_vlc_android_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
{
iomx_enabled = enable_iomx;
/* Don't add any invalid options, otherwise it causes LibVLC to crash */
static const char *argv[] = {
"-I", "dummy",
......@@ -305,17 +314,12 @@ jobjectArray Java_org_videolan_vlc_android_LibVLC_readMediaMeta(JNIEnv *env,
(*env)->FindClass(env, "java/lang/String"),
(*env)->NewStringUTF(env, ""));
jboolean isCopy;
const char *psz_mrl = (*env)->GetStringUTFChars(env, mrl, &isCopy);
libvlc_media_t *m = libvlc_media_new_path((libvlc_instance_t*)instance,
psz_mrl);
(*env)->ReleaseStringUTFChars(env, mrl, psz_mrl);
libvlc_media_t *m = new_media(instance, env, thiz, mrl);
if (!m)
{
LOGE("readMediaMeta: Couldn't create the media!");
return;
}
add_media_codec_options(m);
libvlc_media_parse(m);
......@@ -353,19 +357,13 @@ void Java_org_videolan_vlc_android_LibVLC_readMedia(JNIEnv *env, jobject thiz,
releaseMediaPlayer(env, thiz);
/* Create a new item */
jboolean isCopy;
const char *psz_mrl = (*env)->GetStringUTFChars(env, mrl, &isCopy);
libvlc_media_t *m = libvlc_media_new_path((libvlc_instance_t*)instance,
psz_mrl);
(*env)->ReleaseStringUTFChars(env, mrl, psz_mrl);
libvlc_media_t *m = new_media(instance, env, thiz, mrl);
if (!m)
{
LOGE("readMedia: Couldn't create the media!");
return;
}
add_media_codec_options(m);
/* Create a media player playing environment */
libvlc_media_player_t *mp = libvlc_media_player_new((libvlc_instance_t*)instance);
......@@ -407,21 +405,14 @@ void Java_org_videolan_vlc_android_LibVLC_readMedia(JNIEnv *env, jobject thiz,
jboolean Java_org_videolan_vlc_android_LibVLC_hasVideoTrack(JNIEnv *env, jobject thiz,
jint i_instance, jstring filePath)
{
libvlc_instance_t *p_instance = (libvlc_instance_t *)i_instance;
const char *psz_filePath = (*env)->GetStringUTFChars(env, filePath, 0);
/* Create a new item and assign it to the media player. */
libvlc_media_t *p_m = libvlc_media_new_path(p_instance, psz_filePath);
(*env)->ReleaseStringUTFChars(env, filePath, psz_filePath);
libvlc_media_t *p_m = new_media(i_instance, env, thiz, filePath);
if (p_m == NULL)
{
LOGE("Couldn't create the media!");
return JNI_FALSE;
}
add_media_codec_options(p_m);
/* Get the tracks information of the media. */
libvlc_media_track_info_t *p_tracks;
libvlc_media_parse(p_m);
......@@ -471,19 +462,14 @@ jlong Java_org_videolan_vlc_android_LibVLC_getLengthFromFile(JNIEnv *env, jobjec
pthread_cond_init(&monitor->doneCondVar, NULL);
monitor->length_changed = false;
libvlc_instance_t *p_instance = (libvlc_instance_t *)i_instance;
const char *psz_filePath = (*env)->GetStringUTFChars(env, filePath, 0);
/* Create a new item and assign it to the media player. */
libvlc_media_t *m = libvlc_media_new_path(p_instance, psz_filePath);
libvlc_media_t *m = new_media(i_instance, env, thiz, filePath);
if (m == NULL)
{
LOGE("Couldn't create the media to play!");
goto end;
}
add_media_codec_options(m);
/* Create a media player playing environment */
libvlc_media_player_t *mp = libvlc_media_player_new_from_media (m);
libvlc_event_manager_t *ev = libvlc_media_player_event_manager(mp);
......
......@@ -27,12 +27,11 @@
#define LOG_TAG "VLC/JNI/thumbnailer"
#include "log.h"
#include "utils.h"
#define THUMBNAIL_POSITION 0.5
#define PIXEL_SIZE 4 /* RGBA */
extern void add_media_codec_options(libvlc_media_t *p_md);
typedef struct
{
libvlc_media_player_t *mp;
......@@ -135,18 +134,12 @@ jbyteArray Java_org_videolan_vlc_android_LibVLC_getThumbnail(JNIEnv *env, jobjec
/* Create a media player playing environment */
sys->mp = libvlc_media_player_new(libvlc);
/* Create a new item and assign it to the media player. */
jboolean isCopy;
const char *psz_filePath = (*env)->GetStringUTFChars(env, filePath,
&isCopy);
libvlc_media_t *m = libvlc_media_new_path(libvlc, psz_filePath);
(*env)->ReleaseStringUTFChars(env, filePath, psz_filePath);
libvlc_media_t *m = new_media(instance, env, thiz, filePath);
if (m == NULL)
{
LOGE("Couldn't create the media to play!");
goto end;
}
add_media_codec_options(m);
libvlc_media_add_option( m, ":no-audio" );
libvlc_media_player_set_media(sys->mp, m);
......
/*****************************************************************************
* utils.h
*****************************************************************************
* Copyright © 2012 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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.
*****************************************************************************/
#ifndef LIBVLCJNI_UTILS_H
#define LIBVLCJNI_UTILS_H
libvlc_media_t *new_media(jint libvlc, JNIEnv *env, jobject thiz, jstring filePath);
#endif // LIBVLCJNI_UTILS_H
......@@ -83,25 +83,6 @@ public class LibVLC {
/* First call */
sInstance = new LibVLC();
sInstance.init();
if (sListener == null && MainActivity.getInstance() != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.getInstance());
/* This needs to be stored in a local field, to avoid garbage collection, since
* the shared prefs only keep a weak reference to it. */
sListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
{
if (key.equals("enable_iomx")) {
try {
sInstance.reinit();
} catch (LibVlcException lve) {
Log.e(TAG, "Unable to reinit libvlc: " + lve);
}
}
}
};
prefs.registerOnSharedPreferenceChangeListener(sListener);
}
}
return sInstance;
......@@ -111,11 +92,6 @@ public class LibVLC {
return sInstance;
}
public void reinit() throws LibVlcException {
destroy();
init();
}
/**
* Constructor
* It is private because this class is a singleton.
......@@ -142,17 +118,24 @@ public class LibVLC {
*/
public native void setSurface(Surface f);
/**
*
*/
public boolean useIOMX() {
MainActivity activity = MainActivity.getInstance();
boolean useIomx = false;
if (activity != null)
useIomx = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("enable_iomx", false);
return useIomx;
}
/**
* Initialize the libVLC class
*/
private void init() throws LibVlcException {
Log.v(TAG, "Initializing LibVLC");
if (!mIsInitialized) {
MainActivity activity = MainActivity.getInstance();
boolean useIomx = false;
if (activity != null)
useIomx = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("enable_iomx", false);
nativeInit(useIomx);
nativeInit();
setEventManager(EventManager.getIntance());
mIsInitialized = true;
}
......@@ -232,7 +215,7 @@ public class LibVLC {
* Initialize the libvlc C library
* @return a pointer to the libvlc instance
*/
private native void nativeInit(boolean useIomx) throws LibVlcException;
private native void nativeInit() throws LibVlcException;
/**
* Close the libvlc C library
......
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