Why is my `flutter_webrtc` peer connection always failing?

1.5k views Asked by At

I'm stuck in establishing a WebRTC connection. I've successfully established WebRTC connections on several web applications using this signaling flow but I'm stuck in flutter. I'm using the flutter_webrtc package and here's my signaling flow -

// Caller initiates offer
void onConnectPressed() async {
      final pc = await PeerConnection().pc;
      await pc.createDataChannel("channel", RTCDataChannelInit());

      final offer = await pc.createOffer();
      await pc.setLocalDescription(offer);

      final description = await pc.getLocalDescription();

      // Transporting offer to the other user
      final socket = await SocketConnection().socket;
      socket.emit("callPeer", {
        "peerId": peerIdController.text,
        "signal": json.encode(description?.toMap())
      });
}


// Callee creates answer
useEventSubscription("peerIsCalling", (data) async {
      print("peerIsCalling");
      final pc = await PeerConnection().pc;
      final socket = await SocketConnection().socket;

      final signal = jsonDecode(data["signal"]);
      await pc.setRemoteDescription(
        RTCSessionDescription(
          signal["sdp"],
          signal["type"],
        ),
      );

      final answer = await pc.createAnswer();
      await pc.setLocalDescription(answer);

      final description = await pc.getLocalDescription();

      // Transporting answer to the other user
      print("answer call");
      socket.emit("answerCall", {
        "callerId": data["callerId"],
        "signal": {
          "sdp": description?.sdp,
          "type": description?.type,
        },
      });
});


// Caller gets answered
useEventSubscription("callAnswered", (signal) async {
      final pc = await PeerConnection().pc;
      pc.setRemoteDescription(
        RTCSessionDescription(
          signal["sdp"],
          signal["type"],
        ),
      );
});

Here's my WebRTC peer connection config -

 final pc = await createPeerConnection({
      "bundlePolicy": "balanced",
      "encodedInsertableStreams": false,
      "iceCandidatePoolSize": 0,
      "iceServers": [
        {
          "credential": "",
          "urls": ["stun:stun.l.google.com:19302"],
          "username": ""
        },
        {
          "credential": "",
          "urls": ["stun:global.stun.twilio.com:3478"],
          "username": ""
        }
      ],
      "iceTransportPolicy": "all",
      "rtcpMuxPolicy": "require",
      "sdpSemantics": "unified-plan"
});

I'm listening to the RTCPeerConnectionState and it goes like this -

RTCPeerConnectionStateNew to RTCPeerConnectionStateConnecting to RTCPeerConnectionStateFailed

Demo Application

One thing to note is that I'm able to connect this application to a peer connection created inside a browser.

1

There are 1 answers

0
Piyush On BEST ANSWER

Thank god, I've found the solution. The problem was that I was testing this on android emulators and WebRTC seems to be problematic on android emulators and also as I said, I was not exchanging ice candidates