大网数据广告活动
Web 标准与前端开发

RTCCodecStats

· 52CE 我爱测

The **`RTCCodecStats`** dictionary of the WebRTC API provides statistics about a codec used by RTP streams that are being sent or received by the associated RTCPeerConnection object.

WebRTC

The RTCCodecStats dictionary of the WebRTC API provides statistics about a codec used by RTP streams that are being sent or received by the associated RTCPeerConnection object.

These statistics can be obtained by iterating the RTCStatsReport object returned by RTCPeerConnection.getStats() until you find an entry with the type of codec.

The codec statistics can be correlated with the inbound or outbound stream statistics (both local and remote) by matching their codecId property to the codec's id. For example, if RTCInboundRtpStreamStats.codecId matches an RTCCodecStats.id in the same report, then we know that the codec is being used on this peer connection's inbound stream. If no stream codecId 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 same id.

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 (unless sdpFmtpLine 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 distinct RTCCodecStats objects.

Instance properties

  • RTCCodecStats.channels
    • : A positive number indicating the number of channels supported by the codec.
  • RTCCodecStats.clockRate
    • : A positive number containing the media sampling rate.
  • RTCCodecStats.mimeType
    • : A string containing the media MIME type/subtype, such as video/VP8.
  • RTCCodecStats.payloadType
    • : A positive integer value in the range of 0 to 127 indicating the payload type used in RTP encoding or decoding.
  • RTCCodecStats.sdpFmtpLine
    • : A string containing the format-specific parameters of the "a=fmtp" line in the codec's SDP (if present).
  • RTCCodecStats.transportId
    • : A string containing the unique identifier of the transport on which this codec is being used. This can be used to match to the corresponding RTCTransportStats object.

Common instance properties

The following properties are common to all WebRTC statistics objects (see RTCStatsReport for more information):

  • RTCCodecStats.id
    • : A string that uniquely identifies the object that is being monitored to produce this set of statistics.
  • RTCCodecStats.timestamp
    • : A DOMHighResTimeStamp object indicating the time at which the sample was taken for this statistics object.
  • RTCCodecStats.type
    • : A string with the value "codec", indicating the type of statistics the object contains.

Examples

Given a variable myPeerConnection, which is an instance of RTCPeerConnection, the code below uses await to wait for the statistics report, and then iterates it using RTCStatsReport.forEach(). It then filters the dictionaries for just those reports that have the type of codec and logs the result.

const stats = await myPeerConnection.getStats();

stats.forEach((report) => {
  if (report.type === "codec") {
    // Log the codec information
    console.log(report);
  }
});

Specifications

Browser compatibility

See also

  • RTCStatsReport
  • codecs option in parameter passed to RTCRtpTransceiver.setCodecPreferences() and RTCRtpSender.setParameters()().
  • codecs property in object returned by RTCRtpSender.getParameters() and RTCRtpReceiver.getParameters().

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。