MediaCodec with Surface Input: Recording in background

拈花ヽ惹草 提交于 2019-12-03 02:47:27

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:

  1. eglDestroySurface() must be called
  2. the EGL surface must not be "current" in any thread

The 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!