Browse code

lavc: add JNI support

Matthieu Bouron authored on 2015/09/28 22:18:56
Showing 7 changed files
... ...
@@ -196,6 +196,7 @@ Codecs:
196 196
   interplayvideo.c                      Mike Melanson
197 197
   ivi*                                  Kostya Shishkov
198 198
   jacosub*                              Clément Bœsch
199
+  jni*, ffjni*                          Matthieu Bouron
199 200
   jpeg2000*                             Nicolas Bertrand
200 201
   jpeg_ls.c                             Kostya Shishkov
201 202
   jvdec.c                               Peter Ross
... ...
@@ -207,6 +207,7 @@ External library support:
207 207
   --enable-gnutls          enable gnutls, needed for https support
208 208
                            if openssl is not used [no]
209 209
   --disable-iconv          disable iconv [autodetect]
210
+  --enable-jni             enable JNI support [no]
210 211
   --enable-ladspa          enable LADSPA audio filtering [no]
211 212
   --enable-libass          enable libass subtitles rendering,
212 213
                            needed for subtitles and ass filter [no]
... ...
@@ -1436,6 +1437,7 @@ EXTERNAL_LIBRARY_LIST="
1436 1436
     gmp
1437 1437
     gnutls
1438 1438
     iconv
1439
+    jni
1439 1440
     ladspa
1440 1441
     libass
1441 1442
     libbluray
