RTCCodecStats
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
* Some parts of this feature may have varying levels of support.
TheRTCCodecStats dictionary of theWebRTC API provides statistics about a codec used byRTP streams that are being sent or received by the associatedRTCPeerConnection object.
These statistics can be obtained by iterating theRTCStatsReport object returned byRTCPeerConnection.getStats() until you find an entry with thetype ofcodec.
The codec statistics can be correlated with the inbound or outbound stream statistics (both local and remote) by matching theircodecId property to the codec'sid.For example, ifRTCInboundRtpStreamStats.codecId matches anRTCCodecStats.id in the same report, then we know that the codec is being used on this peer connection's inbound stream.If no streamcodecId references a codec statistic, then that codec statistic object is deleted — if the codec is used again, the statistics object will be recreated with the sameid.
Codec objects may be referenced by multiple RTP streams in media sections using the same transport.In fact, user agents are expected to consolidate information into a single "codec" entry per payload type per transport (unlesssdpFmtpLine is different when sending or receiving, in which case, different codecs will be needed for encoding and decoding).Note that other transports will use their own distinctRTCCodecStats objects.
In this article
Instance properties
channelsOptionalA positive number indicating the number of channels supported by the codec.
clockRateOptionalA positive number containing the media sampling rate.
mimeTypeA string containing the media MIME type/subtype, such as video/VP8.
payloadTypeA positive integer value in the range of 0 to 127 indicating the payload type used in RTP encoding or decoding.
sdpFmtpLineOptionalA string containing the format-specific parameters of the
"a=fmtp"line in the codec'sSDP (if present).transportIdA string containing the unique identifier of the transport on which this codec is being used.This can be used to match to the corresponding
RTCTransportStatsobject.
Common instance properties
The following properties are common to all WebRTC statistics objects (seeRTCStatsReport for more information):
idA string that uniquely identifies the object that is being monitored to produce this set of statistics.
timestampA
DOMHighResTimeStampobject indicating the time at which the sample was taken for this statistics object.typeA string with the value
"codec", indicating the type of statistics the object contains.
Examples
Given a variablemyPeerConnection, which is an instance ofRTCPeerConnection, the code below usesawait to wait for the statistics report, and then iterates it usingRTCStatsReport.forEach().It then filters the dictionaries for just those reports that have the type ofcodec and logs the result.
const stats = await myPeerConnection.getStats();stats.forEach((report) => { if (report.type === "codec") { // Log the codec information console.log(report); }});Specifications
| Specification |
|---|
| Identifiers for WebRTC's Statistics API> # dom-rtcstatstype-codec> |
Browser compatibility
See also
RTCStatsReportcodecsoption in parameter passed toRTCRtpTransceiver.setCodecPreferences()andRTCRtpSender.setParameters()().codecsproperty in object returned byRTCRtpSender.getParameters()andRTCRtpReceiver.getParameters().