rtmp-runtime 0.13.1

Sans-IO RTMP 1.0 (Adobe Real-Time Messaging Protocol) publish engine for both ends: a driveable ingest server state machine and a publish client over the handshake, chunk stream, AMF0, and message layers; optional tokio socket adapter.
Documentation
//! The `broadcast_core::Profile` presets of this crate's bounds. Every number is
//! a documented constant used only by a `for_profile` body; the caller replaces
//! any field through its typed `with_*` setter. No preset refuses a message a
//! real encoder sends.

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;

/// 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.
const CHUNK_MAX_CHUNK_SIZE: u32 = 16 * 1024 * 1024;

/// 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.
const CHUNK_MAX_MESSAGE_LEN: u32 = 16 * 1024 * 1024;

/// Embedded: 16 chunk streams, several times the handful a real peer opens. Measured (#4834): exercised on a real fixture by the Embedded harness.
const CHUNK_MAX_CSID_EMBEDDED: usize = 16;
/// Device: 32 chunk streams, several times the handful a real server opens.
const CHUNK_MAX_CSID_DEVICE: usize = 32;
/// Server: 64 chunk streams, several times what an encoder opens.
const CHUNK_MAX_CSID_SERVER: usize = 64;
/// Headend: 256 chunk streams across a contribution encoder's many streams.
const CHUNK_MAX_CSID_HEADEND: usize = 256;

/// Embedded: 16 MiB, one largest message in progress. Measured (#4834): exercised on a real fixture by the Embedded harness.
const CHUNK_MAX_IN_PROGRESS_BYTES_EMBEDDED: usize = 16 * 1024 * 1024;
/// Device: 16 MiB, one largest message in progress.
const CHUNK_MAX_IN_PROGRESS_BYTES_DEVICE: usize = 16 * 1024 * 1024;
/// Server: 16 MiB, one largest message in progress.
const CHUNK_MAX_IN_PROGRESS_BYTES_SERVER: usize = 16 * 1024 * 1024;
/// Headend: 64 MiB across every in-progress message of a busy contribution link.
const CHUNK_MAX_IN_PROGRESS_BYTES_HEADEND: usize = 64 * 1024 * 1024;

/// Embedded: 16 levels, far deeper than any command or metadata object nests. Measured (#4834): exercised on a real fixture by the Embedded harness.
const AMF0_MAX_DEPTH_EMBEDDED: usize = 16;
/// Device: 32 levels, far deeper than any command or metadata object nests.
const AMF0_MAX_DEPTH_DEVICE: usize = 32;
/// Server: 32 levels, far deeper than any command or metadata object nests.
const AMF0_MAX_DEPTH_SERVER: usize = 32;
/// Headend: 32 levels, far deeper than any command or metadata object nests.
const AMF0_MAX_DEPTH_HEADEND: usize = 32;

/// Embedded: 1024 values, many times a live `connect` or `onMetaData` body. Measured (#4834): exercised on a real fixture by the Embedded harness.
const AMF0_MAX_VALUES_EMBEDDED: usize = 1024;
/// Device: 4096 values, many times a live `onMetaData` body.
const AMF0_MAX_VALUES_DEVICE: usize = 4096;
/// Server: 4096 values, many times an encoder's `onMetaData` body.
const AMF0_MAX_VALUES_SERVER: usize = 4096;
/// Headend: 16 384 values, room for an `onMetaData` with a keyframe index.
const AMF0_MAX_VALUES_HEADEND: usize = 16_384;

/// Every profile: 64 KiB of command body, several times a real `connect`.
const SERVER_MAX_COMMAND_LEN: usize = 64 * 1024;

/// Embedded: 2 wrong stream keys, a mistyped key and its retry. No run-time cost: a timing or retry count.
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_EMBEDDED: u32 = 2;
/// Device: 2 wrong stream keys, a mistyped key and its retry.
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_DEVICE: u32 = 2;
/// Server: 3 wrong stream keys before the connection is closed, slowing key guessing.
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_SERVER: u32 = 3;
/// Headend: 8 wrong stream keys, for an encoder cycling its configured keys.
const SERVER_MAX_FAILED_PUBLISH_ATTEMPTS_HEADEND: u32 = 8;

