Skip to main content

kittycad_modeling_cmds/
websocket.rs

1//! Types for the websocket server.
2
3use std::{borrow::Cow, collections::HashMap};
4
5use parse_display_derive::{Display, FromStr};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8#[cfg(feature = "slog")]
9use slog::{Record, Serializer, KV};
10use uuid::Uuid;
11
12use crate::{
13    id::ModelingCmdId,
14    ok_response::OkModelingCmdResponse,
15    shared::{EngineErrorCode, ExportFile},
16    ModelingCmd,
17};
18
19/// The type of error sent by the KittyCAD API.
20#[derive(Display, FromStr, Copy, Eq, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone, Ord, PartialOrd)]
21#[serde(rename_all = "snake_case")]
22#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
23pub enum ErrorCode {
24    /// Graphics engine failed to complete request, consider retrying
25    InternalEngine,
26    /// API failed to complete request, consider retrying
27    InternalApi,
28    /// User requested something geometrically or graphically impossible.
29    /// Don't retry this request, as it's inherently impossible. Instead, read the error message
30    /// and change your request.
31    BadRequest,
32    /// Auth token is missing from the request
33    AuthTokenMissing,
34    /// Auth token is invalid in some way (expired, incorrect format, etc)
35    AuthTokenInvalid,
36    /// Client sent invalid JSON.
37    InvalidJson,
38    /// Client sent invalid BSON.
39    InvalidBson,
40    /// Client sent a message which is not accepted over this protocol.
41    WrongProtocol,
42    /// Problem sending data between client and KittyCAD API.
43    ConnectionProblem,
44    /// Client sent a Websocket message type which the KittyCAD API does not handle.
45    MessageTypeNotAccepted,
46    /// Client sent a Websocket message intended for WebRTC but it was configured as a WebRTC
47    /// connection.
48    MessageTypeNotAcceptedForWebRTC,
49}
50
51/// Because [`EngineErrorCode`] is a subset of [`ErrorCode`], you can trivially map
52/// each variant of the former to a variant of the latter.
53impl From<EngineErrorCode> for ErrorCode {
54    fn from(value: EngineErrorCode) -> Self {
55        match value {
56            EngineErrorCode::InternalEngine => Self::InternalEngine,
57            EngineErrorCode::BadRequest => Self::BadRequest,
58        }
59    }
60}
61
62/// A graphics command submitted to the KittyCAD engine via the Modeling API.
63#[derive(Debug, Clone, Deserialize, Serialize)]
64#[cfg_attr(feature = "derive-jsonschema-on-enums", derive(schemars::JsonSchema))]
65#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
66pub struct ModelingCmdReq {
67    /// Which command to submit to the Kittycad engine.
68    pub cmd: ModelingCmd,
69    /// ID of command being submitted.
70    pub cmd_id: ModelingCmdId,
71}
72
73/// The websocket messages the server receives.
74#[allow(clippy::large_enum_variant)]
75#[derive(Serialize, Deserialize, Debug, Clone)]
76#[cfg_attr(feature = "derive-jsonschema-on-enums", derive(schemars::JsonSchema))]
77#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
78#[serde(tag = "type", rename_all = "snake_case")]
79#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
80pub enum WebSocketRequest {
81    /// The trickle ICE candidate request.
82    // We box these to avoid a huge size difference between variants.
83    TrickleIce {
84        /// Information about the ICE candidate.
85        candidate: Box<RtcIceCandidateInit>,
86    },
87    /// The SDP offer request.
88    SdpOffer {
89        /// The session description.
90        offer: Box<RtcSessionDescription>,
91    },
92    /// The modeling command request.
93    ModelingCmdReq(ModelingCmdReq),
94    /// A sequence of modeling requests. If any request fails, following requests will not be tried.
95    ModelingCmdBatchReq(ModelingBatch),
96    /// The client-to-server Ping to ensure the WebSocket stays alive.
97    Ping {},
98
99    /// The response to a metrics collection request from the server.
100    MetricsResponse {
101        /// Collected metrics from the Client's end of the engine connection.
102        metrics: Box<ClientMetrics>,
103    },
104
105    /// Return information about the connected instance
106    Debug {},
107
108    /// Authentication header request.
109    Headers {
110        /// The authentication header.
111        headers: HashMap<String, String>,
112    },
113}
114
115/// A sequence of modeling requests. If any request fails, following requests will not be tried.
116#[derive(Serialize, Deserialize, Debug, Clone)]
117#[cfg_attr(feature = "derive-jsonschema-on-enums", derive(schemars::JsonSchema))]
118#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
119#[serde(rename_all = "snake_case")]
120pub struct ModelingBatch {
121    /// A sequence of modeling requests. If any request fails, following requests will not be tried.
122    pub requests: Vec<ModelingCmdReq>,
123    /// ID of batch being submitted.
124    /// Each request has their own individual ModelingCmdId, but this is the
125    /// ID of the overall batch.
126    pub batch_id: ModelingCmdId,
127    /// If false or omitted, responses to each batch command will just be Ok(()).
128    /// If true, responses will be the actual response data for that modeling command.
129    #[serde(default)]
130    pub responses: bool,
131}
132
133impl std::default::Default for ModelingBatch {
134    /// Creates a batch with 0 requests and a random ID.
135    fn default() -> Self {
136        Self {
137            requests: Default::default(),
138            batch_id: Uuid::new_v4().into(),
139            responses: false,
140        }
141    }
142}
143
144impl ModelingBatch {
145    /// Add a new modeling command to the end of this batch.
146    pub fn push(&mut self, req: ModelingCmdReq) {
147        self.requests.push(req);
148    }
149
150    /// Are there any requests in the batch?
151    pub fn is_empty(&self) -> bool {
152        self.requests.is_empty()
153    }
154}
155
156/// Representation of an ICE server used for STUN/TURN
157/// Used to initiate WebRTC connections
158/// based on <https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServer>
159#[derive(serde::Serialize, serde::Deserialize, Debug, JsonSchema, Clone)]
160pub struct IceServer {
161    /// URLs for a given STUN/TURN server.
162    /// IceServer urls can either be a string or an array of strings
163    /// But, we choose to always convert to an array of strings for consistency
164    pub urls: Vec<String>,
165    /// Credentials for a given TURN server.
166    pub credential: Option<String>,
167    /// Username for a given TURN server.
168    pub username: Option<String>,
169}
170
171/// The websocket messages this server sends.
172#[derive(Serialize, Deserialize, Debug, Clone)]
173#[cfg_attr(feature = "derive-jsonschema-on-enums", derive(schemars::JsonSchema))]
174#[serde(tag = "type", content = "data", rename_all = "snake_case")]
175#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
176pub enum OkWebSocketResponseData {
177    /// Information about the ICE servers.
178    IceServerInfo {
179        /// Information about the ICE servers.
180        ice_servers: Vec<IceServer>,
181    },
182    /// The trickle ICE candidate response.
183    // We box these to avoid a huge size difference between variants.
184    TrickleIce {
185        /// Information about the ICE candidate.
186        candidate: Box<RtcIceCandidateInit>,
187    },
188    /// The SDP answer response.
189    SdpAnswer {
190        /// The session description.
191        answer: Box<RtcSessionDescription>,
192    },
193    /// The modeling command response.
194    Modeling {
195        /// The result of the command.
196        modeling_response: OkModelingCmdResponse,
197    },
198    /// Response to a ModelingBatch.
199    ModelingBatch {
200        /// For each request in the batch,
201        /// maps its ID to the request's outcome.
202        responses: HashMap<ModelingCmdId, BatchResponse>,
203    },
204    /// The exported files.
205    Export {
206        /// The exported files
207        files: Vec<RawFile>,
208    },
209
210    /// Request a collection of metrics, to include WebRTC.
211    MetricsRequest {},
212
213    /// Data about the Modeling Session (application-level).
214    ModelingSessionData {
215        /// Data about the Modeling Session (application-level).
216        session: ModelingSessionData,
217    },
218
219    /// Pong response to a Ping message.
220    Pong {},
221
222    /// Information about the connected instance
223    Debug {
224        /// Instance name. This may or may not mean something.
225        name: String,
226    },
227}
228
229/// Successful Websocket response.
230#[derive(Debug, Serialize, Deserialize, Clone)]
231#[cfg_attr(feature = "derive-jsonschema-on-enums", derive(schemars::JsonSchema))]
232#[serde(rename_all = "snake_case")]
233pub struct SuccessWebSocketResponse {
234    /// Always true
235    pub success: bool,
236    /// Which request this is a response to.
237    /// If the request was a modeling command, this is the modeling command ID.
238    /// If no request ID was sent, this will be null.
239    pub request_id: Option<Uuid>,
240    /// The data sent with a successful response.
241    /// This will be flattened into a 'type' and 'data' field.
242    pub resp: OkWebSocketResponseData,
243}
244
245/// Unsuccessful Websocket response.
246#[derive(JsonSchema, Debug, Serialize, Deserialize, Clone)]
247#[serde(rename_all = "snake_case")]
248pub struct FailureWebSocketResponse {
249    /// Always false
250    pub success: bool,
251    /// Which request this is a response to.
252    /// If the request was a modeling command, this is the modeling command ID.
253    /// If no request ID was sent, this will be null.
254    pub request_id: Option<Uuid>,
255    /// The errors that occurred.
256    pub errors: Vec<ApiError>,
257}
258
259/// Websocket responses can either be successful or unsuccessful.
260/// Slightly different schemas in either case.
261#[derive(Debug, Serialize, Deserialize, Clone)]
262#[cfg_attr(feature = "derive-jsonschema-on-enums", derive(schemars::JsonSchema))]
263#[serde(rename_all = "snake_case", untagged)]
264pub enum WebSocketResponse {
265    /// Response sent when a request succeeded.
266    Success(SuccessWebSocketResponse),
267    /// Response sent when a request did not succeed.
268    Failure(FailureWebSocketResponse),
269}
270
271/// Websocket responses can either be successful or unsuccessful.
272/// Slightly different schemas in either case.
273#[derive(Debug, Serialize, Deserialize, Clone)]
274#[cfg_attr(feature = "derive-jsonschema-on-enums", derive(schemars::JsonSchema))]
275#[serde(rename_all = "snake_case", untagged)]
276pub enum BatchResponse {
277    /// Response sent when a request succeeded.
278    Success {
279        /// Response to the modeling command.
280        response: OkModelingCmdResponse,
281    },
282    /// Response sent when a request did not succeed.
283    Failure {
284        /// Errors that occurred during the modeling command.
285        errors: Vec<ApiError>,
286    },
287}
288
289impl WebSocketResponse {
290    /// Make a new success response.
291    pub fn success(request_id: Option<Uuid>, resp: OkWebSocketResponseData) -> Self {
292        Self::Success(SuccessWebSocketResponse {
293            success: true,
294            request_id,
295            resp,
296        })
297    }
298
299    /// Make a new failure response.
300    pub fn failure(request_id: Option<Uuid>, errors: Vec<ApiError>) -> Self {
301        Self::Failure(FailureWebSocketResponse {
302            success: false,
303            request_id,
304            errors,
305        })
306    }
307
308    /// Did the request succeed?
309    pub fn is_success(&self) -> bool {
310        matches!(self, Self::Success(_))
311    }
312
313    /// Did the request fail?
314    pub fn is_failure(&self) -> bool {
315        matches!(self, Self::Failure(_))
316    }
317
318    /// Get the ID of whichever request this response is for.
319    pub fn request_id(&self) -> Option<Uuid> {
320        match self {
321            WebSocketResponse::Success(x) => x.request_id,
322            WebSocketResponse::Failure(x) => x.request_id,
323        }
324    }
325}
326
327/// A raw file with unencoded contents to be passed over binary websockets.
328/// When raw files come back for exports it is sent as binary/bson, not text/json.
329#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone)]
330#[cfg_attr(feature = "python", pyo3::pyclass, pyo3_stub_gen::derive::gen_stub_pyclass)]
331pub struct RawFile {
332    /// The name of the file.
333    pub name: String,
334    /// The contents of the file.
335    #[serde(
336        serialize_with = "serde_bytes::serialize",
337        deserialize_with = "serde_bytes::deserialize"
338    )]
339    pub contents: Vec<u8>,
340}
341
342#[cfg(feature = "python")]
343#[pyo3_stub_gen::derive::gen_stub_pymethods]
344#[pyo3::pymethods]
345impl RawFile {
346    #[getter]
347    fn contents(&self) -> Vec<u8> {
348        self.contents.clone()
349    }
350
351    #[getter]
352    fn name(&self) -> String {
353        self.name.clone()
354    }
355}
356
357impl From<ExportFile> for RawFile {
358    fn from(f: ExportFile) -> Self {
359        Self {
360            name: f.name,
361            contents: f.contents.0,
362        }
363    }
364}
365
366/// An error with an internal message for logging.
367#[derive(Debug, Serialize, Deserialize, JsonSchema)]
368pub struct LoggableApiError {
369    /// The error shown to users
370    pub error: ApiError,
371    /// The string logged internally
372    pub msg_internal: Option<Cow<'static, str>>,
373}
374
375#[cfg(feature = "slog")]
376impl KV for LoggableApiError {
377    fn serialize(&self, _rec: &Record, serializer: &mut dyn Serializer) -> slog::Result {
378        use slog::Key;
379        if let Some(ref msg_internal) = self.msg_internal {
380            serializer.emit_str(Key::from("msg_internal"), msg_internal)?;
381        }
382        serializer.emit_str(Key::from("msg_external"), &self.error.message)?;
383        serializer.emit_str(Key::from("error_code"), &self.error.error_code.to_string())
384    }
385}
386
387/// An error.
388#[derive(Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, Clone)]
389pub struct ApiError {
390    /// The error code.
391    pub error_code: ErrorCode,
392    /// The error message.
393    pub message: String,
394}
395
396impl ApiError {
397    /// Convert to a `LoggableApiError` with no internal message.
398    pub fn no_internal_message(self) -> LoggableApiError {
399        LoggableApiError {
400            error: self,
401            msg_internal: None,
402        }
403    }
404    /// Add an internal log message to this error.
405    pub fn with_message(self, msg_internal: Cow<'static, str>) -> LoggableApiError {
406        LoggableApiError {
407            error: self,
408            msg_internal: Some(msg_internal),
409        }
410    }
411
412    /// Should the internal error message be logged?
413    pub fn should_log_internal_message(&self) -> bool {
414        use ErrorCode as Code;
415        match self.error_code {
416            // Internal errors should always be logged, as they're problems with KittyCAD programming
417            Code::InternalEngine | Code::InternalApi => true,
418            // The user did something wrong, no need to log it, as there's nothing KittyCAD programmers can fix
419            Code::MessageTypeNotAcceptedForWebRTC
420            | Code::MessageTypeNotAccepted
421            | Code::BadRequest
422            | Code::WrongProtocol
423            | Code::AuthTokenMissing
424            | Code::AuthTokenInvalid
425            | Code::InvalidBson
426            | Code::InvalidJson => false,
427            // In debug builds, log connection problems, otherwise don't.
428            Code::ConnectionProblem => cfg!(debug_assertions),
429        }
430    }
431}
432
433/// Serde serializes Result into JSON as "Ok" and "Err", but we want "ok" and "err".
434/// So, create a new enum that serializes as lowercase.
435#[derive(Debug, Serialize, Deserialize, JsonSchema)]
436#[serde(rename_all = "snake_case", rename = "SnakeCaseResult")]
437pub enum SnakeCaseResult<T, E> {
438    /// The result is Ok.
439    Ok(T),
440    /// The result is Err.
441    Err(E),
442}
443
444impl<T, E> From<SnakeCaseResult<T, E>> for Result<T, E> {
445    fn from(value: SnakeCaseResult<T, E>) -> Self {
446        match value {
447            SnakeCaseResult::Ok(x) => Self::Ok(x),
448            SnakeCaseResult::Err(x) => Self::Err(x),
449        }
450    }
451}
452
453impl<T, E> From<Result<T, E>> for SnakeCaseResult<T, E> {
454    fn from(value: Result<T, E>) -> Self {
455        match value {
456            Ok(x) => Self::Ok(x),
457            Err(x) => Self::Err(x),
458        }
459    }
460}
461
462/// ClientMetrics contains information regarding the state of the peer.
463#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
464#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
465pub struct ClientMetrics {
466    /// Counter of the number of WebRTC frames the client has dropped from the
467    /// inbound video stream.
468    ///
469    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-framesdropped
470    pub rtc_frames_dropped: Option<u32>,
471
472    /// Counter of the number of WebRTC frames that the client has decoded
473    /// from the inbound video stream.
474    ///
475    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-freezecount
476    pub rtc_frames_decoded: Option<u64>,
477
478    /// Counter of the number of WebRTC frames that the client has received
479    /// from the inbound video stream.
480    ///
481    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-freezecount
482    pub rtc_frames_received: Option<u64>,
483
484    /// Current number of frames being rendered in the last second. A good target
485    /// is 60 frames per second, but it can fluctuate depending on network
486    /// conditions.
487    ///
488    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-freezecount
489    pub rtc_frames_per_second: Option<u8>, // no way we're more than 255 fps :)
490
491    /// Number of times the inbound video playback has frozen. This is usually due to
492    /// network conditions.
493    ///
494    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-freezecount
495    pub rtc_freeze_count: Option<u32>,
496
497    /// Amount of "jitter" in the inbound video stream. Network latency is the time
498    /// it takes a packet to traverse the network. The amount that the latency
499    /// varies is the jitter. Video latency is the time it takes to render
500    /// a frame sent by the server (including network latency). A low jitter
501    /// means the video latency can be reduced without impacting smooth
502    /// playback. High jitter means clients will increase video latency to
503    /// ensure smooth playback.
504    ///
505    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcreceivedrtpstreamstats-jitter
506    pub rtc_jitter_sec: Option<f64>,
507
508    /// Number of "key frames" decoded in the inbound h.264 stream. A
509    /// key frame is an expensive (bandwidth-wise) "full image" of the video
510    /// frame. Data after the keyframe become -- effectively -- "diff"
511    /// operations on that key frame. The Engine will only send a keyframe if
512    /// required, which is an indication that some of the "diffs" have been
513    /// lost, usually an indication of poor network conditions. We like this
514    /// metric to understand times when the connection has had to recover.
515    ///
516    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-keyframesdecoded
517    pub rtc_keyframes_decoded: Option<u32>,
518
519    /// Number of seconds of frozen video the user has been subjected to.
520    ///
521    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalfreezesduration
522    pub rtc_total_freezes_duration_sec: Option<f32>,
523
524    /// The height of the inbound video stream in pixels.
525    ///
526    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-frameheight
527    pub rtc_frame_height: Option<u32>,
528
529    /// The width of the inbound video stream in pixels.
530    ///
531    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-framewidth
532    pub rtc_frame_width: Option<u32>,
533
534    /// Amount of packets lost in the inbound video stream.
535    ///
536    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcreceivedrtpstreamstats-packetslost
537    pub rtc_packets_lost: Option<u32>,
538
539    ///  Count the total number of Picture Loss Indication (PLI) packets.
540    ///
541    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-plicount
542    pub rtc_pli_count: Option<u32>,
543
544    /// Count of the total number of video pauses experienced by this receiver.
545    ///
546    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-pausecount
547    pub rtc_pause_count: Option<u32>,
548
549    /// Count of the total number of video pauses experienced by this receiver.
550    ///
551    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalpausesduration
552    pub rtc_total_pauses_duration_sec: Option<f32>,
553
554    /// Total duration of pauses in seconds.
555    ///
556    /// This is the "ping" between the client and the STUN server. Not to be confused with the
557    /// E2E RTT documented
558    /// [here](https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteinboundrtpstreamstats-roundtriptime)
559    ///
560    /// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
561    pub rtc_stun_rtt_sec: Option<f32>,
562}
563
564/// ICECandidate represents a ice candidate
565#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
566pub struct RtcIceCandidate {
567    /// The stats ID.
568    pub stats_id: String,
569    /// The foundation for the address.
570    pub foundation: String,
571    /// The priority of the candidate.
572    pub priority: u32,
573    /// The address of the candidate.
574    pub address: String,
575    /// The protocol used for the candidate.
576    pub protocol: RtcIceProtocol,
577    /// The port used for the candidate.
578    pub port: u16,
579    /// The type of the candidate.
580    pub typ: RtcIceCandidateType,
581    /// The component of the candidate.
582    pub component: u16,
583    /// The related address of the candidate.
584    pub related_address: String,
585    /// The related port of the candidate.
586    pub related_port: u16,
587    /// The TCP type of the candidate.
588    pub tcp_type: String,
589}
590
591#[cfg(feature = "webrtc")]
592impl From<webrtc::ice_transport::ice_candidate::RTCIceCandidate> for RtcIceCandidate {
593    fn from(candidate: webrtc::ice_transport::ice_candidate::RTCIceCandidate) -> Self {
594        Self {
595            stats_id: candidate.stats_id,
596            foundation: candidate.foundation,
597            priority: candidate.priority,
598            address: candidate.address,
599            protocol: candidate.protocol.into(),
600            port: candidate.port,
601            typ: candidate.typ.into(),
602            component: candidate.component,
603            related_address: candidate.related_address,
604            related_port: candidate.related_port,
605            tcp_type: candidate.tcp_type,
606        }
607    }
608}
609
610#[cfg(feature = "webrtc")]
611impl From<RtcIceCandidate> for webrtc::ice_transport::ice_candidate::RTCIceCandidate {
612    fn from(candidate: RtcIceCandidate) -> Self {
613        Self {
614            stats_id: candidate.stats_id,
615            foundation: candidate.foundation,
616            priority: candidate.priority,
617            address: candidate.address,
618            protocol: candidate.protocol.into(),
619            port: candidate.port,
620            typ: candidate.typ.into(),
621            component: candidate.component,
622            related_address: candidate.related_address,
623            related_port: candidate.related_port,
624            tcp_type: candidate.tcp_type,
625        }
626    }
627}
628
629/// ICECandidateType represents the type of the ICE candidate used.
630#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
631#[serde(rename_all = "snake_case")]
632pub enum RtcIceCandidateType {
633    /// Unspecified indicates that the candidate type is unspecified.
634    #[default]
635    Unspecified,
636
637    /// ICECandidateTypeHost indicates that the candidate is of Host type as
638    /// described in <https://tools.ietf.org/html/rfc8445#section-5.1.1.1>. A
639    /// candidate obtained by binding to a specific port from an IP address on
640    /// the host. This includes IP addresses on physical interfaces and logical
641    /// ones, such as ones obtained through VPNs.
642    Host,
643
644    /// ICECandidateTypeSrflx indicates the the candidate is of Server
645    /// Reflexive type as described
646    /// <https://tools.ietf.org/html/rfc8445#section-5.1.1.2>. A candidate type
647    /// whose IP address and port are a binding allocated by a NAT for an ICE
648    /// agent after it sends a packet through the NAT to a server, such as a
649    /// STUN server.
650    Srflx,
651
652    /// ICECandidateTypePrflx indicates that the candidate is of Peer
653    /// Reflexive type. A candidate type whose IP address and port are a binding
654    /// allocated by a NAT for an ICE agent after it sends a packet through the
655    /// NAT to its peer.
656    Prflx,
657
658    /// ICECandidateTypeRelay indicates the the candidate is of Relay type as
659    /// described in <https://tools.ietf.org/html/rfc8445#section-5.1.1.2>. A
660    /// candidate type obtained from a relay server, such as a TURN server.
661    Relay,
662}
663
664#[cfg(feature = "webrtc")]
665impl From<webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType> for RtcIceCandidateType {
666    fn from(candidate_type: webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType) -> Self {
667        match candidate_type {
668            webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Host => RtcIceCandidateType::Host,
669            webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Srflx => RtcIceCandidateType::Srflx,
670            webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Prflx => RtcIceCandidateType::Prflx,
671            webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Relay => RtcIceCandidateType::Relay,
672            webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Unspecified => {
673                RtcIceCandidateType::Unspecified
674            }
675        }
676    }
677}
678
679#[cfg(feature = "webrtc")]
680impl From<RtcIceCandidateType> for webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType {
681    fn from(candidate_type: RtcIceCandidateType) -> Self {
682        match candidate_type {
683            RtcIceCandidateType::Host => webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Host,
684            RtcIceCandidateType::Srflx => webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Srflx,
685            RtcIceCandidateType::Prflx => webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Prflx,
686            RtcIceCandidateType::Relay => webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Relay,
687            RtcIceCandidateType::Unspecified => {
688                webrtc::ice_transport::ice_candidate_type::RTCIceCandidateType::Unspecified
689            }
690        }
691    }
692}
693
694/// ICEProtocol indicates the transport protocol type that is used in the
695/// ice.URL structure.
696#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
697#[serde(rename_all = "snake_case")]
698pub enum RtcIceProtocol {
699    /// Unspecified indicates that the protocol is unspecified.
700    #[default]
701    Unspecified,
702
703    /// UDP indicates the URL uses a UDP transport.
704    Udp,
705
706    /// TCP indicates the URL uses a TCP transport.
707    Tcp,
708}
709
710#[cfg(feature = "webrtc")]
711impl From<webrtc::ice_transport::ice_protocol::RTCIceProtocol> for RtcIceProtocol {
712    fn from(protocol: webrtc::ice_transport::ice_protocol::RTCIceProtocol) -> Self {
713        match protocol {
714            webrtc::ice_transport::ice_protocol::RTCIceProtocol::Udp => RtcIceProtocol::Udp,
715            webrtc::ice_transport::ice_protocol::RTCIceProtocol::Tcp => RtcIceProtocol::Tcp,
716            webrtc::ice_transport::ice_protocol::RTCIceProtocol::Unspecified => RtcIceProtocol::Unspecified,
717        }
718    }
719}
720
721#[cfg(feature = "webrtc")]
722impl From<RtcIceProtocol> for webrtc::ice_transport::ice_protocol::RTCIceProtocol {
723    fn from(protocol: RtcIceProtocol) -> Self {
724        match protocol {
725            RtcIceProtocol::Udp => webrtc::ice_transport::ice_protocol::RTCIceProtocol::Udp,
726            RtcIceProtocol::Tcp => webrtc::ice_transport::ice_protocol::RTCIceProtocol::Tcp,
727            RtcIceProtocol::Unspecified => webrtc::ice_transport::ice_protocol::RTCIceProtocol::Unspecified,
728        }
729    }
730}
731
732/// ICECandidateInit is used to serialize ice candidates
733#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
734#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
735#[serde(rename_all = "camelCase")]
736// These HAVE to be camel case as per the RFC.
737pub struct RtcIceCandidateInit {
738    /// The candidate string associated with the object.
739    pub candidate: String,
740    /// The identifier of the "media stream identification" as defined in
741    /// [RFC 8841](https://tools.ietf.org/html/rfc8841).
742    pub sdp_mid: Option<String>,
743    /// The index (starting at zero) of the m-line in the SDP this candidate is
744    /// associated with.
745    #[serde(rename = "sdpMLineIndex")]
746    pub sdp_mline_index: Option<u16>,
747    /// The username fragment (as defined in
748    /// [RFC 8445](https://tools.ietf.org/html/rfc8445#section-5.2.1)) associated with the object.
749    pub username_fragment: Option<String>,
750}
751
752#[cfg(feature = "webrtc")]
753impl From<webrtc::ice_transport::ice_candidate::RTCIceCandidateInit> for RtcIceCandidateInit {
754    fn from(candidate: webrtc::ice_transport::ice_candidate::RTCIceCandidateInit) -> Self {
755        Self {
756            candidate: candidate.candidate,
757            sdp_mid: candidate.sdp_mid,
758            sdp_mline_index: candidate.sdp_mline_index,
759            username_fragment: candidate.username_fragment,
760        }
761    }
762}
763
764#[cfg(feature = "webrtc")]
765impl From<RtcIceCandidateInit> for webrtc::ice_transport::ice_candidate::RTCIceCandidateInit {
766    fn from(candidate: RtcIceCandidateInit) -> Self {
767        Self {
768            candidate: candidate.candidate,
769            sdp_mid: candidate.sdp_mid,
770            sdp_mline_index: candidate.sdp_mline_index,
771            username_fragment: candidate.username_fragment,
772        }
773    }
774}
775
776/// SessionDescription is used to expose local and remote session descriptions.
777#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema)]
778#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
779pub struct RtcSessionDescription {
780    /// SDP type.
781    #[serde(rename = "type")]
782    pub sdp_type: RtcSdpType,
783
784    /// SDP string.
785    pub sdp: String,
786}
787
788#[cfg(feature = "webrtc")]
789impl From<webrtc::peer_connection::sdp::session_description::RTCSessionDescription> for RtcSessionDescription {
790    fn from(desc: webrtc::peer_connection::sdp::session_description::RTCSessionDescription) -> Self {
791        Self {
792            sdp_type: desc.sdp_type.into(),
793            sdp: desc.sdp,
794        }
795    }
796}
797
798#[cfg(feature = "webrtc")]
799impl TryFrom<RtcSessionDescription> for webrtc::peer_connection::sdp::session_description::RTCSessionDescription {
800    type Error = anyhow::Error;
801
802    fn try_from(desc: RtcSessionDescription) -> Result<Self, Self::Error> {
803        let result = match desc.sdp_type {
804            RtcSdpType::Offer => {
805                webrtc::peer_connection::sdp::session_description::RTCSessionDescription::offer(desc.sdp)?
806            }
807            RtcSdpType::Pranswer => {
808                webrtc::peer_connection::sdp::session_description::RTCSessionDescription::pranswer(desc.sdp)?
809            }
810            RtcSdpType::Answer => {
811                webrtc::peer_connection::sdp::session_description::RTCSessionDescription::answer(desc.sdp)?
812            }
813            RtcSdpType::Rollback => anyhow::bail!("Rollback is not supported"),
814            RtcSdpType::Unspecified => anyhow::bail!("Unspecified is not supported"),
815        };
816
817        Ok(result)
818    }
819}
820
821/// SDPType describes the type of an SessionDescription.
822#[derive(Default, Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize, JsonSchema)]
823#[serde(rename_all = "snake_case")]
824#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
825pub enum RtcSdpType {
826    /// Unspecified indicates that the type is unspecified.
827    #[default]
828    Unspecified = 0,
829
830    /// indicates that a description MUST be treated as an SDP offer.
831    Offer,
832
833    /// indicates that a description MUST be treated as an
834    /// SDP answer, but not a final answer. A description used as an SDP
835    /// pranswer may be applied as a response to an SDP offer, or an update to
836    /// a previously sent SDP pranswer.
837    Pranswer,
838
839    /// indicates that a description MUST be treated as an SDP
840    /// final answer, and the offer-answer exchange MUST be considered complete.
841    /// A description used as an SDP answer may be applied as a response to an
842    /// SDP offer or as an update to a previously sent SDP pranswer.
843    Answer,
844
845    /// indicates that a description MUST be treated as
846    /// canceling the current SDP negotiation and moving the SDP offer and
847    /// answer back to what it was in the previous stable state. Note the
848    /// local or remote SDP descriptions in the previous stable state could be
849    /// null if there has not yet been a successful offer-answer negotiation.
850    Rollback,
851}
852
853#[cfg(feature = "webrtc")]
854impl From<webrtc::peer_connection::sdp::sdp_type::RTCSdpType> for RtcSdpType {
855    fn from(sdp_type: webrtc::peer_connection::sdp::sdp_type::RTCSdpType) -> Self {
856        match sdp_type {
857            webrtc::peer_connection::sdp::sdp_type::RTCSdpType::Offer => Self::Offer,
858            webrtc::peer_connection::sdp::sdp_type::RTCSdpType::Pranswer => Self::Pranswer,
859            webrtc::peer_connection::sdp::sdp_type::RTCSdpType::Answer => Self::Answer,
860            webrtc::peer_connection::sdp::sdp_type::RTCSdpType::Rollback => Self::Rollback,
861            webrtc::peer_connection::sdp::sdp_type::RTCSdpType::Unspecified => Self::Unspecified,
862        }
863    }
864}
865
866#[cfg(feature = "webrtc")]
867impl From<RtcSdpType> for webrtc::peer_connection::sdp::sdp_type::RTCSdpType {
868    fn from(sdp_type: RtcSdpType) -> Self {
869        match sdp_type {
870            RtcSdpType::Offer => Self::Offer,
871            RtcSdpType::Pranswer => Self::Pranswer,
872            RtcSdpType::Answer => Self::Answer,
873            RtcSdpType::Rollback => Self::Rollback,
874            RtcSdpType::Unspecified => Self::Unspecified,
875        }
876    }
877}
878/// Successful Websocket response.
879#[derive(JsonSchema, Debug, Serialize, Deserialize, Clone)]
880#[serde(rename_all = "snake_case")]
881pub struct ModelingSessionData {
882    /// ID of the API call this modeling session is using.
883    /// Useful for tracing and debugging.
884    pub api_call_id: String,
885}
886
887#[cfg(test)]
888mod tests {
889    use super::*;
890    use crate::output;
891
892    const REQ_ID: Uuid = uuid::uuid!("cc30d5e2-482b-4498-b5d2-6131c30a50a4");
893
894    #[test]
895    fn serialize_websocket_modeling_ok() {
896        let actual = WebSocketResponse::Success(SuccessWebSocketResponse {
897            success: true,
898            request_id: Some(REQ_ID),
899            resp: OkWebSocketResponseData::Modeling {
900                modeling_response: OkModelingCmdResponse::CurveGetControlPoints(output::CurveGetControlPoints {
901                    control_points: vec![],
902                }),
903            },
904        });
905        let expected = serde_json::json!({
906            "success": true,
907            "request_id": "cc30d5e2-482b-4498-b5d2-6131c30a50a4",
908            "resp": {
909                "type": "modeling",
910                "data": {
911                    "modeling_response": {
912                        "type": "curve_get_control_points",
913                        "data": { "control_points": [] }
914                    }
915                }
916            }
917        });
918        assert_json_eq(actual, expected);
919    }
920
921    #[test]
922    fn serialize_websocket_webrtc_ok() {
923        let actual = WebSocketResponse::Success(SuccessWebSocketResponse {
924            success: true,
925            request_id: Some(REQ_ID),
926            resp: OkWebSocketResponseData::IceServerInfo { ice_servers: vec![] },
927        });
928        let expected = serde_json::json!({
929            "success": true,
930            "request_id": "cc30d5e2-482b-4498-b5d2-6131c30a50a4",
931            "resp": {
932                "type": "ice_server_info",
933                "data": {
934                    "ice_servers": []
935                }
936            }
937        });
938        assert_json_eq(actual, expected);
939    }
940
941    #[test]
942    fn serialize_websocket_export_ok() {
943        let actual = WebSocketResponse::Success(SuccessWebSocketResponse {
944            success: true,
945            request_id: Some(REQ_ID),
946            resp: OkWebSocketResponseData::Export { files: vec![] },
947        });
948        let expected = serde_json::json!({
949            "success": true,
950            "request_id": "cc30d5e2-482b-4498-b5d2-6131c30a50a4",
951            "resp": {
952                "type": "export",
953                "data": {"files": [] }
954            }
955        });
956        assert_json_eq(actual, expected);
957    }
958
959    #[test]
960    fn serialize_websocket_err() {
961        let actual = WebSocketResponse::Failure(FailureWebSocketResponse {
962            success: false,
963            request_id: Some(REQ_ID),
964            errors: vec![ApiError {
965                error_code: ErrorCode::InternalApi,
966                message: "you fucked up!".to_owned(),
967            }],
968        });
969        let expected = serde_json::json!({
970            "success": false,
971            "request_id": "cc30d5e2-482b-4498-b5d2-6131c30a50a4",
972            "errors": [
973                {
974                    "error_code": "internal_api",
975                    "message": "you fucked up!"
976                }
977            ],
978        });
979        assert_json_eq(actual, expected);
980    }
981
982    #[test]
983    fn serialize_websocket_metrics() {
984        let actual = WebSocketRequest::MetricsResponse {
985            metrics: Box::new(ClientMetrics {
986                rtc_frames_dropped: Some(1),
987                rtc_frames_decoded: Some(2),
988                rtc_frames_per_second: Some(3),
989                rtc_frames_received: Some(4),
990                rtc_freeze_count: Some(5),
991                rtc_jitter_sec: Some(6.7),
992                rtc_keyframes_decoded: Some(8),
993                rtc_total_freezes_duration_sec: Some(9.1),
994                rtc_frame_height: Some(100),
995                rtc_frame_width: Some(100),
996                rtc_packets_lost: Some(0),
997                rtc_pli_count: Some(0),
998                rtc_pause_count: Some(0),
999                rtc_total_pauses_duration_sec: Some(0.0),
1000                rtc_stun_rtt_sec: Some(0.005),
1001            }),
1002        };
1003        let expected = serde_json::json!({
1004            "type": "metrics_response",
1005            "metrics": {
1006                "rtc_frames_dropped": 1,
1007                "rtc_frames_decoded": 2,
1008                "rtc_frames_per_second": 3,
1009                "rtc_frames_received": 4,
1010                "rtc_freeze_count": 5,
1011                "rtc_jitter_sec": 6.7,
1012                "rtc_keyframes_decoded": 8,
1013                "rtc_total_freezes_duration_sec": 9.1,
1014                "rtc_frame_height": 100,
1015                "rtc_frame_width": 100,
1016                "rtc_packets_lost": 0,
1017                "rtc_pli_count": 0,
1018                "rtc_pause_count": 0,
1019                "rtc_total_pauses_duration_sec": 0.0,
1020                "rtc_stun_rtt_sec": 0.005,
1021            },
1022        });
1023        assert_json_eq(actual, expected);
1024    }
1025
1026    fn assert_json_eq<T: Serialize>(actual: T, expected: serde_json::Value) {
1027        let json_str = serde_json::to_string(&actual).unwrap();
1028        let actual: serde_json::Value = serde_json::from_str(&json_str).unwrap();
1029        assert_eq!(actual, expected, "got\n{actual:#}\n, expected\n{expected:#}\n");
1030    }
1031}