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 architecture of my encoder is derived from the excellent CameraToMpegTest example, with the addition of displaying camera frames to a GLSurfaceView (see Github links below). I'm currently performing background recording with a two-state solution:
When the hosting Activity is in the foreground, encode one video frame on each call to the
GLSurfaceView.Renderer
'sonDrawFrame
. This allows me access to the GLSurfaceView's EGL state in bursts so as not to block other events queued to the renderer thread.When the hosting Activity enters the background, halt the
onDrawFrame
encoding and encode frames on another background thread within a loop. This mode is identical to the CameraToMpegTest example.
However if the screen is powered off the GLSurfaceView's EGLContext is lost and a new call to onSurfaceCreated
occurs. In this case we have to re-create the EGL window surface connected to MediaCodec's input Surface. Unfortunately this 2nd call to eglCreateWindowSurface
produces:
E/libEGL(18839): EGLNativeWindowType 0x7a931098 already connected to another API
Prior to calling, I release all EGL resources connected to the Android Surface.
Is there a way to swap the EGLSurface connected to MediaCodec's input Surface?
The complete source of my test application is on Github. Main Activity.
Update I applied the lessons learned here into a video sdk for Android based on the MediaCodec & MediaMuxer classes. Hope it helps!