| 1 | use std::sync::Arc; |
| 2 | use std::sync::atomic::{AtomicU32, AtomicU64, Ordering}; |
| 3 | use std::time::{Instant, SystemTime, UNIX_EPOCH}; |
| 4 | |
| 5 | use libwebrtc::media_stream_track::MediaStreamTrack; |
| 6 | use libwebrtc::peer_connection_factory::PeerConnectionFactory; |
| 7 | use libwebrtc::peer_connection_factory::native::PeerConnectionFactoryExt; |
| 8 | use libwebrtc::rtp_parameters::DegradationPreference; |
| 9 | use libwebrtc::rtp_sender::RtpSender; |
| 10 | use libwebrtc::rtp_transceiver::{RtpTransceiver, RtpTransceiverDirection}; |
| 11 | use libwebrtc::video_frame::{I420Buffer, VideoBuffer as _, VideoFrame, VideoRotation}; |
| 12 | use libwebrtc::video_source::VideoResolution; |
| 13 | use libwebrtc::video_source::native::NativeVideoSource; |
| 14 | use libwebrtc::video_track::RtcVideoTrack; |
| 15 | use tokio::sync::mpsc::UnboundedSender; |
| 16 | |
| 17 | use crate::background::BackgroundPicture; |
| 18 | use crate::blur::BlurSettings; |
| 19 | use crate::camera::{CameraCaptureStats, start_camera}; |
| 20 | use crate::capture::Capture; |
| 21 | use crate::control::{CallCommand, CallUpdate}; |
| 22 | use crate::devices::DeviceChoice; |
| 23 | use crate::effects::CameraEffects; |
| 24 | use crate::error::{Error, Result}; |
| 25 | use crate::notices::{CAMERA_STOPPED_NOTICE, SCREEN_SHARE_STOPPED_NOTICE, notice_with_detail}; |
| 26 | use crate::screen::{ |
| 27 | PortalFollowUp, ShareKind, ShareSource, start_portal_capture, start_screen_capture, |
| 28 | }; |
| 29 | use crate::send_control::{ |
| 30 | EncoderLimits, KEYFRAME_NUDGE, LayerPlan, SendLimit, encoder_limits, plan_layers, |
| 31 | }; |
| 32 | use crate::standout::StandoutOverlay; |
| 33 | use crate::timeline::Timeline; |
| 34 | use crate::video_frame::{ |
| 35 | FrameGate, PERSON_FPS, SCREEN_FPS, VideoHub, VideoKey, picture_from_i420, |
| 36 | }; |
| 37 | use crate::video_layout::VideoLines; |
| 38 | use crate::video_pattern::{PatternKind, spawn_pattern}; |
| 39 | use crate::video_quality::VideoQuality; |
| 40 | |
| 41 | pub const VIDEO_MODE_ENV: &str = "CALLING_VIDEO"; |
| 42 | const CAMERA_LABEL: &str = "camera"; |
| 43 | const SCREEN_LABEL: &str = "screen"; |
| 44 | pub const SCREEN_SHARE_FPS: u32 = 15; |
| 45 | pub const MOTION_SHARE_FPS: u32 = 30; |
| 46 | |
| 47 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 48 | pub enum VideoMode { |
| 49 | #[default] |
| 50 | Platform, |
| 51 | Pattern, |
| 52 | Off, |
| 53 | } |
| 54 | |
| 55 | impl VideoMode { |
| 56 | pub fn from_env() -> Self { |
| 57 | Self::parse(std::env::var(VIDEO_MODE_ENV).ok().as_deref()) |
| 58 | } |
| 59 | |
| 60 | pub fn parse(value: Option<&str>) -> Self { |
| 61 | match value.map(str::to_ascii_lowercase).as_deref() { |
| 62 | Some("pattern") => VideoMode::Pattern, |
| 63 | Some("off") => VideoMode::Off, |
| 64 | _ => VideoMode::Platform, |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Where a captured frame goes: the encoder source and, for the self view, the UI hub. |
| 70 | pub struct LocalSink { |
| 71 | source: NativeVideoSource, |
| 72 | self_view: Option<(Arc<VideoHub>, VideoKey, FrameGate)>, |
| 73 | mirror: Option<Arc<BlurSettings>>, |
| 74 | standout: Option<StandoutOverlay>, |
| 75 | frame_size: Option<Arc<AtomicU64>>, |
| 76 | } |
| 77 | |
| 78 | impl LocalSink { |
| 79 | pub fn new(source: NativeVideoSource, hub: Option<(Arc<VideoHub>, VideoKey)>) -> Self { |
| 80 | let self_view = hub.map(|(hub, key)| { |
| 81 | let fps = if matches!(key, VideoKey::LocalScreen) { |
| 82 | SCREEN_FPS |
| 83 | } else { |
| 84 | PERSON_FPS |
| 85 | }; |
| 86 | (hub, key, FrameGate::new(fps)) |
| 87 | }); |
| 88 | LocalSink { |
| 89 | source, |
| 90 | self_view, |
| 91 | mirror: None, |
| 92 | standout: None, |
| 93 | frame_size: None, |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub fn reporting_size(mut self, frame_size: Arc<AtomicU64>) -> Self { |
| 98 | self.frame_size = Some(frame_size); |
| 99 | self |
| 100 | } |
| 101 | |
| 102 | pub fn mirrored_by(mut self, settings: Arc<BlurSettings>) -> Self { |
| 103 | self.mirror = Some(settings); |
| 104 | self |
| 105 | } |
| 106 | |
| 107 | pub fn with_standout(mut self, settings: Arc<BlurSettings>) -> Self { |
| 108 | self.standout = Some(StandoutOverlay::new(settings)); |
| 109 | self |
| 110 | } |
| 111 | |
| 112 | pub fn push(&mut self, buffer: I420Buffer) { |
| 113 | let buffer = match &mut self.standout { |
| 114 | Some(overlay) => overlay.apply(buffer), |
| 115 | None => buffer, |
| 116 | }; |
| 117 | if let Some(frame_size) = &self.frame_size { |
| 118 | frame_size.store( |
| 119 | pack_size(buffer.width(), buffer.height()), |
| 120 | Ordering::Relaxed, |
| 121 | ); |
| 122 | } |
| 123 | let mut frame = VideoFrame::new(VideoRotation::VideoRotation0, buffer); |
| 124 | frame.timestamp_us = SystemTime::now() |
| 125 | .duration_since(UNIX_EPOCH) |
| 126 | .map_or(0, |elapsed| elapsed.as_micros() as i64); |
| 127 | self.source.capture_frame(&frame); |
| 128 | let Some((hub, key, gate)) = &mut self.self_view else { |
| 129 | return; |
| 130 | }; |
| 131 | if gate.admit(Instant::now()) |
| 132 | && let Some(mut picture) = picture_from_i420(frame.buffer, key.max_width()) |
| 133 | { |
| 134 | if self |
| 135 | .mirror |
| 136 | .as_ref() |
| 137 | .is_some_and(|settings| settings.effects().mirror) |
| 138 | { |
| 139 | picture.flip_horizontally(); |
| 140 | } |
| 141 | hub.publish(key.clone(), picture); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fn pack_size(width: u32, height: u32) -> u64 { |
| 147 | (u64::from(width) << 32) | u64::from(height) |
| 148 | } |
| 149 | |
| 150 | fn unpack_size(packed: u64) -> Option<(u32, u32)> { |
| 151 | let (width, height) = ((packed >> 32) as u32, packed as u32); |
| 152 | (width > 0 && height > 0).then_some((width, height)) |
| 153 | } |
| 154 | |
| 155 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 156 | pub enum SendSource { |
| 157 | Camera, |
| 158 | Screen, |
| 159 | } |
| 160 | |
| 161 | struct Outgoing { |
| 162 | source: NativeVideoSource, |
| 163 | track: RtcVideoTrack, |
| 164 | capture: Option<Capture>, |
| 165 | frame_size: Arc<AtomicU64>, |
| 166 | requested: Option<RequestedLimits>, |
| 167 | } |
| 168 | |
| 169 | struct RequestedLimits { |
| 170 | limits: Vec<SendLimit>, |
| 171 | applied_for: Option<(u32, u32)>, |
| 172 | } |
| 173 | |
| 174 | impl Outgoing { |
| 175 | fn new( |
| 176 | factory: &PeerConnectionFactory, |
| 177 | label: &str, |
| 178 | kind: PatternKind, |
| 179 | screencast: bool, |
| 180 | ) -> Self { |
| 181 | let (width, height) = kind.size(); |
| 182 | let source = NativeVideoSource::new(VideoResolution { width, height }, screencast); |
| 183 | let track = factory.create_video_track(label, source.clone()); |
| 184 | Outgoing { |
| 185 | source, |
| 186 | track, |
| 187 | capture: None, |
| 188 | frame_size: Arc::new(AtomicU64::new(0)), |
| 189 | requested: None, |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /// Camera and screen share of the local user; frames come from devices or a generated pattern. |
| 195 | pub struct LocalVideo { |
| 196 | mode: VideoMode, |
| 197 | hub: Arc<VideoHub>, |
| 198 | updates: UnboundedSender<CallUpdate>, |
| 199 | commands: Option<UnboundedSender<CallCommand>>, |
| 200 | camera_generation: Arc<AtomicU64>, |
| 201 | share_generation: Arc<AtomicU64>, |
| 202 | camera: Outgoing, |
| 203 | screen: Outgoing, |
| 204 | lines: Option<VideoLines>, |
| 205 | blur: Arc<BlurSettings>, |
| 206 | motion: bool, |
| 207 | share_fps: Arc<AtomicU32>, |
| 208 | camera_preview: bool, |
| 209 | quality: VideoQuality, |
| 210 | camera_stats: Arc<CameraCaptureStats>, |
| 211 | timeline: Option<Timeline>, |
| 212 | portal_generation: u64, |
| 213 | portal_armed: bool, |
| 214 | portal_keep: bool, |
| 215 | /// The desktop dialog already bound the PipeWire stream. Later `StartShare` |
| 216 | /// commands must attach that capturer, not replace it. |
| 217 | portal_bound: bool, |
| 218 | camera_bitrate_cap: Option<u64>, |
| 219 | } |
| 220 | |
| 221 | impl LocalVideo { |
| 222 | pub fn new( |
| 223 | factory: &PeerConnectionFactory, |
| 224 | mode: VideoMode, |
| 225 | hub: Arc<VideoHub>, |
| 226 | updates: UnboundedSender<CallUpdate>, |
| 227 | ) -> Self { |
| 228 | LocalVideo { |
| 229 | mode, |
| 230 | hub, |
| 231 | updates, |
| 232 | commands: None, |
| 233 | camera_generation: Arc::new(AtomicU64::new(0)), |
| 234 | share_generation: Arc::new(AtomicU64::new(0)), |
| 235 | camera: Outgoing::new(factory, CAMERA_LABEL, PatternKind::Camera, false), |
| 236 | screen: Outgoing::new(factory, SCREEN_LABEL, PatternKind::Screen, true), |
| 237 | lines: None, |
| 238 | blur: Arc::new(BlurSettings::default()), |
| 239 | motion: false, |
| 240 | share_fps: Arc::new(AtomicU32::new(SCREEN_SHARE_FPS)), |
| 241 | camera_preview: false, |
| 242 | quality: VideoQuality::default(), |
| 243 | camera_stats: CameraCaptureStats::new(), |
| 244 | timeline: None, |
| 245 | portal_generation: 0, |
| 246 | portal_armed: false, |
| 247 | portal_keep: false, |
| 248 | portal_bound: false, |
| 249 | camera_bitrate_cap: None, |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | pub fn attach_commands(&mut self, commands: UnboundedSender<CallCommand>) { |
| 254 | self.commands = Some(commands); |
| 255 | } |
| 256 | |
| 257 | pub fn attach_timeline(&mut self, timeline: Timeline) { |
| 258 | self.timeline = Some(timeline); |
| 259 | } |
| 260 | |
| 261 | pub fn video_quality(&self) -> VideoQuality { |
| 262 | self.quality |
| 263 | } |
| 264 | |
| 265 | pub fn set_video_quality(&mut self, quality: VideoQuality) { |
| 266 | self.quality = quality; |
| 267 | } |
| 268 | |
| 269 | pub fn set_motion(&mut self, motion: bool) { |
| 270 | self.motion = motion; |
| 271 | self.share_fps.store( |
| 272 | if motion { |
| 273 | MOTION_SHARE_FPS |
| 274 | } else { |
| 275 | SCREEN_SHARE_FPS |
| 276 | }, |
| 277 | Ordering::Relaxed, |
| 278 | ); |
| 279 | if let Some(share) = self |
| 280 | .lines |
| 281 | .as_ref() |
| 282 | .and_then(|lines| lines.share.as_ref()) |
| 283 | .filter(|_| self.sharing()) |
| 284 | { |
| 285 | let _ = self.tune_share(&share.sender()); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | pub fn set_standout(&self, standout: bool) { |
| 290 | self.blur.set_standout(standout); |
| 291 | } |
| 292 | |
| 293 | fn tune_share(&self, sender: &RtpSender) -> Result<()> { |
| 294 | let mut parameters = sender.parameters(); |
| 295 | parameters.set_degradation_preference(if self.motion { |
| 296 | DegradationPreference::MaintainFramerate |
| 297 | } else { |
| 298 | DegradationPreference::MaintainResolution |
| 299 | }); |
| 300 | let fps = f64::from(self.share_fps.load(Ordering::Relaxed)); |
| 301 | for encoding in &mut parameters.encodings { |
| 302 | encoding.max_framerate = Some(fps); |
| 303 | } |
| 304 | sender |
| 305 | .set_parameters(parameters) |
| 306 | .map_err(|error| Error::Webrtc(error.to_string())) |
| 307 | } |
| 308 | |
| 309 | fn outgoing_mut(&mut self, source: SendSource) -> &mut Outgoing { |
| 310 | match source { |
| 311 | SendSource::Camera => &mut self.camera, |
| 312 | SendSource::Screen => &mut self.screen, |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | fn sender_of(&self, source: SendSource) -> Option<RtpSender> { |
| 317 | let lines = self.lines.as_ref()?; |
| 318 | match source { |
| 319 | SendSource::Camera => Some(lines.camera.sender()), |
| 320 | SendSource::Screen => lines.share.as_ref().map(|share| share.sender()), |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | pub fn apply_send_limit( |
| 325 | &mut self, |
| 326 | source: SendSource, |
| 327 | limits: &[SendLimit], |
| 328 | ) -> Result<Option<Vec<LayerPlan>>> { |
| 329 | let sending = match source { |
| 330 | SendSource::Camera => self.camera_on(), |
| 331 | SendSource::Screen => self.sharing(), |
| 332 | }; |
| 333 | let Some(sender) = self.sender_of(source).filter(|_| sending) else { |
| 334 | // The controller often sends the limit before the source exists. |
| 335 | // Keep it. A repeat of the same limit is dropped by the filter, |
| 336 | // and refresh applies this copy once sending and a frame size exist. |
| 337 | self.outgoing_mut(source).requested = Some(RequestedLimits { |
| 338 | limits: limits.to_vec(), |
| 339 | applied_for: None, |
| 340 | }); |
| 341 | return Ok(None); |
| 342 | }; |
| 343 | let outgoing = match source { |
| 344 | SendSource::Camera => &mut self.camera, |
| 345 | SendSource::Screen => &mut self.screen, |
| 346 | }; |
| 347 | let frame = unpack_size(outgoing.frame_size.load(Ordering::Relaxed)); |
| 348 | outgoing.requested = Some(RequestedLimits { |
| 349 | limits: limits.to_vec(), |
| 350 | applied_for: frame, |
| 351 | }); |
| 352 | let rids: Vec<String> = sender |
| 353 | .parameters() |
| 354 | .encodings |
| 355 | .into_iter() |
| 356 | .map(|encoding| encoding.rid) |
| 357 | .collect(); |
| 358 | if rids.len() > 1 { |
| 359 | let mut plans = plan_layers(limits, frame, &rids); |
| 360 | if source == SendSource::Camera { |
| 361 | Self::apply_bitrate_cap_to_plans(&mut plans, self.camera_bitrate_cap); |
| 362 | } |
| 363 | if plans.iter().any(|plan| plan.keyframe) { |
| 364 | Self::set_layers(&sender, &plans, KEYFRAME_NUDGE)?; |
| 365 | } |
| 366 | Self::set_layers(&sender, &plans, 1.0)?; |
| 367 | return Ok(Some(plans)); |
| 368 | } |
| 369 | let Some(limit) = limits.first() else { |
| 370 | return Ok(None); |
| 371 | }; |
| 372 | let mut encoder = encoder_limits(limit, frame); |
| 373 | if source == SendSource::Camera |
| 374 | && let Some(cap) = self.camera_bitrate_cap |
| 375 | { |
| 376 | encoder.max_bitrate = Some(encoder.max_bitrate.map_or(cap, |max| max.min(cap))); |
| 377 | } |
| 378 | if limit.keyframe { |
| 379 | Self::set_limits(&sender, &encoder, KEYFRAME_NUDGE)?; |
| 380 | } |
| 381 | Self::set_limits(&sender, &encoder, 1.0)?; |
| 382 | Ok(Some(vec![LayerPlan { |
| 383 | rid: rids.into_iter().next().unwrap_or_default(), |
| 384 | active: true, |
| 385 | limits: Some(encoder), |
| 386 | keyframe: limit.keyframe, |
| 387 | }])) |
| 388 | } |
| 389 | |
| 390 | fn apply_bitrate_cap_to_plans(plans: &mut [LayerPlan], cap: Option<u64>) { |
| 391 | let Some(cap) = cap else { return }; |
| 392 | for plan in plans { |
| 393 | if let Some(limits) = plan.limits.as_mut() { |
| 394 | limits.max_bitrate = Some(limits.max_bitrate.map_or(cap, |max| max.min(cap))); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /// The mixer sends its limits once, often before the first frame tells the size to scale from. |
| 400 | pub fn refresh_send_limits(&mut self) -> Vec<(SendSource, Result<Option<Vec<LayerPlan>>>)> { |
| 401 | let mut refreshed = Vec::new(); |
| 402 | for source in [SendSource::Camera, SendSource::Screen] { |
| 403 | let outgoing = match source { |
| 404 | SendSource::Camera => &self.camera, |
| 405 | SendSource::Screen => &self.screen, |
| 406 | }; |
| 407 | let frame = unpack_size(outgoing.frame_size.load(Ordering::Relaxed)); |
| 408 | let Some(requested) = outgoing |
| 409 | .requested |
| 410 | .as_ref() |
| 411 | .filter(|requested| frame.is_some() && requested.applied_for != frame) |
| 412 | else { |
| 413 | continue; |
| 414 | }; |
| 415 | let limits = requested.limits.clone(); |
| 416 | refreshed.push((source, self.apply_send_limit(source, &limits))); |
| 417 | } |
| 418 | refreshed |
| 419 | } |
| 420 | |
| 421 | /// Applies a limit that was waiting for this source and writes it on the call report. |
| 422 | /// The drive loop only records a refresh it performs itself. |
| 423 | fn record_refreshed_send_limits(&mut self) { |
| 424 | let Some(timeline) = self.timeline.clone() else { |
| 425 | self.refresh_send_limits(); |
| 426 | return; |
| 427 | }; |
| 428 | for (source, refreshed) in self.refresh_send_limits() { |
| 429 | match refreshed { |
| 430 | Ok(Some(plans)) => { |
| 431 | timeline.record("send limits", format!("{source:?} {plans:?}")); |
| 432 | } |
| 433 | Ok(None) => {} |
| 434 | Err(error) => timeline.record("send limits failed", error.to_string()), |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | fn set_layers(sender: &RtpSender, plans: &[LayerPlan], nudge: f64) -> Result<()> { |
| 440 | let mut parameters = sender.parameters(); |
| 441 | for encoding in &mut parameters.encodings { |
| 442 | let Some(plan) = plans.iter().find(|plan| plan.rid == encoding.rid) else { |
| 443 | continue; |
| 444 | }; |
| 445 | let nudged = nudge != 1.0 && plan.keyframe; |
| 446 | encoding.active = plan.active; |
| 447 | let Some(limits) = &plan.limits else { continue }; |
| 448 | encoding.scale_resolution_down_by = |
| 449 | Some(limits.scale_down_by * if nudged { nudge } else { 1.0 }); |
| 450 | if let Some(framerate) = limits.max_framerate { |
| 451 | encoding.max_framerate = Some(framerate); |
| 452 | } |
| 453 | encoding.max_bitrate = limits.max_bitrate; |
| 454 | } |
| 455 | sender |
| 456 | .set_parameters(parameters) |
| 457 | .map_err(|error| Error::Webrtc(error.to_string())) |
| 458 | } |
| 459 | |
| 460 | fn set_limits(sender: &RtpSender, limits: &EncoderLimits, nudge: f64) -> Result<()> { |
| 461 | let mut parameters = sender.parameters(); |
| 462 | for encoding in &mut parameters.encodings { |
| 463 | encoding.scale_resolution_down_by = Some(limits.scale_down_by * nudge); |
| 464 | if let Some(framerate) = limits.max_framerate { |
| 465 | encoding.max_framerate = Some(framerate); |
| 466 | } |
| 467 | encoding.max_bitrate = limits.max_bitrate; |
| 468 | } |
| 469 | sender |
| 470 | .set_parameters(parameters) |
| 471 | .map_err(|error| Error::Webrtc(error.to_string())) |
| 472 | } |
| 473 | |
| 474 | pub fn camera_size(&self) -> Option<(u32, u32)> { |
| 475 | unpack_size(self.camera.frame_size.load(Ordering::Relaxed)).filter(|_| self.camera_on()) |
| 476 | } |
| 477 | |
| 478 | pub fn set_background(&self, enabled: bool, picture: Option<Arc<BackgroundPicture>>) { |
| 479 | self.blur.set_picture(picture); |
| 480 | self.blur.set_enabled(enabled); |
| 481 | } |
| 482 | |
| 483 | pub fn set_effects(&self, effects: CameraEffects) { |
| 484 | self.blur.set_effects(effects); |
| 485 | } |
| 486 | |
| 487 | pub fn camera_track(&self) -> RtcVideoTrack { |
| 488 | self.camera.track.clone() |
| 489 | } |
| 490 | |
| 491 | pub fn share_track(&self) -> RtcVideoTrack { |
| 492 | self.screen.track.clone() |
| 493 | } |
| 494 | |
| 495 | pub fn take_effects_failure(&self) -> bool { |
| 496 | self.blur.take_unavailable() |
| 497 | } |
| 498 | |
| 499 | pub fn blur_average_ms(&self) -> Option<f32> { |
| 500 | (self.camera_on() && self.blur.active()).then(|| self.blur.average_ms()) |
| 501 | } |
| 502 | |
| 503 | pub fn blur_model(&self) -> Option<&'static str> { |
| 504 | (self.camera_on() && self.blur.active()) |
| 505 | .then(|| self.blur.model()) |
| 506 | .filter(|model| !model.is_empty()) |
| 507 | } |
| 508 | |
| 509 | pub fn effects_label(&self) -> String { |
| 510 | let effects = self.blur.effects(); |
| 511 | let flag = |on: bool| if on { "on" } else { "off" }; |
| 512 | format!( |
| 513 | "blur={} standout={} framing={} brightness={} soft_focus={}", |
| 514 | flag(self.blur.enabled()), |
| 515 | flag(self.blur.standout()), |
| 516 | flag(effects.auto_framing), |
| 517 | flag(effects.adjust_brightness), |
| 518 | flag(effects.soft_focus) |
| 519 | ) |
| 520 | } |
| 521 | |
| 522 | pub fn has_lines(&self) -> bool { |
| 523 | self.lines.is_some() |
| 524 | } |
| 525 | |
| 526 | pub fn clear_lines(&mut self) { |
| 527 | if let Some(lines) = self.lines.take() { |
| 528 | let _ = lines.camera.sender().set_track(None); |
| 529 | let _ = Self::set_active(&lines.camera.sender(), false); |
| 530 | if let Some(share) = lines.share { |
| 531 | let _ = share.sender().set_track(None); |
| 532 | let _ = Self::set_active(&share.sender(), false); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | pub fn camera_mid(&self) -> Option<String> { |
| 538 | self.lines.as_ref().and_then(|lines| lines.camera.mid()) |
| 539 | } |
| 540 | |
| 541 | pub fn share_mid(&self) -> Option<String> { |
| 542 | self.lines |
| 543 | .as_ref() |
| 544 | .and_then(|lines| lines.share.as_ref()) |
| 545 | .and_then(|share| share.mid()) |
| 546 | } |
| 547 | |
| 548 | pub fn has_share_line(&self) -> bool { |
| 549 | self.lines |
| 550 | .as_ref() |
| 551 | .is_some_and(|lines| lines.share.is_some()) |
| 552 | } |
| 553 | |
| 554 | pub fn add_share_line( |
| 555 | &mut self, |
| 556 | share: RtpTransceiver, |
| 557 | camera: Option<RtpTransceiver>, |
| 558 | ) -> Result<()> { |
| 559 | match (self.lines.as_mut(), camera) { |
| 560 | (Some(lines), _) => lines.share = Some(share), |
| 561 | (None, Some(camera)) => { |
| 562 | self.lines = Some(VideoLines { |
| 563 | camera, |
| 564 | share: Some(share), |
| 565 | }) |
| 566 | } |
| 567 | (None, None) => return Err(Error::Webrtc("no video line negotiated".into())), |
| 568 | } |
| 569 | Ok(()) |
| 570 | } |
| 571 | |
| 572 | pub fn share_sending(&self) -> bool { |
| 573 | self.lines |
| 574 | .as_ref() |
| 575 | .and_then(|lines| lines.share.as_ref()) |
| 576 | .and_then(|share| share.current_direction()) |
| 577 | .is_some_and(|direction| { |
| 578 | matches!( |
| 579 | direction, |
| 580 | RtpTransceiverDirection::SendOnly | RtpTransceiverDirection::SendRecv |
| 581 | ) |
| 582 | }) |
| 583 | } |
| 584 | |
| 585 | pub fn is_available(&self) -> bool { |
| 586 | self.mode != VideoMode::Off |
| 587 | } |
| 588 | |
| 589 | pub fn camera_on(&self) -> bool { |
| 590 | self.camera.capture.is_some() && !self.camera_preview |
| 591 | } |
| 592 | |
| 593 | pub fn camera_previewing(&self) -> bool { |
| 594 | self.camera.capture.is_some() && self.camera_preview |
| 595 | } |
| 596 | |
| 597 | pub fn sharing(&self) -> bool { |
| 598 | self.screen.capture.is_some() |
| 599 | } |
| 600 | |
| 601 | pub fn portal_is_open(&self) -> bool { |
| 602 | self.portal_armed |
| 603 | } |
| 604 | |
| 605 | pub fn set_lines(&mut self, lines: VideoLines) -> Result<()> { |
| 606 | self.clear_lines(); |
| 607 | if self.camera_on() { |
| 608 | Self::begin_sending_on(&lines.camera, &self.camera.track)?; |
| 609 | } |
| 610 | if self.sharing() |
| 611 | && let Some(share) = &lines.share |
| 612 | { |
| 613 | Self::begin_sending_on(share, &self.screen.track)?; |
| 614 | } |
| 615 | self.lines = Some(lines); |
| 616 | Ok(()) |
| 617 | } |
| 618 | |
| 619 | fn ensure_send_direction(transceiver: &RtpTransceiver) -> Result<()> { |
| 620 | match transceiver.direction() { |
| 621 | RtpTransceiverDirection::SendRecv | RtpTransceiverDirection::SendOnly => Ok(()), |
| 622 | RtpTransceiverDirection::RecvOnly | RtpTransceiverDirection::Inactive => transceiver |
| 623 | .set_direction(RtpTransceiverDirection::SendRecv) |
| 624 | .map_err(|error| Error::Webrtc(error.to_string())), |
| 625 | RtpTransceiverDirection::Stopped => Err(Error::Webrtc("video line is stopped".into())), |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | fn begin_sending_on(transceiver: &RtpTransceiver, track: &RtcVideoTrack) -> Result<()> { |
| 630 | Self::ensure_send_direction(transceiver)?; |
| 631 | Self::begin_sending(&transceiver.sender(), track) |
| 632 | } |
| 633 | |
| 634 | fn begin_sending(sender: &RtpSender, track: &RtcVideoTrack) -> Result<()> { |
| 635 | sender |
| 636 | .set_track(Some(MediaStreamTrack::Video(track.clone()))) |
| 637 | .map_err(|error| Error::Webrtc(error.to_string()))?; |
| 638 | // Prefer dropping resolution over framerate when the uplink is tight. |
| 639 | let mut parameters = sender.parameters(); |
| 640 | parameters.set_degradation_preference(DegradationPreference::MaintainFramerate); |
| 641 | sender |
| 642 | .set_parameters(parameters) |
| 643 | .map_err(|error| Error::Webrtc(error.to_string()))?; |
| 644 | Self::set_active(sender, true) |
| 645 | } |
| 646 | |
| 647 | pub fn cap_camera_bitrate(&mut self, cap: Option<u64>) -> Result<()> { |
| 648 | if self.camera_bitrate_cap == cap { |
| 649 | return Ok(()); |
| 650 | } |
| 651 | self.camera_bitrate_cap = cap; |
| 652 | if let Some(limits) = self |
| 653 | .camera |
| 654 | .requested |
| 655 | .as_ref() |
| 656 | .map(|requested| requested.limits.clone()) |
| 657 | { |
| 658 | let _ = self.apply_send_limit(SendSource::Camera, &limits)?; |
| 659 | return Ok(()); |
| 660 | } |
| 661 | let Some(sender) = self |
| 662 | .sender_of(SendSource::Camera) |
| 663 | .filter(|_| self.camera_on()) |
| 664 | else { |
| 665 | return Ok(()); |
| 666 | }; |
| 667 | let mut parameters = sender.parameters(); |
| 668 | for encoding in &mut parameters.encodings { |
| 669 | encoding.max_bitrate = match (encoding.max_bitrate, cap) { |
| 670 | (Some(max), Some(cap)) => Some(max.min(cap)), |
| 671 | (None, Some(cap)) => Some(cap), |
| 672 | (max, None) => max, |
| 673 | }; |
| 674 | } |
| 675 | sender |
| 676 | .set_parameters(parameters) |
| 677 | .map_err(|error| Error::Webrtc(error.to_string())) |
| 678 | } |
| 679 | |
| 680 | pub(crate) fn set_sender_active(sender: &RtpSender, active: bool) -> Result<()> { |
| 681 | let mut parameters = sender.parameters(); |
| 682 | for encoding in &mut parameters.encodings { |
| 683 | encoding.active = active; |
| 684 | } |
| 685 | sender |
| 686 | .set_parameters(parameters) |
| 687 | .map_err(|error| Error::Webrtc(error.to_string())) |
| 688 | } |
| 689 | |
| 690 | fn set_active(sender: &RtpSender, active: bool) -> Result<()> { |
| 691 | Self::set_sender_active(sender, active) |
| 692 | } |
| 693 | |
| 694 | fn camera_sink(&self) -> LocalSink { |
| 695 | LocalSink::new( |
| 696 | self.camera.source.clone(), |
| 697 | Some((self.hub.clone(), VideoKey::LocalCamera)), |
| 698 | ) |
| 699 | .mirrored_by(self.blur.clone()) |
| 700 | .reporting_size(self.camera.frame_size.clone()) |
| 701 | } |
| 702 | |
| 703 | /// Local share frames go to the encoder and to the window in the chat. |
| 704 | fn screen_sink(&self) -> LocalSink { |
| 705 | LocalSink::new( |
| 706 | self.screen.source.clone(), |
| 707 | Some((self.hub.clone(), VideoKey::LocalScreen)), |
| 708 | ) |
| 709 | .with_standout(self.blur.clone()) |
| 710 | .reporting_size(self.screen.frame_size.clone()) |
| 711 | } |
| 712 | |
| 713 | /// A capture thread that was replaced meanwhile must not switch off its successor. |
| 714 | fn report_failure( |
| 715 | &self, |
| 716 | notice_key: &'static str, |
| 717 | generation: &Arc<AtomicU64>, |
| 718 | switch_off: CallCommand, |
| 719 | ) -> impl FnOnce(String) + Send + 'static { |
| 720 | let report = self.report_end(notice_key, generation, switch_off); |
| 721 | move |reason| report(Some(reason)) |
| 722 | } |
| 723 | |
| 724 | /// Like `report_failure`, but `None` switches off without a notice: the |
| 725 | /// user ended it outside the app. |
| 726 | fn report_end( |
| 727 | &self, |
| 728 | notice_key: &'static str, |
| 729 | generation: &Arc<AtomicU64>, |
| 730 | switch_off: CallCommand, |
| 731 | ) -> impl FnOnce(Option<String>) + Send + 'static { |
| 732 | let updates = self.updates.clone(); |
| 733 | let commands = self.commands.clone(); |
| 734 | let started = generation.fetch_add(1, Ordering::SeqCst) + 1; |
| 735 | let generation = generation.clone(); |
| 736 | move |reason| { |
| 737 | if let Some(reason) = reason { |
| 738 | let _ = updates.send(CallUpdate::Notice(notice_with_detail(notice_key, reason))); |
| 739 | } |
| 740 | if generation.load(Ordering::SeqCst) == started |
| 741 | && let Some(commands) = commands |
| 742 | { |
| 743 | let _ = commands.send(switch_off); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | pub fn preview_camera(&mut self, choice: &DeviceChoice) -> Result<()> { |
| 749 | if !self.is_available() { |
| 750 | return Err(Error::Webrtc("video is switched off".into())); |
| 751 | } |
| 752 | self.camera.capture = None; |
| 753 | self.camera.capture = Some(self.capture_camera(choice)?); |
| 754 | self.camera_preview = true; |
| 755 | Ok(()) |
| 756 | } |
| 757 | |
| 758 | pub fn start_camera(&mut self, choice: &DeviceChoice) -> Result<()> { |
| 759 | let lines = self |
| 760 | .lines |
| 761 | .as_ref() |
| 762 | .ok_or_else(|| Error::Webrtc("no video line negotiated".into()))?; |
| 763 | if !self.is_available() { |
| 764 | return Err(Error::Webrtc("video is switched off".into())); |
| 765 | } |
| 766 | if !self.camera_previewing() { |
| 767 | self.camera.capture = None; |
| 768 | self.camera.capture = Some(self.capture_camera(choice)?); |
| 769 | } |
| 770 | Self::begin_sending_on(&lines.camera, &self.camera.track)?; |
| 771 | self.camera_preview = false; |
| 772 | self.record_refreshed_send_limits(); |
| 773 | Ok(()) |
| 774 | } |
| 775 | |
| 776 | pub fn camera_capture_summary(&self) -> Option<String> { |
| 777 | self.camera_stats.summary() |
| 778 | } |
| 779 | |
| 780 | fn capture_camera(&self, choice: &DeviceChoice) -> Result<Capture> { |
| 781 | let mut sink = self.camera_sink(); |
| 782 | Ok(match self.mode { |
| 783 | VideoMode::Pattern => Capture::Task(spawn_pattern( |
| 784 | sink, |
| 785 | PatternKind::Camera, |
| 786 | Some(self.blur.clone()), |
| 787 | )), |
| 788 | _ => { |
| 789 | let timeline = self.timeline.clone(); |
| 790 | start_camera( |
| 791 | choice, |
| 792 | self.quality, |
| 793 | self.blur.clone(), |
| 794 | self.camera_stats.clone(), |
| 795 | move |label| { |
| 796 | if let Some(timeline) = timeline { |
| 797 | timeline.record("camera capture", label); |
| 798 | } else { |
| 799 | eprintln!("calls: camera capture {label}"); |
| 800 | } |
| 801 | }, |
| 802 | move |buffer| sink.push(buffer), |
| 803 | self.report_failure( |
| 804 | CAMERA_STOPPED_NOTICE, |
| 805 | &self.camera_generation, |
| 806 | CallCommand::SetCamera(false), |
| 807 | ), |
| 808 | )? |
| 809 | } |
| 810 | }) |
| 811 | } |
| 812 | |
| 813 | /// Restart capture with the current quality if the camera is already running. |
| 814 | pub fn refresh_camera_capture(&mut self, choice: &DeviceChoice) -> Result<()> { |
| 815 | if self.camera.capture.is_none() { |
| 816 | return Ok(()); |
| 817 | } |
| 818 | let preview = self.camera_preview; |
| 819 | self.camera.capture = None; |
| 820 | self.camera.capture = Some(self.capture_camera(choice)?); |
| 821 | self.camera_preview = preview; |
| 822 | if !preview { |
| 823 | if let Some(lines) = self.lines.as_ref() { |
| 824 | Self::begin_sending_on(&lines.camera, &self.camera.track)?; |
| 825 | } |
| 826 | } |
| 827 | Ok(()) |
| 828 | } |
| 829 | |
| 830 | pub fn stop_camera(&mut self) { |
| 831 | self.camera_generation.fetch_add(1, Ordering::SeqCst); |
| 832 | self.camera_preview = false; |
| 833 | self.camera.capture = None; |
| 834 | self.blur.set_cutout(None); |
| 835 | if let Some(lines) = &self.lines { |
| 836 | let _ = Self::set_active(&lines.camera.sender(), false); |
| 837 | } |
| 838 | self.hub.forget(&VideoKey::LocalCamera); |
| 839 | } |
| 840 | |
| 841 | pub fn accept_portal(&mut self, generation: u64) -> bool { |
| 842 | if self.portal_armed && self.portal_generation == generation { |
| 843 | self.portal_armed = false; |
| 844 | self.portal_keep = true; |
| 845 | self.portal_bound = true; |
| 846 | true |
| 847 | } else { |
| 848 | false |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | pub fn open_portal( |
| 853 | &mut self, |
| 854 | kind: ShareKind, |
| 855 | commands: UnboundedSender<CallCommand>, |
| 856 | ) -> Result<()> { |
| 857 | if !self.is_available() { |
| 858 | return Err(Error::Webrtc("video is switched off".into())); |
| 859 | } |
| 860 | if self.mode == VideoMode::Pattern { |
| 861 | let source = ShareSource { |
| 862 | id: if kind == ShareKind::Screen { 1 } else { 2 }, |
| 863 | kind, |
| 864 | title: if kind == ShareKind::Screen { |
| 865 | "Test screen" |
| 866 | } else { |
| 867 | "Test window" |
| 868 | } |
| 869 | .into(), |
| 870 | }; |
| 871 | let _ = commands.send(CallCommand::StartShare(source)); |
| 872 | return Ok(()); |
| 873 | } |
| 874 | self.portal_generation = self.portal_generation.wrapping_add(1); |
| 875 | let generation = self.portal_generation; |
| 876 | self.portal_armed = false; |
| 877 | self.portal_keep = false; |
| 878 | self.portal_bound = false; |
| 879 | self.screen.capture = None; |
| 880 | let mut sink = self.screen_sink(); |
| 881 | let updates = self.updates.clone(); |
| 882 | // `stop_share` moves the generation on, so only an end the app did not |
| 883 | // ask for switches the share off. |
| 884 | let ended = self.report_end( |
| 885 | SCREEN_SHARE_STOPPED_NOTICE, |
| 886 | &self.share_generation, |
| 887 | CallCommand::StopShare, |
| 888 | ); |
| 889 | let capture = start_portal_capture( |
| 890 | kind, |
| 891 | self.share_fps.clone(), |
| 892 | move |buffer| sink.push(buffer), |
| 893 | move |follow| match follow { |
| 894 | PortalFollowUp::Share(source) => { |
| 895 | let _ = commands.send(CallCommand::FinishPortalShare { source, generation }); |
| 896 | } |
| 897 | PortalFollowUp::Quiet => { |
| 898 | let _ = commands.send(CallCommand::AbandonPortalShare); |
| 899 | } |
| 900 | PortalFollowUp::Notice(text) => { |
| 901 | let _ = updates.send(CallUpdate::Notice(text.to_owned())); |
| 902 | let _ = commands.send(CallCommand::AbandonPortalShare); |
| 903 | } |
| 904 | }, |
| 905 | ended, |
| 906 | )?; |
| 907 | self.portal_armed = true; |
| 908 | self.screen.capture = Some(capture); |
| 909 | Ok(()) |
| 910 | } |
| 911 | |
| 912 | pub fn start_share(&mut self, source: &ShareSource) -> Result<()> { |
| 913 | let lines = self |
| 914 | .lines |
| 915 | .as_ref() |
| 916 | .ok_or_else(|| Error::Webrtc("no video line negotiated".into()))?; |
| 917 | let share = lines |
| 918 | .share |
| 919 | .as_ref() |
| 920 | .ok_or_else(|| Error::Webrtc("sharing needs a meeting".into()))?; |
| 921 | if !self.is_available() { |
| 922 | return Err(Error::Webrtc("video is switched off".into())); |
| 923 | } |
| 924 | let keep = std::mem::take(&mut self.portal_keep) || self.portal_bound; |
| 925 | if !keep { |
| 926 | self.portal_armed = false; |
| 927 | self.portal_generation = self.portal_generation.wrapping_add(1); |
| 928 | self.screen.capture = None; |
| 929 | let mut sink = self.screen_sink(); |
| 930 | let capture = match self.mode { |
| 931 | VideoMode::Pattern => Capture::Task(spawn_pattern(sink, PatternKind::Screen, None)), |
| 932 | _ => start_screen_capture( |
| 933 | source.clone(), |
| 934 | self.share_fps.clone(), |
| 935 | move |buffer| sink.push(buffer), |
| 936 | self.report_end( |
| 937 | SCREEN_SHARE_STOPPED_NOTICE, |
| 938 | &self.share_generation, |
| 939 | CallCommand::StopShare, |
| 940 | ), |
| 941 | )?, |
| 942 | }; |
| 943 | self.screen.capture = Some(capture); |
| 944 | } |
| 945 | Self::begin_sending_on(&share, &self.screen.track)?; |
| 946 | let _ = self.tune_share(&share.sender()); |
| 947 | self.record_refreshed_send_limits(); |
| 948 | Ok(()) |
| 949 | } |
| 950 | |
| 951 | pub fn stop_share(&mut self) { |
| 952 | self.share_generation.fetch_add(1, Ordering::SeqCst); |
| 953 | self.portal_generation = self.portal_generation.wrapping_add(1); |
| 954 | self.portal_armed = false; |
| 955 | self.portal_keep = false; |
| 956 | self.portal_bound = false; |
| 957 | self.screen.capture = None; |
| 958 | self.hub.forget(&VideoKey::LocalScreen); |
| 959 | if let Some(share) = self.lines.as_ref().and_then(|lines| lines.share.as_ref()) { |
| 960 | let _ = Self::set_active(&share.sender(), false); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | pub fn stop_all(&mut self) { |
| 965 | self.stop_camera(); |
| 966 | self.stop_share(); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | #[cfg(test)] |
| 971 | mod tests { |
| 972 | use super::*; |
| 973 | use libwebrtc::MediaType; |
| 974 | use libwebrtc::peer_connection_factory::RtcConfiguration; |
| 975 | use libwebrtc::rtp_transceiver::{RtpTransceiverDirection, RtpTransceiverInit}; |
| 976 | |
| 977 | #[tokio::test] |
| 978 | async fn a_camera_preview_runs_without_a_line_and_stops_cleanly() { |
| 979 | let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel(); |
| 980 | let factory = crate::devices::shared_factory(); |
| 981 | let mut video = LocalVideo::new( |
| 982 | &factory, |
| 983 | VideoMode::Pattern, |
| 984 | VideoHub::new(sender.clone()), |
| 985 | sender, |
| 986 | ); |
| 987 | video.preview_camera(&DeviceChoice::SystemDefault).unwrap(); |
| 988 | assert!(video.camera_previewing() && !video.camera_on()); |
| 989 | assert!( |
| 990 | video.start_camera(&DeviceChoice::SystemDefault).is_err(), |
| 991 | "sending needs a negotiated line" |
| 992 | ); |
| 993 | assert!(video.camera_previewing()); |
| 994 | video.stop_camera(); |
| 995 | assert!(!video.camera_previewing() && !video.camera_on()); |
| 996 | } |
| 997 | |
| 998 | #[tokio::test] |
| 999 | async fn a_camera_limit_that_arrived_while_off_is_applied_when_the_camera_starts() { |
| 1000 | let factory = crate::devices::shared_factory(); |
| 1001 | let peer = factory |
| 1002 | .create_peer_connection(RtcConfiguration::default()) |
| 1003 | .expect("peer"); |
| 1004 | let camera = peer |
| 1005 | .add_transceiver_for_media( |
| 1006 | MediaType::Video, |
| 1007 | RtpTransceiverInit { |
| 1008 | direction: RtpTransceiverDirection::SendRecv, |
| 1009 | stream_ids: vec!["camera".into()], |
| 1010 | send_encodings: Vec::new(), |
| 1011 | }, |
| 1012 | ) |
| 1013 | .expect("transceiver"); |
| 1014 | let (updates, _) = tokio::sync::mpsc::unbounded_channel(); |
| 1015 | let mut video = LocalVideo::new( |
| 1016 | &factory, |
| 1017 | VideoMode::Pattern, |
| 1018 | VideoHub::new(updates.clone()), |
| 1019 | updates, |
| 1020 | ); |
| 1021 | video |
| 1022 | .set_lines(VideoLines { |
| 1023 | camera, |
| 1024 | share: None, |
| 1025 | }) |
| 1026 | .expect("lines"); |
| 1027 | let limit = |bitrate: u64| SendLimit { |
| 1028 | max_fs: Some(920), |
| 1029 | max_mbps: Some(13_800), |
| 1030 | max_fps: Some(15.0), |
| 1031 | max_bitrate: Some(bitrate), |
| 1032 | keyframe: false, |
| 1033 | rid: None, |
| 1034 | ssrc: None, |
| 1035 | }; |
| 1036 | assert!( |
| 1037 | video |
| 1038 | .apply_send_limit(SendSource::Camera, &[limit(600_000)]) |
| 1039 | .unwrap() |
| 1040 | .is_none() |
| 1041 | ); |
| 1042 | assert!( |
| 1043 | video |
| 1044 | .apply_send_limit(SendSource::Camera, &[limit(100_000)]) |
| 1045 | .unwrap() |
| 1046 | .is_none() |
| 1047 | ); |
| 1048 | video |
| 1049 | .start_camera(&DeviceChoice::SystemDefault) |
| 1050 | .expect("camera"); |
| 1051 | tokio::time::sleep(std::time::Duration::from_millis(200)).await; |
| 1052 | let refreshed = video.refresh_send_limits(); |
| 1053 | let bitrate = refreshed.iter().find_map(|(source, applied)| { |
| 1054 | (*source == SendSource::Camera) |
| 1055 | .then_some(applied) |
| 1056 | .and_then(|applied| { |
| 1057 | applied.as_ref().ok().and_then(|plans| { |
| 1058 | plans.as_ref().and_then(|plans| { |
| 1059 | plans.first().and_then(|plan| { |
| 1060 | plan.limits.as_ref().and_then(|limits| limits.max_bitrate) |
| 1061 | }) |
| 1062 | }) |
| 1063 | }) |
| 1064 | }) |
| 1065 | }); |
| 1066 | assert_eq!(bitrate, Some(100_000)); |
| 1067 | } |
| 1068 | |
| 1069 | #[tokio::test] |
| 1070 | async fn clearing_a_sender_track_does_not_crash() { |
| 1071 | let factory = crate::devices::shared_factory(); |
| 1072 | let peer = factory |
| 1073 | .create_peer_connection(RtcConfiguration::default()) |
| 1074 | .expect("peer"); |
| 1075 | let transceiver = peer |
| 1076 | .add_transceiver_for_media( |
| 1077 | MediaType::Video, |
| 1078 | RtpTransceiverInit { |
| 1079 | direction: RtpTransceiverDirection::SendRecv, |
| 1080 | stream_ids: vec!["camera".into()], |
| 1081 | send_encodings: Vec::new(), |
| 1082 | }, |
| 1083 | ) |
| 1084 | .expect("transceiver"); |
| 1085 | let (updates, _) = tokio::sync::mpsc::unbounded_channel(); |
| 1086 | let video = LocalVideo::new( |
| 1087 | &factory, |
| 1088 | VideoMode::Pattern, |
| 1089 | VideoHub::new(updates.clone()), |
| 1090 | updates, |
| 1091 | ); |
| 1092 | let sender = transceiver.sender(); |
| 1093 | sender |
| 1094 | .set_track(Some( |
| 1095 | libwebrtc::media_stream_track::MediaStreamTrack::Video(video.camera_track()), |
| 1096 | )) |
| 1097 | .expect("attach"); |
| 1098 | // Do not call sender.track() afterwards: the FFI track() wrapper crashes on a null |
| 1099 | // webrtc track (get_or_create_media_stream_track). |
| 1100 | sender.set_track(None).expect("detach must accept nullptr"); |
| 1101 | peer.close(); |
| 1102 | } |
| 1103 | |
| 1104 | #[test] |
| 1105 | fn frame_sizes_survive_packing() { |
| 1106 | assert_eq!(unpack_size(pack_size(1280, 720)), Some((1280, 720))); |
| 1107 | assert_eq!(unpack_size(0), None); |
| 1108 | } |
| 1109 | |
| 1110 | #[test] |
| 1111 | fn the_video_mode_follows_the_environment_value() { |
| 1112 | assert_eq!(VideoMode::parse(None), VideoMode::Platform); |
| 1113 | assert_eq!(VideoMode::parse(Some("Pattern")), VideoMode::Pattern); |
| 1114 | assert_eq!(VideoMode::parse(Some("off")), VideoMode::Off); |
| 1115 | assert_eq!(VideoMode::parse(Some("whatever")), VideoMode::Platform); |
| 1116 | } |
| 1117 | } |