Skip to main content

rtmp_runtime/
lib.rs

1//! Sans-IO RTMP 1.0 (the Adobe RTMP specification): the simple handshake,
2//! chunk-stream assembly and writing, protocol control and user control
3//! messages, AMF0 commands, and the session engines for both ends of a live
4//! push. [`server::ServerSession`] is an ingest server that accepts a publish
5//! from OBS or ffmpeg (`connect`, `createStream`, `publish`) and hands the
6//! audio, video and data back as FLV bytes; [`client::ClientSession`] publishes
7//! to a remote server, or plays from one in [`client::ClientMode::Play`]. The
8//! crate also owns the FLV tag syntax an RTMP message carries ([`flv`]).
9//!
10//! `transmux`'s streaming FLV demuxer reads the FLV the server emits;
11//! `multimux` builds its RTMP input and push output, and `broadcast-client` its
12//! RTMP play source, on this crate. The core is `no_std` + `alloc`, opens no
13//! socket and reads no clock: bytes go in, bytes to write and events come out.
14//! Every bound ([`chunk::ChunkLimits`], [`amf0::Amf0Limits`], the session
15//! limits) is the caller's. A discarded or undecodable message is reported as
16//! damage in order with the events around it; only a stream that cannot be
17//! framed is an error. The optional `tokio` feature adds socket adapters and
18//! `target` the `rtmp[s]://` URL grammar.
19//!
20//! # Choosing limits
21//!
22//! No limit struct here has a `Default`. Each one offers
23//! `for_profile(broadcast_core::Profile)`, a complete preset whose every
24//! number is a documented constant, and a typed `with_*` setter per field:
25//!
26//! | Profile | Sized for |
27//! |---|---|
28//! | `Embedded` | one program, a few hundred MB of memory, 32-bit or wasm32, few elementary streams (values checked against the 256 MiB budget where exercised, #4834) |
29//! | `Device` | single-service playback on a phone, tablet, TV or desktop, about 30 s of read-ahead |
30//! | `Server` | a live origin or HLS server carrying several programs |
31//! | `Headend` | a whole multiplex with full SI/EPG, up to 2048 PIDs |
32//!
33//! The presets: [`chunk::ChunkLimits`], [`amf0::Amf0Limits`],
34//! [`client::ClientLimits`] and [`server::ServerLimits`]. A preset delivers a
35//! damaged unit flagged; dropping it is the caller's opt-in.
36//!
37//! # Quickstart
38//!
39//! Replay a real ffmpeg publish capture through the ingest server and collect
40//! the FLV it produces. Every bound is the caller's:
41//!
42//! ```rust
43//! use rtmp_runtime::amf0::Amf0Limits;
44//! use rtmp_runtime::chunk::ChunkLimits;
45//! use rtmp_runtime::server::{RtmpServerConfig, ServerEvent, ServerLimits, ServerSession};
46//!
47//! let limits = ServerLimits {
48//!     chunk: ChunkLimits {
49//!         max_chunk_size: 1 << 20,
50//!         max_message_len: 1 << 20,
51//!         max_csids: 64,
52//!         max_in_progress_bytes: 1 << 22,
53//!     },
54//!     amf0: Amf0Limits { max_depth: 16, max_values: 1024 },
55//!     max_command_len: 16 * 1024,
56//!     max_failed_publish_attempts: 3,
57//! };
58//! let capture = test_fixtures::read("rtmp/obs-publish.bin");
59//! let mut session = ServerSession::new(RtmpServerConfig::new(limits, 4096, 2_500_000, 2_500_000));
60//! let mut flv = Vec::new();
61//! let mut published = false;
62//! for chunk in capture.chunks(1400) {
63//!     let (_reply, events) = session.handle_data(chunk).unwrap();
64//!     for ev in events {
65//!         match ev {
66//!             ServerEvent::Connected { app } => assert_eq!(app, "live"),
67//!             ServerEvent::Publish { stream_key, .. } => {
68//!                 assert_eq!(stream_key, "testkey");
69//!                 published = true;
70//!             }
71//!             ServerEvent::Media { flv: bytes } => flv.extend_from_slice(&bytes),
72//!             _ => {}
73//!         }
74//!     }
75//! }
76//! assert!(published && flv.starts_with(b"FLV"));
77//! ```
78//!
79//! # How do I...
80//!
81//! - Ingest a publish and get FLV out: [`server::ServerSession`],
82//!   [`server::ServerEvent::Media`], `examples/ingest_to_flv.rs`.
83//! - Publish to a server: [`client::ClientSession`],
84//!   [`client::ClientSession::send_video`],
85//!   [`client::ClientSession::send_metadata`], `examples/publish_loopback.rs`.
86//! - Accept publishers on a TCP port with tokio: [`io::AsyncRtmpServer`]
87//!   (feature `tokio`), `examples/capture_publish.rs` (it waits for an
88//!   external publisher such as ffmpeg).
89//! - Read an `rtmp://` or `rtmps://` URL: [`target::RtmpTarget`] (feature
90//!   `target`), `examples/parse_target.rs`.
91//! - Admit only one stream key: [`server::RtmpServerConfig`]'s
92//!   `expected_stream_key`; no example yet.
93//! - Play a stream from a server: [`client::ClientMode::Play`],
94//!   [`client::ClientEvent::Media`]; no example yet.
95//! - Walk the tags of an FLV file: [`flv::tags`], [`flv::FlvHeader`]; no
96//!   example yet.
97//!
98//! # Modules
99//!
100//! Sessions: [`server`] (the ingest state machine), [`client`] (publish and
101//! play).
102//!
103//! Wire layer: [`handshake`] (C0/C1/C2 and S0/S1/S2), [`chunk`] (chunk stream
104//! assembly and writing), [`message`] (message types, protocol and user
105//! control), [`amf0`] (values and commands), [`ids`] (chunk stream, message
106//! stream, sequence and timestamp identities), [`flv`] (FLV file, tag and
107//! tag-body syntax).
108//!
109//! Sockets and URLs: [`io`] (feature `tokio`, the async server and client),
110//! [`target`] (feature `target`, the `rtmp[s]://` URL).
111//!
112//! Errors: [`error`].
113//!
114//! # Feature flags
115//!
116//! - `serde`: serde on the events, the AMF0 values, the limits and the FLV wire
117//!   types.
118//! - `tokio`: [`io::AsyncRtmpServer`] and [`io::AsyncRtmpClient`]; implies
119//!   `target`.
120//! - `target`: [`target::RtmpTarget`], the URL grammar (no sockets).
121//!
122//! # Spec citations
123//!
124//! Adobe RTMP specification 1.0 §5.2 (handshake), §5.3 (chunk stream), §5.4
125//! (protocol control), §6 (message format), §7.1 and §7.2 (message types and
126//! commands), transcribed in `private/specs-verified/rtmp/rtmp-runtime/rtmp.md`;
127//! AMF0 as the RTMP specification references it; the Adobe FLV and F4V file
128//! format specification v10.1 for the FLV tags.
129#![forbid(unsafe_code)]
130#![deny(missing_docs)]
131#![cfg_attr(docsrs, feature(doc_cfg))]
132#![cfg_attr(not(any(feature = "tokio", feature = "target", test)), no_std)]
133
134#[cfg(doctest)]
135#[doc = include_str!("../README.md")]
136struct ReadmeDoctests;
137
138extern crate alloc;
139
140pub mod amf0;
141pub mod chunk;
142pub mod client;
143#[cfg(feature = "tokio")]
144mod codec;
145mod damage;
146pub mod error;
147pub mod flv;
148pub mod handshake;
149pub mod ids;
150#[cfg(feature = "tokio")]
151#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
152pub mod io;
153pub mod message;
154pub mod presets;
155pub mod server;
156#[cfg(feature = "target")]
157#[cfg_attr(docsrs, doc(cfg(feature = "target")))]
158pub mod target;
159#[cfg(test)]
160mod test_support;
161
162pub use error::RtmpError;
163pub use message::ChunkSize;
164
165/// The Adobe RTMP specification version this engine implements.
166pub const RTMP_VERSION: &str = "RTMP 1.0";