I\'m working on a video encoding application which I want to prevent from stopping when the hosting Activity enters the background, or the screen cycles off/on.
The arch
Background first...
When you call eglCreateWindowSurface()
, the Android EGL wrapper calls native_window_api_connect()
on the Surface
you passed in. This eventually turns into a BufferQueue
producer connect call, which means that this EGL surface is now the sole source of graphics buffers for the Surface
.
The EGL surface stays connected to the Surface
until the EGL surface is destroyed. When it is, the surface destructor calls native_window_api_disconnect()
to disconnect the EGL surface from the BufferQueue
. The EGL surface is reference-counted, with the refcount incremented when the surface is passed to eglMakeCurrent()
so to be destroyed two things must happen:
eglDestroySurface()
must be calledThe second item requires calling eglMakeCurrent()
with another EGL surface (or EGL_NO_SURFACE
), or calling eglReleaseThread()
, on any thread that had previously used the surface. One quick way to confirm that this is done is to add logging before calls to eglMakeCurrent()
when the surface is made current and un-current, and compare the thread IDs by viewing the logcat output with adb logcat -v threadtime
. It may also be useful to use EGL queries like eglGetCurrentSurface(EGL_DRAW)
to confirm that you're doing the un-current in the thread that has made the surface current.
If the EGL surface isn't destroyed, it won't disconnect from the Surface
, and attempts to connect a new producer (by calling eglCreateWindowSurface
with a new EGL surface) will be rejected with the "already connected" message.
Update: My implementation is now available in the Grafika test project. If you install this, select "Show + capture camera", start recording, toggle the power, and then stop recording, you should have a complete movie with a long pause in the middle. You can back out, select "Play video", and choose "camera-test.mp4" to view it.