[vlc-devel] [PATCH] android/jni: use a libvlcjni function to attach/detach java thread

Jean-Baptiste Kempf jb at videolan.org
Mon Nov 10 12:12:38 CET 2014


What is this patch?
What's the use of posting it again?

On 08 Nov, Edward Wang wrote :
> From: Thomas Guillem <tom at gllm.fr>
> 
> Fix ART warning about java thread attached without a name.
> 
> (cherry picked from commit d26fefcfa700d3a9c1b2c26f30a1667e72ac381b)
> Signed-off-by: Edward Wang <edward.c.wang at compdigitec.com>
> ---
>  Missed some bits
> 
>  modules/codec/omxil/android_mediacodec.c    | 29 ++++++++++++++++-------------
>  modules/video_output/android/nativewindow.c |  8 +++++---
>  modules/video_output/android/opaque.c       |  8 +++++---
>  modules/video_output/android/surface.c      |  8 +++++---
>  4 files changed, 31 insertions(+), 22 deletions(-)
> 
> diff --git a/modules/codec/omxil/android_mediacodec.c b/modules/codec/omxil/android_mediacodec.c
> index 1157fcd..bc8262b 100644
> --- a/modules/codec/omxil/android_mediacodec.c
> +++ b/modules/codec/omxil/android_mediacodec.c
> @@ -46,7 +46,10 @@
>  #define INFO_OUTPUT_FORMAT_CHANGED  -2
>  #define INFO_TRY_AGAIN_LATER        -1
>  
> -extern JavaVM *myVm;
> +#define THREAD_NAME "android_mediacodec"
> +
> +extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
> +extern void jni_detach_thread();
>  /* JNI functions to get/set an Android Surface object. */
>  extern jobject jni_LockAndGetAndroidJavaSurface();
>  extern void jni_UnlockAndroidSurface();
> @@ -327,7 +330,7 @@ static int OpenDecoder(vlc_object_t *p_this)
>      p_dec->b_need_packetized = true;
>  
>      JNIEnv* env = NULL;
> -    (*myVm)->AttachCurrentThread(myVm, &env, NULL);
> +    jni_attach_thread(&env, THREAD_NAME);
>  
>      for (int i = 0; classes[i].name; i++) {
>          *(jclass*)((uint8_t*)p_sys + classes[i].offset) =
> @@ -539,7 +542,7 @@ static int OpenDecoder(vlc_object_t *p_this)
>          goto error;
>      (*env)->DeleteLocalRef(env, format);
>  
> -    (*myVm)->DetachCurrentThread(myVm);
> +    jni_detach_thread();
>  
>      const int timestamp_fifo_size = 32;
>      p_sys->timestamp_fifo = timestamp_FifoNew(timestamp_fifo_size);
> @@ -549,7 +552,7 @@ static int OpenDecoder(vlc_object_t *p_this)
>      return VLC_SUCCESS;
>  
>   error:
> -    (*myVm)->DetachCurrentThread(myVm);
> +    jni_detach_thread();
>      CloseDecoder(p_this);
>      return VLC_EGENERIC;
>  }
> @@ -567,7 +570,7 @@ static void CloseDecoder(vlc_object_t *p_this)
>       * to prevent the vout from using destroyed output buffers. */
>      if (p_sys->direct_rendering)
>          InvalidateAllPictures(p_dec);
> -    (*myVm)->AttachCurrentThread(myVm, &env, NULL);
> +    jni_attach_thread(&env, THREAD_NAME);
>      if (p_sys->input_buffers)
>          (*env)->DeleteGlobalRef(env, p_sys->input_buffers);
>      if (p_sys->output_buffers)
> @@ -593,7 +596,7 @@ static void CloseDecoder(vlc_object_t *p_this)
>      }
>      if (p_sys->buffer_info)
>          (*env)->DeleteGlobalRef(env, p_sys->buffer_info);
> -    (*myVm)->DetachCurrentThread(myVm);
> +    jni_detach_thread();
>  
>      free(p_sys->name);
>      ArchitectureSpecificCopyHooksDestroy(p_sys->pixel_format, &p_sys->architecture_specific_data);
> @@ -627,14 +630,14 @@ static void DisplayBuffer(picture_sys_t* p_picsys, bool b_render)
>  
>      /* Release the MediaCodec buffer. */
>      JNIEnv *env = NULL;
> -    (*myVm)->AttachCurrentThread(myVm, &env, NULL);
> +    jni_attach_thread(&env, THREAD_NAME);
>      (*env)->CallVoidMethod(env, p_sys->codec, p_sys->release_output_buffer, i_index, b_render);
>      if ((*env)->ExceptionOccurred(env)) {
>          msg_Err(p_dec, "Exception in MediaCodec.releaseOutputBuffer (DisplayBuffer)");
>          (*env)->ExceptionClear(env);
>      }
>  
> -    (*myVm)->DetachCurrentThread(myVm);
> +    jni_detach_thread();
>      p_picsys->b_valid = false;
>  
>      vlc_mutex_unlock(get_android_opaque_mutex());
> @@ -896,7 +899,7 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
>          return NULL;
>      }
>  
> -    (*myVm)->AttachCurrentThread(myVm, &env, NULL);
> +    jni_attach_thread(&env, THREAD_NAME);
>  
>      if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
>          block_Release(p_block);
> @@ -916,7 +919,7 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
>              }
>          }
>          p_sys->decoded = false;
> -        (*myVm)->DetachCurrentThread(myVm);
> +        jni_detach_thread();
>          return NULL;
>      }
>  
> @@ -950,7 +953,7 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
>                   * without assigning NULL to *pp_block. The next call
>                   * to DecodeVideo will try to send the input packet again.
>                   */
> -                (*myVm)->DetachCurrentThread(myVm);
> +                jni_detach_thread();
>                  return p_pic;
>              }
>              timeout = 30 * 1000;
> @@ -976,7 +979,7 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
>                      block_Release(p_block);
>                      *pp_block = NULL;
>                  }
> -                (*myVm)->DetachCurrentThread(myVm);
> +                jni_detach_thread();
>                  return invalid_picture;
>              }
>              continue;
> @@ -1014,7 +1017,7 @@ static picture_t *DecodeVideo(decoder_t *p_dec, block_t **pp_block)
>      }
>      if (!p_pic)
>          GetOutput(p_dec, env, &p_pic, 0);
> -    (*myVm)->DetachCurrentThread(myVm);
> +    jni_detach_thread();
>  
>      block_Release(p_block);
>      *pp_block = NULL;
> diff --git a/modules/video_output/android/nativewindow.c b/modules/video_output/android/nativewindow.c
> index 29014b5..d3f308e 100644
> --- a/modules/video_output/android/nativewindow.c
> +++ b/modules/video_output/android/nativewindow.c
> @@ -37,7 +37,9 @@
>  
>  #include "utils.h"
>  
> -extern JavaVM *myVm;
> +#define THREAD_NAME "ANativeWindow"
> +extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
> +extern void jni_detach_thread();
>  extern jobject jni_LockAndGetAndroidJavaSurface();
>  extern void jni_UnlockAndroidSurface();
>  extern void  jni_SetAndroidSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
> @@ -89,9 +91,9 @@ static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
>          goto error;
>  
>      JNIEnv *p_env;
> -    (*myVm)->AttachCurrentThread(myVm, &p_env, NULL);
> +    jni_attach_thread(&p_env, THREAD_NAME);
>      p_sys->window = p_sys->native_window.winFromSurface(p_env, javaSurface); // ANativeWindow_fromSurface call.
> -    (*myVm)->DetachCurrentThread(myVm);
> +    jni_detach_thread();
>  
>      jni_UnlockAndroidSurface();
>  
> diff --git a/modules/video_output/android/opaque.c b/modules/video_output/android/opaque.c
> index eac9d46..7c28667 100644
> --- a/modules/video_output/android/opaque.c
> +++ b/modules/video_output/android/opaque.c
> @@ -50,7 +50,9 @@ vlc_module_begin()
>      set_callbacks(Open, Close)
>  vlc_module_end()
>  
> -extern JavaVM *myVm;
> +#define THREAD_NAME "vout_mediacodec"
> +extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
> +extern void jni_detach_thread();
>  extern jobject jni_LockAndGetSubtitlesSurface();
>  extern void  jni_UnlockAndroidSurface();
>  
> @@ -124,9 +126,9 @@ static void DisplaySubpicture(vout_display_t *vd, subpicture_t *subpicture)
>      if (!sys->window)
>      {
>          JNIEnv *p_env;
> -        (*myVm)->AttachCurrentThread(myVm, &p_env, NULL);
> +        jni_attach_thread(&p_env, THREAD_NAME);
>          sys->window = sys->native_window.winFromSurface(p_env, jsurf);
> -        (*myVm)->DetachCurrentThread(myVm);
> +        jni_detach_thread();
>      }
>  
>      ANativeWindow_Buffer buf = { 0 };
> diff --git a/modules/video_output/android/surface.c b/modules/video_output/android/surface.c
> index 63cbf15..9ab2f53 100644
> --- a/modules/video_output/android/surface.c
> +++ b/modules/video_output/android/surface.c
> @@ -73,7 +73,9 @@ vlc_module_end()
>   * JNI prototypes
>   *****************************************************************************/
>  
> -extern JavaVM *myVm;
> +#define THREAD_NAME "AndroidSurface"
> +extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
> +extern void jni_detach_thread();
>  extern void *jni_LockAndGetAndroidSurface();
>  extern jobject jni_LockAndGetAndroidJavaSurface();
>  extern void  jni_UnlockAndroidSurface();
> @@ -370,9 +372,9 @@ static int  AndroidLockSurface(picture_t *picture)
>          sys->jsurf = jsurf;
>          if (!sys->window) {
>              JNIEnv *p_env;
> -            (*myVm)->AttachCurrentThread(myVm, &p_env, NULL);
> +            jni_attach_thread(&p_env, THREAD_NAME);
>              sys->window = sys->native_window.winFromSurface(p_env, jsurf);
> -            (*myVm)->DetachCurrentThread(myVm);
> +            jni_detach_thread();
>          }
>          // Using sys->window instead of the native surface object
>          // as parameter to the unlock function
> -- 
> 2.1.3
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel

-- 
With my kindest regards,

-- 
Jean-Baptiste Kempf
http://www.jbkempf.com/ - +33 672 704 734
Sent from my Electronic Device



More information about the vlc-devel mailing list