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