[Android] native_crash_handler: set it static

Thomas Guillem git at videolan.org
Tue Feb 10 13:36:02 CET 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Tue Feb 10 13:32:27 2015 +0100| [3681b74383c9c633226dcb84f58788b85e9fdab5] | committer: Thomas Guillem

native_crash_handler: set it static

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=3681b74383c9c633226dcb84f58788b85e9fdab5
---

 libvlc/jni/libvlcjni.c                             |   15 +++++++---
 libvlc/jni/native_crash_handler.c                  |   29 +++++++++++---------
 libvlc/jni/native_crash_handler.h                  |    4 +--
 libvlc/jni/utils.h                                 |    8 ++++++
 libvlc/src/org/videolan/libvlc/LibVLC.java         |   12 ++++----
 .../src/org/videolan/vlc/util/VLCInstance.java     |    2 +-
 6 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index 965a0f9..b02ca39 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -251,6 +251,8 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
               "java/lang/IllegalArgumentException");
     GET_CLASS(fields.String.clazz,
               "java/lang/String");
+    GET_CLASS(fields.LibVLC.clazz,
+              "org/videolan/libvlc/LibVLC");
     GET_CLASS(fields.VLCObject.clazz,
               "org/videolan/libvlc/VLCObject");
     GET_CLASS(fields.Media.clazz,
@@ -258,6 +260,11 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
     GET_CLASS(fields.Media.Track.clazz,
               "org/videolan/libvlc/Media$Track");
 
+    GET_ID(GetStaticMethodID,
+           fields.LibVLC.onNativeCrashID,
+           fields.LibVLC.clazz,
+           "onNativeCrash", "()V");
+
     GET_ID(GetFieldID,
            fields.VLCObject.mInstanceID,
            fields.VLCObject.clazz,
@@ -292,6 +299,8 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
 #undef GET_CLASS
 #undef GET_ID
 
+    init_native_crash_handler();
+
     LOGD("JNI interface loaded.");
     return VLC_JNI_VERSION;
 }
@@ -303,6 +312,8 @@ void JNI_OnUnload(JavaVM* vm, void* reserved)
     pthread_mutex_destroy(&vout_android_lock);
     pthread_cond_destroy(&vout_android_surf_attached);
 
+    destroy_native_crash_handler();
+
     if ((*vm)->GetEnv(vm, (void**) &env, VLC_JNI_VERSION) != JNI_OK)
         return;
 
@@ -457,14 +468,10 @@ void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
     }
 
     LOGI("LibVLC initialized: %p", instance);
