Android MediaCodec encoding + camera2 how to create a black screen for pause and resume

Viewed 212

I have created a surface with the MediaCodec and added it as a target to the camera2 api. Now I want to sometime pause the MediaCodec encoding and then resume it. What I expect is the encoded video to become black when I pause and continue when I resume. Using MediaCodec.PARAMETER_KEY_SUSPEND doesn't do the job since the output is a frozen frame, and if the paused happens at the beginning of encoding the time starts when I resume. So I was thinking of feeding the encoder with black image, but how can I draw on the surface? Thanks!

Edit: Here is some of the code I use to make the camera2 work with MediaCodec.

  1. I create a simple MediaCodec:

    try {
        mediaCodec =
            MediaCodec.createEncoderByType(mime)
        Logger.d(TAG, "MediaCodec info " + mediaCodec.codecInfo.name)
    
    
    } catch (e: IOException) {
        e.printStackTrace()
    }
    mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
    
    val inputSurface = mediaCodec.createInputSurface() //<-- this is passed to the camera2
    
  2. I add the surface to the camera:

    captureRequestBuilder.addTarget(inputSurface);
    List<Surface> surfaceList = new ArrayList<>();
            surfaceList.add(cameraFrame.getSurface());
            surfaceList.add(inputSurface);
    
            camera.createCaptureSession(
                    surfaceList,
                    captureSessionObserver,
                    cameraThreadHandler
            );
    

And then in a loop I read the mediaCodec output buffers and write them into a muxer.

1 Answers
Related