use crate::statistics::report::RTCStatsReportEntry;
use crate::statistics::stats::RTCStats;
use shared::time::SystemInstant;
use std::hash::{Hash, Hasher};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy)]
pub(crate) struct Seal {
salt: Option<[u64; 2]>,
anchor_instant: Instant,
anchor_epoch: Duration,
}
impl Seal {
pub(crate) fn new(salt: Option<[u64; 2]>, anchor: SystemInstant) -> Self {
let anchor_epoch = anchor.duration_since_unix_epoch();
Self {
salt,
anchor_instant: anchor.instant(anchor_epoch),
anchor_epoch,
}
}
pub(crate) fn id_of(&self, name: &str) -> String {
let Some(salt) = self.salt else {
return name.to_owned();
};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
salt.hash(&mut hasher);
name.hash(&mut hasher);
format!("{:016x}", hasher.finish())
}
fn id(&self, name: &mut String) {
if !name.is_empty() {
*name = self.id_of(name);
}
}
fn optional_id(&self, name: &mut Option<String>) {
if let Some(name) = name {
self.id(name);
}
}
fn epoch_of(&self, instant: Instant) -> Duration {
match instant.checked_duration_since(self.anchor_instant) {
Some(after) => self.anchor_epoch + after,
None => self
.anchor_epoch
.saturating_sub(self.anchor_instant.duration_since(instant)),
}
}
fn timestamp(&self, stamp: &mut SystemInstant) {
let instant = stamp.instant(stamp.duration_since_unix_epoch());
*stamp = SystemInstant::at(instant, self.epoch_of(instant));
}
fn optional_timestamp(&self, stamp: &mut Option<SystemInstant>) {
if let Some(stamp) = stamp {
self.timestamp(stamp);
}
}
fn stats(&self, stats: &mut RTCStats) {
self.timestamp(&mut stats.timestamp);
self.id(&mut stats.id);
}
pub(crate) fn entry(&self, entry: &mut RTCStatsReportEntry) {
match entry {
RTCStatsReportEntry::PeerConnection(s) => self.stats(&mut s.stats),
RTCStatsReportEntry::Transport(s) => {
self.stats(&mut s.stats);
self.id(&mut s.selected_candidate_pair_id);
self.id(&mut s.local_certificate_id);
self.id(&mut s.remote_certificate_id);
}
RTCStatsReportEntry::IceCandidatePair(s) => {
self.stats(&mut s.stats);
self.id(&mut s.transport_id);
self.id(&mut s.local_candidate_id);
self.id(&mut s.remote_candidate_id);
self.optional_timestamp(&mut s.last_packet_sent_timestamp);
self.optional_timestamp(&mut s.last_packet_received_timestamp);
}
RTCStatsReportEntry::LocalCandidate(s) | RTCStatsReportEntry::RemoteCandidate(s) => {
self.stats(&mut s.stats);
self.id(&mut s.transport_id);
}
RTCStatsReportEntry::Certificate(s) => {
self.stats(&mut s.stats);
self.id(&mut s.issuer_certificate_id);
}
RTCStatsReportEntry::Codec(s) => {
self.stats(&mut s.stats);
self.id(&mut s.transport_id);
}
RTCStatsReportEntry::DataChannel(s) => self.stats(&mut s.stats),
RTCStatsReportEntry::InboundRtp(s) => {
let stream = &mut s.received_rtp_stream_stats.rtp_stream_stats;
self.stats(&mut stream.stats);
self.id(&mut stream.transport_id);
self.id(&mut stream.codec_id);
self.optional_id(&mut s.remote_id);
self.optional_id(&mut s.playout_id);
self.optional_timestamp(&mut s.last_packet_received_timestamp);
}
RTCStatsReportEntry::OutboundRtp(s) => {
let stream = &mut s.sent_rtp_stream_stats.rtp_stream_stats;
self.stats(&mut stream.stats);
self.id(&mut stream.transport_id);
self.id(&mut stream.codec_id);
self.id(&mut s.media_source_id);
self.optional_id(&mut s.remote_id);
}
RTCStatsReportEntry::RemoteInboundRtp(s) => {
let stream = &mut s.received_rtp_stream_stats.rtp_stream_stats;
self.stats(&mut stream.stats);
self.id(&mut stream.transport_id);
self.id(&mut stream.codec_id);
self.id(&mut s.local_id);
}
RTCStatsReportEntry::RemoteOutboundRtp(s) => {
let stream = &mut s.sent_rtp_stream_stats.rtp_stream_stats;
self.stats(&mut stream.stats);
self.id(&mut stream.transport_id);
self.id(&mut stream.codec_id);
self.id(&mut s.local_id);
}
RTCStatsReportEntry::AudioSource(s) => self.stats(&mut s.media_source_stats.stats),
RTCStatsReportEntry::VideoSource(s) => self.stats(&mut s.media_source_stats.stats),
RTCStatsReportEntry::AudioPlayout(s) => self.stats(&mut s.stats),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_salted_id_is_stable_on_a_connection_and_differs_between_connections() {
let anchor = SystemInstant::now(Instant::now());
let one = Seal::new(Some([1, 2]), anchor);
let other = Seal::new(Some([3, 4]), anchor);
assert_eq!(one.id_of("RTCTransport"), one.id_of("RTCTransport"));
assert_ne!(one.id_of("RTCTransport"), other.id_of("RTCTransport"));
assert_ne!(one.id_of("RTCTransport"), one.id_of("RTCPeerConnection"));
assert!(!one.id_of("RTCTransport").contains("RTC"));
assert_eq!(
Seal::new(None, anchor).id_of("RTCTransport"),
"RTCTransport"
);
}
#[test]
fn timestamps_keep_the_distance_between_their_instants() {
let t0 = Instant::now();
let anchor = SystemInstant::now(t0 + Duration::from_secs(10));
let seal = Seal::new(None, anchor);
let mut before = SystemInstant::now(t0);
let mut after = SystemInstant::now(t0 + Duration::from_secs(25));
seal.timestamp(&mut before);
seal.timestamp(&mut after);
assert_eq!(
after.duration_since_unix_epoch() - before.duration_since_unix_epoch(),
Duration::from_secs(25)
);
assert_eq!(
anchor.duration_since_unix_epoch() - before.duration_since_unix_epoch(),
Duration::from_secs(10),
"an instant before the anchor is that much earlier, not clamped to it"
);
assert_eq!(before.instant(before.duration_since_unix_epoch()), t0);
}
}