diff mbox series

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

Message ID 20240227145536.281991-2-matthieu.bouron@gmail.com
State New
Headers show
Series [FFmpeg-devel,v2,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 Feb. 27, 2024, 2:50 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.
---
 libavcodec/jni.c | 40 ++++++++++++++++++++++++++++++++++++++++
 libavcodec/jni.h | 17 +++++++++++++++++
 2 files changed, 57 insertions(+)

Comments

Andreas Rheinhardt March 4, 2024, 11:30 a.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.
> ---
>  libavcodec/jni.c | 40 ++++++++++++++++++++++++++++++++++++++++
>  libavcodec/jni.h | 17 +++++++++++++++++
>  2 files changed, 57 insertions(+)
> 
> diff --git a/libavcodec/jni.c b/libavcodec/jni.c
> index ae6490de9d..7d04d0a268 100644
> --- a/libavcodec/jni.c
> +++ b/libavcodec/jni.c
> @@ -64,6 +64,36 @@ void *av_jni_get_java_vm(void *log_ctx)
>      return vm;
>  }
>  
> +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> +{
> +    JNIEnv *env = avpriv_jni_get_env(c);

Patch #1 won't compile on its own due to this; you fix this up in patch #2.

> +    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;
> +}
> +
> +void *av_jni_get_android_app_ctx(void)
> +{
> +    void *ctx;
> +
> +    pthread_mutex_lock(&lock);
> +    ctx = android_app_ctx;
> +    pthread_mutex_unlock(&lock);
> +
> +    return ctx;
> +}
> +
>  #else
>  
>  int av_jni_set_java_vm(void *vm, void *log_ctx)
> @@ -76,4 +106,14 @@ void *av_jni_get_java_vm(void *log_ctx)
>      return NULL;
>  }
>  
> +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> +{
> +    return AVERROR(ENOSYS);
> +}
> +
> +void *av_jni_get_android_app_ctx(void)
> +{
> +    return NULL;
> +}

I am against adding stub functions on platforms where they are known to
be useless, i.e. everything except android.

> +
>  #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 */
Matthieu Bouron March 4, 2024, 3:11 p.m. UTC | #2
On Mon, Mar 4, 2024 at 12:29 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.
> > ---
> >  libavcodec/jni.c | 40 ++++++++++++++++++++++++++++++++++++++++
> >  libavcodec/jni.h | 17 +++++++++++++++++
> >  2 files changed, 57 insertions(+)
> >
> > diff --git a/libavcodec/jni.c b/libavcodec/jni.c
> > index ae6490de9d..7d04d0a268 100644
> > --- a/libavcodec/jni.c
> > +++ b/libavcodec/jni.c
> > @@ -64,6 +64,36 @@ void *av_jni_get_java_vm(void *log_ctx)
> >      return vm;
> >  }
> >
> > +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> > +{
> > +    JNIEnv *env = avpriv_jni_get_env(c);
>
> Patch #1 won't compile on its own due to this; you fix this up in patch #2.
>
> > +    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;
> > +}
> > +
> > +void *av_jni_get_android_app_ctx(void)
> > +{
> > +    void *ctx;
> > +
> > +    pthread_mutex_lock(&lock);
> > +    ctx = android_app_ctx;
> > +    pthread_mutex_unlock(&lock);
> > +
> > +    return ctx;
> > +}
> > +
> >  #else
> >
> >  int av_jni_set_java_vm(void *vm, void *log_ctx)
> > @@ -76,4 +106,14 @@ void *av_jni_get_java_vm(void *log_ctx)
> >      return NULL;
> >  }
> >
> > +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> > +{
> > +    return AVERROR(ENOSYS);
> > +}
> > +
> > +void *av_jni_get_android_app_ctx(void)
> > +{
> > +    return NULL;
> > +}
>
> I am against adding stub functions on platforms where they are known to
> be useless, i.e. everything except android.

Ok. Will update the patch introducing a new header "jni_android.h".
Unless we want to also scope avcodec/jni.h to Android only ?

