diff --git a/modules/audio_output/Makefile.am b/modules/audio_output/Makefile.am index 06936e4c6e..05b8efc2b3 100644 --- a/modules/audio_output/Makefile.am +++ b/modules/audio_output/Makefile.am @@ -2,13 +2,13 @@ aoutdir = $(pluginsdir)/audio_output aout_LTLIBRARIES = libopensles_android_plugin_la_SOURCES = audio_output/android/opensles.c -libopensles_android_plugin_la_LIBADD = libandroid_utils.la $(LIBDL) $(LIBM) +libopensles_android_plugin_la_LIBADD = libandroid_env.la $(LIBDL) $(LIBM) libandroid_audiotrack_plugin_la_SOURCES = audio_output/android/audiotrack.c audio_output/android/device.h -libandroid_audiotrack_plugin_la_LIBADD = libandroid_utils.la +libandroid_audiotrack_plugin_la_LIBADD = libandroid_env.la libandroid_aaudio_plugin_la_SOURCES = audio_output/android/aaudio.c audio_output/android/device.h -libandroid_aaudio_plugin_la_LIBADD = libandroid_utils.la $(LIBDL) +libandroid_aaudio_plugin_la_LIBADD = libandroid_env.la $(LIBDL) libandroid_audiodevice_plugin_la_SOURCES = audio_output/android/device.c audio_output/android/device.h diff --git a/modules/audio_output/android/audiotrack.c b/modules/audio_output/android/audiotrack.c index 8ab16d6334..b6a95afe8b 100644 --- a/modules/audio_output/android/audiotrack.c +++ b/modules/audio_output/android/audiotrack.c @@ -33,7 +33,7 @@ #include #include #include -#include "../video_output/android/utils.h" +#include "../video_output/android/env.h" #include "device.h" #define SMOOTHPOS_SAMPLE_COUNT 10 diff --git a/modules/audio_output/android/device.c b/modules/audio_output/android/device.c index b4a7b86823..e1a546c03c 100644 --- a/modules/audio_output/android/device.c +++ b/modules/audio_output/android/device.c @@ -29,7 +29,7 @@ #include #include #include "device.h" -#include "../video_output/android/utils.h" +#include "../video_output/android/env.h" /* There is an undefined behavior when configuring AudioTrack with SPDIF or * more than 2 channels when there is no HDMI out. It may succeed and the diff --git a/modules/audio_output/android/opensles.c b/modules/audio_output/android/opensles.c index f58a839e5d..ee31414834 100644 --- a/modules/audio_output/android/opensles.c +++ b/modules/audio_output/android/opensles.c @@ -33,12 +33,13 @@ #include #include +#include "../../video_output/android/env.h" + // For native audio #include #include #include -JNIEnv *android_getEnv(vlc_object_t *p_obj, const char *psz_thread_name); #define OPENSLES_BUFFERS 255 /* maximum number of buffers */ #define OPENSLES_BUFLEN VLC_TICK_FROM_MS(10) diff --git a/modules/codec/Makefile.am b/modules/codec/Makefile.am index 7780248884..36e18e7065 100644 --- a/modules/codec/Makefile.am +++ b/modules/codec/Makefile.am @@ -487,7 +487,8 @@ libmediacodec_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/codec/omxil libmediacodec_plugin_la_SOURCES = codec/omxil/mediacodec.c codec/omxil/mediacodec.h \ codec/omxil/mediacodec_jni.c codec/omxil/mediacodec_ndk.c codec/omxil/utils.c \ codec/omxil/omxil_utils.h codec/omxil/qcom.c codec/omxil/qcom.h -libmediacodec_plugin_la_LIBADD = libchroma_copy.la libvlc_hxxxhelper.la libandroid_utils.la $(LIBDL) +libmediacodec_plugin_la_LIBADD = libchroma_copy.la libvlc_hxxxhelper.la \ + libandroid_env.la libandroid_utils.la $(LIBDL) $(GLES2_LIBS) $(EGL_LIBS) codec_LTLIBRARIES += $(LTLIBomxil) EXTRA_LTLIBRARIES += libomxil_plugin.la if HAVE_ANDROID diff --git a/modules/codec/omxil/mediacodec_jni.c b/modules/codec/omxil/mediacodec_jni.c index 19390ab5a7..6d1722ad64 100644 --- a/modules/codec/omxil/mediacodec_jni.c +++ b/modules/codec/omxil/mediacodec_jni.c @@ -37,6 +37,7 @@ #include "../../packetizer/hevc_nal.h" #include "mediacodec.h" +#include "../../video_output/android/env.h" char* MediaCodec_GetName(vlc_object_t *p_obj, const char *psz_mime, int profile, int *p_quirks); diff --git a/modules/keystore/Makefile.am b/modules/keystore/Makefile.am index bdf9bdb7e0..62e57d22bf 100644 --- a/modules/keystore/Makefile.am +++ b/modules/keystore/Makefile.am @@ -9,7 +9,7 @@ libfile_keystore_plugin_la_SOURCES = keystore/file.c \ keystore/list_util.c keystore/list_util.h if HAVE_ANDROID libfile_keystore_plugin_la_SOURCES += keystore/file_crypt_android.c -libfile_keystore_plugin_la_LIBADD = libandroid_utils.la +libfile_keystore_plugin_la_LIBADD = libandroid_env.la endif if HAVE_WIN32 libfile_keystore_plugin_la_SOURCES += keystore/file_crypt_win32.c diff --git a/modules/keystore/file_crypt_android.c b/modules/keystore/file_crypt_android.c index 2cbe98154c..5e6e207ccf 100644 --- a/modules/keystore/file_crypt_android.c +++ b/modules/keystore/file_crypt_android.c @@ -32,7 +32,7 @@ #include -JNIEnv * android_getEnv(vlc_object_t *, const char *); +#include "../video_output/android/env.h" #define GET_ENV() android_getEnv(VLC_OBJECT(p_keystore), "android keystore") static struct diff --git a/modules/video_output/Makefile.am b/modules/video_output/Makefile.am index 9754eed3ed..950abbc2c0 100644 --- a/modules/video_output/Makefile.am +++ b/modules/video_output/Makefile.am @@ -291,26 +291,34 @@ endif libandroid_utils_la_SOURCES = video_output/android/utils.c video_output/android/utils.h libandroid_utils_la_CFLAGS = $(AM_CFLAGS) $(GLES2_CFLAGS) $(EGL_CFLAGS) libandroid_utils_la_LIBADD = $(GLES2_LIBS) $(EGL_LIBS) + +libandroid_env_la_SOURCES = \ + video_output/android/env.c \ + video_output/android/env.h if HAVE_ANDROID -noinst_LTLIBRARIES += libandroid_utils.la +noinst_LTLIBRARIES += \ + libandroid_utils.la \ + libandroid_env.la endif libegl_android_plugin_la_SOURCES = video_output/opengl/egl.c libegl_android_plugin_la_CFLAGS = $(AM_CFLAGS) $(EGL_CFLAGS) -DUSE_PLATFORM_ANDROID=1 -libegl_android_plugin_la_LIBADD = $(EGL_LIBS) libandroid_utils.la +libegl_android_plugin_la_LIBADD = libandroid_env.la $(EGL_LIBS) libandroid_utils.la libandroid_window_plugin_la_SOURCES = video_output/android/window.c \ video_output/wasync_resize_compressor.h -libandroid_window_plugin_la_LIBADD = libandroid_utils.la $(LIBDL) +libandroid_window_plugin_la_LIBADD = \ + libandroid_env.la libandroid_utils.la $(LIBDL) $(EGL_LIBS) $(GLES2_LIBS) libandroid_display_plugin_la_SOURCES = video_output/android/display.c libandroid_display_plugin_la_CFLAGS = $(AM_CFLAGS) $(GLES2_CFLAGS) -DUSE_OPENGL_ES2 -libandroid_display_plugin_la_LIBADD = libvlc_opengles.la +libandroid_display_plugin_la_LIBADD = libvlc_opengles.la $(EGL_LIBS) $(GLES2_LIBS) libglinterop_android_plugin_la_SOURCES = video_output/opengl/interop_android.c \ video_output/opengl/interop.h libglinterop_android_plugin_la_CFLAGS = $(AM_CFLAGS) -DUSE_OPENGL_ES2 -libglinterop_android_plugin_la_LIBADD = libandroid_utils.la +libglinterop_android_plugin_la_LIBADD = libandroid_env.la libandroid_utils.la \ + $(EGL_LIBS) $(GLES2_LIBS) if HAVE_ANDROID vout_LTLIBRARIES += libandroid_window_plugin.la libandroid_display_plugin.la diff --git a/modules/video_output/android/env.c b/modules/video_output/android/env.c new file mode 100644 index 0000000000..b26cc4043e --- /dev/null +++ b/modules/video_output/android/env.c @@ -0,0 +1,103 @@ +/***************************************************************************** + * env.c: shared code between Android modules. + ***************************************************************************** + * Copyright (C) 2014-2015 VLC authors and VideoLAN + * + * Authors: Felix Abecassis + * Thomas Guillem + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "utils.h" +#include +#include +#include +#include + +/* + * Android JNIEnv helper + */ + +static pthread_key_t jni_env_key; +static pthread_once_t jni_env_key_once = PTHREAD_ONCE_INIT; + +/* This function is called when a thread attached to the Java VM is canceled or + * exited */ +static void +jni_detach_thread(void *data) +{ + JNIEnv *env = data; + JavaVM *jvm; + + (*env)->GetJavaVM(env, &jvm); + assert(jvm); + (*jvm)->DetachCurrentThread(jvm); +} + +static void jni_env_key_create() +{ + /* Create a TSD area and setup a destroy callback when a thread that + * previously set the jni_env_key is canceled or exited */ + pthread_key_create(&jni_env_key, jni_detach_thread); +} + +JNIEnv * +android_getEnvCommon(vlc_object_t *p_obj, JavaVM *jvm, const char *psz_name) +{ + assert((p_obj && !jvm) || (!p_obj && jvm)); + + JNIEnv *env; + + pthread_once(&jni_env_key_once, jni_env_key_create); + env = pthread_getspecific(jni_env_key); + if (env == NULL) + { + if (!jvm) + jvm = var_InheritAddress(p_obj, "android-jvm"); + + if (!jvm) + return NULL; + + /* if GetEnv returns JNI_OK, the thread is already attached to the + * JavaVM, so we are already in a java thread, and we don't have to + * setup any destroy callbacks */ + if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2) != JNI_OK) + { + /* attach the thread to the Java VM */ + JavaVMAttachArgs args; + + args.version = JNI_VERSION_1_2; + args.name = psz_name; + args.group = NULL; + + if ((*jvm)->AttachCurrentThread(jvm, &env, &args) != JNI_OK) + return NULL; + + /* Set the attached env to the thread-specific data area (TSD) */ + if (pthread_setspecific(jni_env_key, env) != 0) + { + (*jvm)->DetachCurrentThread(jvm); + return NULL; + } + } + } + + return env; +} diff --git a/modules/video_output/android/env.h b/modules/video_output/android/env.h new file mode 100644 index 0000000000..8da5c251aa --- /dev/null +++ b/modules/video_output/android/env.h @@ -0,0 +1,33 @@ +/***************************************************************************** + * env.h: shared code between Android modules + ***************************************************************************** + * Copyright (C) 2014-2015 VLC authors and VideoLAN + * + * Authors: Felix Abecassis + * Thomas Guillem + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include + +JNIEnv * +android_getEnvCommon(vlc_object_t *p_obj, JavaVM *jvm, const char *psz_name); + +static inline JNIEnv * +android_getEnv(vlc_object_t *p_obj, const char *psz_name) +{ + return android_getEnvCommon(p_obj, NULL, psz_name); +} diff --git a/modules/video_output/android/utils.c b/modules/video_output/android/utils.c index 3c048965a2..cfa3c732ec 100644 --- a/modules/video_output/android/utils.c +++ b/modules/video_output/android/utils.c @@ -26,6 +26,8 @@ #endif #include "utils.h" +#include "env.h" + #include #include #include @@ -150,84 +152,6 @@ struct AWindowHandler #define JNI_STEXCALL(what, method, ...) \ (*p_env)->what(p_env, p_awh->jobj, p_awh->jfields.AWindow.method, ##__VA_ARGS__) -/* - * Android JNIEnv helper - */ - -static pthread_key_t jni_env_key; -static pthread_once_t jni_env_key_once = PTHREAD_ONCE_INIT; - -/* This function is called when a thread attached to the Java VM is canceled or - * exited */ -static void -jni_detach_thread(void *data) -{ - JNIEnv *env = data; - JavaVM *jvm; - - (*env)->GetJavaVM(env, &jvm); - assert(jvm); - (*jvm)->DetachCurrentThread(jvm); -} - -static void jni_env_key_create() -{ - /* Create a TSD area and setup a destroy callback when a thread that - * previously set the jni_env_key is canceled or exited */ - pthread_key_create(&jni_env_key, jni_detach_thread); -} - -static JNIEnv * -android_getEnvCommon(vlc_object_t *p_obj, JavaVM *jvm, const char *psz_name) -{ - assert((p_obj && !jvm) || (!p_obj && jvm)); - - JNIEnv *env; - - pthread_once(&jni_env_key_once, jni_env_key_create); - env = pthread_getspecific(jni_env_key); - if (env == NULL) - { - if (!jvm) - jvm = var_InheritAddress(p_obj, "android-jvm"); - - if (!jvm) - return NULL; - - /* if GetEnv returns JNI_OK, the thread is already attached to the - * JavaVM, so we are already in a java thread, and we don't have to - * setup any destroy callbacks */ - if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2) != JNI_OK) - { - /* attach the thread to the Java VM */ - JavaVMAttachArgs args; - - args.version = JNI_VERSION_1_2; - args.name = psz_name; - args.group = NULL; - - if ((*jvm)->AttachCurrentThread(jvm, &env, &args) != JNI_OK) - return NULL; - - /* Set the attached env to the thread-specific data area (TSD) */ - if (pthread_setspecific(jni_env_key, env) != 0) - { - (*jvm)->DetachCurrentThread(jvm); - return NULL; - } - } - } - - return env; -} - -JNIEnv * -android_getEnv(vlc_object_t *p_obj, const char *psz_name) -{ - return android_getEnvCommon(p_obj, NULL, psz_name); -} - - /* * Android Surface (pre android 2.3) */ diff --git a/modules/video_output/android/utils.h b/modules/video_output/android/utils.h index 48262f42dc..040506b70f 100644 --- a/modules/video_output/android/utils.h +++ b/modules/video_output/android/utils.h @@ -114,15 +114,6 @@ struct vlc_asurfacetexture_operations struct vlc_asurfacetexture *surface); }; -/** - * Attach or get a JNIEnv* - * - * The returned JNIEnv* is created from the android JavaVM attached to the VLC - * object var. - * \return a valid JNIEnv * or NULL. It doesn't need to be released. - */ -JNIEnv *android_getEnv(vlc_object_t *p_obj, const char *psz_thread_name); - /** * Create new AWindowHandler * diff --git a/modules/video_output/opengl/Makefile.am b/modules/video_output/opengl/Makefile.am index 920c9f74ac..8b8b3878ab 100644 --- a/modules/video_output/opengl/Makefile.am +++ b/modules/video_output/opengl/Makefile.am @@ -223,7 +223,8 @@ endif libegl_surfacetexture_plugin_la_SOURCES = video_filter/egl_surfacetexture.c libegl_surfacetexture_plugin_la_CFLAGS = $(AM_CFLAGS) $(EGL_CFLAGS) -DUSE_OPENGL_ES2 -libegl_surfacetexture_plugin_la_LIBADD = $(EGL_LIBS) libandroid_utils.la libvlc_opengles.la +libegl_surfacetexture_plugin_la_LIBADD = $(EGL_LIBS) \ + libandroid_env.la libandroid_utils.la libvlc_opengles.la if HAVE_ANDROID if HAVE_EGL