0

I'am a beginner. I would like to write an app, which has many activities and one othe them is for setting video recording on background but within camera preview. So when I leave the activity the recording must continue.

  • I have a service started from the main activity. This service manage the camera and video recording.
  • The service containes initialized and "prepared" MediaRecorder instance, "recorder". It's set to source from surface like recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE).
  • Camera is opened by cameraManager.openCamera(cameraId, etc.) and cameraDevice.createCaptureSession(surfaceList, etc.) where surfaceList contains surface from the MediaRecorder instance and surface from a SurfaceTexture instance created in the onCreate function of this service.
  • Capturing is done by cameraDevice.createCaptureRequest(some arguments) and cameraCaptureSession.setRepeatingRequest(some arguments). Of course I don't forget add surfaces by requestBuilder.addTarget().
  • I pass the SurfaceTexture instance to the upper activity, where I pass it to the existing TextureView UI element like textureView.setSurfaceTexture(surfaceTextureFromTheService). Works well.
  • I can start recording video to the file by recorder.start(). Recording works well, camera preview too.
  • NOW! I would like to completely leave the activity (but still stay in the app) and somehow remove the surfaceTextureFromTheService from the activity UI. If I simply leave the activity I got an exception:

E/BufferQueueProducer: [SurfaceTexture-0-29154-1] dequeueBuffer: BufferQueue has been abandoned 03-04 22:08:47.525 29154-29440/com.example.hakka.androidcarhelper I/Adreno: DequeueBuffer: dequeueBuffer failed 03-04 22:08:47.525 29154-29440/com.example.hakka.androidcarhelper E/CameraDeviceGLThread-0: Received exception on GL render thread: java.lang.IllegalStateException: swapBuffers: EGL error: 0x300d at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkEglError(SurfaceTextureRenderer.java:530) at android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:523) at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:727) at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:105) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:148) at android.os.HandlerThread.run(HandlerThread.java:61)

I understand I do something wrong if I try to leave activity without releasing the surfaceTextureFromTheService. But how to do it? How can I release the surfaceTextureFromTheService but still to be able reuse it again after I return back to the activity? And of course without calling cameraDevice.createCaptureRequest and cameraCaptureSession.setRepeatingRequest again in the service because if the MediaRecorder is recording I don't want to stop it everytime I leave and return to the activity.

I have spent about 6 days 12 hours a day with solving this problem. But still no luck.

Thank you.

Walid
  • 700
  • 2
  • 10
  • 29
Häkka
  • 1
  • 2

1 Answers1

0

You can call setRepeatingRequest just fine without interrupting the recording, so when you go to the background, replace the repeating request with one that only targets the media recorder Surface. That should avoid the error you're seeing.

That said, returning to the Activity doesn't really work with your plan; if I recall correctly, the TextureView EGL context, etc, gets torn down when the Activity is no longer visible. And the TextureView takes ownership of the SurfaceTexture, so once the TextureView is gone, I don't think that SurfaceTexture can be used again.

To be more flexible, you likely need to build your own TextureView-equivalent with a GLSurfaceView and a bunch of boilerplate EGL; you'll need to attach and detach the SurfaceTexture from an EGL context each time you transition into/out of the Activity, and handle rendering of the SurfaceTexture onto a quad.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47