msf_rtp/utils/
mod.rs

1//! Helpers.
2
3pub mod reorder;
4
5use std::time::{Duration, SystemTime};
6
7use crate::{
8    rtcp::{CompoundRtcpPacket, RtcpPacket},
9    rtp::{IncomingRtpPacket, OrderedRtpPacket, RtpPacket},
10};
11
12/// RTP or RTCP packet.
13///
14/// This is useful when RTP and RTCP packets can be multiplexed in a single
15/// channel. See RFC 5761 for more info.
16#[derive(Clone)]
17pub enum PacketMux<P = RtpPacket> {
18    Rtp(P),
19    Rtcp(CompoundRtcpPacket),
20}
21
22impl From<RtpPacket> for PacketMux {
23    #[inline]
24    fn from(packet: RtpPacket) -> Self {
25        Self::Rtp(packet)
26    }
27}
28
29impl From<IncomingRtpPacket> for PacketMux<IncomingRtpPacket> {
30    #[inline]
31    fn from(packet: IncomingRtpPacket) -> Self {
32        Self::Rtp(packet)
33    }
34}
35
36impl From<OrderedRtpPacket> for PacketMux<OrderedRtpPacket> {
37    #[inline]
38    fn from(packet: OrderedRtpPacket) -> Self {
39        Self::Rtp(packet)
40    }
41}
42
43impl<P> From<RtcpPacket> for PacketMux<P> {
44    #[inline]
45    fn from(packet: RtcpPacket) -> Self {
46        Self::Rtcp(packet.into())
47    }
48}
49
50impl<P> From<CompoundRtcpPacket> for PacketMux<P> {
51    #[inline]
52    fn from(packet: CompoundRtcpPacket) -> Self {
53        Self::Rtcp(packet)
54    }
55}
56
57/// Get NTP timestamp as a 32.32 fixed point number.
58///
59/// This timestamp can be used for RTCP sender reports.
60pub fn ntp_timestamp() -> u64 {
61    let ts = SystemTime::now()
62        .duration_since(SystemTime::UNIX_EPOCH)
63        .unwrap_or(Duration::ZERO);
64
65    let s = ts.as_secs();
66    let n = ts.subsec_nanos();
67
68    let f = (n as u64) * (1u64 << 32) / 1_000_000_000u64;
69
70    ((s + 2_208_988_800) << 32) + f
71}