Class CameraCapturer

  • All Implemented Interfaces:
    VideoCapturer, VideoCapturer

    public class CameraCapturer
    extends java.lang.Object
    implements VideoCapturer
    The CameraCapturer class is used to provide video frames for a LocalVideoTrack from a given cameraId. The frames are provided via the preview API of Camera.

    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 LocalVideoTracks simultaneously.

    • Constructor Detail

      • CameraCapturer

        public CameraCapturer​(@NonNull
                              android.content.Context context,
                              @NonNull
                              java.lang.String cameraId)
        Constructs a CameraCapturer instance.
        Parameters:
        context - application context
        cameraId - unique identifier of the camera device to open that must be specified in Camera1Enumerator.getDeviceNames()
      • CameraCapturer

        public CameraCapturer​(@NonNull
                              android.content.Context context,
                              @NonNull
                              java.lang.String cameraId,
                              @Nullable
                              CameraCapturer.Listener listener)
        Constructs a CameraCapturer instance.
        Parameters:
        context - application context
        cameraId - unique identifier of the camera device to open that must be specified in Camera1Enumerator.getDeviceNames()
        listener - an optional listener of camera capturer events
    • Method Detail

      • isScreencast

        public boolean isScreencast()
        Indicates that the camera capturer is not a screen cast.
        Specified by:
        isScreencast in interface VideoCapturer
        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: VideoCapturer
        This 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 in surfaceTextureHelper, register itself as a listener, and forward the frames to CapturerObserver.onFrameCaptured(). The caller still has ownership of surfaceTextureHelper and 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:
        initialize in interface VideoCapturer
      • startCapture

        public void startCapture​(int width,
                                 int height,
                                 int framerate)
        Description copied from interface: VideoCapturer
        Start capturing frames in a format that is as close as possible to width x height and framerate.
        Specified by:
        startCapture in interface VideoCapturer
      • stopCapture

        public void stopCapture()
        Stops all frames being captured. The Camera interface should be available for use upon completion.

        Note: This method is not meant to be invoked directly.

        Specified by:
        stopCapture in interface VideoCapturer
      • getCameraId

        @NonNull
        public java.lang.String getCameraId()
        Returns the currently set cameraID.
      • switchCamera

        public void switchCamera​(@NonNull
                                 java.lang.String newCameraId)
        Switches the current cameraId. This method can be invoked while capturing frames or not.
        Parameters:
        newCameraId - the new camera id.
      • updateCameraParameters

        public boolean updateCameraParameters​(@NonNull
                                              CameraParameterUpdater cameraParameterUpdater)
        Schedules a camera parameter update. The current camera's Camera.Parameters will be provided for modification via CameraParameterUpdater.apply(Camera.Parameters). Any changes to the parameters will be applied after the invocation of this callback. This method can be invoked while capturing frames or not.

        The following snippet demonstrates how to turn on the flash of a camera while capturing.

        
             // Create camera capturer
             CameraCapturer cameraCapturer = new CameraCapturer(context, cameraId, null);
        
             // Start camera capturer
             LocalVideoTrack cameraVideoTrack = LocalVideoTrack.create(context, true, cameraCapturer);
        
             // Schedule camera parameter update
             cameraCapturer.updateCameraParameters(new CameraParameterUpdater() {
                @Override
                 public void apply(Camera.Parameters cameraParameters) {
                     // Ensure camera supports flash and turn on
                     if (cameraParameters.getFlashMode() != null) {
                          cameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                     }
                 }
             });
         
        Parameters:
        cameraParameterUpdater - camera parameter updater that receives current camera parameters for modification.
        Returns:
        true if update was scheduled or false if an update is pending or could not be scheduled.