[FFmpeg-devel] [PATCH 1/5] lavu: add JNI support

Matthieu Bouron matthieu.bouron at gmail.com
Fri Oct 9 18:26:49 CEST 2015


From: Matthieu Bouron <matthieu.bouron at stupeflix.com>

---
 configure                |  4 +++
 libavutil/Makefile       |  4 +++
 libavutil/jni.c          | 37 ++++++++++++++++++++++++++
 libavutil/jni.h          | 42 +++++++++++++++++++++++++++++
 libavutil/jni_internal.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/jni_internal.h | 42 +++++++++++++++++++++++++++++
 6 files changed, 198 insertions(+)
 create mode 100644 libavutil/jni.c
 create mode 100644 libavutil/jni.h
 create mode 100644 libavutil/jni_internal.c
 create mode 100644 libavutil/jni_internal.h

diff --git a/configure b/configure
index dba7d46..de5b905 100755
--- a/configure
+++ b/configure
@@ -202,6 +202,7 @@ External library support:
   --enable-gnutls          enable gnutls, needed for https support
                            if openssl is not used [no]
   --disable-iconv          disable iconv [autodetect]
+  --enable-jni             enable JNI support [no]
   --enable-ladspa          enable LADSPA audio filtering [no]
   --enable-libaacplus      enable AAC+ encoding via libaacplus [no]
   --enable-libass          enable libass subtitles rendering,
@@ -1374,6 +1375,7 @@ EXTERNAL_LIBRARY_LIST="
     frei0r
     gnutls
     iconv
+    jni
     ladspa
     libaacplus
     libass
@@ -5263,6 +5265,7 @@ enabled chromaprint       && require chromaprint chromaprint.h chromaprint_get_v
 enabled decklink          && { check_header DeckLinkAPI.h || die "ERROR: DeckLinkAPI.h header not found"; }
 enabled frei0r            && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
 enabled gnutls            && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
+enabled jni               && { check_header jni.h; }
 enabled ladspa            && { check_header ladspa.h || die "ERROR: ladspa.h header not found"; }
 enabled libiec61883       && require libiec61883 libiec61883/iec61883.h iec61883_cmp_connect -lraw1394 -lavc1394 -lrom1394 -liec61883
 enabled libaacplus        && require "libaacplus >= 2.0.0" aacplus.h aacplusEncOpen -laacplus
@@ -6000,6 +6003,7 @@ echo "threading support         ${thread_type-no}"
 echo "safe bitstream reader     ${safe_bitstream_reader-no}"
 echo "SDL support               ${sdl-no}"
 echo "opencl enabled            ${opencl-no}"
+echo "JNI support               ${jni-no}"
 echo "texi2html enabled         ${texi2html-no}"
 echo "perl enabled              ${perl-no}"
 echo "pod2man enabled           ${pod2man-no}"
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 7439750..f7386c6 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -67,6 +67,8 @@ HEADERS-$(CONFIG_LZO)                   += lzo.h
 
 HEADERS-$(CONFIG_OPENCL)                += opencl.h
 
+HEADERS-$(CONFIG_JNI)                   += jni.h
+
 ARCH_HEADERS = bswap.h                                                  \
                intmath.h                                                \
                intreadwrite.h                                           \
@@ -140,6 +142,7 @@ OBJS-$(!HAVE_ATOMICS_NATIVE)            += atomic.o                     \
 
 OBJS-$(CONFIG_LZO)                      += lzo.o
 OBJS-$(CONFIG_OPENCL)                   += opencl.o opencl_internal.o
+OBJS-$(CONFIG_JNI)                      += jni.o jni_internal.o
 
 OBJS += $(COMPAT_OBJS:%=../compat/%)
 
@@ -150,6 +153,7 @@ SKIPHEADERS-$(HAVE_ATOMICS_GCC)        += atomic_gcc.h
 SKIPHEADERS-$(HAVE_ATOMICS_SUNCC)      += atomic_suncc.h
 SKIPHEADERS-$(HAVE_ATOMICS_WIN32)      += atomic_win32.h
 SKIPHEADERS-$(CONFIG_OPENCL)           += opencl.h
+SKIPHEADERS-$(CONFIG_JNI)              += jni_internal.h
 
 TESTPROGS = adler32                                                     \
             aes                                                         \
