Prompt
How do I get call quality metrics for WebRTC sessions
Latest observation
To get call quality metrics for WebRTC sessions, you typically read them from the browser’s built-in WebRTC stats APIs and, if needed, supplement with your own signaling/app telemetry.
1) Use RTCPeerConnection.getStats()
This is the primary source of WebRTC quality metrics.
What you can measure
Common metrics include:
- Packet loss: audio/video send and receive loss
- Jitter: variation in packet arrival time
- Round-trip time (RTT): network latency estimate
- Bitrate / bytes sent/received
- Frame rate / frame dimensions for video
- Audio concealment / samples lost
- NACK/PLI/FIR counts for video recovery behavior
- Candidate pair info: current network path, local/remote IPs, transport type
Example
async function logWebRTCStats(pc) {
const stats = await pc.getStats();
stats.forEach(report => {
if (report.type === 'inbound-rtp' && !report.isRemote) {
console.log('Inbound', {
kind: report.kind,
packetsReceived: report.packetsReceived,
packetsLost: report.packetsLost,
jitter: report.jitter,
bytesReceived: report.bytesReceived,
framesDecoded: report.framesDecoded,
frameWidth: report.frameWidth,
frameHeight: report.frameHeight,
});
}
if (report.type === 'outbound-rtp' && !report.isRemote) {
console.log('Outbound', {
kind: report.kind,
packetsSent: report.packetsSent,
bytesSent: report.bytesSent,
framesEncoded: report.framesEncoded,
});
}
if (report.type === 'candidate-pair' && report.state === 'succeeded' && report.selected) {
console.log('Selected candidate pair', {
currentRoundTripTime: report.currentRoundTripTime,
availableOutgoingBitrate: report.availableOutgoingBitrate,
availableIncomingBitrate: report.availableIncomingBitrate,
});
}
});
}
Call it periodically:
setInterval(() => logWebRTCStats(pc), 5000);
2) Derive higher-level quality indicators
Raw stats are useful, but call quality is often expressed as derived metrics:
- Packet loss %
lost / (received + lost)or use deltas over time
- Jitter buffer delay
- Effective bitrate
- bytes delta over time × 8
- Video quality
- resolution, frames per second, freeze count
- Audio quality
- concealment, jitter, late packets
For better accuracy, compare delta values between samples rather than absolute totals.
3) Correlate with signaling and session data
To understand call issues, combine WebRTC stats with:
- session start/end timestamps
- ICE connection state changes
- track mute/unmute events
- selected codec
- device/browser info
- server-side SFU/MCU stats if you use one
Useful events:
pc.oniceconnectionstatechange = () => {
console.log('ICE state:', pc.iceConnectionState);
};
pc.onconnectionstatechange = () => {
console.log('PeerConnection state:', pc.connectionState);
};
4) If you use an SFU or media server
If your app uses Twilio, Daily, Agora, Janus, mediasoup, Jitsi, etc., you can often get:
- server-side RTP stats
- subscriber/publisher packet loss
- relay bandwidth
- per-participant quality
- server relay latency
This is often easier for fleet-wide monitoring than client-only stats.
5) Convert stats into user-facing QoE metrics
Common quality KPIs:
- MOS-like score or “good/fair/poor”
- Call setup time
- Audio/video freeze rate
- One-way latency estimate
- Join success rate
- ICE failure rate
Example thresholds (rough, app-dependent):
- RTT > 300 ms: poor
- packet loss > 2–5%: noticeable issues
- jitter > 30 ms: potential audio/video instability
- video FPS < 15: degraded experience
6) Tools and libraries
You can use:
- Native browser WebRTC stats API
- WebRTC internals in Chrome:
chrome://webrtc-internals - Analytics libraries/platforms:
- callstats.io
- Twilio Insights
- Daily analytics
- Agora analytics
- your own telemetry pipeline
7) Best practice implementation approach
- Poll
getStats()every 2–5 seconds. - Store time-series data for each peer connection.
- Compute deltas for bytes/packets/frames.
- Capture ICE/connection state transitions.
- Send aggregated metrics to your backend.
- Alert on thresholds or sudden changes.
If you want, I can give you:
- a complete JavaScript WebRTC stats collector,
- metric formulas for packet loss/jitter/bitrate, or
- a React/Node example that sends call quality telemetry to your backend.