mmids_core/lib.rs
1//! This crate contains all the building blocks and foundational systems that a mmids application
2//! requires. It also contains standard workflow steps that are likely to be used in most
3//! mmids applications.
4
5extern crate pest;
6#[macro_use]
7extern crate pest_derive;
8
9use rml_rtmp::time::RtmpTimestamp;
10use std::num::Wrapping;
11use std::time::Duration;
12use tracing::error;
13
14pub mod codecs;
15pub mod config;
16pub mod endpoints;
17pub mod event_hub;
18pub mod http_api;
19pub mod net;
20pub mod reactors;
21#[cfg(test)]
22mod test_utils;
23mod utils;
24pub mod workflows;
25
26/// Unique identifier that identifies the flow of video end-to-end. Normally when media data enters
27/// the beginning of a workflow it will be given a unique stream identifier, and it will keep that
28/// identifier until it leaves the last stage of the workflow. This allows for logging to give
29/// visibility of how media is processed throughout it's all lifetime.
30///
31/// If a workflow has a step that requires media to leave the system and then come back in for
32/// further steps, than it should keep the same stream identifier. For example, if
33/// a workflow has an ffmpeg transcoding step in the workflow (e.g. to add a watermark), when
34/// ffmpeg pushes the video back in it will keep the same identifier.
35#[derive(Clone, Debug, PartialEq, Eq, Hash)]
36pub struct StreamId(pub String);
37
38/// Represents timestamps relevant to video data. Contains the decoding time stamp (dts) and
39/// presentation time stamp (dts).
40#[derive(Clone, Debug, PartialEq)]
41pub struct VideoTimestamp {
42 dts: Duration,
43 pts_offset: i32,
44}
45
46impl VideoTimestamp {
47 /// Creates a new video timestamp from RTMP data. RTMP packets contain a timestamp in the
48 /// RTMP header itself and a composition time offset in the `AVCVIDEOPACKET` header. The RTMP
49 /// timestamp is the decoding timestamp (dts), while the composition time offset is added to the
50 /// dts to get the presentation timestamp (pts).
51 pub fn from_rtmp_data(rtmp_timestamp: RtmpTimestamp, mut composition_time_offset: i32) -> Self {
52 if composition_time_offset < -8388608 || composition_time_offset > 8388607 {
53 error!("Composition time offset of {composition_time_offset} is out of 24 bit range. Leaving at zero");
54 composition_time_offset = 0;
55 }
56
57 VideoTimestamp {
58 dts: Duration::from_millis(rtmp_timestamp.value as u64),
59 pts_offset: composition_time_offset,
60 }
61 }
62
63 /// Creates a new video timestamp based on absolute dts and pts values.
64 pub fn from_durations(dts: Duration, pts: Duration) -> Self {
65 let mut pts_offset = pts.as_millis() as i64 - dts.as_millis() as i64;
66 if pts_offset < -8388608 || pts_offset > 8388607 {
67 error!("PTS ({pts:?}) and DTS ({dts:?}) differ by more than a 24 bit number. Setting pts = dts");
68 pts_offset = 0;
69 }
70
71 VideoTimestamp {
72 dts,
73 pts_offset: pts_offset as i32,
74 }
75 }
76
77 /// Creates a video timestamp at zero
78 pub fn from_zero() -> Self {
79 VideoTimestamp {
80 dts: Duration::new(0, 0),
81 pts_offset: 0,
82 }
83 }
84
85 /// Gets the decoding time stamp for this video packet
86 pub fn dts(&self) -> Duration {
87 self.dts
88 }
89
90 /// Gets the presentation time stamp for the video packet
91 pub fn pts(&self) -> Duration {
92 let mut dts = Wrapping(self.dts.as_millis() as u64);
93 if self.pts_offset > 0 {
94 dts += Wrapping(self.pts_offset as u64);
95 } else {
96 dts -= Wrapping((self.pts_offset * -1) as u64);
97 }
98
99 Duration::from_millis(dts.0)
100 }
101
102 /// Gets the offset from the decoding timestamp for the pts
103 pub fn pts_offset(&self) -> i32 {
104 self.pts_offset
105 }
106}