summaryrefslogtreecommitdiffstats
path: root/misc/jni.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:13:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:13:14 +0000
commit60e8a3d404f0640fa5a3f834eae54b4f1fb9127d (patch)
tree1da89a218d0ecf010c67a87cb2f625c4cb18e7d7 /misc/jni.c
parentAdding upstream version 0.37.0. (diff)
downloadmpv-60e8a3d404f0640fa5a3f834eae54b4f1fb9127d.tar.xz
mpv-60e8a3d404f0640fa5a3f834eae54b4f1fb9127d.zip
Adding upstream version 0.38.0.upstream/0.38.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'misc/jni.c')
-rw-r--r--misc/jni.c300
1 files changed, 127 insertions, 173 deletions
diff --git a/misc/jni.c b/misc/jni.c
index 82f6356..0244072 100644
--- a/misc/jni.c
+++ b/misc/jni.c
@@ -20,11 +20,10 @@
*/
#include <libavcodec/jni.h>
-#include <libavutil/mem.h>
-#include <libavutil/bprint.h>
#include <stdlib.h>
#include "jni.h"
+#include "mpv_talloc.h"
#include "osdep/threads.h"
static JavaVM *java_vm;
@@ -46,13 +45,11 @@ static void jni_create_pthread_key(void)
JNIEnv *mp_jni_get_env(struct mp_log *log)
{
- int ret = 0;
JNIEnv *env = NULL;
mp_mutex_lock(&lock);
- if (java_vm == NULL) {
+ if (!java_vm)
java_vm = av_jni_get_java_vm(NULL);
- }
if (!java_vm) {
mp_err(log, "No Java virtual machine has been registered\n");
@@ -61,11 +58,10 @@ JNIEnv *mp_jni_get_env(struct mp_log *log)
mp_exec_once(&once, jni_create_pthread_key);
- if ((env = pthread_getspecific(current_env)) != NULL) {
+ if ((env = pthread_getspecific(current_env)) != NULL)
goto done;
- }
- ret = (*java_vm)->GetEnv(java_vm, (void **)&env, JNI_VERSION_1_6);
+ int 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) {
@@ -92,39 +88,27 @@ done:
char *mp_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, struct mp_log *log)
{
- char *ret = NULL;
- const char *utf_chars = NULL;
-
- jboolean copy = 0;
-
- if (!string) {
+ if (!string)
return NULL;
- }
- utf_chars = (*env)->GetStringUTFChars(env, string, &copy);
+ const char *utf_chars = (*env)->GetStringUTFChars(env, string, NULL);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
- mp_err(log, "String.getStringUTFChars() threw an exception\n");
+ mp_err(log, "getStringUTFChars() threw an exception\n");
return NULL;
}
- ret = av_strdup(utf_chars);
+ char *ret = talloc_strdup(NULL, utf_chars);
(*env)->ReleaseStringUTFChars(env, string, utf_chars);
- if ((*env)->ExceptionCheck(env)) {
- (*env)->ExceptionClear(env);
- mp_err(log, "String.releaseStringUTFChars() threw an exception\n");
- return NULL;
- }
return ret;
}
-jstring mp_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, struct mp_log *log)
+jstring mp_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars,
+ struct mp_log *log)
{
- jstring ret;
-
- ret = (*env)->NewStringUTF(env, utf_chars);
+ jstring ret = (*env)->NewStringUTF(env, utf_chars);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
mp_err(log, "NewStringUTF() threw an exception\n");
@@ -134,24 +118,19 @@ jstring mp_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, struct m
return ret;
}
-int mp_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error, struct mp_log *log)
+int mp_jni_exception_get_summary(JNIEnv *env, jthrowable exception,
+ char **error, struct mp_log *log)
{
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 = NULL;
- av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
+ *error = NULL;
exception_class = (*env)->GetObjectClass(env, exception);
if ((*env)->ExceptionCheck(env)) {
@@ -169,7 +148,7 @@ int mp_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error
goto done;
}
- get_name_id = (*env)->GetMethodID(env, class_class, "getName", "()Ljava/lang/String;");
+ jmethodID get_name_id = (*env)->GetMethodID(env, class_class, "getName", "()Ljava/lang/String;");
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
mp_err(log, "Could not find method Class.getName()\n");
@@ -187,14 +166,13 @@ int mp_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error
if (string) {
name = mp_jni_jstring_to_utf_chars(env, string, log);
- (*env)->DeleteLocalRef(env, string);
- string = NULL;
+ MP_JNI_LOCAL_FREEP(&string);
}
- get_message_id = (*env)->GetMethodID(env, exception_class, "getMessage", "()Ljava/lang/String;");
+ jmethodID get_message_id = (*env)->GetMethodID(env, exception_class, "getMessage", "()Ljava/lang/String;");
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
- mp_err(log, "Could not find method java/lang/Throwable.getMessage()\n");
+ mp_err(log, "Could not find method Throwable.getMessage()\n");
ret = -1;
goto done;
}
@@ -209,164 +187,145 @@ int mp_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error
if (string) {
message = mp_jni_jstring_to_utf_chars(env, string, log);
- (*env)->DeleteLocalRef(env, string);
- string = NULL;
+ MP_JNI_LOCAL_FREEP(&string);
}
if (name && message) {
- av_bprintf(&bp, "%s: %s", name, message);
+ *error = talloc_asprintf(NULL, "%s: %s", name, message);
} else if (name && !message) {
- av_bprintf(&bp, "%s occurred", name);
+ *error = talloc_asprintf(NULL, "%s occurred", name);
} else if (!name && message) {
- av_bprintf(&bp, "Exception: %s", message);
+ *error = talloc_asprintf(NULL, "Exception: %s", message);
} else {
mp_warn(log, "Could not retrieve exception name and message\n");
- av_bprintf(&bp, "Exception occurred");
+ *error = talloc_strdup(NULL, "Exception occurred");
}
- 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);
- }
+ talloc_free(name);
+ talloc_free(message);
- if (string) {
- (*env)->DeleteLocalRef(env, string);
- }
+ MP_JNI_LOCAL_FREEP(&class_class);
+ MP_JNI_LOCAL_FREEP(&exception_class);
+ MP_JNI_LOCAL_FREEP(&string);
return ret;
}
int mp_jni_exception_check(JNIEnv *env, int logging, struct mp_log *log)
{
- int ret;
-
- jthrowable exception;
-
- char *message = NULL;
-
- if (!(*(env))->ExceptionCheck((env))) {
+ if (!(*env)->ExceptionCheck(env))
return 0;
- }
if (!logging) {
- (*(env))->ExceptionClear((env));
+ (*env)->ExceptionClear(env);
return -1;
}
- exception = (*env)->ExceptionOccurred(env);
- (*(env))->ExceptionClear((env));
+ jthrowable exception = (*env)->ExceptionOccurred(env);
+ (*env)->ExceptionClear(env);
- if ((ret = mp_jni_exception_get_summary(env, exception, &message, log)) < 0) {
- (*env)->DeleteLocalRef(env, exception);
+ char *message = NULL;
+ int ret = mp_jni_exception_get_summary(env, exception, &message, log);
+ MP_JNI_LOCAL_FREEP(&exception);
+ if (ret < 0)
return ret;
- }
-
- (*env)->DeleteLocalRef(env, exception);
mp_err(log, "%s\n", message);
- av_free(message);
-
+ talloc_free(message);
return -1;
}
-int mp_jni_init_jfields(JNIEnv *env, void *jfields, const struct MPJniField *jfields_mapping, int global, struct mp_log *log)
+#define CHECK_EXC_MANDATORY() do { \
+ if ((ret = mp_jni_exception_check(env, mandatory, log)) < 0 && \
+ mandatory) { \
+ goto done; \
+ } \
+ } while (0)
+
+int mp_jni_init_jfields(JNIEnv *env, void *jfields,
+ const struct MPJniField *jfields_mapping,
+ int global, struct mp_log *log)
{
- int i, ret = 0;
+ int ret = 0;
jclass last_clazz = NULL;
- for (i = 0; jfields_mapping[i].name; i++) {
- int mandatory = jfields_mapping[i].mandatory;
+ for (int i = 0; jfields_mapping[i].name; i++) {
+ bool mandatory = !!jfields_mapping[i].mandatory;
enum MPJniFieldType type = jfields_mapping[i].type;
- if (type == MP_JNI_CLASS) {
- jclass clazz;
+ void *jfield = (uint8_t*)jfields + jfields_mapping[i].offset;
+ if (type == MP_JNI_CLASS) {
last_clazz = NULL;
- clazz = (*env)->FindClass(env, jfields_mapping[i].name);
- if ((ret = mp_jni_exception_check(env, mandatory, log)) < 0 && mandatory) {
- goto done;
- }
+ jclass clazz = (*env)->FindClass(env, jfields_mapping[i].name);
+ CHECK_EXC_MANDATORY();
- last_clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) =
+ last_clazz = *(jclass*)jfield =
global ? (*env)->NewGlobalRef(env, clazz) : clazz;
- if (global) {
- (*env)->DeleteLocalRef(env, clazz);
- }
-
- } else {
+ if (global)
+ MP_JNI_LOCAL_FREEP(&clazz);
- if (!last_clazz) {
- ret = -1;
- break;
- }
+ continue;
+ }
- switch(type) {
- case MP_JNI_FIELD: {
- jfieldID field_id = (*env)->GetFieldID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
- if ((ret = mp_jni_exception_check(env, mandatory, log)) < 0 && mandatory) {
- goto done;
- }
+ if (!last_clazz) {
+ ret = -1;
+ break;
+ }
- *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = field_id;
- break;
- }
- case MP_JNI_STATIC_FIELD_AS_INT:
- case MP_JNI_STATIC_FIELD: {
- jfieldID field_id = (*env)->GetStaticFieldID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
- if ((ret = mp_jni_exception_check(env, mandatory, log)) < 0 && mandatory) {
- goto done;
- }
+ switch (type) {
+ case MP_JNI_FIELD: {
+ jfieldID field_id = (*env)->GetFieldID(env, last_clazz,
+ jfields_mapping[i].name, jfields_mapping[i].signature);
+ CHECK_EXC_MANDATORY();
- if (type == MP_JNI_STATIC_FIELD_AS_INT) {
- if (field_id) {
- jint value = (*env)->GetStaticIntField(env, last_clazz, field_id);
- if ((ret = mp_jni_exception_check(env, mandatory, log)) < 0 && mandatory) {
- goto done;
- }
- *(jint*)((uint8_t*)jfields + jfields_mapping[i].offset) = value;
- }
- } else {
- *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = field_id;
- }
- break;
- }
- case MP_JNI_METHOD: {
- jmethodID method_id = (*env)->GetMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
- if ((ret = mp_jni_exception_check(env, mandatory, log)) < 0 && mandatory) {
- goto done;
+ *(jfieldID*)jfield = field_id;
+ break;
+ }
+ case MP_JNI_STATIC_FIELD_AS_INT:
+ case MP_JNI_STATIC_FIELD: {
+ jfieldID field_id = (*env)->GetStaticFieldID(env, last_clazz,
+ jfields_mapping[i].name, jfields_mapping[i].signature);
+ CHECK_EXC_MANDATORY();
+
+ if (type == MP_JNI_STATIC_FIELD_AS_INT) {
+ if (field_id) {
+ jint value = (*env)->GetStaticIntField(env, last_clazz, field_id);
+ CHECK_EXC_MANDATORY();
+ *(jint*)jfield = value;
}
-
- *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
- break;
+ } else {
+ *(jfieldID*)jfield = field_id;
}
- case MP_JNI_STATIC_METHOD: {
- jmethodID method_id = (*env)->GetStaticMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
- if ((ret = mp_jni_exception_check(env, mandatory, log)) < 0 && mandatory) {
- goto done;
- }
+ break;
+ }
+ case MP_JNI_METHOD: {
+ jmethodID method_id = (*env)->GetMethodID(env, last_clazz,
+ jfields_mapping[i].name, jfields_mapping[i].signature);
+ CHECK_EXC_MANDATORY();
- *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
- break;
- }
- default:
- mp_err(log, "Unknown JNI field type\n");
- ret = -1;
- goto done;
- }
+ *(jmethodID*)jfield = method_id;
+ break;
+ }
+ case MP_JNI_STATIC_METHOD: {
+ jmethodID method_id = (*env)->GetStaticMethodID(env, last_clazz,
+ jfields_mapping[i].name, jfields_mapping[i].signature);
+ CHECK_EXC_MANDATORY();
- ret = 0;
+ *(jmethodID*)jfield = method_id;
+ break;
+ }
+ default:
+ mp_err(log, "Unknown JNI field type\n");
+ ret = -1;
+ goto done;
}
+
+ ret = 0;
}
done:
@@ -378,48 +337,43 @@ done:
return ret;
}
-int mp_jni_reset_jfields(JNIEnv *env, void *jfields, const struct MPJniField *jfields_mapping, int global, struct mp_log *log)
-{
- int i;
+#undef CHECK_EXC_MANDATORY
- for (i = 0; jfields_mapping[i].name; i++) {
+int mp_jni_reset_jfields(JNIEnv *env, void *jfields,
+ const struct MPJniField *jfields_mapping,
+ int global, struct mp_log *log)
+{
+ for (int i = 0; jfields_mapping[i].name; i++) {
enum MPJniFieldType type = jfields_mapping[i].type;
- switch(type) {
+ void *jfield = (uint8_t*)jfields + jfields_mapping[i].offset;
+
+ switch (type) {
case MP_JNI_CLASS: {
- jclass clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset);
+ jclass clazz = *(jclass*)jfield;
if (!clazz)
continue;
if (global) {
- (*env)->DeleteGlobalRef(env, clazz);
+ MP_JNI_GLOBAL_FREEP(&clazz);
} else {
- (*env)->DeleteLocalRef(env, clazz);
+ MP_JNI_LOCAL_FREEP(&clazz);
}
- *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
- break;
- }
- case MP_JNI_FIELD: {
- *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
- break;
- }
- case MP_JNI_STATIC_FIELD: {
- *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
+ *(jclass*)jfield = NULL;
break;
}
- case MP_JNI_STATIC_FIELD_AS_INT: {
- *(jint*)((uint8_t*)jfields + jfields_mapping[i].offset) = 0;
+ case MP_JNI_FIELD:
+ case MP_JNI_STATIC_FIELD:
+ *(jfieldID*)jfield = NULL;
break;
- }
- case MP_JNI_METHOD: {
- *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
+ case MP_JNI_STATIC_FIELD_AS_INT:
+ *(jint*)jfield = 0;
break;
- }
- case MP_JNI_STATIC_METHOD: {
- *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
+ case MP_JNI_METHOD:
+ case MP_JNI_STATIC_METHOD:
+ *(jmethodID*)jfield = NULL;
break;
- }
default:
mp_err(log, "Unknown JNI field type\n");
}