1use 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
15const CHUNK_MAX_CHUNK_SIZE: u32 = 16 * 1024 * 1024;
17
18const CHUNK_MAX_MESSAGE_LEN: u32 = 16 * 1024 * 1024;
20
21const CHUNK_MAX_CSID_EMBEDDED: usize = 16;
23const CHUNK_MAX_CSID_DEVICE: usize = 32;
25const CHUNK_MAX_CSID_SERVER: usize = 64;
27const CHUNK_MAX_CSID_HEADEND: usize = 256;
29
30const CHUNK_MAX_IN_PROGRESS_BYTES_EMBEDDED: usize = 16 * 1024 * 1024;
32const CHUNK_MAX_IN_PROGRESS_BYTES_DEVICE: usize = 16 * 1024 * 1024;
34const CHUNK_MAX_IN_PROGRESS_BYTES_SERVER: usize = 16 * 1024 * 1024;
36const CHUNK_MAX_IN_PROGRESS_BYTES_HEADEND: usize = 64 * 1024 * 1024;
38
39const AMF0_MAX_DEPTH_EMBEDDED: usize = 16;
41const AMF0_MAX_DEPTH_DEVICE: usize = 32;
43const AMF0_MAX_DEPTH_SERVER: usize = 32;
45const AMF0_MAX_DEPTH_HEADEND: usize = 32;
47
48const AMF0_MAX_VALUES_EMBEDDED: usize = 1024;
50const AMF0_MAX_VALUES_DEVICE: usize = 4096;
52const AMF0_MAX_VALUES_SERVER: usize = 4096;
54const AMF0_MAX_VALUES_HEADEND: usize = 16_384;
56
57const SERVER_MAX_COMMAND_LEN: usize = 64 * 1024;
59
60const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_EMBEDDED: u32 = 2;
62const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_DEVICE: u32 = 2;
64const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_SERVER: u32 = 3;
66const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_HEADEND: u32 = 8;
68
69const SESSION_CHUNK_SIZE: u32 = 4096;
71const SESSION_WINDOW_ACK_SIZE: u32 = 2_500_000;
73const SESSION_PEER_BANDWIDTH: u32 = 2_500_000;
75const SESSION_PLAY_BUFFER_EMBEDDED: Duration = Duration::from_millis(500);
77const SESSION_PLAY_BUFFER_DEVICE: Duration = Duration::from_secs(1);
79const SESSION_PLAY_BUFFER_SERVER: Duration = Duration::from_secs(3);
81const SESSION_PLAY_BUFFER_HEADEND: Duration = Duration::from_secs(3);
83
84impl ChunkLimits {
85 #[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 #[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 #[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 #[must_use]
122 pub fn with_max_csids(mut self, value: usize) -> Self {
123 self.max_csids = value;
124 self
125 }
126
127 #[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 #[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 #[must_use]
157 pub fn with_max_depth(mut self, value: usize) -> Self {
158 self.max_depth = value;
159 self
160 }
161
162 #[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 #[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 #[must_use]
182 pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
183 self.chunk = value;
184 self
185 }
186
187 #[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 #[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 #[must_use]
214 pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
215 self.chunk = value;
216 self
217 }
218
219 #[must_use]
221 pub fn with_amf0(mut self, value: crate::amf0::Amf0Limits) -> Self {
222 self.amf0 = value;
223 self
224 }
225
226 #[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 #[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 #[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 #[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}