diff mbox series

[FFmpeg-devel,v4,1/6] avcodec: add av_jni_{get, set}_android_app_ctx helper

Message ID 20240317223102.49414-2-matthieu.bouron@gmail.com
State New
Headers show
Series [FFmpeg-devel,v4,1/6] avcodec: add av_jni_{get, set}_android_app_ctx helper | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Matthieu Bouron March 17, 2024, 10:28 p.m. UTC
This will allow users to pass the Android ApplicationContext which is mandatory
to retrieve the ContentResolver responsible to resolve/open Android content-uri.
---
 doc/APIchanges   |  3 +++
 libavcodec/jni.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 libavcodec/jni.h | 17 +++++++++++++++++
 3 files changed, 62 insertions(+)

Comments

Andreas Rheinhardt March 17, 2024, 10:38 p.m. UTC | #1
Matthieu Bouron:
> This will allow users to pass the Android ApplicationContext which is mandatory
> to retrieve the ContentResolver responsible to resolve/open Android content-uri.
> ---
>  doc/APIchanges   |  3 +++
>  libavcodec/jni.c | 42 ++++++++++++++++++++++++++++++++++++++++++
>  libavcodec/jni.h | 17 +++++++++++++++++
>  3 files changed, 62 insertions(+)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index a44c8e4f10..ae1868047e 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07
>  
>  API changes, most recent first:
>  
> +2024-03-xx - xxxxxxxxxx - lavc 61.2.100 - jni.h
> +  Add av_jni_set_android_app_ctx() and av_jni_get_android_app_ctx().
> +
>  2024-03-xx - xxxxxxxxxx - lavu 59.2.100 - channel_layout.h
>    Add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL.
>  
> diff --git a/libavcodec/jni.c b/libavcodec/jni.c
> index ae6490de9d..cfe95bd1ec 100644
> --- a/libavcodec/jni.c
> +++ b/libavcodec/jni.c
> @@ -77,3 +77,45 @@ void *av_jni_get_java_vm(void *log_ctx)
>  }
>  
>  #endif
> +
> +#if defined(__ANDROID__)
> +
> +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> +{
> +#if CONFIG_JNI
> +    JNIEnv *env = ff_jni_get_env(log_ctx);
> +    if (!env)
> +        return AVERROR(EINVAL);
> +
> +    jobjectRefType type = (*env)->GetObjectRefType(env, app_ctx);
> +    if (type != JNIGlobalRefType) {
> +        av_log(log_ctx, AV_LOG_ERROR, "Application context must be passed as a global reference");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    pthread_mutex_lock(&lock);
> +    android_app_ctx = app_ctx;
> +    pthread_mutex_unlock(&lock);
> +
> +    return 0;
> +#else
> +    return AVERROR(ENOSYS);
> +#endif
> +}
> +
> +void *av_jni_get_android_app_ctx(void)
> +{
> +#if CONFIG_JNI
> +    void *ctx;
> +
> +    pthread_mutex_lock(&lock);
> +    ctx = android_app_ctx;
> +    pthread_mutex_unlock(&lock);
> +
> +    return ctx;
> +#else
> +    return NULL;
> +#endif
> +}
> +
> +#endif
> diff --git a/libavcodec/jni.h b/libavcodec/jni.h
> index dd99e92611..da8025f830 100644
> --- a/libavcodec/jni.h
> +++ b/libavcodec/jni.h
> @@ -43,4 +43,21 @@ int av_jni_set_java_vm(void *vm, void *log_ctx);
>   */
>  void *av_jni_get_java_vm(void *log_ctx);
>  
> +/*
> + * Set the Android application context which will be used to retrieve the Android
> + * content resolver to resolve content uris.
> + *
> + * @param app_ctx global JNI reference to the Android application context
> + * @return 0 on success, < 0 otherwise
> + */
> +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx);
> +
> +/*
> + * Get the Android application context that has been set with
> + * av_jni_set_android_app_ctx.
> + *
> + * @return a pointer the the Android application context
> + */
> +void *av_jni_get_android_app_ctx(void);

This should mention that these functions are only available on android.

