1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! ICE candidate of a [RTCPeerConnection][1].
//!
//! [1]: https://w3.org/TR/webrtc#dom-rtcpeerconnection
use dart_sys::Dart_Handle;
use derive_more::From;
use medea_macro::dart_bridge;
use crate::platform::dart::utils::{dart_string_into_rust, handle::DartHandle};
#[dart_bridge("flutter/lib/src/native/platform/ice_candidate.g.dart")]
mod ice_candidate {
use std::{os::raw::c_char, ptr};
use dart_sys::Dart_Handle;
use crate::{api::DartValueArg, platform::Error};
extern "C" {
/// Creates a new [`IceCandidate`] with the provided parameters.
pub fn init(
candidate: DartValueArg<String>,
sdp_mid: DartValueArg<Option<String>>,
sdp_m_line_index: DartValueArg<Option<u16>>,
) -> Result<Dart_Handle, Error>;
/// Returns candidate of the provided [`IceCandidate`].
pub fn candidate(
ice_candidate: Dart_Handle,
) -> Result<ptr::NonNull<c_char>, Error>;
/// Returns SDP line index of the provided [`IceCandidate`].
pub fn sdp_m_line_index(
ice_candidate: Dart_Handle,
) -> Result<u64, Error>;
/// Returns SDP MID of the provided [`IceCandidate`].
pub fn sdp_mid(
ice_candidate: Dart_Handle,
) -> Result<ptr::NonNull<c_char>, Error>;
}
}
#[dart_bridge("flutter/lib/src/native/platform/ice_candidate_error.g.dart")]
mod ice_candidate_error {
use std::{os::raw::c_char, ptr};
use dart_sys::Dart_Handle;
use crate::platform::Error;
extern "C" {
/// Returns the local IP address used to communicate with a
/// [STUN]/[TURN] server.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
pub fn address(
error: Dart_Handle,
) -> Result<ptr::NonNull<c_char>, Error>;
/// Returns the port used to communicate with a [STUN]/[TURN] server.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
pub fn port(error: Dart_Handle) -> Result<u32, Error>;
/// Returns the URL identifying the [STUN]/[TURN] server for which the
/// failure occurred.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
pub fn url(error: Dart_Handle) -> Result<ptr::NonNull<c_char>, Error>;
/// Returns the Numeric [STUN] error code returned by the [STUN]/[TURN]
/// server.
///
/// If no host candidate can reach the server, this error code will be
/// set to the value `701`, which is outside the [STUN] error code
/// range. This error is only fired once per server URL while in the
/// `RTCIceGatheringState` of "gathering".
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
pub fn error_code(error: Dart_Handle) -> Result<i32, Error>;
/// [STUN] reason text returned by the [STUN]/[TURN] server.
///
/// If the server could not be reached, this reason test will be set to
/// an implementation-specific value providing details about the error.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
pub fn error_text(
error: Dart_Handle,
) -> Result<ptr::NonNull<c_char>, Error>;
}
}
/// Error occurred with an [ICE] candidate from a [`PeerConnection`].
///
/// [`PeerConnection`]: crate::peer::PeerConnection
/// [ICE]: https://webrtcglossary.com/ice
#[derive(Debug, From)]
pub struct IceCandidateError(DartHandle);
impl IceCandidateError {
/// Returns the local IP address used to communicate with a [STUN]/[TURN]
/// server.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
#[must_use]
pub fn address(&self) -> String {
let address =
unsafe { ice_candidate_error::address(self.0.get()) }.unwrap();
unsafe { dart_string_into_rust(address) }
}
/// Returns the port used to communicate with a [STUN]/[TURN] server.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
#[must_use]
pub fn port(&self) -> u32 {
unsafe { ice_candidate_error::port(self.0.get()) }.unwrap()
}
/// Returns the URL identifying the [STUN]/[TURN] server for which the
/// failure occurred.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
#[must_use]
pub fn url(&self) -> String {
let url = unsafe { ice_candidate_error::url(self.0.get()) }.unwrap();
unsafe { dart_string_into_rust(url) }
}
/// Returns the Numeric [STUN] error code returned by the [STUN]/[TURN]
/// server.
///
/// If no host candidate can reach the server, this error code will be set
/// to the value `701`, which is outside the [STUN] error code range. This
/// error is only fired once per server URL while in the
/// `RTCIceGatheringState` of "gathering".
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
#[must_use]
pub fn error_code(&self) -> i32 {
unsafe { ice_candidate_error::error_code(self.0.get()) }.unwrap()
}
/// [STUN] reason text returned by the [STUN]/[TURN] server.
///
/// If the server could not be reached, this reason test will be set to an
/// implementation-specific value providing details about the error.
///
/// [STUN]: https://webrtcglossary.com/stun
/// [TURN]: https://webrtcglossary.com/turn
#[must_use]
pub fn error_text(&self) -> String {
let error_text =
unsafe { ice_candidate_error::error_text(self.0.get()) }.unwrap();
unsafe { dart_string_into_rust(error_text) }
}
}
/// Wrapper around a [`DartHandle`] representing an ICE candidate of a
/// [RTCPeerConnection][1].
///
/// [1]: https://w3.org/TR/webrtc#dom-rtcpeerconnection
#[derive(Debug, From)]
pub struct IceCandidate(DartHandle);
impl IceCandidate {
/// Returns a new [`IceCandidate`] with the provided parameters.
#[must_use]
pub fn new(
candidate: &str,
sdp_m_line_index: Option<u16>,
sdp_mid: &Option<String>,
) -> Self {
let handle = unsafe {
ice_candidate::init(
candidate.to_owned().into(),
sdp_mid.clone().into(),
sdp_m_line_index.map(i64::from).into(),
)
}
.unwrap();
Self(unsafe { DartHandle::new(handle) })
}
/// Returns the underlying [`Dart_Handle`] of this [`IceCandidate`].
#[must_use]
pub fn handle(&self) -> Dart_Handle {
self.0.get()
}
/// Returns candidate of this [`IceCandidate`].
#[must_use]
pub fn candidate(&self) -> String {
let candidate =
unsafe { ice_candidate::candidate(self.0.get()) }.unwrap();
unsafe { dart_string_into_rust(candidate) }
}
/// Returns SDP M line index of this [`IceCandidate`].
#[expect(clippy::unwrap_in_result, reason = "unreleated")]
#[must_use]
pub fn sdp_m_line_index(&self) -> Option<u16> {
Some(unsafe {
ice_candidate::sdp_m_line_index(self.0.get())
.unwrap()
.try_into()
.unwrap()
})
}
/// Returns SDP MID of this [`IceCandidate`].
#[must_use]
#[expect(clippy::unwrap_in_result, reason = "unrelated")]
pub fn sdp_mid(&self) -> Option<String> {
let mid = unsafe { ice_candidate::sdp_mid(self.0.get()) }.unwrap();
Some(unsafe { dart_string_into_rust(mid) })
}
}