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