2 * JNI utility functions
4 * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libavutil/bprint.h"
29 #include "libavutil/log.h"
35 static JavaVM *java_vm = NULL;
36 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
39 * Check if JniInvocation has been initialized. Only available on
42 * @param log_ctx context used for logging, can be NULL
43 * @return 0 on success, < 0 otherwise
45 static int check_jni_invocation(void *log_ctx)
47 int ret = AVERROR_EXTERNAL;
49 void **jni_invocation = NULL;
51 handle = dlopen(NULL, RTLD_LOCAL);
56 jni_invocation = (void **)dlsym(handle, "_ZN13JniInvocation15jni_invocation_E");
57 if (!jni_invocation) {
58 av_log(log_ctx, AV_LOG_ERROR, "Could not find JniInvocation::jni_invocation_ symbol\n");
62 ret = !(jni_invocation != NULL && *jni_invocation != NULL);
73 * Return created Java virtual machine using private JNI_GetCreatedJavaVMs
74 * function from the specified library name.
76 * @param name library name used for symbol lookups, can be NULL
77 * @param log_ctx context used for logging, can be NULL
78 * @return the current Java virtual machine in use
80 static JavaVM *get_java_vm(const char *name, void *log_ctx)
86 jint (*get_created_java_vms) (JavaVM ** vmBuf, jsize bufLen, jsize *nVMs) = NULL;
88 handle = dlopen(name, RTLD_LOCAL);
93 get_created_java_vms = (jint (*)(JavaVM **, jsize, jsize *)) dlsym(handle, "JNI_GetCreatedJavaVMs");
94 if (!get_created_java_vms) {
95 av_log(log_ctx, AV_LOG_ERROR, "Could not find JNI_GetCreatedJavaVMs symbol in library '%s'\n", name);
99 if (get_created_java_vms(&vm, 1, &nb_vm) != JNI_OK) {
100 av_log(log_ctx, AV_LOG_ERROR, "Could not get created Java virtual machines\n");
112 JNIEnv *ff_jni_attach_env(int *attached, void *log_ctx)
119 pthread_mutex_lock(&lock);
120 if (java_vm == NULL && (java_vm = av_jni_get_java_vm(log_ctx)) == NULL) {
122 av_log(log_ctx, AV_LOG_INFO, "Retrieving current Java virtual machine using Android JniInvocation wrapper\n");
123 if (check_jni_invocation(log_ctx) == 0) {
124 if ((java_vm = get_java_vm(NULL, log_ctx)) != NULL ||
125 (java_vm = get_java_vm("libdvm.so", log_ctx)) != NULL ||
126 (java_vm = get_java_vm("libart.so", log_ctx)) != NULL) {
127 av_log(log_ctx, AV_LOG_INFO, "Found Java virtual machine using Android JniInvocation wrapper\n");
131 pthread_mutex_unlock(&lock);
134 av_log(log_ctx, AV_LOG_ERROR, "Could not retrieve a Java virtual machine\n");
138 ret = (*java_vm)->GetEnv(java_vm, (void **)&env, JNI_VERSION_1_6);
141 if ((*java_vm)->AttachCurrentThread(java_vm, &env, NULL) != 0) {
142 av_log(log_ctx, AV_LOG_ERROR, "Failed to attach the JNI environment to the current thread\n");
151 av_log(log_ctx, AV_LOG_ERROR, "The specified JNI version is not supported\n");
154 av_log(log_ctx, AV_LOG_ERROR, "Failed to get the JNI environment attached to this thread");
161 int ff_jni_detach_env(void *log_ctx)
163 if (java_vm == NULL) {
164 av_log(log_ctx, AV_LOG_ERROR, "No Java virtual machine has been registered\n");
165 return AVERROR(EINVAL);
168 return (*java_vm)->DetachCurrentThread(java_vm);
171 char *ff_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, void *log_ctx)
174 const char *utf_chars = NULL;
182 utf_chars = (*env)->GetStringUTFChars(env, string, ©);
183 if ((*env)->ExceptionCheck(env)) {
184 (*env)->ExceptionClear(env);
185 av_log(log_ctx, AV_LOG_ERROR, "String.getStringUTFChars() threw an exception\n");
189 ret = av_strdup(utf_chars);
191 (*env)->ReleaseStringUTFChars(env, string, utf_chars);
192 if ((*env)->ExceptionCheck(env)) {
193 (*env)->ExceptionClear(env);
194 av_log(log_ctx, AV_LOG_ERROR, "String.releaseStringUTFChars() threw an exception\n");
201 jstring ff_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, void *log_ctx)
205 ret = (*env)->NewStringUTF(env, utf_chars);
206 if ((*env)->ExceptionCheck(env)) {
207 (*env)->ExceptionClear(env);
208 av_log(log_ctx, AV_LOG_ERROR, "NewStringUTF() threw an exception\n");
215 int ff_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error, void *log_ctx)
222 char *message = NULL;
224 jclass class_class = NULL;
225 jmethodID get_name_id = NULL;
227 jclass exception_class = NULL;
228 jmethodID get_message_id = NULL;
232 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
234 exception_class = (*env)->GetObjectClass(env, exception);
235 if ((*env)->ExceptionCheck(env)) {
236 (*env)->ExceptionClear(env);
237 av_log(log_ctx, AV_LOG_ERROR, "Could not find Throwable class\n");
238 ret = AVERROR_EXTERNAL;
242 class_class = (*env)->GetObjectClass(env, exception_class);
243 if ((*env)->ExceptionCheck(env)) {
244 (*env)->ExceptionClear(env);
245 av_log(log_ctx, AV_LOG_ERROR, "Could not find Throwable class's class\n");
246 ret = AVERROR_EXTERNAL;
250 get_name_id = (*env)->GetMethodID(env, class_class, "getName", "()Ljava/lang/String;");
251 if ((*env)->ExceptionCheck(env)) {
252 (*env)->ExceptionClear(env);
253 av_log(log_ctx, AV_LOG_ERROR, "Could not find method Class.getName()\n");
254 ret = AVERROR_EXTERNAL;
258 string = (*env)->CallObjectMethod(env, exception_class, get_name_id);
259 if ((*env)->ExceptionCheck(env)) {
260 (*env)->ExceptionClear(env);
261 av_log(log_ctx, AV_LOG_ERROR, "Class.getName() threw an exception\n");
262 ret = AVERROR_EXTERNAL;
267 name = ff_jni_jstring_to_utf_chars(env, string, log_ctx);
268 (*env)->DeleteLocalRef(env, string);
272 get_message_id = (*env)->GetMethodID(env, exception_class, "getMessage", "()Ljava/lang/String;");
273 if ((*env)->ExceptionCheck(env)) {
274 (*env)->ExceptionClear(env);
275 av_log(log_ctx, AV_LOG_ERROR, "Could not find method java/lang/Throwable.getMessage()\n");
276 ret = AVERROR_EXTERNAL;
280 string = (*env)->CallObjectMethod(env, exception, get_message_id);
281 if ((*env)->ExceptionCheck(env)) {
282 (*env)->ExceptionClear(env);
283 av_log(log_ctx, AV_LOG_ERROR, "Throwable.getMessage() threw an exception\n");
284 ret = AVERROR_EXTERNAL;
289 message = ff_jni_jstring_to_utf_chars(env, string, log_ctx);
290 (*env)->DeleteLocalRef(env, string);
294 if (name && message) {
295 av_bprintf(&bp, "%s: %s", name, message);
296 } else if (name && !message) {
297 av_bprintf(&bp, "%s occured", name);
298 } else if (!name && message) {
299 av_bprintf(&bp, "Exception: %s", message);
301 av_log(log_ctx, AV_LOG_WARNING, "Could not retreive exception name and message\n");
302 av_bprintf(&bp, "Exception occured");
305 ret = av_bprint_finalize(&bp, error);
312 (*env)->DeleteLocalRef(env, class_class);
315 if (exception_class) {
316 (*env)->DeleteLocalRef(env, exception_class);
320 (*env)->DeleteLocalRef(env, string);
326 int ff_jni_exception_check(JNIEnv *env, int log, void *log_ctx)
330 jthrowable exception;
332 char *message = NULL;
334 if (!(*(env))->ExceptionCheck((env))) {
339 (*(env))->ExceptionClear((env));
343 exception = (*env)->ExceptionOccurred(env);
344 (*(env))->ExceptionClear((env));
346 if ((ret = ff_jni_exception_get_summary(env, exception, &message, log_ctx)) < 0) {
347 (*env)->DeleteLocalRef(env, exception);
351 (*env)->DeleteLocalRef(env, exception);
353 av_log(log_ctx, AV_LOG_ERROR, "%s\n", message);
359 int ff_jni_init_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
362 jclass last_clazz = NULL;
364 for (i = 0; jfields_mapping[i].name; i++) {
365 int mandatory = jfields_mapping[i].mandatory;
366 enum FFJniFieldType type = jfields_mapping[i].type;
368 if (type == FF_JNI_CLASS) {
373 clazz = (*env)->FindClass(env, jfields_mapping[i].name);
374 if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
378 last_clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) =
379 global ? (*env)->NewGlobalRef(env, clazz) : clazz;
383 ret = AVERROR_EXTERNAL;
389 jfieldID field_id = (*env)->GetFieldID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
390 if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
394 *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = field_id;
397 case FF_JNI_STATIC_FIELD: {
398 jfieldID field_id = (*env)->GetStaticFieldID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
399 if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
403 *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = field_id;
406 case FF_JNI_METHOD: {
407 jmethodID method_id = (*env)->GetMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
408 if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
412 *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
415 case FF_JNI_STATIC_METHOD: {
416 jmethodID method_id = (*env)->GetStaticMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
417 if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
421 *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
425 av_log(log_ctx, AV_LOG_ERROR, "Unknown JNI field type\n");
426 ret = AVERROR(EINVAL);
434 /* reset jfields in case of failure so it does not leak references */
435 ff_jni_reset_jfields(env, jfields, jfields_mapping, global, log_ctx);
441 int ff_jni_reset_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
445 for (i = 0; jfields_mapping[i].name; i++) {
446 enum FFJniFieldType type = jfields_mapping[i].type;
450 jclass clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset);
455 (*env)->DeleteGlobalRef(env, clazz);
457 (*env)->DeleteLocalRef(env, clazz);
460 *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
464 *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
467 case FF_JNI_STATIC_FIELD: {
468 *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
471 case FF_JNI_METHOD: {
472 *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
475 case FF_JNI_STATIC_METHOD: {
476 *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
480 av_log(log_ctx, AV_LOG_ERROR, "Unknown JNI field type\n");