createStream static method

  1. @internal
Future<MediaStream> createStream(
  1. LocalTrackOptions options
)

Creates a rtc.MediaStream from LocalTrackOptions.

Implementation

@internal
static Future<rtc.MediaStream> createStream(
  LocalTrackOptions options,
) async {
  var constraints = <String, dynamic>{
    'audio': options is AudioCaptureOptions
        ? options.toMediaConstraintsMap()
        : options is ScreenShareCaptureOptions
            ? (options).captureScreenAudio
            : false,
    'video': options is VideoCaptureOptions
        ? options.toMediaConstraintsMap()
        : false,
  };

  final rtc.MediaStream stream;
  if (options is ScreenShareCaptureOptions) {
    if (kIsWeb) {
      if (options.preferCurrentTab) {
        constraints['preferCurrentTab'] = true;
      }
      if (options.selfBrowserSurface != null) {
        constraints['selfBrowserSurface'] = options.selfBrowserSurface!;
      }
    }
    stream = await rtc.navigator.mediaDevices.getDisplayMedia(constraints);
  } else {
    // options is CameraVideoTrackOptions
    stream = await rtc.navigator.mediaDevices.getUserMedia(constraints);
  }

  // Check if the stream looks good
  if ((options is VideoCaptureOptions && stream.getVideoTracks().isEmpty) ||
      (options is AudioCaptureOptions && stream.getAudioTracks().isEmpty)) {
    throw TrackCreateException(
        'Failed to create stream, at least 1 video or audio track should exist');
  }
  return stream;
}