toggleVideo method

  1. @override
Future<void> toggleVideo({
  1. bool? forceValue,
  2. bool ignoreUpdateValue = false,
})
override

Implementation

@override
Future<void> toggleVideo({
  bool? forceValue,
  bool ignoreUpdateValue = false,
}) async {
  if (_mParticipant == null ||
      (_mParticipant!.isSharingScreen && WebRTC.platformIsMobile)) {
    return;
  }

  final tracks = _localStream?.getVideoTracks() ?? [];

  for (final track in tracks) {
    track.enabled = forceValue ?? !_mParticipant!.isVideoEnabled;

    if (kIsWeb) {
      if (!track.enabled) {
        await track.stop();
      } else {
        await _localStream?.removeTrack(track);
      }
    }
  }

  if (kIsWeb && (forceValue ?? !_mParticipant!.isVideoEnabled)) {
    final MediaStream? localStream = await _getUserMedia(onlyStream: true);

    if (localStream != null) {
      await _localStream!.addTrack(localStream.getVideoTracks().first);
      await _replaceVideoTrack(localStream.getVideoTracks().first);

      _mParticipant?.setSrcObject(localStream);
    }
  }

  if (ignoreUpdateValue) return;

  _mParticipant!.isVideoEnabled =
      forceValue ?? !_mParticipant!.isVideoEnabled;

  _notify(CallbackEvents.shouldBeUpdateState);

  if (_roomId != null) {
    _socketEmiter.setVideoEnabled(
      forceValue ?? _mParticipant!.isVideoEnabled,
    );
  }
}