// Set underlying Camera2 stream use case to optimize for video calls.
val videoCallStreamId = CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL.toLong()
// Check available CameraInfos to find the first one that supports // the video call stream use case. val frontCameraInfo = cameraProvider.getAvailableCameraInfos() .first { cameraInfo -> val isVideoCallStreamingSupported = Camera2CameraInfo.from(cameraInfo) .getCameraCharacteristic( CameraCharacteristics.SCALER_AVAILABLE_STREAM_USE_CASES )?.contains(videoCallStreamId) val isFrontFacing = (cameraInfo.getLensFacing() == CameraSelector.LENS_FACING_FRONT) (isVideoCallStreamingSupported == true) && isFrontFacing }
val cameraSelector = frontCameraInfo.cameraSelector
// Start with a Preview Builder. val previewBuilder = Preview.Builder() .setTargetAspectRatio(screenAspectRatio) .setTargetRotation(rotation)
// Use Camera2Interop.Extender to set the video call stream use case. Camera2Interop.Extender(previewBuilder).setStreamUseCase(videoCallStreamId)
// Bind the Preview UseCase and the corresponding CameraSelector. val preview = previewBuilder.build() camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview)
// Check available CameraInfos to find the first one that supports // the video call stream use case. List<CameraInfo> cameraInfos = cameraProvider.getAvailableCameraInfos(); CameraInfofrontCameraInfo=null; for (cameraInfo in cameraInfos) { Long[] availableStreamUseCases = Camera2CameraInfo.from(cameraInfo) .getCameraCharacteristic( CameraCharacteristics.SCALER_AVAILABLE_STREAM_USE_CASES ); booleanisVideoCallStreamingSupported= Arrays.List(availableStreamUseCases) .contains(videoCallStreamId); booleanisFrontFacing= (cameraInfo.getLensFacing() == CameraSelector.LENS_FACING_FRONT);
if (isVideoCallStreamingSupported && isFrontFacing) { frontCameraInfo = cameraInfo; } }
if (frontCameraInfo == null) { // Handle case where video call streaming is not supported. }
// Start with a Preview Builder. Preview.BuilderpreviewBuilder= Preview.Builder() .setTargetAspectRatio(screenAspectRatio) .setTargetRotation(rotation);
// Use Camera2Interop.Extender to set the video call stream use case. Camera2Interop.Extender(previewBuilder).setStreamUseCase(videoCallStreamId);
// Bind the Preview UseCase and the corresponding CameraSelector. Previewpreview= previewBuilder.build() Cameracamera= cameraProvider.bindToLifecycle(this, cameraSelector, preview)