How to record an existing surface (not a PersistentSurface) using MediaRecorder

Viewed 1211

I'm trying to record the camera stream which I don't have access to. I'm only have access to a custom SurfaceView that gets the images from the camera preview and render the images.

I would like to save the surface \ camera video into a file. I'm using an SDK for streaming (Amazon Chime) and I don't have any access to the camera component in order to add a persistent surface for recording.

Although I do have access for each new image as "VideoRenderer.I420Frame" not sure how can I take it and accumulate it into a video.

The other approach that i though about is to use the surface in the "MediaRecorder" but when I'm using this surface (of this view) I'm getting this exception:

java.lang.IllegalArgumentException: not a PersistentSurface

Can I somehow create a PersistentSurface and copy the current surface into the new one while getting new images? Or solve this issue in another way?

this class currently look like that:

class RecordingVideoRenderView : SurfaceViewRenderer, VideoRenderView {

private var recording: Boolean = false
private val logger = ConsoleLogger(LogLevel.DEBUG)
private val TAG = "RecordingVideoRenderView"
private lateinit var recorder: MediaRecorder

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

override fun renderFrame(frame: Any) {
    this.renderFrame(frame as VideoRenderer.I420Frame)
}

override fun initialize(initParams: Any?) {
    this.init((initParams as EglBase).eglBaseContext, null)
}

override fun finalize() {
    this.release()
}

fun startRecording() {
    logger.info(TAG, "startRecording")

    val recorderSurface = MediaCodec.createPersistentInputSurface()
    

    recorder = MediaRecorder()
    recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE)

    // Getting java.lang.IllegalArgumentException: not a PersistentSurface when using:
    // this.holder.surface
    // I would like to copy the content of this.holder.surface to the new PersistentSurface
    recorder.setInputSurface(recorderSurface)

    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
    val outputDir = context.cacheDir
    val outputPath = File.createTempFile("test", "mp4", outputDir).absolutePath
    logger.info(TAG, "outputPath: $outputPath")
    recorder.setOutputFile(outputPath)
    recorder.prepare()
    recorder.start()
    recording = true
}

fun stopRecording() {
    logger.info(TAG, "stopRecording")
    recorder.stop()
    recording = false
}

fun isRecording(): Boolean {
    logger.info(TAG, "isRecording, recording:$recording")
    return recording
}

}

0 Answers
Related