ConcurrentCamera


class ConcurrentCamera


Concurrent camera is a new feature introduced from Android 11, which supports simultaneous streaming of camera devices, for example, it allows a device to have both the front and back cameras operating at the same time.

A concurrent camera object is returned after binding concurrent cameras to LifecycleOwner. It includes a list of Cameras which are operating at the same time. Before binding to LifecycleOwner, check FEATURE_CAMERA_CONCURRENT to see whether this device is supporting concurrent camera or not.

Concurrent camera also supports composition mode, where multiple camera streams are composited into a single stream. This can be used for Picture-in-Picture or other custom layouts. Use CompositionSettings to configure the layout of each camera stream.

CameraX currently only supports dual concurrent camera, which allows two cameras operating at the same time, with at most two UseCases bound for each. The max resolution is 720p or 1440p, more details in the following link, see concurrent camera streaming

Summary

Nested types

Configuration for a single camera in concurrent camera mode, including CameraSelector, LifecycleOwner and UseCaseGroup.

Public constructors

Constructor of concurrent cameras.

Public functions

(Mutable)List<Camera!>

Gets the list of cameras.

Unit
setCompositionSettings(
    compositionSettings: (Mutable)List<CompositionSettings!>
)

Sets the composition settings for concurrent camera.

Public constructors

ConcurrentCamera

Added in 1.3.0
ConcurrentCamera(cameras: (Mutable)List<Camera!>)

Constructor of concurrent cameras.

Parameters
cameras: (Mutable)List<Camera!>

list of Camera.

Public functions

getCameras

Added in 1.3.0
fun getCameras(): (Mutable)List<Camera!>

Gets the list of cameras.

setCompositionSettings

Added in 1.7.0-alpha02
fun setCompositionSettings(
    compositionSettings: (Mutable)List<CompositionSettings!>
): Unit

Sets the composition settings for concurrent camera.

This method can be used to dynamically update the composition settings of the concurrent cameras, for example, to change the position or size of a Picture-in-Picture window.

The composition settings will be applied to the cameras in the order they were bound. The first composition setting is for the primary camera, and the second is for the secondary camera.

The size of the compositionSettings list must be equal to the number of the cameras that were bound using bindToLifecycle.

The following code snippet demonstrates how to update the composition settings for a dual camera setup:

List<SingleCameraConfig> singleCameraConfigs = Arrays.asList(config1, config2);
ConcurrentCamera concurrentCamera = cameraProvider.bindToLifecycle(singleCameraConfigs);

// Primary becomes PiP, Secondary becomes full screen
CompositionSettings primary = new CompositionSettings.Builder()
        .setOffset(0.5f, 0.5f)
        .setScale(0.3f, 0.3f)
        .setZOrder(1) // Display on top
        .build();
CompositionSettings secondary = new CompositionSettings.Builder()
        .setZOrder(0)
        .build();

// The size of the list must match the number of cameras bound (in this case, 2)
concurrentCamera.setCompositionSettings(Arrays.asList(primary, secondary));
Parameters
compositionSettings: (Mutable)List<CompositionSettings!>

A list of CompositionSettings for the concurrent cameras. The size of the list must be equal to the number of the cameras that were bound using bindToLifecycle.

Throws
java.lang.IllegalStateException

if the camera is not in concurrent camera composition mode.

java.lang.IllegalArgumentException

if the size of the composition settings list does not match the number of cameras bound.