-
-    init_native_crash_handler(env, thiz);
 }
 
 void Java_org_videolan_libvlc_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
 {
-    destroy_native_crash_handler(env);
-
     releaseMediaPlayer(env, thiz);
     jlong libVlcInstance = getLong(env, thiz, "mLibVlcInstance");
     if (!libVlcInstance)
diff --git a/libvlc/jni/native_crash_handler.c b/libvlc/jni/native_crash_handler.c
index 2cb1590..d05557b 100644
--- a/libvlc/jni/native_crash_handler.c
+++ b/libvlc/jni/native_crash_handler.c
@@ -18,16 +18,18 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#include <stdbool.h>
 #include <signal.h>
 
 #include "native_crash_handler.h"
+#include "utils.h"
 
 static struct sigaction old_actions[NSIG];
-static jobject j_libVLC;
 
 #define THREAD_NAME "native_crash_handler"
 extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
 extern void jni_detach_thread();
+extern int jni_get_env(JNIEnv **env);
 
 // Monitored signals.
 static const int monitored_signals[] = {
@@ -50,25 +52,28 @@ static const int monitored_signals[] = {
  */
 void sigaction_callback(int signal, siginfo_t *info, void *reserved)
 {
-    // Call the Java LibVLC method that handle the crash.
     JNIEnv *env;
-    jni_attach_thread(&env, THREAD_NAME);
+    bool b_attached = false;
 
-    jclass cls = (*env)->GetObjectClass(env, j_libVLC);
-    jmethodID methodId = (*env)->GetMethodID(env, cls, "onNativeCrash", "()V");
-    (*env)->CallVoidMethod(env, j_libVLC, methodId);
+    if (jni_get_env(&env) < 0) {
+        if (jni_attach_thread(&env, THREAD_NAME) < 0)
+            return;
+        b_attached = true;
+    }
 
-    (*env)->DeleteLocalRef(env, cls);
-    jni_detach_thread();
+    // Call the Java LibVLC method that handle the crash.
+    (*env)->CallStaticVoidMethod(env, fields.LibVLC.clazz,
+                                 fields.LibVLC.onNativeCrashID);
 
     // Call the old signal handler.
     old_actions[signal].sa_handler(signal);
+    if (b_attached)
+        jni_detach_thread();
 }
 
 
-void init_native_crash_handler(JNIEnv *env, jobject j_libVLC_local)
+void init_native_crash_handler()
 {
-    j_libVLC = (*env)->NewGlobalRef(env, j_libVLC_local);
     struct sigaction handler;
     memset(&handler, 0, sizeof(struct sigaction));
 
@@ -84,7 +89,7 @@ void init_native_crash_handler(JNIEnv *env, jobject j_libVLC_local)
 }
 
 
-void destroy_native_crash_handler(JNIEnv *env)
+void destroy_native_crash_handler()
 {
     // Uninstall the signal handlers and restore their old actions.
     for (unsigned i = 0; i < sizeof(monitored_signals) / sizeof(int); ++i)
@@ -92,6 +97,4 @@ void destroy_native_crash_handler(JNIEnv *env)
         const int s = monitored_signals[i];
         sigaction(s, &old_actions[s], NULL);
     }
-
-    (*env)->DeleteGlobalRef(env, j_libVLC);
 }
diff --git a/libvlc/jni/native_crash_handler.h b/libvlc/jni/native_crash_handler.h
index a57e61e..5ec998e 100644
--- a/libvlc/jni/native_crash_handler.h
+++ b/libvlc/jni/native_crash_handler.h
@@ -23,7 +23,7 @@
 
 #include <jni.h>
 
-void init_native_crash_handler(JNIEnv *env, jobject j_libVLC_local);
-void destroy_native_crash_handler(JNIEnv *env);
+void init_native_crash_handler();
+void destroy_native_crash_handler();
 
 #endif // LIBVLCJNI_NATIVE_CRASH_HANDLER_H
diff --git a/libvlc/jni/utils.h b/libvlc/jni/utils.h
index 3c28e88..332f9af 100644
--- a/libvlc/jni/utils.h
+++ b/libvlc/jni/utils.h
@@ -21,6 +21,10 @@
 #ifndef LIBVLCJNI_UTILS_H
 #define LIBVLCJNI_UTILS_H
 
+#include <vlc/vlc.h>
+#include <vlc/libvlc_media.h>
+#include <vlc/libvlc_media_list.h>
+
 struct fields {
     struct {
         jclass clazz;
@@ -33,6 +37,10 @@ struct fields {
     } String;
     struct {
         jclass clazz;
+        jmethodID onNativeCrashID;
+    } LibVLC;
+    struct {
+        jclass clazz;
         jfieldID mInstanceID;
         jmethodID dispatchEventFromNativeID;
     } VLCObject;
diff --git a/libvlc/src/org/videolan/libvlc/LibVLC.java b/libvlc/src/org/videolan/libvlc/LibVLC.java
index b736d7b..b9cf4ae 100644
--- a/libvlc/src/org/videolan/libvlc/LibVLC.java
+++ b/libvlc/src/org/videolan/libvlc/LibVLC.java
@@ -92,7 +92,7 @@ public class LibVLC {
     private String mCachePath = "";
 
     /** Native crash handler */
-    private OnNativeCrashListener mOnNativeCrashListener;
+    private static OnNativeCrashListener sOnNativeCrashListener;
 
     /** Check in libVLC already initialized otherwise crash */
     private boolean mIsInitialized = false;
@@ -716,13 +716,13 @@ public class LibVLC {
         public void onNativeCrash();
     }
 
-    public void setOnNativeCrashListener(OnNativeCrashListener l) {
-        mOnNativeCrashListener = l;
+    public static void setOnNativeCrashListener(OnNativeCrashListener l) {
+        sOnNativeCrashListener = l;
     }
 
-    private void onNativeCrash() {
-        if (mOnNativeCrashListener != null)
-            mOnNativeCrashListener.onNativeCrash();
+    private static void onNativeCrash() {
+        if (sOnNativeCrashListener != null)
+            sOnNativeCrashListener.onNativeCrash();
     }
 
     public String getCachePath() {
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
index d0212c6..17032f8 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
+++ b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
@@ -45,7 +45,7 @@ public class VLCInstance {
             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
             VLCInstance.updateLibVlcSettings(pref);
             instance.init(context);
-            instance.setOnNativeCrashListener(new LibVLC.OnNativeCrashListener() {
+            LibVLC.setOnNativeCrashListener(new LibVLC.OnNativeCrashListener() {
                 @Override
                 public void onNativeCrash() {
                     Intent i = new Intent(context, NativeCrashActivity.class);



More information about the Android mailing list