History

avcodec: add av_jni_{get,set}_android_app_ctx() helpers

This will allow users to pass the Android ApplicationContext which is mandatory to retrieve the ContentResolver responsible to resolve/open Android content URIS.

Matthieu Bouron committed 2 years ago

Commit f17e18d1 parent 07325133 files · +67 −0Browse files

Unified · Split

3 files
  1. doc/APIchanges +3 −0
  2. libavcodec/jni.c +43 −0
  3. libavcodec/jni.h +21 −0
doc/APIchanges+3−0
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07 · 1 unmodified line
22
33 API changes, most recent first:
44
5+2024-03-xx - xxxxxxxxxx - lavc 61.3.100 - jni.h
6+ Add av_jni_set_android_app_ctx() and av_jni_get_android_app_ctx().
7+
58 2024-03-22 - xxxxxxxxxx - lavu 59.4.100 - frame.h
69 Constified the first-level pointee of av_frame_side_data_get()
710 and renamed it to av_frame_side_data_get_c(). From now on,
libavcodec/jni.c+43−0
@@ -35,6 +35,7 @@ · 34 unmodified lines
3535 #include "ffjni.h"
3636
3737 static void *java_vm;
38+static void *android_app_ctx;
3839 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
3940
4041 int av_jni_set_java_vm(void *vm, void *log_ctx)
@@ -77,3 +78,45 @@ void *av_jni_get_java_vm(void *log_ctx) · 36 unmodified lines
7778 }
7879
7980 #endif
81+
82+#if defined(__ANDROID__)
83+
84+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
85+{
86+#if CONFIG_JNI
87+ JNIEnv *env = ff_jni_get_env(log_ctx);
88+ if (!env)
89+ return AVERROR(EINVAL);
90+
91+ jobjectRefType type = (*env)->GetObjectRefType(env, app_ctx);
92+ if (type != JNIGlobalRefType) {
93+ av_log(log_ctx, AV_LOG_ERROR, "Application context must be passed as a global reference");
94+ return AVERROR(EINVAL);
95+ }
96+
97+ pthread_mutex_lock(&lock);
98+ android_app_ctx = app_ctx;
99+ pthread_mutex_unlock(&lock);
100+
101+ return 0;
102+#else
103+ return AVERROR(ENOSYS);
104+#endif
105+}
106+
107+void *av_jni_get_android_app_ctx(void)
108+{
109+#if CONFIG_JNI
110+ void *ctx;
111+
112+ pthread_mutex_lock(&lock);
113+ ctx = android_app_ctx;
114+ pthread_mutex_unlock(&lock);
115+
116+ return ctx;
117+#else
118+ return NULL;
119+#endif
120+}
121+
122+#endif
libavcodec/jni.h+21−0
@@ -43,4 +43,25 @@ int av_jni_set_java_vm(void *vm, void *log_ctx); · 42 unmodified lines
4343 */
4444 void *av_jni_get_java_vm(void *log_ctx);
4545
46+/*
47+ * Set the Android application context which will be used to retrieve the Android
48+ * content resolver to handle content uris.
49+ *
50+ * This function is only available on Android.
51+ *
52+ * @param app_ctx global JNI reference to the Android application context
53+ * @return 0 on success, < 0 otherwise
54+ */
55+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx);
56+
57+/*
58+ * Get the Android application context that has been set with
59+ * av_jni_set_android_app_ctx.
60+ *
61+ * This function is only available on Android.
62+ *
63+ * @return a pointer the the Android application context
64+ */
65+void *av_jni_get_android_app_ctx(void);
66+
4667 #endif /* AVCODEC_JNI_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t