| 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 | |
| 15 | use std::fmt::Debug; |
| 16 | |
| 17 | use crate::{ |
| 18 | imp::peer_connection_factory as imp_pcf, peer_connection::PeerConnection, |
| 19 | rtp_parameters::RtpCapabilities, MediaType, RtcError, |
| 20 | }; |
| 21 | |
| 22 | #[derive(Debug, Clone)] |
| 23 | pub struct IceServer { |
| 24 | pub urls: Vec<String>, |
| 25 | pub username: String, |
| 26 | pub password: String, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 30 | pub enum ContinualGatheringPolicy { |
| 31 | GatherOnce, |
| 32 | GatherContinually, |
| 33 | } |
| 34 | |
| 35 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 36 | pub 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] |
| 54 | pub 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 | |
| 64 | impl 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)] |
| 76 | pub struct PeerConnectionFactory { |
| 77 | pub(crate) handle: imp_pcf::PeerConnectionFactory, |
| 78 | } |
| 79 | |
| 80 | impl Debug for PeerConnectionFactory { |
| 81 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 82 | f.debug_struct("PeerConnectionFactory").finish() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl 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")))] |
| 120 | mod 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 | |
| 134 | pub 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 | /// Create a Platform ADM audio track with explicit WebRTC noise suppression toggle. |
| 150 | fn create_device_audio_track_with_options( |
| 151 | &self, |
| 152 | label: &str, |
| 153 | noise_suppression: bool, |
| 154 | ) -> RtcAudioTrack; |
| 155 | |
| 156 | // Device enumeration |
| 157 | fn playout_devices(&self) -> i16; |
| 158 | fn recording_devices(&self) -> i16; |
| 159 | fn playout_device_name(&self, index: u16) -> String; |
| 160 | fn recording_device_name(&self, index: u16) -> String; |
| 161 | /// Get device GUID (platform-specific unique identifier, stable across hot-plug) |
| 162 | fn playout_device_guid(&self, index: u16) -> String; |
| 163 | fn recording_device_guid(&self, index: u16) -> String; |
| 164 | |
| 165 | // Device selection by index |
| 166 | fn set_playout_device(&self, index: u16) -> bool; |
| 167 | fn set_recording_device(&self, index: u16) -> bool; |
| 168 | /// Device selection by GUID (preferred - stable across device changes) |
| 169 | fn set_playout_device_by_guid(&self, guid: &str) -> bool; |
| 170 | fn set_recording_device_by_guid(&self, guid: &str) -> bool; |
| 171 | |
| 172 | // Recording control (for device switching while active) |
| 173 | fn stop_recording(&self) -> bool; |
| 174 | fn init_recording(&self) -> bool; |
| 175 | fn start_recording(&self) -> bool; |
| 176 | fn recording_is_initialized(&self) -> bool; |
| 177 | |
| 178 | // Playout control (for device switching while active) |
| 179 | fn stop_playout(&self) -> bool; |
| 180 | fn init_playout(&self) -> bool; |
| 181 | fn start_playout(&self) -> bool; |
| 182 | fn playout_is_initialized(&self) -> bool; |
| 183 | |
| 184 | // Built-in audio processing (hardware AEC/AGC/NS) |
| 185 | // Only available on iOS and some Android devices |
| 186 | fn builtin_aec_is_available(&self) -> bool; |
| 187 | fn builtin_agc_is_available(&self) -> bool; |
| 188 | fn builtin_ns_is_available(&self) -> bool; |
| 189 | fn enable_builtin_aec(&self, enable: bool) -> bool; |
| 190 | fn enable_builtin_agc(&self, enable: bool) -> bool; |
| 191 | fn enable_builtin_ns(&self, enable: bool) -> bool; |
| 192 | |
| 193 | // ADM recording control |
| 194 | // Use this to disable microphone when only using NativeAudioSource |
| 195 | fn set_adm_recording_enabled(&self, enabled: bool); |
| 196 | fn adm_recording_enabled(&self) -> bool; |
| 197 | |
| 198 | // ADM playout control |
| 199 | // When disabled (default), playout uses synthetic mode - remote audio is |
| 200 | // delivered via FFI callbacks. When enabled, plays through platform speakers. |
| 201 | fn set_adm_playout_enabled(&self, enabled: bool); |
| 202 | fn adm_playout_enabled(&self) -> bool; |
| 203 | |
| 204 | // Platform ADM lifecycle management |
| 205 | // Call acquire_platform_adm when creating PlatformAudio. |
| 206 | // Call release_platform_adm when disposing PlatformAudio. |
| 207 | // The Platform ADM is only created when first acquired, and terminated |
| 208 | // when the last reference is released. |
| 209 | fn acquire_platform_adm(&self) -> bool; |
| 210 | fn release_platform_adm(&self); |
| 211 | fn platform_adm_ref_count(&self) -> i32; |
| 212 | fn is_platform_adm_active(&self) -> bool; |
| 213 | } |
| 214 | |
| 215 | impl PeerConnectionFactoryExt for PeerConnectionFactory { |
| 216 | fn create_video_track(&self, label: &str, source: NativeVideoSource) -> RtcVideoTrack { |
| 217 | self.handle.create_video_track(label, source) |
| 218 | } |
| 219 | |
| 220 | fn create_audio_track(&self, label: &str, source: NativeAudioSource) -> RtcAudioTrack { |
| 221 | self.handle.create_audio_track(label, source) |
| 222 | } |
| 223 | |
| 224 | fn create_device_audio_track(&self, label: &str) -> RtcAudioTrack { |
| 225 | self.handle.create_device_audio_track(label) |
| 226 | } |
| 227 | |
| 228 | fn create_device_audio_track_with_options( |
| 229 | &self, |
| 230 | label: &str, |
| 231 | noise_suppression: bool, |
| 232 | ) -> RtcAudioTrack { |
| 233 | self.handle.create_device_audio_track_with_options(label, noise_suppression) |
| 234 | } |
| 235 | |
| 236 | fn playout_devices(&self) -> i16 { |
| 237 | self.handle.playout_devices() |
| 238 | } |
| 239 | |
| 240 | fn recording_devices(&self) -> i16 { |
| 241 | self.handle.recording_devices() |
| 242 | } |
| 243 | |
| 244 | fn playout_device_name(&self, index: u16) -> String { |
| 245 | self.handle.playout_device_name(index) |
| 246 | } |
| 247 | |
| 248 | fn recording_device_name(&self, index: u16) -> String { |
| 249 | self.handle.recording_device_name(index) |
| 250 | } |
| 251 | |
| 252 | fn playout_device_guid(&self, index: u16) -> String { |
| 253 | self.handle.playout_device_guid(index) |
| 254 | } |
| 255 | |
| 256 | fn recording_device_guid(&self, index: u16) -> String { |
| 257 | self.handle.recording_device_guid(index) |
| 258 | } |
| 259 | |
| 260 | fn set_playout_device(&self, index: u16) -> bool { |
| 261 | self.handle.set_playout_device(index) |
| 262 | } |
| 263 | |
| 264 | fn set_recording_device(&self, index: u16) -> bool { |
| 265 | self.handle.set_recording_device(index) |
| 266 | } |
| 267 | |
| 268 | fn set_playout_device_by_guid(&self, guid: &str) -> bool { |
| 269 | self.handle.set_playout_device_by_guid(guid) |
| 270 | } |
| 271 | |
| 272 | fn set_recording_device_by_guid(&self, guid: &str) -> bool { |
| 273 | self.handle.set_recording_device_by_guid(guid) |
| 274 | } |
| 275 | |
| 276 | fn stop_recording(&self) -> bool { |
| 277 | self.handle.stop_recording() |
| 278 | } |
| 279 | |
| 280 | fn init_recording(&self) -> bool { |
| 281 | self.handle.init_recording() |
| 282 | } |
| 283 | |
| 284 | fn start_recording(&self) -> bool { |
| 285 | self.handle.start_recording() |
| 286 | } |
| 287 | |
| 288 | fn recording_is_initialized(&self) -> bool { |
| 289 | self.handle.recording_is_initialized() |
| 290 | } |
| 291 | |
| 292 | fn stop_playout(&self) -> bool { |
| 293 | self.handle.stop_playout() |
| 294 | } |
| 295 | |
| 296 | fn init_playout(&self) -> bool { |
| 297 | self.handle.init_playout() |
| 298 | } |
| 299 | |
| 300 | fn start_playout(&self) -> bool { |
| 301 | self.handle.start_playout() |
| 302 | } |
| 303 | |
| 304 | fn playout_is_initialized(&self) -> bool { |
| 305 | self.handle.playout_is_initialized() |
| 306 | } |
| 307 | |
| 308 | fn builtin_aec_is_available(&self) -> bool { |
| 309 | self.handle.builtin_aec_is_available() |
| 310 | } |
| 311 | |
| 312 | fn builtin_agc_is_available(&self) -> bool { |
| 313 | self.handle.builtin_agc_is_available() |
| 314 | } |
| 315 | |
| 316 | fn builtin_ns_is_available(&self) -> bool { |
| 317 | self.handle.builtin_ns_is_available() |
| 318 | } |
| 319 | |
| 320 | fn enable_builtin_aec(&self, enable: bool) -> bool { |
| 321 | self.handle.enable_builtin_aec(enable) |
| 322 | } |
| 323 | |
| 324 | fn enable_builtin_agc(&self, enable: bool) -> bool { |
| 325 | self.handle.enable_builtin_agc(enable) |
| 326 | } |
| 327 | |
| 328 | fn enable_builtin_ns(&self, enable: bool) -> bool { |
| 329 | self.handle.enable_builtin_ns(enable) |
| 330 | } |
| 331 | |
| 332 | fn set_adm_recording_enabled(&self, enabled: bool) { |
| 333 | self.handle.set_adm_recording_enabled(enabled) |
| 334 | } |
| 335 | |
| 336 | fn adm_recording_enabled(&self) -> bool { |
| 337 | self.handle.adm_recording_enabled() |
| 338 | } |
| 339 | |
| 340 | fn set_adm_playout_enabled(&self, enabled: bool) { |
| 341 | self.handle.set_adm_playout_enabled(enabled) |
| 342 | } |
| 343 | |
| 344 | fn adm_playout_enabled(&self) -> bool { |
| 345 | self.handle.adm_playout_enabled() |
| 346 | } |
| 347 | |
| 348 | fn acquire_platform_adm(&self) -> bool { |
| 349 | self.handle.acquire_platform_adm() |
| 350 | } |
| 351 | |
| 352 | fn release_platform_adm(&self) { |
| 353 | self.handle.release_platform_adm() |
| 354 | } |
| 355 | |
| 356 | fn platform_adm_ref_count(&self) -> i32 { |
| 357 | self.handle.platform_adm_ref_count() |
| 358 | } |
| 359 | |
| 360 | fn is_platform_adm_active(&self) -> bool { |
| 361 | self.handle.is_platform_adm_active() |
| 362 | } |
| 363 | } |
| 364 | } |