@@ -58,6 +58,171 @@ class MessageTransportFactory {
5858 nabto::webrtc::SignalingChannelPtr channel);
5959};
6060
61+ /* *
62+ * Class representing a WebRTC Description received or to be sent on the
63+ * MessageTransport.
64+ */
65+ class SignalingDescription {
66+ public:
67+ /* *
68+ * Construct a SignalingDescription object to send
69+ *
70+ * @param descType type of the description, typically "offer" or "answer"
71+ * @param descSdp SDP representation of the description.
72+ */
73+ SignalingDescription (std::string descType, std::string descSdp);
74+
75+ /* *
76+ * Convert the description to JSON format following the network protocol.
77+ *
78+ * @return The JSON document
79+ */
80+ nlohmann::json toJson ();
81+
82+ /* *
83+ * The description type (typically "offer" or "answer")
84+ */
85+ std::string type;
86+
87+ /* *
88+ * SDP of the description.
89+ */
90+ std::string sdp;
91+ };
92+
93+ /* *
94+ * Class representing a WebRTC ICE Candidate received or to be sent on the
95+ * MessageTransport.
96+ */
97+ class SignalingCandidate {
98+ public:
99+ /* *
100+ * Construct a Candidate to be sent by the MessageTransport.
101+ *
102+ * @param cand The string representation of the candidate.
103+ */
104+ explicit SignalingCandidate (std::string cand);
105+
106+ /* *
107+ * Set the optional SDP MID value of a candidate.
108+ *
109+ * @param mid The MID to set.
110+ */
111+ void setSdpMid (const std::string& mid);
112+
113+ /* *
114+ * Set the optional SDP M Line Index of the candidate
115+ *
116+ * @param index The index to set.
117+ */
118+ void setSdpMLineIndex (int index);
119+
120+ /* *
121+ * Set the optional username fragment of the candidate
122+ *
123+ * @param ufrag The username fragment to set.
124+ */
125+ void setUsernameFragment (const std::string& ufrag);
126+
127+ /* *
128+ * Convert the description to JSON format following the network protocol.
129+ *
130+ * @return The JSON document
131+ */
132+ nlohmann::json toJson ();
133+
134+ /* *
135+ * The string representation of the candidate
136+ */
137+ std::string candidate;
138+
139+ /* *
140+ * Optional SDP MID of the candidate. If this is empty, it means the value
141+ * does not exist.
142+ */
143+ std::string sdpMid;
144+
145+ /* *
146+ * Optional SDP M Line Index of the candidate. If this is `<0` it means the
147+ * value does not exist.
148+ */
149+ int sdpMLineIndex{-1 };
150+
151+ /* *
152+ * Optional Username Fragment of the candidate. If this is empty, it means the
153+ * value does not exist.
154+ */
155+ std::string usernameFragment;
156+ };
157+
158+ /* *
159+ * Generalized WebRTC Signaling message to be sent/received by the
160+ * MessageTransport. This message can contain either a SignalingDescription or a
161+ * SignalingCandidate.
162+ */
163+ class WebrtcSignalingMessage {
164+ public:
165+ /* *
166+ * Construct a WebRTC Signaling message from the JSON format defined by the
167+ * protocol.
168+ *
169+ * If the JSON is invalid or not following the protocol, this will throw an
170+ * exception.
171+ *
172+ * @param jsonMessage The message to decode.
173+ * @return The created signaling message.
174+ */
175+ static WebrtcSignalingMessage fromJson (nlohmann::json& jsonMessage);
176+
177+ /* *
178+ * Construct a WebRTC Signaling message from a SignalingDescription.
179+ *
180+ * @param description The description to construct with.
181+ */
182+ explicit WebrtcSignalingMessage (const SignalingDescription& description);
183+
184+ /* *
185+ * Construct a WebRTC Signaling message from a SignalingCandidate.
186+ *
187+ * @param candidate The candidate to construct with.
188+ */
189+ explicit WebrtcSignalingMessage (const SignalingCandidate& candidate);
190+
191+ /* *
192+ * Check if the message is a SignalingDescription.
193+ *
194+ * @return True iff the message contains a description.
195+ */
196+ bool isDescription () const ;
197+
198+ /* *
199+ * Check if the message is a SignalingCandidate.
200+ *
201+ * @return True iff the message contains a candidate.
202+ */
203+ bool isCandidate () const ;
204+
205+ /* *
206+ * Get the SignalingDescription contained in this message if `isDescription()`
207+ * returns true. This must never be called if `isDescription()` returns false.
208+ *
209+ * @return The contained SignalingDescription object.
210+ */
211+ SignalingDescription getDescription () const ;
212+
213+ /* *
214+ * Get the SignalingCandidate contained in this message if `isCandidate()`
215+ * returns true. This must never be called if `isCandidate()` returns false.
216+ *
217+ * @return The contained SignalingCandidate object.
218+ */
219+ SignalingCandidate getCandidate () const ;
220+
221+ private:
222+ std::unique_ptr<SignalingDescription> description_ = nullptr ;
223+ std::unique_ptr<SignalingCandidate> candidate_ = nullptr ;
224+ };
225+
61226/* *
62227 * Handler invoked when the MessageTransport has completed the setup phase of a
63228 * channel.
@@ -68,6 +233,14 @@ class MessageTransportFactory {
68233using SetupDoneHandler = std::function<void (
69234 const std::vector<nabto::webrtc::IceServer>& iceServers)>;
70235
236+ /* *
237+ * Handler invoked when a new message is available from the MessageTransport.
238+ *
239+ * @param message The new message.
240+ */
241+ using MessageTransportMessageHandler =
242+ std::function<void (WebrtcSignalingMessage& message)>;
243+
71244using SetupDoneListenerId = uint32_t ;
72245using TransportMessageListenerId = uint32_t ;
73246using TransportErrorListenerId = uint32_t ;
@@ -108,7 +281,7 @@ class MessageTransport {
108281 * @return ID of the added handler to be used when removing it.
109282 */
110283 virtual TransportMessageListenerId addMessageListener (
111- nabto::webrtc::SignalingMessageHandler handler) = 0;
284+ MessageTransportMessageHandler handler) = 0;
112285
113286 /* *
114287 * Remove message listener.
@@ -144,7 +317,7 @@ class MessageTransport {
144317 *
145318 * @param message The message to send.
146319 */
147- virtual void sendMessage (const nlohmann::json & message) = 0;
320+ virtual void sendMessage (const WebrtcSignalingMessage & message) = 0;
148321};
149322
150323} // namespace util
0 commit comments