HighSpeedVideoSessionConfig


class HighSpeedVideoSessionConfig : SessionConfig


A SessionConfig for high-speed video recording sessions.

This class encapsulates the necessary configurations for a high-speed video recording session, including video capture, optional preview and frame rate. Once configured, this config can be bound to a camera and lifecycle using androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle or androidx.camera.lifecycle.LifecycleCameraProvider.bindToLifecycle.

The supported frame rate range can be queried using CameraInfo.getSupportedFrameRateRanges with a specific HighSpeedVideoSessionConfig. Supported frame rate is at least 120 FPS and is in multiples of 30. Common high-speed frame rates include 120 FPS, 240 FPS, and sometimes higher (like 480 FPS or even 960 FPS on some devices). If the given frame rate is not one of the supported frame rate ranges, an IllegalArgumentException will be thrown when binding to lifecycle.

High-speed recording is subject to specific constraints, aligning with those imposed by Android's camera2 CameraConstrainedHighSpeedCaptureSession API.

Constraints:

Recording a high-speed video follows the same process as recording a regular video. This involves creating a Recorder with the desired QualitySelector and other settings. The supported qualities and related settings for high-speed sessions can be queried using Recorder.getHighSpeedVideoCapabilities.

Slow-Motion Video Recording: When the isSlowMotionEnabled flag is set to true, the recorded high-speed video will be processed and saved at a standard frame rate of 30 FPS. This creates the slow-motion effect upon playback. The resulting playback speed will be inversely proportional to the ratio of the high recording frame rate to the standard playback frame rate (30 FPS).

For example:

  • If you record at 120 FPS with isSlowMotionEnabled set to true, the video will be saved at 30 FPS, resulting in a 1/4x speed during playback (120 / 30 = 4).

  • If you record at 240 FPS with isSlowMotionEnabled set to true, the video will be saved at 30 FPS, resulting in a 1/8x speed during playback (240 / 30 = 8).

If isSlowMotionEnabled is false, the video will be saved at the actual recording frame rate specified by the frameRateRange parameter, e.g. 120 FPS, without slow-motion effect.

See the sample code below for recording a slow-motion video:

import androidx.camera.core.CameraSelector
import androidx.camera.core.DynamicRange
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.lifecycle.awaitInstance
import androidx.camera.video.HighSpeedVideoSessionConfig
import androidx.camera.video.Quality
import androidx.camera.video.QualitySelector
import androidx.camera.video.Recorder
import androidx.camera.video.VideoCapture
import androidx.core.content.ContextCompat

// Get ProcessCameraProvider.
val cameraProvider = ProcessCameraProvider.awaitInstance(activity)

// Get desired CameraInfo.
val cameraInfo = cameraProvider.getCameraInfo(CameraSelector.DEFAULT_BACK_CAMERA)

// Get high-speed video capabilities.
val videoCapabilities = Recorder.getHighSpeedVideoCapabilities(cameraInfo)
if (videoCapabilities == null) {
    // High-speed video is not supported on this camera device.
    return
}

// Get supported Qualities.
val supportedQualities = videoCapabilities.getSupportedQualities(DynamicRange.SDR)

// App's preferred qualities.
val preferredQualities = selectQualities(supportedQualities)

// Create UseCases and HighSpeedVideoSessionConfig.Builder
val preview = Preview.Builder().build()
preview.surfaceProvider = previewView.surfaceProvider
val recorder =
    Recorder.Builder()
        .setQualitySelector(QualitySelector.fromOrderedList(preferredQualities))
        .build()
val videoCapture = VideoCapture.withOutput(recorder)
val sessionConfigBuilder =
    HighSpeedVideoSessionConfig.Builder(videoCapture)
        .setPreview(preview)
        .setSlowMotionEnabled(true)

// Query supported frame rate ranges.
val supportedFrameRateRanges =
    cameraInfo.getSupportedFrameRateRanges(sessionConfigBuilder.build())

// App's preferred frame rate.
val preferredFrameRate = selectFrameRate(supportedFrameRateRanges)

// Apply preferred frame rate.
sessionConfigBuilder.setFrameRateRange(preferredFrameRate)

// Bind to lifecycle.
cameraProvider.bindToLifecycle(
    activity,
    CameraSelector.DEFAULT_BACK_CAMERA,
    sessionConfigBuilder.build(),
)

// Start recording.
val recording =
    recorder.prepareRecording(activity, generateOutputOptions()).start(
        ContextCompat.getMainExecutor(activity)
    ) { videoRecordEvent ->
        // ...
    }
Throws
kotlin.IllegalArgumentException

if any of the constraints are violated.

Summary

Public constructors

HighSpeedVideoSessionConfig(
    videoCapture: VideoCapture<*>,
    preview: Preview?,
    frameRateRange: Range<Int>,
    isSlowMotionEnabled: Boolean
)

Public properties

Boolean

Whether to apply slow-motion effects to the recorded video.

Preview?

Optional Preview use case for displaying a preview during recording.

VideoCapture<*>

The VideoCapture use case for video recording.

Inherited functions

From androidx.camera.core.SessionConfig
Unit
setFeatureSelectionListener(
    executor: Executor,
    listener: Consumer<Set<GroupableFeature>>
)

Sets a listener to know which features are finally selected when a session config is bound, based on the user-defined priorities/ordering for preferredFeatureGroup and device capabilities.

Inherited properties

From androidx.camera.core.SessionConfig
List<CameraEffect>

The list of CameraEffect to be applied on the camera session.

Consumer<Set<GroupableFeature>>

Gets the feature selection listener set to this session config.

Executor

Gets the executor set to this session config for feature selection listener invocation.

Range<Int>

The desired frame rate range for the camera session.

List<GroupableFeature>

A list of preferred GroupableFeature ordered according to priority in descending order, i.e. a feature with a lower index in the list is considered to have a higher priority.

Set<GroupableFeature>

A set of GroupableFeature that are mandatory for the camera session configuration.

List<UseCase>

The list of UseCase to be attached to the camera and receive camera data.

ViewPort?

The ViewPort to be applied on the camera session.

Public constructors

HighSpeedVideoSessionConfig

Added in 1.5.0-beta02
HighSpeedVideoSessionConfig(
    videoCapture: VideoCapture<*>,
    preview: Preview? = null,
    frameRateRange: Range<Int> = FRAME_RATE_RANGE_UNSPECIFIED,
    isSlowMotionEnabled: Boolean = false
)

Public properties

isSlowMotionEnabled

Added in 1.5.0-beta02
val isSlowMotionEnabledBoolean

Whether to apply slow-motion effects to the recorded video.

preview

Added in 1.5.0-beta02
val previewPreview?

Optional Preview use case for displaying a preview during recording.

videoCapture

Added in 1.5.0-beta02
val videoCaptureVideoCapture<*>

The VideoCapture use case for video recording.