>
> > +
> >  #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 */
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Matthieu Bouron March 4, 2024, 4:35 p.m. UTC | #3
On Mon, Mar 4, 2024 at 4:11 PM Matthieu Bouron
<matthieu.bouron@gmail.com> wrote:
>
> On Mon, Mar 4, 2024 at 12:29 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.
> > > ---
> > >  libavcodec/jni.c | 40 ++++++++++++++++++++++++++++++++++++++++
> > >  libavcodec/jni.h | 17 +++++++++++++++++
> > >  2 files changed, 57 insertions(+)
> > >
> > > diff --git a/libavcodec/jni.c b/libavcodec/jni.c
> > > index ae6490de9d..7d04d0a268 100644
> > > --- a/libavcodec/jni.c
> > > +++ b/libavcodec/jni.c
> > > @@ -64,6 +64,36 @@ void *av_jni_get_java_vm(void *log_ctx)
> > >      return vm;
> > >  }
> > >
> > > +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> > > +{
> > > +    JNIEnv *env = avpriv_jni_get_env(c);
> >
> > Patch #1 won't compile on its own due to this; you fix this up in patch #2.
> >
> > > +    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;
> > > +}
> > > +
> > > +void *av_jni_get_android_app_ctx(void)
> > > +{
> > > +    void *ctx;
> > > +
> > > +    pthread_mutex_lock(&lock);
> > > +    ctx = android_app_ctx;
> > > +    pthread_mutex_unlock(&lock);
> > > +
> > > +    return ctx;
> > > +}
> > > +
> > >  #else
> > >
> > >  int av_jni_set_java_vm(void *vm, void *log_ctx)
> > > @@ -76,4 +106,14 @@ void *av_jni_get_java_vm(void *log_ctx)
> > >      return NULL;
> > >  }
> > >
> > > +int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
> > > +{
> > > +    return AVERROR(ENOSYS);
> > > +}
> > > +
> > > +void *av_jni_get_android_app_ctx(void)
> > > +{
> > > +    return NULL;
> > > +}
> >
> > I am against adding stub functions on platforms where they are known to
> > be useless, i.e. everything except android.
>
> Ok. Will update the patch introducing a new header "jni_android.h".
> Unless we want to also scope avcodec/jni.h to Android only ?

Or maybe you mean adding a ifdef guard to it ?

>
> >
> > > +
> > >  #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 */
> >
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Matthieu Bouron March 4, 2024, 7:36 p.m. UTC | #4
On Mon, Mar 04, 2024 at 05:35:36PM +0100, Matthieu Bouron wrote:
> On Mon, Mar 4, 2024 at 4:11 PM Matthieu Bouron
> <matthieu.bouron@gmail.com> wrote:
> >
> > On Mon, Mar 4, 2024 at 12:29 PM Andreas Rheinhardt
> > <andreas.rheinhardt@outlook.com> wrote:

[...]

> > >
> > > I am against adding stub functions on platforms where they are known to
> > > be useless, i.e. everything except android.
> >
> > Ok. Will update the patch introducing a new header "jni_android.h".
> > Unless we want to also scope avcodec/jni.h to Android only ?
> 
> Or maybe you mean adding a ifdef guard to it ?
> 

New patch attached. The functions are now only built for Android.

[...]
From 1b674e78c6a86090f5c9ed6fef5f47ae83ba98be Mon Sep 17 00:00:00 2001
From: Matthieu Bouron <matthieu.bouron@gmail.com>
Date: Mon, 12 Feb 2024 23:13:09 +0100
Subject: [PATCH v3 1/6] avcodec: add av_jni_{get,set}_android_app_ctx helper

This will allow users to pass the Android ApplicationContext which is mandatory
to retrieve the ContentResolver responsible to resolve/open Android content-uri.
---
 libavcodec/jni.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 libavcodec/jni.h | 17 +++++++++++++++++
 2 files changed, 59 insertions(+)

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 */
diff mbox series

Patch

diff --git a/libavcodec/jni.c b/libavcodec/jni.c
index ae6490de9d..7d04d0a268 100644
--- a/libavcodec/jni.c
+++ b/libavcodec/jni.c
@@ -64,6 +64,36 @@  void *av_jni_get_java_vm(void *log_ctx)
     return vm;
 }
 
+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
+{
+    JNIEnv *env = avpriv_jni_get_env(c);
+    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;
+}
+
+void *av_jni_get_android_app_ctx(void)
+{
+    void *ctx;
+
+    pthread_mutex_lock(&lock);
+    ctx = android_app_ctx;
+    pthread_mutex_unlock(&lock);
+
+    return ctx;
+}
+
 #else
 
 int av_jni_set_java_vm(void *vm, void *log_ctx)
@@ -76,4 +106,14 @@  void *av_jni_get_java_vm(void *log_ctx)
     return NULL;
 }
 
+int av_jni_set_android_app_ctx(void *app_ctx, void *log_ctx)
+{
+    return AVERROR(ENOSYS);
+}
+
+void *av_jni_get_android_app_ctx(void)
+{
+    return NULL;
+}
+
 #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 */