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