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//! # Quickstart
21//!
22//! Replay a real ffmpeg publish capture through the ingest server and collect
23//! the FLV it produces. Every bound is the caller's:
24//!
25//! ```rust
26//! use rtmp_runtime::amf0::Amf0Limits;
27//! use rtmp_runtime::chunk::ChunkLimits;
28//! use rtmp_runtime::server::{RtmpServerConfig, ServerEvent, ServerLimits, ServerSession};
29//!
30//! let limits = ServerLimits {
31//! chunk: ChunkLimits {
32//! max_chunk_size: 1 << 20,
33//! max_message_len: 1 << 20,
34//! max_csids: 64,
35//! max_in_progress_bytes: 1 << 22,
36//! },
37//! amf0: Amf0Limits { max_depth: 16, max_values: 1024 },
38//! max_command_len: 16 * 1024,
39//! max_failed_publish_attempts: 3,
40//! };
41//! let capture = test_fixtures::read("rtmp/obs-publish.bin");
42//! let mut session = ServerSession::new(RtmpServerConfig::new(limits, 4096, 2_500_000, 2_500_000));
43//! let mut flv = Vec::new();
44//! let mut published = false;
45//! for chunk in capture.chunks(1400) {
46//! let (_reply, events) = session.handle_data(chunk).unwrap();
47//! for ev in events {
48//! match ev {
49//! ServerEvent::Connected { app } => assert_eq!(app, "live"),
50//! ServerEvent::Publish { stream_key, .. } => {
51//! assert_eq!(stream_key, "testkey");
52//! published = true;
53//! }
54//! ServerEvent::Media { flv: bytes } => flv.extend_from_slice(&bytes),
55//! _ => {}
56//! }
57//! }
58//! }
59//! assert!(published && flv.starts_with(b"FLV"));
60//! ```
61//!
62//! # How do I...
63//!
64//! - Ingest a publish and get FLV out: [`server::ServerSession`],
65//! [`server::ServerEvent::Media`], `examples/ingest_to_flv.rs`.
66//! - Publish to a server: [`client::ClientSession`],
67//! [`client::ClientSession::send_video`],
68//! [`client::ClientSession::send_metadata`], `examples/publish_loopback.rs`.
69//! - Accept publishers on a TCP port with tokio: [`io::AsyncRtmpServer`]
70//! (feature `tokio`), `examples/capture_publish.rs` (it waits for an
71//! external publisher such as ffmpeg).
72//! - Read an `rtmp://` or `rtmps://` URL: [`target::RtmpTarget`] (feature
73//! `target`), `examples/parse_target.rs`.
74//! - Admit only one stream key: [`server::RtmpServerConfig`]'s
75//! `expected_stream_key`; no example yet.
76//! - Play a stream from a server: [`client::ClientMode::Play`],
77//! [`client::ClientEvent::Media`]; no example yet.
78//! - Walk the tags of an FLV file: [`flv::tags`], [`flv::FlvHeader`]; no
79//! example yet.
80//!
81//! # Modules
82//!
83//! Sessions: [`server`] (the ingest state machine), [`client`] (publish and
84//! play).
85//!
86//! Wire layer: [`handshake`] (C0/C1/C2 and S0/S1/S2), [`chunk`] (chunk stream
87//! assembly and writing), [`message`] (message types, protocol and user
88//! control), [`amf0`] (values and commands), [`ids`] (chunk stream, message
89//! stream, sequence and timestamp identities), [`flv`] (FLV file, tag and
90//! tag-body syntax).
91//!
92//! Sockets and URLs: [`io`] (feature `tokio`, the async server and client),
93//! [`target`] (feature `target`, the `rtmp[s]://` URL).
94//!
95//! Errors: [`error`].
96//!
97//! # Feature flags
98//!
99//! - `serde`: serde on the events, the AMF0 values, the limits and the FLV wire
100//! types.
101//! - `tokio`: [`io::AsyncRtmpServer`] and [`io::AsyncRtmpClient`]; implies
102//! `target`.
103//! - `target`: [`target::RtmpTarget`], the URL grammar (no sockets).
104//!
105//! # Spec citations
106//!
107//! Adobe RTMP specification 1.0 §5.2 (handshake), §5.3 (chunk stream), §5.4
108//! (protocol control), §6 (message format), §7.1 and §7.2 (message types and
109//! commands), transcribed in `private/specs-verified/rtmp/rtmp-runtime/rtmp.md`;
110//! AMF0 as the RTMP specification references it; the Adobe FLV and F4V file
111//! format specification v10.1 for the FLV tags.
112#![forbid(unsafe_code)]
113#![deny(missing_docs)]
114#![cfg_attr(docsrs, feature(doc_cfg))]
115#![cfg_attr(not(any(feature = "tokio", feature = "target", test)), no_std)]
116
117#[cfg(doctest)]
118#[doc = include_str!("../README.md")]
119struct ReadmeDoctests;
120
121extern crate alloc;
122
123pub mod amf0;
124pub mod chunk;
125pub mod client;
126#[cfg(feature = "tokio")]
127mod codec;
128mod damage;
129pub mod error;
130pub mod flv;
131pub mod handshake;
132pub mod ids;
133#[cfg(feature = "tokio")]
134#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
135pub mod io;
136pub mod message;
137pub mod server;
138#[cfg(feature = "target")]
139#[cfg_attr(docsrs, doc(cfg(feature = "target")))]
140pub mod target;
141#[cfg(test)]
142mod test_support;
143
144pub use error::RtmpError;
145pub use message::ChunkSize;
146
147/// The Adobe RTMP specification version this engine implements.
148pub const RTMP_VERSION: &str = "RTMP 1.0";