Sunggook Kim
Sunggook Kim

Reputation: 165

Recording 60fps video with Camera2(on Android version 21) API

I'm trying to record a video at 60(or more)fps rate on Camera2(android.hardware.camera2) APIs.

Finally, I succeeded recording at 120fps using CameraConstrainedHighSpeedCaptureSession. But it is only targeted at >=120fps use case not for 60fps.

Even I tried to record at 60fps using normal capture session(CameraCaptureSession), it only supports <=30fps. I could figure it out through this code below.

Range<Integer>[] fpsRanges = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);

I don't know how I could record at 60fps with Camera2 APIs.

Any idea would be most welcome.

Thanks.

Upvotes: 6

Views: 9196

Answers (2)

mendax01
mendax01

Reputation: 31

Use the following code:

private fun recordSession() {
    setUpMediaRecorder()

    val surfaceTexture = textureView.surfaceTexture

    surfaceTexture?.setDefaultBufferSize(mPreviewSize!!.width, mPreviewSize!!.height)
    captureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD)
    val surfaces = ArrayList<Surface>()

    val textureSurface = Surface(surfaceTexture)
    surfaces.add(textureSurface)
    captureRequestBuilder.addTarget(textureSurface)


    val recordSurface = mediaRecorder.surface
    surfaces.add(recordSurface)
    captureRequestBuilder.addTarget(recordSurface)


    mCameraDevice.createCaptureSession(
        surfaces,
        object : CameraCaptureSession.StateCallback() {

            override fun onConfigured(session: CameraCaptureSession) {
                captureSession = session
                val fps: Int = if (PreferenceProvider.getIsFrontCameraOpened()) {
                    PreferenceProvider.getFrontCameraVideoFrameRate()
                } else {
                    PreferenceProvider.getBackCameraVideoFrameRate()
                }
                captureRequestBuilder.set(
                    CaptureRequest.CONTROL_MODE,
                    CaptureRequest.CONTROL_MODE_USE_SCENE_MODE
                )
                captureRequestBuilder.set(
                    CaptureRequest.CONTROL_SCENE_MODE,
                    CaptureRequest.CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO
                )
                captureRequestBuilder.set(
                    CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,
                    Range(fps, fps)
                )
                captureRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, currentExposureLevel)
                updatePreview()
                isRecording = true
                mediaRecorder.start()
            }

            override fun onConfigureFailed(session: CameraCaptureSession) {
                showLog(TAG, "Failed to create CameraRecordSession!")
            }

        },
        mBackgroundHandler
    )
}

NOTE: Make sure you're setting the same frame rate(i.e. 60) in MediaRecorder Configs to get the 60fps effect in the video.

Upvotes: -1

Francisco Durdin Garcia
Francisco Durdin Garcia

Reputation: 13327

You must create a ConstrainedHighSpeedCaptureSession from CameraDevice and instantiate a new session as you possibly did with a normal capture session.

Also you need to set the next values to your Builder:

myPreviewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_USE_SCENE_MODE);
myPreviewRequestBuilder.set(CaptureRequest.CONTROL_SCENE_MODE, CaptureRequest.CONTROL_SCENE_MODE_HIGH_SPEED_VIDEO);
myPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, new Range<Integer>(frameRate, frameRate));

After, generate a CaptureRequestList with your builder:

 myHighSpeedRequestList = ((CameraConstrainedHighSpeedCaptureSession) cameraCaptureSession).createHighSpeedRequestList(myPreviewRequestBuilder.build());

and use it in your capture Session to generate the CaptureSession:

mCaptureSession.setRepeatingBurst(myHighSpeedRequestList,
                                  YourHighSpeedVideoCaptureCallback,
                                  YourBackgroundHandler);

Upvotes: 1

Related Questions