stopScreenSharing method

  1. @override
Future<void> stopScreenSharing({
  1. bool stayInRoom = true,
})
override

Implementation

@override
Future<void> stopScreenSharing({bool stayInRoom = true}) async {
  if (!(_mParticipant?.isSharingScreen ?? true)) return;

  _mParticipant?.setScreenSharing(false);

  if (_mParticipant == null) return;

  if (stayInRoom) {
    if (WebRTC.platformIsMobile &&
        (_localStream?.getVideoTracks().isNotEmpty ?? false)) {
      if (_mParticipant!.isVideoEnabled) {
        await toggleVideo(forceValue: true);
      }
    }

    final List<RTCRtpSender> senders =
        await _mParticipant!.peerConnection.getSenders();

    final RTCRtpSender? sendersVideo = senders
        .where((sender) => sender.track?.kind == 'video')
        .toList()
        .lastOrNull;

    if (sendersVideo != null) {
      _mParticipant!.peerConnection.removeTrack(sendersVideo);
    }
  }

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

  for (final MediaStreamTrack track in _displayStream?.getTracks() ?? []) {
    track.stop();
  }

  _mParticipant?.setScreenSharing(false);
  _displayStream?.dispose();
  _displayStream = null;

  if (stayInRoom) {
    _notify(CallbackEvents.shouldBeUpdateState);
    _socketEmiter.setScreenSharing(false);
  } else {
    _replayKitChannel.closeReplayKit();
  }
}