use crate::amf0::Amf0Limits;
use crate::chunk::ChunkLimits;
use crate::client::ClientLimits;
use crate::client::RtmpClientConfig;
use crate::server::RtmpServerConfig;
use crate::server::ServerLimits;
use broadcast_core::Profile;
use core::time::Duration;
const CHUNK_MAX_CHUNK_SIZE: u32 = 16 * 1024 * 1024;
const CHUNK_MAX_MESSAGE_LEN: u32 = 16 * 1024 * 1024;
const CHUNK_MAX_CSID_EMBEDDED: usize = 16;
const CHUNK_MAX_CSID_DEVICE: usize = 32;
const CHUNK_MAX_CSID_SERVER: usize = 64;
const CHUNK_MAX_CSID_HEADEND: usize = 256;
const CHUNK_MAX_IN_PROGRESS_BYTES_EMBEDDED: usize = 16 * 1024 * 1024;
const CHUNK_MAX_IN_PROGRESS_BYTES_DEVICE: usize = 16 * 1024 * 1024;
const CHUNK_MAX_IN_PROGRESS_BYTES_SERVER: usize = 16 * 1024 * 1024;
const CHUNK_MAX_IN_PROGRESS_BYTES_HEADEND: usize = 64 * 1024 * 1024;
const AMF0_MAX_DEPTH_EMBEDDED: usize = 16;
const AMF0_MAX_DEPTH_DEVICE: usize = 32;
const AMF0_MAX_DEPTH_SERVER: usize = 32;
const AMF0_MAX_DEPTH_HEADEND: usize = 32;
const AMF0_MAX_VALUES_EMBEDDED: usize = 1024;
const AMF0_MAX_VALUES_DEVICE: usize = 4096;
const AMF0_MAX_VALUES_SERVER: usize = 4096;
const AMF0_MAX_VALUES_HEADEND: usize = 16_384;
const SERVER_MAX_COMMAND_LEN: usize = 64 * 1024;
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_EMBEDDED: u32 = 2;
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_DEVICE: u32 = 2;
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_SERVER: u32 = 3;
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_HEADEND: u32 = 8;
const SESSION_CHUNK_SIZE: u32 = 4096;
const SESSION_WINDOW_ACK_SIZE: u32 = 2_500_000;
const SESSION_PEER_BANDWIDTH: u32 = 2_500_000;
const SESSION_PLAY_BUFFER_EMBEDDED: Duration = Duration::from_millis(500);
const SESSION_PLAY_BUFFER_DEVICE: Duration = Duration::from_secs(1);
const SESSION_PLAY_BUFFER_SERVER: Duration = Duration::from_secs(3);
const SESSION_PLAY_BUFFER_HEADEND: Duration = Duration::from_secs(3);
impl ChunkLimits {
#[must_use]
pub fn for_profile(profile: Profile) -> Self {
Self {
max_chunk_size: CHUNK_MAX_CHUNK_SIZE,
max_message_len: CHUNK_MAX_MESSAGE_LEN,
max_csids: profile.select(
CHUNK_MAX_CSID_EMBEDDED,
CHUNK_MAX_CSID_DEVICE,
CHUNK_MAX_CSID_SERVER,
CHUNK_MAX_CSID_HEADEND,
),
max_in_progress_bytes: profile.select(
CHUNK_MAX_IN_PROGRESS_BYTES_EMBEDDED,
CHUNK_MAX_IN_PROGRESS_BYTES_DEVICE,
CHUNK_MAX_IN_PROGRESS_BYTES_SERVER,
CHUNK_MAX_IN_PROGRESS_BYTES_HEADEND,
),
}
}
#[must_use]
pub fn with_max_chunk_size(mut self, value: u32) -> Self {
self.max_chunk_size = value;
self
}
#[must_use]
pub fn with_max_message_len(mut self, value: u32) -> Self {
self.max_message_len = value;
self
}
#[must_use]
pub fn with_max_csids(mut self, value: usize) -> Self {
self.max_csids = value;
self
}
#[must_use]
pub fn with_max_in_progress_bytes(mut self, value: usize) -> Self {
self.max_in_progress_bytes = value;
self
}
}
impl Amf0Limits {
#[must_use]
pub fn for_profile(profile: Profile) -> Self {
Self {
max_depth: profile.select(
AMF0_MAX_DEPTH_EMBEDDED,
AMF0_MAX_DEPTH_DEVICE,
AMF0_MAX_DEPTH_SERVER,
AMF0_MAX_DEPTH_HEADEND,
),
max_values: profile.select(
AMF0_MAX_VALUES_EMBEDDED,
AMF0_MAX_VALUES_DEVICE,
AMF0_MAX_VALUES_SERVER,
AMF0_MAX_VALUES_HEADEND,
),
}
}
#[must_use]
pub fn with_max_depth(mut self, value: usize) -> Self {
self.max_depth = value;
self
}
#[must_use]
pub fn with_max_values(mut self, value: usize) -> Self {
self.max_values = value;
self
}
}
impl ClientLimits {
#[must_use]
pub fn for_profile(profile: Profile) -> Self {
Self {
chunk: ChunkLimits::for_profile(profile),
amf0: Amf0Limits::for_profile(profile),
}
}
#[must_use]
pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
self.chunk = value;
self
}
#[must_use]
pub fn with_amf0(mut self, value: crate::amf0::Amf0Limits) -> Self {
self.amf0 = value;
self
}
}
impl ServerLimits {
#[must_use]
pub fn for_profile(profile: Profile) -> Self {
Self {
chunk: ChunkLimits::for_profile(profile),
amf0: Amf0Limits::for_profile(profile),
max_command_len: SERVER_MAX_COMMAND_LEN,
max_failed_publish_attempts: profile.select(
SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_EMBEDDED,
SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_DEVICE,
SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_SERVER,
SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_HEADEND,
),
}
}
#[must_use]
pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
self.chunk = value;
self
}
#[must_use]
pub fn with_amf0(mut self, value: crate::amf0::Amf0Limits) -> Self {
self.amf0 = value;
self
}
#[must_use]
pub fn with_max_command_len(mut self, value: usize) -> Self {
self.max_command_len = value;
self
}
#[must_use]
pub fn with_max_failed_publish_attempts(mut self, value: u32) -> Self {
self.max_failed_publish_attempts = value;
self
}
}
impl RtmpServerConfig {
#[must_use]
pub fn for_profile(profile: Profile) -> Self {
Self::new(
ServerLimits::for_profile(profile),
SESSION_CHUNK_SIZE,
SESSION_WINDOW_ACK_SIZE,
SESSION_PEER_BANDWIDTH,
)
}
}
impl RtmpClientConfig {
#[must_use]
pub fn for_profile(profile: Profile) -> Self {
Self::new(
ClientLimits::for_profile(profile),
SESSION_CHUNK_SIZE,
SESSION_WINDOW_ACK_SIZE,
profile.select(
SESSION_PLAY_BUFFER_EMBEDDED,
SESSION_PLAY_BUFFER_DEVICE,
SESSION_PLAY_BUFFER_SERVER,
SESSION_PLAY_BUFFER_HEADEND,
),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn server_and_headend_literals_are_pinned() {
let chunk = ChunkLimits::for_profile(Profile::Server);
assert_eq!(chunk.max_chunk_size, 16 * 1024 * 1024);
assert_eq!(chunk.max_message_len, 16 * 1024 * 1024);
assert_eq!(chunk.max_csids, 64);
assert_eq!(chunk.max_in_progress_bytes, 16 * 1024 * 1024);
assert_eq!(
ChunkLimits::for_profile(Profile::Headend).max_in_progress_bytes,
64 * 1024 * 1024
);
let amf0 = Amf0Limits::for_profile(Profile::Server);
assert_eq!(amf0.max_depth, 32);
assert_eq!(amf0.max_values, 4096);
assert_eq!(Amf0Limits::for_profile(Profile::Headend).max_values, 16_384);
let server = crate::server::ServerLimits::for_profile(Profile::Server);
assert_eq!(server.max_command_len, 64 * 1024);
assert_eq!(server.max_failed_publish_attempts, 3);
assert_eq!(server.chunk, chunk);
assert_eq!(server.amf0, amf0);
assert_eq!(
crate::server::ServerLimits::for_profile(Profile::Headend).max_failed_publish_attempts,
8
);
}
#[test]
fn session_presets_are_the_values_the_origin_always_used() {
let server = RtmpServerConfig::for_profile(Profile::Server);
assert_eq!(server.chunk_size, 4096);
assert_eq!(server.window_ack_size, 2_500_000);
assert_eq!(server.peer_bandwidth, 2_500_000);
assert_eq!(server.limits, ServerLimits::for_profile(Profile::Server));
let client = RtmpClientConfig::for_profile(Profile::Server);
assert_eq!(client.play_buffer, Duration::from_secs(3));
assert_eq!(client.chunk_size, 4096);
assert_eq!(
RtmpClientConfig::for_profile(Profile::Device).play_buffer,
Duration::from_secs(1)
);
}
#[test]
fn per_unit_caps_are_equal_on_every_profile() {
for profile in Profile::ALL {
let chunk = ChunkLimits::for_profile(profile);
assert_eq!(chunk.max_message_len, 16 * 1024 * 1024, "{profile:?}");
assert_eq!(
crate::server::ServerLimits::for_profile(profile).max_command_len,
64 * 1024,
"{profile:?}"
);
assert!(
chunk.max_in_progress_bytes >= chunk.max_message_len as usize,
"{profile:?}"
);
assert_eq!(chunk.max_chunk_size, 16 * 1024 * 1024, "{profile:?}");
}
}
#[test]
fn capacities_grow_with_the_profile() {
let in_progress = Profile::ALL.map(|p| ChunkLimits::for_profile(p).max_in_progress_bytes);
assert!(in_progress.windows(2).all(|w| w[0] <= w[1]));
let values = Profile::ALL.map(|p| Amf0Limits::for_profile(p).max_values);
assert!(values.windows(2).all(|w| w[0] <= w[1]));
}
}