HighSpeedVideoSessionConfig


public final class HighSpeedVideoSessionConfig extends 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

Nested types

Builder for HighSpeedVideoSessionConfig

Public constructors

HighSpeedVideoSessionConfig(
    @NonNull VideoCapture<@NonNull ?> videoCapture,
    Preview preview,
    @NonNull Range<@NonNull Integer> frameRateRange,
    boolean isSlowMotionEnabled
)

Public methods

final Preview

Optional Preview use case for displaying a preview during recording.

final @NonNull VideoCapture<@NonNull ?>

The VideoCapture use case for video recording.

final boolean

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

Inherited methods

From androidx.camera.core.SessionConfig
final @NonNull List<@NonNull CameraEffect>

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

final @NonNull Consumer<@NonNull Set<@NonNull GroupableFeature>>

Gets the feature selection listener set to this session config.

final @NonNull Executor

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

final @NonNull Range<@NonNull Integer>

The desired frame rate range for the camera session.

final @NonNull List<@NonNull 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.

final @NonNull Set<@NonNull GroupableFeature>

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

final @NonNull List<@NonNull UseCase>

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

final ViewPort

The ViewPort to be applied on the camera session.

final void

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.

Public constructors

HighSpeedVideoSessionConfig

Added in 1.5.0-beta02
public HighSpeedVideoSessionConfig(
    @NonNull VideoCapture<@NonNull ?> videoCapture,
    Preview preview,
    @NonNull Range<@NonNull Integer> frameRateRange,
    boolean isSlowMotionEnabled
)

Public methods

getPreview

Added in 1.5.0-beta02
public final Preview getPreview()

Optional Preview use case for displaying a preview during recording.

getVideoCapture

Added in 1.5.0-beta02
public final @NonNull VideoCapture<@NonNull ?> getVideoCapture()

The VideoCapture use case for video recording.

isSlowMotionEnabled

Added in 1.5.0-beta02
public final boolean isSlowMotionEnabled()

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