stopScreenShare method

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

Implementation

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

  _videoStats.removeSenders('$kIsMine-${TrackType.screen.toString()}');

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

    final senders = await _mParticipant!.peerConnection.getSenders();
    final videoSender = senders
        .where((s) => s.track?.kind == RtcTrackKind.video.kind)
        .lastOrNull;

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

  final operations = <Future>[];

  if (WebRTC.platformIsAndroid) {
    operations.add(_nativeService.stopForegroundService());
  }

  final tracks = _screenSharingStream?.getTracks() ?? [];

  for (final track in tracks) {
    operations.add(track.stop());
  }

  if (operations.isNotEmpty) {
    await Future.wait(operations);
  }

  _mParticipant = await _mParticipant?.setScreenSharing(false);
  _screenSharingStream?.dispose();
  _screenSharingStream = null;

  if (stayInRoom) {
    _notify(CallbackEvents.shouldBeUpdateState);
    _wsEmitter.toggleScreenSharing(false);
  } else {
    _replayKitChannel.closeReplayKit();
  }
}