... ...
@@ -5556,6 +5558,8 @@ enabled decklink          && { check_header DeckLinkAPI.h || die "ERROR: DeckLin
5556 5556
 enabled frei0r            && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
5557 5557
 enabled gmp               && require2 gmp gmp.h mpz_export -lgmp
5558 5558
 enabled gnutls            && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
5559
+enabled jni               && { [ $target_os = "android" ] && check_header jni.h && enabled pthreads &&
5560
+                               check_lib2 "dlfcn.h" dlopen -ldl; }
5559 5561
 enabled ladspa            && { check_header ladspa.h || die "ERROR: ladspa.h header not found"; }
5560 5562
 enabled libiec61883       && require libiec61883 libiec61883/iec61883.h iec61883_cmp_connect -lraw1394 -lavc1394 -lrom1394 -liec61883
5561 5563
 enabled libass            && require_pkg_config libass ass/ass.h ass_library_init
... ...
@@ -6324,6 +6328,7 @@ echo "threading support         ${thread_type-no}"
6324 6324
 echo "safe bitstream reader     ${safe_bitstream_reader-no}"
6325 6325
 echo "SDL support               ${sdl-no}"
6326 6326
 echo "opencl enabled            ${opencl-no}"
6327
+echo "JNI support               ${jni-no}"
6327 6328
 echo "texi2html enabled         ${texi2html-no}"
6328 6329
 echo "perl enabled              ${perl-no}"
6329 6330
 echo "pod2man enabled           ${pod2man-no}"
... ...
@@ -9,6 +9,7 @@ HEADERS = avcodec.h                                                     \
9 9
           d3d11va.h                                                     \
10 10
           dirac.h                                                       \
11 11
           dxva2.h                                                       \
12
+          jni.h                                                         \
12 13
           qsv.h                                                         \
13 14
           vaapi.h                                                       \
14 15
           vda.h                                                         \
... ...
@@ -30,6 +31,7 @@ OBJS = allcodecs.o                                                      \
30 30
        dirac.o                                                          \
31 31
        dv_profile.o                                                     \
32 32
        imgconvert.o                                                     \
33
+       jni.o                                                            \
33 34
        mathtables.o                                                     \
34 35
        options.o                                                        \
35 36
        parser.o                                                         \
... ...
@@ -79,6 +81,7 @@ OBJS-$(CONFIG_IIRFILTER)               += iirfilter.o
79 79
 OBJS-$(CONFIG_IMDCT15)                 += imdct15.o
80 80
 OBJS-$(CONFIG_INTRAX8)                 += intrax8.o intrax8dsp.o
81 81
 OBJS-$(CONFIG_IVIDSP)                  += ivi_dsp.o
82
+OBJS-$(CONFIG_JNI)                     += ffjni.o jni.o
82 83
 OBJS-$(CONFIG_JPEGTABLES)              += jpegtables.o
83 84
 OBJS-$(CONFIG_LIBXVID)                 += libxvid_rc.o
84 85
 OBJS-$(CONFIG_LLAUDDSP)                += lossless_audiodsp.o
... ...
@@ -936,6 +939,7 @@ SKIPHEADERS                            += %_tablegen.h                  \
936 936
 
937 937
 SKIPHEADERS-$(CONFIG_D3D11VA)          += d3d11va.h dxva2_internal.h
938 938
 SKIPHEADERS-$(CONFIG_DXVA2)            += dxva2.h dxva2_internal.h
939
+SKIPHEADERS-$(CONFIG_JNI)              += ffjni.h
939 940
 SKIPHEADERS-$(CONFIG_LIBSCHROEDINGER)  += libschroedinger.h
940 941
 SKIPHEADERS-$(CONFIG_LIBUTVIDEO)       += libutvideo.h
941 942
 SKIPHEADERS-$(CONFIG_LIBVPX)           += libvpx.h
942 943
new file mode 100644
... ...
@@ -0,0 +1,485 @@
0
+/*
1
+ * JNI utility functions
2
+ *
3
+ * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include <dlfcn.h>
23
+#include <jni.h>
24
+#include <pthread.h>
25
+#include <stdlib.h>
26
+
27
+#include "libavutil/bprint.h"
28
+#include "libavutil/log.h"
29
+
30
+#include "config.h"
31
+#include "jni.h"
32
+#include "ffjni.h"
33
+
34
+static JavaVM *java_vm = NULL;
35
+static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
36
+
37
+/**
38
+ * Check if JniInvocation has been initialized. Only available on
39
+ * Android >= 4.4.
40
+ *
41
+ * @param log_ctx context used for logging, can be NULL
42
+ * @return 0 on success, < 0 otherwise
43
+ */
44
+static int check_jni_invocation(void *log_ctx)
45
+{
46
+    int ret = AVERROR_EXTERNAL;
47
+    void *handle = NULL;
48
+    void **jni_invocation = NULL;
49
+
50
+    handle = dlopen(NULL, RTLD_LOCAL);
51
+    if (!handle) {
52
+        goto done;
53
+    }
54
+
55
+    jni_invocation = (void **)dlsym(handle, "_ZN13JniInvocation15jni_invocation_E");
56
+    if (!jni_invocation) {
57
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find JniInvocation::jni_invocation_ symbol\n");
58
+        goto done;
59
+    }
60
+
61
+    ret = !(jni_invocation != NULL && *jni_invocation != NULL);
62
+
63
+done:
64
+    if (handle) {
65
+        dlclose(handle);
66
+    }
67
+
68
+    return ret;
69
+}
70
+
71
+/**
72
+ * Return created Java virtual machine using private JNI_GetCreatedJavaVMs
73
+ * function from the specified library name.
74
+ *
75
+ * @param name library name used for symbol lookups, can be NULL
76
+ * @param log_ctx context used for logging, can be NULL
77
+ * @return the current Java virtual machine in use
78
+ */
79
+static JavaVM *get_java_vm(const char *name, void *log_ctx)
80
+{
81
+    JavaVM *vm = NULL;
82
+    jsize nb_vm = 0;
83
+
84
+    void *handle = NULL;
85
+    jint (*get_created_java_vms) (JavaVM ** vmBuf, jsize bufLen, jsize *nVMs) = NULL;
86
+
87
+    handle = dlopen(name, RTLD_LOCAL);
88
+    if (!handle) {
89
+        return NULL;
90
+    }
91
+
92
+    get_created_java_vms = (jint (*)(JavaVM **, jsize, jsize *)) dlsym(handle, "JNI_GetCreatedJavaVMs");
93
+    if (!get_created_java_vms) {
94
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find JNI_GetCreatedJavaVMs symbol in library '%s'\n", name);
95
+        goto done;
96
+    }
97
+
98
+    if (get_created_java_vms(&vm, 1, &nb_vm) != JNI_OK) {
99
+        av_log(log_ctx, AV_LOG_ERROR, "Could not get created Java virtual machines\n");
100
+        goto done;
101
+    }
102
+
103
+done:
104
+    if (handle) {
105
+        dlclose(handle);
106
+    }
107
+
108
+    return vm;
109
+}
110
+
111
+JNIEnv *ff_jni_attach_env(int *attached, void *log_ctx)
112
+{
113
+    int ret = 0;
114
+    JNIEnv *env = NULL;
115
+
116
+    *attached = 0;
117
+
118
+    pthread_mutex_lock(&lock);
119
+    if (java_vm == NULL && (java_vm = av_jni_get_java_vm(log_ctx)) == NULL) {
120
+
121
+        av_log(log_ctx, AV_LOG_INFO, "Retrieving current Java virtual machine using Android JniInvocation wrapper\n");
122
+        if (check_jni_invocation(log_ctx) == 0) {
123
+            if ((java_vm = get_java_vm(NULL, log_ctx)) != NULL ||
124
+                (java_vm = get_java_vm("libdvm.so", log_ctx)) != NULL ||
125
+                (java_vm = get_java_vm("libart.so", log_ctx)) != NULL) {
126
+                av_log(log_ctx, AV_LOG_INFO, "Found Java virtual machine using Android JniInvocation wrapper\n");
127
+            }
128
+        }
129
+    }
130
+    pthread_mutex_unlock(&lock);
131
+
132
+    if (!java_vm) {
133
+        av_log(log_ctx, AV_LOG_ERROR, "Could not retrieve a Java virtual machine\n");
134
+        return NULL;
135
+    }
136
+
137
+    ret = (*java_vm)->GetEnv(java_vm, (void **)&env, JNI_VERSION_1_6);
138
+    switch(ret) {
139
+    case JNI_EDETACHED:
140
+        if ((*java_vm)->AttachCurrentThread(java_vm, &env, NULL) != 0) {
141
+            av_log(log_ctx, AV_LOG_ERROR, "Failed to attach the JNI environment to the current thread\n");
142
+            env = NULL;
143
+        } else {
144
+            *attached = 1;
145
+        }
146
+        break;
147
+    case JNI_OK:
148
+        break;
149
+    case JNI_EVERSION:
150
+        av_log(log_ctx, AV_LOG_ERROR, "The specified JNI version is not supported\n");
151
+        break;
152
+    default:
153
+        av_log(log_ctx, AV_LOG_ERROR, "Failed to get the JNI environment attached to this thread");
154
+        break;
155
+    }
156
+
157
+    return env;
158
+}
159
+
160
+int ff_jni_detach_env(void *log_ctx)
161
+{
162
+    if (java_vm == NULL) {
163
+        av_log(log_ctx, AV_LOG_ERROR, "No Java virtual machine has been registered\n");
164
+        return AVERROR(EINVAL);
165
+    }
166
+
167
+    return (*java_vm)->DetachCurrentThread(java_vm);
168
+}
169
+
170
+char *ff_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, void *log_ctx)
171
+{
172
+    char *ret = NULL;
173
+    const char *utf_chars = NULL;
174
+
175
+    jboolean copy = 0;
176
+
177
+    if (!string) {
178
+        return NULL;
179
+    }
180
+
181
+    utf_chars = (*env)->GetStringUTFChars(env, string, &copy);
182
+    if ((*env)->ExceptionCheck(env)) {
183
+        (*env)->ExceptionClear(env);
184
+        av_log(log_ctx, AV_LOG_ERROR, "String.getStringUTFChars() threw an exception\n");
185
+        return NULL;
186
+    }
187
+
188
+    ret = av_strdup(utf_chars);
189
+
190
+    (*env)->ReleaseStringUTFChars(env, string, utf_chars);
191
+    if ((*env)->ExceptionCheck(env)) {
192
+        (*env)->ExceptionClear(env);
193
+        av_log(log_ctx, AV_LOG_ERROR, "String.releaseStringUTFChars() threw an exception\n");
194
+        return NULL;;
195
+    }
196
+
197
+    return ret;
198
+}
199
+
200
+jstring ff_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, void *log_ctx)
201
+{
202
+    jstring ret;
203
+
204
+    ret = (*env)->NewStringUTF(env, utf_chars);
205
+    if ((*env)->ExceptionCheck(env)) {
206
+        (*env)->ExceptionClear(env);
207
+        av_log(log_ctx, AV_LOG_ERROR, "NewStringUTF() threw an exception\n");
208
+        return NULL;
209
+    }
210
+
211
+    return ret;
212
+}
213
+
214
+int ff_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error, void *log_ctx)
215
+{
216
+    int ret = 0;
217
+
218
+    AVBPrint bp;
219
+
220
+    char *name = NULL;
221
+    char *message = NULL;
222
+
223
+    jclass class_class = NULL;
224
+    jmethodID get_name_id = NULL;
225
+
226
+    jclass exception_class = NULL;
227
+    jmethodID get_message_id = NULL;
228
+
229
+    jstring string;
230
+
231
+    av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
232
+
233
+    exception_class = (*env)->GetObjectClass(env, exception);
234
+    if ((*env)->ExceptionCheck(env)) {
235
+        (*env)->ExceptionClear(env);
236
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find Throwable class\n");
237
+        ret = AVERROR_EXTERNAL;
238
+        goto done;
239
+    }
240
+
241
+    class_class = (*env)->GetObjectClass(env, exception_class);
242
+    if ((*env)->ExceptionCheck(env)) {
243
+        (*env)->ExceptionClear(env);
244
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find Throwable class's class\n");
245
+        ret = AVERROR_EXTERNAL;
246
+        goto done;
247
+    }
248
+
249
+    get_name_id = (*env)->GetMethodID(env, class_class, "getName", "()Ljava/lang/String;");
250
+    if ((*env)->ExceptionCheck(env)) {
251
+        (*env)->ExceptionClear(env);
252
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find method Class.getName()\n");
253
+        ret = AVERROR_EXTERNAL;
254
+        goto done;
255
+    }
256
+
257
+    string = (*env)->CallObjectMethod(env, exception_class, get_name_id);
258
+    if ((*env)->ExceptionCheck(env)) {
259
+        (*env)->ExceptionClear(env);
260
+        av_log(log_ctx, AV_LOG_ERROR, "Class.getName() threw an exception\n");
261
+        ret = AVERROR_EXTERNAL;
262
+        goto done;
263
+    }
264
+
265
+    if (string) {
266
+        name = ff_jni_jstring_to_utf_chars(env, string, log_ctx);
267
+        (*env)->DeleteLocalRef(env, string);
268
+        string = NULL;
269
+    }
270
+
271
+    get_message_id = (*env)->GetMethodID(env, exception_class, "getMessage", "()Ljava/lang/String;");
272
+    if ((*env)->ExceptionCheck(env)) {
273
+        (*env)->ExceptionClear(env);
274
+        av_log(log_ctx, AV_LOG_ERROR, "Could not find method java/lang/Throwable.getMessage()\n");
275
+        ret = AVERROR_EXTERNAL;
276
+        goto done;
277
+    }
278
+
279
+    string = (*env)->CallObjectMethod(env, exception, get_message_id);
280
+    if ((*env)->ExceptionCheck(env)) {
281
+        (*env)->ExceptionClear(env);
282
+        av_log(log_ctx, AV_LOG_ERROR, "Throwable.getMessage() threw an exception\n");
283
+        ret = AVERROR_EXTERNAL;
284
+        goto done;
285
+    }
286
+
287
+    if (string) {
288
+        message = ff_jni_jstring_to_utf_chars(env, string, log_ctx);
289
+        (*env)->DeleteLocalRef(env, string);
290
+        string = NULL;
291
+    }
292
+
293
+    if (name && message) {
294
+        av_bprintf(&bp, "%s: %s", name, message);
295
+    } else if (name && !message) {
296
+        av_bprintf(&bp, "%s occured", name);
297
+    } else if (!name && message) {
298
+        av_bprintf(&bp, "Exception: %s", message);
299
+    } else {
300
+        av_log(log_ctx, AV_LOG_WARNING, "Could not retreive exception name and message\n");
301
+        av_bprintf(&bp, "Exception occured");
302
+    }
303
+
304
+    ret = av_bprint_finalize(&bp, error);
305
+done:
306
+
307
+    av_free(name);
308
+    av_free(message);
309
+
310
+    if (class_class) {
311
+        (*env)->DeleteLocalRef(env, class_class);
312
+    }
313
+
314
+    if (exception_class) {
315
+        (*env)->DeleteLocalRef(env, exception_class);
316
+    }
317
+
318
+    if (string) {
319
+        (*env)->DeleteLocalRef(env, string);
320
+    }
321
+
322
+    return ret;
323
+}
324
+
325
+int ff_jni_exception_check(JNIEnv *env, int log, void *log_ctx)
326
+{
327
+    int ret;
328
+
329
+    jthrowable exception;
330
+
331
+    char *message = NULL;
332
+
333
+    if (!(*(env))->ExceptionCheck((env))) {
334
+        return 0;
335
+    }
336
+
337
+    if (!log) {
338
+        (*(env))->ExceptionClear((env));
339
+        return -1;
340
+    }
341
+
342
+    exception = (*env)->ExceptionOccurred(env);
343
+    (*(env))->ExceptionClear((env));
344
+
345
+    if ((ret = ff_jni_exception_get_summary(env, exception, &message, log_ctx)) < 0) {
346
+        (*env)->DeleteLocalRef(env, exception);
347
+        return ret;
348
+    }
349
+
350
+    (*env)->DeleteLocalRef(env, exception);
351
+
352
+    av_log(log_ctx, AV_LOG_ERROR, "%s\n", message);
353
+    av_free(message);
354
+
355
+    return -1;
356
+}
357
+
358
+int ff_jni_init_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
359
+{
360
+    int i, ret = 0;
361
+    jclass last_clazz = NULL;
362
+
363
+    for (i = 0; jfields_mapping[i].name; i++) {
364
+        int mandatory = jfields_mapping[i].mandatory;
365
+        enum FFJniFieldType type = jfields_mapping[i].type;
366
+
367
+        if (type == FF_JNI_CLASS) {
368
+            jclass clazz;
369
+
370
+            last_clazz = NULL;
371
+
372
+            clazz = (*env)->FindClass(env, jfields_mapping[i].name);
373
+            if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
374
+                goto done;
375
+            }
376
+
377
+            last_clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) =
378
+                    global ? (*env)->NewGlobalRef(env, clazz) : clazz;
379
+        } else {
380
+
381
+            if (!last_clazz) {
382
+                ret = AVERROR_EXTERNAL;
383
+                break;
384
+            }
385
+
386
+            switch(type) {
387
+            case FF_JNI_FIELD: {
388
+                jfieldID field_id = (*env)->GetFieldID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
389
+                if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
390
+                    goto done;
391
+                }
392
+
393
+                *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = field_id;
394
+                break;
395
+            }
396
+            case FF_JNI_STATIC_FIELD: {
397
+                jfieldID field_id = (*env)->GetStaticFieldID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
398
+                if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
399
+                    goto done;
400
+                }
401
+
402
+                *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = field_id;
403
+                break;
404
+            }
405
+            case FF_JNI_METHOD: {
406
+                jmethodID method_id = (*env)->GetMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
407
+                if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
408
+                    goto done;
409
+                }
410
+
411
+                *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
412
+                break;
413
+            }
414
+            case FF_JNI_STATIC_METHOD: {
415
+                jmethodID method_id = (*env)->GetStaticMethodID(env, last_clazz, jfields_mapping[i].method, jfields_mapping[i].signature);
416
+                if ((ret = ff_jni_exception_check(env, mandatory, log_ctx)) < 0 && mandatory) {
417
+                    goto done;
418
+                }
419
+
420
+                *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = method_id;
421
+                break;
422
+            }
423
+            default:
424
+                av_log(log_ctx, AV_LOG_ERROR, "Unknown JNI field type\n");
425
+                ret = AVERROR(EINVAL);
426
+                goto done;
427
+            }
428
+        }
429
+    }
430
+
431
+done:
432
+    if (ret < 0) {
433
+        /* reset jfields in case of failure so it does not leak references */
434
+        ff_jni_reset_jfields(env, jfields, jfields_mapping, global, log_ctx);
435
+    }
436
+
437
+    return ret;
438
+}
439
+
440
+int ff_jni_reset_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
441
+{
442
+    int i;
443
+
444
+    for (i = 0; jfields_mapping[i].name; i++) {
445
+        enum FFJniFieldType type = jfields_mapping[i].type;
446
+
447
+        switch(type) {
448
+        case FF_JNI_CLASS: {
449
+            jclass clazz = *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset);
450
+            if (!clazz)
451
+                continue;
452
+
453
+            if (global) {
454
+                (*env)->DeleteGlobalRef(env, clazz);
455
+            } else {
456
+                (*env)->DeleteLocalRef(env, clazz);
457
+            }
458
+
459
+            *(jclass*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
460
+            break;
461
+        }
462
+        case FF_JNI_FIELD: {
463
+            *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
464
+            break;
465
+        }
466
+        case FF_JNI_STATIC_FIELD: {
467
+            *(jfieldID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
468
+            break;
469
+        }
470
+        case FF_JNI_METHOD: {
471
+            *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
472
+            break;
473
+        }
474
+        case FF_JNI_STATIC_METHOD: {
475
+            *(jmethodID*)((uint8_t*)jfields + jfields_mapping[i].offset) = NULL;
476
+            break;
477
+        }
478
+        default:
479
+            av_log(log_ctx, AV_LOG_ERROR, "Unknown JNI field type\n");
480
+        }
481
+    }
482
+
483
+    return 0;
484
+}
0 485
new file mode 100644
... ...
@@ -0,0 +1,150 @@
0
+/*
1
+ * JNI utility functions
2
+ *
3
+ * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AVCODEC_FFJNI_H
23
+#define AVCODEC_FFJNI_H
24
+
25
+#include <jni.h>
26
+
27
+/*
28
+ * Attach a JNI environment to the current thread.
29
+ *
30
+ * @param attached pointer to an integer that will be set to 1 if the
31
+ * environment has been attached to the current thread or 0 if it is
32
+ * already attached.
33
+ * @param log_ctx context used for logging, can be NULL
34
+ * @return the JNI environment on success, NULL otherwise
35
+ */
36
+JNIEnv *ff_jni_attach_env(int *attached, void *log_ctx);
37
+
38
+/*
39
+ * Detach the JNI environment from the current thread.
40
+ *
41
+ * @param log_ctx context used for logging, can be NULL
42
+ * @return 0 on success, < 0 otherwise
43
+ */
44
+int ff_jni_detach_env(void *log_ctx);
45
+
46
+/*
47
+ * Convert a jstring to its utf characters equivalent.
48
+ *
49
+ * @param env JNI environment
50
+ * @param string Java string to convert
51
+ * @param log_ctx context used for logging, can be NULL
52
+ * @return a pointer to an array of unicode characters on success, NULL
53
+ * otherwise
54
+ */
55
+char *ff_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, void *log_ctx);
56
+
57
+/*
58
+ * Convert utf chars to its jstring equivalent.
59
+ *
60
+ * @param env JNI environment
61
+ * @param utf_chars a pointer to an array of unicode characters
62
+ * @param log_ctx context used for logging, can be NULL
63
+ * @return a Java string object on success, NULL otherwise
64
+ */
65
+jstring ff_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, void *log_ctx);
66
+
67
+/*
68
+ * Extract the error summary from a jthrowable in the form of "className: errorMessage"
69
+ *
70
+ * @param env JNI environment
71
+ * @param exception exception to get the summary from
72
+ * @param error address pointing to the error, the value is updated if a
73
+ * summary can be extracted
74
+ * @param log_ctx context used for logging, can be NULL
75
+ * @return 0 on success, < 0 otherwise
76
+ */
77
+int ff_jni_exception_get_summary(JNIEnv *env, jthrowable exception, char **error, void *log_ctx);
78
+
79
+/*
80
+ * Check if an exception has occurred,log it using av_log and clear it.
81
+ *
82
+ * @param env JNI environment
83
+ * @param log value used to enable logging if an exception has occurred,
84
+ * 0 disables logging, != 0 enables logging
85
+ * @param log_ctx context used for logging, can be NULL
86
+ */
87
+int ff_jni_exception_check(JNIEnv *env, int log, void *log_ctx);
88
+
89
+/*
90
+ * Jni field type.
91
+ */
92
+enum FFJniFieldType {
93
+
94
+    FF_JNI_CLASS,
95
+    FF_JNI_FIELD,
96
+    FF_JNI_STATIC_FIELD,
97
+    FF_JNI_METHOD,
98
+    FF_JNI_STATIC_METHOD
99
+
100
+};
101
+
102
+/*
103
+ * Jni field describing a class, a field or a method to be retrieved using
104
+ * the ff_jni_init_jfields method.
105
+ */
106
+struct FFJniField {
107
+
108
+    const char *name;
109
+    const char *method;
110
+    const char *signature;
111
+    enum FFJniFieldType type;
112
+    int offset;
113
+    int mandatory;
114
+
115
+};
116
+
117
+/*
118
+ * Retrieve class references, field ids and method ids to an arbitrary structure.
119
+ *
120
+ * @param env JNI environment
121
+ * @param jfields a pointer to an arbitrary structure where the different
122
+ * fields are declared and where the FFJNIField mapping table offsets are
123
+ * pointing to
124
+ * @param jfields_mapping null terminated array of FFJNIFields describing
125
+ * the class/field/method to be retrieved
126
+ * @param global make the classes references global. It is the caller
127
+ * responsibility to properly release global references.
128
+ * @param log_ctx context used for logging, can be NULL
129
+ * @return 0 on success, < 0 otherwise
130
+ */
131
+int ff_jni_init_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx);
132
+
133
+/*
134
+ * Delete class references, field ids and method ids of an arbitrary structure.
135
+ *
136
+ * @param env JNI environment
137
+ * @param jfields a pointer to an arbitrary structure where the different
138
+ * fields are declared and where the FFJNIField mapping table offsets are
139
+ * pointing to
140
+ * @param jfields_mapping null terminated array of FFJNIFields describing
141
+ * the class/field/method to be deleted
142
+ * @param global threat the classes references as global and delete them
143
+ * accordingly
144
+ * @param log_ctx context used for logging, can be NULL
145
+ * @return 0 on success, < 0 otherwise
146
+ */
147
+int ff_jni_reset_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx);
148
+
149
+#endif /* AVCODEC_FFJNI_H */
0 150
new file mode 100644
... ...
@@ -0,0 +1,80 @@
0
+/*
1
+ * JNI public API functions
2
+ *
3
+ * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include <stdlib.h>
23
+
24
+#include "config.h"
25
+#include "jni.h"
26
+
27
+#if CONFIG_JNI
28
+
29
+#include <errno.h>
30
+#include <jni.h>
31
+#include <pthread.h>
32
+
33
+#include "libavutil/log.h"
34
+#include "libavutil/error.h"
35
+#include "ffjni.h"
36
+
37
+void *java_vm;
38
+pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
39
+
40
+int av_jni_set_java_vm(void *vm, void *log_ctx)
41
+{
42
+    int ret = 0;
43
+
44
+    pthread_mutex_lock(&lock);
45
+    if (java_vm == NULL) {
46
+        java_vm = vm;
47
+    } else if (java_vm != vm) {
48
+        ret = AVERROR(EINVAL);
49
+        av_log(log_ctx, AV_LOG_ERROR, "A Java virtual machine has already been set");
50
+    }
51
+    pthread_mutex_unlock(&lock);
52
+
53
+    return ret;
54
+}
55
+
56
+void *av_jni_get_java_vm(void *log_ctx)
57
+{
58
+    void *vm;
59
+
60
+    pthread_mutex_lock(&lock);
61
+    vm = java_vm;
62
+    pthread_mutex_unlock(&lock);
63
+
64
+    return vm;
65
+}
66
+
67
+#else
68
+
69
+int av_jni_set_java_vm(void *vm, void *log_ctx)
70
+{
71
+    return 0;
72
+}
73
+
74
+void *av_jni_get_java_vm(void *log_ctx)
75
+{
76
+    return NULL;
77
+}
78
+
79
+#endif
0 80
new file mode 100644
... ...
@@ -0,0 +1,46 @@
0
+/*
1
+ * JNI public API functions
2
+ *
3
+ * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AVCODEC_JNI_H
23
+#define AVCODEC_JNI_H
24
+
25
+/*
26
+ * Manually set a Java virtual machine which will be used to retrieve the JNI
27
+ * environment. Once a Java VM is set it cannot be changed afterwards, meaning
28
+ * you can call multiple times av_jni_set_java_vm with the same Java VM pointer
29
+ * however it will error out if you try to set a different Java VM.
30
+ *
31
+ * @param vm Java virtual machine
32
+ * @param log_ctx context used for logging, can be NULL
33
+ * @return 0 on success, < 0 otherwise
34
+ */
35
+int av_jni_set_java_vm(void *vm, void *log_ctx);
36
+
37
+/*
38
+ * Get the Java virtual machine which has been set with av_jni_set_java_vm.
39
+ *
40
+ * @param vm Java virtual machine
41
+ * @return a pointer to the Java virtual machine
42
+ */
43
+void *av_jni_get_java_vm(void *log_ctx);
44
+
45
+#endif /* AVCODEC_JNI_H */