medea_jason/platform/peer_connection.rs
1//! Platform-agnostic functionality of [`platform::RtcPeerConnection`].
2
3use derive_more::with_trait::{Display, From};
4#[cfg(doc)]
5use platform::Transceiver;
6
7use crate::{
8 platform::{self, RtcStatsError},
9 utils::Caused,
10};
11
12/// Representation of [RTCSdpType].
13///
14/// [RTCSdpType]: https://w3.org/TR/webrtc#dom-rtcsdptype
15#[derive(Debug)]
16pub enum SdpType {
17 /// [`offer` type][1] of SDP.
18 ///
19 /// [1]: https://w3.org/TR/webrtc#dom-rtcsdptype-offer
20 Offer(String),
21
22 /// [`answer` type][1] of SDP.
23 ///
24 /// [1]: https://w3.org/TR/webrtc#dom-rtcsdptype-answer
25 Answer(String),
26}
27
28/// [RTCIceCandidate][1] representation.
29///
30/// [1]: https://w3.org/TR/webrtc#rtcicecandidate-interface
31#[derive(Debug)]
32pub struct IceCandidate {
33 /// [`candidate` field][2] of the discovered [RTCIceCandidate][1].
34 ///
35 /// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
36 /// [2]: https://w3.org/TR/webrtc#dom-rtcicecandidate-candidate
37 pub candidate: String,
38
39 /// [`sdpMLineIndex` field][2] of the discovered [RTCIceCandidate][1].
40 ///
41 /// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
42 /// [2]: https://w3.org/TR/webrtc#dom-rtcicecandidate-sdpmlineindex
43 pub sdp_m_line_index: Option<u16>,
44
45 /// [`sdpMid` field][2] of the discovered [RTCIceCandidate][1].
46 ///
47 /// [1]: https://w3.org/TR/webrtc#dom-rtcicecandidate
48 /// [2]: https://w3.org/TR/webrtc#dom-rtcicecandidate-sdpmid
49 pub sdp_mid: Option<String>,
50}
51
52/// Error occurred with an [ICE] candidate from a `PeerConnection`.
53///
54/// [ICE]: https://webrtcglossary.com/ice
55#[derive(Debug)]
56pub struct IceCandidateError {
57 /// Local IP address used to communicate with a [STUN]/[TURN] server.
58 ///
59 /// [STUN]: https://webrtcglossary.com/stun
60 /// [TURN]: https://webrtcglossary.com/turn
61 pub address: Option<String>,
62
63 /// Port used to communicate with a [STUN]/[TURN] server.
64 ///
65 /// [STUN]: https://webrtcglossary.com/stun
66 /// [TURN]: https://webrtcglossary.com/turn
67 pub port: Option<u32>,
68
69 /// URL identifying the [STUN]/[TURN] server for which the failure
70 /// occurred.
71 ///
72 /// [STUN]: https://webrtcglossary.com/stun
73 /// [TURN]: https://webrtcglossary.com/turn
74 pub url: String,
75
76 /// Numeric [STUN] error code returned by the [STUN]/[TURN] server.
77 ///
78 /// If no host candidate can reach the server, this error code will be set
79 /// to the value `701`, which is outside the [STUN] error code range. This
80 /// error is only fired once per server URL while in the
81 /// `RTCIceGatheringState` of "gathering".
82 ///
83 /// [STUN]: https://webrtcglossary.com/stun
84 /// [TURN]: https://webrtcglossary.com/turn
85 pub error_code: i32,
86
87 /// [STUN] reason text returned by the [STUN]/[TURN] server.
88 ///
89 /// If the server could not be reached, this reason test will be set to an
90 /// implementation-specific value providing details about the error.
91 ///
92 /// [STUN]: https://webrtcglossary.com/stun
93 /// [TURN]: https://webrtcglossary.com/turn
94 pub error_text: String,
95}
96
97/// Errors that may occur during signaling between this and remote
98/// [RTCPeerConnection][1] and event handlers setting errors.
99///
100/// [1]: https://w3.org/TR/webrtc#dom-rtcpeerconnection
101#[derive(Caused, Clone, Debug, Display, From)]
102#[cause(error = platform::Error)]
103pub enum RtcPeerConnectionError {
104 /// Occurs when cannot adds new remote candidate to the
105 /// [RTCPeerConnection][1]'s remote description.
106 ///
107 /// [1]: https://w3.org/TR/webrtc#dom-rtcpeerconnection
108 #[display("Failed to add ICE candidate: {_0}")]
109 #[from(ignore)]
110 AddIceCandidateFailed(platform::Error),
111
112 /// Occurs when cannot obtains [SDP answer][`SdpType::Answer`] from
113 /// the underlying [`platform::RtcPeerConnection`].
114 #[display("Failed to create SDP answer: {_0}")]
115 #[from(ignore)]
116 CreateAnswerFailed(platform::Error),
117
118 /// Occurs when a new [`platform::RtcPeerConnection`] cannot be created.
119 #[display("Failed to create PeerConnection: {_0}")]
120 #[from(ignore)]
121 PeerCreationError(platform::Error),
122
123 /// Occurs when cannot obtains [SDP offer][`SdpType::Offer`] from
124 /// the underlying [`platform::RtcPeerConnection`].
125 #[display("Failed to create SDP offer: {_0}")]
126 #[from(ignore)]
127 CreateOfferFailed(platform::Error),
128
129 /// Occurs while getting and parsing [`platform::RtcStats`] of
130 /// [`platform::RtcPeerConnection`].
131 #[display("Failed to get RTCStats: {_0}")]
132 RtcStatsError(#[cause] RtcStatsError),
133
134 /// [PeerConnection.getStats][1] promise thrown exception.
135 ///
136 /// [1]: https://tinyurl.com/w6hmt5f
137 #[display("PeerConnection.getStats() failed with error: {_0}")]
138 #[from(ignore)]
139 GetStatsException(platform::Error),
140
141 /// Occurs if the local description associated with the
142 /// [`platform::RtcPeerConnection`] cannot be changed.
143 #[display("Failed to set local SDP description: {_0}")]
144 #[from(ignore)]
145 SetLocalDescriptionFailed(platform::Error),
146
147 /// Occurs if the description of the remote end of the
148 /// [`platform::RtcPeerConnection`] cannot be changed.
149 #[display("Failed to set remote SDP description: {_0}")]
150 #[from(ignore)]
151 SetRemoteDescriptionFailed(platform::Error),
152
153 /// [`Transceiver::update_send_encodings`] error.
154 #[display("Failed to update sender encodings: {_0}")]
155 #[from(ignore)]
156 UpdateSendEncodingsError(platform::transceiver::UpdateSendEncodingError),
157}