scuffle_rtmp/command_messages/netconnection/
mod.rs

1//! NetConnection command messages.
2
3use std::collections::HashMap;
4
5use scuffle_amf0::{Amf0Object, Amf0Value};
6use scuffle_bytes_util::StringCow;
7
8use super::on_status::{OnStatus, OnStatusCode};
9use crate::command_messages::CommandResultLevel;
10
11pub mod reader;
12pub mod writer;
13
14/// NetConnection command `connect`.
15///
16/// Defined by:
17/// - Legacy RTMP spec, 7.2.1.1
18/// - Enhanced RTMP spec, page 36-37, Enhancing NetConnection connect Command
19#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
20#[serde(bound = "'a: 'de")]
21pub struct NetConnectionCommandConnect<'a> {
22    /// Tells the server application name the client is connected to.
23    pub app: StringCow<'a>,
24    /// represents capability flags which can be combined via a
25    /// Bitwise OR to indicate which extended set of capabilities (i.e.,
26    /// beyond the legacy RTMP specification) are supported via E-RTMP.
27    /// See enum [`CapsExMask`] for the enumerated values representing the
28    /// assigned bits.
29    #[serde(default)]
30    pub caps_ex: Option<CapsExMask>,
31    /// All other parameters.
32    ///
33    /// Defined by:
34    /// - Legacy RTMP spec, page 30
35    /// - Enhanced RTMP spec, page 36-37
36    #[serde(flatten, borrow)]
37    pub others: HashMap<StringCow<'a>, Amf0Value<'a>>,
38}
39
40/// Extended capabilities mask used by the [enhanced connect command](NetConnectionCommandConnect).
41#[derive(serde::Deserialize)]
42#[serde(from = "u8", into = "u8")]
43#[bitmask_enum::bitmask(u8)]
44pub enum CapsExMask {
45    /// Support for reconnection
46    Reconnect = 0x01,
47    /// Support for multitrack
48    Multitrack = 0x02,
49    /// Can parse ModEx signal
50    ModEx = 0x04,
51    /// Support for nano offset
52    TimestampNanoOffset = 0x08,
53}
54
55/// NetConnection command `connect` result.
56///
57/// Defined by:
58/// - Legacy RTMP spec, 7.2.1.1
59#[derive(Debug, Clone, PartialEq)]
60pub struct NetConnectionCommandConnectResult<'a> {
61    /// Properties of the connection.
62    pub properties: NetConnectionCommandConnectResultProperties<'a>,
63    /// Information about the connection.
64    pub information: OnStatus<'a>,
65}
66
67/// NetConnection command `connect` result properties.
68///
69/// > Name-value pairs that describe the properties(fmsver etc.) of the connection.
70///
71/// Defined by:
72/// - Legacy RTMP spec, 7.2.1.1
73#[derive(Debug, Clone, PartialEq, serde::Serialize)]
74#[serde(rename_all = "camelCase")]
75pub struct NetConnectionCommandConnectResultProperties<'a> {
76    /// Flash Media Server version.
77    ///
78    /// Usually set to "FMS/3,0,1,123".
79    pub fms_ver: StringCow<'a>,
80    /// No idea what this means, but it is used by other media servers as well.
81    ///
82    /// Usually set to 31.0.
83    pub capabilities: f64,
84}
85
86impl Default for NetConnectionCommandConnectResultProperties<'static> {
87    fn default() -> Self {
88        Self {
89            fms_ver: "FMS/3,0,1,123".into(),
90            capabilities: 31.0,
91        }
92    }
93}
94
95impl Default for NetConnectionCommandConnectResult<'_> {
96    fn default() -> Self {
97        Self {
98            information: OnStatus {
99                level: CommandResultLevel::Status,
100                code: OnStatusCode::NET_CONNECTION_CONNECT_SUCCESS,
101                description: Some("Connection Succeeded.".into()),
102                others: Some([("objectEncoding".into(), Amf0Value::Number(0.0))].into_iter().collect()),
103            },
104            properties: Default::default(),
105        }
106    }
107}
108
109/// NetConnection commands as defined in 7.2.1.
110#[derive(Debug, Clone, PartialEq)]
111pub enum NetConnectionCommand<'a> {
112    /// Connect command.
113    Connect(NetConnectionCommandConnect<'a>),
114    /// Connect result.
115    ///
116    /// Sent from server to client in response to [`NetConnectionCommand::Connect`].
117    ConnectResult(NetConnectionCommandConnectResult<'a>),
118    /// Call command.
119    Call {
120        /// The command object.
121        command_object: Option<Amf0Object<'a>>,
122        /// Any optional arguments.
123        optional_arguments: Option<Amf0Object<'a>>,
124    },
125    /// Close command.
126    Close,
127    /// Create stream command.
128    CreateStream,
129    /// Create stream result.
130    ///
131    /// Sent from server to client in response to [`NetConnectionCommand::CreateStream`].
132    CreateStreamResult {
133        /// ID of the created stream.
134        stream_id: f64,
135    },
136}