public class Camera2Capturer extends Object implements VideoCapturer
LocalVideoTrack
from the
provided cameraId
. The frames are provided via a CameraCaptureSession
. Camera2Capturer must be run on devices Build.VERSION_CODES.LOLLIPOP
or higher.
This class represents an implementation of a VideoCapturer
interface. Although public,
these methods are not meant to be invoked directly.
Note: This capturer can be reused, but cannot be shared across multiple LocalVideoTrack
s simultaneously.
Modifier and Type | Class and Description |
---|---|
static class |
Camera2Capturer.Exception
Camera2Capturer exception class.
|
static interface |
Camera2Capturer.Listener
Interface that provides events and errors related to
Camera2Capturer . |
Constructor and Description |
---|
Camera2Capturer(android.content.Context context,
String cameraId,
Camera2Capturer.Listener listener)
Constructs a Camera2Capturer instance.
|
Modifier and Type | Method and Description |
---|---|
String |
getCameraId()
Returns the currently set camera ID.
|
List<VideoFormat> |
getSupportedFormats()
Returns a list of all supported video formats.
|
boolean |
isScreencast()
Indicates that the camera2 capturer is not a screen cast.
|
static boolean |
isSupported(android.content.Context context)
Indicates if Camera2Capturer is compatible with device.
|
void |
setSurfaceTextureHelper(SurfaceTextureHelper surfaceTextureHelper)
This method enables SDK provided video capturers to capture to a surface texture.
|
void |
startCapture(VideoFormat captureFormat,
VideoCapturer.Listener videoCapturerListener)
Starts capturing frames at the specified format.
|
void |
stopCapture()
Stops all frames being captured.
|
void |
switchCamera(String newCameraId)
Switches the current
cameraId . |
boolean |
updateCaptureRequest(CaptureRequestUpdater captureRequestUpdater)
Schedules a capture request update.
|
public Camera2Capturer(android.content.Context context, String cameraId, Camera2Capturer.Listener listener)
Note: It is possible to construct multiple instances with different camera IDs, but there are often device limitations on how many camera2 sessions can be open.
context
- application contextcameraId
- unique identifier of the camera device to open that must be specified in
CameraManager.getCameraIdList()
.listener
- listener of camera 2 capturer eventspublic static boolean isSupported(android.content.Context context)
This method checks that all the following conditions are true:
Build.VERSION_CODES.LOLLIPOP
.
CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY
.
context
- application context.public List<VideoFormat> getSupportedFormats()
CameraCharacteristics
, so can vary based on a device's
camera capabilities.
Note: This method can be invoked for informational purposes, but is primarily used internally.
getSupportedFormats
in interface VideoCapturer
public boolean isScreencast()
isScreencast
in interface VideoCapturer
public void startCapture(VideoFormat captureFormat, VideoCapturer.Listener videoCapturerListener)
Note: This method is not meant to be invoked directly.
startCapture
in interface VideoCapturer
captureFormat
- the format in which to capture frames.videoCapturerListener
- consumer of available frames.public void stopCapture()
Note: This method is not meant to be invoked directly.
stopCapture
in interface VideoCapturer
public String getCameraId()
public void switchCamera(String newCameraId)
cameraId
.newCameraId
- the new camera id.public boolean updateCaptureRequest(CaptureRequestUpdater captureRequestUpdater)
A CaptureRequest.Builder
optimal for capturing video for streaming will be
provided for modification to the provided CaptureRequestUpdater
. Modifications to the
CaptureRequest.Builder
will be applied to the CameraCaptureSession
after the invocation of CaptureRequestUpdater.apply(CaptureRequest.Builder)
. updateCaptureRequest(CaptureRequestUpdater)
can be invoked while capturing frames or not.
If the Camera2Capturer
is not capturing the CaptureRequestUpdater
will 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.Exception
to the provided Camera2Capturer.Listener
with
the code Camera2Capturer.Exception.CAPTURE_REQUEST_UPDATE_FAILED
if a successfully scheduled capture
request update failed to be applied.
captureRequestUpdater
- capture request updater that receives the capture request
builder that can be modified.public void setSurfaceTextureHelper(SurfaceTextureHelper surfaceTextureHelper)
VideoCapturer
Camera2Capturer
and ScreenCapturer
.setSurfaceTextureHelper
in interface VideoCapturer
5.13.0