BlueGit

ict rusty-teams

Files

rusty-teams / vendor / libwebrtc / src / peer_connection_factory.rs

peer_connection_factory.rs

peer_connection_factory.rs 11.8 KiB Download Raw
1// Copyright 2025 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Debug;
16
17use crate::{
18 imp::peer_connection_factory as imp_pcf, peer_connection::PeerConnection,
19 rtp_parameters::RtpCapabilities, MediaType, RtcError,
20};
21
22#[derive(Debug, Clone)]
23pub struct IceServer {
24 pub urls: Vec<String>,
25 pub username: String,
26 pub password: String,
27}
28
29#[derive(Debug, Copy, Clone, PartialEq, Eq)]
30pub enum ContinualGatheringPolicy {
31 GatherOnce,
32 GatherContinually,
33}
34
35#[derive(Debug, Copy, Clone, PartialEq, Eq)]
36pub enum IceTransportsType {
37 Relay,
38 NoHost,
39 All,
40}
41
42/// Configuration for a [`PeerConnection`].
43///
44/// This type is `#[non_exhaustive]`: construct it from [`RtcConfiguration::default`]
45/// and set the fields you need, e.g.
46/// ```
47/// # use libwebrtc::peer_connection_factory::{IceTransportsType, RtcConfiguration};
48/// let mut cfg = RtcConfiguration::default();
49/// cfg.ice_transport_type = IceTransportsType::Relay;
50/// ```
51/// New fields may be added in future releases without a breaking change.
52#[derive(Debug, Clone)]
53#[non_exhaustive]
54pub struct RtcConfiguration {
55 pub ice_servers: Vec<IceServer>,
56 pub continual_gathering_policy: ContinualGatheringPolicy,
57 pub ice_transport_type: IceTransportsType,
58 /// WARP: enable SNAP (SCTP-INIT-in-SDP). Maps to the immutable
59 /// `enable_sctp_snap` RTCConfiguration field, so it must be set the same at
60 /// PeerConnection creation and every set_configuration.
61 pub enable_sctp_snap: bool,
62}
63
64impl Default for RtcConfiguration {
65 fn default() -> Self {
66 Self {
67 ice_servers: vec![],
68 continual_gathering_policy: ContinualGatheringPolicy::GatherContinually,
69 ice_transport_type: IceTransportsType::All,
70 enable_sctp_snap: false,
71 }
72 }
73}
74
75#[derive(Clone, Default)]
76pub struct PeerConnectionFactory {
77 pub(crate) handle: imp_pcf::PeerConnectionFactory,
78}
79
80impl Debug for PeerConnectionFactory {
81 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
82 f.debug_struct("PeerConnectionFactory").finish()
83 }
84}
85
86impl PeerConnectionFactory {
87 /// Creates a native peer connection factory that renders received video as soon as possible.
88 #[cfg(not(target_arch = "wasm32"))]
89 pub fn with_zero_playout_delay() -> Self {
90 Self { handle: imp_pcf::PeerConnectionFactory::with_zero_playout_delay() }
91 }
92
93 /// Creates a native peer connection factory with the given runtime options.
94 /// `zero_playout_delay` and `enable_warp` (SPED + SNAP) are independent and
95 /// may be combined.
96 #[cfg(not(target_arch = "wasm32"))]
97 pub fn with_options(zero_playout_delay: bool, enable_warp: bool) -> Self {
98 Self {
99 handle: imp_pcf::PeerConnectionFactory::with_options(zero_playout_delay, enable_warp),
100 }
101 }
102
103 pub fn create_peer_connection(
104 &self,
105 config: RtcConfiguration,
106 ) -> Result<PeerConnection, RtcError> {
107 self.handle.create_peer_connection(config)
108 }
109
110 pub fn get_rtp_sender_capabilities(&self, media_type: MediaType) -> RtpCapabilities {
111 self.handle.get_rtp_sender_capabilities(media_type)
112 }
113
114 pub fn get_rtp_receiver_capabilities(&self, media_type: MediaType) -> RtpCapabilities {
115 self.handle.get_rtp_receiver_capabilities(media_type)
116 }
117}
118
119#[cfg(all(test, not(target_arch = "wasm32")))]
120mod tests {
121 use super::PeerConnectionFactory;
122
123 #[test]
124 fn zero_playout_delay_factory_uses_force_playout_delay_field_trial() {
125 let default_factory = PeerConnectionFactory::default();
126 assert!(!default_factory.handle.zero_playout_delay_enabled());
127 drop(default_factory);
128
129 let low_latency_factory = PeerConnectionFactory::with_zero_playout_delay();
130 assert!(low_latency_factory.handle.zero_playout_delay_enabled());
131 }
132}
133
134pub mod native {
135 use super::PeerConnectionFactory;
136 use crate::{
137 audio_source::native::NativeAudioSource, audio_track::RtcAudioTrack,
138 video_source::native::NativeVideoSource, video_track::RtcVideoTrack,
139 };
140
141 pub trait PeerConnectionFactoryExt {
142 fn create_video_track(&self, label: &str, source: NativeVideoSource) -> RtcVideoTrack;
143 fn create_audio_track(&self, label: &str, source: NativeAudioSource) -> RtcAudioTrack;
144
145 /// Create an audio track that uses the Platform ADM for capture.
146 /// The track will capture audio from the selected recording device.
147 fn create_device_audio_track(&self, label: &str) -> RtcAudioTrack;
148
149 // Device enumeration
150 fn playout_devices(&self) -> i16;
151 fn recording_devices(&self) -> i16;
152 fn playout_device_name(&self, index: u16) -> String;
153 fn recording_device_name(&self, index: u16) -> String;
154 /// Get device GUID (platform-specific unique identifier, stable across hot-plug)
155 fn playout_device_guid(&self, index: u16) -> String;
156 fn recording_device_guid(&self, index: u16) -> String;
157
158 // Device selection by index
159 fn set_playout_device(&self, index: u16) -> bool;
160 fn set_recording_device(&self, index: u16) -> bool;
161 /// Device selection by GUID (preferred - stable across device changes)
162 fn set_playout_device_by_guid(&self, guid: &str) -> bool;
163 fn set_recording_device_by_guid(&self, guid: &str) -> bool;
164
165 // Recording control (for device switching while active)
166 fn stop_recording(&self) -> bool;
167 fn init_recording(&self) -> bool;
168 fn start_recording(&self) -> bool;
169 fn recording_is_initialized(&self) -> bool;
170
171 // Playout control (for device switching while active)
172 fn stop_playout(&self) -> bool;
173 fn init_playout(&self) -> bool;
174 fn start_playout(&self) -> bool;
175 fn playout_is_initialized(&self) -> bool;
176
177 // Built-in audio processing (hardware AEC/AGC/NS)
178 // Only available on iOS and some Android devices
179 fn builtin_aec_is_available(&self) -> bool;
180 fn builtin_agc_is_available(&self) -> bool;
181 fn builtin_ns_is_available(&self) -> bool;
182 fn enable_builtin_aec(&self, enable: bool) -> bool;
183 fn enable_builtin_agc(&self, enable: bool) -> bool;
184 fn enable_builtin_ns(&self, enable: bool) -> bool;
185
186 // ADM recording control
187 // Use this to disable microphone when only using NativeAudioSource
188 fn set_adm_recording_enabled(&self, enabled: bool);
189 fn adm_recording_enabled(&self) -> bool;
190
191 // ADM playout control
192 // When disabled (default), playout uses synthetic mode - remote audio is
193 // delivered via FFI callbacks. When enabled, plays through platform speakers.
194 fn set_adm_playout_enabled(&self, enabled: bool);
195 fn adm_playout_enabled(&self) -> bool;
196
197 // Platform ADM lifecycle management
198 // Call acquire_platform_adm when creating PlatformAudio.
199 // Call release_platform_adm when disposing PlatformAudio.
200 // The Platform ADM is only created when first acquired, and terminated
201 // when the last reference is released.
202 fn acquire_platform_adm(&self) -> bool;
203 fn release_platform_adm(&self);
204 fn platform_adm_ref_count(&self) -> i32;
205 fn is_platform_adm_active(&self) -> bool;
206 }
207
208 impl PeerConnectionFactoryExt for PeerConnectionFactory {
209 fn create_video_track(&self, label: &str, source: NativeVideoSource) -> RtcVideoTrack {
210 self.handle.create_video_track(label, source)
211 }
212
213 fn create_audio_track(&self, label: &str, source: NativeAudioSource) -> RtcAudioTrack {
214 self.handle.create_audio_track(label, source)
215 }
216
217 fn create_device_audio_track(&self, label: &str) -> RtcAudioTrack {
218 self.handle.create_device_audio_track(label)
219 }
220
221 fn playout_devices(&self) -> i16 {
222 self.handle.playout_devices()
223 }
224
225 fn recording_devices(&self) -> i16 {
226 self.handle.recording_devices()
227 }
228
229 fn playout_device_name(&self, index: u16) -> String {
230 self.handle.playout_device_name(index)
231 }
232
233 fn recording_device_name(&self, index: u16) -> String {
234 self.handle.recording_device_name(index)
235 }
236
237 fn playout_device_guid(&self, index: u16) -> String {
238 self.handle.playout_device_guid(index)
239 }
240
241 fn recording_device_guid(&self, index: u16) -> String {
242 self.handle.recording_device_guid(index)
243 }
244
245 fn set_playout_device(&self, index: u16) -> bool {
246 self.handle.set_playout_device(index)
247 }
248
249 fn set_recording_device(&self, index: u16) -> bool {
250 self.handle.set_recording_device(index)
251 }
252
253 fn set_playout_device_by_guid(&self, guid: &str) -> bool {
254 self.handle.set_playout_device_by_guid(guid)
255 }
256
257 fn set_recording_device_by_guid(&self, guid: &str) -> bool {
258 self.handle.set_recording_device_by_guid(guid)
259 }
260
261 fn stop_recording(&self) -> bool {
262 self.handle.stop_recording()
263 }
264
265 fn init_recording(&self) -> bool {
266 self.handle.init_recording()
267 }
268
269 fn start_recording(&self) -> bool {
270 self.handle.start_recording()
271 }
272
273 fn recording_is_initialized(&self) -> bool {
274 self.handle.recording_is_initialized()
275 }
276
277 fn stop_playout(&self) -> bool {
278 self.handle.stop_playout()
279 }
280
281 fn init_playout(&self) -> bool {
282 self.handle.init_playout()
283 }
284
285 fn start_playout(&self) -> bool {
286 self.handle.start_playout()
287 }
288
289 fn playout_is_initialized(&self) -> bool {
290 self.handle.playout_is_initialized()
291 }
292
293 fn builtin_aec_is_available(&self) -> bool {
294 self.handle.builtin_aec_is_available()
295 }
296
297 fn builtin_agc_is_available(&self) -> bool {
298 self.handle.builtin_agc_is_available()
299 }
300
301 fn builtin_ns_is_available(&self) -> bool {
302 self.handle.builtin_ns_is_available()
303 }
304
305 fn enable_builtin_aec(&self, enable: bool) -> bool {
306 self.handle.enable_builtin_aec(enable)
307 }
308
309 fn enable_builtin_agc(&self, enable: bool) -> bool {
310 self.handle.enable_builtin_agc(enable)
311 }
312
313 fn enable_builtin_ns(&self, enable: bool) -> bool {
314 self.handle.enable_builtin_ns(enable)
315 }
316
317 fn set_adm_recording_enabled(&self, enabled: bool) {
318 self.handle.set_adm_recording_enabled(enabled)
319 }
320
321 fn adm_recording_enabled(&self) -> bool {
322 self.handle.adm_recording_enabled()
323 }
324
325 fn set_adm_playout_enabled(&self, enabled: bool) {
326 self.handle.set_adm_playout_enabled(enabled)
327 }
328
329 fn adm_playout_enabled(&self) -> bool {
330 self.handle.adm_playout_enabled()
331 }
332
333 fn acquire_platform_adm(&self) -> bool {
334 self.handle.acquire_platform_adm()
335 }
336
337 fn release_platform_adm(&self) {
338 self.handle.release_platform_adm()
339 }
340
341 fn platform_adm_ref_count(&self) -> i32 {
342 self.handle.platform_adm_ref_count()
343 }
344
345 fn is_platform_adm_active(&self) -> bool {
346 self.handle.is_platform_adm_active()
347 }
348 }
349}