answerWebRtc function

Future<WebRtcConn> answerWebRtc(
  1. P2PStream stream, {
  2. RtcConfig config = const RtcConfig(),
  3. Duration timeout = const Duration(seconds: 30),
})

Runs the answering side ("B") over stream, an inbound /webrtc-signaling/0.0.1 stream. Returns the established connection.

Implementation

Future<WebRtcConn> answerWebRtc(
  P2PStream stream, {
  RtcConfig config = const RtcConfig(),
  Duration timeout = const Duration(seconds: 30),
}) async {
  final signaling = _SignalingChannel(stream);
  final pc = RtcPeerConnection(config);
  try {
    final offer = await signaling.read().timeout(timeout);
    if (offer?.type != SignalingType.sdpOffer) {
      throw WebRtcSignalingException('expected SDP offer, got ${offer?.type.name ?? 'end of stream'}');
    }
    pc.setRemoteDescription(offer!.data, 'offer');
    await signaling.write(SignalingMessage(SignalingType.sdpAnswer, await pc.createAnswer()));
    await _applyCandidatesUntilConnected(pc, signaling, timeout);
    unawaited(stream.close().catchError((Object _) {}));
    return await _connFor(pc, stream, Direction.inbound);
  } catch (e) {
    _log.fine('responder failed: $e');
    await pc.close();
    await stream.reset();
    rethrow;
  }
}