Class Camera2Capturer
- java.lang.Object
-
- com.twilio.video.Camera2Capturer
-
- All Implemented Interfaces:
VideoCapturer,VideoCapturer
public class Camera2Capturer extends java.lang.Object implements VideoCapturer
The Camera2Capturer class is used to provide video frames for aLocalVideoTrackfrom the providedcameraId. The frames are provided via aCameraCaptureSession. Camera2Capturer must be run on devicesBuild.VERSION_CODES.LOLLIPOPor higher.This class represents an implementation of a
VideoCapturerinterface. Although public, these methods are not meant to be invoked directly.This capturer can be reused, but cannot be shared across multiple
LocalVideoTracks simultaneously. It is possible to construct multiple instances with different camera IDs, but there are device specific limitations on how many camera2 sessions can be open.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCamera2Capturer.ExceptionCamera2Capturer exception class.static interfaceCamera2Capturer.ListenerInterface that provides events and errors related toCamera2Capturer.
-
Constructor Summary
Constructors Constructor Description Camera2Capturer(android.content.Context context, java.lang.String cameraId)Constructs a Camera2Capturer instance.Camera2Capturer(android.content.Context context, java.lang.String cameraId, Camera2Capturer.Listener listener)Constructs a Camera2Capturer instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddispose()This method provides an optional step to perform a final cleanup.java.lang.StringgetCameraId()Returns the currently set camera ID.voidinitialize(SurfaceTextureHelper surfaceTextureHelper, android.content.Context context, CapturerObserver capturerObserver)This function is used to initialize the camera thread, the android application context, and the capture observer.booleanisScreencast()Indicates that the camera2 capturer is not a screen cast.static booleanisSupported(android.content.Context context)Indicates if Camera2Capturer is compatible with device.voidstartCapture(int width, int height, int framerate)Start capturing frames in a format that is as close as possible towidth x heightandframerate.voidstopCapture()Stops all frames being captured.voidswitchCamera(java.lang.String newCameraId)Switches the currentcameraId.booleanupdateCaptureRequest(CaptureRequestUpdater captureRequestUpdater)Schedules a capture request update.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.twilio.video.VideoCapturer
changeCaptureFormat, getCaptureFormat
-
-
-
-
Constructor Detail
-
Camera2Capturer
public Camera2Capturer(@NonNull android.content.Context context, @NonNull java.lang.String cameraId)Constructs a Camera2Capturer instance.- Parameters:
context- application contextcameraId- unique identifier of the camera device to open that must be specified inCameraManager.getCameraIdList()
-
Camera2Capturer
public Camera2Capturer(@NonNull android.content.Context context, @NonNull java.lang.String cameraId, @Nullable Camera2Capturer.Listener listener)Constructs a Camera2Capturer instance.- Parameters:
context- application contextcameraId- unique identifier of the camera device to open that must be specified inCameraManager.getCameraIdList()listener- an optional listener of camera2 capturer events
-
-
Method Detail
-
isSupported
public static boolean isSupported(@NonNull android.content.Context context)Indicates if Camera2Capturer is compatible with device.This method checks that all the following conditions are true:
- The device API level is at least
Build.VERSION_CODES.LOLLIPOP. - All device cameras have hardware support level greater than
CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY.
For more details on supported hardware levels see the Android documentation.- Parameters:
context- application context.- Returns:
- true if device supports Camera2Capturer and false if not.
- The device API level is at least
-
isScreencast
public boolean isScreencast()
Indicates that the camera2 capturer is not a screen cast.- Specified by:
isScreencastin interfaceVideoCapturer- Returns:
- true if-and-only-if this is a screen capturer.
-
initialize
public void initialize(@NonNull SurfaceTextureHelper surfaceTextureHelper, @NonNull android.content.Context context, @NonNull CapturerObserver capturerObserver)Description copied from interface:VideoCapturerThis function is used to initialize the camera thread, the android application context, and the capture observer. It will be called only once and before any startCapture() request. The camera thread is guaranteed to be valid until dispose() is called. If the VideoCapturer wants to deliver texture frames, it should do this by rendering on the SurfaceTexture insurfaceTextureHelper, register itself as a listener, and forward the frames to CapturerObserver.onFrameCaptured(). The caller still has ownership ofsurfaceTextureHelperand is responsible for making sure surfaceTextureHelper.dispose() is called. This also means that the caller can reuse the SurfaceTextureHelper to initialize a new VideoCapturer once the previous VideoCapturer has been disposed.- Specified by:
initializein interfaceVideoCapturer
-
startCapture
public void startCapture(int width, int height, int framerate)Description copied from interface:VideoCapturerStart capturing frames in a format that is as close as possible towidth x heightandframerate.- Specified by:
startCapturein interfaceVideoCapturer
-
stopCapture
public void stopCapture()
Stops all frames being captured.Note: This method is not meant to be invoked directly.
- Specified by:
stopCapturein interfaceVideoCapturer
-
dispose
public void dispose()
Description copied from interface:VideoCapturerThis method provides an optional step to perform a final cleanup.- Specified by:
disposein interfaceVideoCapturer- Specified by:
disposein interfaceVideoCapturer
-
getCameraId
@NonNull public java.lang.String getCameraId()
Returns the currently set camera ID.
-
switchCamera
public void switchCamera(@NonNull java.lang.String newCameraId)Switches the currentcameraId.- Parameters:
newCameraId- the new camera id.
-
updateCaptureRequest
public boolean updateCaptureRequest(@NonNull CaptureRequestUpdater captureRequestUpdater)Schedules a capture request update.A
CaptureRequest.Builderoptimal for capturing video for streaming will be provided for modification to the providedCaptureRequestUpdater. Modifications to theCaptureRequest.Builderwill be applied to theCameraCaptureSessionafter the invocation ofCaptureRequestUpdater.apply(CaptureRequest.Builder).updateCaptureRequest(CaptureRequestUpdater)can be invoked while capturing frames or not. If theCamera2Captureris not capturing theCaptureRequestUpdaterwill be called when capturer resumes.The following snippet demonstrates how to turn on the flash of a camera while capturing.
// Create the camera2 capturer Camera2Capturer camera2Capturer = new Camera2Capturer(context, cameraId, camera2Listener); // Start the camera2 capturer LocalVideoTrack camera2VideoTrack = LocalVideoTrack.create(context, true, camera2Capturer); // Schedule the capture request update camera2Capturer.updateCaptureRequest(new CaptureRequestUpdater() { @Override public void apply(@NonNull CaptureRequest.Builder captureRequestBuilder) { captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH); } }The camera2 capturer will raise an
Camera2Capturer.Exceptionto the providedCamera2Capturer.Listenerwith the codeCamera2Capturer.Exception.CAPTURE_REQUEST_UPDATE_FAILEDif a successfully scheduled capture request update failed to be applied.- Parameters:
captureRequestUpdater- capture request updater that receives the capture request builder that can be modified.- Returns:
- true if the update was scheduled or false if an update is pending or could not be scheduled.
-
-