[Android] LibVLC: setup HOME env variable from an Android Context

Thomas Guillem git at videolan.org
Sat Jul 16 19:47:58 CEST 2016


vlc-android | branch: 2.0.x | Thomas Guillem <thomas at gllm.fr> | Tue Jul 12 09:44:47 2016 +0200| [910d5abdc192c8e374348ba3e8adcf322b7239a3] | committer: Jean-Baptiste Kempf

LibVLC: setup HOME env variable from an Android Context

(cherry picked from commit 4f6f5e0180a6d595a03c35824d8cff7106def9d1)
Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> https://code.videolan.org/videolan/vlc-android/commit/910d5abdc192c8e374348ba3e8adcf322b7239a3
---

 libvlc/jni/libvlcjni.c                                 | 14 +++++++++++++-
 libvlc/src/org/videolan/libvlc/LibVLC.java             | 13 +++++++------
 libvlc/src/org/videolan/libvlc/media/MediaPlayer.java  |  2 +-
 libvlc/src/org/videolan/libvlc/media/VideoView.java    |  7 +------
 libvlc/src/org/videolan/libvlc/util/Dumper.java        |  2 +-
 vlc-android/src/org/videolan/vlc/util/VLCInstance.java |  4 ++--
 6 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index cdf8782..fe64c1d 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -356,7 +356,9 @@ void JNI_OnUnload(JavaVM* vm, void* reserved)
 #endif
 }
 
