[vlc-devel] [PATCHv2 06/14] android: utils: load Surface JNI API

Alexandre Janniaux ajanni at videolabs.io
Wed Jun 24 15:14:30 CEST 2020


When creating a SurfaceTexture JNI object, we can, with recent API,
transform the SurfaceTexture into an ASurfaceTexture NDK object.
However, with previous Android version, the only way is to create the
Surface JNI object directly from the SurfaceTexture JNI object, and then
convert the Surface into a ANativeWindow NDK object for the producer
side.

This patch loads the class and the constructor taking a reference to
SurfaceTexture.
---
 modules/video_output/android/utils.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/modules/video_output/android/utils.c b/modules/video_output/android/utils.c
index 92d3abdf88d..5ec872f2e8a 100644
--- a/modules/video_output/android/utils.c
+++ b/modules/video_output/android/utils.c
@@ -133,6 +133,10 @@ static struct
           jmethodID init_iz;
           jmethodID init_z;
     } SurfaceTexture;
+    struct {
+        jclass clazz;
+        jmethodID init_st;
+    } Surface;
 } jfields;
 
 #define JNI_CALL(what, obj, method, ...) \
@@ -713,6 +717,7 @@ InitJNIFields(JNIEnv *env, vlc_object_t *p_obj, jobject *jobj)
     }
 
     jfields.SurfaceTexture.clazz = NULL;
+    jfields.Surface.clazz = NULL;
 
     jclass surfacetexture_class =
         (*env)->FindClass(env, "android/graphics/SurfaceTexture");
@@ -734,6 +739,17 @@ InitJNIFields(JNIEnv *env, vlc_object_t *p_obj, jobject *jobj)
         !jfields.SurfaceTexture.init_z)
         goto error;
 
+    jclass surface_class = (*env)->FindClass(env, "android/view/Surface");
+    CHECK_EXCEPTION("android/view/Surface class", true);
+
+    jfields.Surface.clazz = (*env)->NewGlobalRef(env, surface_class);
+    (*env)->DeleteLocalRef(env, surface_class);
+    if (jfields.Surface.clazz == NULL)
+        goto error;
+
+    GET_METHOD(Surface, init_st, "<init>",
+               "(Landroid/graphics/SurfaceTexture;)V", true);
+
 #undef GET_METHOD
 #undef CHECK_EXCEPTION
 
@@ -751,6 +767,10 @@ error:
         (*env)->DeleteGlobalRef(env, jfields.SurfaceTexture.clazz);
     jfields.SurfaceTexture.clazz = NULL;
 
+    if (jfields.Surface.clazz)
+        (*env)->DeleteGlobalRef(env, jfields.Surface.clazz);
+    jfields.Surface.clazz = NULL;
+
     vlc_mutex_unlock(&lock);
     msg_Err(p_obj, "Failed to load jfields table");
     return VLC_EGENERIC;
@@ -879,6 +899,9 @@ AWindowHandler_destroy(AWindowHandler *p_awh)
         if (jfields.SurfaceTexture.clazz)
             (*p_env)->DeleteGlobalRef(p_env, jfields.SurfaceTexture.clazz);
 
+        if (jfields.Surface.clazz)
+            (*p_env)->DeleteGlobalRef(p_env, jfields.Surface.clazz);
+
         JNI_ANWCALL(CallVoidMethod, unregisterNative);
         AWindowHandler_releaseANativeWindowEnv(p_awh, p_env, AWindow_Video);
         AWindowHandler_releaseANativeWindowEnv(p_awh, p_env, AWindow_Subtitles);
-- 
2.27.0



More information about the vlc-devel mailing list