RTCPeerConnectionWeb constructor

RTCPeerConnectionWeb(
  1. String _peerConnectionId,
  2. RtcPeerConnection _jsPc
)

Implementation

RTCPeerConnectionWeb(this._peerConnectionId, this._jsPc) {
  _jsPc.onAddStream.listen((mediaStreamEvent) {
    final jsStream = mediaStreamEvent.stream;
    if (jsStream == null) {
      throw Exception('Unable to get the stream from the event');
    }
    if (jsStream.id == null) {
      throw Exception('The stream must have a valid identifier');
    }

    final _remoteStream = _remoteStreams.putIfAbsent(
        jsStream.id!, () => MediaStreamWeb(jsStream, _peerConnectionId));

    onAddStream?.call(_remoteStream);

    jsStream.onAddTrack.listen((mediaStreamTrackEvent) {
      final jsTrack =
          (mediaStreamTrackEvent as html.MediaStreamTrackEvent).track;
      if (jsTrack == null) {
        throw Exception('The Media Stream track is null');
      }
      final track = MediaStreamTrackWeb(jsTrack);
      _remoteStream.addTrack(track, addToNative: false).then((_) {
        onAddTrack?.call(_remoteStream, track);
      });
    });

    jsStream.onRemoveTrack.listen((mediaStreamTrackEvent) {
      final jsTrack =
          (mediaStreamTrackEvent as html.MediaStreamTrackEvent).track;
      if (jsTrack == null) {
        throw Exception('The Media Stream track is null');
      }
      final track = MediaStreamTrackWeb(jsTrack);
      _remoteStream.removeTrack(track, removeFromNative: false).then((_) {
        onRemoveTrack?.call(_remoteStream, track);
      });
    });
  });

  _jsPc.onDataChannel.listen((dataChannelEvent) {
    if (dataChannelEvent.channel != null) {
      onDataChannel?.call(RTCDataChannelWeb(dataChannelEvent.channel!));
    }
  });

  _jsPc.onIceCandidate.listen((iceEvent) {
    if (iceEvent.candidate != null) {
      onIceCandidate?.call(_iceFromJs(iceEvent.candidate!));
    }
  });

  _jsPc.onIceConnectionStateChange.listen((_) {
    _iceConnectionState =
        iceConnectionStateForString(_jsPc.iceConnectionState);
    onIceConnectionState?.call(_iceConnectionState!);
  });

  jsutil.setProperty(_jsPc, 'onicegatheringstatechange', js.allowInterop((_) {
    _iceGatheringState = iceGatheringStateforString(_jsPc.iceGatheringState);
    onIceGatheringState?.call(_iceGatheringState!);
  }));

  _jsPc.onRemoveStream.listen((mediaStreamEvent) {
    if (mediaStreamEvent.stream?.id != null) {
      final _remoteStream =
          _remoteStreams.remove(mediaStreamEvent.stream!.id);
      if (_remoteStream != null) {
        onRemoveStream?.call(_remoteStream);
      }
    }
  });

  _jsPc.onSignalingStateChange.listen((_) {
    _signalingState = signalingStateForString(_jsPc.signalingState);
    onSignalingState?.call(_signalingState!);
  });

  _jsPc.onIceConnectionStateChange.listen((_) {
    _connectionState = peerConnectionStateForString(_jsPc.iceConnectionState);
    onConnectionState?.call(_connectionState!);
  });

  _jsPc.onNegotiationNeeded.listen((_) {
    onRenegotiationNeeded?.call();
  });

  _jsPc.onTrack.listen((trackEvent) {
    if (trackEvent.track != null && trackEvent.receiver != null) {
      onTrack?.call(
        RTCTrackEvent(
          track: MediaStreamTrackWeb(trackEvent.track!),
          receiver: RTCRtpReceiverWeb(trackEvent.receiver!),
          transceiver: RTCRtpTransceiverWeb.fromJsObject(
              jsutil.getProperty(trackEvent, 'transceiver')),
          streams: (trackEvent.streams != null)
              ? trackEvent.streams!
                  .map((dynamic stream) =>
                      MediaStreamWeb(stream, _peerConnectionId))
                  .toList()
              : [],
        ),
      );
    }
  });
}