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_common::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    Malformed {
24        /// What was being parsed when the malformed data was found.
25        what: &'static str,
26    },
27
28    /// A Set Chunk Size announced a value this engine cannot adopt: zero, a
29    /// non-zero reserved top bit (§5.4.1), or above `MAX_CHUNK_SIZE`.
30    #[error("chunk size {value} is outside the range this engine accepts")]
31    ChunkSizeOutOfRange {
32        /// The announced value, as it was on the wire.
33        value: u32,
34    },
35
36    /// A Set Peer Bandwidth carried a limit type byte outside `0..=2`
37    /// (§5.4.5).
38    #[error("set peer bandwidth limit type {value} is not 0..=2")]
39    InvalidLimitType {
40        /// The offending byte.
41        value: u8,
42    },
43
44    /// A User Control message carried an event type §6.7 does not define.
45    #[error("unsupported user control event type {value}")]
46    UnsupportedUserControlEvent {
47        /// The offending event type.
48        value: u16,
49    },
50
51    /// An AMF0 value used a marker this engine does not decode (`[AMF0]`
52    /// §2.1's reserved, legacy and AMF3-switch markers).
53    #[error("unsupported AMF0 marker {marker:#04x}")]
54    UnsupportedAmf0Marker {
55        /// The offending marker byte.
56        marker: u8,
57    },
58
59    /// AMF0 data nested containers deeper than
60    /// [`MAX_AMF0_DEPTH`](crate::amf0::MAX_AMF0_DEPTH).
61    #[error("AMF0 nesting depth exceeds {max}")]
62    Amf0DepthExceeded {
63        /// The limit that was reached.
64        max: usize,
65    },
66
67    /// An AMF0 String carried more than the 65535 bytes its 16-bit length
68    /// prefix can hold; AMF0 has no Long String variant to fall back on
69    /// inside these messages.
70    #[error("{len} bytes do not fit the AMF0 string length prefix")]
71    Amf0StringTooLong {
72        /// The string's length.
73        len: usize,
74    },
75
76    /// The first value of a Command Message body was not an AMF0 String
77    /// (§7.1.1: the command name).
78    #[error("command name is not an AMF0 string: {}", displayed(.value))]
79    CommandNameNotString {
80        /// The value that stood where the name belongs.
81        value: Amf0Value,
82    },
83
84    /// The second value of a Command Message body was not an AMF0 Number
85    /// (§7.1.1: the transaction id).
86    #[error("command transaction id is not an AMF0 number: {}", displayed(.value))]
87    CommandTransactionIdNotNumber {
88        /// The value that stood where the transaction id belongs.
89        value: Amf0Value,
90    },
91
92    /// The handshake version byte was not [`RTMP_VERSION`](crate::handshake::RTMP_VERSION)
93    /// (§5.2.2).
94    #[error("handshake version {value} is not {expected}")]
95    HandshakeVersion {
96        /// The version byte received.
97        value: u8,
98        /// The version this engine speaks.
99        expected: u8,
100    },
101
102    /// An AMF0 Date carried a non-zero reserved time-zone field (`[AMF0]`
103    /// §2.13: it MUST be `0x0000`).
104    #[error("AMF0 date reserved time zone is {value}, must be 0")]
105    Amf0DateTimeZone {
106        /// The offending field.
107        value: u16,
108    },
109
110    /// An AMF0 value was not valid UTF-8 where the format requires text
111    /// (`[AMF0]` §2.4/§2.5/§2.14).
112    #[error("invalid AMF0 {what}")]
113    Amf0Utf8 {
114        /// What held the invalid text.
115        what: &'static str,
116    },
117
118    /// Reassembly held more distinct chunk streams than the per-connection
119    /// bound allows.
120    #[error("more than {max} concurrent chunk streams")]
121    TooManyChunkStreams {
122        /// The bound that was reached.
123        max: usize,
124    },
125
126    /// The in-progress reassembly buffer grew past the per-connection
127    /// budget (#1108/RTMP-W1).
128    #[error("buffered input exceeds {max} bytes")]
129    InputBudgetExceeded {
130        /// The budget, in bytes.
131        max: usize,
132    },
133
134    /// Assembling a message hit a chunk-stream state that cannot complete:
135    /// a continuation before any chunk, a header on a completed message, a
136    /// stream without a message type id, or a chunk that does not match its
137    /// message's declared length.
138    #[error("inconsistent chunk stream {chunk_stream_id}: {what}")]
139    ChunkStreamState {
140        /// The chunk stream id whose state did not hold.
141        chunk_stream_id: u32,
142        /// What was inconsistent.
143        what: &'static str,
144    },
145
146    /// An operation was attempted, or a message arrived, that is not valid
147    /// in the session's current state (e.g. a command received before the
148    /// handshake completed, or `publish` before `createStream`). The server
149    /// maps a rejected `publish` to `NetStream.Publish.BadName`; a client
150    /// sees a rejected stream key as this variant after `publish`.
151    #[error("unexpected state: {what}")]
152    UnexpectedState {
153        /// Description of the state violation.
154        what: &'static str,
155    },
156
157    /// A [`crate::chunk::Message`] was built with a `chunk_stream_id` outside
158    /// the basic header's encodable range (2..=65599, RTMP §5.3.1.1), so it
159    /// cannot be written.
160    #[error("chunk stream id {chunk_stream_id} is outside the encodable range 2..=65599")]
161    InvalidChunkStreamId {
162        /// The offending chunk stream id.
163        chunk_stream_id: u32,
164    },
165
166    /// A length or count did not fit the wire field it is written to
167    /// (#1129): a serializer that narrowed a `usize` with `as u32` (then
168    /// packed it into a 24-bit field) would have silently wrapped and
169    /// emitted a misframed chunk stream; this is returned instead.
170    #[error(transparent)]
171    FieldOverflow(#[from] broadcast_common::error::FieldOverflow),
172
173    /// The output buffer passed to `serialize_into` was smaller than
174    /// `serialized_len()`.
175    #[error(transparent)]
176    OutputBufferTooSmall(#[from] broadcast_common::error::OutputBufferTooSmall),
177}
178
179/// Render one AMF0 value for an error message; the value's `Debug` form is
180/// the only lossless rendering it has.
181fn displayed(value: &Amf0Value) -> alloc::string::String {
182    alloc::format!("{value:?}")
183}
184
185impl RtmpError {
186    /// The error for an announced chunk size this engine cannot adopt.
187    pub(crate) const fn chunk_size_out_of_range(value: u32) -> Self {
188        RtmpError::ChunkSizeOutOfRange { value }
189    }
190}