diff --git a/libavutil/jni.c b/libavutil/jni.c
new file mode 100644
index 0000000..c6ce3a5
--- /dev/null
+++ b/libavutil/jni.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) Stupeflix 2015
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "config.h"
+#include "jni.h"
+#include "log.h"
+
+#include <stdlib.h>
+
+JavaVM *java_vm = NULL;
+
+void av_jni_register_java_vm(JavaVM *vm)
+{
+    java_vm = vm;
+}
+
+JavaVM *av_jni_get_java_vm(void)
+{
+    return java_vm;
+}
diff --git a/libavutil/jni.h b/libavutil/jni.h
new file mode 100644
index 0000000..3c36eaf
--- /dev/null
+++ b/libavutil/jni.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) Stupeflix 2015
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_JNI_H
+#define AVUTIL_JNI_H
+
+#include <jni.h>
+
+/*
+ * Register a java virtual machine that will be used to manage the JNI
+ * environment.
+ *
+ * @param vm java virtual machine
+ */
+void av_jni_register_java_vm(JavaVM *vm);
+
+/*
+ * Get the registered java virtual machine.
+ *
+ * @return the java virtual machine, NULL if no java virtual machine has been
+ * registered
+ */
+JavaVM *av_jni_get_java_vm(void);
+
+#endif /* AVUTIL_JNI_H */
diff --git a/libavutil/jni_internal.c b/libavutil/jni_internal.c
new file mode 100644
index 0000000..b17275d
--- /dev/null
+++ b/libavutil/jni_internal.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) Stupeflix 2015
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "config.h"
+#include "jni.h"
+#include "jni_internal.h"
+#include "log.h"
+
+#include <stdlib.h>
+
+extern JavaVM *java_vm;
+
+JNIEnv *avpriv_jni_attach_env(void *log_ctx)
+{
+    int ret = 0;
+    JNIEnv *env = NULL;
+
+    if (java_vm == NULL) {
+        av_log(log_ctx, AV_LOG_ERROR, "No java virtual machine has been registered\n");
+        return NULL;
+    }
+
+    ret = (*java_vm)->GetEnv(java_vm, (void **)&env, JNI_VERSION_1_6);
+    switch(ret) {
+    case JNI_EDETACHED:
+        if ((*java_vm)->AttachCurrentThread(java_vm, &env, NULL) != 0) {
+            av_log(log_ctx, AV_LOG_ERROR, "Failed to attach the JNI environment to the current thread\n");
+            env = NULL;
+        }
+        break;
+    case JNI_OK:
+        break;
+    case JNI_EVERSION:
+        av_log(log_ctx, AV_LOG_ERROR, "The specified JNI version is not supported\n");
+        break;
+    default:
+        av_log(log_ctx, AV_LOG_ERROR, "Failed to get the JNI environment attached to this thread");
+        break;
+    }
+
+    return env;
+}
+
+int avpriv_jni_detach_env(void *log_ctx)
+{
+    if (java_vm == NULL) {
+        av_log(log_ctx, AV_LOG_ERROR, "No java virtual machine has been registered\n");
+        return AVERROR(EINVAL);
+    }
+
+    return (*java_vm)->DetachCurrentThread(java_vm);
+}
diff --git a/libavutil/jni_internal.h b/libavutil/jni_internal.h
new file mode 100644
index 0000000..6fc1fa6
--- /dev/null
+++ b/libavutil/jni_internal.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) Stupeflix 2015
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_JNI_INTERNAL_H
+#define AVUTIL_JNI_INTERNAL_H
+
+#include <jni.h>
+
+/*
+ * Attach a JNI environment to the current thread.
+ *
+ * @param log_ctx context used for logging, can be NULL
+ * @return the JNI environment on success, NULL otherwise
+ */
+JNIEnv *avpriv_jni_attach_env(void *log_ctx);
+
+/*
+ * Detach the JNI environment from the current thread.
+ *
+ * @param log_ctx context used for logging, can be NULL
+ * @return 0 on success, < 0 otherwise
+ */
+int avpriv_jni_detach_env(void *log_ctx);
+
+#endif /* AVUTIL_JNI_INTERNAL_H */
-- 
2.6.1



More information about the ffmpeg-devel mailing list