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
//! Wrapper around [RTCRtpEncodingParameters][0].
//!
//! [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
use dart_sys::Dart_Handle;
use medea_client_api_proto::{EncodingParameters, ScalabilityMode};
use medea_macro::dart_bridge;
use crate::platform::dart::utils::handle::DartHandle;
use super::utils::{c_str_into_string, string_into_c_str};
#[dart_bridge(
"flutter/lib/src/native/platform/send_encoding_parameters.g.dart"
)]
mod send_encoding_parameters {
use std::{os::raw::c_char, ptr};
use dart_sys::Dart_Handle;
use crate::platform::Error;
extern "C" {
/// Creates new [RTCRtpEncodingParameters][0].
///
/// [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
pub fn new_send_encoding_parameters(
rid: ptr::NonNull<c_char>,
active: bool,
) -> Result<Dart_Handle, Error>;
/// Returns [RID] from the provided [RTCRtpEncodingParameters][0].
///
/// [RID]: https://w3.org/TR/webrtc#dom-rtcrtpcodingparameters-rid
/// [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
pub fn get_rid(
encoding: Dart_Handle,
) -> Result<ptr::NonNull<c_char>, Error>;
/// Sets [activeness][1] of the provided [RTCRtpEncodingParameters][0].
///
/// [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters-active
pub fn set_active(
encoding: Dart_Handle,
active: bool,
) -> Result<(), Error>;
/// Sets [maxBitrate][1] of the provided [RTCRtpEncodingParameters][0].
///
/// [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
/// [1]:https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters-maxbitrate
pub fn set_max_bitrate(
encoding: Dart_Handle,
max_bitrate: i64,
) -> Result<(), Error>;
/// Sets [scaleResolutionDownBy][1] of the provided
/// [RTCRtpEncodingParameters][0].
///
/// [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
/// [1]: https://tinyurl.com/ypzzc75t
pub fn set_scale_resolution_down_by(
encoding: Dart_Handle,
scale_resolution_down_by: i64,
) -> Result<(), Error>;
/// Sets [scalabilityMode][1] of the provided
/// [RTCRtpEncodingParameters][0].
///
/// [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
/// [1]: https://tinyurl.com/3zuaee45
pub fn set_scalability_mode(
encoding: Dart_Handle,
scalability_mode: ptr::NonNull<c_char>,
) -> Result<(), Error>;
}
}
/// Wrapper around [RTCRtpEncodingParameters][0] providing handy methods for its
/// direction changes.
///
/// [0]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters
#[derive(Clone, Debug)]
pub struct SendEncodingParameters(DartHandle);
impl From<DartHandle> for SendEncodingParameters {
fn from(value: DartHandle) -> Self {
Self(value)
}
}
impl SendEncodingParameters {
/// Creates new [`SendEncodingParameters`].
#[must_use]
pub fn new(rid: String, active: bool) -> Self {
let handle = unsafe {
send_encoding_parameters::new_send_encoding_parameters(
string_into_c_str(rid),
active,
)
}
.unwrap();
Self(unsafe { DartHandle::new(handle) })
}
/// Returns the underlying [`Dart_Handle`] of these
/// [`SendEncodingParameters`].
#[must_use]
pub fn handle(&self) -> Dart_Handle {
self.0.get()
}
/// Returns [RID] of these [`SendEncodingParameters`].
///
/// [RID]: https://w3.org/TR/webrtc#dom-rtcrtpcodingparameters-rid
#[must_use]
pub fn rid(&self) -> String {
let handle = self.0.get();
let rid = unsafe { send_encoding_parameters::get_rid(handle) }.unwrap();
unsafe { c_str_into_string(rid) }
}
/// Sets [activeness][1] of these [`SendEncodingParameters`].
///
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters-active
#[allow(clippy::needless_pass_by_ref_mut)] // semantically correct
pub fn set_active(&mut self, active: bool) {
let handle = self.0.get();
unsafe { send_encoding_parameters::set_active(handle, active) }
.unwrap();
}
/// Sets [maxBitrate][1] of these [`SendEncodingParameters`].
///
/// [1]: https://w3.org/TR/webrtc#dom-rtcrtpencodingparameters-maxbitrate
#[allow(clippy::needless_pass_by_ref_mut)] // semantically correct
pub fn set_max_bitrate(&mut self, max_bitrate: i64) {
let handle = self.0.get();
unsafe {
send_encoding_parameters::set_max_bitrate(handle, max_bitrate)
}
.unwrap();
}
/// Sets [scaleResolutionDownBy][1] of these [`SendEncodingParameters`].
///
/// [1]: https://tinyurl.com/ypzzc75t
#[allow(clippy::needless_pass_by_ref_mut)] // semantically correct
pub fn set_scale_resolution_down_by(
&mut self,
scale_resolution_down_by: i64,
) {
let handle = self.0.get();
unsafe {
send_encoding_parameters::set_scale_resolution_down_by(
handle,
scale_resolution_down_by,
)
}
.unwrap();
}
/// Sets [scalabilityMode][1] of these [`SendEncodingParameters`].
///
/// [1]: https://tinyurl.com/3zuaee45
#[allow(clippy::needless_pass_by_ref_mut)] // semantically correct
pub fn set_scalability_mode(&mut self, scalability_mode: ScalabilityMode) {
let handle = self.0.get();
unsafe {
send_encoding_parameters::set_scalability_mode(
handle,
string_into_c_str(scalability_mode.to_string()),
)
}
.unwrap();
}
}
impl From<EncodingParameters> for SendEncodingParameters {
fn from(from: EncodingParameters) -> Self {
let EncodingParameters {
rid,
active,
max_bitrate,
scale_resolution_down_by,
} = from;
let mut enc = Self::new(rid, active);
if let Some(b) = max_bitrate {
enc.set_max_bitrate(b.into());
}
if let Some(s) = scale_resolution_down_by {
enc.set_scale_resolution_down_by(s.into());
}
enc
}
}