feat(calling): start the camera preview when a video call is dialed
- the camera and its background effect run from the dial on, so the self view shows while ringing (was ~10 s after accept) - sending starts on the existing preview once the video line is negotiated - turning the camera off while ringing, or a failed escalation, stops the preview Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -813,6 +813,12 @@ async fn drive(
813
813
live.audio = Some(audio);
814
814
let mut local_video = LocalVideo::new(&factory, options.video, control.video.clone(), control.updates());
815
815
let mut camera_choice = options.camera.clone();
816
if options.start_camera {
817
match local_video.preview_camera(&camera_choice) {
818
Ok(()) => control.send(CallUpdate::Camera(true)),
819
Err(error) => timeline.record("camera preview failed", error.to_string()),
820
}
821
}
816
822
let mut media_description_request = 1u32;
817
823
refresh_cameras(control.updates(), options.video, timeline.clone());
818
824
refresh_share_sources(control.updates(), options.video);
@@ -1821,6 +1827,11 @@ async fn drive(
1821
1827
device_lists = devices::list_devices(&factory);
1822
1828
control.send(CallUpdate::Devices(device_lists.clone()));
1823
1829
}
1830
Some(CallCommand::SetCamera(false)) if local_video.camera_previewing() => {
1831
start_camera_pending = false;
1832
local_video.stop_camera();
1833
control.send(CallUpdate::Camera(false));
1834
}
1824
1835
Some(CallCommand::SetCamera(on)) => {
1825
1836
let direct = layout == Layout::OneToOne && negotiator.path() != MediaPath::Mixer;
1826
1837
if !remote_set || (on && !video_possible(remote_set, layout, negotiator.path())) {
@@ -1883,7 +1894,11 @@ async fn drive(
1883
1894
}
1884
1895
Some(CallCommand::SelectCamera(choice)) => {
1885
1896
camera_choice = choice;
1886
if local_video.camera_on()
1897
if local_video.camera_previewing()
1898
&& let Err(error) = local_video.preview_camera(&camera_choice)
1899
{
1900
control.send(CallUpdate::Notice(format!("Camera could not start: {error}")));
1901
} else if local_video.camera_on()
1887
1902
&& let Err(error) = local_video.start_camera(&camera_choice)
1888
1903
{
1889
1904
control.send(CallUpdate::Notice(format!("Camera could not start: {error}")));
@@ -2427,6 +2442,10 @@ async fn drive(
2427
2442
pending.session.peer.close();
2428
2443
negotiator.escalation_failed();
2429
2444
start_camera_pending = false;
2445
if local_video.camera_previewing() {
2446
local_video.stop_camera();
2447
control.send(CallUpdate::Camera(false));
2448
}
2430
2449
share_pending = None;
2431
2450
settle_failed_escalation(pending.offered.is_none(), layout, &mut negotiator, &mut deferred_changes, &mut local_video, control);
2432
2451
timeline.record("escalation failed", "no answer for the new peer connection in time");
@@ -813,6 +813,12 @@ async fn drive(813
live.audio = Some(audio);813
live.audio = Some(audio);814
let mut local_video = LocalVideo::new(&factory, options.video, control.video.clone(), control.updates());814
let mut local_video = LocalVideo::new(&factory, options.video, control.video.clone(), control.updates());815
let mut camera_choice = options.camera.clone();815
let mut camera_choice = options.camera.clone();816
if options.start_camera {817
match local_video.preview_camera(&camera_choice) {818
Ok(()) => control.send(CallUpdate::Camera(true)),819
Err(error) => timeline.record("camera preview failed", error.to_string()),820
}821
}816
let mut media_description_request = 1u32;822
let mut media_description_request = 1u32;817
refresh_cameras(control.updates(), options.video, timeline.clone());823
refresh_cameras(control.updates(), options.video, timeline.clone());818
refresh_share_sources(control.updates(), options.video);824
refresh_share_sources(control.updates(), options.video);@@ -1821,6 +1827,11 @@ async fn drive(1821
device_lists = devices::list_devices(&factory);1827
device_lists = devices::list_devices(&factory);1822
control.send(CallUpdate::Devices(device_lists.clone()));1828
control.send(CallUpdate::Devices(device_lists.clone()));1823
}1829
}1830
Some(CallCommand::SetCamera(false)) if local_video.camera_previewing() => {1831
start_camera_pending = false;1832
local_video.stop_camera();1833
control.send(CallUpdate::Camera(false));1834
}1824
Some(CallCommand::SetCamera(on)) => {1835
Some(CallCommand::SetCamera(on)) => {1825
let direct = layout == Layout::OneToOne && negotiator.path() != MediaPath::Mixer;1836
let direct = layout == Layout::OneToOne && negotiator.path() != MediaPath::Mixer;1826
if !remote_set || (on && !video_possible(remote_set, layout, negotiator.path())) {1837
if !remote_set || (on && !video_possible(remote_set, layout, negotiator.path())) {@@ -1883,7 +1894,11 @@ async fn drive(1883
}1894
}1884
Some(CallCommand::SelectCamera(choice)) => {1895
Some(CallCommand::SelectCamera(choice)) => {1885
camera_choice = choice;1896
camera_choice = choice;1886
if local_video.camera_on()1897
if local_video.camera_previewing()1898
&& let Err(error) = local_video.preview_camera(&camera_choice)1899
{1900
control.send(CallUpdate::Notice(format!("Camera could not start: {error}")));1901
} else if local_video.camera_on()1887
&& let Err(error) = local_video.start_camera(&camera_choice)1902
&& let Err(error) = local_video.start_camera(&camera_choice)1888
{1903
{1889
control.send(CallUpdate::Notice(format!("Camera could not start: {error}")));1904
control.send(CallUpdate::Notice(format!("Camera could not start: {error}")));@@ -2427,6 +2442,10 @@ async fn drive(2427
pending.session.peer.close();2442
pending.session.peer.close();2428
negotiator.escalation_failed();2443
negotiator.escalation_failed();2429
start_camera_pending = false;2444
start_camera_pending = false;2445
if local_video.camera_previewing() {2446
local_video.stop_camera();2447
control.send(CallUpdate::Camera(false));2448
}2430
share_pending = None;2449
share_pending = None;2431
settle_failed_escalation(pending.offered.is_none(), layout, &mut negotiator, &mut deferred_changes, &mut local_video, control);2450
settle_failed_escalation(pending.offered.is_none(), layout, &mut negotiator, &mut deferred_changes, &mut local_video, control);2432
timeline.record("escalation failed", "no answer for the new peer connection in time");2451
timeline.record("escalation failed", "no answer for the new peer connection in time");@@ -161,6 +161,7 @@ pub struct LocalVideo {
161
161
blur: Arc<BlurSettings>,
162
162
motion: bool,
163
163
share_fps: Arc<AtomicU32>,
164
camera_preview: bool,
164
165
}
165
166
166
167
impl LocalVideo {
@@ -175,6 +176,7 @@ impl LocalVideo {
175
176
blur: Arc::new(BlurSettings::default()),
176
177
motion: false,
177
178
share_fps: Arc::new(AtomicU32::new(SCREEN_SHARE_FPS)),
179
camera_preview: false,
178
180
}
179
181
}
180
182
@@ -343,7 +345,11 @@ impl LocalVideo {
343
345
}
344
346
345
347
pub fn camera_on(&self) -> bool {
346
self.camera.capture.is_some()
348
self.camera.capture.is_some() && !self.camera_preview
349
}
350
351
pub fn camera_previewing(&self) -> bool {
352
self.camera.capture.is_some() && self.camera_preview
347
353
}
348
354
349
355
pub fn sharing(&self) -> bool {
@@ -391,23 +397,41 @@ impl LocalVideo {
391
397
}
392
398
}
393
399
400
pub fn preview_camera(&mut self, choice: &DeviceChoice) -> Result<()> {
401
if !self.is_available() {
402
return Err(Error::Webrtc("video is switched off".into()));
403
}
404
self.camera.capture = None;
405
self.camera.capture = Some(self.capture_camera(choice)?);
406
self.camera_preview = true;
407
Ok(())
408
}
409
394
410
pub fn start_camera(&mut self, choice: &DeviceChoice) -> Result<()> {
395
411
let lines = self.lines.as_ref().ok_or_else(|| Error::Webrtc("no video line negotiated".into()))?;
396
412
if !self.is_available() {
397
413
return Err(Error::Webrtc("video is switched off".into()));
398
414
}
399
self.camera.capture = None;
415
let sender = lines.camera.sender();
416
if !self.camera_previewing() {
417
self.camera.capture = None;
418
self.camera.capture = Some(self.capture_camera(choice)?);
419
}
420
Self::begin_sending(&sender, &self.camera.track)?;
421
self.camera_preview = false;
422
Ok(())
423
}
424
425
fn capture_camera(&self, choice: &DeviceChoice) -> Result<Capture> {
400
426
let mut sink = self.camera_sink();
401
let capture = match self.mode {
427
Ok(match self.mode {
402
428
VideoMode::Pattern => Capture::Task(spawn_pattern(sink, PatternKind::Camera, Some(self.blur.clone()))),
403
429
_ => start_camera(choice, self.blur.clone(), move |buffer| sink.push(buffer), self.report_failure("Camera"))?,
404
};
405
Self::begin_sending(&lines.camera.sender(), &self.camera.track)?;
406
self.camera.capture = Some(capture);
407
Ok(())
430
})
408
431
}
409
432
410
433
pub fn stop_camera(&mut self) {
434
self.camera_preview = false;
411
435
self.camera.capture = None;
412
436
self.blur.set_cutout(None);
413
437
if let Some(lines) = &self.lines {
@@ -453,6 +477,19 @@ impl LocalVideo {
453
477
mod tests {
454
478
use super::*;
455
479
480
#[tokio::test]
481
async fn a_camera_preview_runs_without_a_line_and_stops_cleanly() {
482
let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel();
483
let factory = crate::devices::shared_factory();
484
let mut video = LocalVideo::new(&factory, VideoMode::Pattern, VideoHub::new(sender.clone()), sender);
485
video.preview_camera(&DeviceChoice::SystemDefault).unwrap();
486
assert!(video.camera_previewing() && !video.camera_on());
487
assert!(video.start_camera(&DeviceChoice::SystemDefault).is_err(), "sending needs a negotiated line");
488
assert!(video.camera_previewing());
489
video.stop_camera();
490
assert!(!video.camera_previewing() && !video.camera_on());
491
}
492
456
493
#[test]
457
494
fn frame_sizes_survive_packing() {
458
495
assert_eq!(unpack_size(pack_size(1280, 720)), Some((1280, 720)));
@@ -161,6 +161,7 @@ pub struct LocalVideo {161
blur: Arc<BlurSettings>,161
blur: Arc<BlurSettings>,162
motion: bool,162
motion: bool,163
share_fps: Arc<AtomicU32>,163
share_fps: Arc<AtomicU32>,164
camera_preview: bool,164
}165
}165
166
166
impl LocalVideo {167
impl LocalVideo {@@ -175,6 +176,7 @@ impl LocalVideo {175
blur: Arc::new(BlurSettings::default()),176
blur: Arc::new(BlurSettings::default()),176
motion: false,177
motion: false,177
share_fps: Arc::new(AtomicU32::new(SCREEN_SHARE_FPS)),178
share_fps: Arc::new(AtomicU32::new(SCREEN_SHARE_FPS)),179
camera_preview: false,178
}180
}179
}181
}180
182
@@ -343,7 +345,11 @@ impl LocalVideo {343
}345
}344
346
345
pub fn camera_on(&self) -> bool {347
pub fn camera_on(&self) -> bool {346
self.camera.capture.is_some()348
self.camera.capture.is_some() && !self.camera_preview349
}350
351
pub fn camera_previewing(&self) -> bool {352
self.camera.capture.is_some() && self.camera_preview347
}353
}348
354
349
pub fn sharing(&self) -> bool {355
pub fn sharing(&self) -> bool {@@ -391,23 +397,41 @@ impl LocalVideo {391
}397
}392
}398
}393
399
400
pub fn preview_camera(&mut self, choice: &DeviceChoice) -> Result<()> {401
if !self.is_available() {402
return Err(Error::Webrtc("video is switched off".into()));403
}404
self.camera.capture = None;405
self.camera.capture = Some(self.capture_camera(choice)?);406
self.camera_preview = true;407
Ok(())408
}409
394
pub fn start_camera(&mut self, choice: &DeviceChoice) -> Result<()> {410
pub fn start_camera(&mut self, choice: &DeviceChoice) -> Result<()> {395
let lines = self.lines.as_ref().ok_or_else(|| Error::Webrtc("no video line negotiated".into()))?;411
let lines = self.lines.as_ref().ok_or_else(|| Error::Webrtc("no video line negotiated".into()))?;396
if !self.is_available() {412
if !self.is_available() {397
return Err(Error::Webrtc("video is switched off".into()));413
return Err(Error::Webrtc("video is switched off".into()));398
}414
}399
self.camera.capture = None;415
let sender = lines.camera.sender();416
if !self.camera_previewing() {417
self.camera.capture = None;418
self.camera.capture = Some(self.capture_camera(choice)?);419
}420
Self::begin_sending(&sender, &self.camera.track)?;421
self.camera_preview = false;422
Ok(())423
}424
425
fn capture_camera(&self, choice: &DeviceChoice) -> Result<Capture> {400
let mut sink = self.camera_sink();426
let mut sink = self.camera_sink();401
let capture = match self.mode {427
Ok(match self.mode {402
VideoMode::Pattern => Capture::Task(spawn_pattern(sink, PatternKind::Camera, Some(self.blur.clone()))),428
VideoMode::Pattern => Capture::Task(spawn_pattern(sink, PatternKind::Camera, Some(self.blur.clone()))),403
_ => start_camera(choice, self.blur.clone(), move |buffer| sink.push(buffer), self.report_failure("Camera"))?,429
_ => start_camera(choice, self.blur.clone(), move |buffer| sink.push(buffer), self.report_failure("Camera"))?,404
};430
})405
Self::begin_sending(&lines.camera.sender(), &self.camera.track)?;406
self.camera.capture = Some(capture);407
Ok(())408
}431
}409
432
410
pub fn stop_camera(&mut self) {433
pub fn stop_camera(&mut self) {434
self.camera_preview = false;411
self.camera.capture = None;435
self.camera.capture = None;412
self.blur.set_cutout(None);436
self.blur.set_cutout(None);413
if let Some(lines) = &self.lines {437
if let Some(lines) = &self.lines {@@ -453,6 +477,19 @@ impl LocalVideo {453
mod tests {477
mod tests {454
use super::*;478
use super::*;455
479
480
#[tokio::test]481
async fn a_camera_preview_runs_without_a_line_and_stops_cleanly() {482
let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel();483
let factory = crate::devices::shared_factory();484
let mut video = LocalVideo::new(&factory, VideoMode::Pattern, VideoHub::new(sender.clone()), sender);485
video.preview_camera(&DeviceChoice::SystemDefault).unwrap();486
assert!(video.camera_previewing() && !video.camera_on());487
assert!(video.start_camera(&DeviceChoice::SystemDefault).is_err(), "sending needs a negotiated line");488
assert!(video.camera_previewing());489
video.stop_camera();490
assert!(!video.camera_previewing() && !video.camera_on());491
}492
456
#[test]493
#[test]457
fn frame_sizes_survive_packing() {494
fn frame_sizes_survive_packing() {458
assert_eq!(unpack_size(pack_size(1280, 720)), Some((1280, 720)));495
assert_eq!(unpack_size(pack_size(1280, 720)), Some((1280, 720)));