Connection Statistics
The stats APIs make it easy to get information about your ongoing connections and other WebRTC internals. They closely follow the WebRTC Statistics API RFC. Note that the underlying WebRTC statistics API is not supported consistently from browser to browser. In all cases where our statistics API is wrapping that provided by the browser, if the browser does not implement the given API, then the API returns a null stats object. This is the expected behaviour and the app code should be checking for null stats objects.
Note
About the code examples on this page:
- For .NET MAUI and Unity, use the C# code.
- For macOS, use the iOS code.
getStats
You can call the getStats
function on any active Connection
. For example, one way to do so would be to wire up an event to call getStats
once the Connection
state transitions to Connected
, and then unwire the event when the Connection state transitions to Closed
(or Failed
). Your event could be triggered by some UI element (like a button click), or fired on a Timer, whatever meets your use case.
connection.GetStats().Then((stats) => {
var transport = stats.Streams[0].Transport;
if (transport != null)
{
var localCandidates = transport.LocalCandidates;
var remoteCandidates = transport.RemoteCandidates;
var activeCandidatePair = transport.ActiveCandidatePair;
var activeLocalCandidateId = activeCandidatePair.LocalCandidateId;
var activeRemoteCandidateId = activeCandidatePair.RemoteCandidateId;
for (var i = 0; i < localCandidates.Length; i++)
{
var localCandidate = localCandidates[i];
if (localCandidate.Id == activeLocalCandidateId)
{
// this is the active local candidate
// check the protocol - UDP or TCP
var localCandidateProtocol = localCandidate.Protocol;
if (localCandidate.IsRelayed)
{
// check the relay server IP
var relayServerIPAddress = localCandidate.IPAddress;
}
}
}
for (var i = 0; i < remoteCandidates.Length; i++)
{
var remoteCandidate = remoteCandidates[i];
if (remoteCandidate.Id == activeRemoteCandidateId)
{
// this is the active remote candidate
if (remoteCandidate.IsRelayed)
{
// check the relay server IP
var relayServerIPAddress = remoteCandidate.IPAddress;
}
}
}
}
});
OnStats
The OnStats
event is raised when the current connection's statistics are gathered.
OnNetworkQuality
The OnNetworkQuality
event is raised when the connection's network quality is estimated. OnNetworkQuality
returns a value that ranges from 0.0 to 1.0, where 0.0 is the lowest quality and 1.0 is the highest quality.
connection.OnNetworkQuality += networkQuality =>
{
// networkQuality is value ranging from 0.0 (lowest quality) to 1.0 (highest quality)
};
OnMediaQuality
The OnMediaQuality
event is raised when the connection's media quality is estimated. OnMediaQuality
returns a value that ranges from 0.0 to 1.0, where 0.0 is the lowest quality and 1.0 is the highest quality.
The OnMediaQuality
and OnNetworkQuality
events are intentionally independent of each other. SFU downstream connections need to be able to distinguish between a poor network connection to the Media Server (a problem with me) and a poor media feed (a problem with you).
connection.OnMediaQuality += mediaQuality =>
{
// mediaQuality is value ranging from 0.0 (lowest quality) to 1.0 (highest quality)
};
Set Statistic Events Frequency
Use StatsEventInterval
to set the interval, in milliseconds, in which statistics events like OnStats
, OnNetworkQuality
, and onMediaQuality
are raised.