Android surfaceview to native code

Hi all,

I’d like to use GStreamer within a Unity Android app. According to this example, I’m going to need something like

SurfaceView sv = (SurfaceView) this.findViewById(R.id.surface_video);

that I can pass to the C/C++ native plugin. To be able to render a video, GStreamer needs a

ANativeWindow *new_native_window = ANativeWindow_fromSurface(env, surface);

So my question is, how do I get a SurfaceView from Unity?

I tried to get a SurfaceView from a custom activity but without success. From a custom activity, I have access to a mUnityPlayer object. Can I get the app’s SurfaceView from it?

Thanks in advance for any input.

I think at the very least in Unity 6, UnityPlayer.getView() function exists, which should return the surface view

1 Like

Thanks. Is this documented somewhere?
Any chance that it can be done with Unity 2022.3?

it looks like, the same exist in Unity 2022.3.

Docs for public java methods in Unity sadly don’t exist yet.

Thanks. I can indeed get the SurfaceView with that function. However, when I use it in the native code, I get the following errors:

2024/12/02 15:40:24.875 13239 13448 Error BufferQueueProducer [SurfaceView[com.DefaultCompany.UnityProject/com.company.unityproject.OverrideExample]#1(BLAST Consumer)1](id:33b700000001,api:2,p:13239,c:13239) connect: already connected (cur=2 req=1)
2024/12/02 15:40:24.875 13239 13448 Error libEGL eglCreateWindowSurface: native_window_api_connect (win=0xb400007e91798010) failed (0xffffffea) (already connected to another API?)
2024/12/02 15:40:24.875 13239 13448 Error libEGL eglCreateWindowSurfaceTmpl:694 error 3003 (EGL_BAD_ALLOC)

These errors make sense because the surface is probably already in use.
Can I create a new surface in Java that I can pass to the native code, with an OpenGL texture that I can then pass to Unity? Something along the lines of this example, although I haven’t been able to get this example working, even in single-threaded mode.

Thanks!


This GitHub - nghuuanhdai/UnityNativeExternalTextureExample: Display Android OpenGL external surface texture in Unity helped me a lot