Skip to main content

rtmp_runtime/
error.rs

1//! Error type for the RTMP ingest session engine.
2//!
3//! Structured [`thiserror`] errors covering the failure surface of the
4//! sans-IO engine: handshake framing (§5.2), chunk stream parsing (§5.3),
5//! message reassembly (§6), AMF0 decoding (`[AMF0]`), and session state-machine
6//! violations (§7.2) — see `private/specs-verified/rtmp/rtmp-runtime/rtmp.md`.
7
8use crate::amf0::Amf0Value;
9
10/// Errors produced by the RTMP ingest session engine.
11#[non_exhaustive]
12#[derive(Debug, thiserror::Error, Clone, PartialEq)]
13pub enum RtmpError {
14    /// Not enough input bytes were available to parse a complete structure
15    /// (handshake message, chunk header, or reassembled RTMP message).
16    #[error(transparent)]
17    BufferTooShort(#[from] broadcast_core::error::BufferTooShort),
18
19    /// The input did not conform to the expected wire layout (bad handshake
20    /// version, invalid chunk `fmt`/CSID encoding, a length that overruns
21    /// its buffer, or similar).
22    #[error("malformed {what}")]
23    #[non_exhaustive]
24    Malformed {
25        /// What was being parsed when the malformed data was found.
26        what: &'static str,
27    },
28
29    /// A Set Chunk Size announced a value this engine cannot adopt: zero, a
30    /// non-zero reserved top bit (§5.4.1), or above the caller's `ChunkLimits::max_chunk_size`.
31    #[error("chunk size {value} is outside the range this engine accepts")]
32    #[non_exhaustive]
33    ChunkSizeOutOfRange {
34        /// The announced value, as it was on the wire.
35        value: u32,
36    },
37
38    /// A Set Peer Bandwidth carried a limit type byte outside `0..=2`
39    /// (§5.4.5).
40    #[error("set peer bandwidth limit type {value} is not 0..=2")]
41    #[non_exhaustive]
42    InvalidLimitType {
43        /// The offending byte.
44        value: u8,
45    },
46
47    /// A User Control message carried an event type §6.7 does not define.
48    #[error("unsupported user control event type {value}")]
49    #[non_exhaustive]
50    UnsupportedUserControlEvent {
51        /// The offending event type.
52        value: u16,
53    },
54
55    /// An AMF0 value used a marker this engine does not decode (`[AMF0]`
56    /// §2.1's reserved, legacy and AMF3-switch markers).
57    #[error("unsupported AMF0 marker {marker:#04x}")]
58    #[non_exhaustive]
59    UnsupportedAmf0Marker {
60        /// The offending marker byte.
61        marker: u8,
62    },
63
64    /// AMF0 data nested containers deeper than the caller's
65    /// [`Amf0Limits::max_depth`](crate::amf0::Amf0Limits::max_depth).
66    #[error("AMF0 nesting depth exceeds {max}")]
67    #[non_exhaustive]
68    Amf0DepthExceeded {
69        /// The limit that was reached.
70        max: usize,
71    },
72
73    /// AMF0 data held more values than the caller's
74    /// [`Amf0Limits::max_values`](crate::amf0::Amf0Limits::max_values) — a wide container
75    /// (millions of one-byte nulls, or three-byte empty-key pairs) would
76    /// otherwise expand into millions of `Amf0Value`s from one message.
77    #[error("AMF0 value count exceeds {max}")]
78    #[non_exhaustive]
79    Amf0TooManyValues {
80        /// The limit that was reached.
81        max: usize,
82    },
83
84    /// A Command Message body was longer than the caller's
85    /// [`ServerLimits::max_command_len`](crate::server::ServerLimits::max_command_len).
86    #[error("command message is {len} bytes, above the {max}-byte limit")]
87    #[non_exhaustive]
88    CommandTooLong {
89        /// The body length that was rejected.
90        len: usize,
91        /// The limit that was reached.
92        max: usize,
93    },
94
95    /// An AMF0 String carried more than the 65535 bytes its 16-bit length
96    /// prefix can hold; AMF0 has no Long String variant to fall back on
97    /// inside these messages.
98    #[error("{len} bytes do not fit the AMF0 string length prefix")]
99    #[non_exhaustive]
100    Amf0StringTooLong {
101        /// The string's length.
102        len: usize,
103    },
104
105    /// The first value of a Command Message body was not an AMF0 String
106    /// (§7.1.1: the command name).
107    #[error("command name is not an AMF0 string: {}", displayed(.value))]
108    #[non_exhaustive]
109    CommandNameNotString {
110        /// The value that stood where the name belongs.
111        value: Amf0Value,
112    },
113
114    /// The second value of a Command Message body was not an AMF0 Number
115    /// (§7.1.1: the transaction id).
116    #[error("command transaction id is not an AMF0 number: {}", displayed(.value))]
117    #[non_exhaustive]
118    CommandTransactionIdNotNumber {
119        /// The value that stood where the transaction id belongs.
120        value: Amf0Value,
121    },
122
123    /// The handshake version byte was not [`RTMP_VERSION`](crate::handshake::RTMP_VERSION)
124    /// (§5.2.2).
125    #[error("handshake version {value} is not {expected}")]
126    #[non_exhaustive]
127    HandshakeVersion {
128        /// The version byte received.
129        value: u8,
130        /// The version this engine speaks.
131        expected: u8,
132    },
133
134    /// An AMF0 value was not valid UTF-8 where the format requires text
135    /// (`[AMF0]` §2.4/§2.5/§2.14).
136    #[error("invalid AMF0 {what}")]
137    #[non_exhaustive]
138    Amf0Utf8 {
139        /// What held the invalid text.
140        what: &'static str,
141    },
142
143    /// Reassembly held more distinct chunk streams than the per-connection
144    /// bound allows.
145    #[error("more than {max} concurrent chunk streams")]
146    #[non_exhaustive]
147    TooManyChunkStreams {
148        /// The bound that was reached.
149        max: usize,
150    },
151
152    /// The in-progress reassembly buffer grew past the per-connection
153    /// budget (#1108/RTMP-W1).
154    #[error("buffered input exceeds {max} bytes")]
155    #[non_exhaustive]
156    InputBudgetExceeded {
157        /// The budget, in bytes.
158        max: usize,
159    },
160
161    /// Assembling a message hit a chunk-stream state that cannot complete:
162    /// a continuation before any chunk, a header on a completed message, a
163    /// stream without a message type id, or a chunk that does not match its
164    /// message's declared length.
165    #[error("inconsistent chunk stream {chunk_stream_id}: {what}")]
166    #[non_exhaustive]
167    ChunkStreamState {
168        /// The chunk stream id whose state did not hold.
169        chunk_stream_id: u32,
170        /// What was inconsistent.
171        what: &'static str,
172    },
173
174    /// An operation was attempted, or a message arrived, that is not valid
175    /// in the session's current state (e.g. a command received before the
176    /// handshake completed, or `publish` before `createStream`). The server
177    /// maps a rejected `publish` to `NetStream.Publish.BadName`; a client
178    /// sees a rejected stream key as this variant after `publish`.
179    #[error("unexpected state: {what}")]
180    #[non_exhaustive]
181    UnexpectedState {
182        /// Description of the state violation.
183        what: &'static str,
184    },
185
186    /// A [`crate::chunk::Message`] was built with a `chunk_stream_id` outside
187    /// the basic header's encodable range (2..=65599, RTMP §5.3.1.1), so it
188    /// cannot be written.
189    #[error("chunk stream id {chunk_stream_id} is outside the encodable range 2..=65599")]
190    #[non_exhaustive]
191    InvalidChunkStreamId {
192        /// The offending chunk stream id.
193        chunk_stream_id: u32,
194    },
195
196    /// A length or count did not fit the wire field it is written to
197    /// (#1129): a serializer that narrowed a `usize` with `as u32` (then
198    /// packed it into a 24-bit field) would have silently wrapped and
199    /// emitted a misframed chunk stream; this is returned instead.
200    #[error(transparent)]
201    FieldOverflow(#[from] broadcast_core::error::FieldOverflow),
202
203    /// The output buffer passed to `serialize_into` was smaller than
204    /// `serialized_len()`.
205    #[error(transparent)]
206    OutputBufferTooSmall(#[from] broadcast_core::error::OutputBufferTooSmall),
207
208    /// The caller's [`broadcast_core::damage::DamagePolicy`] refused a report
209    /// the session was about to publish: the session ends with the same
210    /// report inside this error.
211    #[error(transparent)]
212    Damage(#[from] broadcast_core::damage::DamageError),
213}
214
215impl RtmpError {
216    /// Builds [`Self::Malformed`].
217    #[must_use]
218    pub fn malformed(what: &'static str) -> Self {
219        Self::Malformed { what }
220    }
221}
222
223/// Render one AMF0 value for an error message; the value's `Debug` form is
224/// the only lossless rendering it has.
225fn displayed(value: &Amf0Value) -> alloc::string::String {
226    alloc::format!("{value:?}")
227}
228
229impl RtmpError {
230    /// The error for an announced chunk size this engine cannot adopt.
231    pub(crate) const fn chunk_size_out_of_range(value: u32) -> Self {
232        RtmpError::ChunkSizeOutOfRange { value }
233    }
234}