3

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.

Piyush
  • 693
  • 1
  • 7
  • 12
  • 1
    I am nor sure about issue but [this](https://github.com/yeasin50/Flutter-Video-Calling-App/blob/master/lib/screens/call_page.dart) might help you. – Md. Yeasin Sheikh Apr 06 '22 at 09:52
  • are you able to establish a connection in your app? The only difference I noticed is that you are exchanging ice candidates (which I don't know much about) and adding streams and not data channels. – Piyush Apr 06 '22 at 11:41

1 Answers1

1

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

Piyush
  • 693
  • 1
  • 7
  • 12