How do I get information about the type of connection of a WebRTC PeerConnection?

browser, javascript, webrtc

Solution

According to the specification, which is currently implemented in Firefox, but not in Chrome, you can indeed suss out the active candidate from the statistics available for candidate pairs, which are:

dictionary RTCIceCandidatePairStats : RTCStats {
    DOMString                     transportId;
    DOMString                     localCandidateId;
    DOMString                     remoteCandidateId;
    RTCStatsIceCandidatePairState state;
    unsigned long long            priority;
    boolean                       nominated;
    boolean                       writable;
    boolean                       readable;
    unsigned long long            bytesSent;
    unsigned long long            bytesReceived;
    double                        roundTripTime;
    double                        availableOutgoingBitrate;
    double                        availableIncomingBitrate;
};

Combined with the stats on the individual candidates:

dictionary RTCIceCandidateAttributes : RTCStats {
    DOMString                ipAddress;
    long                     portNumber;
    DOMString                transport;
    RTCStatsIceCandidateType candidateType;
    long                     priority;
    DOMString                addressSourceUrl;
};

Use `peerConnection.getStats()` to look for an ice candidate pair that is both nominated and has succeeded:

pc.getStats(null))
.then(function(stats) {
  return Object.keys(stats).forEach(function(key) {
    if (stats[key].type == "candidatepair" &&
        stats[key].nominated && stats[key].state == "succeeded") {
      var remote = stats[stats[key].remoteCandidateId];
      console.log("Connected to: " + remote.ipAddress +":"+
                  remote.portNumber +" "+ remote.transport +
                  " "+ remote.candidateType);
    }
  });
})
.catch(function(e) { console.log(e.name); });

This might output something like:

Connected to: 192.168.1.2:49190 udp host

which you could test against the LAN range. If instead it returned something like:

Connected to: 24.57.143.7:61102 udp relayed

then you'd have a TURN connection.

Here's a jsfiddle that shows this (requires Firefox Developer Edition for other reasons).

Problem

Is there any way to get information about the type of connection used in WebRTC in a programmatic way? For example in my app I use local connections as well as STUN and TURN. From the ICE candidates I can gather if the type of the candidates is host or relay, and on the server I can see if the connection is attempted via STUN (connection initiation) or TURN (steady stream during connection). Up until now I could not find a way to access the information about the finally used type of connection in the browser. There are the candidates, the browser stops gathering and then there is a working connection. Looking through the events I couldn't find any information though. I know that Chrome supports getStats() on the peerconnection, which allows me access to most of the information found in chrome://webrtc-internals, however I didn't find this information there either. Is there any way to access this information from javascript? Thank you very much.

Original source

Related problems