<div dir="ltr">To use the <span style="font-family:arial,sans-serif;font-size:13px">NativeCrashHandler inside </span><span style="font-family:arial,sans-serif;font-size:13px">VLCApplication::onCreate I need to statically load the dynamic libraries, which now are loaded in LibVLC.java.</span><div><span style="font-family:arial,sans-serif;font-size:13px">Should I move this static block to </span><span style="font-family:arial,sans-serif;font-size:13px">NativeCrashHandler or to </span><span style="font-family:arial,sans-serif;font-size:13px">VLCApplication::onCreate as a normal init method?</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Thanks</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2014-12-01 7:23 GMT-02:00 Thomas Guillem <span dir="ltr"><<a href="mailto:thomas@gllm.fr" target="_blank">thomas@gllm.fr</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
<br>
On Sat, Nov 29, 2014, at 06:22, Paulo Vitor Magacho da Silva wrote:<br>
> ---<br>
> libvlc/jni/libvlcjni-equalizer.c | 4 +-<br>
> libvlc/jni/libvlcjni-util.c | 17 --<br>
> libvlc/jni/libvlcjni.c | 187<br>
> ++++++++++++---------<br>
> libvlc/jni/native_crash_handler.c | 119 +++++++++++--<br>
> libvlc/jni/native_crash_handler.h | 8 +-<br>
> libvlc/jni/utils.h | 2 -<br>
> libvlc/jni/vout.c | 186<br>
> +++++++++++---------<br>
> libvlc/jni/vout.h | 25 ++-<br>
> libvlc/src/org/videolan/libvlc/EventHandler.java | 8 -<br>
> libvlc/src/org/videolan/libvlc/LibVLC.java | 35 ++--<br>
> .../src/org/videolan/vlc/audio/AudioService.java | 14 +-<br>
> .../org/videolan/vlc/gui/PreferencesActivity.java | 2 +-<br>
> .../vlc/gui/video/VideoPlayerActivity.java | 9 +-<br>
> 13 files changed, 381 insertions(+), 235 deletions(-)<br>
><br>
> diff --git a/libvlc/jni/libvlcjni-equalizer.c<br>
> b/libvlc/jni/libvlcjni-equalizer.c<br>
> index 2dad0e4..d11f24d 100644<br>
> --- a/libvlc/jni/libvlcjni-equalizer.c<br>
> +++ b/libvlc/jni/libvlcjni-equalizer.c<br>
> @@ -89,10 +89,10 @@ jfloatArray<br>
> Java_org_videolan_libvlc_LibVLC_getPreset(JNIEnv *env, jobject thiz,<br>
> * apply equalizer settings (param bands is float[] (first element is<br>
> preamp, then bands))<br>
> */<br>
> //"--audio-filter=equalizer", "--equalizer-bands=-3.5 -4.5 -1 0 0 5 8 8<br>
> 8 8",<br>
> -jint Java_org_videolan_libvlc_LibVLC_setNativeEqualizer(JNIEnv *env,<br>
> jobject thiz, jlong media_player, jfloatArray bands)<br>
> +jint Java_org_videolan_libvlc_LibVLC_setNativeEqualizer(JNIEnv *env,<br>
> jobject thiz, jfloatArray bands)<br>
> {<br>
> jint res = -1;<br>
> - libvlc_media_player_t *mp =<br>
> (libvlc_media_player_t*)(intptr_t)media_player;<br>
> + libvlc_media_player_t *mp = getMediaPlayer(env, thiz);<br>
> if (!mp)<br>
> return res;<br>
><br>
> diff --git a/libvlc/jni/libvlcjni-util.c b/libvlc/jni/libvlcjni-util.c<br>
> index 205d59d..67c2554 100644<br>
> --- a/libvlc/jni/libvlcjni-util.c<br>
> +++ b/libvlc/jni/libvlcjni-util.c<br>
> @@ -131,23 +131,6 @@ void arrayListStringAdd(JNIEnv *env, jclass class,<br>
> jmethodID methodID, jobject a<br>
> (*env)->DeleteLocalRef(env, jstr);<br>
> }<br>
><br>
> -jobject getEventHandlerReference(JNIEnv *env, jobject thiz, jobject<br>
> eventHandler)<br>
> -{<br>
> - jclass cls = (*env)->GetObjectClass(env, eventHandler);<br>
> - if (!cls) {<br>
> - LOGE("setEventHandler: failed to get class reference");<br>
> - return NULL;<br>
> - }<br>
> -<br>
> - jmethodID methodID = (*env)->GetMethodID(env, cls, "callback",<br>
> "(ILandroid/os/Bundle;)V");<br>
> - if (!methodID) {<br>
> - LOGE("setEventHandler: failed to get the callback method");<br>
> - return NULL;<br>
> - }<br>
> -<br>
> - return (*env)->NewGlobalRef(env, eventHandler);<br>
> -}<br>
> -<br>
> static void debug_buffer_log(void *data, int level, const char *fmt,<br>
> va_list ap)<br>
> {<br>
> bool isAttached = false;<br>
> diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c<br>
> index b2c0cce..eaca4ca 100644<br>
> --- a/libvlc/jni/libvlcjni.c<br>
> +++ b/libvlc/jni/libvlcjni.c<br>
> @@ -72,6 +72,26 @@ static void add_media_options(libvlc_media_t *p_md,<br>
> JNIEnv *env, jobjectArray me<br>
> }<br>
> }<br>
><br>
> +static void setLibVlcSysInstance(JNIEnv *env, jobject thiz,<br>
> libvlc_instance_sys_t *p_sys)<br>
> +{<br>
> + setLong(env, thiz, "mLibVlcInstance", (jlong)(intptr_t) p_sys);<br>
> +}<br>
> +<br>
> +static libvlc_instance_sys_t *getLibVlcSysInstance(JNIEnv *env, jobject<br>
> thiz)<br>
> +{<br>
> + return (libvlc_instance_sys_t *)(intptr_t)getLong(env, thiz,<br>
> "mLibVlcInstance");<br>
> +}<br>
> +<br>
> +static void releaseMediaPlayer(JNIEnv *env, jobject thiz)<br>
> +{<br>
> + libvlc_media_player_t *p_mp = getMediaPlayer(env, thiz);<br>
> + if (p_mp)<br>
> + {<br>
> + libvlc_media_player_stop(p_mp);<br>
> + libvlc_media_player_release(p_mp);<br>
> + }<br>
> +}<br>
> +<br>
> libvlc_media_t *new_media(JNIEnv *env, jobject thiz, jstring<br>
> fileLocation, bool noOmx, bool noVideo)<br>
> {<br>
> libvlc_instance_t *libvlc = getLibVlcInstance(env, thiz);<br>
> @@ -98,24 +118,20 @@ libvlc_media_t *new_media(JNIEnv *env, jobject thiz,<br>
> jstring fileLocation, bool<br>
><br>
> libvlc_instance_t *getLibVlcInstance(JNIEnv *env, jobject thiz)<br>
> {<br>
> - return (libvlc_instance_t*)(intptr_t)getLong(env, thiz,<br>
> "mLibVlcInstance");<br>
> + libvlc_instance_sys_t *p_sys = getLibVlcSysInstance(env, thiz);<br>
> + return p_sys ? p_sys->p_libvlc : NULL;<br>
> }<br>
><br>
> -libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz)<br>
> +android_surf_value_t *getAndroidSurfaceInstance(JNIEnv *env, jobject<br>
> thiz)<br>
> {<br>
> - return (libvlc_media_player_t*)(intptr_t)getLong(env, thiz,<br>
> "mInternalMediaPlayerInstance");<br>
> + libvlc_instance_sys_t *p_sys = getLibVlcSysInstance(env, thiz);<br>
> + return p_sys ? p_sys->p_surf : NULL;<br>
> }<br>
><br>
> -<br>
> -static void releaseMediaPlayer(JNIEnv *env, jobject thiz)<br>
> +libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz)<br>
> {<br>
> - libvlc_media_player_t* p_mp = getMediaPlayer(env, thiz);<br>
> - if (p_mp)<br>
> - {<br>
> - libvlc_media_player_stop(p_mp);<br>
> - libvlc_media_player_release(p_mp);<br>
> - setLong(env, thiz, "mInternalMediaPlayerInstance", 0);<br>
> - }<br>
> + libvlc_instance_sys_t *p_sys = getLibVlcSysInstance(env, thiz);<br>
> + return p_sys ? p_sys->p_mp : NULL;<br>
> }<br>
><br>
> /* Pointer to the Java virtual machine<br>
> @@ -124,17 +140,12 @@ static void releaseMediaPlayer(JNIEnv *env, jobject<br>
> thiz)<br>
> */<br>
> static JavaVM *myVm;<br>
><br>
> -static jobject eventHandlerInstance = NULL;<br>
> -<br>
> static void vlc_event_callback(const libvlc_event_t *ev, void *data)<br>
> {<br>
> JNIEnv *env;<br>
><br>
> bool isAttached = false;<br>
><br>
> - if (eventHandlerInstance == NULL)<br>
> - return;<br>
> -<br>
> if (jni_get_env(&env) < 0) {<br>
> if (jni_attach_thread(&env, THREAD_NAME) < 0)<br>
> return;<br>
> @@ -193,20 +204,31 @@ static void vlc_event_callback(const libvlc_event_t<br>
> *ev, void *data)<br>
> free(mrl);<br>
> }<br>
><br>
> + /* The LibVLC object */<br>
> + jobject vlcObject = (jobject) data;<br>
> +<br>
> + /* Get the event handler object */<br>
> + jclass cls = (*env)->GetObjectClass(env, vlcObject);<br>
> + jmethodID methodId = (*env)->GetMethodID(env, cls,<br>
> "getEventHandler", "()Lorg/videolan/libvlc/EventHandler;");<br>
> + jobject eventHandlerInstance = (*env)->CallObjectMethod(env,<br>
> vlcObject, methodId);<br>
> +<br>
> + (*env)->DeleteLocalRef(env, cls);<br>
> +<br>
> /* Get the object class */<br>
> - jclass cls = (*env)->GetObjectClass(env, eventHandlerInstance);<br>
> + cls = (*env)->GetObjectClass(env, eventHandlerInstance);<br>
> if (!cls) {<br>
> LOGE("EventHandler: failed to get class reference");<br>
> goto end;<br>
> }<br>
><br>
> /* Find the callback ID */<br>
> - jmethodID methodID = (*env)->GetMethodID(env, cls, "callback",<br>
> "(ILandroid/os/Bundle;)V");<br>
> - if (methodID) {<br>
> - (*env)->CallVoidMethod(env, eventHandlerInstance, methodID,<br>
> ev->type, bundle);<br>
> + methodId = (*env)->GetMethodID(env, cls, "callback",<br>
> "(ILandroid/os/Bundle;)V");<br>
> + if (methodId) {<br>
> + (*env)->CallVoidMethod(env, eventHandlerInstance, methodId,<br>
> ev->type, bundle);<br>
> } else {<br>
> LOGE("EventHandler: failed to get the callback method");<br>
> }<br>
> + (*env)->DeleteLocalRef(env, cls);<br>
><br>
> end:<br>
> (*env)->DeleteLocalRef(env, bundle);<br>
> @@ -219,16 +241,16 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)<br>
> // Keep a reference on the Java VM.<br>
> myVm = vm;<br>
><br>
> - pthread_mutex_init(&vout_android_lock, NULL);<br>
> - pthread_cond_init(&vout_android_surf_attached, NULL);<br>
> + init_native_crash_handler();<br>
><br>
> LOGD("JNI interface loaded.");<br>
> return VLC_JNI_VERSION;<br>
> }<br>
><br>
> void JNI_OnUnload(JavaVM* vm, void* reserved) {<br>
> - pthread_mutex_destroy(&vout_android_lock);<br>
> - pthread_cond_destroy(&vout_android_surf_attached);<br>
> + destroy_native_crash_handler();<br>
> +<br>
> + LOGD("JNI interface un-loaded.");<br>
> }<br>
><br>
> int jni_attach_thread(JNIEnv **env, const char *thread_name)<br>
> @@ -353,8 +375,6 @@ void<br>
> Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)<br>
> };<br>
> libvlc_instance_t *instance = libvlc_new(sizeof(argv) /<br>
> sizeof(*argv), argv);<br>
><br>
> - setLong(env, thiz, "mLibVlcInstance", (jlong)(intptr_t) instance);<br>
> -<br>
> (*env)->ReleaseStringUTFChars(env, chroma, chromastr);<br>
> (*env)->ReleaseStringUTFChars(env, subsencoding, subsencodingstr);<br>
><br>
> @@ -364,45 +384,50 @@ void<br>
> Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)<br>
> (*env)->ThrowNew(env, exc, "Unable to instantiate LibVLC");<br>
> }<br>
><br>
> + /* Set values to libvlc sys instance */<br>
> + libvlc_instance_sys_t *p_sys = (libvlc_instance_sys_t *) calloc(1,<br>
> sizeof(libvlc_instance_sys_t));<br>
> + p_sys->p_libvlc = instance;<br>
> + p_sys->p_surf = (android_surf_value_t *) calloc(1,<br>
> sizeof(android_surf_value_t));<br>
> + p_sys->p_this = (*env)->NewGlobalRef(env, thiz);<br>
> +<br>
> + pthread_mutex_init(&p_sys->p_surf->vout_android_lock, NULL);<br>
> + pthread_cond_init(&p_sys->p_surf->vout_android_surf_attached, NULL);<br>
> +<br>
> + setLibVlcSysInstance(env, thiz, p_sys);<br>
> +<br>
> LOGI("LibVLC initialized: %p", instance);<br>
><br>
> libvlc_log_set(instance, debug_log, &verbosity);<br>
><br>
> - init_native_crash_handler(env, thiz);<br>
> + add_native_crash_handler(env, p_sys->p_this);<br>
> }<br>
><br>
> void Java_org_videolan_libvlc_LibVLC_nativeDestroy(JNIEnv *env, jobject<br>
> thiz)<br>
> {<br>
> - destroy_native_crash_handler(env);<br>
> + libvlc_instance_sys_t *p_sys = getLibVlcSysInstance(env, thiz);<br>
> +<br>
> + remove_native_crash_handler(env, p_sys->p_this);<br>
><br>
> releaseMediaPlayer(env, thiz);<br>
> - jlong libVlcInstance = getLong(env, thiz, "mLibVlcInstance");<br>
> - if (!libVlcInstance)<br>
> - return; // Already destroyed<br>
<br>
</div></div>this "Already destroyed" check should be done just after<br>
<span class="">libvlc_instance_sys_t *p_sys = getLibVlcSysInstance(env, thiz);<br>
> +<br>
<br>
><br>
</span><div><div class="h5">> - libvlc_instance_t *instance = (libvlc_instance_t*)(intptr_t)<br>
> libVlcInstance;<br>
> - libvlc_log_unset(instance);<br>
> - libvlc_release(instance);<br>
> + if (p_sys->p_this != NULL)<br>
> + (*env)->DeleteGlobalRef(env, p_sys->p_this);<br>
><br>
> - setLong(env, thiz, "mLibVlcInstance", 0);<br>
> -}<br>
> + pthread_mutex_destroy(&p_sys->p_surf->vout_android_lock);<br>
> + pthread_cond_destroy(&p_sys->p_surf->vout_android_surf_attached);<br>
><br>
> -void Java_org_videolan_libvlc_LibVLC_detachEventHandler(JNIEnv *env,<br>
> jobject thiz)<br>
> -{<br>
> - if (eventHandlerInstance != NULL) {<br>
> - (*env)->DeleteGlobalRef(env, eventHandlerInstance);<br>
> - eventHandlerInstance = NULL;<br>
> - }<br>
> -}<br>
> + free(p_sys->p_surf);<br>
><br>
> -void Java_org_videolan_libvlc_LibVLC_setEventHandler(JNIEnv *env,<br>
> jobject thiz, jobject eventHandler)<br>
> -{<br>
> - if (eventHandlerInstance != NULL) {<br>
> - (*env)->DeleteGlobalRef(env, eventHandlerInstance);<br>
> - eventHandlerInstance = NULL;<br>
> - }<br>
> + if (!p_sys->p_libvlc)<br>
> + return; // Already destroyed<br>
><br>
> - eventHandlerInstance = getEventHandlerReference(env, thiz,<br>
> eventHandler);<br>
> + libvlc_instance_t *instance = p_sys->p_libvlc;<br>
> + LOGI("LibVLC destroyed: %p", instance);<br>
> +<br>
> + libvlc_log_unset(instance);<br>
> + libvlc_release(instance);<br>
> +<br>
> + setLibVlcSysInstance(env, thiz, NULL);<br>
<br>
</div></div>you forgot to free p_sys<br>
<div><div class="h5"><br>
> }<br>
><br>
> void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz,<br>
> @@ -411,25 +436,24 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv<br>
> *env, jobject thiz,<br>
> /* Release previous media player, if any */<br>
> releaseMediaPlayer(env, thiz);<br>
><br>
> - libvlc_instance_t *p_instance = getLibVlcInstance(env, thiz);<br>
> + libvlc_instance_sys_t *p_sys = getLibVlcSysInstance(env, thiz);<br>
><br>
> /* Create a media player playing environment */<br>
> - libvlc_media_player_t *mp = libvlc_media_player_new(p_instance);<br>
> - libvlc_media_player_set_video_title_display(mp,<br>
> libvlc_position_disable, 0);<br>
> - jobject myJavaLibVLC = (*env)->NewGlobalRef(env, thiz);<br>
> + p_sys->p_mp = libvlc_media_player_new(p_sys->p_libvlc);<br>
> +<br>
> + libvlc_media_player_set_video_title_display(p_sys->p_mp,<br>
> libvlc_position_disable, 0);<br>
><br>
> //if AOUT_AUDIOTRACK_JAVA, we use amem<br>
> jclass cls = (*env)->GetObjectClass(env, thiz);<br>
> jmethodID methodId = (*env)->GetMethodID(env, cls, "getAout",<br>
> "()I");<br>
> if ( (*env)->CallIntMethod(env, thiz, methodId) ==<br>
> AOUT_AUDIOTRACK_JAVA )<br>
> {<br>
> - libvlc_audio_set_callbacks(mp, aout_play, aout_pause, NULL,<br>
> NULL, NULL,<br>
> - (void*) myJavaLibVLC);<br>
> - libvlc_audio_set_format_callbacks(mp, aout_open, aout_close);<br>
> + libvlc_audio_set_callbacks(p_sys->p_mp, aout_play, aout_pause,<br>
> NULL, NULL, NULL, p_sys->p_this);<br>
> + libvlc_audio_set_format_callbacks(p_sys->p_mp, aout_open,<br>
> aout_close);<br>
> }<br>
><br>
> /* Connect the event manager */<br>
> - libvlc_event_manager_t *ev = libvlc_media_player_event_manager(mp);<br>
> + libvlc_event_manager_t *ev =<br>
> libvlc_media_player_event_manager(p_sys->p_mp);<br>
> static const libvlc_event_type_t mp_events[] = {<br>
> libvlc_MediaPlayerPlaying,<br>
> libvlc_MediaPlayerPaused,<br>
> @@ -441,10 +465,7 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv<br>
> *env, jobject thiz,<br>
> libvlc_MediaPlayerEncounteredError<br>
> };<br>
> for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)<br>
> - libvlc_event_attach(ev, mp_events[i], vlc_event_callback, myVm);<br>
> -<br>
> - /* Keep a pointer to this media player */<br>
> - setLong(env, thiz, "mInternalMediaPlayerInstance",<br>
> (jlong)(intptr_t)mp);<br>
> + libvlc_event_attach(ev, mp_events[i], vlc_event_callback,<br>
> p_sys->p_this);<br>
><br>
> cls = (*env)->GetObjectClass(env, thiz);<br>
> jmethodID methodID = (*env)->GetMethodID(env, cls, "applyEqualizer",<br>
> "()V");<br>
> @@ -452,13 +473,16 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv<br>
> *env, jobject thiz,<br>
><br>
> const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, 0);<br>
><br>
> - libvlc_media_t* p_md = libvlc_media_new_location(p_instance, p_mrl);<br>
> + libvlc_media_t* p_md = libvlc_media_new_location(p_sys->p_libvlc,<br>
> p_mrl);<br>
> /* media options */<br>
> if (mediaOptions != NULL)<br>
> add_media_options(p_md, env, mediaOptions);<br>
><br>
> (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);<br>
><br>
> + /* set the Android surface value structure before playing */<br>
> + libvlc_media_player_set_surfacevalue(p_sys->p_mp, p_sys->p_surf);<br>
> +<br>
<br>
</div></div>again, I would call that function: libvlc_media_player_set_androidobject<br>
<div><div class="h5"><br>
> /* Connect the media event manager. */<br>
> libvlc_event_manager_t *ev_media = libvlc_media_event_manager(p_md);<br>
> static const libvlc_event_type_t mp_media_events[] = {<br>
> @@ -466,10 +490,10 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv<br>
> *env, jobject thiz,<br>
> libvlc_MediaMetaChanged,<br>
> };<br>
> for(int i = 0; i < (sizeof(mp_media_events) /<br>
> sizeof(*mp_media_events)); i++)<br>
> - libvlc_event_attach(ev_media, mp_media_events[i],<br>
> vlc_event_callback, myVm);<br>
> + libvlc_event_attach(ev_media, mp_media_events[i],<br>
> vlc_event_callback, p_sys->p_this);<br>
><br>
> - libvlc_media_player_set_media(mp, p_md);<br>
> - libvlc_media_player_play(mp);<br>
> + libvlc_media_player_set_media(p_sys->p_mp, p_md);<br>
> + libvlc_media_player_play(p_sys->p_mp);<br>
> }<br>
><br>
> jfloat Java_org_videolan_libvlc_LibVLC_getRate(JNIEnv *env, jobject<br>
> thiz) {<br>
> @@ -654,23 +678,26 @@ void<br>
> Java_org_videolan_libvlc_LibVLC_playerNavigate(JNIEnv *env, jobject thiz,<br>
> j<br>
> libvlc_media_player_navigate(mp, (unsigned) nav);<br>
> }<br>
><br>
> -// TODO: remove static variables<br>
> -static int i_window_width = 0;<br>
> -static int i_window_height = 0;<br>
> -<br>
> void Java_org_videolan_libvlc_LibVLC_setWindowSize(JNIEnv *env, jobject<br>
> thiz, jint width, jint height)<br>
> {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - i_window_width = width;<br>
> - i_window_height = height;<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + android_surf_value_t *android_surface =<br>
> getAndroidSurfaceInstance(env, thiz);<br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + android_surface->i_window_width = width;<br>
> + android_surface->i_window_height = height;<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> -int jni_GetWindowSize(int *width, int *height)<br>
> +int jni_GetWindowSize(android_surf_value_t *android_surface, int *width,<br>
> int *height)<br>
> {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - *width = i_window_width;<br>
> - *height = i_window_height;<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + *width = android_surface->i_window_width;<br>
> + *height = android_surface->i_window_height;<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return 0;<br>
> }<br>
> diff --git a/libvlc/jni/native_crash_handler.c<br>
> b/libvlc/jni/native_crash_handler.c<br>
> index 2cb1590..1a41c5b 100644<br>
> --- a/libvlc/jni/native_crash_handler.c<br>
> +++ b/libvlc/jni/native_crash_handler.c<br>
> @@ -19,11 +19,22 @@<br>
> *****************************************************************************/<br>
><br>
> #include <signal.h><br>
> +#include <pthread.h><br>
><br>
> #include "native_crash_handler.h"<br>
><br>
> +#define LOG_TAG "VLC/JNI/crash"<br>
> +#include "log.h"<br>
> +<br>
> +struct libvlc_crash_handler_t {<br>
> + void *j_libVLC;<br>
> + libvlc_crash_handler_t *next;<br>
> + libvlc_crash_handler_t *prev;<br>
> +};<br>
> +<br>
> static struct sigaction old_actions[NSIG];<br>
> -static jobject j_libVLC;<br>
> +static libvlc_crash_handler_t *first = NULL;<br>
> +static libvlc_crash_handler_t *last = NULL;<br>
><br>
> #define THREAD_NAME "native_crash_handler"<br>
> extern int jni_attach_thread(JNIEnv **env, const char *thread_name);<br>
> @@ -44,6 +55,7 @@ static const int monitored_signals[] = {<br>
> SIGPIPE<br>
> };<br>
><br>
> +pthread_mutex_t native_handler_lock;<br>
><br>
> /**<br>
> * Callback called when a monitored signal is triggered.<br>
> @@ -54,21 +66,32 @@ void sigaction_callback(int signal, siginfo_t *info,<br>
> void *reserved)<br>
> JNIEnv *env;<br>
> jni_attach_thread(&env, THREAD_NAME);<br>
><br>
> - jclass cls = (*env)->GetObjectClass(env, j_libVLC);<br>
> - jmethodID methodId = (*env)->GetMethodID(env, cls, "onNativeCrash",<br>
> "()V");<br>
> - (*env)->CallVoidMethod(env, j_libVLC, methodId);<br>
> + pthread_mutex_lock(&native_handler_lock);<br>
> +<br>
> + libvlc_crash_handler_t *lp = first;<br>
> + while (lp != NULL)<br>
> + {<br>
> + jclass cls = (*env)->GetObjectClass(env, lp->j_libVLC);<br>
> + jmethodID methodId = (*env)->GetMethodID(env, cls,<br>
> "onNativeCrash", "()V");<br>
> + (*env)->CallVoidMethod(env, lp->j_libVLC, methodId);<br>
> +<br>
> + (*env)->DeleteLocalRef(env, cls);<br>
> +<br>
> + libvlc_crash_handler_t *todelete = lp;<br>
> + lp = lp->next;<br>
> +<br>
> + free(todelete);<br>
> + }<br>
> + pthread_mutex_unlock(&native_handler_lock);<br>
><br>
> - (*env)->DeleteLocalRef(env, cls);<br>
> jni_detach_thread();<br>
><br>
> // Call the old signal handler.<br>
> old_actions[signal].sa_handler(signal);<br>
> }<br>
><br>
> -<br>
> -void init_native_crash_handler(JNIEnv *env, jobject j_libVLC_local)<br>
> +void init_native_crash_handler()<br>
> {<br>
> - j_libVLC = (*env)->NewGlobalRef(env, j_libVLC_local);<br>
> struct sigaction handler;<br>
> memset(&handler, 0, sizeof(struct sigaction));<br>
><br>
> @@ -81,10 +104,13 @@ void init_native_crash_handler(JNIEnv *env, jobject<br>
> j_libVLC_local)<br>
> const int s = monitored_signals[i];<br>
> sigaction(s, &handler, &old_actions[s]);<br>
> }<br>
> -}<br>
><br>
> + pthread_mutex_init(&native_handler_lock, NULL);<br>
> +<br>
> + LOGI("Native crash handler initialized");<br>
> +}<br>
><br>
> -void destroy_native_crash_handler(JNIEnv *env)<br>
> +void destroy_native_crash_handler()<br>
> {<br>
> // Uninstall the signal handlers and restore their old actions.<br>
> for (unsigned i = 0; i < sizeof(monitored_signals) / sizeof(int);<br>
> ++i)<br>
> @@ -93,5 +119,76 @@ void destroy_native_crash_handler(JNIEnv *env)<br>
> sigaction(s, &old_actions[s], NULL);<br>
> }<br>
><br>
> - (*env)->DeleteGlobalRef(env, j_libVLC);<br>
> + pthread_mutex_destroy(&native_handler_lock);<br>
> +<br>
> + LOGI("Native crash handler destroyed");<br>
> +}<br>
> +<br>
> +void add_native_crash_handler(JNIEnv *env, void *j_libVLC_local)<br>
> +{<br>
> + LOGI("Add crash handler to %p", j_libVLC_local);<br>
> +<br>
> + pthread_mutex_lock(&native_handler_lock);<br>
> +<br>
> + libvlc_crash_handler_t *current = (libvlc_crash_handler_t *)<br>
> malloc(sizeof(libvlc_crash_handler_t));<br>
> + current->j_libVLC = j_libVLC_local;<br>
> + current->prev = last;<br>
> + current->next = NULL;<br>
> +<br>
> + // update values<br>
> + if (first == NULL)<br>
> + first = current;<br>
> + if (last != NULL)<br>
> + last->next = current;<br>
> +<br>
> + last = current;<br>
> +<br>
> + libvlc_crash_handler_t *lp = first;<br>
> + while (lp != NULL)<br>
> + {<br>
> + LOGI("Current handlers: %p", lp);<br>
> + lp = lp->next;<br>
> + }<br>
> +<br>
> + pthread_mutex_unlock(&native_handler_lock);<br>
> +}<br>
> +<br>
> +<br>
> +void remove_native_crash_handler(JNIEnv *env, jobject j_libVLC_local)<br>
> +{<br>
> + LOGI("Remove crash handler to %p", j_libVLC_local);<br>
> +<br>
> + pthread_mutex_lock(&native_handler_lock);<br>
> +<br>
> + libvlc_crash_handler_t *lp = first;<br>
> + while (lp != NULL)<br>
> + {<br>
> + if (j_libVLC_local == lp->j_libVLC)<br>
> + {<br>
> + LOGI("Destroying handler: %p", lp);<br>
> +<br>
> + libvlc_crash_handler_t *todelete = lp;<br>
> + libvlc_crash_handler_t *prev = lp->prev;<br>
> + libvlc_crash_handler_t *next = lp->next;<br>
> + if (lp == first) {<br>
> + first = next;<br>
> + if (first != NULL)<br>
> + first->prev = NULL;<br>
> + } else if (lp == last) {<br>
> + last = prev;<br>
> + last->next = NULL;<br>
> + } else {<br>
> + prev->next = next;<br>
> + next->prev = prev;<br>
> + }<br>
> +<br>
> + free(todelete);<br>
> +<br>
> + break;<br>
> + }<br>
> +<br>
> + lp = lp->next;<br>
> + }<br>
> +<br>
> + pthread_mutex_unlock(&native_handler_lock);<br>
> }<br>
> diff --git a/libvlc/jni/native_crash_handler.h<br>
> b/libvlc/jni/native_crash_handler.h<br>
> index a57e61e..080fc0b 100644<br>
> --- a/libvlc/jni/native_crash_handler.h<br>
> +++ b/libvlc/jni/native_crash_handler.h<br>
> @@ -23,7 +23,11 @@<br>
><br>
> #include <jni.h><br>
><br>
> -void init_native_crash_handler(JNIEnv *env, jobject j_libVLC_local);<br>
> -void destroy_native_crash_handler(JNIEnv *env);<br>
> +typedef struct libvlc_crash_handler_t libvlc_crash_handler_t;<br>
> +<br>
> +void init_native_crash_handler();<br>
> +void destroy_native_crash_handler();<br>
> +void add_native_crash_handler(JNIEnv *env, jobject j_libVLC_local);<br>
> +void remove_native_crash_handler(JNIEnv *env, jobject j_libVLC_local);<br>
<br>
<br>
</div></div>You don't need all that for nativecrash handler. It must be a singleton.<br>
on vlc-android, you can check how it is used:<br>
public void onNativeCrash() {<br>
Intent i = new Intent(context, NativeCrashActivity.class);<br>
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br>
i.putExtra("PID", android.os.Process.myPid());<br>
context.startActivity(i);<br>
}<br>
<br>
You see, it start a new activity and it just need a context.<br>
<br>
So I would add NativeCrashHandler.java in libvlc with a static native<br>
function: Java_org_videolan_libvlc_NativeCrashHandler_setListener.<br>
And I would set the listener in VLCApplication::onCreate by calling<br>
NativeCrashHandler.setListener()<br>
<div><div class="h5"><br>
><br>
> #endif // LIBVLCJNI_NATIVE_CRASH_HANDLER_H<br>
> diff --git a/libvlc/jni/utils.h b/libvlc/jni/utils.h<br>
> index 5da3514..ac23660 100644<br>
> --- a/libvlc/jni/utils.h<br>
> +++ b/libvlc/jni/utils.h<br>
> @@ -43,8 +43,6 @@ void arrayListGetIDs(JNIEnv *env, jclass* p_class,<br>
> jmethodID* p_add, jmethodID*<br>
><br>
> void arrayListStringAdd(JNIEnv *env, jclass class, jmethodID methodID,<br>
> jobject arrayList, const char* str);<br>
><br>
> -jobject getEventHandlerReference(JNIEnv *env, jobject thiz, jobject<br>
> eventHandler);<br>
> -<br>
> void debug_log(void *data, int level, const libvlc_log_t *ctx, const<br>
> char *fmt, va_list ap);<br>
><br>
> #endif // LIBVLCJNI_UTILS_H<br>
> diff --git a/libvlc/jni/vout.c b/libvlc/jni/vout.c<br>
> index 04c933c..7cdc234 100644<br>
> --- a/libvlc/jni/vout.c<br>
> +++ b/libvlc/jni/vout.c<br>
> @@ -22,100 +22,104 @@<br>
> #include <vlc_common.h><br>
><br>
> #include <jni.h><br>
> +#include "vout.h"<br>
> +<br>
> +#define LOG_TAG "VLC/JNI/vout"<br>
> +#include "log.h"<br>
><br>
> #define THREAD_NAME "jni_vout"<br>
> extern int jni_attach_thread(JNIEnv **env, const char *thread_name);<br>
> extern void jni_detach_thread();<br>
> extern int jni_get_env(JNIEnv **env);<br>
><br>
> -pthread_mutex_t vout_android_lock;<br>
> -pthread_cond_t vout_android_surf_attached;<br>
> -static void *vout_android_gui = NULL;<br>
> -static jobject vout_android_java_surf = NULL;<br>
> -static jobject vout_android_subtitles_surf = NULL;<br>
> -static bool vout_video_player_activity_created = false;<br>
> -<br>
> -void *jni_LockAndGetSubtitlesSurface() {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - while (vout_android_subtitles_surf == NULL)<br>
> - pthread_cond_wait(&vout_android_surf_attached,<br>
> &vout_android_lock);<br>
> - return vout_android_subtitles_surf;<br>
> +void *jni_LockAndGetSubtitlesSurface(android_surf_value_t<br>
> *android_surface) {<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + while (android_surface->vout_android_subtitles_surf == NULL)<br>
> + pthread_cond_wait(&android_surface->vout_android_surf_attached,<br>
> &android_surface->vout_android_lock);<br>
> + return android_surface->vout_android_subtitles_surf;<br>
> +}<br>
> +<br>
> +jobject jni_LockAndGetAndroidJavaSurface(android_surf_value_t<br>
> *android_surface) {<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + while (android_surface->vout_android_java_surf == NULL)<br>
> + pthread_cond_wait(&android_surface->vout_android_surf_attached,<br>
> &android_surface->vout_android_lock);<br>
> + return android_surface->vout_android_java_surf;<br>
> }<br>
><br>
> -jobject jni_LockAndGetAndroidJavaSurface() {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - while (vout_android_java_surf == NULL)<br>
> - pthread_cond_wait(&vout_android_surf_attached,<br>
> &vout_android_lock);<br>
> - return vout_android_java_surf;<br>
> +void jni_LockAndroidSurface(android_surf_value_t *android_surface) {<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> -void jni_UnlockAndroidSurface() {<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> +void jni_UnlockAndroidSurface(android_surf_value_t *android_surface) {<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> -void jni_EventHardwareAccelerationError()<br>
> +void jni_EventHardwareAccelerationError(android_surf_value_t<br>
> *android_surface)<br>
> {<br>
> JNIEnv *env;<br>
> bool isAttached = false;<br>
><br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - if (vout_android_gui == NULL) {<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + if (android_surface->vout_android_gui == NULL) {<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return;<br>
> }<br>
><br>
> if (jni_get_env(&env) < 0) {<br>
> if (jni_attach_thread(&env, THREAD_NAME) < 0) {<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return;<br>
> }<br>
> isAttached = true;<br>
> }<br>
><br>
> - jclass cls = (*env)->GetObjectClass(env, vout_android_gui);<br>
> + jclass cls = (*env)->GetObjectClass(env,<br>
> android_surface->vout_android_gui);<br>
> jmethodID methodId = (*env)->GetMethodID(env, cls,<br>
> "eventHardwareAccelerationError", "()V");<br>
> - (*env)->CallVoidMethod(env, vout_android_gui, methodId);<br>
> + (*env)->CallVoidMethod(env, android_surface->vout_android_gui,<br>
> methodId);<br>
><br>
> (*env)->DeleteLocalRef(env, cls);<br>
> if (isAttached)<br>
> jni_detach_thread();<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> -static void jni_SetSurfaceLayoutEnv(JNIEnv *p_env, int width, int<br>
> height, int visible_width, int visible_height, int sar_num, int sar_den)<br>
> +void jni_SetSurfaceLayoutEnv(JNIEnv *p_env, android_surf_value_t<br>
> *android_surface, int width, int height, int visible_width, int<br>
> visible_height, int sar_num, int sar_den)<br>
> {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - if (vout_android_gui == NULL) {<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + if (android_surface->vout_android_gui == NULL) {<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return;<br>
> }<br>
><br>
> - jclass cls = (*p_env)->GetObjectClass (p_env, vout_android_gui);<br>
> + jclass cls = (*p_env)->GetObjectClass (p_env,<br>
> android_surface->vout_android_gui);<br>
> jmethodID methodId = (*p_env)->GetMethodID (p_env, cls,<br>
> "setSurfaceLayout", "(IIIIII)V");<br>
><br>
> - (*p_env)->CallVoidMethod (p_env, vout_android_gui, methodId, width,<br>
> height, visible_width, visible_height, sar_num, sar_den);<br>
> + (*p_env)->CallVoidMethod (p_env, android_surface->vout_android_gui,<br>
> methodId, width, height, visible_width, visible_height, sar_num,<br>
> sar_den);<br>
><br>
> (*p_env)->DeleteLocalRef(p_env, cls);<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> -void jni_SetSurfaceLayout(int width, int height, int visible_width, int<br>
> visible_height, int sar_num, int sar_den)<br>
> +void jni_SetSurfaceLayout(android_surf_value_t *android_surface, int<br>
> width, int height, int visible_width, int visible_height, int sar_num,<br>
> int sar_den)<br>
> {<br>
> JNIEnv *p_env;<br>
> bool isAttached = false;<br>
><br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> if (jni_get_env(&p_env) < 0) {<br>
> if (jni_attach_thread(&p_env, THREAD_NAME) < 0)<br>
> return;<br>
> isAttached = true;<br>
> }<br>
> - jni_SetSurfaceLayoutEnv(p_env, width, height, visible_width,<br>
> visible_height, sar_num, sar_den);<br>
> + jni_SetSurfaceLayoutEnv(p_env, android_surface, width, height,<br>
> visible_width, visible_height, sar_num, sar_den);<br>
><br>
> if (isAttached)<br>
> jni_detach_thread();<br>
> }<br>
><br>
> -void *jni_AndroidJavaSurfaceToNativeSurface(jobject *surf)<br>
> +void *jni_AndroidJavaSurfaceToNativeSurface(jobject surf)<br>
> {<br>
> JNIEnv *p_env;<br>
> jclass clz;<br>
> @@ -154,28 +158,29 @@ void *jni_AndroidJavaSurfaceToNativeSurface(jobject<br>
> *surf)<br>
> return native_surface;<br>
> }<br>
><br>
> -int jni_ConfigureSurface(jobject jsurf, int width, int height, int hal,<br>
> bool *configured)<br>
> +int jni_ConfigureSurface(android_surf_value_t *android_surface, int<br>
> width, int height, int hal, bool *configured)<br>
> {<br>
> JNIEnv *p_env;<br>
> bool isAttached = false;<br>
> int ret;<br>
><br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - if (vout_android_gui == NULL) {<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + if (android_surface->vout_android_gui == NULL) {<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return -1;<br>
> }<br>
><br>
> if (jni_get_env(&p_env) < 0) {<br>
> if (jni_attach_thread(&p_env, THREAD_NAME) < 0) {<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return -1;<br>
> }<br>
> isAttached = true;<br>
> }<br>
> - jclass clz = (*p_env)->GetObjectClass (p_env, vout_android_gui);<br>
> + jclass clz = (*p_env)->GetObjectClass (p_env,<br>
> android_surface->vout_android_gui);<br>
> jmethodID methodId = (*p_env)->GetMethodID (p_env, clz,<br>
> "configureSurface", "(Landroid/view/Surface;III)I");<br>
> - ret = (*p_env)->CallIntMethod (p_env, vout_android_gui, methodId,<br>
> jsurf, width, height, hal);<br>
> + ret = (*p_env)->CallIntMethod (p_env,<br>
> android_surface->vout_android_gui, methodId,<br>
> +<br>
> android_surface->vout_android_java_surf, width, height, hal);<br>
> if (ret >= 0 && configured)<br>
> *configured = ret == 1;<br>
><br>
> @@ -183,62 +188,81 @@ int jni_ConfigureSurface(jobject jsurf, int width,<br>
> int height, int hal, bool *co<br>
><br>
> if (isAttached)<br>
> jni_detach_thread();<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return ret == -1 ? -1 : 0;<br>
> }<br>
><br>
> -bool jni_IsVideoPlayerActivityCreated() {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - bool result = vout_video_player_activity_created;<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> +bool jni_IsVideoPlayerActivityCreated(android_surf_value_t<br>
> *android_surface) {<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + bool result = android_surface->vout_video_player_activity_created;<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> return result;<br>
> }<br>
><br>
> void<br>
> Java_org_videolan_libvlc_LibVLC_eventVideoPlayerActivityCreated(JNIEnv<br>
> *env, jobject thiz, jboolean created) {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - vout_video_player_activity_created = created;<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + android_surf_value_t * android_surface =<br>
> getAndroidSurfaceInstance(env, thiz);<br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + android_surface->vout_video_player_activity_created = created;<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> void Java_org_videolan_libvlc_LibVLC_attachSurface(JNIEnv *env, jobject<br>
> thiz, jobject surf, jobject gui) {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> -<br>
> - if (vout_android_gui != NULL)<br>
> - (*env)->DeleteGlobalRef(env, vout_android_gui);<br>
> - if (vout_android_java_surf != NULL)<br>
> - (*env)->DeleteGlobalRef(env, vout_android_java_surf);<br>
> - vout_android_gui = (*env)->NewGlobalRef(env, gui);<br>
> - vout_android_java_surf = (*env)->NewGlobalRef(env, surf);<br>
> - pthread_cond_signal(&vout_android_surf_attached);<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + android_surf_value_t * android_surface =<br>
> getAndroidSurfaceInstance(env, thiz);<br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + if (android_surface->vout_android_gui != NULL)<br>
> + (*env)->DeleteGlobalRef(env, android_surface->vout_android_gui);<br>
> + if (android_surface->vout_android_java_surf != NULL)<br>
> + (*env)->DeleteGlobalRef(env,<br>
> android_surface->vout_android_java_surf);<br>
> + android_surface->vout_android_gui = (*env)->NewGlobalRef(env, gui);<br>
> + android_surface->vout_android_java_surf = (*env)->NewGlobalRef(env,<br>
> surf);<br>
> + pthread_cond_signal(&android_surface->vout_android_surf_attached);<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> void Java_org_videolan_libvlc_LibVLC_detachSurface(JNIEnv *env, jobject<br>
> thiz) {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - if (vout_android_gui != NULL)<br>
> - (*env)->DeleteGlobalRef(env, vout_android_gui);<br>
> - if (vout_android_java_surf != NULL)<br>
> - (*env)->DeleteGlobalRef(env, vout_android_java_surf);<br>
> - vout_android_gui = NULL;<br>
> - vout_android_java_surf = NULL;<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + android_surf_value_t * android_surface =<br>
> getAndroidSurfaceInstance(env, thiz);<br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + if (android_surface->vout_android_gui != NULL)<br>
> + (*env)->DeleteGlobalRef(env, android_surface->vout_android_gui);<br>
> + if (android_surface->vout_android_java_surf != NULL)<br>
> + (*env)->DeleteGlobalRef(env,<br>
> android_surface->vout_android_java_surf);<br>
> + android_surface->vout_android_gui = NULL;<br>
> + android_surface->vout_android_java_surf = NULL;<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> void Java_org_videolan_libvlc_LibVLC_attachSubtitlesSurface(JNIEnv *env,<br>
> jobject thiz, jobject surf) {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - if (vout_android_subtitles_surf != NULL)<br>
> - (*env)->DeleteGlobalRef(env, vout_android_subtitles_surf);<br>
> - vout_android_subtitles_surf = (*env)->NewGlobalRef(env, surf);<br>
> - pthread_cond_signal(&vout_android_surf_attached);<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + android_surf_value_t * android_surface =<br>
> getAndroidSurfaceInstance(env, thiz);<br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + if (android_surface->vout_android_subtitles_surf != NULL)<br>
> + (*env)->DeleteGlobalRef(env,<br>
> android_surface->vout_android_subtitles_surf);<br>
> + android_surface->vout_android_subtitles_surf =<br>
> (*env)->NewGlobalRef(env, surf);<br>
> + pthread_cond_signal(&android_surface->vout_android_surf_attached);<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> void Java_org_videolan_libvlc_LibVLC_detachSubtitlesSurface(JNIEnv *env,<br>
> jobject thiz) {<br>
> - pthread_mutex_lock(&vout_android_lock);<br>
> - if (vout_android_subtitles_surf != NULL)<br>
> - (*env)->DeleteGlobalRef(env, vout_android_subtitles_surf);<br>
> - vout_android_subtitles_surf = NULL;<br>
> - pthread_mutex_unlock(&vout_android_lock);<br>
> + android_surf_value_t * android_surface =<br>
> getAndroidSurfaceInstance(env, thiz);<br>
> + if (android_surface == NULL)<br>
> + return;<br>
> +<br>
> + pthread_mutex_lock(&android_surface->vout_android_lock);<br>
> + if (android_surface->vout_android_subtitles_surf != NULL)<br>
> + (*env)->DeleteGlobalRef(env,<br>
> android_surface->vout_android_subtitles_surf);<br>
> + android_surface->vout_android_subtitles_surf = NULL;<br>
> + pthread_mutex_unlock(&android_surface->vout_android_lock);<br>
> }<br>
><br>
> static int mouse_x = -1;<br>
> diff --git a/libvlc/jni/vout.h b/libvlc/jni/vout.h<br>
> index c3d4fd7..6681b59 100644<br>
> --- a/libvlc/jni/vout.h<br>
> +++ b/libvlc/jni/vout.h<br>
> @@ -21,8 +21,27 @@<br>
> #ifndef LIBVLCJNI_VOUT_H<br>
> #define LIBVLCJNI_VOUT_H<br>
><br>
> -/* vout lock initialized in vout.c */<br>
> -pthread_mutex_t vout_android_lock;<br>
> -pthread_cond_t vout_android_surf_attached;<br>
> +#include <pthread.h><br>
> +<br>
> +<br>
> +typedef struct android_surf_value_t {<br>
> + pthread_mutex_t vout_android_lock;<br>
> + pthread_cond_t vout_android_surf_attached;<br>
> + void *vout_android_gui;<br>
> + jobject vout_android_java_surf;<br>
> + jobject vout_android_subtitles_surf;<br>
> + bool vout_video_player_activity_created;<br>
> + int i_window_width;<br>
> + int i_window_height;<br>
> +} android_surf_value_t;<br>
> +<br>
> +typedef struct libvlc_instance_sys_t {<br>
> + void *p_this;<br>
> + libvlc_instance_t *p_libvlc;<br>
> + libvlc_media_player_t *p_mp;<br>
> + android_surf_value_t *p_surf;<br>
> +} libvlc_instance_sys_t;<br>
> +<br>
> +android_surf_value_t *getAndroidSurfaceInstance(JNIEnv *env, jobject<br>
> thiz);<br>
><br>
> #endif // LIBVLCJNI_VOUT_H<br>
> diff --git a/libvlc/src/org/videolan/libvlc/EventHandler.java<br>
> b/libvlc/src/org/videolan/libvlc/EventHandler.java<br>
> index 4ec0861..2d13d30 100644<br>
> --- a/libvlc/src/org/videolan/libvlc/EventHandler.java<br>
> +++ b/libvlc/src/org/videolan/libvlc/EventHandler.java<br>
> @@ -97,19 +97,11 @@ public class EventHandler {<br>
> public static final int HardwareAccelerationError = 0x3000;<br>
><br>
> private ArrayList<Handler> mEventHandler;<br>
> - private static EventHandler mInstance;<br>
><br>
> EventHandler() {<br>
> mEventHandler = new ArrayList<Handler>();<br>
> }<br>
><br>
> - public static EventHandler getInstance() {<br>
> - if (mInstance == null) {<br>
> - mInstance = new EventHandler();<br>
> - }<br>
> - return mInstance;<br>
> - }<br>
> -<br>
> public void addHandler(Handler handler) {<br>
> if (!mEventHandler.contains(handler))<br>
> mEventHandler.add(handler);<br>
> diff --git a/libvlc/src/org/videolan/libvlc/LibVLC.java<br>
> b/libvlc/src/org/videolan/libvlc/LibVLC.java<br>
> index 5d1f830..5273887 100644<br>
> --- a/libvlc/src/org/videolan/libvlc/LibVLC.java<br>
> +++ b/libvlc/src/org/videolan/libvlc/LibVLC.java<br>
> @@ -61,11 +61,12 @@ public class LibVLC {<br>
><br>
> private static LibVLC sInstance;<br>
><br>
> + private final EventHandler eventHandler = new EventHandler();<br>
> +<br>
> /** libVLC instance C pointer */<br>
> private long mLibVlcInstance = 0; // Read-only, reserved for JNI<br>
> /** libvlc_media_player pointer and index */<br>
> private int mInternalMediaPlayerIndex = 0; // Read-only, reserved<br>
> for JNI<br>
> - private long mInternalMediaPlayerInstance = 0; // Read-only,<br>
> reserved for JNI<br>
><br>
> private MediaList mMediaList; // Pointer to media list being<br>
> followed<br>
> private MediaList mPrimaryList; // Primary/default media list; see<br>
> getPrimaryMediaList()<br>
> @@ -190,10 +191,9 @@ public class LibVLC {<br>
> }<br>
><br>
> /**<br>
> - * Constructor<br>
> - * It is private because this class is a singleton.<br>
> + * Constructor.<br>
> */<br>
> - private LibVLC() {<br>
> + public LibVLC() {<br>
> mAout = new AudioOutput();<br>
> }<br>
><br>
> @@ -259,7 +259,7 @@ public class LibVLC {<br>
> */<br>
> public native void setSurface(Surface f);<br>
><br>
> - public static synchronized void restart(Context context) {<br>
> + public static synchronized void restartInstance(Context context) {<br>
> if (sInstance != null) {<br>
> try {<br>
> sInstance.destroy();<br>
> @@ -270,6 +270,15 @@ public class LibVLC {<br>
> }<br>
> }<br>
><br>
> + public void restart(Context context) {<br>
> + try {<br>
> + this.destroy();<br>
> + this.init(context);<br>
> + } catch (LibVlcException lve) {<br>
> + Log.e(TAG, "Unable to reinit libvlc: " + lve);<br>
> + }<br>
> + }<br>
> +<br>
> /**<br>
> * those get/is* are called from native code to get settings values.<br>
> */<br>
> @@ -488,11 +497,11 @@ public class LibVLC {<br>
> applyEqualizer();<br>
> }<br>
><br>
> - private void applyEqualizer()<br>
> + protected void applyEqualizer()<br>
> {<br>
> - setNativeEqualizer(mInternalMediaPlayerInstance,<br>
> this.equalizer);<br>
> + setNativeEqualizer(this.equalizer);<br>
> }<br>
> - private native int setNativeEqualizer(long mediaPlayer, float[]<br>
> bands);<br>
> + private native int setNativeEqualizer(float[] bands);<br>
><br>
> public boolean frameSkipEnabled() {<br>
> return frameSkip;<br>
> @@ -538,7 +547,6 @@ public class LibVLC {<br>
> mCachePath = (cacheDir != null) ? cacheDir.getAbsolutePath()<br>
> : null;<br>
> nativeInit();<br>
> mMediaList = mPrimaryList = new MediaList(this);<br>
> - setEventHandler(EventHandler.getInstance());<br>
> mIsInitialized = true;<br>
> }<br>
> }<br>
> @@ -550,7 +558,6 @@ public class LibVLC {<br>
> public void destroy() {<br>
> Log.v(TAG, "Destroying LibVLC instance");<br>
> nativeDestroy();<br>
> - detachEventHandler();<br>
> mIsInitialized = false;<br>
> }<br>
><br>
> @@ -830,10 +837,6 @@ public class LibVLC {<br>
> return mMediaList.expandMedia(mInternalMediaPlayerIndex);<br>
> }<br>
><br>
> - private native void setEventHandler(EventHandler eventHandler);<br>
> -<br>
> - private native void detachEventHandler();<br>
> -<br>
> public native float[] getBands();<br>
><br>
> public native String[] getPresets();<br>
> @@ -853,6 +856,10 @@ public class LibVLC {<br>
> mOnNativeCrashListener.onNativeCrash();<br>
> }<br>
><br>
> + public EventHandler getEventHandler() {<br>
> + return eventHandler;<br>
> + }<br>
> +<br>
> public String getCachePath() {<br>
> return mCachePath;<br>
> }<br>
> diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java<br>
> b/vlc-android/src/org/videolan/vlc/audio/AudioService.java<br>
> index c33abc8..d039fba 100644<br>
> --- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java<br>
> +++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java<br>
> @@ -116,7 +116,6 @@ public class AudioService extends Service {<br>
><br>
> private LibVLC mLibVLC;<br>
> private HashMap<IAudioServiceCallback, Integer> mCallback;<br>
> - private EventHandler mEventHandler;<br>
> private OnAudioFocusChangeListener audioFocusListener;<br>
> private boolean mDetectHeadset = true;<br>
> private PowerManager.WakeLock mWakeLock;<br>
> @@ -165,7 +164,6 @@ public class AudioService extends Service {<br>
> mPrevIndex = -1;<br>
> mNextIndex = -1;<br>
> mPrevious = new Stack<Integer>();<br>
> - mEventHandler = EventHandler.getInstance();<br>
> mRemoteControlClientReceiverComponent = new<br>
> ComponentName(getPackageName(),<br>
> RemoteControlClientReceiver.class.getName());<br>
><br>
> @@ -346,7 +344,7 @@ public class AudioService extends Service {<br>
> }<br>
><br>
> /*<br>
> - * Incoming Call : Pause if VLC is playing audio or video.<br>
> + * Incoming Call : Pause if VLC is playing audio or video.<br>
> */<br>
> if<br>
> (action.equalsIgnoreCase(VLCApplication.INCOMING_CALL_INTENT))<br>
> {<br>
> mWasPlayingAudio = mLibVLC.isPlaying() &&<br>
> mLibVLC.getVideoTracksCount() < 1;<br>
> @@ -625,7 +623,7 @@ public class AudioService extends Service {<br>
> String MRL = mLibVLC.getMediaList().getMRL(mCurrentIndex);<br>
> int index = mCurrentIndex;<br>
> mCurrentIndex = -1;<br>
> - mEventHandler.removeHandler(mVlcEventHandler);<br>
> + mLibVLC.getEventHandler().removeHandler(mVlcEventHandler);<br>
> // Preserve playback when switching to video<br>
> hideNotification(false);<br>
><br>
> @@ -833,7 +831,7 @@ public class AudioService extends Service {<br>
><br>
> private void stop() {<br>
> mLibVLC.stop();<br>
> - mEventHandler.removeHandler(mVlcEventHandler);<br>
> + mLibVLC.getEventHandler().removeHandler(mVlcEventHandler);<br>
> mLibVLC.getMediaList().getEventHandler().removeHandler(mListEventHandler);<br>
> setRemoteControlClientPlaybackState(EventHandler.MediaPlayerStopped);<br>
> mCurrentIndex = -1;<br>
> @@ -1158,7 +1156,7 @@ public class AudioService extends Service {<br>
> throws RemoteException {<br>
><br>
> Log.v(TAG, "Loading position " +<br>
> ((Integer)position).toString() + " in " +<br>
> mediaPathList.toString());<br>
> - mEventHandler.addHandler(mVlcEventHandler);<br>
> + mLibVLC.getEventHandler().addHandler(mVlcEventHandler);<br>
><br>
> mLibVLC.getMediaList().getEventHandler().removeHandler(mListEventHandler);<br>
> mLibVLC.setMediaList();<br>
> @@ -1226,7 +1224,7 @@ public class AudioService extends Service {<br>
> mCurrentIndex = 0;<br>
> }<br>
><br>
> - mEventHandler.addHandler(mVlcEventHandler);<br>
> + mLibVLC.getEventHandler().addHandler(mVlcEventHandler);<br>
> mLibVLC.playIndex(mCurrentIndex);<br>
> mHandler.sendEmptyMessage(SHOW_PROGRESS);<br>
> setUpRemoteControlClient();<br>
> @@ -1250,7 +1248,7 @@ public class AudioService extends Service {<br>
><br>
> if(URI == null || !mLibVLC.isPlaying())<br>
> return;<br>
> - mEventHandler.addHandler(mVlcEventHandler);<br>
> + mLibVLC.getEventHandler().addHandler(mVlcEventHandler);<br>
> mCurrentIndex = index;<br>
><br>
> // Notify everyone<br>
> diff --git<br>
> a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java<br>
> b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java<br>
> index 9f0109c..b069452 100644<br>
> --- a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java<br>
> +++ b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java<br>
> @@ -292,7 +292,7 @@ public class PreferencesActivity extends<br>
> PreferenceActivity implements OnSharedP<br>
> || key.equalsIgnoreCase("network_caching")<br>
> || key.equalsIgnoreCase("dev_hardware_decoder")) {<br>
> VLCInstance.updateLibVlcSettings(sharedPreferences);<br>
> - LibVLC.restart(this);<br>
> + LibVLC.restartInstance(this);<br>
> }<br>
> }<br>
><br>
> diff --git<br>
> a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java<br>
> b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java<br>
> index 4cf4793..195de50 100644<br>
> --- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java<br>
> +++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java<br>
> @@ -439,8 +439,7 @@ public class VideoPlayerActivity extends<br>
> ActionBarActivity implements IVideoPlay<br>
> // SurfaceView is now available for MediaCodec direct rendering.<br>
> mLibVLC.eventVideoPlayerActivityCreated(true);<br>
><br>
> - EventHandler em = EventHandler.getInstance();<br>
> - em.addHandler(eventHandler);<br>
> + mLibVLC.getEventHandler().addHandler(eventHandler);<br>
><br>
> this.setVolumeControlStream(AudioManager.STREAM_MUSIC);<br>
><br>
> @@ -590,8 +589,7 @@ public class VideoPlayerActivity extends<br>
> ActionBarActivity implements IVideoPlay<br>
> super.onDestroy();<br>
> unregisterReceiver(mReceiver);<br>
><br>
> - EventHandler em = EventHandler.getInstance();<br>
> - em.removeHandler(eventHandler);<br>
> + mLibVLC.getEventHandler().removeHandler(eventHandler);<br>
><br>
> // MediaCodec opaque direct rendering should not be used anymore<br>
> since there is no surface to attach.<br>
> mLibVLC.eventVideoPlayerActivityCreated(false);<br>
> @@ -1228,8 +1226,7 @@ public class VideoPlayerActivity extends<br>
> ActionBarActivity implements IVideoPlay<br>
> }<br>
><br>
> public void eventHardwareAccelerationError() {<br>
> - EventHandler em = EventHandler.getInstance();<br>
> - em.callback(EventHandler.HardwareAccelerationError, new<br>
> Bundle());<br>
> +<br>
> mLibVLC.getEventHandler().callback(EventHandler.HardwareAccelerationError,<br>
> new Bundle());<br>
> }<br>
><br>
> private void handleHardwareAccelerationError() {<br>
> --<br>
> 1.9.3 (Apple Git-50)<br>
><br>
><br>
</div></div>> _______________________________________________<br>
> Android mailing list<br>
> <a href="mailto:Android@videolan.org">Android@videolan.org</a><br>
> <a href="https://mailman.videolan.org/listinfo/android" target="_blank">https://mailman.videolan.org/listinfo/android</a><br>
><br>
<br>
The rest is good.<br>
_______________________________________________<br>
Android mailing list<br>
<a href="mailto:Android@videolan.org">Android@videolan.org</a><br>
<a href="https://mailman.videolan.org/listinfo/android" target="_blank">https://mailman.videolan.org/listinfo/android</a><br>
</blockquote></div><br></div>