> +
>  #endif /* AVCODEC_JNI_H */
Matthieu Bouron March 17, 2024, 10:43 p.m. UTC | #2
On Sun, Mar 17, 2024 at 11:38 PM Andreas Rheinhardt
<andreas.rheinhardt@outlook.com> wrote:
>
> Matthieu Bouron:
> > This will allow users to pass the Android ApplicationContext which is mandatory
> > to retrieve the ContentResolver responsible to resolve/open Android content-uri.
> > ---
> >  doc/APIchanges   |  3 +++
> >  libavcodec/jni.c | 42 ++++++++++++++++++++++++++++++++++++++++++
> >  libavcodec/jni.h | 17 +++++++++++++++++
> >  3 files changed, 62 insertions(+)
> >
> > diff --git a/doc/APIchanges b/doc/APIchanges
> > index a44c8e4f10..ae1868047e 100644
> > --- a/doc/APIchanges
> > +++ b/doc/APIchanges
> > @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07
> >
> >  API changes, most recent first:
> >
> > +2024-03-xx - xxxxxxxxxx - lavc 61.2.100 - jni.h
> > +  Add av_jni_set_android_app_ctx() and av_jni_get_android_app_ctx().
> > +
> >  2024-03-xx - xxxxxxxxxx - lavu 59.2.100 - channel_layout.h
> >    Add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL.
> >
> > diff --git a/libavcodec/jni.c b/libavcodec/jni.c
> > index ae6490de9d..cfe95bd1ec 100644
> > --- a/libavcodec/jni.c
> > +++ b/libavcodec/jni.c
> > @@ -77,3 +77,45 @@ void *av_jni_get_java_vm(void *log_ctx)
> >  }
> >
> >  #endif
> > +
> > +#if defined(__ANDROID__)
> > +
> > +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> > +{
> > +#if CONFIG_JNI
> > +    JNIEnv *env = ff_jni_get_env(log_ctx);
> > +    if (!env)
> > +        return AVERROR(EINVAL);
> > +
> > +    jobjectRefType type = (*env)->GetObjectRefType(env, app_ctx);
> > +    if (type != JNIGlobalRefType) {
> > +        av_log(log_ctx, AV_LOG_ERROR, "Application context must be passed as a global reference");
> > +        return AVERROR(EINVAL);
> > +    }
> > +
> > +    pthread_mutex_lock(&lock);
> > +    android_app_ctx = app_ctx;
> > +    pthread_mutex_unlock(&lock);
> > +
> > +    return 0;
> > +#else
> > +    return AVERROR(ENOSYS);
> > +#endif
> > +}
> > +
> > +void *av_jni_get_android_app_ctx(void)
> > +{
> > +#if CONFIG_JNI
> > +    void *ctx;
> > +
> > +    pthread_mutex_lock(&lock);
> > +    ctx = android_app_ctx;
> > +    pthread_mutex_unlock(&lock);
> > +
> > +    return ctx;
> > +#else
> > +    return NULL;
> > +#endif
> > +}
> > +
> > +#endif
> > diff --git a/libavcodec/jni.h b/libavcodec/jni.h
> > index dd99e92611..da8025f830 100644
> > --- a/libavcodec/jni.h
> > +++ b/libavcodec/jni.h
> > @@ -43,4 +43,21 @@ int av_jni_set_java_vm(void *vm, void *log_ctx);
> >   */
> >  void *av_jni_get_java_vm(void *log_ctx);
> >
> > +/*
> > + * Set the Android application context which will be used to retrieve the Android
> > + * content resolver to resolve content uris.
> > + *
> > + * @param app_ctx global JNI reference to the Android application context
> > + * @return 0 on success, < 0 otherwise
> > + */
> > +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx);
> > +
> > +/*
> > + * Get the Android application context that has been set with
> > + * av_jni_set_android_app_ctx.
> > + *
> > + * @return a pointer the the Android application context
> > + */
> > +void *av_jni_get_android_app_ctx(void);
>
> This should mention that these functions are only available on android.

Added the following chunk locally to both functions:

  *
+ * This function is only available on Android.
+ *
diff mbox series

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index a44c8e4f10..ae1868047e 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@  The last version increases of all libraries were on 2024-03-07
 
 API changes, most recent first:
 
+2024-03-xx - xxxxxxxxxx - lavc 61.2.100 - jni.h
+  Add av_jni_set_android_app_ctx() and av_jni_get_android_app_ctx().
+
 2024-03-xx - xxxxxxxxxx - lavu 59.2.100 - channel_layout.h
   Add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL.
 
diff --git a/libavcodec/jni.c b/libavcodec/jni.c
index ae6490de9d..cfe95bd1ec 100644
--- a/libavcodec/jni.c
+++ b/libavcodec/jni.c
@@ -77,3 +77,45 @@  void *av_jni_get_java_vm(void *log_ctx)
 }
 
 #endif
+
+#if defined(__ANDROID__)
+
+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
+{
+#if CONFIG_JNI
+    JNIEnv *env = ff_jni_get_env(log_ctx);
+    if (!env)
+        return AVERROR(EINVAL);
+
+    jobjectRefType type = (*env)->GetObjectRefType(env, app_ctx);
+    if (type != JNIGlobalRefType) {
+        av_log(log_ctx, AV_LOG_ERROR, "Application context must be passed as a global reference");
+        return AVERROR(EINVAL);
+    }
+
+    pthread_mutex_lock(&lock);
+    android_app_ctx = app_ctx;
+    pthread_mutex_unlock(&lock);
+
+    return 0;
+#else
+    return AVERROR(ENOSYS);
+#endif
+}
+
+void *av_jni_get_android_app_ctx(void)
+{
+#if CONFIG_JNI
+    void *ctx;
+
+    pthread_mutex_lock(&lock);
+    ctx = android_app_ctx;
+    pthread_mutex_unlock(&lock);
+
+    return ctx;
+#else
+    return NULL;
+#endif
+}
+
+#endif
diff --git a/libavcodec/jni.h b/libavcodec/jni.h
index dd99e92611..da8025f830 100644
--- a/libavcodec/jni.h
+++ b/libavcodec/jni.h
@@ -43,4 +43,21 @@  int av_jni_set_java_vm(void *vm, void *log_ctx);
  */
 void *av_jni_get_java_vm(void *log_ctx);
 
+/*
+ * Set the Android application context which will be used to retrieve the Android
+ * content resolver to resolve content uris.
+ *
+ * @param app_ctx global JNI reference to the Android application context
+ * @return 0 on success, < 0 otherwise
+ */
+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx);
+
+/*
+ * Get the Android application context that has been set with
+ * av_jni_set_android_app_ctx.
+ *
+ * @return a pointer the the Android application context
+ */
+void *av_jni_get_android_app_ctx(void);
+
 #endif /* AVCODEC_JNI_H */