startCameraStream method

Future<void> startCameraStream({
  1. required String facingMode,
})

Implementation

Future<void> startCameraStream({required String facingMode}) async {
  final pc = peerConnection;
  if (pc == null) {
    Log().error("startCameraStream, pc is null");
    return;
  }
  Log().info('startCameraStream, facingMode = $facingMode');
  // if (CameraManager().isCameraOn == true && CameraManager().currentFacingMode != facingMode) {
  //   CameraManager().turnCameraOff();
  // }
  final existSender = localVideoSender;
  if (existSender != null) {
    await pc.removeTrack(existSender);
  }
  await CameraManager().turnCameraOff();
  final videoStream = await CameraManager().getVideoMedia(facingMode);
  localVideoStream = videoStream;

  final videoTrack = await _createVideoTrack(videoStream);
  localVideoTrack = videoTrack;
  localVideoTrack?.enabled = true;
  Log().info(
      'startCameraStream, videoStream = $videoStream, videoTrack = $videoTrack');
  if (videoTrack != null) {
    localVideoSender = await pc.addTrack(videoTrack, localVideoStream!);
    await localVideoSender?.replaceTrack(videoTrack);
  } else {
    Log().error('startCameraStream - videoTrack is null');
  }
}