Rendering same video to 2 Surfaces from a MediaCodec



MediaCodec has 2 ways of operating: you either pass a Surface for it to render to, or you read the output buffer and paint it to the screen yourself.

In the first case, where I pass a surface: is it possible to paint the same MediaCodec decoded video to 2 surfaces?

The decoding loop looks something like this:

int outputBufferId = codec.dequeueOutputBuffer(…);
  if (outputBufferId >= 0) {
    //Do nothing, the MediaCodec will automatically draw to our surface
    //But how to draw to 2 surfaces?
    codec.releaseOutputBuffer(outputBufferId, …);
  } else if (outputBufferId == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
    // Subsequent data will conform to new format.
    // Can ignore if using getOutputFormat(outputBufferId)
    outputFormat = codec.getOutputFormat(); // option B
  }

Answer

The only way you’re going to be able to draw to multiple surfaces at once is if you’re going to use OpenGL.

If you want to, I could give you a couple of suggestions.

First, you’ll need a surface backed by a surface texture. The surface texture is where the decoder will queue decoded frames and you can retrieve it from there. Once a frame is available in the surface texture, you’ll use OpenGL (with multiple EGLSurface(s) connected to each surface. You can create such surface using eglCreateWindowSurface).

Once you have your surfaces connected and a valid EGL context, just use eglMakeCurrent and pass in the EGL surface you created, one for each draw operation. So eglMakeCurrent will allow you to draw on one surface at a time and you can do that for each surface you have. You can generally find examples of some of these in grafika



Source: stackoverflow