1use crate::statistics::stats::RTCStatsType;
7use crate::statistics::stats::certificate::RTCCertificateStats;
8use crate::statistics::stats::codec::RTCCodecStats;
9use crate::statistics::stats::data_channel::RTCDataChannelStats;
10use crate::statistics::stats::ice_candidate::RTCIceCandidateStats;
11use crate::statistics::stats::ice_candidate_pair::RTCIceCandidatePairStats;
12use crate::statistics::stats::media::audio_playout::RTCAudioPlayoutStats;
13use crate::statistics::stats::media::audio_source::RTCAudioSourceStats;
14use crate::statistics::stats::media::video_source::RTCVideoSourceStats;
15use crate::statistics::stats::peer_connection::RTCPeerConnectionStats;
16use crate::statistics::stats::rtp_stream::received::inbound::RTCInboundRtpStreamStats;
17use crate::statistics::stats::rtp_stream::received::remote_inbound::RTCRemoteInboundRtpStreamStats;
18use crate::statistics::stats::rtp_stream::sent::outbound::RTCOutboundRtpStreamStats;
19use crate::statistics::stats::rtp_stream::sent::remote_outbound::RTCRemoteOutboundRtpStreamStats;
20use crate::statistics::stats::transport::RTCTransportStats;
21use std::collections::HashMap;
22
23#[derive(Debug)]
27#[non_exhaustive]
28pub enum RTCStatsReportEntry {
29 PeerConnection(RTCPeerConnectionStats),
31 Transport(RTCTransportStats),
33 IceCandidatePair(RTCIceCandidatePairStats),
35 LocalCandidate(RTCIceCandidateStats),
37 RemoteCandidate(RTCIceCandidateStats),
39 Certificate(RTCCertificateStats),
41 Codec(RTCCodecStats),
43 DataChannel(RTCDataChannelStats),
45 InboundRtp(RTCInboundRtpStreamStats),
47 OutboundRtp(RTCOutboundRtpStreamStats),
49 RemoteInboundRtp(RTCRemoteInboundRtpStreamStats),
51 RemoteOutboundRtp(RTCRemoteOutboundRtpStreamStats),
53 AudioSource(RTCAudioSourceStats),
55 VideoSource(RTCVideoSourceStats),
57 AudioPlayout(RTCAudioPlayoutStats),
59}
60
61impl RTCStatsReportEntry {
62 pub fn stats_type(&self) -> RTCStatsType {
64 match self {
65 RTCStatsReportEntry::PeerConnection(_) => RTCStatsType::PeerConnection,
66 RTCStatsReportEntry::Transport(_) => RTCStatsType::Transport,
67 RTCStatsReportEntry::IceCandidatePair(_) => RTCStatsType::CandidatePair,
68 RTCStatsReportEntry::LocalCandidate(_) => RTCStatsType::LocalCandidate,
69 RTCStatsReportEntry::RemoteCandidate(_) => RTCStatsType::RemoteCandidate,
70 RTCStatsReportEntry::Certificate(_) => RTCStatsType::Certificate,
71 RTCStatsReportEntry::Codec(_) => RTCStatsType::Codec,
72 RTCStatsReportEntry::DataChannel(_) => RTCStatsType::DataChannel,
73 RTCStatsReportEntry::InboundRtp(_) => RTCStatsType::InboundRTP,
74 RTCStatsReportEntry::OutboundRtp(_) => RTCStatsType::OutboundRTP,
75 RTCStatsReportEntry::RemoteInboundRtp(_) => RTCStatsType::RemoteInboundRTP,
76 RTCStatsReportEntry::RemoteOutboundRtp(_) => RTCStatsType::RemoteOutboundRTP,
77 RTCStatsReportEntry::AudioSource(_) => RTCStatsType::MediaSource,
78 RTCStatsReportEntry::VideoSource(_) => RTCStatsType::MediaSource,
79 RTCStatsReportEntry::AudioPlayout(_) => RTCStatsType::MediaPlayout,
80 }
81 }
82
83 pub fn id(&self) -> &str {
85 match self {
86 RTCStatsReportEntry::PeerConnection(s) => &s.stats.id,
87 RTCStatsReportEntry::Transport(s) => &s.stats.id,
88 RTCStatsReportEntry::IceCandidatePair(s) => &s.stats.id,
89 RTCStatsReportEntry::LocalCandidate(s) => &s.stats.id,
90 RTCStatsReportEntry::RemoteCandidate(s) => &s.stats.id,
91 RTCStatsReportEntry::Certificate(s) => &s.stats.id,
92 RTCStatsReportEntry::Codec(s) => &s.stats.id,
93 RTCStatsReportEntry::DataChannel(s) => &s.stats.id,
94 RTCStatsReportEntry::InboundRtp(s) => {
95 &s.received_rtp_stream_stats.rtp_stream_stats.stats.id
96 }
97 RTCStatsReportEntry::OutboundRtp(s) => {
98 &s.sent_rtp_stream_stats.rtp_stream_stats.stats.id
99 }
100 RTCStatsReportEntry::RemoteInboundRtp(s) => {
101 &s.received_rtp_stream_stats.rtp_stream_stats.stats.id
102 }
103 RTCStatsReportEntry::RemoteOutboundRtp(s) => {
104 &s.sent_rtp_stream_stats.rtp_stream_stats.stats.id
105 }
106 RTCStatsReportEntry::AudioSource(s) => &s.media_source_stats.stats.id,
107 RTCStatsReportEntry::VideoSource(s) => &s.media_source_stats.stats.id,
108 RTCStatsReportEntry::AudioPlayout(s) => &s.stats.id,
109 }
110 }
111}
112
113#[derive(Debug, Default)]
146pub struct RTCStatsReport {
147 entries: HashMap<String, RTCStatsReportEntry>,
149 order: Vec<String>,
151}
152
153impl RTCStatsReport {
154 pub(crate) fn new(entries: Vec<RTCStatsReportEntry>) -> Self {
156 let mut map = HashMap::new();
157 let mut order = Vec::with_capacity(entries.len());
158
159 for entry in entries {
160 let id = entry.id().to_string();
161 order.push(id.clone());
162 map.insert(id, entry);
163 }
164
165 Self {
166 entries: map,
167 order,
168 }
169 }
170
171 pub fn len(&self) -> usize {
173 self.entries.len()
174 }
175
176 pub fn is_empty(&self) -> bool {
178 self.entries.is_empty()
179 }
180
181 pub fn get(&self, id: &str) -> Option<&RTCStatsReportEntry> {
183 self.entries.get(id)
184 }
185
186 pub fn contains(&self, id: &str) -> bool {
188 self.entries.contains_key(id)
189 }
190
191 pub fn iter(&self) -> impl Iterator<Item = &RTCStatsReportEntry> {
193 self.order.iter().filter_map(|id| self.entries.get(id))
194 }
195
196 pub fn iter_by_type(&self, typ: RTCStatsType) -> impl Iterator<Item = &RTCStatsReportEntry> {
198 self.iter().filter(move |e| e.stats_type() == typ)
199 }
200
201 pub fn ids(&self) -> impl Iterator<Item = &str> {
203 self.order.iter().map(|s| s.as_str())
204 }
205
206 pub fn peer_connection(&self) -> Option<&RTCPeerConnectionStats> {
212 self.get("RTCPeerConnection").and_then(|e| match e {
213 RTCStatsReportEntry::PeerConnection(s) => Some(s),
214 _ => None,
215 })
216 }
217
218 pub fn transport(&self) -> Option<&RTCTransportStats> {
220 self.entries.values().find_map(|e| match e {
222 RTCStatsReportEntry::Transport(s) => Some(s),
223 _ => None,
224 })
225 }
226
227 pub fn inbound_rtp_streams(&self) -> impl Iterator<Item = &RTCInboundRtpStreamStats> {
229 self.iter().filter_map(|e| match e {
230 RTCStatsReportEntry::InboundRtp(s) => Some(s),
231 _ => None,
232 })
233 }
234
235 pub fn outbound_rtp_streams(&self) -> impl Iterator<Item = &RTCOutboundRtpStreamStats> {
237 self.iter().filter_map(|e| match e {
238 RTCStatsReportEntry::OutboundRtp(s) => Some(s),
239 _ => None,
240 })
241 }
242
243 pub fn data_channels(&self) -> impl Iterator<Item = &RTCDataChannelStats> {
245 self.iter().filter_map(|e| match e {
246 RTCStatsReportEntry::DataChannel(s) => Some(s),
247 _ => None,
248 })
249 }
250
251 pub fn candidate_pairs(&self) -> impl Iterator<Item = &RTCIceCandidatePairStats> {
253 self.iter().filter_map(|e| match e {
254 RTCStatsReportEntry::IceCandidatePair(s) => Some(s),
255 _ => None,
256 })
257 }
258}