createScreenSharingTrack method

Future<void> createScreenSharingTrack(
  1. MediaStreamTrack track, {
  2. required WebRTCCodec vCodec,
  3. required MediaStream stream,
  4. String kind = 'video',
})

Implementation

Future<void> createScreenSharingTrack(
  MediaStreamTrack track, {
  required WebRTCCodec vCodec,
  required MediaStream stream,
  String kind = 'video',
}) async {
  final transceiver = await addTransceiver(
    track: track,
    kind: RTCRtpMediaType.RTCRtpMediaTypeVideo,
    init: RTCRtpTransceiverInit(
      direction: TransceiverDirection.SendOnly,
      streams: [stream],
    ),
  );

  if (!kIsWeb) return;

  final caps = await getRtpReceiverCapabilities(kind);
  if (caps.codecs == null) return;

  final List<RTCRtpCodecCapability> matched = [];
  final List<RTCRtpCodecCapability> partialMatched = [];
  final List<RTCRtpCodecCapability> unmatched = [];
  for (final c in caps.codecs!) {
    final codec = c.mimeType.toLowerCase();
    if (codec == 'audio/opus') {
      matched.add(c);
      continue;
    }

    final matchesVideoCodec =
        codec.toLowerCase() == 'video/${vCodec.codec}'.toLowerCase();
    if (!matchesVideoCodec) {
      unmatched.add(c);
      continue;
    }
    // for h264 codecs that have sdpFmtpLine available, use only if the
    // profile-level-id is 42e01f for cross-browser compatibility
    if (vCodec.codec == 'h264') {
      if (c.sdpFmtpLine != null &&
          c.sdpFmtpLine!.contains('profile-level-id=42e01f')) {
        matched.add(c);
      } else {
        partialMatched.add(c);
      }
      continue;
    }
    matched.add(c);
  }
  matched.addAll([...partialMatched]);
  try {
    await transceiver.setCodecPreferences(matched);
  } catch (e) {
    WaterbusLogger.instance.bug('setCodecPreferences failed: $e');
  }
}