rtc_rtp/extension/abs_send_time_extension/
mod.rs1#[cfg(test)]
2mod abs_send_time_extension_test;
3
4use shared::{
5 error::{Error, Result},
6 marshal::{Marshal, MarshalSize, Unmarshal},
7};
8
9use bytes::{Buf, BufMut};
10
11pub const ABS_SEND_TIME_EXTENSION_SIZE: usize = 3;
13
14#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)]
17pub struct AbsSendTimeExtension {
18 pub timestamp: u64,
20}
21
22impl Unmarshal for AbsSendTimeExtension {
23 fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
25 where
26 Self: Sized,
27 B: Buf,
28 {
29 if raw_packet.remaining() < ABS_SEND_TIME_EXTENSION_SIZE {
30 return Err(Error::ErrBufferTooSmall);
31 }
32
33 let b0 = raw_packet.get_u8();
34 let b1 = raw_packet.get_u8();
35 let b2 = raw_packet.get_u8();
36 let timestamp = (b0 as u64) << 16 | (b1 as u64) << 8 | b2 as u64;
37
38 Ok(AbsSendTimeExtension { timestamp })
39 }
40}
41
42impl MarshalSize for AbsSendTimeExtension {
43 fn marshal_size(&self) -> usize {
45 ABS_SEND_TIME_EXTENSION_SIZE
46 }
47}
48
49impl Marshal for AbsSendTimeExtension {
50 fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
52 if buf.remaining_mut() < ABS_SEND_TIME_EXTENSION_SIZE {
53 return Err(Error::ErrBufferTooSmall);
54 }
55
56 buf.put_u8(((self.timestamp & 0xFF0000) >> 16) as u8);
57 buf.put_u8(((self.timestamp & 0xFF00) >> 8) as u8);
58 buf.put_u8((self.timestamp & 0xFF) as u8);
59
60 Ok(ABS_SEND_TIME_EXTENSION_SIZE)
61 }
62}
63
64impl AbsSendTimeExtension {
65 pub fn new(send_time_ntp: u64) -> Self {
67 AbsSendTimeExtension {
68 timestamp: send_time_ntp >> 14,
69 }
70 }
71
72 pub fn estimate(&self, receive_ntp: u64) -> u64 {
75 let mut ntp = receive_ntp & 0xFFFFFFC000000000 | (self.timestamp & 0xFFFFFF) << 14;
77 if receive_ntp < ntp {
78 ntp -= 0x1000000 << 14;
80 }
81
82 ntp
83 }
85}