rtci_rtp/extension/abs_capture_time_extension/mod.rs
1#[cfg(test)]
2mod abs_capture_time_extension_test;
3
4use shared::{
5 error::{Error, Result},
6 marshal::{Marshal, MarshalSize, Unmarshal},
7};
8
9use bytes::{Buf, BufMut};
10
11/// The element's size without the clock offset.
12pub const ABS_CAPTURE_TIME_EXTENSION_SIZE: usize = 8;
13/// The element's size with the clock offset.
14pub const ABS_CAPTURE_TIME_EXTENDED_EXTENSION_SIZE: usize = 16;
15
16/// The absolute capture time extension
17/// (`http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time`, a libwebrtc experiment):
18/// when the first frame of the packet was captured, on the capturer's NTP clock, and
19/// optionally that clock's estimated offset from the sender's.
20///
21/// ```text
22/// 0 1 2 3
23/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
24/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25/// | ID | len=7 | absolute capture timestamp (bit 0-23) |
26/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27/// | absolute capture timestamp (bit 24-55) |
28/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29/// | ... (56-63) |
30/// +-+-+-+-+-+-+-+-+
31/// ```
32///
33/// With the offset the element is 16 bytes (`len=15`): the timestamp, then the offset.
34#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)]
35pub struct AbsCaptureTimeExtension {
36 /// The capture time as an NTP timestamp: unsigned 32.32 fixed point seconds since 1900.
37 pub timestamp: u64,
38 /// The estimated offset of the capturer's clock from the sender's, signed 32.32 fixed
39 /// point seconds, when the sender knows it.
40 pub estimated_capture_clock_offset: Option<i64>,
41}
42
43impl Unmarshal for AbsCaptureTimeExtension {
44 /// # Errors
45 ///
46 /// Fails for an element that is neither 8 nor 16 bytes.
47 fn unmarshal<B>(buf: &mut B) -> Result<Self>
48 where
49 Self: Sized,
50 B: Buf,
51 {
52 match buf.remaining() {
53 ABS_CAPTURE_TIME_EXTENSION_SIZE => Ok(AbsCaptureTimeExtension {
54 timestamp: buf.get_u64(),
55 estimated_capture_clock_offset: None,
56 }),
57 ABS_CAPTURE_TIME_EXTENDED_EXTENSION_SIZE => Ok(AbsCaptureTimeExtension {
58 timestamp: buf.get_u64(),
59 estimated_capture_clock_offset: Some(buf.get_i64()),
60 }),
61 _ => Err(Error::ErrBufferTooSmall),
62 }
63 }
64}
65
66impl MarshalSize for AbsCaptureTimeExtension {
67 fn marshal_size(&self) -> usize {
68 if self.estimated_capture_clock_offset.is_some() {
69 ABS_CAPTURE_TIME_EXTENDED_EXTENSION_SIZE
70 } else {
71 ABS_CAPTURE_TIME_EXTENSION_SIZE
72 }
73 }
74}
75
76impl Marshal for AbsCaptureTimeExtension {
77 fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
78 if buf.remaining_mut() < self.marshal_size() {
79 return Err(Error::ErrBufferTooSmall);
80 }
81 buf.put_u64(self.timestamp);
82 if let Some(offset) = self.estimated_capture_clock_offset {
83 buf.put_i64(offset);
84 }
85 Ok(self.marshal_size())
86 }
87}