Commit 3681b743 authored by Thomas Guillem's avatar Thomas Guillem

native_crash_handler: set it static

parent f0ee101b
......@@ -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)
......
......@@ -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);
}
......@@ -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
......@@ -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;
......@@ -31,6 +35,10 @@ struct fields {
struct {
jclass clazz;
} String;
struct {
jclass clazz;
jmethodID onNativeCrashID;
} LibVLC;
struct {
jclass clazz;
jfieldID mInstanceID;
......
......@@ -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() {
......
......@@ -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);
......
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