[FFmpeg-devel] [PATCH 2/5] lavu/jni: add more JNI helper

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


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

---
 libavutil/jni_internal.c | 290 +++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/jni_internal.h | 101 +++++++++++++++++
 2 files changed, 391 insertions(+)

diff --git a/libavutil/jni_internal.c b/libavutil/jni_internal.c
index b17275d..2c1dc70 100644
--- a/libavutil/jni_internal.c
+++ b/libavutil/jni_internal.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "bprint.h"
 #include "config.h"
 #include "jni.h"
 #include "jni_internal.h"
@@ -67,3 +68,292 @@ int avpriv_jni_detach_env(void *log_ctx)
 
     return (*java_vm)->DetachCurrentThread(java_vm);
 }
+
+char *avpriv_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, void *log_ctx)
+{
+    char *ret = NULL;
+    const char *utf_chars = NULL;
+
+    jboolean copy = 0;
+
+    utf_chars = (*env)->GetStringUTFChars(env, string, &copy);
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "String.getStringUTFChars() threw an exception\n");
+        return NULL;
+    }
+
+    ret = av_strdup(utf_chars);
+
+    (*env)->ReleaseStringUTFChars(env, string, utf_chars);
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "String.releaseStringUTFChars() threw an exception\n");
+        return NULL;;
+    }
+
+    return ret;
+}
+
+jstring avpriv_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, void *log_ctx)
+{
+    jstring ret;
+
+    ret = (*env)->NewStringUTF(env, utf_chars);
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "NewStringUTF() threw an exception\n");
+        return NULL;
+    }
+
+    return ret;
+}
+
+int avpriv_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error, void *log_ctx)
+{
+    int ret = 0;
+
+    AVBPrint bp;
+
+    char *name = NULL;
+    char *message = NULL;
+
+    jclass class_class = NULL;
+    jmethodID get_name_id = NULL;
+
+    jclass exception_class = NULL;
+    jmethodID get_message_id = NULL;
+
+    jstring string;
+
+    av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
+
+    exception_class = (*env)->GetObjectClass(env, exception);
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find Throwable class\n");
+        ret = AVERROR_EXTERNAL;
+        goto done;
+    }
+
+    class_class = (*env)->GetObjectClass(env, exception_class);
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find Throwable class's class\n");
+        ret = AVERROR_EXTERNAL;
+        goto done;
+    }
+
+    get_name_id = (*env)->GetMethodID(env, class_class, "getName", "()Ljava/lang/String;");
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find method Class.getName()\n");
+        ret = AVERROR_EXTERNAL;
+        goto done;
+    }
+
+    string = (*env)->CallObjectMethod(env, exception_class, get_name_id);
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "Class.getName() threw an exception\n");
+        ret = AVERROR_EXTERNAL;
+        goto done;
+    }
+
+    name = avpriv_jni_jstring_to_utf_chars(env, string, log_ctx);
+    if (!name) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+
+    (*env)->DeleteLocalRef(env, string);
+
+    get_message_id = (*env)->GetMethodID(env, exception_class, "getMessage", "()Ljava/lang/String;");
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find method java/lang/Throwable.getMessage()\n");
+        ret = AVERROR_EXTERNAL;
+        goto done;
+    }
+
+    string = (*env)->CallObjectMethod(env, exception, get_message_id);
+    if ((*env)->ExceptionCheck(env)) {
+        (*env)->ExceptionClear(env);
+        av_log(log_ctx, AV_LOG_ERROR, "Throwable.getMessage() threw an exception\n");
+        ret = AVERROR_EXTERNAL;
+        goto done;
+    }
+
+    message = avpriv_jni_jstring_to_utf_chars(env, string, log_ctx);
+    if (!message) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+
+    (*env)->DeleteLocalRef(env, string);
+
+    av_bprintf(&bp, "%s: %s", name, message);
+    ret = av_bprint_finalize(&bp, error);
+
+done:
+
+    av_free(name);
+    av_free(message);
+
+    if (class_class) {
+        (*env)->DeleteLocalRef(env, class_class);
+    }
+
+    if (exception_class) {
+        (*env)->DeleteLocalRef(env, exception_class);
+    }
+
+    return ret;
+}
+
+int avpriv_jni_exception_check(JNIEnv *env, int log, void *log_ctx)
+{
+    int ret;
+
+    jthrowable exception;
+
+    char *message;
+
+    if (!(*(env))->ExceptionCheck((env))) {
+        return 0;
+    }
+
+    if (!log) {
+        (*(env))->ExceptionClear((env));
+        return -1;
+    }
+
+    exception = (*env)->ExceptionOccurred(env);
+    (*(env))->ExceptionClear((env));
+
+    if ((ret = avpriv_jni_exception_get_summary(env, exception, &message, log_ctx)) < 0) {
+        return ret;
+    }
+
+    (*env)->DeleteLocalRef(env, exception);
+
+    av_log(log_ctx, AV_LOG_ERROR, "%s\n", message);
+    av_free(message);
+
+    return -1;
+}
+
+int avpriv_jni_init_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
+{
+    int i, ret = 0;
+    jclass last_clazz = NULL;
+
+    for (i = 0; jfields_mapping[i].name; i++) {
+        int mandatory = jfields_mapping[i].mandatory;
+        enum FFJniFieldType type = jfields_mapping[i].type;
+
+        if (type == FF_JNI_CLASS) {
+            jclass clazz;
+
+            last_clazz = NULL;
+
+            clazz = (*env)->FindClass(env, jfields_mapping[i].name);
+            if ((ret = avpriv_jni_exception_check(env, mandatory, log_ctx) && mandatory) < 0) {
+                goto done;
+            }
+
+            last_clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) =
+                    global ? (*env)->NewGlobalRef(env, clazz) : clazz;
+        } else {
+
+            if (!last_clazz) {
+                ret = AVERROR_EXTERNAL;
+                break;
+            }
+
+            switch(type) {
+            case (FF_JNI_FIELD): {
+                jfieldID field_id = (*env)->GetFieldID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
+                if ((ret = avpriv_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
+                    goto done;
+                }
+
+                *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = field_id;
+                break;
+            }
+            case (FF_JNI_METHOD): {
+                jmethodID method_id = (*env)->GetMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
+                if ((ret = avpriv_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
+                    goto done;
+                }
+
+                *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
+                break;
+            }
+            case (FF_JNI_STATIC_METHOD): {
+                jmethodID method_id = (*env)->GetStaticMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
+                if ((ret = avpriv_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
+                    goto done;
+                }
+
+                *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
+                break;
+            }
+            default:
+                av_log(log_ctx, AV_LOG_ERROR, "Unknown JNI field type\n");
+                ret = AVERROR(EINVAL);
+                goto done;
+            }
+        }
+    }
+
+done:
+    if (ret < 0) {
+        /* reset jfields in case of failure so it does not leak references */
+        avpriv_jni_reset_jfields(env, jfields, jfields_mapping, global, log_ctx);
+    }
+
+    return ret;
+}
+
+int avpriv_jni_reset_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
+{
+    int i;
+
+    for (i = 0; jfields_mapping[i].name; i++) {
+        enum FFJniFieldType type = jfields_mapping[i].type;
+
+        switch(type) {
+        case (FF_JNI_CLASS): {
+            jclass clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset);
+            if (!clazz)
+                continue;
+
+            if (global) {
+                (*env)->DeleteGlobalRef(env, clazz);
+            } else {
+                (*env)->DeleteLocalRef(env, clazz);
+            }
+
+            *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
+            break;
+        }
+        case (FF_JNI_FIELD): {
+            *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
+            break;
+        }
+        case (FF_JNI_METHOD): {
+            *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
+            break;
+        }
+        case (FF_JNI_STATIC_METHOD): {
+            *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
+            break;
+        }
+        default:
+            av_log(log_ctx, AV_LOG_ERROR, "Unknown JNI field type\n");
+        }
+    }
+
+    return 0;
+}
diff --git a/libavutil/jni_internal.h b/libavutil/jni_internal.h
index 6fc1fa6..3411c78 100644
--- a/libavutil/jni_internal.h
+++ b/libavutil/jni_internal.h
@@ -39,4 +39,105 @@ JNIEnv *avpriv_jni_attach_env(void *log_ctx);
  */
 int avpriv_jni_detach_env(void *log_ctx);
 
+/*
+ * Convert a jstring to its utf characters equivalent.
+ *
+ * @param env JNI environment
+ * @param string java string to convert
+ * @param log_ctx context used for logging, can be NULL
+ * @return a pointer to an array of unicode caracters on success, NULL
+ * otherwise
+ */
+char *avpriv_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, void *log_ctx);
+
+/*
+ * Convert utf chars to its jstring equivalent.
+ *
+ * @param env JNI environment
+ * @param utf_chars a pointer to an array of unicode caracters
+ * @param log_ctx context used for logging, can be NULL
+ * @return a java string object on success, NULL otherwise
+ */
+jstring avpriv_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, void *log_ctx);
+
+/*
+ * Extract the error summary from a jthrowable in the form of "className: errorMesage"
+ *
+ * @param env JNI environment
+ * @param exception exception to get the summary from
+ * @param error address pointing to the error, the value is updated if a
+ * summary can be extracted
+ * @param log_ctx context used for logging, can be NULL
+ * @return 0 on success, < 0 otherwise
+ */
+int avpriv_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error, void *log_ctx);
+
+/*
+ * Check if an exception has occurred,log it using av_log and clear it.
+ *
+ * @param env JNI environment
+ * @param log value used to enable logging if an exception has occured,
+ * 0 disables logging, != 0 enables logging
+ * @param log_ctx context used for logging, can be NULL
+ */
+int avpriv_jni_exception_check(JNIEnv *env, int log, void *log_ctx);
+
+/*
+ * Jni field type.
+ */
+enum FFJniFieldType {
+
+    FF_JNI_CLASS,
+    FF_JNI_FIELD,
+    FF_JNI_METHOD,
+    FF_JNI_STATIC_METHOD
+
+} MemberType;
+
+/*
+ * Jni field describing a class, a field or a method to be retrived using
+ * the avpriv_jni_init_jfields method.
+ */
+struct FFJniField {
+
+    const char *name;
+    const char *method;
+    const char *signature;
+    enum FFJniFieldType type;
+    int offset;
+    int mandatory;
+
+};
+
+/*
+ * Retreive class references, field ids and method ids to an arbitrary structure.
+ *
+ * @param env JNI environment
+ * @param jfields a pointer to an arbitrary structure where the different
+ * fields are declared and where the FFJNIField mapping table offsets are
+ * pointing to
+ * @param jfields_mapping null terminated array of FFJNIFields describing
+ * the class/field/method to be retrived
+ * @param global, wheter or not to make the classes reference global, it is
+ * the caller responsability to properly release global references.
+ * @param log_ctx context used for logging, can be NULL
+ * @return 0 on success, < 0 otherwise
+ */
+int avpriv_jni_init_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx);
+
+/*
+ * Delete class references, field ids and method ids of an arbitrary structure.
+ *
+ * @param env JNI environment
+ * @param jfields a pointer to an arbitrary structure where the different
+ * fields are declared and where the FFJNIField mapping table offsets are
+ * pointing to
+ * @param jfields_mapping null terminated array of FFJNIFields describing
+ * the class/field/method to be deleted
+ * @param global, wheter or not the classes reference are global
+ * @param log_ctx context used for logging, can be NULL
+ * @return 0 on success, < 0 otherwise
+ */
+int avpriv_jni_reset_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx);
+
 #endif /* AVUTIL_JNI_INTERNAL_H */
-- 
2.6.1



More information about the ffmpeg-devel mailing list