avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / jni.c
blobfcb483741316ef13f890cbbba17d0b13d58d4b8c
1 /*
2 * JNI public API 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
23 #include "config.h"
25 #include <stdlib.h>
27 #include "libavutil/error.h"
28 #include "jni.h"
30 #if CONFIG_JNI
31 #include <jni.h>
32 #include <pthread.h>
34 #include "libavutil/log.h"
35 #include "ffjni.h"
37 static void *java_vm;
38 static void *android_app_ctx;
39 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
41 int av_jni_set_java_vm(void *vm, void *log_ctx)
43 int ret = 0;
45 pthread_mutex_lock(&lock);
46 if (java_vm == NULL) {
47 java_vm = vm;
48 } else if (java_vm != vm) {
49 ret = AVERROR(EINVAL);
50 av_log(log_ctx, AV_LOG_ERROR, "A Java virtual machine has already been set");
52 pthread_mutex_unlock(&lock);
54 return ret;
57 void *av_jni_get_java_vm(void *log_ctx)
59 void *vm;
61 pthread_mutex_lock(&lock);
62 vm = java_vm;
63 pthread_mutex_unlock(&lock);
65 return vm;
68 #else
70 int av_jni_set_java_vm(void *vm, void *log_ctx)
72 return AVERROR(ENOSYS);
75 void *av_jni_get_java_vm(void *log_ctx)
77 return NULL;
80 #endif
82 #if defined(__ANDROID__)
84 int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
86 #if CONFIG_JNI
87 jobjectRefType type;
89 JNIEnv *env = ff_jni_get_env(log_ctx);
90 if (!env)
91 return AVERROR(EINVAL);
93 type = (*env)->GetObjectRefType(env, app_ctx);
94 if (type != JNIGlobalRefType) {
95 av_log(log_ctx, AV_LOG_ERROR, "Application context must be passed as a global reference");
96 return AVERROR(EINVAL);
99 pthread_mutex_lock(&lock);
100 android_app_ctx = app_ctx;
101 pthread_mutex_unlock(&lock);
103 return 0;
104 #else
105 return AVERROR(ENOSYS);
106 #endif
109 void *av_jni_get_android_app_ctx(void)
111 #if CONFIG_JNI
112 void *ctx;
114 pthread_mutex_lock(&lock);
115 ctx = android_app_ctx;
116 pthread_mutex_unlock(&lock);
118 return ctx;
119 #else
120 return NULL;
121 #endif
124 #endif