Skip to main content

rtc/statistics/stats/
mod.rs

1//! WebRTC Statistics API types.
2//!
3//! This module contains the W3C WebRTC Statistics API types as defined in
4//! the [WebRTC Statistics API specification](https://www.w3.org/TR/webrtc-stats/).
5//!
6//! These types are returned by [`RTCPeerConnection::get_stats()`](crate::peer_connection::RTCPeerConnection::get_stats)
7//! and provide detailed information about the WebRTC connection, including:
8//!
9//! - Transport statistics (ICE, DTLS)
10//! - RTP stream statistics (inbound/outbound)
11//! - Data channel statistics
12//! - Codec information
13//! - ICE candidate information
14//!
15//! # Examples
16//!
17//! ```
18//! use rtc::peer_connection::RTCPeerConnection;
19//! use rtc::statistics::StatsSelector;
20//! use std::time::Instant;
21//!
22//! # fn example(peer_connection: &mut RTCPeerConnection) {
23//! let report = peer_connection.get_stats(Instant::now(), StatsSelector::None);
24//!
25//! // Access transport statistics
26//! if let Some(transport) = report.transport() {
27//!     println!("Bytes sent: {}", transport.bytes_sent);
28//!     println!("Bytes received: {}", transport.bytes_received);
29//! }
30//!
31//! // Iterate over data channels
32//! for dc in report.data_channels() {
33//!     println!("Channel '{}': {} messages sent", dc.label, dc.messages_sent);
34//! }
35//! # }
36//! ```
37
38use ::serde::{Deserialize, Serialize};
39use shared::serde::instant_to_epoch;
40use shared::time::SystemInstant;
41
42pub mod certificate;
43pub mod codec;
44pub mod data_channel;
45pub mod ice_candidate;
46pub mod ice_candidate_pair;
47pub mod media;
48pub mod peer_connection;
49pub mod rtp_stream;
50pub mod transport;
51
52/// The type of a statistics object.
53///
54/// This enum corresponds to the `RTCStatsType` enum in the W3C WebRTC Statistics API.
55/// Each variant represents a different category of statistics that can be collected.
56///
57/// # Serialization
58///
59/// Values are serialized using the W3C-specified lowercase hyphenated format
60/// (e.g., `InboundRTP` serializes to `"inbound-rtp"`).
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
62#[non_exhaustive]
63pub enum RTCStatsType {
64    /// Statistics for a media codec.
65    #[serde(rename = "codec")]
66    Codec,
67    /// Statistics for an inbound RTP stream.
68    #[serde(rename = "inbound-rtp")]
69    InboundRTP,
70    /// Statistics for an outbound RTP stream.
71    #[serde(rename = "outbound-rtp")]
72    OutboundRTP,
73    /// Statistics for an inbound RTP stream from the remote peer's perspective.
74    #[serde(rename = "remote-inbound-rtp")]
75    RemoteInboundRTP,
76    /// Statistics for an outbound RTP stream from the remote peer's perspective.
77    #[serde(rename = "remote-outbound-rtp")]
78    RemoteOutboundRTP,
79    /// Statistics for a media source (audio or video).
80    #[serde(rename = "media-source")]
81    MediaSource,
82    /// Statistics for audio playout.
83    #[serde(rename = "media-playout")]
84    MediaPlayout,
85    /// Statistics for the peer connection.
86    #[serde(rename = "peer-connection")]
87    PeerConnection,
88    /// Statistics for a data channel.
89    #[serde(rename = "data-channel")]
90    DataChannel,
91    /// Statistics for the transport layer.
92    #[serde(rename = "transport")]
93    Transport,
94    /// Statistics for an ICE candidate pair.
95    #[serde(rename = "candidate-pair")]
96    CandidatePair,
97    /// Statistics for a local ICE candidate.
98    #[serde(rename = "local-candidate")]
99    LocalCandidate,
100    /// Statistics for a remote ICE candidate.
101    #[serde(rename = "remote-candidate")]
102    RemoteCandidate,
103    /// Statistics for a certificate.
104    #[serde(rename = "certificate")]
105    Certificate,
106}
107
108/// The unique identifier for a statistics object.
109///
110/// Each statistics object has a unique ID that can be used to look up
111/// the object in the statistics report or to reference it from other
112/// statistics objects.
113pub type RTCStatsId = String;
114
115/// Base statistics fields common to all statistics objects.
116///
117/// This struct is embedded (via `#[serde(flatten)]`) in all specific
118/// statistics types and provides the common fields required by the
119/// W3C WebRTC Statistics API.
120///
121/// # JSON Serialization
122///
123/// The `timestamp` field is serialized as milliseconds since the Unix epoch,
124/// and `typ` is serialized as `"type"` with the W3C-specified string value.
125#[derive(Debug, Serialize, Deserialize)]
126pub struct RTCStats {
127    /// The timestamp when this statistics object was generated.
128    ///
129    /// Serialized as milliseconds since the Unix epoch for W3C compatibility.
130    #[serde(with = "instant_to_epoch")]
131    pub timestamp: SystemInstant,
132
133    /// The type of this statistics object.
134    #[serde(rename = "type")]
135    pub typ: RTCStatsType,
136
137    /// The unique identifier for this statistics object.
138    pub id: RTCStatsId,
139}
140
141/// The reason for quality limitation in video encoding.
142///
143/// This enum indicates why the video encoder may have reduced quality
144/// (resolution, frame rate, or bitrate) during encoding.
145#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
146#[non_exhaustive]
147pub enum RTCQualityLimitationReason {
148    /// No quality limitation is active.
149    #[default]
150    #[serde(rename = "none")]
151    None,
152    /// Quality is limited due to CPU constraints.
153    #[serde(rename = "cpu")]
154    Cpu,
155    /// Quality is limited due to bandwidth constraints.
156    #[serde(rename = "bandwidth")]
157    Bandwidth,
158    /// Quality is limited for another reason.
159    #[serde(rename = "other")]
160    Other,
161}