[Android] [RFC] jni: maintain libvlc options from java side

Zhang Rui bbcallen at gmail.com
Wed Jul 10 10:55:32 CEST 2013


---
 vlc-android/jni/libvlcjni.c                     | 76 ++++++++++++-------------
 vlc-android/src/org/videolan/libvlc/LibVLC.java | 27 ++++++++-
 2 files changed, 60 insertions(+), 43 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index a651a43..8ef58e8 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -478,57 +478,53 @@ void Java_org_videolan_libvlc_LibVLC_stopDebugBuffer(JNIEnv *env, jobject thiz)
     (*env)->DeleteLocalRef(env, libvlcj);
 }
 
-void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
+void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz, jarray arguments)
 {
-    //only use OpenSLES if java side says we can
-    jclass cls = (*env)->GetObjectClass(env, thiz);
-    jmethodID methodId = (*env)->GetMethodID(env, cls, "getAout", "()I");
-    bool use_opensles = (*env)->CallIntMethod(env, thiz, methodId) == AOUT_OPENSLES;
-
-    methodId = (*env)->GetMethodID(env, cls, "timeStretchingEnabled", "()Z");
-    bool enable_time_stretch = (*env)->CallBooleanMethod(env, thiz, methodId);
+    jmethodID methodId;
+    int       i     = 0;
+    int       argc  = (*env)->GetArrayLength(env, arguments);
+    jstring   jarg  = NULL;
+    char     *carg  = NULL;
+    jstring  *jargv = (jstring *) calloc(argc, sizeof(jstring));
+    char    **cargv = (char **)   calloc(argc, sizeof(char *));
 
-    methodId = (*env)->GetMethodID(env, cls, "getDeblocking", "()I");
-    int deblocking = (*env)->CallIntMethod(env, thiz, methodId);
-    char deblockstr[2] = "3";
-    snprintf(deblockstr, 2, "%d", deblocking);
-    LOGD("Using deblocking level %d", deblocking);
+    jclass cls = (*env)->GetObjectClass(env, thiz);
 
-    methodId = (*env)->GetMethodID(env, cls, "getChroma", "()Ljava/lang/String;");
-    jstring chroma = (*env)->CallObjectMethod(env, thiz, methodId);
-    const char *chromastr = (*env)->GetStringUTFChars(env, chroma, 0);
-    LOGD("Chroma set to \"%s\"", chromastr);
+    for (i = 0; i < argc; i++) {
+        jargv[i] = (*env)->GetObjectArrayElement(env, arguments, i);
+        if (!jargv[i]) {
+            LOGE("nativeInit: invalid arg %d", i);
+            cargv[i] = NULL;
+            continue;
+        }
 
-    methodId = (*env)->GetMethodID(env, cls, "getSubtitlesEncoding", "()Ljava/lang/String;");
-    jstring subsencoding = (*env)->CallObjectMethod(env, thiz, methodId);
-    const char *subsencodingstr = (*env)->GetStringUTFChars(env, subsencoding, 0);
-    LOGD("Subtitle encoding set to \"%s\"", subsencodingstr);
+        cargv[i] = (char *) (*env)->GetStringUTFChars(env, jargv[i], NULL);
+        if (!cargv[i]) {
+            LOGE("nativeInit: null arg %d", i);
+            (*env)->DeleteLocalRef(env, jargv[i]);
+            jargv[i] = NULL;
+            continue;
+        }
+    }
 
+    jclass cls = (*env)->GetObjectClass(env, thiz);
     methodId = (*env)->GetMethodID(env, cls, "isVerboseMode", "()Z");
     verbosity = (*env)->CallBooleanMethod(env, thiz, methodId);
 
-    /* Don't add any invalid options, otherwise it causes LibVLC to crash */
-    const char *argv[] = {
-        "-I", "dummy",
-        "--no-osd",
-        "--no-video-title-show",
-        "--no-stats",
-        "--no-plugins-cache",
-        "--no-drop-late-frames",
-        "--avcodec-fast",
-        "--avcodec-threads=0",
-        "--subsdec-encoding", subsencodingstr,
-        enable_time_stretch ? "--audio-time-stretch" : "--no-audio-time-stretch",
-        "--avcodec-skiploopfilter", deblockstr,
-        use_opensles ? "--aout=opensles" : "--aout=android_audiotrack",
-        "--androidsurface-chroma", chromastr != NULL && chromastr[0] != 0 ? chromastr : "RV32",
-    };
-    libvlc_instance_t *instance = libvlc_new(sizeof(argv) / sizeof(*argv), argv);
+    libvlc_instance_t *instance = libvlc_new(argc, (const char *const *) cargv);
 
     setLong(env, thiz, "mLibVlcInstance", (jlong)(intptr_t) instance);
 
-    (*env)->ReleaseStringUTFChars(env, chroma, chromastr);
-    (*env)->ReleaseStringUTFChars(env, subsencoding, subsencodingstr);
+    for (i = 0; i < argc; ++i) {
+        if (jargv[i]) {
+            if (cargv[i])
+                (*env)->ReleaseStringUTFChars(env, jargv[i], cargv[i]);
+            (*env)->DeleteLocalRef(env, jargv[i]);
+        }
+    }
+
+    free(cargv);
+    free(jargv);
 
     if (!instance)
     {
diff --git a/vlc-android/src/org/videolan/libvlc/LibVLC.java b/vlc-android/src/org/videolan/libvlc/LibVLC.java
index a8bd9ff..86bd1fd 100644
--- a/vlc-android/src/org/videolan/libvlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/libvlc/LibVLC.java
@@ -25,6 +25,7 @@ import java.util.Map;
 
 import android.content.Context;
 import android.os.Build;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Surface;
 
@@ -228,7 +229,7 @@ public class LibVLC {
     }
 
     public String getChroma() {
-        return chroma;
+        return TextUtils.isEmpty(chroma) ? "RV32" : chroma;
     }
 
     public void setChroma(String chroma) {
@@ -254,7 +255,27 @@ public class LibVLC {
                 Log.e(TAG, LibVlcUtil.getErrorMsg());
                 throw new LibVlcException();
             }
-            nativeInit();
+
+            ArrayList<String> arguments = new ArrayList<String>();
+            arguments.add("-I");
+            arguments.add("dummy");
+            arguments.add("--no-osd");
+            arguments.add("--no-video-title-show");
+            arguments.add("--no-stats");
+            arguments.add("--no-plugins-cache");
+            arguments.add("--no-drop-late-frames");
+            arguments.add("--avcodec-fast");
+            arguments.add("--avcodec-threads=0");
+            arguments.add("--subsdec-encoding");
+            arguments.add(getSubtitlesEncoding());
+            arguments.add(timeStretchingEnabled() ? "--audio-time-stretch" : "--no-audio-time-stretch");
+            arguments.add("--avcodec-skiploopfilter");
+            arguments.add(String.valueOf(getDeblocking()));
+            arguments.add(getAout() == AOUT_OPENSLES ? "--aout=opensles" : "--aout=android_audiotrack");
+            arguments.add("--androidsurface-chroma");
+            arguments.add(getChroma());
+
+            nativeInit(arguments.toArray(new String[arguments.size()]));
             setEventHandler(EventHandler.getInstance());
             mIsInitialized = true;
         }
@@ -372,7 +393,7 @@ public class LibVLC {
      * Initialize the libvlc C library
      * @return a pointer to the libvlc instance
      */
-    private native void nativeInit() throws LibVlcException;
+    private native void nativeInit(String arguments[]) throws LibVlcException;
 
     /**
      * Close the libvlc C library
-- 
1.8.2.1



More information about the Android mailing list