/// Every profile: announce a 4096-byte chunk size (Set Chunk Size), as encoders do.
const SESSION_CHUNK_SIZE: u32 = 4096;
/// Every profile: announce a 2 500 000-byte Window Acknowledgement Size, as encoders do.
const SESSION_WINDOW_ACK_SIZE: u32 = 2_500_000;
/// Every profile: an ingest announces a 2 500 000-byte Set Peer Bandwidth, matching its window.
const SESSION_PEER_BANDWIDTH: u32 = 2_500_000;
/// Embedded: announce a 500 ms buffer before `play`. No run-time cost: a timing or retry count.
const SESSION_PLAY_BUFFER_EMBEDDED: Duration = Duration::from_millis(500);
/// Device: announce a 1 s buffer before `play`.
const SESSION_PLAY_BUFFER_DEVICE: Duration = Duration::from_secs(1);
/// Server: announce a 3 s buffer before `play`, the value the origin always used.
const SESSION_PLAY_BUFFER_SERVER: Duration = Duration::from_secs(3);
/// Headend: announce a 3 s buffer before `play`.
const SESSION_PLAY_BUFFER_HEADEND: Duration = Duration::from_secs(3);

impl ChunkLimits {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[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,
            ),
        }
    }

    /// Replace [`Self::max_chunk_size`].
    #[must_use]
    pub fn with_max_chunk_size(mut self, value: u32) -> Self {
        self.max_chunk_size = value;
        self
    }

    /// Replace [`Self::max_message_len`].
    #[must_use]
    pub fn with_max_message_len(mut self, value: u32) -> Self {
        self.max_message_len = value;
        self
    }

    /// Replace [`Self::max_csids`].
    #[must_use]
    pub fn with_max_csids(mut self, value: usize) -> Self {
        self.max_csids = value;
        self
    }

    /// Replace [`Self::max_in_progress_bytes`].
    #[must_use]
    pub fn with_max_in_progress_bytes(mut self, value: usize) -> Self {
        self.max_in_progress_bytes = value;
        self
    }
}

impl Amf0Limits {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[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,
            ),
        }
    }

    /// Replace [`Self::max_depth`].
    #[must_use]
    pub fn with_max_depth(mut self, value: usize) -> Self {
        self.max_depth = value;
        self
    }

    /// Replace [`Self::max_values`].
    #[must_use]
    pub fn with_max_values(mut self, value: usize) -> Self {
        self.max_values = value;
        self
    }
}

impl ClientLimits {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[must_use]
    pub fn for_profile(profile: Profile) -> Self {
        Self {
            chunk: ChunkLimits::for_profile(profile),
            amf0: Amf0Limits::for_profile(profile),
        }
    }

    /// Replace [`Self::chunk`].
    #[must_use]
    pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
        self.chunk = value;
        self
    }

    /// Replace [`Self::amf0`].
    #[must_use]
    pub fn with_amf0(mut self, value: crate::amf0::Amf0Limits) -> Self {
        self.amf0 = value;
        self
    }
}

impl ServerLimits {
    /// The [`Profile`] preset, replaceable through its `with_*` setters.
    #[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,
            ),
        }
    }

    /// Replace [`Self::chunk`].
    #[must_use]
    pub fn with_chunk(mut self, value: crate::chunk::ChunkLimits) -> Self {
        self.chunk = value;
        self
    }

    /// Replace [`Self::amf0`].
    #[must_use]
    pub fn with_amf0(mut self, value: crate::amf0::Amf0Limits) -> Self {
        self.amf0 = value;
        self
    }

    /// Replace [`Self::max_command_len`].
    #[must_use]
    pub fn with_max_command_len(mut self, value: usize) -> Self {
        self.max_command_len = value;
        self
    }

    /// Replace [`Self::max_failed_publish_attempts`].
    #[must_use]
    pub fn with_max_failed_publish_attempts(mut self, value: u32) -> Self {
        self.max_failed_publish_attempts = value;
        self
    }
}

impl RtmpServerConfig {
    /// The [`Profile`] preset: an ingest admitting any publisher under
    /// [`ServerLimits::for_profile`], replaceable through its `with_*` setters
    /// and public fields.
    #[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 {
    /// The [`Profile`] preset: a publishing client under
    /// [`ClientLimits::for_profile`]; `app`, `stream_key` and `tc_url` start
    /// empty, as with [`Self::new`].
    #[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]));
    }
}