I have an application, which captures video from camera. I'm isung GLSurfaceView for showing preview and MediaCodec for encoding. GlSurfaceView has it's own GLThread with EGLContext, and i'm creating another thread for MediaCodec and initialising another EGLContext in that thread. And it's working, i can record videos.
Problem comes when i'm trying to minimise and then restore the application. Call to SurfaceTexture.updateTexImage() causes IllegalStateException and logcat shows following message:
02-19 18:19:58.400 8528-15845 E/GLConsumer:[unnamed-8528-0] checkAndUpdateEglState: invalid current EGLContext
02-19 18:19:58.400 8528-15845 E/_GLViewRender: error updating text image
02-19 18:19:58.400 8528-15845 E/_GLViewRender: java.lang.IllegalStateException: Unable to update texture contents (see logcat for details)
02-19 18:19:58.400 8528-15845 E/_GLViewRender: at android.graphics.SurfaceTexture.nativeUpdateTexImage(Native Method)
02-19 18:19:58.400 8528-15845 E/_GLViewRender: at android.graphics.SurfaceTexture.updateTexImage(SurfaceTexture.java:240)
02-19 18:19:58.400 8528-15845 E/_GLViewRender: at ui.GLSurfaceViewRender.onDrawFrame(GLSurfaceViewRender.java:105)
02-19 18:19:58.400 8528-15845 E/_GLViewRender: at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1583)
02-19 18:19:58.400 8528-15845 E/_GLViewRender: at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1286)
What i'm doing when app minimises:
Stopping camera preview:
if (isPreviewOn && mCamera != null) {
isPreviewOn = false;
mCamera.stopPreview();
}
Releasing camera:
try {
mCamera.setPreviewTexture(null);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.release();
mCamera = null;
Calling
GLSurfaceView.onPause()
What i'm doing when app restores:
Calling
GLSurfaceView.onPause()
Opening camera, setting preview surface texture, and starting preview
if (mCamera == null) {
mCamera = Camera.open(CAMERA_ID);
params = mCamera.getParameters();
}
try {
mCamera.setPreviewTexture(renderer.getSurfaceTexture());
} catch (IOException e) {
Log.e(TAG, "error setting ST to preview", e);
return;
}
if (!isPreviewOn && mCamera != null) {
isPreviewOn = true;
mCamera.startPreview();
}
And when updateTextImage is called next time, this error happens.
Any ideas why?
A first clue is that your Surface is detached from your GlContext.
I don't know where you took this example, but I think that when the camera becomes null, you can expect the remaining instances to also be nullified or invalid. Everything needs to be recreated. Generally, most of the initialization is done inside the constructor of your view that is derived from GlSurfaceView
.
In other words: make sure you pass through the same steps as during the first run. Make a "Initialize()" method.
There is this old tutorial showing the basic of camera usage with a surface.