Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(desktop): mirroring and screen sharing restart cleanly

Beea25ca191865c
diff --git a/desktop/app/src/mirror.rs b/desktop/app/src/mirror.rs
index 5d3c944..f2ef559 100644
--- a/desktop/app/src/mirror.rs
+++ b/desktop/app/src/mirror.rs
@@ -374,26 +374,6 @@ fn latest_bitmap(window: &Window) -> Option<Bitmap> {
Some(Bitmap { width, height, bgra })
}
-fn free_path(dir: &Path, name: &str) -> PathBuf {
- let first = dir.join(name);
- if !first.exists() {
- return first;
- }
- let path = Path::new(name);
- let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or(name);
- let ext = path.extension().and_then(|e| e.to_str());
- for n in 2..10_000u32 {
- let candidate = match ext {
- Some(ext) => dir.join(format!("{stem} ({n}).{ext}")),
- None => dir.join(format!("{stem} ({n})")),
- };
- if !candidate.exists() {
- return candidate;
- }
- }
- first
-}
-
/// Copies the current mirror frame for `window` to the PC clipboard as PNG.
pub fn screenshot_clipboard(window: Window) {
let Some(bitmap) = latest_bitmap(&window) else {
@@ -442,7 +422,7 @@ pub fn screenshot_file(window: Window) {
show_message(tr!("Mirror", "The screenshot couldn't be saved."));
return;
}
- let path = free_path(&dir, &base_name);
+ let path = crate::photos::free_path(&dir, &base_name);
if let Err(e) = std::fs::write(&path, &png) {
tracing::warn!(error = %e, "can't write mirror screenshot");
set_notice(window, "Couldn't save screenshot");
@@ -536,7 +516,7 @@ pub fn toggle_recording(window: Window) {
show_message(tr!("Mirror", "Couldn't start recording."));
return;
}
- let path = free_path(&dir, &base_name);
+ let path = crate::photos::free_path(&dir, &base_name);
let (width, height) = LAST_CONFIG
.lock()
.unwrap_or_else(|e| e.into_inner())
@@ -951,7 +931,23 @@ pub fn resize(window: Window, width: u32, height: u32) {
});
}
+/// Each request's number per window, so an earlier request's timer can't
+/// end a retry of the same window.
+static ATTEMPTS: Mutex<Option<HashMap<Window, u64>>> = Mutex::new(None);
+
+fn next_attempt(window: Window) -> u64 {
+ let mut attempts = ATTEMPTS.lock().unwrap_or_else(|e| e.into_inner());
+ let n = attempts.get_or_insert_with(HashMap::new).entry(window).or_default();
+ *n += 1;
+ *n
+}
+
+fn is_latest_attempt(window: Window, attempt: u64) -> bool {
+ ATTEMPTS.lock().unwrap_or_else(|e| e.into_inner()).as_ref().and_then(|a| a.get(&window)) == Some(&attempt)
+}
+
fn request(window: Window, options: MirrorStart) {
+ let attempt = next_attempt(window);
if window.session == MIRROR_SCREEN {
crate::phone_sound::stop_for_window(window.device);
}
@@ -982,7 +978,8 @@ fn request(window: Window, options: MirrorStart) {
// seconds, or the phone couldn't start it (its stream then ends with no picture).
if app {
tokio::time::sleep(APP_START_TIMEOUT).await;
- let stuck = state(|s| s.windows.get(&window).is_some_and(|w| w.phase == Phase::Asking));
+ let stuck = is_latest_attempt(window, attempt)
+ && state(|s| s.windows.get(&window).is_some_and(|w| w.phase == Phase::Asking));
if stuck {
tracing::warn!(session = window.session, "an app window never started on the phone");
node.mirror_stop(window.device, window.session).await;
@@ -1291,11 +1288,7 @@ fn active_or_showing_window() -> Option<Window> {
{
return Some(active);
}
- s.windows
- .iter()
- .find(|(_, shown)| matches!(shown.phase, Phase::Showing))
- .or_else(|| s.windows.iter().next())
- .map(|(w, _)| *w)
+ s.windows.iter().find(|(_, shown)| matches!(shown.phase, Phase::Showing)).map(|(w, _)| *w)
})
}
@@ -1745,6 +1738,13 @@ fn ensure_gamepad_loop() {
let target = active_or_showing_window().filter(|w| matches!(phase(w), Some(Phase::Showing)));
let Some(window) = target else {
RUNNING.store(false, Ordering::Release);
+ // A window that started showing just now found the loop still
+ // running and left it to this one.
+ let showing =
+ active_or_showing_window().is_some_and(|w| matches!(phase(&w), Some(Phase::Showing)));
+ if showing && !RUNNING.swap(true, Ordering::AcqRel) {
+ continue;
+ }
break;
};
match poll_gamepad() {
@@ -1791,6 +1791,23 @@ struct Sink {
sound: Mutex<Option<Player>>,
}
+/// Asks the phone for a keyframe, at most every 500 ms per window: a
+/// decoder that's behind would otherwise ask on every dropped frame.
+fn ask_keyframe(window: Window) {
+ static ASKED: Mutex<Option<HashMap<Window, std::time::Instant>>> = Mutex::new(None);
+ {
+ let mut asked = ASKED.lock().unwrap_or_else(|e| e.into_inner());
+ let asked = asked.get_or_insert_with(HashMap::new);
+ if asked.get(&window).is_some_and(|at| at.elapsed() < Duration::from_millis(500)) {
+ return;
+ }
+ asked.insert(window, std::time::Instant::now());
+ }
+ if let Some(node) = core_host::node() {
+ core_host::spawn(async move { node.mirror_keyframe(window.device, window.session).await });
+ }
+}
+
/// The decoder thread's end of a sink.
struct Video {
queue: SyncSender<Item>,
@@ -1821,10 +1838,7 @@ impl Video {
impl Sink {
fn keyframe_please(&self) {
- let window = self.window;
- if let Some(node) = core_host::node() {
- core_host::spawn(async move { node.mirror_keyframe(window.device, window.session).await });
- }
+ ask_keyframe(self.window);
}
}
@@ -1948,11 +1962,7 @@ fn decode(window: Window, items: &Receiver<Item>, waiting: &AtomicUsize) {
}
};
tracing::info!(gpu = decoder.on_gpu(), codec = decoder.codec(), "mirror decoder ready");
- let keyframe_please = || {
- if let Some(node) = core_host::node() {
- core_host::spawn(async move { node.mirror_keyframe(window.device, window.session).await });
- }
- };
+ let keyframe_please = || ask_keyframe(window);
let mut size = (0, 0);
// After a gap, pictures are garbage until the next keyframe.
let mut need_keyframe = true;
diff --git a/desktop/app/src/pc_mirror.rs b/desktop/app/src/pc_mirror.rs
index 7a8677c..d211017 100644
--- a/desktop/app/src/pc_mirror.rs
+++ b/desktop/app/src/pc_mirror.rs
@@ -12,7 +12,7 @@ mod imp {
use std::mem::{ManuallyDrop, size_of};
use std::sync::{
Arc, Mutex,
- atomic::{AtomicBool, Ordering},
+ atomic::{AtomicBool, AtomicU64, Ordering},
};
#[cfg(any(windows, target_os = "linux"))]
use std::time::{Duration, Instant};
@@ -127,7 +127,8 @@ mod imp {
ACTIVE.lock().unwrap_or_else(|e| e.into_inner()).as_ref().is_some_and(|s| s.watching)
}
- /// Stops a phone watching one of this PC's players; screen sharing goes on.
+ /// Stops a phone watching one of this PC's players. That ends the
+ /// session: a watching session is the one capture there is.
pub fn stop_watching() {
let peer =
ACTIVE.lock().unwrap_or_else(|e| e.into_inner()).as_ref().filter(|s| s.watching).map(|s| s.peer);
@@ -138,7 +139,9 @@ mod imp {
/// Starts or replaces the active PC-to-phone screen mirror stream for `peer`.
pub fn start(peer: DeviceId, options: MirrorStart) {
- stop_all();
+ // The same phone asking again replaced its own stream: telling it to
+ // stop could land after the new one starts and end that.
+ stop_session(Some(peer));
let stop = Arc::new(AtomicBool::new(false));
let force_keyframe = Arc::new(AtomicBool::new(false));
{
@@ -153,10 +156,15 @@ mod imp {
core_host::host().hub.changed(crate::state::Changes::REMOTE);
core_host::refresh_capabilities();
- std::thread::Builder::new()
+ let stop_on_failure = Arc::clone(&stop);
+ if let Err(e) = std::thread::Builder::new()
.name("nectarlink-pc-mirror".into())
.spawn(move || run_capture_loop(peer, options, stop, force_keyframe))
- .ok();
+ {
+ // Not a session that claims to share with nothing behind it.
+ tracing::warn!(error = %e, "can't start sharing this PC's screen");
+ cleanup_session(&stop_on_failure);
+ }
}
/// Stops PC-to-phone mirroring if `peer` matches the active viewer.
@@ -182,6 +190,11 @@ mod imp {
/// Stops any active PC-to-phone mirroring session.
pub fn stop_all() {
+ stop_session(None);
+ }
+
+ /// Stops the active session, telling its phone unless it's `replaced_by`.
+ fn stop_session(replaced_by: Option<DeviceId>) {
let stopped = {
let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
if let Some(s) = guard.take() {
@@ -194,7 +207,9 @@ mod imp {
if let Some(peer) = stopped {
win::input::release_held_buttons();
crate::input_share::leave_remote();
- if let Some(node) = core_host::node() {
+ if let Some(node) = core_host::node()
+ && replaced_by != Some(peer)
+ {
node.set_pc_mirror_session_allowed(peer, false);
core_host::spawn(async move {
node.mirror_stop(peer, nectarlink_core::MIRROR_SCREEN).await;
@@ -2163,6 +2178,10 @@ mod imp {
} else {
Duration::from_millis(250)
};
+ // Its last frame would otherwise show as a live screen for good.
+ if !self.alive() {
+ return None;
+ }
let deadline = Instant::now() + wait;
loop {
if let Some(frame) = self.latest_frame.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {
@@ -2597,8 +2616,16 @@ mod imp {
}
}
+ // Only when no newer session took over: the screencast is shared.
#[cfg(target_os = "linux")]
- stop_wayland_screencast();
+ if ACTIVE
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .as_ref()
+ .is_none_or(|s| Arc::ptr_eq(&s.stop, &stop))
+ {
+ stop_wayland_screencast();
+ }
tracing::info!(%peer, "stopped sharing this PC's screen");
crate::input_share::leave_remote();
sender.close();
@@ -2616,35 +2643,49 @@ mod imp {
/// Phone speaker sends it), with the PC's speakers muted if the user wants
/// (capturing the sound doesn't depend on them), for as long as this lives;
/// then as before.
+ /// The PC's sound goes to the phone while it watches, and comes back
+ /// afterwards. A restart starts a new one before the old one ends, so
+ /// the settings from before the first are kept here, and only the newest
+ /// puts them back.
struct SoundToPhone {
- was_on: bool,
- was_target: Option<DeviceId>,
- /// This muted the speakers (they weren't already).
- muted: bool,
+ generation: u64,
}
+ /// Whether the sound went to the phone, and where, before any of this;
+ /// and whether this muted the speakers (they weren't already).
+ static BEFORE: Mutex<Option<(bool, Option<DeviceId>, bool)>> = Mutex::new(None);
+ static NEWEST: AtomicU64 = AtomicU64::new(0);
+
impl SoundToPhone {
fn start(peer: DeviceId) -> SoundToPhone {
- let muted = MUTE_WHILE_WATCHED.load(Ordering::Relaxed)
- && win::audio::speaker_state().is_some_and(|(_, muted)| !muted)
- && win::audio::set_speaker_mute(true).is_ok();
- let sound = SoundToPhone {
- was_on: crate::pc_audio_stream::is_enabled(),
- was_target: crate::pc_audio_stream::target(),
- muted,
- };
+ let generation = NEWEST.fetch_add(1, Ordering::AcqRel) + 1;
+ let mut before = BEFORE.lock().unwrap_or_else(|e| e.into_inner());
+ if before.is_none() {
+ let muted = MUTE_WHILE_WATCHED.load(Ordering::Relaxed)
+ && win::audio::speaker_state().is_some_and(|(_, muted)| !muted)
+ && win::audio::set_speaker_mute(true).is_ok();
+ *before =
+ Some((crate::pc_audio_stream::is_enabled(), crate::pc_audio_stream::target(), muted));
+ }
crate::pc_audio_stream::set_target(Some(peer));
crate::pc_audio_stream::set_enabled(true);
- sound
+ SoundToPhone { generation }
}
}
impl Drop for SoundToPhone {
fn drop(&mut self) {
- crate::pc_audio_stream::set_target(self.was_target);
- crate::pc_audio_stream::set_enabled(self.was_on);
+ if NEWEST.load(Ordering::Acquire) != self.generation {
+ return;
+ }
+ let Some((was_on, was_target, muted)) = BEFORE.lock().unwrap_or_else(|e| e.into_inner()).take()
+ else {
+ return;
+ };
+ crate::pc_audio_stream::set_target(was_target);
+ crate::pc_audio_stream::set_enabled(was_on);
// Unmuted only if this muted it.
- if self.muted {
+ if muted {
let _ = win::audio::set_speaker_mute(false);
}
}