pallgeuer
pallgeuer

Reputation: 1366

Android Camera2 + MediaCodec + MediaMuxer using persistent surface?

I'm writing a video camera app. When I open my capture activity I would like to configure a camera capture session that starts a preview, and when the user later presses the record button it should start recording video using MediaCodec + MediaMuxer (not MediaRecorder for app-specific reasons) without having to recreate a new CameraCaptureSession. I tried using MediaCodec#createPersistentInputSurface to make this possible.

So far I have got the preview working and the MediaCodec/MediaMuxer bit is producing a 'valid' output video file (as per ffprobe), but the content of the video is flashes of random diagonal colours/scrambled. On another device the same code fails to create the capture session at all. I believe the problem is that I am creating the MediaCodec instance after creating the capture session, meaning that when the camera is configured with the persistent surface it has no idea what output size and colour format it should be using.

My question: What pipeline do I need to follow in order to achieve the behaviour I'm looking for with MediaCodec and a persistent input surface?

My notes:

A bit of a chicken and the egg problem. Is there a way to break this cyclic dependency loop?

Upvotes: 3

Views: 990

Answers (1)

pallgeuer
pallgeuer

Reputation: 1366

I ended up getting the required behaviour (even if it seems a bit hacky) by doing the following:

  • Create the persistent input surface
  • Create a dummy MediaCodec with exactly the same configuration/parameters as you will later be recording with, going as far as calling MediaCodec::configure and MediaCodec::setInputSurface (with the persistent input surface). This initialises the persistent surface's internal color/format parameters etc, which is required for creating the capture session to succeed.
  • Create the required capture session with the persistent input surface
  • Release the dummy video encoder as we don't need it anymore

From there on, you can keep the one same camera capture session and create new MediaCodecs every time you want to record something, but they always need to be configured exactly like the dummy one was, otherwise there could be recording issues.

Side-note: In the question I thought deferred surfaces could help, but I fully understand them now, and no, they don't help with that specific problem.

Upvotes: 2

Related Questions