toggleScreenShare method

Future<void> toggleScreenShare(
  1. BuildContext context
)

Toggles local screen-sharing stream on and off.

Implementation

Future<void> toggleScreenShare(BuildContext context) async {
  if (PlatformUtils.isMobile) {
    keepUi();
  }

  final LocalTrackState state = _currentCall.value.screenShareState.value;

  if (state == LocalTrackState.enabled || state == LocalTrackState.enabling) {
    await _currentCall.value.setScreenShareEnabled(false);
  } else {
    // TODO: `medea_jason` should have `onScreenChange` callback.
    await _currentCall.value.enumerateDevices(media: false);

    // Currently only desktops can have multiple displays.
    if (PlatformUtils.isMobile || PlatformUtils.isWeb) {
      return await _currentCall.value.setScreenShareEnabled(
        true,

        // Whether to share or not to share the audio is dependent on the
        // browser's API, so always try to query it.
        withAudio: !PlatformUtils.isMobile,
      );
    }

    final ScreenShareRequest? display = await ScreenShareView.show(
      router.context!,
      _currentCall,
    );

    if (display != null) {
      await _currentCall.value.setScreenShareEnabled(
        true,
        device: display.details,
        withAudio: display.audio,
      );
    }
  }
}