-void Java_org_videolan_libvlc_LibVLC_nativeNew(JNIEnv *env, jobject thiz, jobjectArray jstringArray)
+void Java_org_videolan_libvlc_LibVLC_nativeNew(JNIEnv *env, jobject thiz,
+                                               jobjectArray jstringArray,
+                                               jstring jhomePath)
 {
     vlcjni_object *p_obj = NULL;
     libvlc_instance_t *p_libvlc = NULL;
@@ -364,6 +366,16 @@ void Java_org_videolan_libvlc_LibVLC_nativeNew(JNIEnv *env, jobject thiz, jobjec
     const char **argv = NULL;
     int argc = 0;
 
+    if (jhomePath)
+    {
+        const char *psz_home = (*env)->GetStringUTFChars(env, jhomePath, 0);
+        if (psz_home)
+        {
+            setenv("HOME", psz_home, 1);
+            (*env)->ReleaseStringUTFChars(env, jhomePath, psz_home);
+        }
+    }
+
     if (jstringArray)
     {
         argc = (*env)->GetArrayLength(env, jstringArray);
diff --git a/libvlc/src/org/videolan/libvlc/LibVLC.java b/libvlc/src/org/videolan/libvlc/LibVLC.java
index 7fc1a87..10c9320 100644
--- a/libvlc/src/org/videolan/libvlc/LibVLC.java
+++ b/libvlc/src/org/videolan/libvlc/LibVLC.java
@@ -20,6 +20,7 @@
 
 package org.videolan.libvlc;
 
+import android.content.Context;
 import android.os.Build;
 import android.util.Log;
 
@@ -45,7 +46,7 @@ public class LibVLC extends VLCObject<LibVLC.Event> {
      *
      * @param options
      */
-    public LibVLC(ArrayList<String> options) {
+    public LibVLC(Context context, ArrayList<String> options) {
         loadLibraries();
 
         boolean setAout = true, setChroma = true;
@@ -78,14 +79,14 @@ public class LibVLC extends VLCObject<LibVLC.Event> {
             }
         }
 
-        nativeNew(options.toArray(new String[options.size()]));
+        nativeNew(options.toArray(new String[options.size()]), context.getDir("vlc", Context.MODE_PRIVATE).getAbsolutePath());
     }
 
     /**
      * Create a LibVLC
      */
-    public LibVLC() {
-        this(null);
+    public LibVLC(Context context) {
+        this(context, null);
     }
 
     /**
@@ -141,10 +142,10 @@ public class LibVLC extends VLCObject<LibVLC.Event> {
     }
 
     /* JNI */
-    private native void nativeNew(String[] options);
+    private native void nativeNew(String[] options, String homePath);
     private native void nativeRelease();
     private native void nativeSetUserAgent(String name, String http);
-    
+
     private static boolean sLoaded = false;
 
     static synchronized void loadLibraries() {
diff --git a/libvlc/src/org/videolan/libvlc/media/MediaPlayer.java b/libvlc/src/org/videolan/libvlc/media/MediaPlayer.java
index a42ec29..88a671b 100644
--- a/libvlc/src/org/videolan/libvlc/media/MediaPlayer.java
+++ b/libvlc/src/org/videolan/libvlc/media/MediaPlayer.java
@@ -72,7 +72,7 @@ public class MediaPlayer
     private org.videolan.libvlc.MediaPlayer mMediaPlayer;
 
     public MediaPlayer() {
-        mLibVLC = new LibVLC(); //FIXME, this is wrong
+        mLibVLC = new LibVLC(null); //FIXME, this is wrong
         mMediaPlayer = new org.videolan.libvlc.MediaPlayer(mLibVLC);
     }
 
diff --git a/libvlc/src/org/videolan/libvlc/media/VideoView.java b/libvlc/src/org/videolan/libvlc/media/VideoView.java
index e3054ab..6a16bca 100644
--- a/libvlc/src/org/videolan/libvlc/media/VideoView.java
+++ b/libvlc/src/org/videolan/libvlc/media/VideoView.java
@@ -55,6 +55,7 @@ public class VideoView extends SurfaceView
 
     public VideoView(Context context) {
         super(context);
+        sLibVLC = new LibVLC(context, null);
     }
 
     public VideoView(Context context, AttributeSet attrs) {
@@ -70,10 +71,6 @@ public class VideoView extends SurfaceView
         super(context, attrs, defStyleAttr, defStyleRes);
     }
 
-    private void initLibVLC() {
-        sLibVLC = new LibVLC();
-    }
-
     @Override
     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
@@ -91,12 +88,10 @@ public class VideoView extends SurfaceView
     }
 
     public void setVideoPath(String path) {
-        initLibVLC();
         final Media media = new Media(sLibVLC, path);
     }
 
     public void setVideoURI(Uri uri) {
-        initLibVLC();
         final Media media = new Media(sLibVLC, uri);
     }
 
diff --git a/libvlc/src/org/videolan/libvlc/util/Dumper.java b/libvlc/src/org/videolan/libvlc/util/Dumper.java
index 56fc6bc..3bff77f 100644
--- a/libvlc/src/org/videolan/libvlc/util/Dumper.java
+++ b/libvlc/src/org/videolan/libvlc/util/Dumper.java
@@ -60,7 +60,7 @@ public class Dumper {
         options.add("--no-audio");
         options.add("--no-spu");
         options.add("-vvv");
-        mLibVLC = new LibVLC(options);
+        mLibVLC = new LibVLC(null, options);
 
         final Media media = new Media(mLibVLC, uri);
         mMediaPlayer = new MediaPlayer(media);
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
index 2766bc6..140720e 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
+++ b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
@@ -111,7 +111,7 @@ public class VLCInstance {
                 linkCompatLib(context);
             }
 
-            sLibVLC = new LibVLC(VLCOptions.getLibOptions());
+            sLibVLC = new LibVLC(context, VLCOptions.getLibOptions());
             LibVLC.setOnNativeCrashListener(new LibVLC.OnNativeCrashListener() {
                 @Override
                 public void onNativeCrash() {
@@ -130,7 +130,7 @@ public class VLCInstance {
     public static synchronized void restart() throws IllegalStateException {
         if (sLibVLC != null) {
             sLibVLC.release();
-            sLibVLC = new LibVLC(VLCOptions.getLibOptions());
+            sLibVLC = new LibVLC(VLCApplication.getAppContext(), VLCOptions.getLibOptions());
         }
     }
 



More information about the Android mailing list