Skip to main content

rtmp_runtime/
presets.rs

1//! The `broadcast_core::Profile` presets of this crate's bounds. Every number is
2//! a documented constant used only by a `for_profile` body; the caller replaces
3//! any field through its typed `with_*` setter. No preset refuses a message a
4//! real encoder sends.
5
6use crate::amf0::Amf0Limits;
7use crate::chunk::ChunkLimits;
8use crate::client::ClientLimits;
9use crate::client::RtmpClientConfig;
10use crate::server::RtmpServerConfig;
11use crate::server::ServerLimits;
12use broadcast_core::Profile;
13use core::time::Duration;
14
15/// Every profile: 16 MiB, so no peer's Set Chunk Size is refused (a refused size mis-frames the session); memory is bounded by the in-progress cap.
16const CHUNK_MAX_CHUNK_SIZE: u32 = 16 * 1024 * 1024;
17
18/// Every profile: 16 MiB, above the 24-bit `message_length` field's largest value, a ceiling not an allocation, so no real 4K keyframe is dropped.
19const CHUNK_MAX_MESSAGE_LEN: u32 = 16 * 1024 * 1024;
20
21/// Embedded: 16 chunk streams, several times the handful a real peer opens. Measured (#4834): exercised on a real fixture by the Embedded harness.
22const CHUNK_MAX_CSID_EMBEDDED: usize = 16;
23/// Device: 32 chunk streams, several times the handful a real server opens.
24const CHUNK_MAX_CSID_DEVICE: usize = 32;
25/// Server: 64 chunk streams, several times what an encoder opens.
26const CHUNK_MAX_CSID_SERVER: usize = 64;
27/// Headend: 256 chunk streams across a contribution encoder's many streams.
28const CHUNK_MAX_CSID_HEADEND: usize = 256;
29
30/// Embedded: 16 MiB, one largest message in progress. Measured (#4834): exercised on a real fixture by the Embedded harness.
31const CHUNK_MAX_IN_PROGRESS_BYTES_EMBEDDED: usize = 16 * 1024 * 1024;
32/// Device: 16 MiB, one largest message in progress.
33const CHUNK_MAX_IN_PROGRESS_BYTES_DEVICE: usize = 16 * 1024 * 1024;
34/// Server: 16 MiB, one largest message in progress.
35const CHUNK_MAX_IN_PROGRESS_BYTES_SERVER: usize = 16 * 1024 * 1024;
36/// Headend: 64 MiB across every in-progress message of a busy contribution link.
37const CHUNK_MAX_IN_PROGRESS_BYTES_HEADEND: usize = 64 * 1024 * 1024;
38
39/// Embedded: 16 levels, far deeper than any command or metadata object nests. Measured (#4834): exercised on a real fixture by the Embedded harness.
40const AMF0_MAX_DEPTH_EMBEDDED: usize = 16;
41/// Device: 32 levels, far deeper than any command or metadata object nests.
42const AMF0_MAX_DEPTH_DEVICE: usize = 32;
43/// Server: 32 levels, far deeper than any command or metadata object nests.
44const AMF0_MAX_DEPTH_SERVER: usize = 32;
45/// Headend: 32 levels, far deeper than any command or metadata object nests.
46const AMF0_MAX_DEPTH_HEADEND: usize = 32;
47
48/// Embedded: 1024 values, many times a live `connect` or `onMetaData` body. Measured (#4834): exercised on a real fixture by the Embedded harness.
49const AMF0_MAX_VALUES_EMBEDDED: usize = 1024;
50/// Device: 4096 values, many times a live `onMetaData` body.
51const AMF0_MAX_VALUES_DEVICE: usize = 4096;
52/// Server: 4096 values, many times an encoder's `onMetaData` body.
53const AMF0_MAX_VALUES_SERVER: usize = 4096;
54/// Headend: 16 384 values, room for an `onMetaData` with a keyframe index.
55const AMF0_MAX_VALUES_HEADEND: usize = 16_384;
56
57/// Every profile: 64 KiB of command body, several times a real `connect`.
58const SERVER_MAX_COMMAND_LEN: usize = 64 * 1024;
59
60/// Embedded: 2 wrong stream keys, a mistyped key and its retry. No run-time cost: a timing or retry count.
61const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_EMBEDDED: u32 = 2;
62/// Device: 2 wrong stream keys, a mistyped key and its retry.
63const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_DEVICE: u32 = 2;
64/// Server: 3 wrong stream keys before the connection is closed, slowing key guessing.
65const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_SERVER: u32 = 3;
66/// Headend: 8 wrong stream keys, for an encoder cycling its configured keys.
67const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_HEADEND: u32 = 8;
68
69/// Every profile: announce a 4096-byte chunk size (Set Chunk Size), as encoders do.
70const SESSION_CHUNK_SIZE: u32 = 4096;
71/// Every profile: announce a 2 500 000-byte Window Acknowledgement Size, as encoders do.
72const SESSION_WINDOW_ACK_SIZE: u32 = 2_500_000;
73/// Every profile: an ingest announces a 2 500 000-byte Set Peer Bandwidth, matching its window.
74const SESSION_PEER_BANDWIDTH: u32 = 2_500_000;
75/// Embedded: announce a 500 ms buffer before `play`. No run-time cost: a timing or retry count.
76const SESSION_PLAY_BUFFER_EMBEDDED: Duration = Duration::from_millis(500);
77/// Device: announce a 1 s buffer before `play`.
78const SESSION_PLAY_BUFFER_DEVICE: Duration = Duration::from_secs(1);
79/// Server: announce a 3 s buffer before `play`, the value the origin always used.
80const SESSION_PLAY_BUFFER_SERVER: Duration = Duration::from_secs(3);
81/// Headend: announce a 3 s buffer before `play`.
82const SESSION_PLAY_BUFFER_HEADEND: Duration = Duration::from_secs(3);
83
84impl ChunkLimits {
85    /// The [`Profile`] preset, replaceable through its `with_*` setters.
86    #[must_use]
87    pub fn for_profile(profile: Profile) -> Self {
88        Self {
89            max_chunk_size: CHUNK_MAX_CHUNK_SIZE,
90            max_message_len: CHUNK_MAX_MESSAGE_LEN,
91            max_csids: profile.select(
92                CHUNK_MAX_CSID_EMBEDDED,
93                CHUNK_MAX_CSID_DEVICE,
94                CHUNK_MAX_CSID_SERVER,
95                CHUNK_MAX_CSID_HEADEND,
96            ),
97            max_in_progress_bytes: profile.select(
98                CHUNK_MAX_IN_PROGRESS_BYTES_EMBEDDED,
99                CHUNK_MAX_IN_PROGRESS_BYTES_DEVICE,
100                CHUNK_MAX_IN_PROGRESS_BYTES_SERVER,
101                CHUNK_MAX_IN_PROGRESS_BYTES_HEADEND,
102            ),
103        }
104    }
105
106    /// Replace [`Self::max_chunk_size`].
107    #[must_use]
108    pub fn with_max_chunk_size(mut self, value: u32) -> Self {
109        self.max_chunk_size = value;
110        self
111    }
112
113    /// Replace [`Self::max_message_len`].
114    #[must_use]
115    pub fn with_max_message_len(mut self, value: u32) -> Self {
116        self.max_message_len = value;
117        self
118    }
119
120    /// Replace [`Self::max_csids`].
121    #[must_use]
122    pub fn with_max_csids(mut self, value: usize) -> Self {
123        self.max_csids = value;
124        self
125    }
126
127    /// Replace [`Self::max_in_progress_bytes`].
128    #[must_use]
129    pub fn with_max_in_progress_bytes(mut self, value: usize) -> Self {
130        self.max_in_progress_bytes = value;
131        self
132    }
133}
134
135impl Amf0Limits {
136    /// The [`Profile`] preset, replaceable through its `with_*` setters.
137    #[must_use]
138    pub fn for_profile(profile: Profile) -> Self {
139        Self {
140            max_depth: profile.select(
141                AMF0_MAX_DEPTH_EMBEDDED,
142                AMF0_MAX_DEPTH_DEVICE,
143                AMF0_MAX_DEPTH_SERVER,
144                AMF0_MAX_DEPTH_HEADEND,
145            ),
146            max_values: profile.select(
147                AMF0_MAX_VALUES_EMBEDDED,
148                AMF0_MAX_VALUES_DEVICE,
149                AMF0_MAX_VALUES_SERVER,
150                AMF0_MAX_VALUES_HEADEND,
151            ),
152        }
153    }
154
155    /// Replace [`Self::max_depth`].
156    #[must_use]
157    pub fn with_max_depth(mut self, value: usize) -> Self {
158        self.max_depth = value;
159        self
160    }
161
162    /// Replace [`Self::max_values`].
163    #[must_use]
164    pub fn with_max_values(mut self, value: usize) -> Self {
165        self.max_values = value;
166        self
167    }
168}
169
170impl ClientLimits {
171    /// The [`Profile`] preset, replaceable through its `with_*` setters.
172    #[must_use]
173    pub fn for_profile(profile: Profile) -> Self {
174        Self {
175            chunk: ChunkLimits::for_profile(profile),
176            amf0: Amf0Limits::for_profile(profile),
177        }
178    }
179
180    /// Replace [`Self::chunk`].
181    #[must_use]
182    pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
183        self.chunk = value;
184        self
185    }
186
187    /// Replace [`Self::amf0`].
188    #[must_use]
189    pub fn with_amf0(mut self, value: crate::amf0::Amf0Limits) -> Self {
190        self.amf0 = value;
191        self
192    }
193}
194
195impl ServerLimits {
196    /// The [`Profile`] preset, replaceable through its `with_*` setters.
197    #[must_use]
198    pub fn for_profile(profile: Profile) -> Self {
199        Self {
200            chunk: ChunkLimits::for_profile(profile),
201            amf0: Amf0Limits::for_profile(profile),
202            max_command_len: SERVER_MAX_COMMAND_LEN,
203            max_failed_publish_attempts: profile.select(
204                SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_EMBEDDED,
205                SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_DEVICE,
206                SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_SERVER,
207                SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_HEADEND,
208            ),
209        }
210    }
211
212    /// Replace [`Self::chunk`].
213    #[must_use]
214    pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
215        self.chunk = value;
216        self
217    }
218
219    /// Replace [`Self::amf0`].
220    #[must_use]
221    pub fn with_amf0(mut self, value: crate::amf0::Amf0Limits) -> Self {
222        self.amf0 = value;
223        self
224    }
225
226    /// Replace [`Self::max_command_len`].
227    #[must_use]
228    pub fn with_max_command_len(mut self, value: usize) -> Self {
229        self.max_command_len = value;
230        self
231    }
232
233    /// Replace [`Self::max_failed_publish_attempts`].
234    #[must_use]
235    pub fn with_max_failed_publish_attempts(mut self, value: u32) -> Self {
236        self.max_failed_publish_attempts = value;
237        self
238    }
239}
240
241impl RtmpServerConfig {
242    /// The [`Profile`] preset: an ingest admitting any publisher under
243    /// [`ServerLimits::for_profile`], replaceable through its `with_*` setters
244    /// and public fields.
245    #[must_use]
246    pub fn for_profile(profile: Profile) -> Self {
247        Self::new(
248            ServerLimits::for_profile(profile),
249            SESSION_CHUNK_SIZE,
250            SESSION_WINDOW_ACK_SIZE,
251            SESSION_PEER_BANDWIDTH,
252        )
253    }
254}
255
256impl RtmpClientConfig {
257    /// The [`Profile`] preset: a publishing client under
258    /// [`ClientLimits::for_profile`]; `app`, `stream_key` and `tc_url` start
259    /// empty, as with [`Self::new`].
260    #[must_use]
261    pub fn for_profile(profile: Profile) -> Self {
262        Self::new(
263            ClientLimits::for_profile(profile),
264            SESSION_CHUNK_SIZE,
265            SESSION_WINDOW_ACK_SIZE,
266            profile.select(
267                SESSION_PLAY_BUFFER_EMBEDDED,
268                SESSION_PLAY_BUFFER_DEVICE,
269                SESSION_PLAY_BUFFER_SERVER,
270                SESSION_PLAY_BUFFER_HEADEND,
271            ),
272        )
273    }
274}
275
276#[cfg(test)]
277mod tests {
278    use super::*;
279
280    #[test]
281    fn server_and_headend_literals_are_pinned() {
282        let chunk = ChunkLimits::for_profile(Profile::Server);
283        assert_eq!(chunk.max_chunk_size, 16 * 1024 * 1024);
284        assert_eq!(chunk.max_message_len, 16 * 1024 * 1024);
285        assert_eq!(chunk.max_csids, 64);
286        assert_eq!(chunk.max_in_progress_bytes, 16 * 1024 * 1024);
287        assert_eq!(
288            ChunkLimits::for_profile(Profile::Headend).max_in_progress_bytes,
289            64 * 1024 * 1024
290        );
291        let amf0 = Amf0Limits::for_profile(Profile::Server);
292        assert_eq!(amf0.max_depth, 32);
293        assert_eq!(amf0.max_values, 4096);
294        assert_eq!(Amf0Limits::for_profile(Profile::Headend).max_values, 16_384);
295        let server = crate::server::ServerLimits::for_profile(Profile::Server);
296        assert_eq!(server.max_command_len, 64 * 1024);
297        assert_eq!(server.max_failed_publish_attempts, 3);
298        assert_eq!(server.chunk, chunk);
299        assert_eq!(server.amf0, amf0);
300        assert_eq!(
301            crate::server::ServerLimits::for_profile(Profile::Headend).max_failed_publish_attempts,
302            8
303        );
304    }
305
306    #[test]
307    fn session_presets_are_the_values_the_origin_always_used() {
308        let server = RtmpServerConfig::for_profile(Profile::Server);
309        assert_eq!(server.chunk_size, 4096);
310        assert_eq!(server.window_ack_size, 2_500_000);
311        assert_eq!(server.peer_bandwidth, 2_500_000);
312        assert_eq!(server.limits, ServerLimits::for_profile(Profile::Server));
313        let client = RtmpClientConfig::for_profile(Profile::Server);
314        assert_eq!(client.play_buffer, Duration::from_secs(3));
315        assert_eq!(client.chunk_size, 4096);
316        assert_eq!(
317            RtmpClientConfig::for_profile(Profile::Device).play_buffer,
318            Duration::from_secs(1)
319        );
320    }
321
322    #[test]
323    fn per_unit_caps_are_equal_on_every_profile() {
324        for profile in Profile::ALL {
325            let chunk = ChunkLimits::for_profile(profile);
326            assert_eq!(chunk.max_message_len, 16 * 1024 * 1024, "{profile:?}");
327            assert_eq!(
328                crate::server::ServerLimits::for_profile(profile).max_command_len,
329                64 * 1024,
330                "{profile:?}"
331            );
332            assert!(
333                chunk.max_in_progress_bytes >= chunk.max_message_len as usize,
334                "{profile:?}"
335            );
336            assert_eq!(chunk.max_chunk_size, 16 * 1024 * 1024, "{profile:?}");
337        }
338    }
339
340    #[test]
341    fn capacities_grow_with_the_profile() {
342        let in_progress = Profile::ALL.map(|p| ChunkLimits::for_profile(p).max_in_progress_bytes);
343        assert!(in_progress.windows(2).all(|w| w[0] <= w[1]));
344        let values = Profile::ALL.map(|p| Amf0Limits::for_profile(p).max_values);
345        assert!(values.windows(2).all(|w| w[0] <= w[1]));
346    }
347}