startScreenSharing method

  1. @override
Future<void> startScreenSharing({
  1. DesktopCapturerSource? source,
})
override

Implementation

@override
Future<void> startScreenSharing({DesktopCapturerSource? source}) async {
  try {
    if (_mParticipant == null || _mParticipant!.isSharingScreen) return;

    if (WebRTC.platformIsAndroid) {
      await _nativeService.startForegroundService();
    }

    _displayStream = await _getDisplayMedia(source);

    if (_displayStream?.getVideoTracks().isEmpty ?? true) return;

    if (WebRTC.platformIsMobile && _mParticipant!.isVideoEnabled) {
      await toggleVideo(forceValue: false);
    }

    final screenTrack = _displayStream!.getVideoTracks().first;

    await _mParticipant!.peerConnection.createScreenSharingTrack(
      screenTrack,
      vCodec: _callSetting.preferedCodec,
      stream: _displayStream!,
    );

    await _mParticipant?.setSrcObject(_displayStream!, isDisplayStream: true);

    _displayStream?.getVideoTracks().first.onEnded = () {
      stopScreenSharing();
    };

    _mParticipant?.setScreenSharing(true);

    _notify(CallbackEvents.shouldBeUpdateState);
    _socketEmiter.setScreenSharing(true);
  } catch (e) {
    stopScreenSharing();
  }
}