Skip to content

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

refactor(desktop): align module layout, honest error handling, and capability advertising for macOS

saivinay18050a3d5fd4e31b
diff --git a/README.md b/README.md
index 4cecd08..1101f27 100644
--- a/README.md
+++ b/README.md
@@ -123,6 +123,23 @@ searchable **Timeline** of everything that moved, a **Command Palette**
Four themes, **Hive**, **Bloom**, **Glass** and **Graphite**, each in light
and dark.
+### Platform support
+
+| Feature | Windows | macOS (Preview) |
+|---|---|---|
+| Pairing & mutual TLS | Yes | Yes |
+| Files & folder transfer | Yes | Yes |
+| Photos browser & download | Yes | Yes |
+| Continuity Camera | Yes | Yes |
+| Calls & SMS viewer | Yes | Yes |
+| Phone quick toggles & volume | Yes | Yes |
+| Clipboard text sync | Yes | Yes (two-way text, incoming photo) |
+| System notifications | Yes | Yes (system banners) |
+| Phone screen mirroring | Yes | Not yet (in development) |
+| Screen recording (MP4) | Yes | Not yet |
+| PC audio stream / phone mic | Yes | Not yet |
+| Storage mount in Explorer | Yes | Windows Cloud Files only |
+
## Private by design
- **Local-first.** Devices talk directly over your network or a USB cable.
@@ -228,6 +245,7 @@ docs/ Guide, technical docs, protocol, design system (CC B
|---|---|
| Core and CLI | [Rust](https://rustup.rs) (stable, pinned in `rust-toolchain.toml`) |
| Windows app | Windows 11, Visual Studio 2022 Build Tools (C++), [Qt 6.12](https://www.qt.io/download-open-source) for MSVC 2022 x64 |
+| macOS app | macOS 14+, Xcode Command Line Tools, [Qt 6.12](https://www.qt.io/download-open-source) (or Homebrew Qt 6) |
| Android app | Android SDK (platform 37, NDK 30.0.16248370), JDK 17 or newer, `cargo-ndk` (see [android/README.md](android/README.md)) |
```sh
diff --git a/core/nectarlink-core/src/features.rs b/core/nectarlink-core/src/features.rs
index 76320f2..58e083f 100644
--- a/core/nectarlink-core/src/features.rs
+++ b/core/nectarlink-core/src/features.rs
@@ -1461,4 +1461,74 @@ mod tests {
);
assert_eq!(matrix.state("device.find_phone"), Some(FeatureState::Available));
}
+
+ #[test]
+ fn macos_desktop_matrix_reflects_supported_and_unsupported_features() {
+ const MAC_DESKTOP: &[&str] = &[
+ "core.ping",
+ "device.battery",
+ "device.ring",
+ "files.transfer",
+ "clip.image",
+ "media.remote",
+ "link.open",
+ "task.notify",
+ "camera.continuity",
+ "pc.power",
+ "pc.wake",
+ "photos.show",
+ "calls.show",
+ "contacts.show",
+ "sms.show",
+ "toggles.show",
+ ];
+ let mac = facts(DeviceKind::Desktop, "macos", "15.0", PowerLevel::NotApplicable, MAC_DESKTOP);
+ let mut phone_caps = BASIC_PHONE.to_vec();
+ phone_caps.extend(["link.open", "storage.read", "storage.write"]);
+ let phone = facts(DeviceKind::Phone, "android", "16", PowerLevel::Elevated, &phone_caps);
+ let m = matrix_with(&phone, &mac, &HashMap::new());
+
+ // Supported macOS features are Available:
+ assert_eq!(m.state("device.find_phone"), Some(FeatureState::Available));
+ assert_eq!(m.state("device.battery"), Some(FeatureState::Available));
+ assert_eq!(m.state("device.pc_actions"), Some(FeatureState::Available));
+ assert_eq!(m.state("device.links_to_pc"), Some(FeatureState::Available));
+ assert_eq!(m.state("device.links_to_phone"), Some(FeatureState::Available));
+ assert_eq!(m.state("camera.continuity"), Some(FeatureState::Available));
+ assert_eq!(m.state("files.send"), Some(FeatureState::Available));
+ assert_eq!(m.state("files.recent_photos"), Some(FeatureState::Available));
+ assert_eq!(m.state("calls.alerts"), Some(FeatureState::Available));
+ assert_eq!(m.state("contacts.read"), Some(FeatureState::Available));
+ assert_eq!(m.state("toggles.volume"), Some(FeatureState::Available));
+ assert_eq!(m.state("clipboard.pc_to_phone"), Some(FeatureState::Available));
+
+ // Unsupported macOS features are NOT Available:
+ assert_eq!(
+ m.state("mirroring.view"),
+ Some(locked(UpgradeAction::UpdateApp(Role::Desktop), Effort::Minutes(2)))
+ );
+ assert_ne!(m.state("mirroring.control"), Some(FeatureState::Available));
+ assert_ne!(m.state("mirroring.app_windows"), Some(FeatureState::Available));
+ assert_ne!(m.state("mirroring.audio"), Some(FeatureState::Available));
+ assert_eq!(
+ m.state("media.pc_control"),
+ Some(locked(UpgradeAction::UpdateApp(Role::Desktop), Effort::Minutes(2)))
+ );
+ // With default toggles, storage is opt-in (toggle off):
+ assert_eq!(
+ m.state("files.storage"),
+ Some(locked(UpgradeAction::EnableDeviceToggle("storage"), Effort::Instant))
+ );
+ // With storage toggle enabled, missing desktop("storage.mount") locks behind UpdateApp:
+ let toggles_with_storage = HashMap::from([("storage".to_owned(), true)]);
+ let m_storage = matrix_with(&phone, &mac, &toggles_with_storage);
+ assert_eq!(
+ m_storage.state("files.storage"),
+ Some(locked(UpgradeAction::UpdateApp(Role::Desktop), Effort::Minutes(2)))
+ );
+ assert_eq!(
+ m.state("camera.webcam"),
+ Some(locked(UpgradeAction::EnableAddon("vcam"), Effort::Minutes(1)))
+ );
+ }
}
diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rs
index 3d00ad5..70fbd39 100644
--- a/core/nectarlink-core/src/node.rs
+++ b/core/nectarlink-core/src/node.rs
@@ -75,7 +75,6 @@ const BASE_CAPABILITIES: &[&str] = &[
"media.remote",
"link.open",
"task.notify",
- "mirror.view",
];
/// This device's mutable description, sent to peers.
diff --git a/desktop/app/cpp/app_helpers.cpp b/desktop/app/cpp/app_helpers.cpp
index 412d50d..7fa3e21 100644
--- a/desktop/app/cpp/app_helpers.cpp
+++ b/desktop/app/cpp/app_helpers.cpp
@@ -318,4 +318,25 @@ rust::String translate(rust::Str context, rust::Str text)
return rust::String(QCoreApplication::translate(c.c_str(), t.c_str()).toStdString());
}
-
+rust::Vec<uint8_t> decode_image(rust::Slice<const uint8_t> bytes)
+{
+ rust::Vec<uint8_t> result;
+ if (bytes.empty())
+ return result;
+ QImage img;
+ if (!img.loadFromData(bytes.data(), static_cast<int>(bytes.size())))
+ return result;
+ const QImage argb = img.convertToFormat(QImage::Format_ARGB32);
+ const uint32_t width = static_cast<uint32_t>(argb.width());
+ const uint32_t height = static_cast<uint32_t>(argb.height());
+ const size_t byteCount = static_cast<size_t>(argb.sizeInBytes());
+ result.reserve(8 + byteCount);
+ for (int i = 0; i < 4; ++i)
+ result.push_back(static_cast<uint8_t>((width >> (i * 8)) & 0xFF));
+ for (int i = 0; i < 4; ++i)
+ result.push_back(static_cast<uint8_t>((height >> (i * 8)) & 0xFF));
+ const uint8_t *bits = argb.constBits();
+ for (size_t i = 0; i < byteCount; ++i)
+ result.push_back(bits[i]);
+ return result;
+}
diff --git a/desktop/app/cpp/app_helpers.h b/desktop/app/cpp/app_helpers.h
index 1b6a953..9353951 100644
--- a/desktop/app/cpp/app_helpers.h
+++ b/desktop/app/cpp/app_helpers.h
@@ -52,5 +52,5 @@ bool install_translator(rust::Str dir, rust::Str language);
// builds in Rust; `text` itself when there is no translation.
rust::String translate(rust::Str context, rust::Str text);
-
-
+// Decodes image bytes (PNG, JPEG, WebP, BMP, etc.) into width, height and ARGB32 bytes.
+rust::Vec<uint8_t> decode_image(rust::Slice<const uint8_t> bytes);
diff --git a/desktop/app/i18n/rust_strings.h b/desktop/app/i18n/rust_strings.h
index 5bfe555..29ee72c 100644
--- a/desktop/app/i18n/rust_strings.h
+++ b/desktop/app/i18n/rust_strings.h
@@ -351,7 +351,7 @@ static const char *const nectarlink_rust_strings[] = {
QT_TRANSLATE_NOOP("Obs", "OBS isn't reachable on port %1. Turn on its WebSocket server."), // desktop/app/src/obs.rs
QT_TRANSLATE_NOOP("Obs", "OBS: %1"), // desktop/app/src/obs.rs
QT_TRANSLATE_NOOP("Obs", "The service on that port isn't OBS."), // desktop/app/src/obs.rs
- QT_TRANSLATE_NOOP("PcMirror", "Can't find the player's window. Play the video on this PC again, then turn on Play on phone."), // desktop/app/src/pc_mirror/windows.rs
+ QT_TRANSLATE_NOOP("PcMirror", "Can't find the player's window. Play the video on this PC again, then turn on Play on phone."), // desktop/app/src/pc_mirror.rs
QT_TRANSLATE_NOOP("PhoneMic", "Tap Start on %1 to use its microphone."), // desktop/app/src/phone_mic.rs
QT_TRANSLATE_NOOP("PhoneMic", "The microphone is turned off for this PC on %1."), // desktop/app/src/phone_mic.rs
QT_TRANSLATE_NOOP("PhoneMic", "Update NectarLink on %1 to use its microphone."), // desktop/app/src/phone_mic.rs
@@ -379,12 +379,12 @@ static const char *const nectarlink_rust_strings[] = {
QT_TRANSLATE_NOOP("Photos", "The phone isn't connected right now."), // desktop/app/src/photos.rs
QT_TRANSLATE_NOOP("Photos", "The photo couldn't be copied to the clipboard."), // desktop/app/src/photos.rs
QT_TRANSLATE_NOOP("Photos", "The photo couldn't be copied. It's in Downloads\\Nectarlink."), // desktop/app/src/photos.rs
- QT_TRANSLATE_NOOP("PlayOnPhone", "Connect your phone to play on it."), // desktop/app/src/play_on_phone/windows.rs
- QT_TRANSLATE_NOOP("PlayOnPhone", "Media is turned off for %1."), // desktop/app/src/play_on_phone/windows.rs
- QT_TRANSLATE_NOOP("PlayOnPhone", "Opening in %1 on %2. Tap the notification there."), // desktop/app/src/play_on_phone/windows.rs
- QT_TRANSLATE_NOOP("PlayOnPhone", "Sent to %1. Tap the notification there."), // desktop/app/src/play_on_phone/windows.rs
- QT_TRANSLATE_NOOP("PlayOnPhone", "Tap the notification on %1 to watch."), // desktop/app/src/play_on_phone/windows.rs
- QT_TRANSLATE_NOOP("PlayOnPhone", "Update NectarLink on %1 to play on it."), // desktop/app/src/play_on_phone/windows.rs
+ QT_TRANSLATE_NOOP("PlayOnPhone", "Connect your phone to play on it."), // desktop/app/src/play_on_phone.rs
+ QT_TRANSLATE_NOOP("PlayOnPhone", "Media is turned off for %1."), // desktop/app/src/play_on_phone.rs
+ QT_TRANSLATE_NOOP("PlayOnPhone", "Opening in %1 on %2. Tap the notification there."), // desktop/app/src/play_on_phone.rs
+ QT_TRANSLATE_NOOP("PlayOnPhone", "Sent to %1. Tap the notification there."), // desktop/app/src/play_on_phone.rs
+ QT_TRANSLATE_NOOP("PlayOnPhone", "Tap the notification on %1 to watch."), // desktop/app/src/play_on_phone.rs
+ QT_TRANSLATE_NOOP("PlayOnPhone", "Update NectarLink on %1 to play on it."), // desktop/app/src/play_on_phone.rs
QT_TRANSLATE_NOOP("Prefs", "Connected to OBS."), // desktop/app/src/bridge/prefs.rs
QT_TRANSLATE_NOOP("Recordings", "Recording from %1 saved"), // desktop/app/src/recordings.rs
QT_TRANSLATE_NOOP("SendTo", "Copied to your phone's clipboard."), // desktop/app/src/send_to.rs
diff --git a/desktop/app/src/automations.rs b/desktop/app/src/automations.rs
index 188f6e7..78857e1 100644
--- a/desktop/app/src/automations.rs
+++ b/desktop/app/src/automations.rs
@@ -16,8 +16,10 @@ use crate::{deck, settings, win};
/// Supported automation trigger identifiers.
pub const TRIGGER_PHONE_CONNECTED: &str = "phone_connected";
pub const TRIGGER_PHONE_DISCONNECTED: &str = "phone_disconnected";
+#[allow(dead_code)]
pub const TRIGGER_PHONE_WALKED_AWAY: &str = "phone_walked_away";
pub const TRIGGER_CALL_RINGING: &str = "call_ringing";
+
pub const TRIGGER_CALL_ENDED: &str = "call_ended";
pub const TRIGGER_BATTERY_LOW: &str = "battery_low";
pub const TRIGGER_CHARGING_STARTED: &str = "charging_started";
diff --git a/desktop/app/src/bridge/native.rs b/desktop/app/src/bridge/native.rs
index 282df8c..fa1210c 100644
--- a/desktop/app/src/bridge/native.rs
+++ b/desktop/app/src/bridge/native.rs
@@ -50,5 +50,8 @@ pub mod ffi {
/// `text` in the installed language (`QCoreApplication::translate`).
fn translate(context: &str, text: &str) -> String;
+
+ /// Decodes image bytes into a byte vector: 4-byte width (LE), 4-byte height (LE), followed by ARGB32 bytes.
+ fn decode_image(bytes: &[u8]) -> Vec<u8>;
}
}
diff --git a/desktop/app/src/core_host.rs b/desktop/app/src/core_host.rs
index 2a50897..54ad9c3 100644
--- a/desktop/app/src/core_host.rs
+++ b/desktop/app/src/core_host.rs
@@ -40,6 +40,7 @@ pub fn host() -> &'static CoreHost {
}
/// The running host's data directory, if the host has been started.
+#[allow(dead_code)]
pub fn try_data_dir() -> Option<&'static std::path::Path> {
HOST.get().map(|h| h.data_dir.as_path())
}
@@ -99,9 +100,10 @@ pub fn start(data_dir: PathBuf, platform: Arc<dyn Platform>) -> std::io::Result<
/// Computes the PC's advertised capabilities, advertising capture (`mirror.capture`)
/// and input injection (`mirror.input`, `input.inject`) only when enabled for at
/// least one paired device (or actively sharing).
-pub fn compute_capabilities(node: Option<&Node>) -> Vec<String> {
+pub fn compute_capabilities(
+ #[cfg_attr(not(windows), allow(unused_variables))] node: Option<&Node>,
+) -> Vec<String> {
let mut caps = vec![
- "media.control".into(),
"pc.power".into(),
nectarlink_core::PC_WAKE.into(),
nectarlink_core::PHOTOS_SHOW.into(),
@@ -109,21 +111,28 @@ pub fn compute_capabilities(node: Option<&Node>) -> Vec<String> {
nectarlink_core::CALLS_SHOW.into(),
nectarlink_core::CONTACTS_SHOW.into(),
nectarlink_core::SMS_SHOW.into(),
- nectarlink_core::MIRROR_VIEW.into(),
- nectarlink_core::MIRROR_LISTEN.into(),
- nectarlink_core::DECK_ACTIONS.into(),
- nectarlink_core::PC_AUDIO.into(),
- nectarlink_core::RECORDER.into(),
nectarlink_core::TOGGLES_SHOW.into(),
- nectarlink_core::STORAGE_MOUNT.into(),
- nectarlink_core::STORAGE_READ.into(),
- nectarlink_core::STORAGE_WRITE.into(),
- nectarlink_core::WEBCAM_VIRTUAL.into(),
- nectarlink_core::MEDIA_STREAM.into(),
- nectarlink_core::MIC_LISTEN.into(),
];
+ #[cfg(windows)]
+ {
+ caps.push("media.control".into());
+ caps.push(nectarlink_core::STORAGE_MOUNT.into());
+ caps.push(nectarlink_core::STORAGE_READ.into());
+ caps.push(nectarlink_core::STORAGE_WRITE.into());
+ caps.push(nectarlink_core::MIRROR_VIEW.into());
+ caps.push(nectarlink_core::MIRROR_LISTEN.into());
+ caps.push(nectarlink_core::DECK_ACTIONS.into());
+ caps.push(nectarlink_core::PC_AUDIO.into());
+ caps.push(nectarlink_core::RECORDER.into());
+ caps.push(nectarlink_core::WEBCAM_VIRTUAL.into());
+ caps.push(nectarlink_core::MEDIA_STREAM.into());
+ caps.push(nectarlink_core::MIC_LISTEN.into());
+ }
+ #[cfg(windows)]
let mut pc_mirror_enabled = crate::pc_mirror::is_active();
+ #[cfg(windows)]
let mut remote_input_enabled = false;
+ #[cfg(windows)]
if let Some(node) = node
&& let Ok(devices) = node.paired_devices()
{
@@ -140,9 +149,11 @@ pub fn compute_capabilities(node: Option<&Node>) -> Vec<String> {
}
}
}
+ #[cfg(windows)]
if pc_mirror_enabled {
caps.push(nectarlink_core::MIRROR_CAPTURE.into());
}
+ #[cfg(windows)]
if remote_input_enabled {
caps.push(nectarlink_core::MIRROR_INPUT.into());
caps.push(nectarlink_core::INPUT_INJECT.into());
@@ -361,7 +372,7 @@ fn this_device() -> DeviceInfo {
DeviceInfo {
name,
kind: if crate::win::is_laptop() { DeviceKind::Laptop } else { DeviceKind::Desktop },
- os: "windows".into(),
+ os: std::env::consts::OS.into(),
os_ver: crate::win::os_version(),
model: None,
accent: None,
@@ -392,3 +403,55 @@ pub fn shutdown() {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ #[cfg(target_os = "macos")]
+ fn macos_advertises_only_supported_capabilities() {
+ let caps = compute_capabilities(None);
+ // Ensure unsupported features on macOS are never advertised
+ assert!(!caps.contains(&"mirror.view".into()), "macOS must not advertise mirror.view");
+ assert!(!caps.contains(&"mirror.listen".into()), "macOS must not advertise mirror.listen");
+ assert!(!caps.contains(&"media.control".into()), "macOS must not advertise media.control");
+ assert!(
+ !caps.contains(&nectarlink_core::STORAGE_MOUNT.into()),
+ "macOS must not advertise storage.mount"
+ );
+ assert!(
+ !caps.contains(&nectarlink_core::STORAGE_READ.into()),
+ "macOS must not advertise storage.read"
+ );
+ assert!(
+ !caps.contains(&nectarlink_core::STORAGE_WRITE.into()),
+ "macOS must not advertise storage.write"
+ );
+ assert!(
+ !caps.contains(&nectarlink_core::WEBCAM_VIRTUAL.into()),
+ "macOS must not advertise camera.virtual"
+ );
+ assert!(!caps.contains(&nectarlink_core::PC_AUDIO.into()), "macOS must not advertise pc.audio");
+ assert!(!caps.contains(&nectarlink_core::RECORDER.into()), "macOS must not advertise recorder");
+ assert!(
+ !caps.contains(&nectarlink_core::DECK_ACTIONS.into()),
+ "macOS must not advertise deck.actions"
+ );
+ assert!(
+ !caps.contains(&nectarlink_core::MEDIA_STREAM.into()),
+ "macOS must not advertise media.stream"
+ );
+ assert!(!caps.contains(&nectarlink_core::MIC_LISTEN.into()), "macOS must not advertise mic.listen");
+
+ // Ensure supported features on macOS are advertised
+ assert!(caps.contains(&"pc.power".into()));
+ assert!(caps.contains(&nectarlink_core::PC_WAKE.into()));
+ assert!(caps.contains(&nectarlink_core::PHOTOS_SHOW.into()));
+ assert!(caps.contains(&nectarlink_core::CAMERA_CONTINUITY.into()));
+ assert!(caps.contains(&nectarlink_core::CALLS_SHOW.into()));
+ assert!(caps.contains(&nectarlink_core::CONTACTS_SHOW.into()));
+ assert!(caps.contains(&nectarlink_core::SMS_SHOW.into()));
+ assert!(caps.contains(&nectarlink_core::TOGGLES_SHOW.into()));
+ }
+}
diff --git a/desktop/app/src/local_api.rs b/desktop/app/src/local_api.rs
index 41fdaa7..ea4b286 100644
--- a/desktop/app/src/local_api.rs
+++ b/desktop/app/src/local_api.rs
@@ -102,6 +102,7 @@ pub fn token_file_path() -> PathBuf {
/// `None` if no secure source answered: the API then stays off rather than run with a
/// guessable token.
pub fn generate_token() -> Option<String> {
+ let mut bytes = [0u8; 32];
crate::win::secure_random(&mut bytes).then(|| HEXLOWER.encode(&bytes))
}
diff --git a/desktop/app/src/mac/audio.rs b/desktop/app/src/mac/audio.rs
index 1b20f98..6955de0 100644
--- a/desktop/app/src/mac/audio.rs
+++ b/desktop/app/src/mac/audio.rs
@@ -1,16 +1,27 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! macOS speaker volume, mute, and state.
+fn parse_speaker_state(text: &str) -> Option<(u8, bool)> {
+ let mut parts = text.split_whitespace();
+ let vol: u8 = parts.next()?.parse().ok()?;
+ let muted: bool = parts.next()?.parse().unwrap_or(false);
+ Some((vol, muted))
+}
+
pub fn speaker_state() -> Option<(u8, bool)> {
let output = std::process::Command::new("osascript")
- .args(["-e", "output volume of (get volume settings)", "-e", "output muted of (get volume settings)"])
+ .args([
+ "-e",
+ "set v to get volume settings",
+ "-e",
+ "(output volume of v as string) & \" \" & (output muted of v as string)",
+ ])
.output()
.ok()?;
- let text = String::from_utf8_lossy(&output.stdout);
- let mut lines = text.lines();
- let vol: u8 = lines.next()?.trim().parse().ok()?;
- let muted: bool = lines.next()?.trim().parse().unwrap_or(false);
- Some((vol, muted))
+ if !output.status.success() {
+ return None;
+ }
+ parse_speaker_state(&String::from_utf8_lossy(&output.stdout))
}
pub fn set_speaker_volume(level: u8) -> Result<(), String> {
@@ -76,3 +87,25 @@ pub fn output_devices() -> Vec<nectarlink_core::AudioOutputDevice> {
pub fn watch(_changed: fn()) {}
pub fn start(_on_changed: impl Fn() + Send + Sync + 'static) {}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn speaker_state_parses_volume_and_mute() {
+ assert_eq!(parse_speaker_state("25 false"), Some((25, false)));
+ assert_eq!(parse_speaker_state("100 true\n"), Some((100, true)));
+ assert_eq!(parse_speaker_state("0 false"), Some((0, false)));
+ assert_eq!(parse_speaker_state(""), None);
+ assert_eq!(parse_speaker_state("invalid"), None);
+ }
+
+ #[test]
+ fn query_live_speaker_state() {
+ let state = speaker_state();
+ assert!(state.is_some());
+ let (vol, _muted) = state.unwrap();
+ assert!(vol <= 100);
+ }
+}
diff --git a/desktop/app/src/mac/audio_out.rs b/desktop/app/src/mac/audio_out.rs
index 08e6123..8ee6e2a 100644
--- a/desktop/app/src/mac/audio_out.rs
+++ b/desktop/app/src/mac/audio_out.rs
@@ -1,13 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! macOS audio output streaming player.
-use std::sync::mpsc::{SyncSender, sync_channel};
-
#[derive(Debug)]
-pub struct Player {
- #[allow(dead_code)]
- queue: SyncSender<Vec<u8>>,
-}
+pub struct Player;
impl Player {
pub fn start(rate: u32, channels: u16) -> std::io::Result<Player> {
@@ -15,9 +10,22 @@ impl Player {
}
pub fn start_on(_device: Option<String>, _rate: u32, _channels: u16) -> std::io::Result<Player> {
- let (queue, _) = sync_channel(64);
- Ok(Player { queue })
+ Err(std::io::Error::new(
+ std::io::ErrorKind::Unsupported,
+ "streamed audio playback is not yet implemented on macOS",
+ ))
}
pub fn push(&self, _data: Vec<u8>) {}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn audio_out_reports_unsupported() {
+ assert_eq!(Player::start(48000, 2).unwrap_err().kind(), std::io::ErrorKind::Unsupported);
+ assert_eq!(Player::start_on(None, 48000, 2).unwrap_err().kind(), std::io::ErrorKind::Unsupported);
+ }
+}
diff --git a/desktop/app/src/mac/clipboard.rs b/desktop/app/src/mac/clipboard.rs
index 2d216b8..b721a02 100644
--- a/desktop/app/src/mac/clipboard.rs
+++ b/desktop/app/src/mac/clipboard.rs
@@ -3,9 +3,14 @@
use std::{
path::PathBuf,
- sync::{Mutex, OnceLock},
+ sync::{
+ Mutex, OnceLock,
+ atomic::{AtomicU64, Ordering},
+ },
};
+static CLIP_COUNTER: AtomicU64 = AtomicU64::new(0);
+
#[derive(Clone, PartialEq, Eq)]
pub enum Image {
Png(Vec<u8>),
@@ -111,8 +116,31 @@ pub fn write_private(text: &str) -> Result<(), String> {
write(text)
}
-pub fn write_image(_mime: &str, _bytes: &[u8]) -> Result<(), String> {
- Ok(())
+pub fn write_image(mime: &str, bytes: &[u8]) -> Result<(), String> {
+ if bytes.is_empty() {
+ return Err("empty image data".into());
+ }
+ let png_bytes = if mime == "image/png" && bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
+ bytes.to_vec()
+ } else {
+ let bitmap = super::image::decode(bytes)?;
+ super::image::encode_png(&bitmap)?
+ };
+
+ let seq = CLIP_COUNTER.fetch_add(1, Ordering::Relaxed);
+ let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos());
+ let tmp = std::env::temp_dir().join(format!("nectar_clip_{}_{}_{}.png", std::process::id(), now, seq));
+
+ std::fs::write(&tmp, &png_bytes).map_err(|e| e.to_string())?;
+ let script =
+ format!(r#"set the clipboard to (read (POSIX file "{}") as «class PNGf»)"#, tmp.to_string_lossy());
+ let res = std::process::Command::new("osascript").args(["-e", &script]).output();
+ let _ = std::fs::remove_file(&tmp);
+ match res {
+ Ok(out) if out.status.success() => Ok(()),
+ Ok(out) => Err(String::from_utf8_lossy(&out.stderr).into_owned()),
+ Err(e) => Err(e.to_string()),
+ }
}
pub fn build_dropfiles_payload(_paths: &[PathBuf]) -> Vec<u8> {
@@ -130,3 +158,54 @@ pub fn read_files() -> Option<Vec<PathBuf>> {
pub fn write_files(_paths: &[PathBuf]) -> bool {
false
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn write_image_rejects_empty_data() {
+ assert!(write_image("image/png", &[]).is_err());
+ }
+
+ #[test]
+ fn write_image_rejects_corrupted_jpeg() {
+ assert!(write_image("image/jpeg", &[0xff, 0xd8, 0x00, 0x00]).is_err());
+ }
+
+ #[test]
+ fn write_image_converts_valid_jpeg() {
+ #[rustfmt::skip]
+ const TINY_JPEG: &[u8] = &[
+ 255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 8,
+ 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26,
+ 31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, 48, 49, 52, 52,
+ 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, 255, 219, 0, 67, 1, 9, 9, 9, 12, 11, 12, 24,
+ 13, 13, 24, 50, 33, 28, 33, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 255, 192, 0, 17, 8, 0, 1, 0, 1, 3, 1, 34, 0, 2, 17, 1, 3,
+ 17, 1, 255, 196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,
+ 7, 8, 9, 10, 11, 255, 196, 0, 181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2,
+ 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66,
+ 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40,
+ 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89,
+ 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132,
+ 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165,
+ 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198,
+ 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226, 227, 228, 229, 230,
+ 231, 232, 233, 234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 196, 0, 31, 1, 0,
+ 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0,
+ 181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18,
+ 65, 81, 7, 97, 113, 19, 34, 50, 129, 8, 20, 66, 145, 161, 177, 193, 9, 35, 51, 82, 240, 21, 98,
+ 114, 209, 10, 22, 36, 52, 225, 37, 241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57,
+ 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103,
+ 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137,
+ 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170,
+ 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210,
+ 211, 212, 213, 214, 215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243,
+ 244, 245, 246, 247, 248, 249, 250, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 226, 232,
+ 162, 138, 249, 147, 247, 19, 255, 217,
+ ];
+ assert!(write_image("image/jpeg", TINY_JPEG).is_ok());
+ }
+}
diff --git a/desktop/app/src/mac/h264.rs b/desktop/app/src/mac/h264.rs
index 60a04ec..1633d84 100644
--- a/desktop/app/src/mac/h264.rs
+++ b/desktop/app/src/mac/h264.rs
@@ -24,11 +24,11 @@ pub struct Decoder {
impl Decoder {
pub fn new_for_codec(codec: &str) -> Result<Decoder, String> {
- Ok(Decoder { codec: codec.to_string() })
+ Err(format!("video decoding ({codec}) is not yet supported on macOS"))
}
pub fn new_gpu(codec: &str) -> Result<Decoder, String> {
- Ok(Decoder { codec: codec.to_string() })
+ Self::new_for_codec(codec)
}
pub fn on_gpu(&self) -> bool {
@@ -40,7 +40,7 @@ impl Decoder {
}
pub fn decode(&mut self, _data: &[u8], _time_us: u64) -> Result<Vec<Picture>, String> {
- Ok(Vec::new())
+ Err("video decoding is not supported on macOS".into())
}
pub fn flush(&mut self) {}
@@ -54,3 +54,20 @@ pub fn to_bgrx(picture: &Picture, width: u32, height: u32) -> Vec<u8> {
pub fn ensure_mf_started() -> Result<(), String> {
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn h264_decoder_reports_unsupported_on_creation() {
+ assert!(Decoder::new_for_codec("h264").is_err());
+ assert!(Decoder::new_gpu("h264").is_err());
+ }
+
+ #[test]
+ fn h264_decode_reports_unsupported() {
+ let mut decoder = Decoder::default();
+ assert!(decoder.decode(&[0, 0, 0, 1], 0).is_err());
+ }
+}
diff --git a/desktop/app/src/mac/image.rs b/desktop/app/src/mac/image.rs
index af60e84..50833e0 100644
--- a/desktop/app/src/mac/image.rs
+++ b/desktop/app/src/mac/image.rs
@@ -9,12 +9,18 @@ pub struct Bitmap {
}
pub fn decode(bytes: &[u8]) -> Result<Bitmap, String> {
- if bytes.len() >= 24 && &bytes[0..8] == b"\x89PNG\r\n\x1a\n" {
- let width = u32::from_be_bytes(bytes[16..20].try_into().unwrap_or([0; 4]));
- let height = u32::from_be_bytes(bytes[20..24].try_into().unwrap_or([0; 4]));
- return Ok(Bitmap { width, height, bgra: vec![0; (width * height * 4) as usize] });
+ if bytes.is_empty() {
+ return Err("empty image data".into());
+ }
+ let out = crate::bridge::native::ffi::decode_image(bytes);
+ if out.len() >= 8 {
+ let width = u32::from_le_bytes(out[0..4].try_into().unwrap_or([0; 4]));
+ let height = u32::from_le_bytes(out[4..8].try_into().unwrap_or([0; 4]));
+ let bgra = out[8..].to_vec();
+ Ok(Bitmap { width, height, bgra })
+ } else {
+ Err("failed to decode image".into())
}
- Ok(Bitmap { width: 0, height: 0, bgra: Vec::new() })
}
pub fn encode_png(bitmap: &Bitmap) -> Result<Vec<u8>, String> {
@@ -140,3 +146,33 @@ fn write_chunk(out: &mut Vec<u8>, tag: &[u8; 4], data: &[u8]) {
}
out.extend_from_slice(&(crc ^ 0xffff_ffff_u32).to_be_bytes());
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn decode_rejects_empty_data() {
+ assert!(decode(&[]).is_err());
+ }
+
+ #[test]
+ fn encode_and_decode_roundtrip_preserves_pixels() {
+ let original = Bitmap {
+ width: 2,
+ height: 2,
+ bgra: vec![
+ 255, 0, 0, 255, // blue
+ 0, 255, 0, 255, // green
+ 0, 0, 255, 255, // red
+ 255, 255, 255, 255, // white
+ ],
+ };
+ let png = encode_png(&original).expect("encode_png failed");
+ let decoded = decode(&png).expect("decode failed");
+ assert_eq!(decoded.width, 2);
+ assert_eq!(decoded.height, 2);
+ assert_eq!(decoded.bgra.len(), 16);
+ assert_eq!(decoded.bgra, original.bgra);
+ }
+}
diff --git a/desktop/app/src/mac/mp4.rs b/desktop/app/src/mac/mp4.rs
index 7db909e..84e171d 100644
--- a/desktop/app/src/mac/mp4.rs
+++ b/desktop/app/src/mac/mp4.rs
@@ -51,18 +51,38 @@ impl MirrorRecorder {
pub fn on_audio_config(&mut self, _rate: u32, _channels: u8) {}
pub fn on_packet(&mut self, _keyframe: bool, _time_us: u64, _data: &[u8]) -> Result<(), String> {
- Ok(())
+ Err("screen recording is not supported on macOS".into())
}
pub fn on_audio(&mut self, _time_us: u64, _pcm: &[u8]) -> Result<(), String> {
- Ok(())
+ Err("audio recording is not supported on macOS".into())
}
pub fn finish(self) -> Result<PathBuf, String> {
- Ok(self.path)
+ Err("screen recording is not supported on macOS".into())
}
}
pub fn merge_sps_pps(sps_pps: &mut Vec<u8>, data: &[u8]) {
sps_pps.extend_from_slice(data);
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn mp4_recorder_reports_unsupported() {
+ let mut rec = MirrorRecorder::new(PathBuf::from("/tmp/dummy.mp4"), 1920, 1080, 60, None);
+ assert!(rec.on_packet(true, 0, &[1, 2, 3]).is_err());
+ assert!(rec.on_audio(0, &[1, 2, 3]).is_err());
+ assert!(rec.finish().is_err());
+ }
+
+ #[test]
+ fn local_timestamp_filename_formats_correctly() {
+ let name = local_timestamp_filename("Screen", "mp4");
+ assert!(name.starts_with("Screen "));
+ assert!(name.ends_with(".mp4"));
+ }
+}
diff --git a/desktop/app/src/main.rs b/desktop/app/src/main.rs
index 7723b5d..f7c1c13 100644
--- a/desktop/app/src/main.rs
+++ b/desktop/app/src/main.rs
@@ -7,7 +7,6 @@
// A GUI app: no console window in release builds.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
-#![cfg_attr(not(windows), allow(unused))]
mod automations;
mod battery;
@@ -59,12 +58,9 @@ mod updater;
mod webcam;
#[cfg(windows)]
mod win;
-
-#[cfg(target_os = "macos")]
-mod mac;
-
#[cfg(target_os = "macos")]
-use mac as win;
+#[path = "mac/mod.rs"]
+mod win;
use std::{
path::{Path, PathBuf},
diff --git a/desktop/app/src/media_file.rs b/desktop/app/src/media_file.rs
index 9868588..c5a0ae8 100644
--- a/desktop/app/src/media_file.rs
+++ b/desktop/app/src/media_file.rs
@@ -4,6 +4,8 @@
//! shortcut to every file opened lately (the Recent folder), and the
//! player's title or its window's names the file.
+#![cfg_attr(not(windows), allow(dead_code))]
+
use std::path::{Path, PathBuf};
use crate::win::{browser, shortcut};
diff --git a/desktop/app/src/mirror.rs b/desktop/app/src/mirror.rs
index 16ba27d..8099443 100644
--- a/desktop/app/src/mirror.rs
+++ b/desktop/app/src/mirror.rs
@@ -1354,6 +1354,7 @@ pub fn leave_window() {
}
/// Records text that arrived from a remote device so focus/hover sync never echoes it back.
+#[allow(dead_code)]
pub fn note_remote_clipboard(text: &str) {
let normalized = text.replace("\r\n", "\n");
state(|s| {
@@ -1404,10 +1405,13 @@ pub fn sync_clipboard_on_focus(window: Window) {
}
/// Standard XInput / Xbox left thumbstick deadzone.
+#[allow(dead_code)]
pub const LEFT_THUMB_DEADZONE: i16 = 7849;
/// Standard XInput / Xbox right thumbstick deadzone.
+#[allow(dead_code)]
pub const RIGHT_THUMB_DEADZONE: i16 = 8689;
/// Standard XInput / Xbox trigger threshold.
+#[allow(dead_code)]
pub const TRIGGER_THRESHOLD: u8 = 30;
/// Normalized controller snapshot forwarded via [`nectarlink_core::MirrorInput::Gamepad`].
@@ -1437,12 +1441,14 @@ impl GamepadState {
}
}
+#[allow(dead_code)]
fn deadzone_axis(val: i16, deadzone: i16) -> i16 {
if val.saturating_abs() <= deadzone { 0 } else { val }
}
/// Applies stick deadzones and trigger thresholds so idle controller noise does
/// not generate redundant [`nectarlink_core::MirrorInput::Gamepad`] frames.
+#[allow(dead_code)]
pub fn normalize_gamepad_state(raw: GamepadState) -> GamepadState {
GamepadState {
buttons: raw.buttons,
diff --git a/desktop/app/src/pc_audio_stream.rs b/desktop/app/src/pc_audio_stream.rs
new file mode 100644
index 0000000..5cd0a97
--- /dev/null
+++ b/desktop/app/src/pc_audio_stream.rs
@@ -0,0 +1,428 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//! Phone as Speaker / Headphones for PC Audio (§3.8 P2): captures Windows
+//! system audio via WASAPI loopback (`AUDCLNT_STREAMFLAGS_LOOPBACK`), converts
+//! 32-bit float stereo samples to 16-bit signed little-endian PCM, and streams
+//! them to connected phones over NectarLink's low-latency audio stream.
+
+#[cfg(windows)]
+mod imp {
+
+ #![allow(unsafe_code)]
+
+ use std::{
+ sync::atomic::{AtomicBool, Ordering},
+ time::Duration,
+ };
+
+ use windows::Win32::{
+ Media::Audio::{
+ AUDCLNT_BUFFERFLAGS_SILENT, AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK,
+ IAudioCaptureClient, IAudioClient, IMMDeviceEnumerator, MMDeviceEnumerator, WAVEFORMATEX,
+ eConsole, eRender,
+ },
+ System::{
+ Com::{CLSCTX_ALL, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx, CoTaskMemFree},
+ Threading::{GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_TIME_CRITICAL},
+ },
+ };
+
+ use crate::core_host;
+
+ static ENABLED: AtomicBool = AtomicBool::new(false);
+ static WORKER_RUNNING: AtomicBool = AtomicBool::new(false);
+ /// The phone that last turned "Phone speaker" on. Audio goes there while it's online;
+ /// when the PC turned it on (or that phone is away), to the first phone that is.
+ static TARGET: std::sync::Mutex<Option<nectarlink_core::DeviceId>> = std::sync::Mutex::new(None);
+
+ /// The phone the sound goes to first, if one asked.
+ pub fn target() -> Option<nectarlink_core::DeviceId> {
+ *TARGET.lock().unwrap_or_else(|e| e.into_inner())
+ }
+
+ /// Records which phone asked to be the speaker.
+ pub fn set_target(phone: Option<nectarlink_core::DeviceId>) {
+ *TARGET.lock().unwrap_or_else(|e| e.into_inner()) = phone;
+ }
+
+ /// Returns whether PC-to-phone audio streaming is currently enabled.
+ pub fn is_enabled() -> bool {
+ ENABLED.load(Ordering::SeqCst)
+ }
+
+ /// Enables or disables PC-to-phone audio loopback streaming.
+ pub fn set_enabled(enabled: bool) {
+ let prev = ENABLED.swap(enabled, Ordering::SeqCst);
+ if enabled && !prev && core_host::node().is_some() {
+ ensure_worker();
+ }
+ }
+
+ /// Converts interleaved stereo `f32` samples (`[-1.0, 1.0]`) into 16-bit
+ /// signed little-endian PCM bytes (`pcm_s16le`).
+ #[cfg(test)]
+ pub fn f32_stereo_to_i16le(samples: &[f32]) -> Vec<u8> {
+ let mut out = Vec::with_capacity(samples.len() * 2);
+ for &sample in samples {
+ let clean = if sample.is_finite() { sample.clamp(-1.0, 1.0) } else { 0.0 };
+ let scaled = (clean * 32767.0).round() as i16;
+ out.extend_from_slice(&scaled.to_le_bytes());
+ }
+ out
+ }
+
+ #[derive(Clone, Copy)]
+ struct Biquad {
+ b0: f32,
+ b1: f32,
+ b2: f32,
+ a1: f32,
+ a2: f32,
+ z1_l: f32,
+ z2_l: f32,
+ z1_r: f32,
+ z2_r: f32,
+ }
+
+ impl Biquad {
+ fn low_shelf(sample_rate: f32, freq: f32, gain_db: f32) -> Self {
+ let a = 10.0_f32.powf(gain_db / 40.0);
+ let w0 = 2.0 * std::f32::consts::PI * (freq / sample_rate.max(8_000.0)).clamp(0.001, 0.45);
+ let cos_w0 = w0.cos();
+ let sin_w0 = w0.sin();
+ let alpha = sin_w0 / 2.0 * std::f32::consts::SQRT_2;
+ let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
+
+ let b0 = a * ((a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha);
+ let b1 = 2.0 * a * ((a - 1.0) - (a + 1.0) * cos_w0);
+ let b2 = a * ((a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha);
+ let a0 = (a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha;
+ let a1 = -2.0 * ((a - 1.0) + (a + 1.0) * cos_w0);
+ let a2 = (a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha;
+
+ Self {
+ b0: b0 / a0,
+ b1: b1 / a0,
+ b2: b2 / a0,
+ a1: a1 / a0,
+ a2: a2 / a0,
+ z1_l: 0.0,
+ z2_l: 0.0,
+ z1_r: 0.0,
+ z2_r: 0.0,
+ }
+ }
+
+ fn high_shelf(sample_rate: f32, freq: f32, gain_db: f32) -> Self {
+ let a = 10.0_f32.powf(gain_db / 40.0);
+ let w0 = 2.0 * std::f32::consts::PI * (freq / sample_rate.max(8_000.0)).clamp(0.001, 0.45);
+ let cos_w0 = w0.cos();
+ let sin_w0 = w0.sin();
+ let alpha = sin_w0 / 2.0 * std::f32::consts::SQRT_2;
+ let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
+
+ let b0 = a * ((a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha);
+ let b1 = -2.0 * a * ((a - 1.0) + (a + 1.0) * cos_w0);
+ let b2 = a * ((a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha);
+ let a0 = (a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha;
+ let a1 = 2.0 * ((a - 1.0) - (a + 1.0) * cos_w0);
+ let a2 = (a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha;
+
+ Self {
+ b0: b0 / a0,
+ b1: b1 / a0,
+ b2: b2 / a0,
+ a1: a1 / a0,
+ a2: a2 / a0,
+ z1_l: 0.0,
+ z2_l: 0.0,
+ z1_r: 0.0,
+ z2_r: 0.0,
+ }
+ }
+
+ #[inline]
+ fn process_stereo(&mut self, l: f32, r: f32) -> (f32, f32) {
+ let out_l = self.b0 * l + self.z1_l;
+ self.z1_l = self.b1 * l - self.a1 * out_l + self.z2_l;
+ self.z2_l = self.b2 * l - self.a2 * out_l;
+
+ let out_r = self.b0 * r + self.z1_r;
+ self.z1_r = self.b1 * r - self.a1 * out_r + self.z2_r;
+ self.z2_r = self.b2 * r - self.a2 * out_r;
+ (out_l, out_r)
+ }
+ }
+
+ struct SpeakerEnhancer {
+ low_shelf: Biquad,
+ high_shelf: Biquad,
+ }
+
+ impl SpeakerEnhancer {
+ fn new(sample_rate: u32) -> Self {
+ let sr = sample_rate as f32;
+ Self {
+ low_shelf: Biquad::low_shelf(sr, 155.0, 3.5),
+ high_shelf: Biquad::high_shelf(sr, 3800.0, 1.5),
+ }
+ }
+
+ #[inline]
+ fn soft_limit(x: f32) -> f32 {
+ if !x.is_finite() {
+ return 0.0;
+ }
+ let boosted = x * 1.12;
+ (boosted / (1.0 + 0.18 * boosted.abs())).clamp(-1.0, 1.0)
+ }
+
+ fn encode_stereo_f32(&mut self, floats: &[f32], channels: usize) -> Vec<u8> {
+ let frames = floats.len() / channels.max(1);
+ let mut out = Vec::with_capacity(frames * 4);
+ for frame in floats.chunks_exact(channels.max(1)) {
+ let l_in = if frame[0].is_finite() { frame[0] } else { 0.0 };
+ let r_in = if channels > 1 && frame[1].is_finite() { frame[1] } else { l_in };
+ let (l1, r1) = self.low_shelf.process_stereo(l_in, r_in);
+ let (l2, r2) = self.high_shelf.process_stereo(l1, r1);
+ let l_i16 = (Self::soft_limit(l2) * 32767.0).round() as i16;
+ let r_i16 = (Self::soft_limit(r2) * 32767.0).round() as i16;
+ out.extend_from_slice(&l_i16.to_le_bytes());
+ out.extend_from_slice(&r_i16.to_le_bytes());
+ }
+ out
+ }
+
+ fn encode_stereo_i16(&mut self, samples: &[i16], channels: usize) -> Vec<u8> {
+ let frames = samples.len() / channels.max(1);
+ let mut out = Vec::with_capacity(frames * 4);
+ for frame in samples.chunks_exact(channels.max(1)) {
+ let l_in = (frame[0] as f32) / 32768.0;
+ let r_in = if channels > 1 { (frame[1] as f32) / 32768.0 } else { l_in };
+ let (l1, r1) = self.low_shelf.process_stereo(l_in, r_in);
+ let (l2, r2) = self.high_shelf.process_stereo(l1, r1);
+ let l_i16 = (Self::soft_limit(l2) * 32767.0).round() as i16;
+ let r_i16 = (Self::soft_limit(r2) * 32767.0).round() as i16;
+ out.extend_from_slice(&l_i16.to_le_bytes());
+ out.extend_from_slice(&r_i16.to_le_bytes());
+ }
+ out
+ }
+ }
+
+ fn ensure_worker() {
+ if WORKER_RUNNING.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_err() {
+ return;
+ }
+ std::thread::Builder::new()
+ .name("nectarlink-pc-audio-loopback".into())
+ .spawn(|| {
+ run_loopback_worker();
+ WORKER_RUNNING.store(false, Ordering::SeqCst);
+ })
+ .ok();
+ }
+
+ fn run_loopback_worker() {
+ // SAFETY: Initialize COM and elevate thread priority for low-latency audio capture.
+ unsafe {
+ let _ = CoInitializeEx(None, COINIT_MULTITHREADED);
+ let _ = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
+ }
+
+ while is_enabled() {
+ if let Err(e) = capture_and_stream_once() {
+ tracing::debug!(error = %e, "PC audio loopback stream cycle ended");
+ std::thread::sleep(Duration::from_millis(500));
+ }
+ }
+ }
+
+ fn capture_and_stream_once() -> Result<(), String> {
+ let node = core_host::node().ok_or_else(|| "core node not running".to_owned())?;
+ let peers: Vec<_> = node
+ .paired_devices()
+ .unwrap_or_default()
+ .into_iter()
+ .filter(|d| matches!(d.link, nectarlink_core::LinkState::Online { .. }))
+ .map(|d| d.id)
+ .collect();
+ let wanted = *TARGET.lock().unwrap_or_else(|e| e.into_inner());
+ let Some(&peer) = wanted.as_ref().filter(|w| peers.contains(w)).or_else(|| peers.first()) else {
+ std::thread::sleep(Duration::from_millis(500));
+ return Ok(());
+ };
+
+ // SAFETY: WASAPI loopback initialization on the default render endpoint.
+ let (client, capture, rate, channels, bits_per_sample) = unsafe {
+ let enumerator: IMMDeviceEnumerator =
+ CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).map_err(|e| e.to_string())?;
+ let device = enumerator.GetDefaultAudioEndpoint(eRender, eConsole).map_err(|e| e.to_string())?;
+ let client: IAudioClient = device.Activate(CLSCTX_ALL, None).map_err(|e| e.to_string())?;
+ let mix_ptr: *mut WAVEFORMATEX = client.GetMixFormat().map_err(|e| e.to_string())?;
+ if mix_ptr.is_null() {
+ return Err("GetMixFormat returned null".into());
+ }
+ let mix = *mix_ptr;
+ let rate = mix.nSamplesPerSec.clamp(8_000, 96_000);
+ let channels = mix.nChannels.max(1) as usize;
+ let bits = mix.wBitsPerSample;
+ // 10 ms shared loopback buffer in 100-ns units = 100_000.
+ let init_res = client.Initialize(
+ AUDCLNT_SHAREMODE_SHARED,
+ AUDCLNT_STREAMFLAGS_LOOPBACK,
+ 100_000,
+ 0,
+ mix_ptr,
+ None,
+ );
+ CoTaskMemFree(Some(mix_ptr.cast()));
+ init_res.map_err(|e| e.to_string())?;
+ let capture: IAudioCaptureClient = client.GetService().map_err(|e| e.to_string())?;
+ client.Start().map_err(|e| e.to_string())?;
+ (client, capture, rate, channels, bits)
+ };
+
+ let mut enhancer = SpeakerEnhancer::new(rate);
+ let (tx, mut rx) = tokio::sync::mpsc::channel::<(u32, u8, Vec<u8>)>(64);
+ let send_node = node.clone();
+ core_host::spawn(async move {
+ while let Some((r, ch, pcm)) = rx.recv().await {
+ // Slice into 960-byte (5 ms at 48 kHz stereo) sub-MTU chunks so each
+ // chunk travels in a single QUIC datagram without HOL blocking.
+ for chunk in pcm.chunks(960) {
+ if send_node.send_pc_audio_pcm(&peer, r, ch, chunk.to_vec()).await.is_err() {
+ return;
+ }
+ }
+ }
+ });
+
+ // Keep sending zero-filled PCM for ~500 ms after audible audio stops so brief
+ // pauses between words or beats never starve the phone's AudioTrack ringbuffer.
+ let mut silence_tail_packets: u32 = 0;
+
+ 'capture: while is_enabled() {
+ std::thread::sleep(Duration::from_millis(2));
+ // Another phone asked for the sound (it watches the PC's playback):
+ // start over for that one.
+ if target() != wanted && target().is_some_and(|t| peers.contains(&t)) {
+ break 'capture;
+ }
+ // SAFETY: Drain available capture packets from `IAudioCaptureClient`.
+ unsafe {
+ loop {
+ let Ok(packet_frames) = capture.GetNextPacketSize() else { break };
+ if packet_frames == 0 {
+ break;
+ }
+ let mut data_ptr: *mut u8 = std::ptr::null_mut();
+ let mut frames_to_read: u32 = 0;
+ let mut flags: u32 = 0;
+ if capture.GetBuffer(&mut data_ptr, &mut frames_to_read, &mut flags, None, None).is_err()
+ {
+ break;
+ }
+ let silent = (flags & (AUDCLNT_BUFFERFLAGS_SILENT.0 as u32)) != 0 || data_ptr.is_null();
+ let frames = frames_to_read as usize;
+ let pcm_bytes = if silent {
+ if silence_tail_packets > 0 {
+ silence_tail_packets -= 1;
+ vec![0u8; frames * 4]
+ } else {
+ Vec::new()
+ }
+ } else if bits_per_sample == 32 {
+ let floats = std::slice::from_raw_parts(data_ptr.cast::<f32>(), frames * channels);
+ let has_signal = floats.iter().any(|&s| s.is_finite() && s.abs() > 1e-5);
+ if has_signal {
+ silence_tail_packets = 50;
+ enhancer.encode_stereo_f32(floats, channels)
+ } else if silence_tail_packets > 0 {
+ silence_tail_packets -= 1;
+ vec![0u8; frames * 4]
+ } else {
+ Vec::new()
+ }
+ } else if bits_per_sample == 16 {
+ let i16s = std::slice::from_raw_parts(data_ptr.cast::<i16>(), frames * channels);
+ let has_signal = i16s.iter().any(|&s| s.unsigned_abs() > 2);
+ if has_signal {
+ silence_tail_packets = 50;
+ enhancer.encode_stereo_i16(i16s, channels)
+ } else if silence_tail_packets > 0 {
+ silence_tail_packets -= 1;
+ vec![0u8; frames * 4]
+ } else {
+ Vec::new()
+ }
+ } else {
+ Vec::new()
+ };
+ let _ = capture.ReleaseBuffer(frames_to_read);
+
+ if !pcm_bytes.is_empty()
+ && matches!(
+ tx.try_send((rate, 2, pcm_bytes)),
+ Err(tokio::sync::mpsc::error::TrySendError::Closed(_))
+ )
+ {
+ break 'capture;
+ }
+ }
+ }
+ }
+
+ // SAFETY: Stop the WASAPI client before dropping.
+ unsafe {
+ let _ = client.Stop();
+ }
+ Ok(())
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ #[test]
+ fn f32_stereo_to_i16le_clamps_and_encodes_little_endian() {
+ let samples = [0.0_f32, 1.0, -1.0, 2.5, -3.0, f32::NAN];
+ let bytes = f32_stereo_to_i16le(&samples);
+ assert_eq!(bytes.len(), samples.len() * 2);
+ let decoded: Vec<i16> = bytes.as_chunks::<2>().0.iter().map(|&c| i16::from_le_bytes(c)).collect();
+ assert_eq!(decoded, vec![0, 32767, -32767, 32767, -32767, 0]);
+ }
+
+ #[test]
+ fn set_enabled_toggles_atomic_state_cleanly() {
+ set_enabled(false);
+ assert!(!is_enabled());
+ set_enabled(true);
+ assert!(is_enabled());
+ set_enabled(false);
+ assert!(!is_enabled());
+ }
+ }
+}
+
+#[cfg(not(windows))]
+mod imp {
+ #![allow(dead_code)]
+
+ pub fn target() -> Option<nectarlink_core::DeviceId> {
+ None
+ }
+
+ pub fn set_target(_phone: Option<nectarlink_core::DeviceId>) {}
+
+ pub fn is_enabled() -> bool {
+ false
+ }
+
+ pub fn set_enabled(_enabled: bool) {}
+
+ pub fn f32_stereo_to_i16le(_samples: &[f32]) -> Vec<u8> {
+ Vec::new()
+ }
+}
+
+pub use imp::*;
diff --git a/desktop/app/src/pc_audio_stream/mod.rs b/desktop/app/src/pc_audio_stream/mod.rs
deleted file mode 100644
index 8a28451..0000000
--- a/desktop/app/src/pc_audio_stream/mod.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Streaming PC loopback audio to the phone.
-
-#[cfg(windows)]
-mod windows;
-#[cfg(windows)]
-pub use self::windows::*;
-
-#[cfg(not(windows))]
-mod non_windows;
-#[cfg(not(windows))]
-pub use self::non_windows::*;
diff --git a/desktop/app/src/pc_audio_stream/non_windows.rs b/desktop/app/src/pc_audio_stream/non_windows.rs
deleted file mode 100644
index 211346e..0000000
--- a/desktop/app/src/pc_audio_stream/non_windows.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! macOS / non-Windows PC audio stream stubs.
-
-pub fn target() -> Option<nectarlink_core::DeviceId> {
- None
-}
-
-pub fn set_target(_phone: Option<nectarlink_core::DeviceId>) {}
-
-pub fn is_enabled() -> bool {
- false
-}
-
-pub fn set_enabled(_enabled: bool) {}
-
-pub fn f32_stereo_to_i16le(_samples: &[f32]) -> Vec<u8> {
- Vec::new()
-}
diff --git a/desktop/app/src/pc_audio_stream/windows.rs b/desktop/app/src/pc_audio_stream/windows.rs
deleted file mode 100644
index 03e7efb..0000000
--- a/desktop/app/src/pc_audio_stream/windows.rs
+++ /dev/null
@@ -1,397 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Phone as Speaker / Headphones for PC Audio (§3.8 P2): captures Windows
-//! system audio via WASAPI loopback (`AUDCLNT_STREAMFLAGS_LOOPBACK`), converts
-//! 32-bit float stereo samples to 16-bit signed little-endian PCM, and streams
-//! them to connected phones over NectarLink's low-latency audio stream.
-
-#![allow(unsafe_code)]
-
-use std::{
- sync::atomic::{AtomicBool, Ordering},
- time::Duration,
-};
-
-use windows::Win32::{
- Media::Audio::{
- AUDCLNT_BUFFERFLAGS_SILENT, AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK,
- IAudioCaptureClient, IAudioClient, IMMDeviceEnumerator, MMDeviceEnumerator, WAVEFORMATEX, eConsole,
- eRender,
- },
- System::{
- Com::{CLSCTX_ALL, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx, CoTaskMemFree},
- Threading::{GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_TIME_CRITICAL},
- },
-};
-
-use crate::core_host;
-
-static ENABLED: AtomicBool = AtomicBool::new(false);
-static WORKER_RUNNING: AtomicBool = AtomicBool::new(false);
-/// The phone that last turned "Phone speaker" on. Audio goes there while it's online;
-/// when the PC turned it on (or that phone is away), to the first phone that is.
-static TARGET: std::sync::Mutex<Option<nectarlink_core::DeviceId>> = std::sync::Mutex::new(None);
-
-/// The phone the sound goes to first, if one asked.
-pub fn target() -> Option<nectarlink_core::DeviceId> {
- *TARGET.lock().unwrap_or_else(|e| e.into_inner())
-}
-
-/// Records which phone asked to be the speaker.
-pub fn set_target(phone: Option<nectarlink_core::DeviceId>) {
- *TARGET.lock().unwrap_or_else(|e| e.into_inner()) = phone;
-}
-
-/// Returns whether PC-to-phone audio streaming is currently enabled.
-pub fn is_enabled() -> bool {
- ENABLED.load(Ordering::SeqCst)
-}
-
-/// Enables or disables PC-to-phone audio loopback streaming.
-pub fn set_enabled(enabled: bool) {
- let prev = ENABLED.swap(enabled, Ordering::SeqCst);
- if enabled && !prev && core_host::node().is_some() {
- ensure_worker();
- }
-}
-
-/// Converts interleaved stereo `f32` samples (`[-1.0, 1.0]`) into 16-bit
-/// signed little-endian PCM bytes (`pcm_s16le`).
-#[cfg(test)]
-pub fn f32_stereo_to_i16le(samples: &[f32]) -> Vec<u8> {
- let mut out = Vec::with_capacity(samples.len() * 2);
- for &sample in samples {
- let clean = if sample.is_finite() { sample.clamp(-1.0, 1.0) } else { 0.0 };
- let scaled = (clean * 32767.0).round() as i16;
- out.extend_from_slice(&scaled.to_le_bytes());
- }
- out
-}
-
-#[derive(Clone, Copy)]
-struct Biquad {
- b0: f32,
- b1: f32,
- b2: f32,
- a1: f32,
- a2: f32,
- z1_l: f32,
- z2_l: f32,
- z1_r: f32,
- z2_r: f32,
-}
-
-impl Biquad {
- fn low_shelf(sample_rate: f32, freq: f32, gain_db: f32) -> Self {
- let a = 10.0_f32.powf(gain_db / 40.0);
- let w0 = 2.0 * std::f32::consts::PI * (freq / sample_rate.max(8_000.0)).clamp(0.001, 0.45);
- let cos_w0 = w0.cos();
- let sin_w0 = w0.sin();
- let alpha = sin_w0 / 2.0 * std::f32::consts::SQRT_2;
- let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
-
- let b0 = a * ((a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha);
- let b1 = 2.0 * a * ((a - 1.0) - (a + 1.0) * cos_w0);
- let b2 = a * ((a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha);
- let a0 = (a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha;
- let a1 = -2.0 * ((a - 1.0) + (a + 1.0) * cos_w0);
- let a2 = (a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha;
-
- Self {
- b0: b0 / a0,
- b1: b1 / a0,
- b2: b2 / a0,
- a1: a1 / a0,
- a2: a2 / a0,
- z1_l: 0.0,
- z2_l: 0.0,
- z1_r: 0.0,
- z2_r: 0.0,
- }
- }
-
- fn high_shelf(sample_rate: f32, freq: f32, gain_db: f32) -> Self {
- let a = 10.0_f32.powf(gain_db / 40.0);
- let w0 = 2.0 * std::f32::consts::PI * (freq / sample_rate.max(8_000.0)).clamp(0.001, 0.45);
- let cos_w0 = w0.cos();
- let sin_w0 = w0.sin();
- let alpha = sin_w0 / 2.0 * std::f32::consts::SQRT_2;
- let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha;
-
- let b0 = a * ((a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha);
- let b1 = -2.0 * a * ((a - 1.0) + (a + 1.0) * cos_w0);
- let b2 = a * ((a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha);
- let a0 = (a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha;
- let a1 = 2.0 * ((a - 1.0) - (a + 1.0) * cos_w0);
- let a2 = (a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha;
-
- Self {
- b0: b0 / a0,
- b1: b1 / a0,
- b2: b2 / a0,
- a1: a1 / a0,
- a2: a2 / a0,
- z1_l: 0.0,
- z2_l: 0.0,
- z1_r: 0.0,
- z2_r: 0.0,
- }
- }
-
- #[inline]
- fn process_stereo(&mut self, l: f32, r: f32) -> (f32, f32) {
- let out_l = self.b0 * l + self.z1_l;
- self.z1_l = self.b1 * l - self.a1 * out_l + self.z2_l;
- self.z2_l = self.b2 * l - self.a2 * out_l;
-
- let out_r = self.b0 * r + self.z1_r;
- self.z1_r = self.b1 * r - self.a1 * out_r + self.z2_r;
- self.z2_r = self.b2 * r - self.a2 * out_r;
- (out_l, out_r)
- }
-}
-
-struct SpeakerEnhancer {
- low_shelf: Biquad,
- high_shelf: Biquad,
-}
-
-impl SpeakerEnhancer {
- fn new(sample_rate: u32) -> Self {
- let sr = sample_rate as f32;
- Self { low_shelf: Biquad::low_shelf(sr, 155.0, 3.5), high_shelf: Biquad::high_shelf(sr, 3800.0, 1.5) }
- }
-
- #[inline]
- fn soft_limit(x: f32) -> f32 {
- if !x.is_finite() {
- return 0.0;
- }
- let boosted = x * 1.12;
- (boosted / (1.0 + 0.18 * boosted.abs())).clamp(-1.0, 1.0)
- }
-
- fn encode_stereo_f32(&mut self, floats: &[f32], channels: usize) -> Vec<u8> {
- let frames = floats.len() / channels.max(1);
- let mut out = Vec::with_capacity(frames * 4);
- for frame in floats.chunks_exact(channels.max(1)) {
- let l_in = if frame[0].is_finite() { frame[0] } else { 0.0 };
- let r_in = if channels > 1 && frame[1].is_finite() { frame[1] } else { l_in };
- let (l1, r1) = self.low_shelf.process_stereo(l_in, r_in);
- let (l2, r2) = self.high_shelf.process_stereo(l1, r1);
- let l_i16 = (Self::soft_limit(l2) * 32767.0).round() as i16;
- let r_i16 = (Self::soft_limit(r2) * 32767.0).round() as i16;
- out.extend_from_slice(&l_i16.to_le_bytes());
- out.extend_from_slice(&r_i16.to_le_bytes());
- }
- out
- }
-
- fn encode_stereo_i16(&mut self, samples: &[i16], channels: usize) -> Vec<u8> {
- let frames = samples.len() / channels.max(1);
- let mut out = Vec::with_capacity(frames * 4);
- for frame in samples.chunks_exact(channels.max(1)) {
- let l_in = (frame[0] as f32) / 32768.0;
- let r_in = if channels > 1 { (frame[1] as f32) / 32768.0 } else { l_in };
- let (l1, r1) = self.low_shelf.process_stereo(l_in, r_in);
- let (l2, r2) = self.high_shelf.process_stereo(l1, r1);
- let l_i16 = (Self::soft_limit(l2) * 32767.0).round() as i16;
- let r_i16 = (Self::soft_limit(r2) * 32767.0).round() as i16;
- out.extend_from_slice(&l_i16.to_le_bytes());
- out.extend_from_slice(&r_i16.to_le_bytes());
- }
- out
- }
-}
-
-fn ensure_worker() {
- if WORKER_RUNNING.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_err() {
- return;
- }
- std::thread::Builder::new()
- .name("nectarlink-pc-audio-loopback".into())
- .spawn(|| {
- run_loopback_worker();
- WORKER_RUNNING.store(false, Ordering::SeqCst);
- })
- .ok();
-}
-
-fn run_loopback_worker() {
- // SAFETY: Initialize COM and elevate thread priority for low-latency audio capture.
- unsafe {
- let _ = CoInitializeEx(None, COINIT_MULTITHREADED);
- let _ = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
- }
-
- while is_enabled() {
- if let Err(e) = capture_and_stream_once() {
- tracing::debug!(error = %e, "PC audio loopback stream cycle ended");
- std::thread::sleep(Duration::from_millis(500));
- }
- }
-}
-
-fn capture_and_stream_once() -> Result<(), String> {
- let node = core_host::node().ok_or_else(|| "core node not running".to_owned())?;
- let peers: Vec<_> = node
- .paired_devices()
- .unwrap_or_default()
- .into_iter()
- .filter(|d| matches!(d.link, nectarlink_core::LinkState::Online { .. }))
- .map(|d| d.id)
- .collect();
- let wanted = *TARGET.lock().unwrap_or_else(|e| e.into_inner());
- let Some(&peer) = wanted.as_ref().filter(|w| peers.contains(w)).or_else(|| peers.first()) else {
- std::thread::sleep(Duration::from_millis(500));
- return Ok(());
- };
-
- // SAFETY: WASAPI loopback initialization on the default render endpoint.
- let (client, capture, rate, channels, bits_per_sample) = unsafe {
- let enumerator: IMMDeviceEnumerator =
- CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).map_err(|e| e.to_string())?;
- let device = enumerator.GetDefaultAudioEndpoint(eRender, eConsole).map_err(|e| e.to_string())?;
- let client: IAudioClient = device.Activate(CLSCTX_ALL, None).map_err(|e| e.to_string())?;
- let mix_ptr: *mut WAVEFORMATEX = client.GetMixFormat().map_err(|e| e.to_string())?;
- if mix_ptr.is_null() {
- return Err("GetMixFormat returned null".into());
- }
- let mix = *mix_ptr;
- let rate = mix.nSamplesPerSec.clamp(8_000, 96_000);
- let channels = mix.nChannels.max(1) as usize;
- let bits = mix.wBitsPerSample;
- // 10 ms shared loopback buffer in 100-ns units = 100_000.
- let init_res = client.Initialize(
- AUDCLNT_SHAREMODE_SHARED,
- AUDCLNT_STREAMFLAGS_LOOPBACK,
- 100_000,
- 0,
- mix_ptr,
- None,
- );
- CoTaskMemFree(Some(mix_ptr.cast()));
- init_res.map_err(|e| e.to_string())?;
- let capture: IAudioCaptureClient = client.GetService().map_err(|e| e.to_string())?;
- client.Start().map_err(|e| e.to_string())?;
- (client, capture, rate, channels, bits)
- };
-
- let mut enhancer = SpeakerEnhancer::new(rate);
- let (tx, mut rx) = tokio::sync::mpsc::channel::<(u32, u8, Vec<u8>)>(64);
- let send_node = node.clone();
- core_host::spawn(async move {
- while let Some((r, ch, pcm)) = rx.recv().await {
- // Slice into 960-byte (5 ms at 48 kHz stereo) sub-MTU chunks so each
- // chunk travels in a single QUIC datagram without HOL blocking.
- for chunk in pcm.chunks(960) {
- if send_node.send_pc_audio_pcm(&peer, r, ch, chunk.to_vec()).await.is_err() {
- return;
- }
- }
- }
- });
-
- // Keep sending zero-filled PCM for ~500 ms after audible audio stops so brief
- // pauses between words or beats never starve the phone's AudioTrack ringbuffer.
- let mut silence_tail_packets: u32 = 0;
-
- 'capture: while is_enabled() {
- std::thread::sleep(Duration::from_millis(2));
- // Another phone asked for the sound (it watches the PC's playback):
- // start over for that one.
- if target() != wanted && target().is_some_and(|t| peers.contains(&t)) {
- break 'capture;
- }
- // SAFETY: Drain available capture packets from `IAudioCaptureClient`.
- unsafe {
- loop {
- let Ok(packet_frames) = capture.GetNextPacketSize() else { break };
- if packet_frames == 0 {
- break;
- }
- let mut data_ptr: *mut u8 = std::ptr::null_mut();
- let mut frames_to_read: u32 = 0;
- let mut flags: u32 = 0;
- if capture.GetBuffer(&mut data_ptr, &mut frames_to_read, &mut flags, None, None).is_err() {
- break;
- }
- let silent = (flags & (AUDCLNT_BUFFERFLAGS_SILENT.0 as u32)) != 0 || data_ptr.is_null();
- let frames = frames_to_read as usize;
- let pcm_bytes = if silent {
- if silence_tail_packets > 0 {
- silence_tail_packets -= 1;
- vec![0u8; frames * 4]
- } else {
- Vec::new()
- }
- } else if bits_per_sample == 32 {
- let floats = std::slice::from_raw_parts(data_ptr.cast::<f32>(), frames * channels);
- let has_signal = floats.iter().any(|&s| s.is_finite() && s.abs() > 1e-5);
- if has_signal {
- silence_tail_packets = 50;
- enhancer.encode_stereo_f32(floats, channels)
- } else if silence_tail_packets > 0 {
- silence_tail_packets -= 1;
- vec![0u8; frames * 4]
- } else {
- Vec::new()
- }
- } else if bits_per_sample == 16 {
- let i16s = std::slice::from_raw_parts(data_ptr.cast::<i16>(), frames * channels);
- let has_signal = i16s.iter().any(|&s| s.unsigned_abs() > 2);
- if has_signal {
- silence_tail_packets = 50;
- enhancer.encode_stereo_i16(i16s, channels)
- } else if silence_tail_packets > 0 {
- silence_tail_packets -= 1;
- vec![0u8; frames * 4]
- } else {
- Vec::new()
- }
- } else {
- Vec::new()
- };
- let _ = capture.ReleaseBuffer(frames_to_read);
-
- if !pcm_bytes.is_empty()
- && matches!(
- tx.try_send((rate, 2, pcm_bytes)),
- Err(tokio::sync::mpsc::error::TrySendError::Closed(_))
- )
- {
- break 'capture;
- }
- }
- }
- }
-
- // SAFETY: Stop the WASAPI client before dropping.
- unsafe {
- let _ = client.Stop();
- }
- Ok(())
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn f32_stereo_to_i16le_clamps_and_encodes_little_endian() {
- let samples = [0.0_f32, 1.0, -1.0, 2.5, -3.0, f32::NAN];
- let bytes = f32_stereo_to_i16le(&samples);
- assert_eq!(bytes.len(), samples.len() * 2);
- let decoded: Vec<i16> = bytes.as_chunks::<2>().0.iter().map(|&c| i16::from_le_bytes(c)).collect();
- assert_eq!(decoded, vec![0, 32767, -32767, 32767, -32767, 0]);
- }
-
- #[test]
- fn set_enabled_toggles_atomic_state_cleanly() {
- set_enabled(false);
- assert!(!is_enabled());
- set_enabled(true);
- assert!(is_enabled());
- set_enabled(false);
- assert!(!is_enabled());
- }
-}
diff --git a/desktop/app/src/pc_mirror.rs b/desktop/app/src/pc_mirror.rs
new file mode 100644
index 0000000..218b412
--- /dev/null
+++ b/desktop/app/src/pc_mirror.rs
@@ -0,0 +1,1106 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//! Reverse Mirroring ("Your PC in Your Pocket"): captures the PC's primary
+//! display, encodes it to low-latency H.264 via Windows Media Foundation, and
+//! streams it over `mirror` / `video` (`session = 0`) to a paired phone, while
+//! injecting touch, scroll, key, and text input back into Windows (`docs/PLAN.md` §8.6).
+
+#[cfg(windows)]
+mod imp {
+
+ #![allow(unsafe_code)]
+
+ use std::{
+ mem::{ManuallyDrop, size_of},
+ sync::{
+ Arc, Mutex,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::{Duration, Instant},
+ };
+
+ use nectarlink_core::{
+ DeviceId, KeyMod, MirrorConfig, MirrorInput, MirrorSend, MirrorStart, PacketKind, mirror_keys,
+ };
+ use windows::Win32::{
+ Foundation::{POINT, RECT},
+ Graphics::Gdi::{
+ BI_RGB, BITMAPINFO, BITMAPINFOHEADER, CreateCompatibleBitmap, CreateCompatibleDC, DESKTOPHORZRES,
+ DESKTOPVERTRES, DeleteDC, DeleteObject, GetDC, GetDIBits, GetDeviceCaps, HALFTONE, ReleaseDC,
+ SRCCOPY, SelectObject, SetStretchBltMode, StretchBlt,
+ },
+ Media::MediaFoundation::{
+ CODECAPI_AVEncCommonMeanBitRate, CODECAPI_AVEncMPVGOPSize, CODECAPI_AVEncVideoForceKeyFrame,
+ CODECAPI_AVLowLatencyMode, ICodecAPI, IMFMediaBuffer, IMFMediaType, IMFSample, IMFTransform,
+ MF_E_NOTACCEPTING, MF_E_TRANSFORM_NEED_MORE_INPUT, MF_E_TRANSFORM_STREAM_CHANGE, MF_LOW_LATENCY,
+ MF_MT_AVG_BITRATE, MF_MT_FRAME_RATE, MF_MT_FRAME_SIZE, MF_MT_INTERLACE_MODE, MF_MT_MAJOR_TYPE,
+ MF_MT_MPEG_SEQUENCE_HEADER, MF_MT_PIXEL_ASPECT_RATIO, MF_MT_SUBTYPE, MFCreateMediaType,
+ MFCreateMemoryBuffer, MFCreateSample, MFMediaType_Video, MFSampleExtension_CleanPoint,
+ MFT_CATEGORY_VIDEO_ENCODER, MFT_MESSAGE_COMMAND_FLUSH, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING,
+ MFT_MESSAGE_NOTIFY_END_STREAMING, MFT_MESSAGE_NOTIFY_START_OF_STREAM, MFT_OUTPUT_DATA_BUFFER,
+ MFT_OUTPUT_STREAM_PROVIDES_SAMPLES, MFVideoFormat_H264, MFVideoFormat_NV12,
+ MFVideoInterlace_Progressive,
+ },
+ System::Variant::VARIANT,
+ UI::WindowsAndMessaging::{
+ CURSOR_SHOWING, CURSORINFO, DI_NORMAL, DrawIconEx, GetCursorInfo, GetSystemMetrics, SM_CXSCREEN,
+ SM_CYSCREEN,
+ },
+ };
+
+ use crate::{
+ core_host,
+ win::{
+ self,
+ h264::{create_mft, ensure_mf_started},
+ },
+ };
+
+ struct ActiveSession {
+ peer: DeviceId,
+ stop: Arc<AtomicBool>,
+ force_keyframe: Arc<AtomicBool>,
+ /// Play on phone: the phone watches a player, and its input is ignored.
+ watching: bool,
+ }
+
+ static ACTIVE: Mutex<Option<ActiveSession>> = Mutex::new(None);
+
+ /// How long Play on phone looks for the player's window before streaming
+ /// the screen instead.
+ const FIND_WINDOW: Duration = Duration::from_secs(5);
+
+ /// The lowest bitrate the stream backs off to on a slow link.
+ const MIN_BITRATE: u32 = 600_000;
+
+ /// What a sharing session sent in the last stats window.
+ #[derive(Default)]
+ struct Stats {
+ frames: u64,
+ keyframes: u64,
+ bytes: u64,
+ dropped: u64,
+ rebuilds: u64,
+ }
+
+ /// Returns the device currently viewing this PC's screen, if any.
+ pub fn active_peer() -> Option<DeviceId> {
+ ACTIVE.lock().unwrap_or_else(|e| e.into_inner()).as_ref().map(|s| s.peer)
+ }
+
+ /// Whether this PC's screen is currently being shared with a phone.
+ pub fn is_active() -> bool {
+ active_peer().is_some()
+ }
+
+ /// Whether a phone watches one of this PC's players (Play on phone).
+ pub fn is_watching() -> bool {
+ 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.
+ 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);
+ if let Some(peer) = peer {
+ stop_for(&peer);
+ }
+ }
+
+ /// Starts or replaces the active PC-to-phone screen mirror stream for `peer`.
+ pub fn start(peer: DeviceId, options: MirrorStart) {
+ stop_all();
+ let stop = Arc::new(AtomicBool::new(false));
+ let force_keyframe = Arc::new(AtomicBool::new(false));
+ {
+ let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
+ *guard = Some(ActiveSession {
+ peer,
+ stop: Arc::clone(&stop),
+ force_keyframe: Arc::clone(&force_keyframe),
+ watching: options.watch.is_some(),
+ });
+ }
+ core_host::host().hub.changed(crate::state::Changes::REMOTE);
+ core_host::refresh_capabilities();
+
+ std::thread::Builder::new()
+ .name("nectarlink-pc-mirror".into())
+ .spawn(move || run_capture_loop(peer, options, stop, force_keyframe))
+ .ok();
+ }
+
+ /// Stops PC-to-phone mirroring if `peer` matches the active viewer.
+ pub fn stop_for(peer: &DeviceId) {
+ let stopped = {
+ let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
+ if guard.as_ref().is_some_and(|s| &s.peer == peer)
+ && let Some(s) = guard.take()
+ {
+ s.stop.store(true, Ordering::SeqCst);
+ true
+ } else {
+ false
+ }
+ };
+ if stopped {
+ win::input::release_held_buttons();
+ crate::input_share::leave_remote();
+ core_host::host().hub.changed(crate::state::Changes::REMOTE);
+ core_host::refresh_capabilities();
+ }
+ }
+
+ /// Stops any active PC-to-phone mirroring session.
+ pub fn stop_all() {
+ let stopped = {
+ let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
+ if let Some(s) = guard.take() {
+ s.stop.store(true, Ordering::SeqCst);
+ Some(s.peer)
+ } else {
+ None
+ }
+ };
+ if let Some(peer) = stopped {
+ win::input::release_held_buttons();
+ crate::input_share::leave_remote();
+ if let Some(node) = core_host::node() {
+ node.set_pc_mirror_session_allowed(peer, false);
+ core_host::spawn(async move {
+ node.mirror_stop(peer, nectarlink_core::MIRROR_SCREEN).await;
+ });
+ }
+ core_host::host().hub.changed(crate::state::Changes::REMOTE);
+ core_host::refresh_capabilities();
+ }
+ }
+
+ /// Requests an immediate IDR keyframe for `peer`.
+ pub fn request_keyframe(peer: &DeviceId) {
+ let guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
+ if let Some(s) = guard.as_ref()
+ && &s.peer == peer
+ {
+ s.force_keyframe.store(true, Ordering::SeqCst);
+ }
+ }
+
+ /// Injects touch, scroll, key, or text input from a phone viewing the PC screen.
+ pub fn handle_input(peer: &DeviceId, session: u32, input: MirrorInput) {
+ if session != nectarlink_core::MIRROR_SCREEN {
+ return;
+ }
+ if active_peer() != Some(*peer) || is_watching() {
+ return;
+ }
+ let remote_input_on = core_host::node()
+ .and_then(|n| n.device_toggles(*peer).ok())
+ .is_some_and(|toggles| toggles.iter().any(|(k, v)| *k == "remote_input" && *v));
+ if !remote_input_on {
+ return;
+ }
+ let _ = crate::input_share::enter_remote(*peer);
+ match input {
+ MirrorInput::Touch { action, x, y } => {
+ win::input::mirror_touch(action, x, y);
+ }
+ MirrorInput::Scroll { x, y, dx, dy } => {
+ win::input::move_absolute(x, y);
+ win::input::scroll(dx, dy);
+ }
+ MirrorInput::Text { text } => {
+ win::input::type_text(&text);
+ }
+ MirrorInput::Key { key } => match key.as_str() {
+ mirror_keys::BACK => win::input::press_key("escape", &[]),
+ mirror_keys::HOME => win::input::press_key("a", &[KeyMod::Win]),
+ mirror_keys::RECENTS => win::input::press_key("task_view", &[]),
+ mirror_keys::ENTER => win::input::press_key("enter", &[]),
+ mirror_keys::BACKSPACE => win::input::press_key("backspace", &[]),
+ mirror_keys::DELETE => win::input::press_key("delete", &[]),
+ mirror_keys::LEFT => win::input::press_key("left", &[]),
+ mirror_keys::RIGHT => win::input::press_key("right", &[]),
+ mirror_keys::UP => win::input::press_key("up", &[]),
+ mirror_keys::DOWN => win::input::press_key("down", &[]),
+ mirror_keys::TAB => win::input::press_key("tab", &[]),
+ mirror_keys::NOTIFICATIONS => win::input::press_key("n", &[KeyMod::Win]),
+ _ => {}
+ },
+ MirrorInput::Gamepad { .. } => {}
+ }
+ }
+
+ pub fn on_event(event: &nectarlink_core::NodeEvent) {
+ match event {
+ nectarlink_core::NodeEvent::LinkChanged {
+ device,
+ link: nectarlink_core::LinkState::Offline { .. },
+ }
+ | nectarlink_core::NodeEvent::DeviceRemoved(device) => {
+ stop_for(device);
+ }
+ nectarlink_core::NodeEvent::PcMirrorRequested { .. } => {
+ core_host::host().hub.update(|s| s.apply(event));
+ crate::bridge::app::request_activation();
+ }
+ _ => {}
+ }
+ }
+
+ /// Computes target encoder dimensions (multiples of 16) fitting within `max_size`.
+ pub(crate) fn target_dimensions(src_w: u32, src_h: u32, max_size: u32) -> (u32, u32) {
+ let max_edge = max_size.clamp(320, 1920) as f64;
+ let sw = src_w.max(64) as f64;
+ let sh = src_h.max(64) as f64;
+ let longest = sw.max(sh);
+ let scale = if longest > max_edge { max_edge / longest } else { 1.0 };
+ let w = (((sw * scale).round() as u32) / 16).max(4) * 16;
+ let h = (((sh * scale).round() as u32) / 16).max(4) * 16;
+ (w, h)
+ }
+
+ /// Converts a top-down 32-bit BGRA image (`width * height * 4` bytes) into
+ /// BT.709 limited-range NV12 (`width * height * 3 / 2` bytes).
+ pub(crate) fn bgra_to_nv12(bgra: &[u8], width: usize, height: usize, nv12: &mut Vec<u8>) {
+ let y_size = width * height;
+ let uv_size = y_size / 2;
+ nv12.resize(y_size + uv_size, 0);
+ let (y_plane, uv_plane) = nv12.split_at_mut(y_size);
+
+ for row in 0..height {
+ let src_row = row * width * 4;
+ let y_row = row * width;
+ for col in 0..width {
+ let px = src_row + col * 4;
+ let b = i32::from(bgra[px]);
+ let g = i32::from(bgra[px + 1]);
+ let r = i32::from(bgra[px + 2]);
+ let y = ((47 * r + 157 * g + 16 * b + 128) >> 8) + 16;
+ y_plane[y_row + col] = y.clamp(16, 235) as u8;
+ }
+ }
+
+ for row in (0..height).step_by(2) {
+ let src0 = row * width * 4;
+ let src1 = (row + 1).min(height - 1) * width * 4;
+ let uv_row = (row / 2) * width;
+ for col in (0..width).step_by(2) {
+ let p00 = src0 + col * 4;
+ let p01 = src0 + (col + 1).min(width - 1) * 4;
+ let p10 = src1 + col * 4;
+ let p11 = src1 + (col + 1).min(width - 1) * 4;
+
+ let b = (i32::from(bgra[p00])
+ + i32::from(bgra[p01])
+ + i32::from(bgra[p10])
+ + i32::from(bgra[p11])
+ + 2)
+ >> 2;
+ let g = (i32::from(bgra[p00 + 1])
+ + i32::from(bgra[p01 + 1])
+ + i32::from(bgra[p10 + 1])
+ + i32::from(bgra[p11 + 1])
+ + 2)
+ >> 2;
+ let r = (i32::from(bgra[p00 + 2])
+ + i32::from(bgra[p01 + 2])
+ + i32::from(bgra[p10 + 2])
+ + i32::from(bgra[p11 + 2])
+ + 2)
+ >> 2;
+
+ let u = ((-26 * r - 87 * g + 112 * b + 128) >> 8) + 128;
+ let v = ((112 * r - 102 * g - 10 * b + 128) >> 8) + 128;
+ uv_plane[uv_row + col] = u.clamp(16, 240) as u8;
+ uv_plane[uv_row + col + 1] = v.clamp(16, 240) as u8;
+ }
+ }
+ }
+
+ pub(crate) struct H264Encoder {
+ transform: IMFTransform,
+ /// The encoder's settings interface, when it has one: keyframes on demand and live
+ /// bitrate changes without rebuilding the encoder.
+ codec_api: Option<ICodecAPI>,
+ width: u32,
+ height: u32,
+ provides_samples: bool,
+ output_size: u32,
+ seq_header: Vec<u8>,
+ sent_first_frame: bool,
+ }
+
+ impl Drop for H264Encoder {
+ fn drop(&mut self) {
+ // SAFETY: flushes and ends streaming on the MFT before drop.
+ unsafe {
+ let _ = self.transform.ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0);
+ let _ = self.transform.ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, 0);
+ }
+ }
+ }
+
+ impl H264Encoder {
+ pub(crate) fn new(width: u32, height: u32, fps: u32, bitrate: u32) -> windows::core::Result<Self> {
+ ensure_mf_started()?;
+ // SAFETY: Media Foundation COM calls with valid parameters.
+ unsafe {
+ let transform =
+ create_mft(MFT_CATEGORY_VIDEO_ENCODER, MFVideoFormat_NV12, MFVideoFormat_H264)?;
+ if let Ok(attrs) = transform.GetAttributes() {
+ let _ = attrs.SetUINT32(&MF_LOW_LATENCY, 1);
+ }
+
+ let out_type: IMFMediaType = MFCreateMediaType()?;
+ out_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
+ out_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_H264)?;
+ let _ = out_type.SetUINT32(
+ &windows::Win32::Media::MediaFoundation::MF_MT_MPEG2_PROFILE,
+ windows::Win32::Media::MediaFoundation::eAVEncH264VProfile_Base.0 as u32,
+ );
+ out_type.SetUINT32(&MF_MT_AVG_BITRATE, bitrate.clamp(500_000, 20_000_000))?;
+ out_type.SetUINT32(&MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive.0 as u32)?;
+ out_type.SetUINT64(&MF_MT_FRAME_SIZE, (u64::from(width) << 32) | u64::from(height))?;
+ out_type.SetUINT64(&MF_MT_FRAME_RATE, (u64::from(fps.max(1)) << 32) | 1)?;
+ out_type.SetUINT64(&MF_MT_PIXEL_ASPECT_RATIO, (1u64 << 32) | 1)?;
+ transform.SetOutputType(0, Some(&out_type), 0)?;
+
+ let in_type: IMFMediaType = MFCreateMediaType()?;
+ in_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
+ in_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_NV12)?;
+ in_type.SetUINT32(&MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive.0 as u32)?;
+ in_type.SetUINT64(&MF_MT_FRAME_SIZE, (u64::from(width) << 32) | u64::from(height))?;
+ in_type.SetUINT64(&MF_MT_FRAME_RATE, (u64::from(fps.max(1)) << 32) | 1)?;
+ in_type.SetUINT64(&MF_MT_PIXEL_ASPECT_RATIO, (1u64 << 32) | 1)?;
+ transform.SetInputType(0, Some(&in_type), 0)?;
+
+ let mut seq_header = Vec::new();
+ if let Ok(cur_out) = transform.GetOutputCurrentType(0)
+ && let Ok(len) = cur_out.GetBlobSize(&MF_MT_MPEG_SEQUENCE_HEADER)
+ && len > 0
+ {
+ let mut buf = vec![0u8; len as usize];
+ if cur_out.GetBlob(&MF_MT_MPEG_SEQUENCE_HEADER, &mut buf, None).is_ok() {
+ seq_header = buf;
+ }
+ }
+
+ let info = transform.GetOutputStreamInfo(0)?;
+ let provides_samples = (info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES.0 as u32)) != 0;
+ let output_size = info.cbSize.max(256 * 1024);
+
+ // A keyframe every two seconds keeps late joiners and lost packets recoverable
+ // without rebuilding the encoder, and low-latency mode stops it holding frames back.
+ let codec_api: Option<ICodecAPI> = windows::core::Interface::cast(&transform).ok();
+ if let Some(api) = codec_api.as_ref() {
+ let _ = api.SetValue(&CODECAPI_AVLowLatencyMode, &VARIANT::from(true));
+ let _ = api.SetValue(&CODECAPI_AVEncMPVGOPSize, &VARIANT::from(fps.max(1) * 2));
+ }
+
+ let _ = transform.ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
+ let _ = transform.ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
+
+ Ok(Self {
+ transform,
+ codec_api,
+ width,
+ height,
+ provides_samples,
+ output_size,
+ seq_header,
+ sent_first_frame: false,
+ })
+ }
+ }
+
+ /// Asks for the next frame to be a keyframe. False when the encoder can't, so the
+ /// caller must rebuild it instead.
+ pub(crate) fn request_keyframe(&self) -> bool {
+ // SAFETY: ICodecAPI::SetValue with a valid GUID and VARIANT.
+ self.codec_api.as_ref().is_some_and(|api| {
+ unsafe { api.SetValue(&CODECAPI_AVEncVideoForceKeyFrame, &VARIANT::from(1u32)) }.is_ok()
+ })
+ }
+
+ /// Changes the target bitrate in place. False when the encoder can't.
+ pub(crate) fn set_bitrate(&self, bitrate: u32) -> bool {
+ // SAFETY: ICodecAPI::SetValue with a valid GUID and VARIANT.
+ self.codec_api.as_ref().is_some_and(|api| {
+ unsafe { api.SetValue(&CODECAPI_AVEncCommonMeanBitRate, &VARIANT::from(bitrate)) }.is_ok()
+ })
+ }
+
+ /// Feeds one NV12 frame (`width * height * 3 / 2` bytes) and drains encoded
+ /// Annex-B H.264 access units `(is_keyframe, bytes)`.
+ pub(crate) fn encode(
+ &mut self,
+ nv12: &[u8],
+ hns: i64,
+ dur_hns: i64,
+ ) -> windows::core::Result<Vec<(bool, Vec<u8>)>> {
+ let expected = (self.width as usize) * (self.height as usize) * 3 / 2;
+ if nv12.len() < expected {
+ return Ok(Vec::new());
+ }
+ // SAFETY: Media Foundation sample creation and MFT ProcessInput/ProcessOutput.
+ unsafe {
+ let sample = make_sample(&nv12[..expected], hns, dur_hns)?;
+ let mut out = Vec::new();
+ match self.transform.ProcessInput(0, &sample, 0) {
+ Ok(()) => self.drain(&mut out)?,
+ Err(e) if e.code() == MF_E_NOTACCEPTING => {
+ self.drain(&mut out)?;
+ self.transform.ProcessInput(0, &sample, 0)?;
+ self.drain(&mut out)?;
+ }
+ Err(e) => return Err(e),
+ }
+ Ok(out)
+ }
+ }
+
+ unsafe fn drain(&mut self, out: &mut Vec<(bool, Vec<u8>)>) -> windows::core::Result<()> {
+ loop {
+ // SAFETY: MFT output draining with allocated or MFT-provided sample.
+ unsafe {
+ let allocated = if self.provides_samples {
+ None
+ } else {
+ let s: IMFSample = MFCreateSample()?;
+ let b: IMFMediaBuffer = MFCreateMemoryBuffer(self.output_size)?;
+ s.AddBuffer(&b)?;
+ Some(s)
+ };
+ let mut data = MFT_OUTPUT_DATA_BUFFER {
+ dwStreamID: 0,
+ pSample: ManuallyDrop::new(allocated),
+ dwStatus: 0,
+ pEvents: ManuallyDrop::new(None),
+ };
+ let mut status = 0u32;
+ let res = self.transform.ProcessOutput(0, std::slice::from_mut(&mut data), &mut status);
+ let sample = ManuallyDrop::take(&mut data.pSample);
+ let _ = ManuallyDrop::take(&mut data.pEvents);
+
+ match res {
+ Ok(()) => {
+ if let Some(sample) = sample {
+ let clean = sample
+ .GetUINT32(&MFSampleExtension_CleanPoint)
+ .map(|v| v != 0)
+ .unwrap_or(false);
+ let buf: IMFMediaBuffer = sample.ConvertToContiguousBuffer()?;
+ let mut ptr = std::ptr::null_mut();
+ let mut len = 0u32;
+ buf.Lock(&mut ptr, None, Some(&mut len))?;
+ let slice = std::slice::from_raw_parts(ptr, len as usize);
+ let is_idr = !self.sent_first_frame || clean || has_h264_idr(slice);
+ let mut bytes = Vec::new();
+ if is_idr && !has_h264_sps(slice) {
+ if self.seq_header.is_empty()
+ && let Ok(cur_out) = self.transform.GetOutputCurrentType(0)
+ && let Ok(hdr_len) = cur_out.GetBlobSize(&MF_MT_MPEG_SEQUENCE_HEADER)
+ && hdr_len > 0
+ {
+ let mut hdr = vec![0u8; hdr_len as usize];
+ if cur_out
+ .GetBlob(&MF_MT_MPEG_SEQUENCE_HEADER, &mut hdr, None)
+ .is_ok()
+ {
+ self.seq_header = hdr;
+ }
+ }
+ if !self.seq_header.is_empty() {
+ bytes.extend_from_slice(&self.seq_header);
+ }
+ }
+ bytes.extend_from_slice(slice);
+ buf.Unlock()?;
+ if !bytes.is_empty() {
+ self.sent_first_frame = true;
+ out.push((is_idr, bytes));
+ }
+ }
+ }
+ Err(e) if e.code() == MF_E_TRANSFORM_NEED_MORE_INPUT => return Ok(()),
+ Err(e) if e.code() == MF_E_TRANSFORM_STREAM_CHANGE => {
+ if let Ok(cur_out) = self.transform.GetOutputAvailableType(0, 0) {
+ self.transform.SetOutputType(0, Some(&cur_out), 0)?;
+ if let Ok(len) = cur_out.GetBlobSize(&MF_MT_MPEG_SEQUENCE_HEADER)
+ && len > 0
+ {
+ let mut buf = vec![0u8; len as usize];
+ if cur_out.GetBlob(&MF_MT_MPEG_SEQUENCE_HEADER, &mut buf, None).is_ok() {
+ self.seq_header = buf;
+ }
+ }
+ }
+ }
+ Err(e) => return Err(e),
+ }
+ }
+ }
+ }
+ }
+
+ unsafe fn make_sample(bytes: &[u8], hns: i64, dur_hns: i64) -> windows::core::Result<IMFSample> {
+ // SAFETY: allocates an IMFMediaBuffer of `bytes.len()` and copies `bytes` into it.
+ unsafe {
+ let buf: IMFMediaBuffer = MFCreateMemoryBuffer(bytes.len() as u32)?;
+ let mut dst = std::ptr::null_mut();
+ buf.Lock(&mut dst, None, None)?;
+ std::ptr::copy_nonoverlapping(bytes.as_ptr(), dst, bytes.len());
+ buf.Unlock()?;
+ buf.SetCurrentLength(bytes.len() as u32)?;
+ let sample: IMFSample = MFCreateSample()?;
+ sample.AddBuffer(&buf)?;
+ sample.SetSampleTime(hns)?;
+ sample.SetSampleDuration(dur_hns.max(1))?;
+ Ok(sample)
+ }
+ }
+
+ fn has_h264_idr(data: &[u8]) -> bool {
+ data.windows(4).any(|w| w[..3] == [0, 0, 1] && (w[3] & 0x1f) == 5)
+ }
+
+ fn has_h264_sps(data: &[u8]) -> bool {
+ data.windows(4).any(|w| w[..3] == [0, 0, 1] && (w[3] & 0x1f) == 7)
+ }
+
+ /// Captures the primary desktop monitor into `bgra` scaled to `(dst_w, dst_h)`,
+ /// with the pointer, and returns the current physical screen dimensions
+ /// `(src_w, src_h)`. With `region` (physical screen pixels), captures only
+ /// that, without the pointer (Play on phone), and returns its size.
+ fn capture_screen(
+ region: Option<RECT>,
+ dst_w: u32,
+ dst_h: u32,
+ bgra: &mut Vec<u8>,
+ ) -> Option<(u32, u32)> {
+ // SAFETY: Win32 GDI device context and bitmap calls with balanced cleanup.
+ unsafe {
+ let hdc_screen = GetDC(None);
+ if hdc_screen.is_invalid() {
+ return None;
+ }
+ let (src_x, src_y, src_w, src_h) = match region {
+ Some(r) => {
+ (r.left, r.top, (r.right - r.left).max(1) as u32, (r.bottom - r.top).max(1) as u32)
+ }
+ None => {
+ let phys_w = GetDeviceCaps(Some(hdc_screen), DESKTOPHORZRES);
+ let phys_h = GetDeviceCaps(Some(hdc_screen), DESKTOPVERTRES);
+ let w = if phys_w > 0 { phys_w } else { GetSystemMetrics(SM_CXSCREEN) }.max(1) as u32;
+ let h = if phys_h > 0 { phys_h } else { GetSystemMetrics(SM_CYSCREEN) }.max(1) as u32;
+ (0, 0, w, h)
+ }
+ };
+
+ let hdc_mem = CreateCompatibleDC(Some(hdc_screen));
+ if hdc_mem.is_invalid() {
+ let _ = ReleaseDC(None, hdc_screen);
+ return None;
+ }
+ let hbm = CreateCompatibleBitmap(hdc_screen, dst_w as i32, dst_h as i32);
+ if hbm.is_invalid() {
+ let _ = DeleteDC(hdc_mem);
+ let _ = ReleaseDC(None, hdc_screen);
+ return None;
+ }
+ let old = SelectObject(hdc_mem, hbm.into());
+ let _ = SetStretchBltMode(hdc_mem, HALFTONE);
+ let ok = StretchBlt(
+ hdc_mem,
+ 0,
+ 0,
+ dst_w as i32,
+ dst_h as i32,
+ Some(hdc_screen),
+ src_x,
+ src_y,
+ src_w as i32,
+ src_h as i32,
+ SRCCOPY,
+ )
+ .as_bool();
+
+ // Draw the mouse cursor at its scaled position so the phone user sees where they point.
+ let mut ci = CURSORINFO {
+ cbSize: size_of::<CURSORINFO>() as u32,
+ flags: Default::default(),
+ hCursor: Default::default(),
+ ptScreenPos: POINT::default(),
+ };
+ if region.is_none() && GetCursorInfo(&mut ci).is_ok() && (ci.flags.0 & CURSOR_SHOWING.0) != 0 {
+ let cx = ((ci.ptScreenPos.x as i64) * (dst_w as i64) / (src_w as i64)) as i32;
+ let cy = ((ci.ptScreenPos.y as i64) * (dst_h as i64) / (src_h as i64)) as i32;
+ let _ = DrawIconEx(hdc_mem, cx, cy, ci.hCursor.into(), 0, 0, 0, None, DI_NORMAL);
+ }
+
+ let needed = (dst_w as usize) * (dst_h as usize) * 4;
+ bgra.resize(needed, 0);
+
+ let mut bmi = BITMAPINFO {
+ bmiHeader: BITMAPINFOHEADER {
+ biSize: size_of::<BITMAPINFOHEADER>() as u32,
+ biWidth: dst_w as i32,
+ biHeight: -(dst_h as i32), // Top-down DIB.
+ biPlanes: 1,
+ biBitCount: 32,
+ biCompression: BI_RGB.0,
+ biSizeImage: needed as u32,
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+
+ let lines = GetDIBits(
+ hdc_mem,
+ hbm,
+ 0,
+ dst_h,
+ Some(bgra.as_mut_ptr().cast()),
+ &mut bmi,
+ windows::Win32::Graphics::Gdi::DIB_RGB_COLORS,
+ );
+
+ let _ = SelectObject(hdc_mem, old);
+ let _ = DeleteObject(hbm.into());
+ let _ = DeleteDC(hdc_mem);
+ let _ = ReleaseDC(None, hdc_screen);
+
+ if ok && lines > 0 { Some((src_w, src_h)) } else { None }
+ }
+ }
+
+ fn run_capture_loop(
+ peer: DeviceId,
+ options: MirrorStart,
+ stop: Arc<AtomicBool>,
+ force_keyframe: Arc<AtomicBool>,
+ ) {
+ let Some(node) = core_host::node() else {
+ cleanup_session(&stop);
+ return;
+ };
+ let runtime = &core_host::host().runtime;
+ let Ok(sender) = runtime.block_on(node.mirror_open(peer)) else {
+ tracing::warn!(%peer, "couldn't open PC mirror stream to phone");
+ cleanup_session(&stop);
+ return;
+ };
+
+ // Give the phone's PcMirrorView TextureView and MediaCodec a brief moment to
+ // mount before sending the initial MirrorConfig and IDR keyframe.
+ std::thread::sleep(Duration::from_millis(120));
+
+ let fps = options.fps.clamp(10, 60);
+ let max_bitrate = options.bitrate.clamp(500_000, 20_000_000);
+ // Lowered when the link can't keep up (frames dropped to hold the delay down),
+ // raised back once it has been clean for a while.
+ let mut bitrate = max_bitrate;
+ let frame_interval = Duration::from_micros(1_000_000 / u64::from(fps));
+ let dur_hns = (10_000_000 / u64::from(fps)) as i64;
+
+ let mut bgra = Vec::new();
+ let mut nv12 = Vec::new();
+ let mut stream_dims: Option<(u32, u32)> = None;
+ let mut encoder: Option<H264Encoder> = None;
+ let started = Instant::now();
+ let mut last_reinit = Instant::now() - Duration::from_secs(5);
+ let mut last_drop: Option<Instant> = None;
+ let mut last_rate_change = Instant::now();
+ let mut stats = Stats::default();
+ let mut stats_since = Instant::now();
+ // Play on phone: the player's picture instead of the screen, and the
+ // PC's sound to that phone while it watches. The picture's place comes
+ // in physical pixels; so does this thread's capture.
+ if options.watch.is_some() {
+ win::browser::use_physical_pixels();
+ }
+ let watched = options.watch.as_deref().map(crate::play_on_phone::Watched::follow);
+ let mut window_capture: Option<win::window_capture::WindowCapture> = None;
+ let _sound = watched.is_some().then(|| SoundToPhone::start(peer));
+ if watched.is_some() {
+ tracing::info!(%peer, fps, bitrate, "streaming a player to the phone");
+ } else {
+ tracing::info!(%peer, fps, bitrate, "sharing this PC's screen");
+ }
+
+ while !stop.load(Ordering::Relaxed) {
+ let tick_start = Instant::now();
+
+ let place = watched.as_ref().and_then(crate::play_on_phone::Watched::place);
+ if watched.is_some() && place.is_none() && started.elapsed() < FIND_WINDOW {
+ // Not found yet: nothing to show.
+ std::thread::sleep(frame_interval);
+ continue;
+ }
+ if watched.is_some() && place.is_none() {
+ // The player's window can't be found. Not the screen instead:
+ // that would show the phone whatever else is on it.
+ tracing::info!(%peer, "can't find the player's window; stopping");
+ crate::bridge::app::show_message(crate::i18n::tr!(
+ "PcMirror",
+ "Can't find the player's window. Play the video on this PC again, then turn on Play on phone."
+ ));
+ break;
+ }
+ if let Some(place) = place {
+ let r = place.on_screen;
+ let dims =
+ target_dimensions((r.right - r.left) as u32, (r.bottom - r.top) as u32, options.max_size);
+ if stream_dims != Some(dims) {
+ // A new size (the window moved to full screen, say): a new stream config.
+ stream_dims = Some(dims);
+ encoder = None;
+ }
+ // The window's own pixels, whatever is in front of it; the
+ // screen there when Windows can't capture the window.
+ if window_capture.as_ref().is_none_or(|c| c.window() != place.window()) {
+ window_capture = win::window_capture::WindowCapture::new(place.window())
+ .inspect_err(
+ |e| tracing::info!(error = %e, "can't capture the window; capturing the screen"),
+ )
+ .ok();
+ }
+ }
+
+ let (dst_w, dst_h) = match stream_dims {
+ Some(dims) => dims,
+ None => {
+ let (src_w, src_h) = unsafe {
+ let hdc = GetDC(None);
+ let pw = if !hdc.is_invalid() { GetDeviceCaps(Some(hdc), DESKTOPHORZRES) } else { 0 };
+ let ph = if !hdc.is_invalid() { GetDeviceCaps(Some(hdc), DESKTOPVERTRES) } else { 0 };
+ if !hdc.is_invalid() {
+ let _ = ReleaseDC(None, hdc);
+ }
+ (
+ if pw > 0 { pw as u32 } else { GetSystemMetrics(SM_CXSCREEN).max(640) as u32 },
+ if ph > 0 { ph as u32 } else { GetSystemMetrics(SM_CYSCREEN).max(480) as u32 },
+ )
+ };
+ let dims = target_dimensions(src_w, src_h, options.max_size);
+ stream_dims = Some(dims);
+ dims
+ }
+ };
+
+ // A keyframe on request comes from the running encoder. Rebuilding it (a new
+ // stream config plus a full keyframe) is the fallback for encoders that can't.
+ let requested_kf = force_keyframe.swap(false, Ordering::SeqCst);
+ let keyframe_in_place =
+ requested_kf && encoder.as_ref().is_some_and(H264Encoder::request_keyframe);
+ let need_reinit = encoder.is_none()
+ || (requested_kf
+ && !keyframe_in_place
+ && last_reinit.elapsed() >= Duration::from_millis(1400));
+
+ if need_reinit {
+ match H264Encoder::new(dst_w, dst_h, fps, bitrate) {
+ Ok(enc) => {
+ let cfg = MirrorConfig {
+ codec: nectarlink_core::MIRROR_H264.into(),
+ width: dst_w,
+ height: dst_h,
+ session: nectarlink_core::MIRROR_SCREEN,
+ };
+ if matches!(sender.send(PacketKind::Config, 0, cfg.to_cbor()), MirrorSend::Closed) {
+ break;
+ }
+ encoder = Some(enc);
+ last_reinit = Instant::now();
+ stats.rebuilds += 1;
+ }
+ Err(e) => {
+ tracing::warn!(error = %e, "PC mirror H.264 encoder init failed");
+ break;
+ }
+ }
+ }
+
+ let captured = match (place, window_capture.as_mut()) {
+ (Some(place), Some(capture)) => capture.capture(place.in_window, dst_w, dst_h, &mut bgra),
+ (Some(place), None) => {
+ capture_screen(Some(place.on_screen), dst_w, dst_h, &mut bgra).is_some()
+ }
+ (None, _) => capture_screen(None, dst_w, dst_h, &mut bgra).is_some(),
+ };
+ if captured && let Some(enc) = encoder.as_mut() {
+ bgra_to_nv12(&bgra, dst_w as usize, dst_h as usize, &mut nv12);
+ let time_us = started.elapsed().as_micros() as u64;
+ let hns = (time_us * 10) as i64;
+ match enc.encode(&nv12, hns, dur_hns) {
+ Ok(packets) => {
+ let mut send_failed = false;
+ for (is_keyframe, bytes) in packets {
+ let kind = if is_keyframe { PacketKind::Keyframe } else { PacketKind::Frame };
+ let len = bytes.len();
+ match sender.send(kind, time_us, bytes) {
+ MirrorSend::Queued => {
+ stats.frames += 1;
+ stats.bytes += len as u64;
+ if is_keyframe {
+ stats.keyframes += 1;
+ }
+ }
+ MirrorSend::Dropped => stats.dropped += 1,
+ MirrorSend::NeedKeyframe => {
+ stats.dropped += 1;
+ last_drop = Some(Instant::now());
+ force_keyframe.store(true, Ordering::SeqCst);
+ // The link is slower than the stream: ask for less, at most every 2 s.
+ if last_rate_change.elapsed() >= Duration::from_secs(2) {
+ let lower = (bitrate / 4 * 3).max(MIN_BITRATE);
+ if lower < bitrate && enc.set_bitrate(lower) {
+ bitrate = lower;
+ last_rate_change = Instant::now();
+ }
+ }
+ }
+ MirrorSend::Closed => {
+ send_failed = true;
+ break;
+ }
+ }
+ }
+ if send_failed {
+ break;
+ }
+ // Clean for 10 s: step the bitrate back up toward what the phone asked for.
+ let clean = last_drop.is_none_or(|t| t.elapsed() >= Duration::from_secs(10));
+ if clean
+ && bitrate < max_bitrate
+ && last_rate_change.elapsed() >= Duration::from_secs(10)
+ {
+ let higher = (bitrate / 4 * 5).min(max_bitrate);
+ if enc.set_bitrate(higher) {
+ bitrate = higher;
+ last_rate_change = Instant::now();
+ }
+ }
+ }
+ Err(e) => {
+ tracing::warn!(error = %e, "PC mirror frame encode error; rebuilding the encoder");
+ encoder = None;
+ }
+ }
+ }
+
+ if stats_since.elapsed() >= Duration::from_secs(10) {
+ let secs = stats_since.elapsed().as_secs_f64();
+ tracing::info!(
+ %peer,
+ fps = format!("{:.1}", stats.frames as f64 / secs),
+ kbps = (stats.bytes as f64 * 8.0 / secs / 1000.0) as u64,
+ keyframes = stats.keyframes,
+ dropped = stats.dropped,
+ rebuilds = stats.rebuilds,
+ bitrate,
+ "sharing this PC's screen"
+ );
+ stats = Stats::default();
+ stats_since = Instant::now();
+ }
+
+ let elapsed = tick_start.elapsed();
+ if let Some(rem) = frame_interval.checked_sub(elapsed) {
+ std::thread::sleep(rem);
+ }
+ }
+
+ tracing::info!(%peer, "stopped sharing this PC's screen");
+ crate::input_share::leave_remote();
+ sender.close();
+ cleanup_session(&stop);
+ }
+
+ /// Mute this PC's speakers while a phone watches (the setting).
+ static MUTE_WHILE_WATCHED: AtomicBool = AtomicBool::new(true);
+
+ pub fn set_mute_while_watched(on: bool) {
+ MUTE_WHILE_WATCHED.store(on, Ordering::Relaxed);
+ }
+
+ /// The PC's sound going to a phone that watches one of its players (as
+ /// 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.
+ struct SoundToPhone {
+ was_on: bool,
+ was_target: Option<DeviceId>,
+ /// This muted the speakers (they weren't already).
+ muted: bool,
+ }
+
+ 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,
+ };
+ crate::pc_audio_stream::set_target(Some(peer));
+ crate::pc_audio_stream::set_enabled(true);
+ sound
+ }
+ }
+
+ 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);
+ // Unmuted only if this muted it.
+ if self.muted {
+ let _ = win::audio::set_speaker_mute(false);
+ }
+ }
+ }
+
+ fn cleanup_session(stop: &Arc<AtomicBool>) {
+ let removed = {
+ let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
+ if guard.as_ref().is_some_and(|s| Arc::ptr_eq(&s.stop, stop)) {
+ guard.take().is_some()
+ } else {
+ false
+ }
+ };
+ if removed {
+ core_host::host().hub.changed(crate::state::Changes::REMOTE);
+ core_host::refresh_capabilities();
+ }
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ #[test]
+ fn encodes_nv12_frames_and_decodes_back_with_mft() {
+ let (w, h) = target_dimensions(1920, 1080, 1280);
+ assert_eq!(w % 16, 0);
+ assert_eq!(h % 16, 0);
+
+ let mut enc = H264Encoder::new(w, h, 30, 4_000_000).expect("H264Encoder MFT");
+ let mut dec = crate::win::h264::Decoder::new_for_codec("h264").expect("H264 Decoder MFT");
+
+ let mut bgra = vec![0u8; (w as usize) * (h as usize) * 4];
+ for (i, px) in bgra.as_chunks_mut::<4>().0.iter_mut().enumerate() {
+ px[0] = (i % 255) as u8;
+ px[1] = ((i / 7) % 255) as u8;
+ px[2] = ((i / 13) % 255) as u8;
+ px[3] = 255;
+ }
+ let mut nv12 = Vec::new();
+ bgra_to_nv12(&bgra, w as usize, h as usize, &mut nv12);
+
+ let mut decoded_any = false;
+ let mut produced_packets = 0usize;
+ for i in 0..8 {
+ let packets = enc.encode(&nv12, i * 333_333, 333_333).expect("encode frame");
+ for (_keyframe, data) in packets {
+ produced_packets += 1;
+ let pics = dec.decode(&data, (i as u64) * 33_333).expect("decode packet");
+ if let Some(pic) = pics.first() {
+ assert_eq!(pic.width, w);
+ assert_eq!(pic.height, h);
+ decoded_any = true;
+ }
+ }
+ }
+ assert!(produced_packets > 0, "encoder must emit H.264 access units");
+ assert!(decoded_any, "decoder must reconstruct at least one frame from H264Encoder");
+ }
+
+ /// With a Chromium browser showing a video on a page whose title has
+ /// `NECTARLINK_TEST_TAB` in it, in view: captures just the video, as
+ /// Play on phone does.
+ #[test]
+ #[ignore]
+ fn captures_a_browsers_video() {
+ use crate::win::browser::{Automation, window_for};
+ let wanted = std::env::var("NECTARLINK_TEST_TAB").expect("NECTARLINK_TEST_TAB");
+ // SAFETY: COM for this test's thread.
+ let _ = unsafe {
+ windows::Win32::System::Com::CoInitializeEx(
+ None,
+ windows::Win32::System::Com::COINIT_MULTITHREADED,
+ )
+ };
+ crate::win::browser::use_physical_pixels();
+ let window = window_for("Chrome", &wanted).expect("the test tab's window");
+ let rect = Automation::new().unwrap().video_rect(&window).expect("the video");
+ let (sw, sh) = ((rect.right - rect.left) as u32, (rect.bottom - rect.top) as u32);
+ let (w, h) = target_dimensions(sw, sh, 1280);
+ let mut bgra = Vec::new();
+ assert_eq!(capture_screen(Some(rect), w, h, &mut bgra), Some((sw, sh)));
+ let distinct: std::collections::HashSet<&[u8; 4]> = bgra.as_chunks::<4>().0.iter().collect();
+ eprintln!("{rect:?} as {w}x{h}: {} colors", distinct.len());
+ assert!(distinct.len() > 50, "a moving test pattern, not a blank");
+ }
+
+ #[test]
+ fn keyframes_come_on_request_without_rebuilding_the_encoder() {
+ let (w, h) = target_dimensions(1280, 720, 1280);
+ let mut enc = H264Encoder::new(w, h, 30, 2_000_000).expect("H264Encoder MFT");
+ let mut nv12 = Vec::new();
+ bgra_to_nv12(&vec![128u8; (w as usize) * (h as usize) * 4], w as usize, h as usize, &mut nv12);
+
+ let keyframes_after = |enc: &mut H264Encoder, start: i64, count: i64| {
+ (start..start + count)
+ .flat_map(|i| enc.encode(&nv12, i * 333_333, 333_333).expect("encode frame"))
+ .filter(|(keyframe, data)| *keyframe && has_h264_idr(data))
+ .count()
+ };
+ // The first frame is a keyframe; the next few (well inside the 2 s GOP) are not.
+ assert_eq!(keyframes_after(&mut enc, 0, 1), 1, "the stream starts with a keyframe");
+ assert_eq!(keyframes_after(&mut enc, 1, 5), 0, "no keyframes mid-GOP unprompted");
+ assert!(enc.request_keyframe(), "the Windows H.264 encoder takes keyframe requests");
+ assert_eq!(keyframes_after(&mut enc, 6, 2), 1, "a requested keyframe arrives promptly");
+ assert!(enc.set_bitrate(1_000_000), "the bitrate changes in place");
+ }
+ }
+}
+
+#[cfg(not(windows))]
+mod imp {
+ #![allow(dead_code)]
+
+ use nectarlink_core::{DeviceId, MirrorInput, MirrorStart, NodeEvent};
+
+ pub fn active_peer() -> Option<DeviceId> {
+ None
+ }
+
+ pub fn is_active() -> bool {
+ false
+ }
+
+ pub fn is_watching() -> bool {
+ false
+ }
+
+ pub fn stop_watching() {}
+
+ pub fn start(_peer: DeviceId, _options: MirrorStart) {}
+
+ pub fn stop_for(_peer: &DeviceId) {}
+
+ pub fn stop_all() {}
+
+ pub fn request_keyframe(_peer: &DeviceId) {}
+
+ pub fn handle_input(_peer: &DeviceId, _session: u32, _input: MirrorInput) {}
+
+ pub fn on_event(_event: &NodeEvent) {}
+
+ pub fn set_mute_while_watched(_on: bool) {}
+}
+
+pub use imp::*;
diff --git a/desktop/app/src/pc_mirror/mod.rs b/desktop/app/src/pc_mirror/mod.rs
deleted file mode 100644
index 42019c7..0000000
--- a/desktop/app/src/pc_mirror/mod.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Reverse Mirroring: streaming PC display to phone.
-
-#[cfg(windows)]
-mod windows;
-#[cfg(windows)]
-pub use self::windows::*;
-
-#[cfg(not(windows))]
-mod non_windows;
-#[cfg(not(windows))]
-pub use self::non_windows::*;
diff --git a/desktop/app/src/pc_mirror/non_windows.rs b/desktop/app/src/pc_mirror/non_windows.rs
deleted file mode 100644
index d660856..0000000
--- a/desktop/app/src/pc_mirror/non_windows.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! macOS / non-Windows reverse mirroring stubs.
-
-use nectarlink_core::{DeviceId, MirrorInput, MirrorStart, NodeEvent};
-
-pub fn active_peer() -> Option<DeviceId> {
- None
-}
-
-pub fn is_active() -> bool {
- false
-}
-
-pub fn is_watching() -> bool {
- false
-}
-
-pub fn stop_watching() {}
-
-pub fn start(_peer: DeviceId, _options: MirrorStart) {}
-
-pub fn stop_for(_peer: &DeviceId) {}
-
-pub fn stop_all() {}
-
-pub fn request_keyframe(_peer: &DeviceId) {}
-
-pub fn handle_input(_peer: &DeviceId, _session: u32, _input: MirrorInput) {}
-
-pub fn on_event(_event: &NodeEvent) {}
-
-pub fn set_mute_while_watched(_on: bool) {}
diff --git a/desktop/app/src/pc_mirror/windows.rs b/desktop/app/src/pc_mirror/windows.rs
deleted file mode 100644
index d6f7f6c..0000000
--- a/desktop/app/src/pc_mirror/windows.rs
+++ /dev/null
@@ -1,1047 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Reverse Mirroring ("Your PC in Your Pocket"): captures the PC's primary
-//! display, encodes it to low-latency H.264 via Windows Media Foundation, and
-//! streams it over `mirror` / `video` (`session = 0`) to a paired phone, while
-//! injecting touch, scroll, key, and text input back into Windows (`docs/PLAN.md` §8.6).
-
-#![allow(unsafe_code)]
-
-use std::{
- mem::{ManuallyDrop, size_of},
- sync::{
- Arc, Mutex,
- atomic::{AtomicBool, Ordering},
- },
- time::{Duration, Instant},
-};
-
-use nectarlink_core::{
- DeviceId, KeyMod, MirrorConfig, MirrorInput, MirrorSend, MirrorStart, PacketKind, mirror_keys,
-};
-use windows::Win32::{
- Foundation::{POINT, RECT},
- Graphics::Gdi::{
- BI_RGB, BITMAPINFO, BITMAPINFOHEADER, CreateCompatibleBitmap, CreateCompatibleDC, DESKTOPHORZRES,
- DESKTOPVERTRES, DeleteDC, DeleteObject, GetDC, GetDIBits, GetDeviceCaps, HALFTONE, ReleaseDC,
- SRCCOPY, SelectObject, SetStretchBltMode, StretchBlt,
- },
- Media::MediaFoundation::{
- CODECAPI_AVEncCommonMeanBitRate, CODECAPI_AVEncMPVGOPSize, CODECAPI_AVEncVideoForceKeyFrame,
- CODECAPI_AVLowLatencyMode, ICodecAPI, IMFMediaBuffer, IMFMediaType, IMFSample, IMFTransform,
- MF_E_NOTACCEPTING, MF_E_TRANSFORM_NEED_MORE_INPUT, MF_E_TRANSFORM_STREAM_CHANGE, MF_LOW_LATENCY,
- MF_MT_AVG_BITRATE, MF_MT_FRAME_RATE, MF_MT_FRAME_SIZE, MF_MT_INTERLACE_MODE, MF_MT_MAJOR_TYPE,
- MF_MT_MPEG_SEQUENCE_HEADER, MF_MT_PIXEL_ASPECT_RATIO, MF_MT_SUBTYPE, MFCreateMediaType,
- MFCreateMemoryBuffer, MFCreateSample, MFMediaType_Video, MFSampleExtension_CleanPoint,
- MFT_CATEGORY_VIDEO_ENCODER, MFT_MESSAGE_COMMAND_FLUSH, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING,
- MFT_MESSAGE_NOTIFY_END_STREAMING, MFT_MESSAGE_NOTIFY_START_OF_STREAM, MFT_OUTPUT_DATA_BUFFER,
- MFT_OUTPUT_STREAM_PROVIDES_SAMPLES, MFVideoFormat_H264, MFVideoFormat_NV12,
- MFVideoInterlace_Progressive,
- },
- System::Variant::VARIANT,
- UI::WindowsAndMessaging::{
- CURSOR_SHOWING, CURSORINFO, DI_NORMAL, DrawIconEx, GetCursorInfo, GetSystemMetrics, SM_CXSCREEN,
- SM_CYSCREEN,
- },
-};
-
-use crate::{
- core_host,
- win::{
- self,
- h264::{create_mft, ensure_mf_started},
- },
-};
-
-struct ActiveSession {
- peer: DeviceId,
- stop: Arc<AtomicBool>,
- force_keyframe: Arc<AtomicBool>,
- /// Play on phone: the phone watches a player, and its input is ignored.
- watching: bool,
-}
-
-static ACTIVE: Mutex<Option<ActiveSession>> = Mutex::new(None);
-
-/// How long Play on phone looks for the player's window before streaming
-/// the screen instead.
-const FIND_WINDOW: Duration = Duration::from_secs(5);
-
-/// The lowest bitrate the stream backs off to on a slow link.
-const MIN_BITRATE: u32 = 600_000;
-
-/// What a sharing session sent in the last stats window.
-#[derive(Default)]
-struct Stats {
- frames: u64,
- keyframes: u64,
- bytes: u64,
- dropped: u64,
- rebuilds: u64,
-}
-
-/// Returns the device currently viewing this PC's screen, if any.
-pub fn active_peer() -> Option<DeviceId> {
- ACTIVE.lock().unwrap_or_else(|e| e.into_inner()).as_ref().map(|s| s.peer)
-}
-
-/// Whether this PC's screen is currently being shared with a phone.
-pub fn is_active() -> bool {
- active_peer().is_some()
-}
-
-/// Whether a phone watches one of this PC's players (Play on phone).
-pub fn is_watching() -> bool {
- 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.
-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);
- if let Some(peer) = peer {
- stop_for(&peer);
- }
-}
-
-/// Starts or replaces the active PC-to-phone screen mirror stream for `peer`.
-pub fn start(peer: DeviceId, options: MirrorStart) {
- stop_all();
- let stop = Arc::new(AtomicBool::new(false));
- let force_keyframe = Arc::new(AtomicBool::new(false));
- {
- let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
- *guard = Some(ActiveSession {
- peer,
- stop: Arc::clone(&stop),
- force_keyframe: Arc::clone(&force_keyframe),
- watching: options.watch.is_some(),
- });
- }
- core_host::host().hub.changed(crate::state::Changes::REMOTE);
- core_host::refresh_capabilities();
-
- std::thread::Builder::new()
- .name("nectarlink-pc-mirror".into())
- .spawn(move || run_capture_loop(peer, options, stop, force_keyframe))
- .ok();
-}
-
-/// Stops PC-to-phone mirroring if `peer` matches the active viewer.
-pub fn stop_for(peer: &DeviceId) {
- let stopped = {
- let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
- if guard.as_ref().is_some_and(|s| &s.peer == peer)
- && let Some(s) = guard.take()
- {
- s.stop.store(true, Ordering::SeqCst);
- true
- } else {
- false
- }
- };
- if stopped {
- win::input::release_held_buttons();
- crate::input_share::leave_remote();
- core_host::host().hub.changed(crate::state::Changes::REMOTE);
- core_host::refresh_capabilities();
- }
-}
-
-/// Stops any active PC-to-phone mirroring session.
-pub fn stop_all() {
- let stopped = {
- let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
- if let Some(s) = guard.take() {
- s.stop.store(true, Ordering::SeqCst);
- Some(s.peer)
- } else {
- None
- }
- };
- if let Some(peer) = stopped {
- win::input::release_held_buttons();
- crate::input_share::leave_remote();
- if let Some(node) = core_host::node() {
- node.set_pc_mirror_session_allowed(peer, false);
- core_host::spawn(async move {
- node.mirror_stop(peer, nectarlink_core::MIRROR_SCREEN).await;
- });
- }
- core_host::host().hub.changed(crate::state::Changes::REMOTE);
- core_host::refresh_capabilities();
- }
-}
-
-/// Requests an immediate IDR keyframe for `peer`.
-pub fn request_keyframe(peer: &DeviceId) {
- let guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
- if let Some(s) = guard.as_ref()
- && &s.peer == peer
- {
- s.force_keyframe.store(true, Ordering::SeqCst);
- }
-}
-
-/// Injects touch, scroll, key, or text input from a phone viewing the PC screen.
-pub fn handle_input(peer: &DeviceId, session: u32, input: MirrorInput) {
- if session != nectarlink_core::MIRROR_SCREEN {
- return;
- }
- if active_peer() != Some(*peer) || is_watching() {
- return;
- }
- let remote_input_on = core_host::node()
- .and_then(|n| n.device_toggles(*peer).ok())
- .is_some_and(|toggles| toggles.iter().any(|(k, v)| *k == "remote_input" && *v));
- if !remote_input_on {
- return;
- }
- let _ = crate::input_share::enter_remote(*peer);
- match input {
- MirrorInput::Touch { action, x, y } => {
- win::input::mirror_touch(action, x, y);
- }
- MirrorInput::Scroll { x, y, dx, dy } => {
- win::input::move_absolute(x, y);
- win::input::scroll(dx, dy);
- }
- MirrorInput::Text { text } => {
- win::input::type_text(&text);
- }
- MirrorInput::Key { key } => match key.as_str() {
- mirror_keys::BACK => win::input::press_key("escape", &[]),
- mirror_keys::HOME => win::input::press_key("a", &[KeyMod::Win]),
- mirror_keys::RECENTS => win::input::press_key("task_view", &[]),
- mirror_keys::ENTER => win::input::press_key("enter", &[]),
- mirror_keys::BACKSPACE => win::input::press_key("backspace", &[]),
- mirror_keys::DELETE => win::input::press_key("delete", &[]),
- mirror_keys::LEFT => win::input::press_key("left", &[]),
- mirror_keys::RIGHT => win::input::press_key("right", &[]),
- mirror_keys::UP => win::input::press_key("up", &[]),
- mirror_keys::DOWN => win::input::press_key("down", &[]),
- mirror_keys::TAB => win::input::press_key("tab", &[]),
- mirror_keys::NOTIFICATIONS => win::input::press_key("n", &[KeyMod::Win]),
- _ => {}
- },
- MirrorInput::Gamepad { .. } => {}
- }
-}
-
-pub fn on_event(event: &nectarlink_core::NodeEvent) {
- match event {
- nectarlink_core::NodeEvent::LinkChanged {
- device,
- link: nectarlink_core::LinkState::Offline { .. },
- }
- | nectarlink_core::NodeEvent::DeviceRemoved(device) => {
- stop_for(device);
- }
- nectarlink_core::NodeEvent::PcMirrorRequested { .. } => {
- core_host::host().hub.update(|s| s.apply(event));
- crate::bridge::app::request_activation();
- }
- _ => {}
- }
-}
-
-/// Computes target encoder dimensions (multiples of 16) fitting within `max_size`.
-pub(crate) fn target_dimensions(src_w: u32, src_h: u32, max_size: u32) -> (u32, u32) {
- let max_edge = max_size.clamp(320, 1920) as f64;
- let sw = src_w.max(64) as f64;
- let sh = src_h.max(64) as f64;
- let longest = sw.max(sh);
- let scale = if longest > max_edge { max_edge / longest } else { 1.0 };
- let w = (((sw * scale).round() as u32) / 16).max(4) * 16;
- let h = (((sh * scale).round() as u32) / 16).max(4) * 16;
- (w, h)
-}
-
-/// Converts a top-down 32-bit BGRA image (`width * height * 4` bytes) into
-/// BT.709 limited-range NV12 (`width * height * 3 / 2` bytes).
-pub(crate) fn bgra_to_nv12(bgra: &[u8], width: usize, height: usize, nv12: &mut Vec<u8>) {
- let y_size = width * height;
- let uv_size = y_size / 2;
- nv12.resize(y_size + uv_size, 0);
- let (y_plane, uv_plane) = nv12.split_at_mut(y_size);
-
- for row in 0..height {
- let src_row = row * width * 4;
- let y_row = row * width;
- for col in 0..width {
- let px = src_row + col * 4;
- let b = i32::from(bgra[px]);
- let g = i32::from(bgra[px + 1]);
- let r = i32::from(bgra[px + 2]);
- let y = ((47 * r + 157 * g + 16 * b + 128) >> 8) + 16;
- y_plane[y_row + col] = y.clamp(16, 235) as u8;
- }
- }
-
- for row in (0..height).step_by(2) {
- let src0 = row * width * 4;
- let src1 = (row + 1).min(height - 1) * width * 4;
- let uv_row = (row / 2) * width;
- for col in (0..width).step_by(2) {
- let p00 = src0 + col * 4;
- let p01 = src0 + (col + 1).min(width - 1) * 4;
- let p10 = src1 + col * 4;
- let p11 = src1 + (col + 1).min(width - 1) * 4;
-
- let b = (i32::from(bgra[p00])
- + i32::from(bgra[p01])
- + i32::from(bgra[p10])
- + i32::from(bgra[p11])
- + 2)
- >> 2;
- let g = (i32::from(bgra[p00 + 1])
- + i32::from(bgra[p01 + 1])
- + i32::from(bgra[p10 + 1])
- + i32::from(bgra[p11 + 1])
- + 2)
- >> 2;
- let r = (i32::from(bgra[p00 + 2])
- + i32::from(bgra[p01 + 2])
- + i32::from(bgra[p10 + 2])
- + i32::from(bgra[p11 + 2])
- + 2)
- >> 2;
-
- let u = ((-26 * r - 87 * g + 112 * b + 128) >> 8) + 128;
- let v = ((112 * r - 102 * g - 10 * b + 128) >> 8) + 128;
- uv_plane[uv_row + col] = u.clamp(16, 240) as u8;
- uv_plane[uv_row + col + 1] = v.clamp(16, 240) as u8;
- }
- }
-}
-
-pub(crate) struct H264Encoder {
- transform: IMFTransform,
- /// The encoder's settings interface, when it has one: keyframes on demand and live
- /// bitrate changes without rebuilding the encoder.
- codec_api: Option<ICodecAPI>,
- width: u32,
- height: u32,
- provides_samples: bool,
- output_size: u32,
- seq_header: Vec<u8>,
- sent_first_frame: bool,
-}
-
-impl Drop for H264Encoder {
- fn drop(&mut self) {
- // SAFETY: flushes and ends streaming on the MFT before drop.
- unsafe {
- let _ = self.transform.ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0);
- let _ = self.transform.ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, 0);
- }
- }
-}
-
-impl H264Encoder {
- pub(crate) fn new(width: u32, height: u32, fps: u32, bitrate: u32) -> windows::core::Result<Self> {
- ensure_mf_started()?;
- // SAFETY: Media Foundation COM calls with valid parameters.
- unsafe {
- let transform = create_mft(MFT_CATEGORY_VIDEO_ENCODER, MFVideoFormat_NV12, MFVideoFormat_H264)?;
- if let Ok(attrs) = transform.GetAttributes() {
- let _ = attrs.SetUINT32(&MF_LOW_LATENCY, 1);
- }
-
- let out_type: IMFMediaType = MFCreateMediaType()?;
- out_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
- out_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_H264)?;
- let _ = out_type.SetUINT32(
- &windows::Win32::Media::MediaFoundation::MF_MT_MPEG2_PROFILE,
- windows::Win32::Media::MediaFoundation::eAVEncH264VProfile_Base.0 as u32,
- );
- out_type.SetUINT32(&MF_MT_AVG_BITRATE, bitrate.clamp(500_000, 20_000_000))?;
- out_type.SetUINT32(&MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive.0 as u32)?;
- out_type.SetUINT64(&MF_MT_FRAME_SIZE, (u64::from(width) << 32) | u64::from(height))?;
- out_type.SetUINT64(&MF_MT_FRAME_RATE, (u64::from(fps.max(1)) << 32) | 1)?;
- out_type.SetUINT64(&MF_MT_PIXEL_ASPECT_RATIO, (1u64 << 32) | 1)?;
- transform.SetOutputType(0, Some(&out_type), 0)?;
-
- let in_type: IMFMediaType = MFCreateMediaType()?;
- in_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
- in_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_NV12)?;
- in_type.SetUINT32(&MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive.0 as u32)?;
- in_type.SetUINT64(&MF_MT_FRAME_SIZE, (u64::from(width) << 32) | u64::from(height))?;
- in_type.SetUINT64(&MF_MT_FRAME_RATE, (u64::from(fps.max(1)) << 32) | 1)?;
- in_type.SetUINT64(&MF_MT_PIXEL_ASPECT_RATIO, (1u64 << 32) | 1)?;
- transform.SetInputType(0, Some(&in_type), 0)?;
-
- let mut seq_header = Vec::new();
- if let Ok(cur_out) = transform.GetOutputCurrentType(0)
- && let Ok(len) = cur_out.GetBlobSize(&MF_MT_MPEG_SEQUENCE_HEADER)
- && len > 0
- {
- let mut buf = vec![0u8; len as usize];
- if cur_out.GetBlob(&MF_MT_MPEG_SEQUENCE_HEADER, &mut buf, None).is_ok() {
- seq_header = buf;
- }
- }
-
- let info = transform.GetOutputStreamInfo(0)?;
- let provides_samples = (info.dwFlags & (MFT_OUTPUT_STREAM_PROVIDES_SAMPLES.0 as u32)) != 0;
- let output_size = info.cbSize.max(256 * 1024);
-
- // A keyframe every two seconds keeps late joiners and lost packets recoverable
- // without rebuilding the encoder, and low-latency mode stops it holding frames back.
- let codec_api: Option<ICodecAPI> = windows::core::Interface::cast(&transform).ok();
- if let Some(api) = codec_api.as_ref() {
- let _ = api.SetValue(&CODECAPI_AVLowLatencyMode, &VARIANT::from(true));
- let _ = api.SetValue(&CODECAPI_AVEncMPVGOPSize, &VARIANT::from(fps.max(1) * 2));
- }
-
- let _ = transform.ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0);
- let _ = transform.ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0);
-
- Ok(Self {
- transform,
- codec_api,
- width,
- height,
- provides_samples,
- output_size,
- seq_header,
- sent_first_frame: false,
- })
- }
- }
-
- /// Asks for the next frame to be a keyframe. False when the encoder can't, so the
- /// caller must rebuild it instead.
- pub(crate) fn request_keyframe(&self) -> bool {
- // SAFETY: ICodecAPI::SetValue with a valid GUID and VARIANT.
- self.codec_api.as_ref().is_some_and(|api| {
- unsafe { api.SetValue(&CODECAPI_AVEncVideoForceKeyFrame, &VARIANT::from(1u32)) }.is_ok()
- })
- }
-
- /// Changes the target bitrate in place. False when the encoder can't.
- pub(crate) fn set_bitrate(&self, bitrate: u32) -> bool {
- // SAFETY: ICodecAPI::SetValue with a valid GUID and VARIANT.
- self.codec_api.as_ref().is_some_and(|api| {
- unsafe { api.SetValue(&CODECAPI_AVEncCommonMeanBitRate, &VARIANT::from(bitrate)) }.is_ok()
- })
- }
-
- /// Feeds one NV12 frame (`width * height * 3 / 2` bytes) and drains encoded
- /// Annex-B H.264 access units `(is_keyframe, bytes)`.
- pub(crate) fn encode(
- &mut self,
- nv12: &[u8],
- hns: i64,
- dur_hns: i64,
- ) -> windows::core::Result<Vec<(bool, Vec<u8>)>> {
- let expected = (self.width as usize) * (self.height as usize) * 3 / 2;
- if nv12.len() < expected {
- return Ok(Vec::new());
- }
- // SAFETY: Media Foundation sample creation and MFT ProcessInput/ProcessOutput.
- unsafe {
- let sample = make_sample(&nv12[..expected], hns, dur_hns)?;
- let mut out = Vec::new();
- match self.transform.ProcessInput(0, &sample, 0) {
- Ok(()) => self.drain(&mut out)?,
- Err(e) if e.code() == MF_E_NOTACCEPTING => {
- self.drain(&mut out)?;
- self.transform.ProcessInput(0, &sample, 0)?;
- self.drain(&mut out)?;
- }
- Err(e) => return Err(e),
- }
- Ok(out)
- }
- }
-
- unsafe fn drain(&mut self, out: &mut Vec<(bool, Vec<u8>)>) -> windows::core::Result<()> {
- loop {
- // SAFETY: MFT output draining with allocated or MFT-provided sample.
- unsafe {
- let allocated = if self.provides_samples {
- None
- } else {
- let s: IMFSample = MFCreateSample()?;
- let b: IMFMediaBuffer = MFCreateMemoryBuffer(self.output_size)?;
- s.AddBuffer(&b)?;
- Some(s)
- };
- let mut data = MFT_OUTPUT_DATA_BUFFER {
- dwStreamID: 0,
- pSample: ManuallyDrop::new(allocated),
- dwStatus: 0,
- pEvents: ManuallyDrop::new(None),
- };
- let mut status = 0u32;
- let res = self.transform.ProcessOutput(0, std::slice::from_mut(&mut data), &mut status);
- let sample = ManuallyDrop::take(&mut data.pSample);
- let _ = ManuallyDrop::take(&mut data.pEvents);
-
- match res {
- Ok(()) => {
- if let Some(sample) = sample {
- let clean = sample
- .GetUINT32(&MFSampleExtension_CleanPoint)
- .map(|v| v != 0)
- .unwrap_or(false);
- let buf: IMFMediaBuffer = sample.ConvertToContiguousBuffer()?;
- let mut ptr = std::ptr::null_mut();
- let mut len = 0u32;
- buf.Lock(&mut ptr, None, Some(&mut len))?;
- let slice = std::slice::from_raw_parts(ptr, len as usize);
- let is_idr = !self.sent_first_frame || clean || has_h264_idr(slice);
- let mut bytes = Vec::new();
- if is_idr && !has_h264_sps(slice) {
- if self.seq_header.is_empty()
- && let Ok(cur_out) = self.transform.GetOutputCurrentType(0)
- && let Ok(hdr_len) = cur_out.GetBlobSize(&MF_MT_MPEG_SEQUENCE_HEADER)
- && hdr_len > 0
- {
- let mut hdr = vec![0u8; hdr_len as usize];
- if cur_out.GetBlob(&MF_MT_MPEG_SEQUENCE_HEADER, &mut hdr, None).is_ok() {
- self.seq_header = hdr;
- }
- }
- if !self.seq_header.is_empty() {
- bytes.extend_from_slice(&self.seq_header);
- }
- }
- bytes.extend_from_slice(slice);
- buf.Unlock()?;
- if !bytes.is_empty() {
- self.sent_first_frame = true;
- out.push((is_idr, bytes));
- }
- }
- }
- Err(e) if e.code() == MF_E_TRANSFORM_NEED_MORE_INPUT => return Ok(()),
- Err(e) if e.code() == MF_E_TRANSFORM_STREAM_CHANGE => {
- if let Ok(cur_out) = self.transform.GetOutputAvailableType(0, 0) {
- self.transform.SetOutputType(0, Some(&cur_out), 0)?;
- if let Ok(len) = cur_out.GetBlobSize(&MF_MT_MPEG_SEQUENCE_HEADER)
- && len > 0
- {
- let mut buf = vec![0u8; len as usize];
- if cur_out.GetBlob(&MF_MT_MPEG_SEQUENCE_HEADER, &mut buf, None).is_ok() {
- self.seq_header = buf;
- }
- }
- }
- }
- Err(e) => return Err(e),
- }
- }
- }
- }
-}
-
-unsafe fn make_sample(bytes: &[u8], hns: i64, dur_hns: i64) -> windows::core::Result<IMFSample> {
- // SAFETY: allocates an IMFMediaBuffer of `bytes.len()` and copies `bytes` into it.
- unsafe {
- let buf: IMFMediaBuffer = MFCreateMemoryBuffer(bytes.len() as u32)?;
- let mut dst = std::ptr::null_mut();
- buf.Lock(&mut dst, None, None)?;
- std::ptr::copy_nonoverlapping(bytes.as_ptr(), dst, bytes.len());
- buf.Unlock()?;
- buf.SetCurrentLength(bytes.len() as u32)?;
- let sample: IMFSample = MFCreateSample()?;
- sample.AddBuffer(&buf)?;
- sample.SetSampleTime(hns)?;
- sample.SetSampleDuration(dur_hns.max(1))?;
- Ok(sample)
- }
-}
-
-fn has_h264_idr(data: &[u8]) -> bool {
- data.windows(4).any(|w| w[..3] == [0, 0, 1] && (w[3] & 0x1f) == 5)
-}
-
-fn has_h264_sps(data: &[u8]) -> bool {
- data.windows(4).any(|w| w[..3] == [0, 0, 1] && (w[3] & 0x1f) == 7)
-}
-
-/// Captures the primary desktop monitor into `bgra` scaled to `(dst_w, dst_h)`,
-/// with the pointer, and returns the current physical screen dimensions
-/// `(src_w, src_h)`. With `region` (physical screen pixels), captures only
-/// that, without the pointer (Play on phone), and returns its size.
-fn capture_screen(region: Option<RECT>, dst_w: u32, dst_h: u32, bgra: &mut Vec<u8>) -> Option<(u32, u32)> {
- // SAFETY: Win32 GDI device context and bitmap calls with balanced cleanup.
- unsafe {
- let hdc_screen = GetDC(None);
- if hdc_screen.is_invalid() {
- return None;
- }
- let (src_x, src_y, src_w, src_h) = match region {
- Some(r) => (r.left, r.top, (r.right - r.left).max(1) as u32, (r.bottom - r.top).max(1) as u32),
- None => {
- let phys_w = GetDeviceCaps(Some(hdc_screen), DESKTOPHORZRES);
- let phys_h = GetDeviceCaps(Some(hdc_screen), DESKTOPVERTRES);
- let w = if phys_w > 0 { phys_w } else { GetSystemMetrics(SM_CXSCREEN) }.max(1) as u32;
- let h = if phys_h > 0 { phys_h } else { GetSystemMetrics(SM_CYSCREEN) }.max(1) as u32;
- (0, 0, w, h)
- }
- };
-
- let hdc_mem = CreateCompatibleDC(Some(hdc_screen));
- if hdc_mem.is_invalid() {
- let _ = ReleaseDC(None, hdc_screen);
- return None;
- }
- let hbm = CreateCompatibleBitmap(hdc_screen, dst_w as i32, dst_h as i32);
- if hbm.is_invalid() {
- let _ = DeleteDC(hdc_mem);
- let _ = ReleaseDC(None, hdc_screen);
- return None;
- }
- let old = SelectObject(hdc_mem, hbm.into());
- let _ = SetStretchBltMode(hdc_mem, HALFTONE);
- let ok = StretchBlt(
- hdc_mem,
- 0,
- 0,
- dst_w as i32,
- dst_h as i32,
- Some(hdc_screen),
- src_x,
- src_y,
- src_w as i32,
- src_h as i32,
- SRCCOPY,
- )
- .as_bool();
-
- // Draw the mouse cursor at its scaled position so the phone user sees where they point.
- let mut ci = CURSORINFO {
- cbSize: size_of::<CURSORINFO>() as u32,
- flags: Default::default(),
- hCursor: Default::default(),
- ptScreenPos: POINT::default(),
- };
- if region.is_none() && GetCursorInfo(&mut ci).is_ok() && (ci.flags.0 & CURSOR_SHOWING.0) != 0 {
- let cx = ((ci.ptScreenPos.x as i64) * (dst_w as i64) / (src_w as i64)) as i32;
- let cy = ((ci.ptScreenPos.y as i64) * (dst_h as i64) / (src_h as i64)) as i32;
- let _ = DrawIconEx(hdc_mem, cx, cy, ci.hCursor.into(), 0, 0, 0, None, DI_NORMAL);
- }
-
- let needed = (dst_w as usize) * (dst_h as usize) * 4;
- bgra.resize(needed, 0);
-
- let mut bmi = BITMAPINFO {
- bmiHeader: BITMAPINFOHEADER {
- biSize: size_of::<BITMAPINFOHEADER>() as u32,
- biWidth: dst_w as i32,
- biHeight: -(dst_h as i32), // Top-down DIB.
- biPlanes: 1,
- biBitCount: 32,
- biCompression: BI_RGB.0,
- biSizeImage: needed as u32,
- ..Default::default()
- },
- ..Default::default()
- };
-
- let lines = GetDIBits(
- hdc_mem,
- hbm,
- 0,
- dst_h,
- Some(bgra.as_mut_ptr().cast()),
- &mut bmi,
- windows::Win32::Graphics::Gdi::DIB_RGB_COLORS,
- );
-
- let _ = SelectObject(hdc_mem, old);
- let _ = DeleteObject(hbm.into());
- let _ = DeleteDC(hdc_mem);
- let _ = ReleaseDC(None, hdc_screen);
-
- if ok && lines > 0 { Some((src_w, src_h)) } else { None }
- }
-}
-
-fn run_capture_loop(
- peer: DeviceId,
- options: MirrorStart,
- stop: Arc<AtomicBool>,
- force_keyframe: Arc<AtomicBool>,
-) {
- let Some(node) = core_host::node() else {
- cleanup_session(&stop);
- return;
- };
- let runtime = &core_host::host().runtime;
- let Ok(sender) = runtime.block_on(node.mirror_open(peer)) else {
- tracing::warn!(%peer, "couldn't open PC mirror stream to phone");
- cleanup_session(&stop);
- return;
- };
-
- // Give the phone's PcMirrorView TextureView and MediaCodec a brief moment to
- // mount before sending the initial MirrorConfig and IDR keyframe.
- std::thread::sleep(Duration::from_millis(120));
-
- let fps = options.fps.clamp(10, 60);
- let max_bitrate = options.bitrate.clamp(500_000, 20_000_000);
- // Lowered when the link can't keep up (frames dropped to hold the delay down),
- // raised back once it has been clean for a while.
- let mut bitrate = max_bitrate;
- let frame_interval = Duration::from_micros(1_000_000 / u64::from(fps));
- let dur_hns = (10_000_000 / u64::from(fps)) as i64;
-
- let mut bgra = Vec::new();
- let mut nv12 = Vec::new();
- let mut stream_dims: Option<(u32, u32)> = None;
- let mut encoder: Option<H264Encoder> = None;
- let started = Instant::now();
- let mut last_reinit = Instant::now() - Duration::from_secs(5);
- let mut last_drop: Option<Instant> = None;
- let mut last_rate_change = Instant::now();
- let mut stats = Stats::default();
- let mut stats_since = Instant::now();
- // Play on phone: the player's picture instead of the screen, and the
- // PC's sound to that phone while it watches. The picture's place comes
- // in physical pixels; so does this thread's capture.
- if options.watch.is_some() {
- win::browser::use_physical_pixels();
- }
- let watched = options.watch.as_deref().map(crate::play_on_phone::Watched::follow);
- let mut window_capture: Option<win::window_capture::WindowCapture> = None;
- let _sound = watched.is_some().then(|| SoundToPhone::start(peer));
- if watched.is_some() {
- tracing::info!(%peer, fps, bitrate, "streaming a player to the phone");
- } else {
- tracing::info!(%peer, fps, bitrate, "sharing this PC's screen");
- }
-
- while !stop.load(Ordering::Relaxed) {
- let tick_start = Instant::now();
-
- let place = watched.as_ref().and_then(crate::play_on_phone::Watched::place);
- if watched.is_some() && place.is_none() && started.elapsed() < FIND_WINDOW {
- // Not found yet: nothing to show.
- std::thread::sleep(frame_interval);
- continue;
- }
- if watched.is_some() && place.is_none() {
- // The player's window can't be found. Not the screen instead:
- // that would show the phone whatever else is on it.
- tracing::info!(%peer, "can't find the player's window; stopping");
- crate::bridge::app::show_message(crate::i18n::tr!(
- "PcMirror",
- "Can't find the player's window. Play the video on this PC again, then turn on Play on phone."
- ));
- break;
- }
- if let Some(place) = place {
- let r = place.on_screen;
- let dims =
- target_dimensions((r.right - r.left) as u32, (r.bottom - r.top) as u32, options.max_size);
- if stream_dims != Some(dims) {
- // A new size (the window moved to full screen, say): a new stream config.
- stream_dims = Some(dims);
- encoder = None;
- }
- // The window's own pixels, whatever is in front of it; the
- // screen there when Windows can't capture the window.
- if window_capture.as_ref().is_none_or(|c| c.window() != place.window()) {
- window_capture = win::window_capture::WindowCapture::new(place.window())
- .inspect_err(
- |e| tracing::info!(error = %e, "can't capture the window; capturing the screen"),
- )
- .ok();
- }
- }
-
- let (dst_w, dst_h) = match stream_dims {
- Some(dims) => dims,
- None => {
- let (src_w, src_h) = unsafe {
- let hdc = GetDC(None);
- let pw = if !hdc.is_invalid() { GetDeviceCaps(Some(hdc), DESKTOPHORZRES) } else { 0 };
- let ph = if !hdc.is_invalid() { GetDeviceCaps(Some(hdc), DESKTOPVERTRES) } else { 0 };
- if !hdc.is_invalid() {
- let _ = ReleaseDC(None, hdc);
- }
- (
- if pw > 0 { pw as u32 } else { GetSystemMetrics(SM_CXSCREEN).max(640) as u32 },
- if ph > 0 { ph as u32 } else { GetSystemMetrics(SM_CYSCREEN).max(480) as u32 },
- )
- };
- let dims = target_dimensions(src_w, src_h, options.max_size);
- stream_dims = Some(dims);
- dims
- }
- };
-
- // A keyframe on request comes from the running encoder. Rebuilding it (a new
- // stream config plus a full keyframe) is the fallback for encoders that can't.
- let requested_kf = force_keyframe.swap(false, Ordering::SeqCst);
- let keyframe_in_place = requested_kf && encoder.as_ref().is_some_and(H264Encoder::request_keyframe);
- let need_reinit = encoder.is_none()
- || (requested_kf && !keyframe_in_place && last_reinit.elapsed() >= Duration::from_millis(1400));
-
- if need_reinit {
- match H264Encoder::new(dst_w, dst_h, fps, bitrate) {
- Ok(enc) => {
- let cfg = MirrorConfig {
- codec: nectarlink_core::MIRROR_H264.into(),
- width: dst_w,
- height: dst_h,
- session: nectarlink_core::MIRROR_SCREEN,
- };
- if matches!(sender.send(PacketKind::Config, 0, cfg.to_cbor()), MirrorSend::Closed) {
- break;
- }
- encoder = Some(enc);
- last_reinit = Instant::now();
- stats.rebuilds += 1;
- }
- Err(e) => {
- tracing::warn!(error = %e, "PC mirror H.264 encoder init failed");
- break;
- }
- }
- }
-
- let captured = match (place, window_capture.as_mut()) {
- (Some(place), Some(capture)) => capture.capture(place.in_window, dst_w, dst_h, &mut bgra),
- (Some(place), None) => capture_screen(Some(place.on_screen), dst_w, dst_h, &mut bgra).is_some(),
- (None, _) => capture_screen(None, dst_w, dst_h, &mut bgra).is_some(),
- };
- if captured && let Some(enc) = encoder.as_mut() {
- bgra_to_nv12(&bgra, dst_w as usize, dst_h as usize, &mut nv12);
- let time_us = started.elapsed().as_micros() as u64;
- let hns = (time_us * 10) as i64;
- match enc.encode(&nv12, hns, dur_hns) {
- Ok(packets) => {
- let mut send_failed = false;
- for (is_keyframe, bytes) in packets {
- let kind = if is_keyframe { PacketKind::Keyframe } else { PacketKind::Frame };
- let len = bytes.len();
- match sender.send(kind, time_us, bytes) {
- MirrorSend::Queued => {
- stats.frames += 1;
- stats.bytes += len as u64;
- if is_keyframe {
- stats.keyframes += 1;
- }
- }
- MirrorSend::Dropped => stats.dropped += 1,
- MirrorSend::NeedKeyframe => {
- stats.dropped += 1;
- last_drop = Some(Instant::now());
- force_keyframe.store(true, Ordering::SeqCst);
- // The link is slower than the stream: ask for less, at most every 2 s.
- if last_rate_change.elapsed() >= Duration::from_secs(2) {
- let lower = (bitrate / 4 * 3).max(MIN_BITRATE);
- if lower < bitrate && enc.set_bitrate(lower) {
- bitrate = lower;
- last_rate_change = Instant::now();
- }
- }
- }
- MirrorSend::Closed => {
- send_failed = true;
- break;
- }
- }
- }
- if send_failed {
- break;
- }
- // Clean for 10 s: step the bitrate back up toward what the phone asked for.
- let clean = last_drop.is_none_or(|t| t.elapsed() >= Duration::from_secs(10));
- if clean && bitrate < max_bitrate && last_rate_change.elapsed() >= Duration::from_secs(10)
- {
- let higher = (bitrate / 4 * 5).min(max_bitrate);
- if enc.set_bitrate(higher) {
- bitrate = higher;
- last_rate_change = Instant::now();
- }
- }
- }
- Err(e) => {
- tracing::warn!(error = %e, "PC mirror frame encode error; rebuilding the encoder");
- encoder = None;
- }
- }
- }
-
- if stats_since.elapsed() >= Duration::from_secs(10) {
- let secs = stats_since.elapsed().as_secs_f64();
- tracing::info!(
- %peer,
- fps = format!("{:.1}", stats.frames as f64 / secs),
- kbps = (stats.bytes as f64 * 8.0 / secs / 1000.0) as u64,
- keyframes = stats.keyframes,
- dropped = stats.dropped,
- rebuilds = stats.rebuilds,
- bitrate,
- "sharing this PC's screen"
- );
- stats = Stats::default();
- stats_since = Instant::now();
- }
-
- let elapsed = tick_start.elapsed();
- if let Some(rem) = frame_interval.checked_sub(elapsed) {
- std::thread::sleep(rem);
- }
- }
-
- tracing::info!(%peer, "stopped sharing this PC's screen");
- crate::input_share::leave_remote();
- sender.close();
- cleanup_session(&stop);
-}
-
-/// Mute this PC's speakers while a phone watches (the setting).
-static MUTE_WHILE_WATCHED: AtomicBool = AtomicBool::new(true);
-
-pub fn set_mute_while_watched(on: bool) {
- MUTE_WHILE_WATCHED.store(on, Ordering::Relaxed);
-}
-
-/// The PC's sound going to a phone that watches one of its players (as
-/// 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.
-struct SoundToPhone {
- was_on: bool,
- was_target: Option<DeviceId>,
- /// This muted the speakers (they weren't already).
- muted: bool,
-}
-
-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,
- };
- crate::pc_audio_stream::set_target(Some(peer));
- crate::pc_audio_stream::set_enabled(true);
- sound
- }
-}
-
-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);
- // Unmuted only if this muted it.
- if self.muted {
- let _ = win::audio::set_speaker_mute(false);
- }
- }
-}
-
-fn cleanup_session(stop: &Arc<AtomicBool>) {
- let removed = {
- let mut guard = ACTIVE.lock().unwrap_or_else(|e| e.into_inner());
- if guard.as_ref().is_some_and(|s| Arc::ptr_eq(&s.stop, stop)) {
- guard.take().is_some()
- } else {
- false
- }
- };
- if removed {
- core_host::host().hub.changed(crate::state::Changes::REMOTE);
- core_host::refresh_capabilities();
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn encodes_nv12_frames_and_decodes_back_with_mft() {
- let (w, h) = target_dimensions(1920, 1080, 1280);
- assert_eq!(w % 16, 0);
- assert_eq!(h % 16, 0);
-
- let mut enc = H264Encoder::new(w, h, 30, 4_000_000).expect("H264Encoder MFT");
- let mut dec = crate::win::h264::Decoder::new_for_codec("h264").expect("H264 Decoder MFT");
-
- let mut bgra = vec![0u8; (w as usize) * (h as usize) * 4];
- for (i, px) in bgra.as_chunks_mut::<4>().0.iter_mut().enumerate() {
- px[0] = (i % 255) as u8;
- px[1] = ((i / 7) % 255) as u8;
- px[2] = ((i / 13) % 255) as u8;
- px[3] = 255;
- }
- let mut nv12 = Vec::new();
- bgra_to_nv12(&bgra, w as usize, h as usize, &mut nv12);
-
- let mut decoded_any = false;
- let mut produced_packets = 0usize;
- for i in 0..8 {
- let packets = enc.encode(&nv12, i * 333_333, 333_333).expect("encode frame");
- for (_keyframe, data) in packets {
- produced_packets += 1;
- let pics = dec.decode(&data, (i as u64) * 33_333).expect("decode packet");
- if let Some(pic) = pics.first() {
- assert_eq!(pic.width, w);
- assert_eq!(pic.height, h);
- decoded_any = true;
- }
- }
- }
- assert!(produced_packets > 0, "encoder must emit H.264 access units");
- assert!(decoded_any, "decoder must reconstruct at least one frame from H264Encoder");
- }
-
- /// With a Chromium browser showing a video on a page whose title has
- /// `NECTARLINK_TEST_TAB` in it, in view: captures just the video, as
- /// Play on phone does.
- #[test]
- #[ignore]
- fn captures_a_browsers_video() {
- use crate::win::browser::{Automation, window_for};
- let wanted = std::env::var("NECTARLINK_TEST_TAB").expect("NECTARLINK_TEST_TAB");
- // SAFETY: COM for this test's thread.
- let _ = unsafe {
- windows::Win32::System::Com::CoInitializeEx(
- None,
- windows::Win32::System::Com::COINIT_MULTITHREADED,
- )
- };
- crate::win::browser::use_physical_pixels();
- let window = window_for("Chrome", &wanted).expect("the test tab's window");
- let rect = Automation::new().unwrap().video_rect(&window).expect("the video");
- let (sw, sh) = ((rect.right - rect.left) as u32, (rect.bottom - rect.top) as u32);
- let (w, h) = target_dimensions(sw, sh, 1280);
- let mut bgra = Vec::new();
- assert_eq!(capture_screen(Some(rect), w, h, &mut bgra), Some((sw, sh)));
- let distinct: std::collections::HashSet<&[u8; 4]> = bgra.as_chunks::<4>().0.iter().collect();
- eprintln!("{rect:?} as {w}x{h}: {} colors", distinct.len());
- assert!(distinct.len() > 50, "a moving test pattern, not a blank");
- }
-
- #[test]
- fn keyframes_come_on_request_without_rebuilding_the_encoder() {
- let (w, h) = target_dimensions(1280, 720, 1280);
- let mut enc = H264Encoder::new(w, h, 30, 2_000_000).expect("H264Encoder MFT");
- let mut nv12 = Vec::new();
- bgra_to_nv12(&vec![128u8; (w as usize) * (h as usize) * 4], w as usize, h as usize, &mut nv12);
-
- let keyframes_after = |enc: &mut H264Encoder, start: i64, count: i64| {
- (start..start + count)
- .flat_map(|i| enc.encode(&nv12, i * 333_333, 333_333).expect("encode frame"))
- .filter(|(keyframe, data)| *keyframe && has_h264_idr(data))
- .count()
- };
- // The first frame is a keyframe; the next few (well inside the 2 s GOP) are not.
- assert_eq!(keyframes_after(&mut enc, 0, 1), 1, "the stream starts with a keyframe");
- assert_eq!(keyframes_after(&mut enc, 1, 5), 0, "no keyframes mid-GOP unprompted");
- assert!(enc.request_keyframe(), "the Windows H.264 encoder takes keyframe requests");
- assert_eq!(keyframes_after(&mut enc, 6, 2), 1, "a requested keyframe arrives promptly");
- assert!(enc.set_bitrate(1_000_000), "the bitrate changes in place");
- }
-}
diff --git a/desktop/app/src/play_on_phone.rs b/desktop/app/src/play_on_phone.rs
new file mode 100644
index 0000000..79386b6
--- /dev/null
+++ b/desktop/app/src/play_on_phone.rs
@@ -0,0 +1,414 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//! Play on phone, on this PC (docs/protocol/media.md §4): moves what one of
+//! its players plays to a phone. A protected streaming service opens on
+//! the phone in its app and the PC pauses; anything else the phone watches
+//! streamed from the PC, which keeps playing it.
+
+#[cfg(windows)]
+mod imp {
+
+ #![allow(unsafe_code)]
+
+ use crate::i18n::tr;
+ use std::{
+ sync::{
+ Arc, Mutex,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::{Duration, Instant},
+ };
+
+ use nectarlink_core::{DeviceId, Error, FeatureState, LinkState, MediaAction, PlayOnPhone};
+ use windows::Win32::{
+ Foundation::{HWND, RECT},
+ System::Com::{COINIT_MULTITHREADED, CoInitializeEx},
+ };
+
+ use crate::{
+ bridge::app::{describe, show_message},
+ core_host,
+ win::browser::{self, Automation, Window},
+ };
+
+ /// The phone to play on: the first one online that can watch a PC's
+ /// playback, else the first one online.
+ pub fn phone() -> Option<(DeviceId, String)> {
+ core_host::host().hub.read(|s| {
+ let online: Vec<_> =
+ s.devices.iter().filter(|d| matches!(d.link, LinkState::Online { .. })).collect();
+ let watches = |id: &DeviceId| {
+ s.matrices.get(id).and_then(|m| m.state("media.play_on_phone"))
+ == Some(FeatureState::Available)
+ };
+ online
+ .iter()
+ .find(|d| watches(&d.id))
+ .or_else(|| online.first())
+ .map(|d| (d.id, d.info.name.clone()))
+ })
+ }
+
+ /// Apps (by player ID or name) that play music, never a video worth
+ /// watching on a phone, unless they say otherwise.
+ const MUSIC_APPS: &[&str] = &[
+ "spotify",
+ "applemusic",
+ "itunes",
+ "zunemusic",
+ "foobar2000",
+ "musicbee",
+ "aimp",
+ "winamp",
+ "tidal",
+ "deezer",
+ "amazonmusic",
+ "youtube music",
+ "soundcloud",
+ ];
+
+ /// Whether a player looks like it plays a video: it says so, or it is
+ /// anything but a music app (browsers say "music" for every video).
+ pub(crate) fn plays_video(id: &str, app: &str, says_video: bool) -> bool {
+ if says_video {
+ return true;
+ }
+ let (id, app) = (id.to_ascii_lowercase(), app.to_ascii_lowercase());
+ !MUSIC_APPS.iter().any(|m| id.contains(m) || app.contains(m))
+ }
+
+ /// The player of this PC's to offer for Play on phone: the first one playing
+ /// a video.
+ pub fn watchable() -> Option<String> {
+ crate::media::local_views()
+ .into_iter()
+ .map(|v| v.player)
+ .find(|p| p.playing && plays_video(&p.id, &p.app, crate::win::media_sessions::says_video(&p.id)))
+ .map(|p| p.id)
+ }
+
+ /// When a phone was last asked to watch (its notification is up).
+ static ASKED: Mutex<Option<Instant>> = Mutex::new(None);
+ /// How long a phone has to accept before the toggle gives up waiting.
+ const ASK_FOR: Duration = Duration::from_secs(60);
+
+ /// Where Play on phone is: `"off"`, `"asking"` (the phone's notification
+ /// is up) or `"watching"`.
+ pub fn state() -> &'static str {
+ if crate::pc_mirror::is_watching() {
+ *ASKED.lock().unwrap_or_else(|e| e.into_inner()) = None;
+ return "watching";
+ }
+ match *ASKED.lock().unwrap_or_else(|e| e.into_inner()) {
+ Some(at) if at.elapsed() < ASK_FOR => "asking",
+ _ => "off",
+ }
+ }
+
+ fn asked() {
+ *ASKED.lock().unwrap_or_else(|e| e.into_inner()) = Some(Instant::now());
+ core_host::host().hub.changed(crate::state::Changes::MEDIA);
+ // The toggle stops waiting when the phone doesn't answer.
+ core_host::spawn(async {
+ tokio::time::sleep(ASK_FOR).await;
+ core_host::host().hub.changed(crate::state::Changes::MEDIA);
+ });
+ }
+
+ /// The Play on phone toggle: on plays the video playing now on the phone;
+ /// off stops the phone watching (or waiting to).
+ pub fn set_on(on: bool) {
+ if on {
+ match watchable() {
+ Some(player) => play(player),
+ None => core_host::host().hub.changed(crate::state::Changes::MEDIA),
+ }
+ return;
+ }
+ *ASKED.lock().unwrap_or_else(|e| e.into_inner()) = None;
+ crate::pc_mirror::stop_watching();
+ core_host::host().hub.changed(crate::state::Changes::MEDIA);
+ }
+
+ /// What this PC's `player` plays: its title, and where it is, in seconds.
+ fn now_playing(player: &str) -> (String, Option<u32>) {
+ let views = crate::media::local_views();
+ let Some(view) = views.into_iter().find(|v| v.player.id == player) else {
+ return (String::new(), None);
+ };
+ let position = view.player.position.map(|ms| {
+ let since =
+ if view.player.playing { view.at.elapsed().map_or(0, |d| d.as_millis() as u64) } else { 0 };
+ ((ms + since) / 1000) as u32
+ });
+ (view.player.title.unwrap_or_default(), position.filter(|&s| s > 0))
+ }
+
+ /// Plays what this PC's `player` plays on a phone (the Play on phone
+ /// button on the PC's media card).
+ pub fn play(player: String) {
+ let Some((phone, name)) = phone() else {
+ return show_message(tr!("PlayOnPhone", "Connect your phone to play on it."));
+ };
+ let spawned = std::thread::Builder::new().name("play-on-phone".into()).spawn(move || {
+ // SAFETY: COM for this short-lived thread (UI Automation).
+ let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
+ let (title, position) = now_playing(&player);
+ let window = browser::window_for(&player, &title);
+ // A video file in a desktop player: the phone plays the file itself.
+ let file = crate::media_file::file_for(&player, &title);
+ let url = window
+ .as_ref()
+ .filter(|w| browser::is_browser(&w.program))
+ .and_then(|w| Automation::new().ok()?.page_url(w));
+ let plan = nectarlink_core::play_on_phone::plan(url.as_deref(), position);
+ let Some(node) = core_host::node() else { return };
+ core_host::spawn(async move {
+ if let Some(file) = file {
+ let shown = (!title.is_empty()).then_some(title);
+ let at = u64::from(position.unwrap_or(0)) * 1000;
+ match node.offer_watch_file(phone, player, shown, file, at).await {
+ Ok(()) => {
+ asked();
+ show_message(tr!("PlayOnPhone", "Tap the notification on %1 to watch.", name));
+ }
+ Err(e) => show_message(failed(&e, &name)),
+ }
+ return;
+ }
+ match plan {
+ PlayOnPhone::Open { url, service } => match node.open_link(phone, url).await {
+ Ok(()) => {
+ crate::media::command(
+ crate::media::LOCAL_DEVICE,
+ player,
+ MediaAction::Pause,
+ None,
+ );
+ show_message(tr!(
+ "PlayOnPhone",
+ "Opening in %1 on %2. Tap the notification there.",
+ service,
+ name
+ ));
+ }
+ Err(e) => show_message(failed(&e, &name)),
+ },
+ PlayOnPhone::Stream { open_instead } => {
+ let shown = (!title.is_empty()).then_some(title);
+ match node.offer_watch(phone, player.clone(), shown).await {
+ Ok(()) => {
+ asked();
+ show_message(tr!("PlayOnPhone", "Tap the notification on %1 to watch.", name))
+ }
+ // A phone that can't watch yet: the page opens there instead.
+ Err(Error::Unsupported) if let Some(url) = open_instead => {
+ match node.open_link(phone, url).await {
+ Ok(()) => {
+ crate::media::command(
+ crate::media::LOCAL_DEVICE,
+ player,
+ MediaAction::Pause,
+ None,
+ );
+ show_message(tr!(
+ "PlayOnPhone",
+ "Sent to %1. Tap the notification there.",
+ name
+ ));
+ }
+ Err(e) => show_message(failed(&e, &name)),
+ }
+ }
+ Err(e) => show_message(failed(&e, &name)),
+ }
+ }
+ }
+ });
+ });
+ if let Err(e) = spawned {
+ tracing::warn!(error = %e, "can't start Play on phone");
+ }
+ }
+
+ fn failed(error: &Error, phone: &str) -> String {
+ match error {
+ Error::Unsupported => tr!("PlayOnPhone", "Update NectarLink on %1 to play on it.", phone),
+ Error::Denied => tr!("PlayOnPhone", "Media is turned off for %1.", phone),
+ other => describe(other),
+ }
+ }
+
+ /// Where a watched player's picture is, kept up to date while a phone
+ /// watches it: the video moves when the page scrolls, the window moves or
+ /// goes full screen.
+ pub struct Watched {
+ place: Arc<Mutex<Option<Place>>>,
+ stop: Arc<AtomicBool>,
+ }
+
+ /// A watched player's picture: its window, and where in it the picture is.
+ #[derive(Debug, Clone, Copy)]
+ pub struct Place {
+ /// The window's handle (as a number: handles go between threads).
+ window: isize,
+ /// The picture, in physical screen pixels.
+ pub on_screen: RECT,
+ /// The picture, in the window's own pixels (from its frame's top-left),
+ /// as a capture of the window has it.
+ pub in_window: RECT,
+ }
+
+ /// How often the picture's place is looked up again.
+ const FOLLOW_EVERY: Duration = Duration::from_millis(500);
+ /// How long a browser's player may take to show up in UI Automation
+ /// before the whole page is streamed instead.
+ const FIND_PLAYER: Duration = Duration::from_secs(2);
+
+ impl Place {
+ /// The window the picture is in.
+ pub fn window(&self) -> HWND {
+ HWND(self.window as *mut core::ffi::c_void)
+ }
+ }
+
+ impl Watched {
+ /// Starts following `player`'s picture.
+ pub fn follow(player: &str) -> Watched {
+ let place = Arc::new(Mutex::new(None));
+ let stop = Arc::new(AtomicBool::new(false));
+ let (found, stopped, player) = (place.clone(), stop.clone(), player.to_owned());
+ let spawned = std::thread::Builder::new().name("play-on-phone-follow".into()).spawn(move || {
+ // SAFETY: COM for this thread, which ends with the watching.
+ let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
+ // Places in the pixels the capture uses.
+ browser::use_physical_pixels();
+ let automation = Automation::new().ok();
+ let mut window: Option<Window> = None;
+ let started = std::time::Instant::now();
+ while !stopped.load(Ordering::Relaxed) {
+ // The window it played in, while it's still there; else find it again.
+ if window.as_ref().is_none_or(|w| browser::frame_of(w.hwnd).is_none()) {
+ let (title, _) = now_playing(&player);
+ window = browser::window_for(&player, &title);
+ }
+ // Minimized, it draws nothing and the phone's picture stops:
+ // back behind the other windows, it plays on out of the way.
+ if let Some(w) = window.as_ref().filter(|w| browser::is_minimized(w.hwnd)) {
+ tracing::info!(
+ "the watched player's window was minimized; restoring it behind the others"
+ );
+ browser::restore_behind(w.hwnd);
+ std::thread::sleep(FOLLOW_EVERY);
+ continue;
+ }
+ let place = window.as_ref().and_then(|w| {
+ let frame = browser::frame_of(w.hwnd)?;
+ let on_screen = match &automation {
+ // A browser builds what UI Automation sees of a page
+ // only once asked: give the player a moment to show
+ // up before streaming the whole page.
+ Some(a) if browser::is_browser(&w.program) => match a.player_rect(w) {
+ Some(player) => player,
+ None if started.elapsed() < FIND_PLAYER => return None,
+ None => a.page_rect(w).unwrap_or(frame),
+ },
+ _ => frame,
+ };
+ let in_window = RECT {
+ left: on_screen.left - frame.left,
+ top: on_screen.top - frame.top,
+ right: on_screen.right - frame.left,
+ bottom: on_screen.bottom - frame.top,
+ };
+ Some(Place { window: w.hwnd.0 as isize, on_screen, in_window })
+ });
+ if place.is_some() {
+ *found.lock().unwrap_or_else(|e| e.into_inner()) = place;
+ }
+ std::thread::sleep(FOLLOW_EVERY);
+ }
+ });
+ if let Err(e) = spawned {
+ tracing::warn!(error = %e, "can't follow the watched player");
+ }
+ Watched { place, stop }
+ }
+
+ /// Where the picture is now, once known.
+ pub fn place(&self) -> Option<Place> {
+ *self.place.lock().unwrap_or_else(|e| e.into_inner())
+ }
+ }
+
+ impl Drop for Watched {
+ fn drop(&mut self) {
+ self.stop.store(true, Ordering::Relaxed);
+ }
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use super::plays_video;
+
+ #[test]
+ fn music_apps_are_not_offered_unless_they_say_video() {
+ assert!(plays_video("chrome", "YouTube", false), "a browser's video says music");
+ assert!(plays_video("F0DC299D809B9700", "Zen", false));
+ assert!(plays_video(r"C:/Program Files/VideoLAN/VLC/vlc.exe", "vlc", false));
+ assert!(!plays_video("Spotify.exe", "Spotify", false));
+ assert!(!plays_video("chrome", "YouTube Music", false), "named by its site");
+ assert!(!plays_video(
+ "Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic",
+ "Media Player",
+ false
+ ));
+ assert!(plays_video(
+ "Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic",
+ "Media Player",
+ true
+ ));
+ }
+ }
+}
+
+#[cfg(not(windows))]
+mod imp {
+ #![allow(dead_code)]
+
+ use nectarlink_core::DeviceId;
+
+ pub fn phone() -> Option<(DeviceId, String)> {
+ None
+ }
+
+ pub fn watchable() -> Option<String> {
+ None
+ }
+
+ pub fn state() -> &'static str {
+ "idle"
+ }
+
+ pub fn set_on(_on: bool) {}
+
+ pub fn play(_player: String) {}
+
+ pub struct Watched;
+
+ #[derive(Debug, Clone, Copy)]
+ pub struct Place {
+ pub window: isize,
+ }
+
+ impl Watched {
+ pub fn follow(_player: &str) -> Watched {
+ Watched
+ }
+
+ pub fn place(&self) -> Option<Place> {
+ None
+ }
+ }
+}
+
+pub use imp::*;
diff --git a/desktop/app/src/play_on_phone/mod.rs b/desktop/app/src/play_on_phone/mod.rs
deleted file mode 100644
index 1eba1bd..0000000
--- a/desktop/app/src/play_on_phone/mod.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Play media on phone and window tracking.
-
-#[cfg(windows)]
-mod windows;
-#[cfg(windows)]
-pub use self::windows::*;
-
-#[cfg(not(windows))]
-mod non_windows;
-#[cfg(not(windows))]
-pub use self::non_windows::*;
diff --git a/desktop/app/src/play_on_phone/non_windows.rs b/desktop/app/src/play_on_phone/non_windows.rs
deleted file mode 100644
index 4d7d918..0000000
--- a/desktop/app/src/play_on_phone/non_windows.rs
+++ /dev/null
@@ -1,37 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! macOS / non-Windows Play on Phone stubs.
-
-use nectarlink_core::DeviceId;
-
-pub fn phone() -> Option<(DeviceId, String)> {
- None
-}
-
-pub fn watchable() -> Option<String> {
- None
-}
-
-pub fn state() -> &'static str {
- "idle"
-}
-
-pub fn set_on(_on: bool) {}
-
-pub fn play(_player: String) {}
-
-pub struct Watched;
-
-#[derive(Debug, Clone, Copy)]
-pub struct Place {
- pub window: isize,
-}
-
-impl Watched {
- pub fn follow(_player: &str) -> Watched {
- Watched
- }
-
- pub fn place(&self) -> Option<Place> {
- None
- }
-}
diff --git a/desktop/app/src/play_on_phone/windows.rs b/desktop/app/src/play_on_phone/windows.rs
deleted file mode 100644
index 55fd756..0000000
--- a/desktop/app/src/play_on_phone/windows.rs
+++ /dev/null
@@ -1,350 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Play on phone, on this PC (docs/protocol/media.md §4): moves what one of
-//! its players plays to a phone. A protected streaming service opens on
-//! the phone in its app and the PC pauses; anything else the phone watches
-//! streamed from the PC, which keeps playing it.
-
-#![allow(unsafe_code)]
-
-use crate::i18n::tr;
-use std::{
- sync::{
- Arc, Mutex,
- atomic::{AtomicBool, Ordering},
- },
- time::{Duration, Instant},
-};
-
-use nectarlink_core::{DeviceId, Error, FeatureState, LinkState, MediaAction, PlayOnPhone};
-use windows::Win32::{
- Foundation::{HWND, RECT},
- System::Com::{COINIT_MULTITHREADED, CoInitializeEx},
-};
-
-use crate::{
- bridge::app::{describe, show_message},
- core_host,
- win::browser::{self, Automation, Window},
-};
-
-/// The phone to play on: the first one online that can watch a PC's
-/// playback, else the first one online.
-pub fn phone() -> Option<(DeviceId, String)> {
- core_host::host().hub.read(|s| {
- let online: Vec<_> =
- s.devices.iter().filter(|d| matches!(d.link, LinkState::Online { .. })).collect();
- let watches = |id: &DeviceId| {
- s.matrices.get(id).and_then(|m| m.state("media.play_on_phone")) == Some(FeatureState::Available)
- };
- online.iter().find(|d| watches(&d.id)).or_else(|| online.first()).map(|d| (d.id, d.info.name.clone()))
- })
-}
-
-/// Apps (by player ID or name) that play music, never a video worth
-/// watching on a phone, unless they say otherwise.
-const MUSIC_APPS: &[&str] = &[
- "spotify",
- "applemusic",
- "itunes",
- "zunemusic",
- "foobar2000",
- "musicbee",
- "aimp",
- "winamp",
- "tidal",
- "deezer",
- "amazonmusic",
- "youtube music",
- "soundcloud",
-];
-
-/// Whether a player looks like it plays a video: it says so, or it is
-/// anything but a music app (browsers say "music" for every video).
-pub(crate) fn plays_video(id: &str, app: &str, says_video: bool) -> bool {
- if says_video {
- return true;
- }
- let (id, app) = (id.to_ascii_lowercase(), app.to_ascii_lowercase());
- !MUSIC_APPS.iter().any(|m| id.contains(m) || app.contains(m))
-}
-
-/// The player of this PC's to offer for Play on phone: the first one playing
-/// a video.
-pub fn watchable() -> Option<String> {
- crate::media::local_views()
- .into_iter()
- .map(|v| v.player)
- .find(|p| p.playing && plays_video(&p.id, &p.app, crate::win::media_sessions::says_video(&p.id)))
- .map(|p| p.id)
-}
-
-/// When a phone was last asked to watch (its notification is up).
-static ASKED: Mutex<Option<Instant>> = Mutex::new(None);
-/// How long a phone has to accept before the toggle gives up waiting.
-const ASK_FOR: Duration = Duration::from_secs(60);
-
-/// Where Play on phone is: `"off"`, `"asking"` (the phone's notification
-/// is up) or `"watching"`.
-pub fn state() -> &'static str {
- if crate::pc_mirror::is_watching() {
- *ASKED.lock().unwrap_or_else(|e| e.into_inner()) = None;
- return "watching";
- }
- match *ASKED.lock().unwrap_or_else(|e| e.into_inner()) {
- Some(at) if at.elapsed() < ASK_FOR => "asking",
- _ => "off",
- }
-}
-
-fn asked() {
- *ASKED.lock().unwrap_or_else(|e| e.into_inner()) = Some(Instant::now());
- core_host::host().hub.changed(crate::state::Changes::MEDIA);
- // The toggle stops waiting when the phone doesn't answer.
- core_host::spawn(async {
- tokio::time::sleep(ASK_FOR).await;
- core_host::host().hub.changed(crate::state::Changes::MEDIA);
- });
-}
-
-/// The Play on phone toggle: on plays the video playing now on the phone;
-/// off stops the phone watching (or waiting to).
-pub fn set_on(on: bool) {
- if on {
- match watchable() {
- Some(player) => play(player),
- None => core_host::host().hub.changed(crate::state::Changes::MEDIA),
- }
- return;
- }
- *ASKED.lock().unwrap_or_else(|e| e.into_inner()) = None;
- crate::pc_mirror::stop_watching();
- core_host::host().hub.changed(crate::state::Changes::MEDIA);
-}
-
-/// What this PC's `player` plays: its title, and where it is, in seconds.
-fn now_playing(player: &str) -> (String, Option<u32>) {
- let views = crate::media::local_views();
- let Some(view) = views.into_iter().find(|v| v.player.id == player) else {
- return (String::new(), None);
- };
- let position = view.player.position.map(|ms| {
- let since =
- if view.player.playing { view.at.elapsed().map_or(0, |d| d.as_millis() as u64) } else { 0 };
- ((ms + since) / 1000) as u32
- });
- (view.player.title.unwrap_or_default(), position.filter(|&s| s > 0))
-}
-
-/// Plays what this PC's `player` plays on a phone (the Play on phone
-/// button on the PC's media card).
-pub fn play(player: String) {
- let Some((phone, name)) = phone() else {
- return show_message(tr!("PlayOnPhone", "Connect your phone to play on it."));
- };
- let spawned = std::thread::Builder::new().name("play-on-phone".into()).spawn(move || {
- // SAFETY: COM for this short-lived thread (UI Automation).
- let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
- let (title, position) = now_playing(&player);
- let window = browser::window_for(&player, &title);
- // A video file in a desktop player: the phone plays the file itself.
- let file = crate::media_file::file_for(&player, &title);
- let url = window
- .as_ref()
- .filter(|w| browser::is_browser(&w.program))
- .and_then(|w| Automation::new().ok()?.page_url(w));
- let plan = nectarlink_core::play_on_phone::plan(url.as_deref(), position);
- let Some(node) = core_host::node() else { return };
- core_host::spawn(async move {
- if let Some(file) = file {
- let shown = (!title.is_empty()).then_some(title);
- let at = u64::from(position.unwrap_or(0)) * 1000;
- match node.offer_watch_file(phone, player, shown, file, at).await {
- Ok(()) => {
- asked();
- show_message(tr!("PlayOnPhone", "Tap the notification on %1 to watch.", name));
- }
- Err(e) => show_message(failed(&e, &name)),
- }
- return;
- }
- match plan {
- PlayOnPhone::Open { url, service } => match node.open_link(phone, url).await {
- Ok(()) => {
- crate::media::command(crate::media::LOCAL_DEVICE, player, MediaAction::Pause, None);
- show_message(tr!(
- "PlayOnPhone",
- "Opening in %1 on %2. Tap the notification there.",
- service,
- name
- ));
- }
- Err(e) => show_message(failed(&e, &name)),
- },
- PlayOnPhone::Stream { open_instead } => {
- let shown = (!title.is_empty()).then_some(title);
- match node.offer_watch(phone, player.clone(), shown).await {
- Ok(()) => {
- asked();
- show_message(tr!("PlayOnPhone", "Tap the notification on %1 to watch.", name))
- }
- // A phone that can't watch yet: the page opens there instead.
- Err(Error::Unsupported) if let Some(url) = open_instead => {
- match node.open_link(phone, url).await {
- Ok(()) => {
- crate::media::command(
- crate::media::LOCAL_DEVICE,
- player,
- MediaAction::Pause,
- None,
- );
- show_message(tr!(
- "PlayOnPhone",
- "Sent to %1. Tap the notification there.",
- name
- ));
- }
- Err(e) => show_message(failed(&e, &name)),
- }
- }
- Err(e) => show_message(failed(&e, &name)),
- }
- }
- }
- });
- });
- if let Err(e) = spawned {
- tracing::warn!(error = %e, "can't start Play on phone");
- }
-}
-
-fn failed(error: &Error, phone: &str) -> String {
- match error {
- Error::Unsupported => tr!("PlayOnPhone", "Update NectarLink on %1 to play on it.", phone),
- Error::Denied => tr!("PlayOnPhone", "Media is turned off for %1.", phone),
- other => describe(other),
- }
-}
-
-/// Where a watched player's picture is, kept up to date while a phone
-/// watches it: the video moves when the page scrolls, the window moves or
-/// goes full screen.
-pub struct Watched {
- place: Arc<Mutex<Option<Place>>>,
- stop: Arc<AtomicBool>,
-}
-
-/// A watched player's picture: its window, and where in it the picture is.
-#[derive(Debug, Clone, Copy)]
-pub struct Place {
- /// The window's handle (as a number: handles go between threads).
- window: isize,
- /// The picture, in physical screen pixels.
- pub on_screen: RECT,
- /// The picture, in the window's own pixels (from its frame's top-left),
- /// as a capture of the window has it.
- pub in_window: RECT,
-}
-
-/// How often the picture's place is looked up again.
-const FOLLOW_EVERY: Duration = Duration::from_millis(500);
-/// How long a browser's player may take to show up in UI Automation
-/// before the whole page is streamed instead.
-const FIND_PLAYER: Duration = Duration::from_secs(2);
-
-impl Place {
- /// The window the picture is in.
- pub fn window(&self) -> HWND {
- HWND(self.window as *mut core::ffi::c_void)
- }
-}
-
-impl Watched {
- /// Starts following `player`'s picture.
- pub fn follow(player: &str) -> Watched {
- let place = Arc::new(Mutex::new(None));
- let stop = Arc::new(AtomicBool::new(false));
- let (found, stopped, player) = (place.clone(), stop.clone(), player.to_owned());
- let spawned = std::thread::Builder::new().name("play-on-phone-follow".into()).spawn(move || {
- // SAFETY: COM for this thread, which ends with the watching.
- let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
- // Places in the pixels the capture uses.
- browser::use_physical_pixels();
- let automation = Automation::new().ok();
- let mut window: Option<Window> = None;
- let started = std::time::Instant::now();
- while !stopped.load(Ordering::Relaxed) {
- // The window it played in, while it's still there; else find it again.
- if window.as_ref().is_none_or(|w| browser::frame_of(w.hwnd).is_none()) {
- let (title, _) = now_playing(&player);
- window = browser::window_for(&player, &title);
- }
- // Minimized, it draws nothing and the phone's picture stops:
- // back behind the other windows, it plays on out of the way.
- if let Some(w) = window.as_ref().filter(|w| browser::is_minimized(w.hwnd)) {
- tracing::info!(
- "the watched player's window was minimized; restoring it behind the others"
- );
- browser::restore_behind(w.hwnd);
- std::thread::sleep(FOLLOW_EVERY);
- continue;
- }
- let place = window.as_ref().and_then(|w| {
- let frame = browser::frame_of(w.hwnd)?;
- let on_screen = match &automation {
- // A browser builds what UI Automation sees of a page
- // only once asked: give the player a moment to show
- // up before streaming the whole page.
- Some(a) if browser::is_browser(&w.program) => match a.player_rect(w) {
- Some(player) => player,
- None if started.elapsed() < FIND_PLAYER => return None,
- None => a.page_rect(w).unwrap_or(frame),
- },
- _ => frame,
- };
- let in_window = RECT {
- left: on_screen.left - frame.left,
- top: on_screen.top - frame.top,
- right: on_screen.right - frame.left,
- bottom: on_screen.bottom - frame.top,
- };
- Some(Place { window: w.hwnd.0 as isize, on_screen, in_window })
- });
- if place.is_some() {
- *found.lock().unwrap_or_else(|e| e.into_inner()) = place;
- }
- std::thread::sleep(FOLLOW_EVERY);
- }
- });
- if let Err(e) = spawned {
- tracing::warn!(error = %e, "can't follow the watched player");
- }
- Watched { place, stop }
- }
-
- /// Where the picture is now, once known.
- pub fn place(&self) -> Option<Place> {
- *self.place.lock().unwrap_or_else(|e| e.into_inner())
- }
-}
-
-impl Drop for Watched {
- fn drop(&mut self) {
- self.stop.store(true, Ordering::Relaxed);
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::plays_video;
-
- #[test]
- fn music_apps_are_not_offered_unless_they_say_video() {
- assert!(plays_video("chrome", "YouTube", false), "a browser's video says music");
- assert!(plays_video("F0DC299D809B9700", "Zen", false));
- assert!(plays_video(r"C:/Program Files/VideoLAN/VLC/vlc.exe", "vlc", false));
- assert!(!plays_video("Spotify.exe", "Spotify", false));
- assert!(!plays_video("chrome", "YouTube Music", false), "named by its site");
- assert!(!plays_video("Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic", "Media Player", false));
- assert!(plays_video("Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic", "Media Player", true));
- }
-}
diff --git a/desktop/app/src/recordings.rs b/desktop/app/src/recordings.rs
index ea1f97d..dc7f3cc 100644
--- a/desktop/app/src/recordings.rs
+++ b/desktop/app/src/recordings.rs
@@ -600,6 +600,7 @@ mod tests {
}
/// Builds a valid 48 kHz mono AAC-LC `.m4a` file for Media Foundation tests.
+ #[allow(dead_code)]
fn test_m4a(duration_secs: u32) -> Vec<u8> {
const AAC_FRAME: [u8; 6] = [0x01, 0x48, 0x00, 0x84, 0x21, 0x7E];
const SAMPLE_RATE: u32 = 48_000;
diff --git a/desktop/app/src/setup.rs b/desktop/app/src/setup.rs
index e1f49b2..9928acd 100644
--- a/desktop/app/src/setup.rs
+++ b/desktop/app/src/setup.rs
@@ -35,11 +35,14 @@ pub const INSTALLED_ARG: &str = "--installed";
/// Where the Android app is downloaded (the done screen's QR code and link).
pub const ANDROID_URL: &str = "https://github.com/thtbee/nectarlink/releases/latest";
-const APP_EXE: &str = "nectarlink-desktop.exe";
+const APP_EXE: &str = if cfg!(windows) { "nectarlink-desktop.exe" } else { "nectarlink-desktop" };
+#[cfg(windows)]
const UNINSTALL_KEY: &str = r"Software\Microsoft\Windows\CurrentVersion\Uninstall\Nectarlink";
-const SETUP_COPY_NAME: &str = "Nectarlink Uninstall.exe";
+const SETUP_COPY_NAME: &str = if cfg!(windows) { "Nectarlink Uninstall.exe" } else { "Nectarlink Uninstall" };
+#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
+#[cfg(windows)]
const DETACHED_PROCESS: u32 = 0x0000_0008;
/// What the window is for.
diff --git a/desktop/app/src/storage.rs b/desktop/app/src/storage.rs
new file mode 100644
index 0000000..f767b85
--- /dev/null
+++ b/desktop/app/src/storage.rs
@@ -0,0 +1,1309 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//! Phone storage in File Explorer (`docs/protocol/storage.md`, `PLAN.md` §3.6).
+//!
+//! Registers a Windows Cloud Files (`cfapi`) sync root per paired phone under
+//! `%USERPROFILE%\Nectarlink\<Phone name>` when the phone's `storage` toggle is
+//! enabled. Placeholders are populated on demand when folders are opened
+//! (`CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS`) and hydrated on open via ranged
+//! reads (`CF_CALLBACK_TYPE_FETCH_DATA`). Renames, deletes, and new or modified
+//! files dropped into the sync root are forwarded to the phone.
+
+#[cfg(windows)]
+mod imp {
+
+ #![allow(unsafe_code)]
+
+ use std::{
+ collections::{HashMap, HashSet},
+ ffi::{OsStr, c_void},
+ os::windows::ffi::{OsStrExt, OsStringExt},
+ path::{Path, PathBuf},
+ sync::{
+ Arc, Mutex, MutexGuard,
+ atomic::{AtomicBool, Ordering},
+ },
+ time::Duration,
+ };
+
+ use nectarlink_core::{DeviceId, LinkState, NodeEvent, StorageEntry};
+ use windows::{
+ Security::Cryptography::{BinaryStringEncoding, CryptographicBuffer},
+ Storage::{
+ Provider::{
+ StorageProviderHardlinkPolicy, StorageProviderHydrationPolicy,
+ StorageProviderHydrationPolicyModifier, StorageProviderInSyncPolicy,
+ StorageProviderPopulationPolicy, StorageProviderSyncRootInfo, StorageProviderSyncRootManager,
+ },
+ StorageFolder,
+ },
+ Win32::{
+ Foundation::{
+ CloseHandle, GENERIC_READ, GENERIC_WRITE, HANDLE, INVALID_HANDLE_VALUE, NTSTATUS,
+ STATUS_ACCESS_DENIED, STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE, STATUS_CLOUD_FILE_UNSUCCESSFUL,
+ STATUS_SUCCESS,
+ },
+ Storage::{
+ CloudFilters::{
+ CF_CALLBACK_INFO, CF_CALLBACK_PARAMETERS, CF_CALLBACK_REGISTRATION,
+ CF_CALLBACK_TYPE_FETCH_DATA, CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS, CF_CALLBACK_TYPE_NONE,
+ CF_CALLBACK_TYPE_NOTIFY_DELETE, CF_CALLBACK_TYPE_NOTIFY_RENAME,
+ CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH, CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO,
+ CF_CONNECTION_KEY, CF_CONVERT_FLAG_MARK_IN_SYNC, CF_FS_METADATA,
+ CF_IN_SYNC_STATE_IN_SYNC, CF_OPERATION_INFO, CF_OPERATION_PARAMETERS,
+ CF_OPERATION_PARAMETERS_0,
+ CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAG_DISABLE_ON_DEMAND_POPULATION, CF_OPERATION_TYPE,
+ CF_OPERATION_TYPE_ACK_DELETE, CF_OPERATION_TYPE_ACK_RENAME,
+ CF_OPERATION_TYPE_TRANSFER_DATA, CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS,
+ CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC, CF_PLACEHOLDER_CREATE_INFO,
+ CF_PLACEHOLDER_STATE_IN_SYNC, CF_PLACEHOLDER_STATE_PLACEHOLDER, CF_SET_IN_SYNC_FLAG_NONE,
+ CfConnectSyncRoot, CfConvertToPlaceholder, CfCreatePlaceholders, CfDisconnectSyncRoot,
+ CfExecute, CfGetPlaceholderStateFromAttributeTag, CfReportProviderProgress,
+ CfSetInSyncState,
+ },
+ FileSystem::{
+ CreateFileW, FILE_ACTION_ADDED, FILE_ACTION_MODIFIED, FILE_ACTION_RENAMED_NEW_NAME,
+ FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_NORMAL, FILE_BASIC_INFO,
+ FILE_FLAG_BACKUP_SEMANTICS, FILE_LIST_DIRECTORY, FILE_NOTIFY_CHANGE_DIR_NAME,
+ FILE_NOTIFY_CHANGE_FILE_NAME, FILE_NOTIFY_CHANGE_LAST_WRITE, FILE_NOTIFY_CHANGE_SIZE,
+ FILE_NOTIFY_INFORMATION, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, FindClose,
+ FindFirstFileW, OPEN_EXISTING, ReadDirectoryChangesW, WIN32_FIND_DATAW,
+ },
+ },
+ },
+ core::{HSTRING, PCWSTR},
+ };
+
+ use crate::{
+ core_host,
+ state::{Changes, CoreStatus},
+ win,
+ };
+
+ /// Provider prefix for sync root IDs (`NectarLink!<device_id>`).
+ const PROVIDER_PREFIX: &str = "Nectarlink!";
+
+ /// Difference between Windows FILETIME epoch (1601-01-01) and Unix epoch
+ /// (1970-01-01) in 100-nanosecond intervals.
+ const EPOCH_DIFF_100NS: i64 = 116_444_736_000_000_000;
+
+ /// Cloud file network unavailable NTSTATUS (`STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE`).
+ const STATUS_CLOUD_NETWORK_UNAVAILABLE: NTSTATUS = STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE;
+
+ /// Cloud file unsuccessful / cancelled operation NTSTATUS (`STATUS_CLOUD_FILE_UNSUCCESSFUL`).
+ const STATUS_CLOUD_UNSUCCESSFUL: NTSTATUS = STATUS_CLOUD_FILE_UNSUCCESSFUL;
+
+ /// Chunk size for hydrating files via `CF_OPERATION_TYPE_TRANSFER_DATA` (must be
+ /// a multiple of 4096 bytes for non-EOF chunks).
+ const HYDRATE_CHUNK_BYTES: usize = 256 * 1024;
+
+ fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
+ m.lock().unwrap_or_else(|e| e.into_inner())
+ }
+
+ struct ActiveRoot {
+ device: DeviceId,
+ name: String,
+ root_path: PathBuf,
+ connection_key: CF_CONNECTION_KEY,
+ stop_watcher: Arc<AtomicBool>,
+ populated_dirs: Mutex<HashSet<String>>,
+ uploading: Mutex<HashSet<String>>,
+ }
+
+ static ACTIVE_ROOTS: Mutex<Option<HashMap<DeviceId, Arc<ActiveRoot>>>> = Mutex::new(None);
+ static RECONCILING: Mutex<()> = Mutex::new(());
+
+ fn active_roots() -> MutexGuard<'static, Option<HashMap<DeviceId, Arc<ActiveRoot>>>> {
+ lock(&ACTIVE_ROOTS)
+ }
+
+ fn get_active_root(device: &DeviceId) -> Option<Arc<ActiveRoot>> {
+ active_roots().as_ref().and_then(|m| m.get(device).cloned())
+ }
+
+ /// Starts watching paired devices and their `storage` toggles to register or
+ /// unregister File Explorer sync roots.
+ pub fn start() {
+ {
+ let mut map = active_roots();
+ if map.is_none() {
+ *map = Some(HashMap::new());
+ }
+ }
+ core_host::host().hub.subscribe(
+ Changes::DEVICES | Changes::STATUS | Changes::CAPABILITIES | Changes::TOGGLES,
+ || {
+ sync();
+ true
+ },
+ );
+ sync();
+ }
+
+ /// Triggers a reconciliation of active sync roots (e.g. after a device toggle changes).
+ pub fn sync() {
+ let host = core_host::host();
+ let Some(node) = core_host::node() else { return };
+ let ready = host.hub.read(|s| matches!(s.status, Some(CoreStatus::Ready { .. })));
+ if !ready {
+ return;
+ }
+ let devices = host.hub.read(|s| {
+ s.devices
+ .iter()
+ .map(|d| (d.id, d.info.name.clone(), matches!(d.link, LinkState::Online { .. })))
+ .collect::<Vec<_>>()
+ });
+ let mut desired: Vec<(DeviceId, String, bool)> = Vec::new();
+ let mut disabled: Vec<(DeviceId, String)> = Vec::new();
+ for (id, name, online) in devices {
+ let enabled = node
+ .device_toggles(id)
+ .ok()
+ .and_then(|t| t.into_iter().find(|(k, _)| *k == "storage").map(|(_, on)| on))
+ .unwrap_or(false);
+ if enabled {
+ desired.push((id, name, online));
+ } else {
+ disabled.push((id, name));
+ }
+ }
+
+ let _ = std::thread::Builder::new().name("storage-sync".into()).spawn(move || {
+ let _guard = lock(&RECONCILING);
+ win::with_com(|| reconcile(&desired, &disabled));
+ });
+ }
+
+ /// Handles core events (`StorageChanged`, `DeviceRemoved`, `LinkChanged`).
+ pub fn on_event(event: &NodeEvent) {
+ match event {
+ NodeEvent::DeviceRemoved(device) => {
+ let device = *device;
+ let _ = std::thread::Builder::new().name("storage-unpair".into()).spawn(move || {
+ let _guard = lock(&RECONCILING);
+ win::with_com(|| remove_device_root(device, None));
+ });
+ }
+ NodeEvent::DeviceAdded(_)
+ | NodeEvent::PeerInfoChanged { .. }
+ | NodeEvent::Capabilities(_)
+ | NodeEvent::LinkChanged { .. } => {
+ sync();
+ }
+ NodeEvent::StorageChanged { device, path } => {
+ let device = *device;
+ let path = path.clone();
+ if let Some(root) = get_active_root(&device) {
+ core_host::spawn(async move {
+ refresh_folder(root, &path).await;
+ });
+ }
+ }
+ _ => {}
+ }
+ }
+
+ /// Disconnects active sync roots when the desktop app exits.
+ pub fn shutdown() {
+ let roots: Vec<Arc<ActiveRoot>> =
+ active_roots().as_mut().map(|m| m.drain().map(|(_, v)| v).collect()).unwrap_or_default();
+ for root in roots {
+ root.stop_watcher.store(true, Ordering::Relaxed);
+ // SAFETY: connection_key was obtained from CfConnectSyncRoot.
+ let _ = unsafe { CfDisconnectSyncRoot(root.connection_key) };
+ }
+ }
+
+ /// Unregisters all `NectarLink!*` sync roots from Windows (`--uninstall`).
+ pub fn unregister_all() {
+ shutdown();
+ win::with_com(|| {
+ if let Ok(roots) = StorageProviderSyncRootManager::GetCurrentSyncRoots() {
+ let count = roots.Size().unwrap_or(0);
+ for i in 0..count {
+ if let Ok(info) = roots.GetAt(i)
+ && let Ok(id) = info.Id()
+ {
+ let id_str = id.to_string_lossy();
+ if id_str.starts_with(PROVIDER_PREFIX) {
+ let folder_path = info
+ .Path()
+ .ok()
+ .and_then(|f| f.Path().ok())
+ .map(|p| PathBuf::from(p.to_string_lossy()));
+ let _ = StorageProviderSyncRootManager::Unregister(&id);
+ if let Some(path) = folder_path {
+ cleanup_sync_root_dir(&path);
+ }
+ }
+ }
+ }
+ }
+ if let Some(base) = base_sync_dir() {
+ let _ = std::fs::remove_dir(&base);
+ }
+ });
+ }
+
+ /// Returns the local sync root path for a paired phone (`%USERPROFILE%\Nectarlink\<Phone name>`).
+ pub fn sync_root_path_for(device: DeviceId) -> Option<PathBuf> {
+ if let Some(active) = get_active_root(&device) {
+ return Some(active.root_path.clone());
+ }
+ let name = core_host::host().hub.read(|s| s.name_of(&device))?;
+ Some(base_sync_dir()?.join(sanitize_folder_name(&name)))
+ }
+
+ /// Opens a paired phone's sync root folder in File Explorer.
+ pub fn open_in_explorer(device: DeviceId) -> Result<(), String> {
+ let path =
+ sync_root_path_for(device).ok_or_else(|| "Phone storage folder is not available".to_owned())?;
+ if !path.exists() {
+ std::fs::create_dir_all(&path).map_err(|e| e.to_string())?;
+ }
+ win::shell::run(Path::new("explorer.exe"), &format!("\"{}\"", path.display()))
+ }
+
+ fn base_sync_dir() -> Option<PathBuf> {
+ dirs::home_dir().map(|h| h.join("Nectarlink"))
+ }
+
+ fn sync_root_id(device: DeviceId) -> HSTRING {
+ HSTRING::from(format!("{PROVIDER_PREFIX}{device}"))
+ }
+
+ /// Sanitizes a phone's display name into a valid Windows directory name.
+ pub fn sanitize_folder_name(device_name: &str) -> String {
+ let cleaned: String = device_name
+ .chars()
+ .map(|c| if c.is_control() || r#"<>:"/\|?*"#.contains(c) { ' ' } else { c })
+ .collect();
+ let mut name = cleaned.split_whitespace().collect::<Vec<_>>().join(" ");
+ name = name.trim_end_matches(['.', ' ']).chars().take(64).collect();
+ let stem = name.split('.').next().unwrap_or_default().to_ascii_uppercase();
+ let reserved = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")
+ || ((stem.starts_with("COM") || stem.starts_with("LPT"))
+ && stem.len() == 4
+ && stem.as_bytes()[3].is_ascii_digit());
+ if name.is_empty() {
+ "Phone".into()
+ } else if reserved {
+ format!("{name} phone")
+ } else {
+ name
+ }
+ }
+
+ /// Converts a Unix timestamp in milliseconds to Windows FILETIME 100ns ticks
+ /// since 1601-01-01.
+ pub fn unix_ms_to_filetime_ticks(unix_ms: i64) -> i64 {
+ EPOCH_DIFF_100NS.saturating_add(unix_ms.max(0).saturating_mul(10_000))
+ }
+
+ /// Converts Windows FILETIME 100ns ticks since 1601-01-01 to Unix milliseconds.
+ #[cfg(test)]
+ pub fn filetime_ticks_to_unix_ms(ticks: i64) -> i64 {
+ ticks.saturating_sub(EPOCH_DIFF_100NS).max(0) / 10_000
+ }
+
+ /// Computes the slash-separated relative path inside `root_path` from a full
+ /// local path (`""` for the sync root itself). Returns `None` if `full_path` is
+ /// not inside `root_path`.
+ pub fn relative_path_under_root(root_path: &Path, full_path: &Path) -> Option<String> {
+ let root_norm = normalize_win_path(root_path);
+ let full_norm = normalize_win_path(full_path);
+ let root_lower = root_norm.to_lowercase();
+ let full_lower = full_norm.to_lowercase();
+ if full_lower == root_lower {
+ return Some(String::new());
+ }
+ let prefix = format!("{root_lower}\\");
+ if !full_lower.starts_with(&prefix) {
+ return None;
+ }
+ let rel = &full_norm[prefix.len()..];
+ let parts: Vec<&str> = rel.split('\\').filter(|s| !s.is_empty()).collect();
+ if parts.iter().any(|p| *p == "." || *p == "..") {
+ return None;
+ }
+ Some(parts.join("/"))
+ }
+
+ fn normalize_win_path(path: &Path) -> String {
+ let s = path.to_string_lossy();
+ let stripped = s.strip_prefix(r"\\?\").unwrap_or(&s);
+ stripped.trim_end_matches('\\').to_owned()
+ }
+
+ fn reconcile(desired: &[(DeviceId, String, bool)], disabled: &[(DeviceId, String)]) {
+ // Unregister any disabled devices that belong to this instance.
+ for (id, name) in disabled {
+ remove_device_root(*id, Some(name));
+ }
+
+ // Remove any active roots no longer in `desired`.
+ let active_ids: Vec<DeviceId> =
+ active_roots().as_ref().map(|m| m.keys().copied().collect()).unwrap_or_default();
+ for id in active_ids {
+ if !desired.iter().any(|(d, _, _)| *d == id) {
+ remove_device_root(id, None);
+ }
+ }
+
+ // Register and connect desired roots.
+ for (id, name, online) in desired {
+ let already = get_active_root(id);
+ if let Some(existing) = already {
+ if existing.name == *name {
+ if *online {
+ let root = existing.clone();
+ core_host::spawn(async move {
+ let is_root_populated = lock(&root.populated_dirs).contains("");
+ if is_root_populated {
+ refresh_folder(root, "").await;
+ }
+ });
+ }
+ continue;
+ }
+ // Name changed: replace root.
+ remove_device_root(*id, Some(&existing.name));
+ }
+
+ if let Err(e) = register_and_connect(*id, name, *online) {
+ tracing::warn!(device = %id.short(), error = %e, "can't register phone storage sync root");
+ }
+ }
+ }
+
+ fn remove_device_root(device: DeviceId, known_name: Option<&str>) {
+ let removed = active_roots().as_mut().and_then(|m| m.remove(&device));
+ let mut folder_to_clean: Option<PathBuf> = None;
+ if let Some(root) = removed {
+ root.stop_watcher.store(true, Ordering::Relaxed);
+ // SAFETY: connection_key was obtained from CfConnectSyncRoot.
+ let _ = unsafe { CfDisconnectSyncRoot(root.connection_key) };
+ folder_to_clean = Some(root.root_path.clone());
+ } else if let Some(name) = known_name
+ && let Some(base) = base_sync_dir()
+ {
+ folder_to_clean = Some(base.join(sanitize_folder_name(name)));
+ }
+
+ let id = sync_root_id(device);
+ let _ = StorageProviderSyncRootManager::Unregister(&id);
+
+ if let Some(path) = folder_to_clean {
+ cleanup_sync_root_dir(&path);
+ }
+ if let Some(base) = base_sync_dir() {
+ let _ = std::fs::remove_dir(&base);
+ }
+ }
+
+ fn cleanup_sync_root_dir(path: &Path) {
+ if !path.exists() {
+ return;
+ }
+ let _ = std::fs::remove_dir_all(path);
+ }
+
+ fn register_and_connect(device: DeviceId, name: &str, online: bool) -> Result<(), String> {
+ let base = base_sync_dir().ok_or_else(|| "no user home directory".to_owned())?;
+ let folder_name = sanitize_folder_name(name);
+ let root_path = base.join(&folder_name);
+ std::fs::create_dir_all(&root_path).map_err(|e| format!("create sync root dir: {e}"))?;
+
+ let exe = std::env::current_exe().map_err(|e| e.to_string())?;
+ let icon_resource = format!("{},0", exe.display());
+
+ let info = StorageProviderSyncRootInfo::new().map_err(|e| format!("SyncRootInfo::new: {e}"))?;
+ info.SetId(&sync_root_id(device)).map_err(|e| format!("SetId: {e}"))?;
+
+ let folder = StorageFolder::GetFolderFromPathAsync(&HSTRING::from(root_path.as_os_str()))
+ .map_err(|e| format!("GetFolderFromPathAsync: {e}"))?
+ .join()
+ .map_err(|e| format!("GetFolderFromPathAsync.join: {e}"))?;
+ info.SetPath(&folder).map_err(|e| format!("SetPath: {e}"))?;
+ info.SetDisplayNameResource(&HSTRING::from(name))
+ .map_err(|e| format!("SetDisplayNameResource: {e}"))?;
+ info.SetIconResource(&HSTRING::from(&icon_resource)).map_err(|e| format!("SetIconResource: {e}"))?;
+ info.SetVersion(&HSTRING::from(env!("CARGO_PKG_VERSION"))).map_err(|e| format!("SetVersion: {e}"))?;
+ info.SetHydrationPolicy(StorageProviderHydrationPolicy::Full)
+ .map_err(|e| format!("SetHydrationPolicy: {e}"))?;
+ info.SetHydrationPolicyModifier(StorageProviderHydrationPolicyModifier::None)
+ .map_err(|e| format!("SetHydrationPolicyModifier: {e}"))?;
+ info.SetPopulationPolicy(StorageProviderPopulationPolicy::Full)
+ .map_err(|e| format!("SetPopulationPolicy: {e}"))?;
+ info.SetInSyncPolicy(
+ StorageProviderInSyncPolicy::FileCreationTime
+ | StorageProviderInSyncPolicy::DirectoryCreationTime,
+ )
+ .map_err(|e| format!("SetInSyncPolicy: {e}"))?;
+ info.SetHardlinkPolicy(StorageProviderHardlinkPolicy::None)
+ .map_err(|e| format!("SetHardlinkPolicy: {e}"))?;
+ info.SetShowSiblingsAsGroup(false).map_err(|e| format!("SetShowSiblingsAsGroup: {e}"))?;
+
+ let context_buf = CryptographicBuffer::ConvertStringToBinary(
+ &HSTRING::from(device.to_string()),
+ BinaryStringEncoding::Utf8,
+ )
+ .map_err(|e| format!("ConvertStringToBinary: {e}"))?;
+ info.SetContext(&context_buf).map_err(|e| format!("SetContext: {e}"))?;
+
+ StorageProviderSyncRootManager::Register(&info).map_err(|e| format!("Register: {e}"))?;
+
+ let callbacks = [
+ CF_CALLBACK_REGISTRATION {
+ Type: CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS,
+ Callback: Some(on_fetch_placeholders),
+ },
+ CF_CALLBACK_REGISTRATION { Type: CF_CALLBACK_TYPE_FETCH_DATA, Callback: Some(on_fetch_data) },
+ CF_CALLBACK_REGISTRATION {
+ Type: CF_CALLBACK_TYPE_NOTIFY_RENAME,
+ Callback: Some(on_notify_rename),
+ },
+ CF_CALLBACK_REGISTRATION {
+ Type: CF_CALLBACK_TYPE_NOTIFY_DELETE,
+ Callback: Some(on_notify_delete),
+ },
+ CF_CALLBACK_REGISTRATION { Type: CF_CALLBACK_TYPE_NONE, Callback: None },
+ ];
+
+ // Keep the DeviceId in a heap allocation for the process lifetime so async
+ // cfapi callbacks always read a valid DeviceId and look up `ACTIVE_ROOTS`.
+ let ctx_ptr: *const c_void = Box::into_raw(Box::new(device)).cast();
+ let wide_root = to_wide_null(&root_path);
+ // SAFETY: wide_root is null-terminated, callbacks ends with CF_CALLBACK_TYPE_NONE.
+ let connection_key = unsafe {
+ CfConnectSyncRoot(
+ PCWSTR(wide_root.as_ptr()),
+ callbacks.as_ptr(),
+ Some(ctx_ptr),
+ CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO | CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH,
+ )
+ }
+ .map_err(|e| format!("CfConnectSyncRoot: {e}"))?;
+
+ let stop_watcher = Arc::new(AtomicBool::new(false));
+ let active = Arc::new(ActiveRoot {
+ device,
+ name: name.to_owned(),
+ root_path: root_path.clone(),
+ connection_key,
+ stop_watcher: stop_watcher.clone(),
+ populated_dirs: Mutex::new(HashSet::new()),
+ uploading: Mutex::new(HashSet::new()),
+ });
+
+ if let Some(map) = active_roots().as_mut() {
+ map.insert(device, active.clone());
+ }
+
+ start_directory_watcher(active.clone(), stop_watcher);
+
+ if online {
+ core_host::spawn(async move {
+ // Pre-populate top-level placeholders when connected so opening the
+ // sync root in Explorer or inspecting it immediately shows folders.
+ refresh_folder(active, "").await;
+ });
+ }
+
+ tracing::info!(device = %device.short(), "registered phone storage sync root");
+ Ok(())
+ }
+
+ fn to_wide_null(s: impl AsRef<OsStr>) -> Vec<u16> {
+ s.as_ref().encode_wide().chain(std::iter::once(0)).collect()
+ }
+
+ unsafe fn pcwstr_to_string(ptr: PCWSTR) -> String {
+ if ptr.is_null() {
+ return String::new();
+ }
+ // SAFETY: caller guarantees `ptr` is a valid null-terminated wide string.
+ let slice = unsafe { ptr.as_wide() };
+ std::ffi::OsString::from_wide(slice).to_string_lossy().into_owned()
+ }
+
+ unsafe fn callback_root_and_path(
+ info: *const CF_CALLBACK_INFO,
+ ) -> Option<(Arc<ActiveRoot>, String, CF_CONNECTION_KEY, i64, i64)> {
+ // SAFETY: Windows passes a valid CF_CALLBACK_INFO pointer to registered callbacks.
+ let cb = unsafe { info.as_ref()? };
+ if cb.CallbackContext.is_null() {
+ return None;
+ }
+ // SAFETY: CallbackContext points to a leaked `Box<DeviceId>`.
+ let device = unsafe { *(cb.CallbackContext as *const DeviceId) };
+ let root = get_active_root(&device)?;
+ // SAFETY: VolumeDosName and NormalizedPath are valid PCWSTRs in CF_CALLBACK_INFO.
+ let vol = unsafe { pcwstr_to_string(cb.VolumeDosName) };
+ // SAFETY: NormalizedPath is valid when CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH is set.
+ let norm = unsafe { pcwstr_to_string(cb.NormalizedPath) };
+ let full = PathBuf::from(format!("{vol}{norm}"));
+ let rel = relative_path_under_root(&root.root_path, &full)?;
+ Some((root, rel, cb.ConnectionKey, cb.TransferKey, cb.RequestKey))
+ }
+
+ fn make_op_info(
+ connection_key: CF_CONNECTION_KEY,
+ transfer_key: i64,
+ request_key: i64,
+ op_type: CF_OPERATION_TYPE,
+ ) -> CF_OPERATION_INFO {
+ CF_OPERATION_INFO {
+ StructSize: std::mem::size_of::<CF_OPERATION_INFO>() as u32,
+ Type: op_type,
+ ConnectionKey: connection_key,
+ TransferKey: transfer_key,
+ CorrelationVector: std::ptr::null(),
+ SyncStatus: std::ptr::null(),
+ RequestKey: request_key,
+ }
+ }
+
+ unsafe extern "system" fn on_fetch_placeholders(
+ callback_info: *const CF_CALLBACK_INFO,
+ _callback_parameters: *const CF_CALLBACK_PARAMETERS,
+ ) {
+ // SAFETY: callback_info is provided by Windows cfapi.
+ let Some((root, rel_path, connection_key, transfer_key, request_key)) =
+ (unsafe { callback_root_and_path(callback_info) })
+ else {
+ return;
+ };
+
+ core_host::spawn(async move {
+ let Some(node) = core_host::node() else {
+ ack_placeholders_failed(connection_key, transfer_key, request_key);
+ return;
+ };
+ match node.storage_list(root.device, &rel_path).await {
+ Ok(entries) => {
+ lock(&root.populated_dirs).insert(rel_path.clone());
+ transfer_placeholders(connection_key, transfer_key, request_key, &rel_path, &entries);
+ }
+ Err(e) => {
+ tracing::debug!(
+ device = %root.device.short(),
+ error = %e,
+ "fetch placeholders failed"
+ );
+ ack_placeholders_failed(connection_key, transfer_key, request_key);
+ }
+ }
+ });
+ }
+
+ fn transfer_placeholders(
+ connection_key: CF_CONNECTION_KEY,
+ transfer_key: i64,
+ request_key: i64,
+ parent_rel: &str,
+ entries: &[StorageEntry],
+ ) {
+ let mut wide_names: Vec<Vec<u16>> = Vec::with_capacity(entries.len());
+ let mut identities: Vec<Vec<u8>> = Vec::with_capacity(entries.len());
+ for entry in entries {
+ wide_names.push(to_wide_null(&entry.name));
+ let rel = if parent_rel.is_empty() {
+ entry.name.clone()
+ } else {
+ format!("{parent_rel}/{}", entry.name)
+ };
+ identities.push(rel.into_bytes());
+ }
+
+ let mut create_infos: Vec<CF_PLACEHOLDER_CREATE_INFO> = entries
+ .iter()
+ .enumerate()
+ .map(|(i, entry)| build_placeholder_create_info(entry, &wide_names[i], &identities[i]))
+ .collect();
+
+ let op_info =
+ make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS);
+ let mut op_params = CF_OPERATION_PARAMETERS {
+ ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
+ Anonymous: CF_OPERATION_PARAMETERS_0 {
+ TransferPlaceholders: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_4 {
+ Flags: CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAG_DISABLE_ON_DEMAND_POPULATION,
+ CompletionStatus: STATUS_SUCCESS,
+ PlaceholderTotalCount: create_infos.len() as i64,
+ PlaceholderArray: if create_infos.is_empty() {
+ std::ptr::null_mut()
+ } else {
+ create_infos.as_mut_ptr()
+ },
+ PlaceholderCount: create_infos.len() as u32,
+ EntriesProcessed: 0,
+ },
+ },
+ };
+ // SAFETY: op_info and op_params point to valid stack structures and buffers.
+ let _ = unsafe { CfExecute(&op_info, &mut op_params) };
+ }
+
+ fn ack_placeholders_failed(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64) {
+ let op_info =
+ make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS);
+ let mut op_params = CF_OPERATION_PARAMETERS {
+ ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
+ Anonymous: CF_OPERATION_PARAMETERS_0 {
+ TransferPlaceholders: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_4 {
+ Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAGS(0),
+ CompletionStatus: STATUS_CLOUD_NETWORK_UNAVAILABLE,
+ PlaceholderTotalCount: 0,
+ PlaceholderArray: std::ptr::null_mut(),
+ PlaceholderCount: 0,
+ EntriesProcessed: 0,
+ },
+ },
+ };
+ // SAFETY: op_info and op_params are valid.
+ let _ = unsafe { CfExecute(&op_info, &mut op_params) };
+ }
+
+ fn build_placeholder_create_info(
+ entry: &StorageEntry,
+ wide_name: &[u16],
+ identity: &[u8],
+ ) -> CF_PLACEHOLDER_CREATE_INFO {
+ let ticks = unix_ms_to_filetime_ticks(entry.modified);
+ let attrs = if entry.is_dir { FILE_ATTRIBUTE_DIRECTORY.0 } else { FILE_ATTRIBUTE_NORMAL.0 };
+ let file_size = if entry.is_dir { 0 } else { entry.size as i64 };
+ CF_PLACEHOLDER_CREATE_INFO {
+ RelativeFileName: PCWSTR(wide_name.as_ptr()),
+ FsMetadata: CF_FS_METADATA {
+ BasicInfo: FILE_BASIC_INFO {
+ CreationTime: ticks,
+ LastAccessTime: ticks,
+ LastWriteTime: ticks,
+ ChangeTime: ticks,
+ FileAttributes: attrs,
+ },
+ FileSize: file_size,
+ },
+ FileIdentity: identity.as_ptr().cast(),
+ FileIdentityLength: identity.len() as u32,
+ Flags: CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC,
+ Result: windows::core::HRESULT(0),
+ CreateUsn: 0,
+ }
+ }
+
+ unsafe extern "system" fn on_fetch_data(
+ callback_info: *const CF_CALLBACK_INFO,
+ callback_parameters: *const CF_CALLBACK_PARAMETERS,
+ ) {
+ // SAFETY: callback_info and callback_parameters are provided by Windows cfapi.
+ let Some((root, rel_path, connection_key, transfer_key, request_key)) =
+ (unsafe { callback_root_and_path(callback_info) })
+ else {
+ return;
+ };
+ let Some(params) = (unsafe { callback_parameters.as_ref() }) else {
+ return;
+ };
+ // SAFETY: Union variant matches CF_CALLBACK_TYPE_FETCH_DATA.
+ let fetch = unsafe { params.Anonymous.FetchData };
+ let required_offset = fetch.RequiredFileOffset.max(0) as u64;
+ let required_length = fetch.RequiredLength.max(0) as u64;
+
+ core_host::spawn(async move {
+ let Some(node) = core_host::node() else {
+ ack_data_failed(
+ connection_key,
+ transfer_key,
+ request_key,
+ required_offset as i64,
+ STATUS_CLOUD_NETWORK_UNAVAILABLE,
+ );
+ return;
+ };
+
+ let (meta, mut recv) = match node
+ .storage_read_stream(root.device, &rel_path, required_offset, Some(required_length))
+ .await
+ {
+ Ok(res) => res,
+ Err(e) => {
+ tracing::debug!(
+ device = %root.device.short(),
+ error = %e,
+ "fetch data stream failed"
+ );
+ ack_data_failed(
+ connection_key,
+ transfer_key,
+ request_key,
+ required_offset as i64,
+ STATUS_CLOUD_NETWORK_UNAVAILABLE,
+ );
+ return;
+ }
+ };
+ let total_size = meta.size;
+
+ if required_length == 0 {
+ let _ = transfer_data_chunk(
+ connection_key,
+ transfer_key,
+ request_key,
+ &[],
+ required_offset as i64,
+ STATUS_SUCCESS,
+ );
+ return;
+ }
+
+ let mut current_offset = required_offset;
+ let mut remaining = required_length;
+ let mut buf = Vec::with_capacity(HYDRATE_CHUNK_BYTES);
+
+ while remaining > 0 {
+ let want = (remaining as usize).min(HYDRATE_CHUNK_BYTES);
+ buf.clear();
+ while buf.len() < want {
+ match recv.read_chunk(want - buf.len()).await {
+ Ok(Some(chunk)) => buf.extend_from_slice(&chunk),
+ Ok(None) => break,
+ Err(e) => {
+ tracing::debug!(error = %e, "hydration stream read error");
+ ack_data_failed(
+ connection_key,
+ transfer_key,
+ request_key,
+ current_offset as i64,
+ STATUS_CLOUD_NETWORK_UNAVAILABLE,
+ );
+ return;
+ }
+ }
+ }
+ if buf.is_empty() {
+ break;
+ }
+ let len = buf.len() as u64;
+ let completed = (current_offset + len).min(total_size);
+ // SAFETY: valid connection_key and transfer_key during active fetch.
+ let _ = unsafe {
+ CfReportProviderProgress(
+ connection_key,
+ transfer_key,
+ total_size as i64,
+ completed as i64,
+ )
+ };
+ if !transfer_data_chunk(
+ connection_key,
+ transfer_key,
+ request_key,
+ &buf,
+ current_offset as i64,
+ STATUS_SUCCESS,
+ ) {
+ return;
+ }
+ current_offset += len;
+ remaining = remaining.saturating_sub(len);
+ }
+ });
+ }
+
+ fn transfer_data_chunk(
+ connection_key: CF_CONNECTION_KEY,
+ transfer_key: i64,
+ request_key: i64,
+ data: &[u8],
+ offset: i64,
+ status: NTSTATUS,
+ ) -> bool {
+ let op_info =
+ make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_TRANSFER_DATA);
+ let mut op_params = CF_OPERATION_PARAMETERS {
+ ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
+ Anonymous: CF_OPERATION_PARAMETERS_0 {
+ TransferData: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_0 {
+ Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_TRANSFER_DATA_FLAGS(0),
+ CompletionStatus: status,
+ Buffer: if data.is_empty() { std::ptr::null() } else { data.as_ptr().cast() },
+ Offset: offset,
+ Length: data.len() as i64,
+ },
+ },
+ };
+ // SAFETY: op_info and op_params reference valid memory for the duration of CfExecute.
+ unsafe { CfExecute(&op_info, &mut op_params) }.is_ok()
+ }
+
+ fn ack_data_failed(
+ connection_key: CF_CONNECTION_KEY,
+ transfer_key: i64,
+ request_key: i64,
+ offset: i64,
+ status: NTSTATUS,
+ ) {
+ let _ = transfer_data_chunk(connection_key, transfer_key, request_key, &[], offset, status);
+ }
+
+ unsafe extern "system" fn on_notify_rename(
+ callback_info: *const CF_CALLBACK_INFO,
+ callback_parameters: *const CF_CALLBACK_PARAMETERS,
+ ) {
+ // SAFETY: callback_info and callback_parameters are provided by Windows cfapi.
+ let Some((root, old_rel, connection_key, transfer_key, request_key)) =
+ (unsafe { callback_root_and_path(callback_info) })
+ else {
+ return;
+ };
+ let Some(cb) = (unsafe { callback_info.as_ref() }) else { return };
+ let Some(params) = (unsafe { callback_parameters.as_ref() }) else { return };
+ // SAFETY: Union variant matches CF_CALLBACK_TYPE_NOTIFY_RENAME.
+ let rename = unsafe { params.Anonymous.Rename };
+ // SAFETY: VolumeDosName and TargetPath are valid PCWSTRs.
+ let vol = unsafe { pcwstr_to_string(cb.VolumeDosName) };
+ let target_norm = unsafe { pcwstr_to_string(rename.TargetPath) };
+ let target_full = PathBuf::from(format!("{vol}{target_norm}"));
+ let new_rel = relative_path_under_root(&root.root_path, &target_full);
+
+ core_host::spawn(async move {
+ let Some(node) = core_host::node() else {
+ ack_rename(connection_key, transfer_key, request_key, STATUS_CLOUD_NETWORK_UNAVAILABLE);
+ return;
+ };
+ let status = match new_rel {
+ Some(new_rel) if !old_rel.is_empty() && !new_rel.is_empty() => {
+ match node.storage_rename(root.device, &old_rel, &new_rel).await {
+ Ok(()) => STATUS_SUCCESS,
+ Err(nectarlink_core::Error::Offline) => STATUS_CLOUD_NETWORK_UNAVAILABLE,
+ Err(nectarlink_core::Error::Denied) => STATUS_ACCESS_DENIED,
+ Err(_) => STATUS_CLOUD_UNSUCCESSFUL,
+ }
+ }
+ _ => STATUS_ACCESS_DENIED,
+ };
+ ack_rename(connection_key, transfer_key, request_key, status);
+ });
+ }
+
+ fn ack_rename(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64, status: NTSTATUS) {
+ let op_info = make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_ACK_RENAME);
+ let mut op_params = CF_OPERATION_PARAMETERS {
+ ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
+ Anonymous: CF_OPERATION_PARAMETERS_0 {
+ AckRename: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_6 {
+ Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_ACK_RENAME_FLAGS(0),
+ CompletionStatus: status,
+ },
+ },
+ };
+ // SAFETY: op_info and op_params are valid.
+ let _ = unsafe { CfExecute(&op_info, &mut op_params) };
+ }
+
+ unsafe extern "system" fn on_notify_delete(
+ callback_info: *const CF_CALLBACK_INFO,
+ _callback_parameters: *const CF_CALLBACK_PARAMETERS,
+ ) {
+ // SAFETY: callback_info is provided by Windows cfapi.
+ let Some((root, rel_path, connection_key, transfer_key, request_key)) =
+ (unsafe { callback_root_and_path(callback_info) })
+ else {
+ return;
+ };
+
+ core_host::spawn(async move {
+ if rel_path.is_empty() {
+ ack_delete(connection_key, transfer_key, request_key, STATUS_ACCESS_DENIED);
+ return;
+ }
+ let Some(node) = core_host::node() else {
+ ack_delete(connection_key, transfer_key, request_key, STATUS_CLOUD_NETWORK_UNAVAILABLE);
+ return;
+ };
+ let status = match node.storage_delete(root.device, &rel_path, true).await {
+ Ok(()) => STATUS_SUCCESS,
+ Err(nectarlink_core::Error::Offline) => STATUS_CLOUD_NETWORK_UNAVAILABLE,
+ Err(nectarlink_core::Error::Denied) => STATUS_ACCESS_DENIED,
+ Err(nectarlink_core::Error::Storage(msg)) if msg.contains("not_found") => STATUS_SUCCESS,
+ Err(_) => STATUS_CLOUD_UNSUCCESSFUL,
+ };
+ ack_delete(connection_key, transfer_key, request_key, status);
+ });
+ }
+
+ fn ack_delete(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64, status: NTSTATUS) {
+ let op_info = make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_ACK_DELETE);
+ let mut op_params = CF_OPERATION_PARAMETERS {
+ ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
+ Anonymous: CF_OPERATION_PARAMETERS_0 {
+ AckDelete: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_7 {
+ Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_ACK_DELETE_FLAGS(0),
+ CompletionStatus: status,
+ },
+ },
+ };
+ // SAFETY: op_info and op_params are valid.
+ let _ = unsafe { CfExecute(&op_info, &mut op_params) };
+ }
+
+ /// Refreshes a folder's placeholders from the phone (`storage.changed` or initial connect).
+ async fn refresh_folder(root: Arc<ActiveRoot>, rel_folder: &str) {
+ let Some(node) = core_host::node() else { return };
+ let Ok(entries) = node.storage_list(root.device, rel_folder).await else { return };
+
+ let local_dir = if rel_folder.is_empty() {
+ root.root_path.clone()
+ } else {
+ root.root_path.join(rel_folder.replace('/', "\\"))
+ };
+ if !local_dir.exists() {
+ return;
+ }
+
+ lock(&root.populated_dirs).insert(rel_folder.to_owned());
+
+ let mut missing: Vec<StorageEntry> = Vec::new();
+ for entry in entries {
+ let child_path = local_dir.join(&entry.name);
+ if !child_path.exists() {
+ missing.push(entry);
+ }
+ }
+ if missing.is_empty() {
+ return;
+ }
+
+ let mut wide_names: Vec<Vec<u16>> = Vec::with_capacity(missing.len());
+ let mut identities: Vec<Vec<u8>> = Vec::with_capacity(missing.len());
+ for entry in &missing {
+ wide_names.push(to_wide_null(&entry.name));
+ let rel = if rel_folder.is_empty() {
+ entry.name.clone()
+ } else {
+ format!("{rel_folder}/{}", entry.name)
+ };
+ identities.push(rel.into_bytes());
+ }
+ let mut create_infos: Vec<CF_PLACEHOLDER_CREATE_INFO> = missing
+ .iter()
+ .enumerate()
+ .map(|(i, entry)| build_placeholder_create_info(entry, &wide_names[i], &identities[i]))
+ .collect();
+
+ let wide_dir = to_wide_null(&local_dir);
+ let mut processed = 0u32;
+ // SAFETY: wide_dir is null-terminated and create_infos stays alive for the call.
+ let _ = unsafe {
+ CfCreatePlaceholders(
+ PCWSTR(wide_dir.as_ptr()),
+ &mut create_infos,
+ windows::Win32::Storage::CloudFilters::CF_CREATE_FLAG_NONE,
+ Some(&mut processed),
+ )
+ };
+ }
+
+ /// Watches the sync root directory for files or folders dropped in by the user
+ /// and uploads them to the phone via `storage.write` / `storage.mkdir`.
+ fn start_directory_watcher(root: Arc<ActiveRoot>, stop: Arc<AtomicBool>) {
+ let _ = std::thread::Builder::new().name("storage-watch".into()).spawn(move || {
+ let wide_dir = to_wide_null(&root.root_path);
+ // SAFETY: wide_dir is a valid null-terminated path.
+ let dir_handle = unsafe {
+ CreateFileW(
+ PCWSTR(wide_dir.as_ptr()),
+ FILE_LIST_DIRECTORY.0,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ None,
+ OPEN_EXISTING,
+ FILE_FLAG_BACKUP_SEMANTICS,
+ None,
+ )
+ };
+ let Ok(dir_handle) = dir_handle else { return };
+ if dir_handle == INVALID_HANDLE_VALUE {
+ return;
+ }
+
+ let mut buf = vec![0u64; 4096];
+ while !stop.load(Ordering::Relaxed) {
+ let mut bytes_returned = 0u32;
+ // SAFETY: buf is 8-byte aligned and valid for ReadDirectoryChangesW.
+ let ok = unsafe {
+ ReadDirectoryChangesW(
+ dir_handle,
+ buf.as_mut_ptr().cast(),
+ (buf.len() * std::mem::size_of::<u64>()) as u32,
+ true,
+ FILE_NOTIFY_CHANGE_FILE_NAME
+ | FILE_NOTIFY_CHANGE_DIR_NAME
+ | FILE_NOTIFY_CHANGE_LAST_WRITE
+ | FILE_NOTIFY_CHANGE_SIZE,
+ Some(&mut bytes_returned),
+ None,
+ None,
+ )
+ }
+ .is_ok();
+
+ if !ok || stop.load(Ordering::Relaxed) || bytes_returned == 0 {
+ break;
+ }
+
+ let changed_paths = parse_notify_buffer(
+ // SAFETY: bytes_returned bytes were initialized by ReadDirectoryChangesW.
+ unsafe { std::slice::from_raw_parts(buf.as_ptr().cast::<u8>(), bytes_returned as usize) },
+ );
+
+ for rel_win in changed_paths {
+ let rel_slash = rel_win.replace('\\', "/");
+ if rel_slash.is_empty()
+ || rel_slash.split('/').any(|seg| seg.starts_with('.') || seg.starts_with("~$"))
+ {
+ continue;
+ }
+ let full_path = root.root_path.join(&rel_win);
+ if !needs_upload(&full_path) {
+ continue;
+ }
+ {
+ let mut uploading = lock(&root.uploading);
+ if !uploading.insert(rel_slash.clone()) {
+ continue;
+ }
+ }
+ let root_clone = root.clone();
+ core_host::spawn(async move {
+ // Brief debounce so Explorer finishes writing the dropped file.
+ tokio::time::sleep(Duration::from_millis(350)).await;
+ upload_local_item(&root_clone, &rel_slash, &full_path).await;
+ lock(&root_clone.uploading).remove(&rel_slash);
+ });
+ }
+ }
+
+ // SAFETY: dir_handle was opened with CreateFileW above.
+ let _ = unsafe { CloseHandle(dir_handle) };
+ });
+ }
+
+ fn parse_notify_buffer(bytes: &[u8]) -> Vec<String> {
+ let mut out = Vec::new();
+ let mut offset = 0usize;
+ while offset + std::mem::size_of::<FILE_NOTIFY_INFORMATION>() <= bytes.len() {
+ // SAFETY: offset is within bounds and 4-byte aligned in ReadDirectoryChangesW buffer.
+ let info = unsafe { &*(bytes.as_ptr().add(offset) as *const FILE_NOTIFY_INFORMATION) };
+ let name_bytes = info.FileNameLength as usize;
+ let name_offset = offset + std::mem::offset_of!(FILE_NOTIFY_INFORMATION, FileName);
+ if name_offset + name_bytes <= bytes.len()
+ && matches!(
+ info.Action,
+ FILE_ACTION_ADDED | FILE_ACTION_MODIFIED | FILE_ACTION_RENAMED_NEW_NAME
+ )
+ {
+ // SAFETY: FileName is a UTF-16 slice of `name_bytes / 2` u16 elements.
+ let wide = unsafe {
+ std::slice::from_raw_parts(bytes.as_ptr().add(name_offset) as *const u16, name_bytes / 2)
+ };
+ let rel = std::ffi::OsString::from_wide(wide).to_string_lossy().into_owned();
+ if !out.contains(&rel) {
+ out.push(rel);
+ }
+ }
+ if info.NextEntryOffset == 0 {
+ break;
+ }
+ offset = offset.saturating_add(info.NextEntryOffset as usize);
+ }
+ out
+ }
+
+ /// Checks whether `full_path` is a newly dropped non-placeholder file/folder or
+ /// a modified placeholder that is no longer in sync.
+ fn needs_upload(full_path: &Path) -> bool {
+ let wide = to_wide_null(full_path);
+ let mut find_data = WIN32_FIND_DATAW::default();
+ // SAFETY: wide is null-terminated and find_data is a valid struct.
+ let handle = unsafe { FindFirstFileW(PCWSTR(wide.as_ptr()), &mut find_data) };
+ let Ok(handle) = handle else { return false };
+ if handle == INVALID_HANDLE_VALUE {
+ return false;
+ }
+ // SAFETY: handle came from FindFirstFileW.
+ let _ = unsafe { FindClose(handle) };
+ // SAFETY: dwFileAttributes and dwReserved0 (reparse tag) were populated by FindFirstFileW.
+ let state = unsafe {
+ CfGetPlaceholderStateFromAttributeTag(find_data.dwFileAttributes, find_data.dwReserved0)
+ };
+ if (state.0 & CF_PLACEHOLDER_STATE_PLACEHOLDER.0) == 0 {
+ return true;
+ }
+ (state.0 & CF_PLACEHOLDER_STATE_IN_SYNC.0) == 0
+ }
+
+ async fn upload_local_item(root: &Arc<ActiveRoot>, rel_slash: &str, full_path: &Path) {
+ if !full_path.exists() || !needs_upload(full_path) {
+ return;
+ }
+ let Some(node) = core_host::node() else { return };
+ if full_path.is_dir() {
+ if node.storage_mkdir(root.device, rel_slash).await.is_ok() {
+ mark_path_in_sync(full_path, rel_slash, true);
+ }
+ } else if full_path.is_file() && node.storage_write(root.device, rel_slash, full_path).await.is_ok() {
+ mark_path_in_sync(full_path, rel_slash, false);
+ }
+ }
+
+ fn mark_path_in_sync(full_path: &Path, rel_slash: &str, is_dir: bool) {
+ let wide = to_wide_null(full_path);
+ let flags = if is_dir { FILE_FLAG_BACKUP_SEMANTICS } else { Default::default() };
+ // SAFETY: wide is a valid null-terminated path.
+ let handle = unsafe {
+ CreateFileW(
+ PCWSTR(wide.as_ptr()),
+ GENERIC_READ.0 | GENERIC_WRITE.0,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ None,
+ OPEN_EXISTING,
+ flags,
+ None,
+ )
+ };
+ let Ok(handle): Result<HANDLE, _> = handle else { return };
+ if handle == INVALID_HANDLE_VALUE {
+ return;
+ }
+ let identity = rel_slash.as_bytes();
+ // SAFETY: handle is open with read/write access; identity is valid for the call.
+ let converted = unsafe {
+ CfConvertToPlaceholder(
+ handle,
+ Some(identity.as_ptr().cast()),
+ identity.len() as u32,
+ CF_CONVERT_FLAG_MARK_IN_SYNC,
+ None,
+ None,
+ )
+ };
+ if converted.is_err() {
+ // Already a placeholder: mark it in sync.
+ // SAFETY: handle is valid.
+ let _ =
+ unsafe { CfSetInSyncState(handle, CF_IN_SYNC_STATE_IN_SYNC, CF_SET_IN_SYNC_FLAG_NONE, None) };
+ }
+ // SAFETY: handle was opened with CreateFileW above.
+ let _ = unsafe { CloseHandle(handle) };
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ #[test]
+ fn converts_unix_ms_to_filetime_and_back() {
+ let unix_ms = 1_710_000_000_123i64;
+ let ticks = unix_ms_to_filetime_ticks(unix_ms);
+ assert_eq!(ticks, EPOCH_DIFF_100NS + unix_ms * 10_000);
+ assert_eq!(filetime_ticks_to_unix_ms(ticks), unix_ms);
+ assert_eq!(unix_ms_to_filetime_ticks(-500), EPOCH_DIFF_100NS);
+ }
+
+ #[test]
+ fn builds_placeholder_metadata_for_files_and_folders() {
+ let file_entry = StorageEntry {
+ name: "photo.jpg".into(),
+ size: 42_000,
+ modified: 1_700_000_000_000,
+ is_dir: false,
+ };
+ let wide_file = to_wide_null(&file_entry.name);
+ let id_file = b"DCIM/photo.jpg";
+ let info_file = build_placeholder_create_info(&file_entry, &wide_file, id_file);
+ assert_eq!(info_file.FsMetadata.FileSize, 42_000);
+ assert_eq!(info_file.FsMetadata.BasicInfo.FileAttributes, FILE_ATTRIBUTE_NORMAL.0);
+ assert_eq!(
+ info_file.FsMetadata.BasicInfo.LastWriteTime,
+ unix_ms_to_filetime_ticks(1_700_000_000_000)
+ );
+ assert_eq!(info_file.FileIdentityLength, id_file.len() as u32);
+
+ let dir_entry =
+ StorageEntry { name: "Camera".into(), size: 999, modified: 1_700_000_000_000, is_dir: true };
+ let wide_dir = to_wide_null(&dir_entry.name);
+ let id_dir = b"DCIM/Camera";
+ let info_dir = build_placeholder_create_info(&dir_entry, &wide_dir, id_dir);
+ assert_eq!(info_dir.FsMetadata.FileSize, 0);
+ assert_eq!(info_dir.FsMetadata.BasicInfo.FileAttributes, FILE_ATTRIBUTE_DIRECTORY.0);
+ }
+
+ #[test]
+ fn normalizes_relative_paths_and_sanitizes_folder_names() {
+ let root = Path::new(r"C:\Users\alice\Nectarlink\Pixel 9");
+ assert_eq!(relative_path_under_root(root, root), Some(String::new()));
+ assert_eq!(
+ relative_path_under_root(
+ root,
+ Path::new(r"C:\Users\alice\Nectarlink\Pixel 9\DCIM\Camera\a.jpg")
+ ),
+ Some("DCIM/Camera/a.jpg".into())
+ );
+ assert_eq!(
+ relative_path_under_root(root, Path::new(r"C:\Users\alice\Nectarlink\Pixel 9 Pro\DCIM")),
+ None
+ );
+
+ assert_eq!(sanitize_folder_name("Pixel 9 (test)"), "Pixel 9 (test)");
+ assert_eq!(sanitize_folder_name("My:Phone*1?"), "My Phone 1");
+ assert_eq!(sanitize_folder_name("CON"), "CON phone");
+ }
+ }
+}
+
+#[cfg(not(windows))]
+mod imp {
+ #![allow(dead_code)]
+
+ use nectarlink_core::{DeviceId, NodeEvent};
+ use std::path::{Path, PathBuf};
+
+ pub fn start() {}
+
+ pub fn sync() {}
+
+ pub fn on_event(_event: &NodeEvent) {}
+
+ pub fn shutdown() {}
+
+ pub fn unregister_all() {}
+
+ pub fn sync_root_path_for(_device: DeviceId) -> Option<PathBuf> {
+ None
+ }
+
+ pub fn open_in_explorer(_device: DeviceId) -> Result<(), String> {
+ Err("phone storage exploration is not supported on this platform".into())
+ }
+
+ pub fn sanitize_folder_name(device_name: &str) -> String {
+ device_name.chars().filter(|c| c.is_alphanumeric() || matches!(c, ' ' | '-' | '_')).collect()
+ }
+
+ pub fn unix_ms_to_filetime_ticks(unix_ms: i64) -> i64 {
+ unix_ms * 10_000 + 116_444_736_000_000_000
+ }
+
+ pub fn filetime_ticks_to_unix_ms(ticks: i64) -> i64 {
+ (ticks - 116_444_736_000_000_000) / 10_000
+ }
+
+ pub fn relative_path_under_root(root_path: &Path, full_path: &Path) -> Option<String> {
+ full_path.strip_prefix(root_path).ok().and_then(|p| p.to_str()).map(|s| s.replace('\\', "/"))
+ }
+}
+
+pub use imp::*;
diff --git a/desktop/app/src/storage/mod.rs b/desktop/app/src/storage/mod.rs
deleted file mode 100644
index 8ec9219..0000000
--- a/desktop/app/src/storage/mod.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Phone storage in File Explorer (Windows CloudFiles) and stubs for other platforms.
-
-#[cfg(windows)]
-mod windows;
-#[cfg(windows)]
-pub use self::windows::*;
-
-#[cfg(not(windows))]
-mod non_windows;
-#[cfg(not(windows))]
-pub use self::non_windows::*;
diff --git a/desktop/app/src/storage/non_windows.rs b/desktop/app/src/storage/non_windows.rs
deleted file mode 100644
index d3900f4..0000000
--- a/desktop/app/src/storage/non_windows.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! macOS / non-Windows storage stubs.
-
-use nectarlink_core::{DeviceId, NodeEvent};
-use std::path::{Path, PathBuf};
-
-pub fn start() {}
-
-pub fn sync() {}
-
-pub fn on_event(_event: &NodeEvent) {}
-
-pub fn shutdown() {}
-
-pub fn unregister_all() {}
-
-pub fn sync_root_path_for(_device: DeviceId) -> Option<PathBuf> {
- None
-}
-
-pub fn open_in_explorer(_device: DeviceId) -> Result<(), String> {
- Err("phone storage exploration is not supported on this platform".into())
-}
-
-pub fn sanitize_folder_name(device_name: &str) -> String {
- device_name.chars().filter(|c| c.is_alphanumeric() || matches!(c, ' ' | '-' | '_')).collect()
-}
-
-pub fn unix_ms_to_filetime_ticks(unix_ms: i64) -> i64 {
- unix_ms * 10_000 + 116_444_736_000_000_000
-}
-
-pub fn filetime_ticks_to_unix_ms(ticks: i64) -> i64 {
- (ticks - 116_444_736_000_000_000) / 10_000
-}
-
-pub fn relative_path_under_root(root_path: &Path, full_path: &Path) -> Option<String> {
- full_path.strip_prefix(root_path).ok().and_then(|p| p.to_str()).map(|s| s.replace('\\', "/"))
-}
diff --git a/desktop/app/src/storage/windows.rs b/desktop/app/src/storage/windows.rs
deleted file mode 100644
index 229fb53..0000000
--- a/desktop/app/src/storage/windows.rs
+++ /dev/null
@@ -1,1231 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-//! Phone storage in File Explorer (`docs/protocol/storage.md`, `PLAN.md` §3.6).
-//!
-//! Registers a Windows Cloud Files (`cfapi`) sync root per paired phone under
-//! `%USERPROFILE%\Nectarlink\<Phone name>` when the phone's `storage` toggle is
-//! enabled. Placeholders are populated on demand when folders are opened
-//! (`CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS`) and hydrated on open via ranged
-//! reads (`CF_CALLBACK_TYPE_FETCH_DATA`). Renames, deletes, and new or modified
-//! files dropped into the sync root are forwarded to the phone.
-
-#![allow(unsafe_code)]
-
-use std::{
- collections::{HashMap, HashSet},
- ffi::{OsStr, c_void},
- os::windows::ffi::{OsStrExt, OsStringExt},
- path::{Path, PathBuf},
- sync::{
- Arc, Mutex, MutexGuard,
- atomic::{AtomicBool, Ordering},
- },
- time::Duration,
-};
-
-use nectarlink_core::{DeviceId, LinkState, NodeEvent, StorageEntry};
-use windows::{
- Security::Cryptography::{BinaryStringEncoding, CryptographicBuffer},
- Storage::{
- Provider::{
- StorageProviderHardlinkPolicy, StorageProviderHydrationPolicy,
- StorageProviderHydrationPolicyModifier, StorageProviderInSyncPolicy,
- StorageProviderPopulationPolicy, StorageProviderSyncRootInfo, StorageProviderSyncRootManager,
- },
- StorageFolder,
- },
- Win32::{
- Foundation::{
- CloseHandle, GENERIC_READ, GENERIC_WRITE, HANDLE, INVALID_HANDLE_VALUE, NTSTATUS,
- STATUS_ACCESS_DENIED, STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE, STATUS_CLOUD_FILE_UNSUCCESSFUL,
- STATUS_SUCCESS,
- },
- Storage::{
- CloudFilters::{
- CF_CALLBACK_INFO, CF_CALLBACK_PARAMETERS, CF_CALLBACK_REGISTRATION,
- CF_CALLBACK_TYPE_FETCH_DATA, CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS, CF_CALLBACK_TYPE_NONE,
- CF_CALLBACK_TYPE_NOTIFY_DELETE, CF_CALLBACK_TYPE_NOTIFY_RENAME,
- CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH, CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO,
- CF_CONNECTION_KEY, CF_CONVERT_FLAG_MARK_IN_SYNC, CF_FS_METADATA, CF_IN_SYNC_STATE_IN_SYNC,
- CF_OPERATION_INFO, CF_OPERATION_PARAMETERS, CF_OPERATION_PARAMETERS_0,
- CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAG_DISABLE_ON_DEMAND_POPULATION, CF_OPERATION_TYPE,
- CF_OPERATION_TYPE_ACK_DELETE, CF_OPERATION_TYPE_ACK_RENAME, CF_OPERATION_TYPE_TRANSFER_DATA,
- CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS, CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC,
- CF_PLACEHOLDER_CREATE_INFO, CF_PLACEHOLDER_STATE_IN_SYNC, CF_PLACEHOLDER_STATE_PLACEHOLDER,
- CF_SET_IN_SYNC_FLAG_NONE, CfConnectSyncRoot, CfConvertToPlaceholder, CfCreatePlaceholders,
- CfDisconnectSyncRoot, CfExecute, CfGetPlaceholderStateFromAttributeTag,
- CfReportProviderProgress, CfSetInSyncState,
- },
- FileSystem::{
- CreateFileW, FILE_ACTION_ADDED, FILE_ACTION_MODIFIED, FILE_ACTION_RENAMED_NEW_NAME,
- FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_NORMAL, FILE_BASIC_INFO, FILE_FLAG_BACKUP_SEMANTICS,
- FILE_LIST_DIRECTORY, FILE_NOTIFY_CHANGE_DIR_NAME, FILE_NOTIFY_CHANGE_FILE_NAME,
- FILE_NOTIFY_CHANGE_LAST_WRITE, FILE_NOTIFY_CHANGE_SIZE, FILE_NOTIFY_INFORMATION,
- FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, FindClose, FindFirstFileW,
- OPEN_EXISTING, ReadDirectoryChangesW, WIN32_FIND_DATAW,
- },
- },
- },
- core::{HSTRING, PCWSTR},
-};
-
-use crate::{
- core_host,
- state::{Changes, CoreStatus},
- win,
-};
-
-/// Provider prefix for sync root IDs (`NectarLink!<device_id>`).
-const PROVIDER_PREFIX: &str = "Nectarlink!";
-
-/// Difference between Windows FILETIME epoch (1601-01-01) and Unix epoch
-/// (1970-01-01) in 100-nanosecond intervals.
-const EPOCH_DIFF_100NS: i64 = 116_444_736_000_000_000;
-
-/// Cloud file network unavailable NTSTATUS (`STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE`).
-const STATUS_CLOUD_NETWORK_UNAVAILABLE: NTSTATUS = STATUS_CLOUD_FILE_NETWORK_UNAVAILABLE;
-
-/// Cloud file unsuccessful / cancelled operation NTSTATUS (`STATUS_CLOUD_FILE_UNSUCCESSFUL`).
-const STATUS_CLOUD_UNSUCCESSFUL: NTSTATUS = STATUS_CLOUD_FILE_UNSUCCESSFUL;
-
-/// Chunk size for hydrating files via `CF_OPERATION_TYPE_TRANSFER_DATA` (must be
-/// a multiple of 4096 bytes for non-EOF chunks).
-const HYDRATE_CHUNK_BYTES: usize = 256 * 1024;
-
-fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
- m.lock().unwrap_or_else(|e| e.into_inner())
-}
-
-struct ActiveRoot {
- device: DeviceId,
- name: String,
- root_path: PathBuf,
- connection_key: CF_CONNECTION_KEY,
- stop_watcher: Arc<AtomicBool>,
- populated_dirs: Mutex<HashSet<String>>,
- uploading: Mutex<HashSet<String>>,
-}
-
-static ACTIVE_ROOTS: Mutex<Option<HashMap<DeviceId, Arc<ActiveRoot>>>> = Mutex::new(None);
-static RECONCILING: Mutex<()> = Mutex::new(());
-
-fn active_roots() -> MutexGuard<'static, Option<HashMap<DeviceId, Arc<ActiveRoot>>>> {
- lock(&ACTIVE_ROOTS)
-}
-
-fn get_active_root(device: &DeviceId) -> Option<Arc<ActiveRoot>> {
- active_roots().as_ref().and_then(|m| m.get(device).cloned())
-}
-
-/// Starts watching paired devices and their `storage` toggles to register or
-/// unregister File Explorer sync roots.
-pub fn start() {
- {
- let mut map = active_roots();
- if map.is_none() {
- *map = Some(HashMap::new());
- }
- }
- core_host::host().hub.subscribe(
- Changes::DEVICES | Changes::STATUS | Changes::CAPABILITIES | Changes::TOGGLES,
- || {
- sync();
- true
- },
- );
- sync();
-}
-
-/// Triggers a reconciliation of active sync roots (e.g. after a device toggle changes).
-pub fn sync() {
- let host = core_host::host();
- let Some(node) = core_host::node() else { return };
- let ready = host.hub.read(|s| matches!(s.status, Some(CoreStatus::Ready { .. })));
- if !ready {
- return;
- }
- let devices = host.hub.read(|s| {
- s.devices
- .iter()
- .map(|d| (d.id, d.info.name.clone(), matches!(d.link, LinkState::Online { .. })))
- .collect::<Vec<_>>()
- });
- let mut desired: Vec<(DeviceId, String, bool)> = Vec::new();
- let mut disabled: Vec<(DeviceId, String)> = Vec::new();
- for (id, name, online) in devices {
- let enabled = node
- .device_toggles(id)
- .ok()
- .and_then(|t| t.into_iter().find(|(k, _)| *k == "storage").map(|(_, on)| on))
- .unwrap_or(false);
- if enabled {
- desired.push((id, name, online));
- } else {
- disabled.push((id, name));
- }
- }
-
- let _ = std::thread::Builder::new().name("storage-sync".into()).spawn(move || {
- let _guard = lock(&RECONCILING);
- win::with_com(|| reconcile(&desired, &disabled));
- });
-}
-
-/// Handles core events (`StorageChanged`, `DeviceRemoved`, `LinkChanged`).
-pub fn on_event(event: &NodeEvent) {
- match event {
- NodeEvent::DeviceRemoved(device) => {
- let device = *device;
- let _ = std::thread::Builder::new().name("storage-unpair".into()).spawn(move || {
- let _guard = lock(&RECONCILING);
- win::with_com(|| remove_device_root(device, None));
- });
- }
- NodeEvent::DeviceAdded(_)
- | NodeEvent::PeerInfoChanged { .. }
- | NodeEvent::Capabilities(_)
- | NodeEvent::LinkChanged { .. } => {
- sync();
- }
- NodeEvent::StorageChanged { device, path } => {
- let device = *device;
- let path = path.clone();
- if let Some(root) = get_active_root(&device) {
- core_host::spawn(async move {
- refresh_folder(root, &path).await;
- });
- }
- }
- _ => {}
- }
-}
-
-/// Disconnects active sync roots when the desktop app exits.
-pub fn shutdown() {
- let roots: Vec<Arc<ActiveRoot>> =
- active_roots().as_mut().map(|m| m.drain().map(|(_, v)| v).collect()).unwrap_or_default();
- for root in roots {
- root.stop_watcher.store(true, Ordering::Relaxed);
- // SAFETY: connection_key was obtained from CfConnectSyncRoot.
- let _ = unsafe { CfDisconnectSyncRoot(root.connection_key) };
- }
-}
-
-/// Unregisters all `NectarLink!*` sync roots from Windows (`--uninstall`).
-pub fn unregister_all() {
- shutdown();
- win::with_com(|| {
- if let Ok(roots) = StorageProviderSyncRootManager::GetCurrentSyncRoots() {
- let count = roots.Size().unwrap_or(0);
- for i in 0..count {
- if let Ok(info) = roots.GetAt(i)
- && let Ok(id) = info.Id()
- {
- let id_str = id.to_string_lossy();
- if id_str.starts_with(PROVIDER_PREFIX) {
- let folder_path = info
- .Path()
- .ok()
- .and_then(|f| f.Path().ok())
- .map(|p| PathBuf::from(p.to_string_lossy()));
- let _ = StorageProviderSyncRootManager::Unregister(&id);
- if let Some(path) = folder_path {
- cleanup_sync_root_dir(&path);
- }
- }
- }
- }
- }
- if let Some(base) = base_sync_dir() {
- let _ = std::fs::remove_dir(&base);
- }
- });
-}
-
-/// Returns the local sync root path for a paired phone (`%USERPROFILE%\Nectarlink\<Phone name>`).
-pub fn sync_root_path_for(device: DeviceId) -> Option<PathBuf> {
- if let Some(active) = get_active_root(&device) {
- return Some(active.root_path.clone());
- }
- let name = core_host::host().hub.read(|s| s.name_of(&device))?;
- Some(base_sync_dir()?.join(sanitize_folder_name(&name)))
-}
-
-/// Opens a paired phone's sync root folder in File Explorer.
-pub fn open_in_explorer(device: DeviceId) -> Result<(), String> {
- let path =
- sync_root_path_for(device).ok_or_else(|| "Phone storage folder is not available".to_owned())?;
- if !path.exists() {
- std::fs::create_dir_all(&path).map_err(|e| e.to_string())?;
- }
- win::shell::run(Path::new("explorer.exe"), &format!("\"{}\"", path.display()))
-}
-
-fn base_sync_dir() -> Option<PathBuf> {
- dirs::home_dir().map(|h| h.join("Nectarlink"))
-}
-
-fn sync_root_id(device: DeviceId) -> HSTRING {
- HSTRING::from(format!("{PROVIDER_PREFIX}{device}"))
-}
-
-/// Sanitizes a phone's display name into a valid Windows directory name.
-pub fn sanitize_folder_name(device_name: &str) -> String {
- let cleaned: String = device_name
- .chars()
- .map(|c| if c.is_control() || r#"<>:"/\|?*"#.contains(c) { ' ' } else { c })
- .collect();
- let mut name = cleaned.split_whitespace().collect::<Vec<_>>().join(" ");
- name = name.trim_end_matches(['.', ' ']).chars().take(64).collect();
- let stem = name.split('.').next().unwrap_or_default().to_ascii_uppercase();
- let reserved = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")
- || ((stem.starts_with("COM") || stem.starts_with("LPT"))
- && stem.len() == 4
- && stem.as_bytes()[3].is_ascii_digit());
- if name.is_empty() {
- "Phone".into()
- } else if reserved {
- format!("{name} phone")
- } else {
- name
- }
-}
-
-/// Converts a Unix timestamp in milliseconds to Windows FILETIME 100ns ticks
-/// since 1601-01-01.
-pub fn unix_ms_to_filetime_ticks(unix_ms: i64) -> i64 {
- EPOCH_DIFF_100NS.saturating_add(unix_ms.max(0).saturating_mul(10_000))
-}
-
-/// Converts Windows FILETIME 100ns ticks since 1601-01-01 to Unix milliseconds.
-#[cfg(test)]
-pub fn filetime_ticks_to_unix_ms(ticks: i64) -> i64 {
- ticks.saturating_sub(EPOCH_DIFF_100NS).max(0) / 10_000
-}
-
-/// Computes the slash-separated relative path inside `root_path` from a full
-/// local path (`""` for the sync root itself). Returns `None` if `full_path` is
-/// not inside `root_path`.
-pub fn relative_path_under_root(root_path: &Path, full_path: &Path) -> Option<String> {
- let root_norm = normalize_win_path(root_path);
- let full_norm = normalize_win_path(full_path);
- let root_lower = root_norm.to_lowercase();
- let full_lower = full_norm.to_lowercase();
- if full_lower == root_lower {
- return Some(String::new());
- }
- let prefix = format!("{root_lower}\\");
- if !full_lower.starts_with(&prefix) {
- return None;
- }
- let rel = &full_norm[prefix.len()..];
- let parts: Vec<&str> = rel.split('\\').filter(|s| !s.is_empty()).collect();
- if parts.iter().any(|p| *p == "." || *p == "..") {
- return None;
- }
- Some(parts.join("/"))
-}
-
-fn normalize_win_path(path: &Path) -> String {
- let s = path.to_string_lossy();
- let stripped = s.strip_prefix(r"\\?\").unwrap_or(&s);
- stripped.trim_end_matches('\\').to_owned()
-}
-
-fn reconcile(desired: &[(DeviceId, String, bool)], disabled: &[(DeviceId, String)]) {
- // Unregister any disabled devices that belong to this instance.
- for (id, name) in disabled {
- remove_device_root(*id, Some(name));
- }
-
- // Remove any active roots no longer in `desired`.
- let active_ids: Vec<DeviceId> =
- active_roots().as_ref().map(|m| m.keys().copied().collect()).unwrap_or_default();
- for id in active_ids {
- if !desired.iter().any(|(d, _, _)| *d == id) {
- remove_device_root(id, None);
- }
- }
-
- // Register and connect desired roots.
- for (id, name, online) in desired {
- let already = get_active_root(id);
- if let Some(existing) = already {
- if existing.name == *name {
- if *online {
- let root = existing.clone();
- core_host::spawn(async move {
- let is_root_populated = lock(&root.populated_dirs).contains("");
- if is_root_populated {
- refresh_folder(root, "").await;
- }
- });
- }
- continue;
- }
- // Name changed: replace root.
- remove_device_root(*id, Some(&existing.name));
- }
-
- if let Err(e) = register_and_connect(*id, name, *online) {
- tracing::warn!(device = %id.short(), error = %e, "can't register phone storage sync root");
- }
- }
-}
-
-fn remove_device_root(device: DeviceId, known_name: Option<&str>) {
- let removed = active_roots().as_mut().and_then(|m| m.remove(&device));
- let mut folder_to_clean: Option<PathBuf> = None;
- if let Some(root) = removed {
- root.stop_watcher.store(true, Ordering::Relaxed);
- // SAFETY: connection_key was obtained from CfConnectSyncRoot.
- let _ = unsafe { CfDisconnectSyncRoot(root.connection_key) };
- folder_to_clean = Some(root.root_path.clone());
- } else if let Some(name) = known_name
- && let Some(base) = base_sync_dir()
- {
- folder_to_clean = Some(base.join(sanitize_folder_name(name)));
- }
-
- let id = sync_root_id(device);
- let _ = StorageProviderSyncRootManager::Unregister(&id);
-
- if let Some(path) = folder_to_clean {
- cleanup_sync_root_dir(&path);
- }
- if let Some(base) = base_sync_dir() {
- let _ = std::fs::remove_dir(&base);
- }
-}
-
-fn cleanup_sync_root_dir(path: &Path) {
- if !path.exists() {
- return;
- }
- let _ = std::fs::remove_dir_all(path);
-}
-
-fn register_and_connect(device: DeviceId, name: &str, online: bool) -> Result<(), String> {
- let base = base_sync_dir().ok_or_else(|| "no user home directory".to_owned())?;
- let folder_name = sanitize_folder_name(name);
- let root_path = base.join(&folder_name);
- std::fs::create_dir_all(&root_path).map_err(|e| format!("create sync root dir: {e}"))?;
-
- let exe = std::env::current_exe().map_err(|e| e.to_string())?;
- let icon_resource = format!("{},0", exe.display());
-
- let info = StorageProviderSyncRootInfo::new().map_err(|e| format!("SyncRootInfo::new: {e}"))?;
- info.SetId(&sync_root_id(device)).map_err(|e| format!("SetId: {e}"))?;
-
- let folder = StorageFolder::GetFolderFromPathAsync(&HSTRING::from(root_path.as_os_str()))
- .map_err(|e| format!("GetFolderFromPathAsync: {e}"))?
- .join()
- .map_err(|e| format!("GetFolderFromPathAsync.join: {e}"))?;
- info.SetPath(&folder).map_err(|e| format!("SetPath: {e}"))?;
- info.SetDisplayNameResource(&HSTRING::from(name)).map_err(|e| format!("SetDisplayNameResource: {e}"))?;
- info.SetIconResource(&HSTRING::from(&icon_resource)).map_err(|e| format!("SetIconResource: {e}"))?;
- info.SetVersion(&HSTRING::from(env!("CARGO_PKG_VERSION"))).map_err(|e| format!("SetVersion: {e}"))?;
- info.SetHydrationPolicy(StorageProviderHydrationPolicy::Full)
- .map_err(|e| format!("SetHydrationPolicy: {e}"))?;
- info.SetHydrationPolicyModifier(StorageProviderHydrationPolicyModifier::None)
- .map_err(|e| format!("SetHydrationPolicyModifier: {e}"))?;
- info.SetPopulationPolicy(StorageProviderPopulationPolicy::Full)
- .map_err(|e| format!("SetPopulationPolicy: {e}"))?;
- info.SetInSyncPolicy(
- StorageProviderInSyncPolicy::FileCreationTime | StorageProviderInSyncPolicy::DirectoryCreationTime,
- )
- .map_err(|e| format!("SetInSyncPolicy: {e}"))?;
- info.SetHardlinkPolicy(StorageProviderHardlinkPolicy::None)
- .map_err(|e| format!("SetHardlinkPolicy: {e}"))?;
- info.SetShowSiblingsAsGroup(false).map_err(|e| format!("SetShowSiblingsAsGroup: {e}"))?;
-
- let context_buf = CryptographicBuffer::ConvertStringToBinary(
- &HSTRING::from(device.to_string()),
- BinaryStringEncoding::Utf8,
- )
- .map_err(|e| format!("ConvertStringToBinary: {e}"))?;
- info.SetContext(&context_buf).map_err(|e| format!("SetContext: {e}"))?;
-
- StorageProviderSyncRootManager::Register(&info).map_err(|e| format!("Register: {e}"))?;
-
- let callbacks = [
- CF_CALLBACK_REGISTRATION {
- Type: CF_CALLBACK_TYPE_FETCH_PLACEHOLDERS,
- Callback: Some(on_fetch_placeholders),
- },
- CF_CALLBACK_REGISTRATION { Type: CF_CALLBACK_TYPE_FETCH_DATA, Callback: Some(on_fetch_data) },
- CF_CALLBACK_REGISTRATION { Type: CF_CALLBACK_TYPE_NOTIFY_RENAME, Callback: Some(on_notify_rename) },
- CF_CALLBACK_REGISTRATION { Type: CF_CALLBACK_TYPE_NOTIFY_DELETE, Callback: Some(on_notify_delete) },
- CF_CALLBACK_REGISTRATION { Type: CF_CALLBACK_TYPE_NONE, Callback: None },
- ];
-
- // Keep the DeviceId in a heap allocation for the process lifetime so async
- // cfapi callbacks always read a valid DeviceId and look up `ACTIVE_ROOTS`.
- let ctx_ptr: *const c_void = Box::into_raw(Box::new(device)).cast();
- let wide_root = to_wide_null(&root_path);
- // SAFETY: wide_root is null-terminated, callbacks ends with CF_CALLBACK_TYPE_NONE.
- let connection_key = unsafe {
- CfConnectSyncRoot(
- PCWSTR(wide_root.as_ptr()),
- callbacks.as_ptr(),
- Some(ctx_ptr),
- CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO | CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH,
- )
- }
- .map_err(|e| format!("CfConnectSyncRoot: {e}"))?;
-
- let stop_watcher = Arc::new(AtomicBool::new(false));
- let active = Arc::new(ActiveRoot {
- device,
- name: name.to_owned(),
- root_path: root_path.clone(),
- connection_key,
- stop_watcher: stop_watcher.clone(),
- populated_dirs: Mutex::new(HashSet::new()),
- uploading: Mutex::new(HashSet::new()),
- });
-
- if let Some(map) = active_roots().as_mut() {
- map.insert(device, active.clone());
- }
-
- start_directory_watcher(active.clone(), stop_watcher);
-
- if online {
- core_host::spawn(async move {
- // Pre-populate top-level placeholders when connected so opening the
- // sync root in Explorer or inspecting it immediately shows folders.
- refresh_folder(active, "").await;
- });
- }
-
- tracing::info!(device = %device.short(), "registered phone storage sync root");
- Ok(())
-}
-
-fn to_wide_null(s: impl AsRef<OsStr>) -> Vec<u16> {
- s.as_ref().encode_wide().chain(std::iter::once(0)).collect()
-}
-
-unsafe fn pcwstr_to_string(ptr: PCWSTR) -> String {
- if ptr.is_null() {
- return String::new();
- }
- // SAFETY: caller guarantees `ptr` is a valid null-terminated wide string.
- let slice = unsafe { ptr.as_wide() };
- std::ffi::OsString::from_wide(slice).to_string_lossy().into_owned()
-}
-
-unsafe fn callback_root_and_path(
- info: *const CF_CALLBACK_INFO,
-) -> Option<(Arc<ActiveRoot>, String, CF_CONNECTION_KEY, i64, i64)> {
- // SAFETY: Windows passes a valid CF_CALLBACK_INFO pointer to registered callbacks.
- let cb = unsafe { info.as_ref()? };
- if cb.CallbackContext.is_null() {
- return None;
- }
- // SAFETY: CallbackContext points to a leaked `Box<DeviceId>`.
- let device = unsafe { *(cb.CallbackContext as *const DeviceId) };
- let root = get_active_root(&device)?;
- // SAFETY: VolumeDosName and NormalizedPath are valid PCWSTRs in CF_CALLBACK_INFO.
- let vol = unsafe { pcwstr_to_string(cb.VolumeDosName) };
- // SAFETY: NormalizedPath is valid when CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH is set.
- let norm = unsafe { pcwstr_to_string(cb.NormalizedPath) };
- let full = PathBuf::from(format!("{vol}{norm}"));
- let rel = relative_path_under_root(&root.root_path, &full)?;
- Some((root, rel, cb.ConnectionKey, cb.TransferKey, cb.RequestKey))
-}
-
-fn make_op_info(
- connection_key: CF_CONNECTION_KEY,
- transfer_key: i64,
- request_key: i64,
- op_type: CF_OPERATION_TYPE,
-) -> CF_OPERATION_INFO {
- CF_OPERATION_INFO {
- StructSize: std::mem::size_of::<CF_OPERATION_INFO>() as u32,
- Type: op_type,
- ConnectionKey: connection_key,
- TransferKey: transfer_key,
- CorrelationVector: std::ptr::null(),
- SyncStatus: std::ptr::null(),
- RequestKey: request_key,
- }
-}
-
-unsafe extern "system" fn on_fetch_placeholders(
- callback_info: *const CF_CALLBACK_INFO,
- _callback_parameters: *const CF_CALLBACK_PARAMETERS,
-) {
- // SAFETY: callback_info is provided by Windows cfapi.
- let Some((root, rel_path, connection_key, transfer_key, request_key)) =
- (unsafe { callback_root_and_path(callback_info) })
- else {
- return;
- };
-
- core_host::spawn(async move {
- let Some(node) = core_host::node() else {
- ack_placeholders_failed(connection_key, transfer_key, request_key);
- return;
- };
- match node.storage_list(root.device, &rel_path).await {
- Ok(entries) => {
- lock(&root.populated_dirs).insert(rel_path.clone());
- transfer_placeholders(connection_key, transfer_key, request_key, &rel_path, &entries);
- }
- Err(e) => {
- tracing::debug!(
- device = %root.device.short(),
- error = %e,
- "fetch placeholders failed"
- );
- ack_placeholders_failed(connection_key, transfer_key, request_key);
- }
- }
- });
-}
-
-fn transfer_placeholders(
- connection_key: CF_CONNECTION_KEY,
- transfer_key: i64,
- request_key: i64,
- parent_rel: &str,
- entries: &[StorageEntry],
-) {
- let mut wide_names: Vec<Vec<u16>> = Vec::with_capacity(entries.len());
- let mut identities: Vec<Vec<u8>> = Vec::with_capacity(entries.len());
- for entry in entries {
- wide_names.push(to_wide_null(&entry.name));
- let rel =
- if parent_rel.is_empty() { entry.name.clone() } else { format!("{parent_rel}/{}", entry.name) };
- identities.push(rel.into_bytes());
- }
-
- let mut create_infos: Vec<CF_PLACEHOLDER_CREATE_INFO> = entries
- .iter()
- .enumerate()
- .map(|(i, entry)| build_placeholder_create_info(entry, &wide_names[i], &identities[i]))
- .collect();
-
- let op_info =
- make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS);
- let mut op_params = CF_OPERATION_PARAMETERS {
- ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
- Anonymous: CF_OPERATION_PARAMETERS_0 {
- TransferPlaceholders: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_4 {
- Flags: CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAG_DISABLE_ON_DEMAND_POPULATION,
- CompletionStatus: STATUS_SUCCESS,
- PlaceholderTotalCount: create_infos.len() as i64,
- PlaceholderArray: if create_infos.is_empty() {
- std::ptr::null_mut()
- } else {
- create_infos.as_mut_ptr()
- },
- PlaceholderCount: create_infos.len() as u32,
- EntriesProcessed: 0,
- },
- },
- };
- // SAFETY: op_info and op_params point to valid stack structures and buffers.
- let _ = unsafe { CfExecute(&op_info, &mut op_params) };
-}
-
-fn ack_placeholders_failed(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64) {
- let op_info =
- make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_TRANSFER_PLACEHOLDERS);
- let mut op_params = CF_OPERATION_PARAMETERS {
- ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
- Anonymous: CF_OPERATION_PARAMETERS_0 {
- TransferPlaceholders: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_4 {
- Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_TRANSFER_PLACEHOLDERS_FLAGS(0),
- CompletionStatus: STATUS_CLOUD_NETWORK_UNAVAILABLE,
- PlaceholderTotalCount: 0,
- PlaceholderArray: std::ptr::null_mut(),
- PlaceholderCount: 0,
- EntriesProcessed: 0,
- },
- },
- };
- // SAFETY: op_info and op_params are valid.
- let _ = unsafe { CfExecute(&op_info, &mut op_params) };
-}
-
-fn build_placeholder_create_info(
- entry: &StorageEntry,
- wide_name: &[u16],
- identity: &[u8],
-) -> CF_PLACEHOLDER_CREATE_INFO {
- let ticks = unix_ms_to_filetime_ticks(entry.modified);
- let attrs = if entry.is_dir { FILE_ATTRIBUTE_DIRECTORY.0 } else { FILE_ATTRIBUTE_NORMAL.0 };
- let file_size = if entry.is_dir { 0 } else { entry.size as i64 };
- CF_PLACEHOLDER_CREATE_INFO {
- RelativeFileName: PCWSTR(wide_name.as_ptr()),
- FsMetadata: CF_FS_METADATA {
- BasicInfo: FILE_BASIC_INFO {
- CreationTime: ticks,
- LastAccessTime: ticks,
- LastWriteTime: ticks,
- ChangeTime: ticks,
- FileAttributes: attrs,
- },
- FileSize: file_size,
- },
- FileIdentity: identity.as_ptr().cast(),
- FileIdentityLength: identity.len() as u32,
- Flags: CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC,
- Result: windows::core::HRESULT(0),
- CreateUsn: 0,
- }
-}
-
-unsafe extern "system" fn on_fetch_data(
- callback_info: *const CF_CALLBACK_INFO,
- callback_parameters: *const CF_CALLBACK_PARAMETERS,
-) {
- // SAFETY: callback_info and callback_parameters are provided by Windows cfapi.
- let Some((root, rel_path, connection_key, transfer_key, request_key)) =
- (unsafe { callback_root_and_path(callback_info) })
- else {
- return;
- };
- let Some(params) = (unsafe { callback_parameters.as_ref() }) else {
- return;
- };
- // SAFETY: Union variant matches CF_CALLBACK_TYPE_FETCH_DATA.
- let fetch = unsafe { params.Anonymous.FetchData };
- let required_offset = fetch.RequiredFileOffset.max(0) as u64;
- let required_length = fetch.RequiredLength.max(0) as u64;
-
- core_host::spawn(async move {
- let Some(node) = core_host::node() else {
- ack_data_failed(
- connection_key,
- transfer_key,
- request_key,
- required_offset as i64,
- STATUS_CLOUD_NETWORK_UNAVAILABLE,
- );
- return;
- };
-
- let (meta, mut recv) = match node
- .storage_read_stream(root.device, &rel_path, required_offset, Some(required_length))
- .await
- {
- Ok(res) => res,
- Err(e) => {
- tracing::debug!(
- device = %root.device.short(),
- error = %e,
- "fetch data stream failed"
- );
- ack_data_failed(
- connection_key,
- transfer_key,
- request_key,
- required_offset as i64,
- STATUS_CLOUD_NETWORK_UNAVAILABLE,
- );
- return;
- }
- };
- let total_size = meta.size;
-
- if required_length == 0 {
- let _ = transfer_data_chunk(
- connection_key,
- transfer_key,
- request_key,
- &[],
- required_offset as i64,
- STATUS_SUCCESS,
- );
- return;
- }
-
- let mut current_offset = required_offset;
- let mut remaining = required_length;
- let mut buf = Vec::with_capacity(HYDRATE_CHUNK_BYTES);
-
- while remaining > 0 {
- let want = (remaining as usize).min(HYDRATE_CHUNK_BYTES);
- buf.clear();
- while buf.len() < want {
- match recv.read_chunk(want - buf.len()).await {
- Ok(Some(chunk)) => buf.extend_from_slice(&chunk),
- Ok(None) => break,
- Err(e) => {
- tracing::debug!(error = %e, "hydration stream read error");
- ack_data_failed(
- connection_key,
- transfer_key,
- request_key,
- current_offset as i64,
- STATUS_CLOUD_NETWORK_UNAVAILABLE,
- );
- return;
- }
- }
- }
- if buf.is_empty() {
- break;
- }
- let len = buf.len() as u64;
- let completed = (current_offset + len).min(total_size);
- // SAFETY: valid connection_key and transfer_key during active fetch.
- let _ = unsafe {
- CfReportProviderProgress(connection_key, transfer_key, total_size as i64, completed as i64)
- };
- if !transfer_data_chunk(
- connection_key,
- transfer_key,
- request_key,
- &buf,
- current_offset as i64,
- STATUS_SUCCESS,
- ) {
- return;
- }
- current_offset += len;
- remaining = remaining.saturating_sub(len);
- }
- });
-}
-
-fn transfer_data_chunk(
- connection_key: CF_CONNECTION_KEY,
- transfer_key: i64,
- request_key: i64,
- data: &[u8],
- offset: i64,
- status: NTSTATUS,
-) -> bool {
- let op_info = make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_TRANSFER_DATA);
- let mut op_params = CF_OPERATION_PARAMETERS {
- ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
- Anonymous: CF_OPERATION_PARAMETERS_0 {
- TransferData: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_0 {
- Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_TRANSFER_DATA_FLAGS(0),
- CompletionStatus: status,
- Buffer: if data.is_empty() { std::ptr::null() } else { data.as_ptr().cast() },
- Offset: offset,
- Length: data.len() as i64,
- },
- },
- };
- // SAFETY: op_info and op_params reference valid memory for the duration of CfExecute.
- unsafe { CfExecute(&op_info, &mut op_params) }.is_ok()
-}
-
-fn ack_data_failed(
- connection_key: CF_CONNECTION_KEY,
- transfer_key: i64,
- request_key: i64,
- offset: i64,
- status: NTSTATUS,
-) {
- let _ = transfer_data_chunk(connection_key, transfer_key, request_key, &[], offset, status);
-}
-
-unsafe extern "system" fn on_notify_rename(
- callback_info: *const CF_CALLBACK_INFO,
- callback_parameters: *const CF_CALLBACK_PARAMETERS,
-) {
- // SAFETY: callback_info and callback_parameters are provided by Windows cfapi.
- let Some((root, old_rel, connection_key, transfer_key, request_key)) =
- (unsafe { callback_root_and_path(callback_info) })
- else {
- return;
- };
- let Some(cb) = (unsafe { callback_info.as_ref() }) else { return };
- let Some(params) = (unsafe { callback_parameters.as_ref() }) else { return };
- // SAFETY: Union variant matches CF_CALLBACK_TYPE_NOTIFY_RENAME.
- let rename = unsafe { params.Anonymous.Rename };
- // SAFETY: VolumeDosName and TargetPath are valid PCWSTRs.
- let vol = unsafe { pcwstr_to_string(cb.VolumeDosName) };
- let target_norm = unsafe { pcwstr_to_string(rename.TargetPath) };
- let target_full = PathBuf::from(format!("{vol}{target_norm}"));
- let new_rel = relative_path_under_root(&root.root_path, &target_full);
-
- core_host::spawn(async move {
- let Some(node) = core_host::node() else {
- ack_rename(connection_key, transfer_key, request_key, STATUS_CLOUD_NETWORK_UNAVAILABLE);
- return;
- };
- let status = match new_rel {
- Some(new_rel) if !old_rel.is_empty() && !new_rel.is_empty() => {
- match node.storage_rename(root.device, &old_rel, &new_rel).await {
- Ok(()) => STATUS_SUCCESS,
- Err(nectarlink_core::Error::Offline) => STATUS_CLOUD_NETWORK_UNAVAILABLE,
- Err(nectarlink_core::Error::Denied) => STATUS_ACCESS_DENIED,
- Err(_) => STATUS_CLOUD_UNSUCCESSFUL,
- }
- }
- _ => STATUS_ACCESS_DENIED,
- };
- ack_rename(connection_key, transfer_key, request_key, status);
- });
-}
-
-fn ack_rename(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64, status: NTSTATUS) {
- let op_info = make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_ACK_RENAME);
- let mut op_params = CF_OPERATION_PARAMETERS {
- ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
- Anonymous: CF_OPERATION_PARAMETERS_0 {
- AckRename: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_6 {
- Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_ACK_RENAME_FLAGS(0),
- CompletionStatus: status,
- },
- },
- };
- // SAFETY: op_info and op_params are valid.
- let _ = unsafe { CfExecute(&op_info, &mut op_params) };
-}
-
-unsafe extern "system" fn on_notify_delete(
- callback_info: *const CF_CALLBACK_INFO,
- _callback_parameters: *const CF_CALLBACK_PARAMETERS,
-) {
- // SAFETY: callback_info is provided by Windows cfapi.
- let Some((root, rel_path, connection_key, transfer_key, request_key)) =
- (unsafe { callback_root_and_path(callback_info) })
- else {
- return;
- };
-
- core_host::spawn(async move {
- if rel_path.is_empty() {
- ack_delete(connection_key, transfer_key, request_key, STATUS_ACCESS_DENIED);
- return;
- }
- let Some(node) = core_host::node() else {
- ack_delete(connection_key, transfer_key, request_key, STATUS_CLOUD_NETWORK_UNAVAILABLE);
- return;
- };
- let status = match node.storage_delete(root.device, &rel_path, true).await {
- Ok(()) => STATUS_SUCCESS,
- Err(nectarlink_core::Error::Offline) => STATUS_CLOUD_NETWORK_UNAVAILABLE,
- Err(nectarlink_core::Error::Denied) => STATUS_ACCESS_DENIED,
- Err(nectarlink_core::Error::Storage(msg)) if msg.contains("not_found") => STATUS_SUCCESS,
- Err(_) => STATUS_CLOUD_UNSUCCESSFUL,
- };
- ack_delete(connection_key, transfer_key, request_key, status);
- });
-}
-
-fn ack_delete(connection_key: CF_CONNECTION_KEY, transfer_key: i64, request_key: i64, status: NTSTATUS) {
- let op_info = make_op_info(connection_key, transfer_key, request_key, CF_OPERATION_TYPE_ACK_DELETE);
- let mut op_params = CF_OPERATION_PARAMETERS {
- ParamSize: std::mem::size_of::<CF_OPERATION_PARAMETERS>() as u32,
- Anonymous: CF_OPERATION_PARAMETERS_0 {
- AckDelete: windows::Win32::Storage::CloudFilters::CF_OPERATION_PARAMETERS_0_7 {
- Flags: windows::Win32::Storage::CloudFilters::CF_OPERATION_ACK_DELETE_FLAGS(0),
- CompletionStatus: status,
- },
- },
- };
- // SAFETY: op_info and op_params are valid.
- let _ = unsafe { CfExecute(&op_info, &mut op_params) };
-}
-
-/// Refreshes a folder's placeholders from the phone (`storage.changed` or initial connect).
-async fn refresh_folder(root: Arc<ActiveRoot>, rel_folder: &str) {
- let Some(node) = core_host::node() else { return };
- let Ok(entries) = node.storage_list(root.device, rel_folder).await else { return };
-
- let local_dir = if rel_folder.is_empty() {
- root.root_path.clone()
- } else {
- root.root_path.join(rel_folder.replace('/', "\\"))
- };
- if !local_dir.exists() {
- return;
- }
-
- lock(&root.populated_dirs).insert(rel_folder.to_owned());
-
- let mut missing: Vec<StorageEntry> = Vec::new();
- for entry in entries {
- let child_path = local_dir.join(&entry.name);
- if !child_path.exists() {
- missing.push(entry);
- }
- }
- if missing.is_empty() {
- return;
- }
-
- let mut wide_names: Vec<Vec<u16>> = Vec::with_capacity(missing.len());
- let mut identities: Vec<Vec<u8>> = Vec::with_capacity(missing.len());
- for entry in &missing {
- wide_names.push(to_wide_null(&entry.name));
- let rel =
- if rel_folder.is_empty() { entry.name.clone() } else { format!("{rel_folder}/{}", entry.name) };
- identities.push(rel.into_bytes());
- }
- let mut create_infos: Vec<CF_PLACEHOLDER_CREATE_INFO> = missing
- .iter()
- .enumerate()
- .map(|(i, entry)| build_placeholder_create_info(entry, &wide_names[i], &identities[i]))
- .collect();
-
- let wide_dir = to_wide_null(&local_dir);
- let mut processed = 0u32;
- // SAFETY: wide_dir is null-terminated and create_infos stays alive for the call.
- let _ = unsafe {
- CfCreatePlaceholders(
- PCWSTR(wide_dir.as_ptr()),
- &mut create_infos,
- windows::Win32::Storage::CloudFilters::CF_CREATE_FLAG_NONE,
- Some(&mut processed),
- )
- };
-}
-
-/// Watches the sync root directory for files or folders dropped in by the user
-/// and uploads them to the phone via `storage.write` / `storage.mkdir`.
-fn start_directory_watcher(root: Arc<ActiveRoot>, stop: Arc<AtomicBool>) {
- let _ = std::thread::Builder::new().name("storage-watch".into()).spawn(move || {
- let wide_dir = to_wide_null(&root.root_path);
- // SAFETY: wide_dir is a valid null-terminated path.
- let dir_handle = unsafe {
- CreateFileW(
- PCWSTR(wide_dir.as_ptr()),
- FILE_LIST_DIRECTORY.0,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- None,
- OPEN_EXISTING,
- FILE_FLAG_BACKUP_SEMANTICS,
- None,
- )
- };
- let Ok(dir_handle) = dir_handle else { return };
- if dir_handle == INVALID_HANDLE_VALUE {
- return;
- }
-
- let mut buf = vec![0u64; 4096];
- while !stop.load(Ordering::Relaxed) {
- let mut bytes_returned = 0u32;
- // SAFETY: buf is 8-byte aligned and valid for ReadDirectoryChangesW.
- let ok = unsafe {
- ReadDirectoryChangesW(
- dir_handle,
- buf.as_mut_ptr().cast(),
- (buf.len() * std::mem::size_of::<u64>()) as u32,
- true,
- FILE_NOTIFY_CHANGE_FILE_NAME
- | FILE_NOTIFY_CHANGE_DIR_NAME
- | FILE_NOTIFY_CHANGE_LAST_WRITE
- | FILE_NOTIFY_CHANGE_SIZE,
- Some(&mut bytes_returned),
- None,
- None,
- )
- }
- .is_ok();
-
- if !ok || stop.load(Ordering::Relaxed) || bytes_returned == 0 {
- break;
- }
-
- let changed_paths = parse_notify_buffer(
- // SAFETY: bytes_returned bytes were initialized by ReadDirectoryChangesW.
- unsafe { std::slice::from_raw_parts(buf.as_ptr().cast::<u8>(), bytes_returned as usize) },
- );
-
- for rel_win in changed_paths {
- let rel_slash = rel_win.replace('\\', "/");
- if rel_slash.is_empty()
- || rel_slash.split('/').any(|seg| seg.starts_with('.') || seg.starts_with("~$"))
- {
- continue;
- }
- let full_path = root.root_path.join(&rel_win);
- if !needs_upload(&full_path) {
- continue;
- }
- {
- let mut uploading = lock(&root.uploading);
- if !uploading.insert(rel_slash.clone()) {
- continue;
- }
- }
- let root_clone = root.clone();
- core_host::spawn(async move {
- // Brief debounce so Explorer finishes writing the dropped file.
- tokio::time::sleep(Duration::from_millis(350)).await;
- upload_local_item(&root_clone, &rel_slash, &full_path).await;
- lock(&root_clone.uploading).remove(&rel_slash);
- });
- }
- }
-
- // SAFETY: dir_handle was opened with CreateFileW above.
- let _ = unsafe { CloseHandle(dir_handle) };
- });
-}
-
-fn parse_notify_buffer(bytes: &[u8]) -> Vec<String> {
- let mut out = Vec::new();
- let mut offset = 0usize;
- while offset + std::mem::size_of::<FILE_NOTIFY_INFORMATION>() <= bytes.len() {
- // SAFETY: offset is within bounds and 4-byte aligned in ReadDirectoryChangesW buffer.
- let info = unsafe { &*(bytes.as_ptr().add(offset) as *const FILE_NOTIFY_INFORMATION) };
- let name_bytes = info.FileNameLength as usize;
- let name_offset = offset + std::mem::offset_of!(FILE_NOTIFY_INFORMATION, FileName);
- if name_offset + name_bytes <= bytes.len()
- && matches!(info.Action, FILE_ACTION_ADDED | FILE_ACTION_MODIFIED | FILE_ACTION_RENAMED_NEW_NAME)
- {
- // SAFETY: FileName is a UTF-16 slice of `name_bytes / 2` u16 elements.
- let wide = unsafe {
- std::slice::from_raw_parts(bytes.as_ptr().add(name_offset) as *const u16, name_bytes / 2)
- };
- let rel = std::ffi::OsString::from_wide(wide).to_string_lossy().into_owned();
- if !out.contains(&rel) {
- out.push(rel);
- }
- }
- if info.NextEntryOffset == 0 {
- break;
- }
- offset = offset.saturating_add(info.NextEntryOffset as usize);
- }
- out
-}
-
-/// Checks whether `full_path` is a newly dropped non-placeholder file/folder or
-/// a modified placeholder that is no longer in sync.
-fn needs_upload(full_path: &Path) -> bool {
- let wide = to_wide_null(full_path);
- let mut find_data = WIN32_FIND_DATAW::default();
- // SAFETY: wide is null-terminated and find_data is a valid struct.
- let handle = unsafe { FindFirstFileW(PCWSTR(wide.as_ptr()), &mut find_data) };
- let Ok(handle) = handle else { return false };
- if handle == INVALID_HANDLE_VALUE {
- return false;
- }
- // SAFETY: handle came from FindFirstFileW.
- let _ = unsafe { FindClose(handle) };
- // SAFETY: dwFileAttributes and dwReserved0 (reparse tag) were populated by FindFirstFileW.
- let state =
- unsafe { CfGetPlaceholderStateFromAttributeTag(find_data.dwFileAttributes, find_data.dwReserved0) };
- if (state.0 & CF_PLACEHOLDER_STATE_PLACEHOLDER.0) == 0 {
- return true;
- }
- (state.0 & CF_PLACEHOLDER_STATE_IN_SYNC.0) == 0
-}
-
-async fn upload_local_item(root: &Arc<ActiveRoot>, rel_slash: &str, full_path: &Path) {
- if !full_path.exists() || !needs_upload(full_path) {
- return;
- }
- let Some(node) = core_host::node() else { return };
- if full_path.is_dir() {
- if node.storage_mkdir(root.device, rel_slash).await.is_ok() {
- mark_path_in_sync(full_path, rel_slash, true);
- }
- } else if full_path.is_file() && node.storage_write(root.device, rel_slash, full_path).await.is_ok() {
- mark_path_in_sync(full_path, rel_slash, false);
- }
-}
-
-fn mark_path_in_sync(full_path: &Path, rel_slash: &str, is_dir: bool) {
- let wide = to_wide_null(full_path);
- let flags = if is_dir { FILE_FLAG_BACKUP_SEMANTICS } else { Default::default() };
- // SAFETY: wide is a valid null-terminated path.
- let handle = unsafe {
- CreateFileW(
- PCWSTR(wide.as_ptr()),
- GENERIC_READ.0 | GENERIC_WRITE.0,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- None,
- OPEN_EXISTING,
- flags,
- None,
- )
- };
- let Ok(handle): Result<HANDLE, _> = handle else { return };
- if handle == INVALID_HANDLE_VALUE {
- return;
- }
- let identity = rel_slash.as_bytes();
- // SAFETY: handle is open with read/write access; identity is valid for the call.
- let converted = unsafe {
- CfConvertToPlaceholder(
- handle,
- Some(identity.as_ptr().cast()),
- identity.len() as u32,
- CF_CONVERT_FLAG_MARK_IN_SYNC,
- None,
- None,
- )
- };
- if converted.is_err() {
- // Already a placeholder: mark it in sync.
- // SAFETY: handle is valid.
- let _ = unsafe { CfSetInSyncState(handle, CF_IN_SYNC_STATE_IN_SYNC, CF_SET_IN_SYNC_FLAG_NONE, None) };
- }
- // SAFETY: handle was opened with CreateFileW above.
- let _ = unsafe { CloseHandle(handle) };
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn converts_unix_ms_to_filetime_and_back() {
- let unix_ms = 1_710_000_000_123i64;
- let ticks = unix_ms_to_filetime_ticks(unix_ms);
- assert_eq!(ticks, EPOCH_DIFF_100NS + unix_ms * 10_000);
- assert_eq!(filetime_ticks_to_unix_ms(ticks), unix_ms);
- assert_eq!(unix_ms_to_filetime_ticks(-500), EPOCH_DIFF_100NS);
- }
-
- #[test]
- fn builds_placeholder_metadata_for_files_and_folders() {
- let file_entry = StorageEntry {
- name: "photo.jpg".into(),
- size: 42_000,
- modified: 1_700_000_000_000,
- is_dir: false,
- };
- let wide_file = to_wide_null(&file_entry.name);
- let id_file = b"DCIM/photo.jpg";
- let info_file = build_placeholder_create_info(&file_entry, &wide_file, id_file);
- assert_eq!(info_file.FsMetadata.FileSize, 42_000);
- assert_eq!(info_file.FsMetadata.BasicInfo.FileAttributes, FILE_ATTRIBUTE_NORMAL.0);
- assert_eq!(
- info_file.FsMetadata.BasicInfo.LastWriteTime,
- unix_ms_to_filetime_ticks(1_700_000_000_000)
- );
- assert_eq!(info_file.FileIdentityLength, id_file.len() as u32);
-
- let dir_entry =
- StorageEntry { name: "Camera".into(), size: 999, modified: 1_700_000_000_000, is_dir: true };
- let wide_dir = to_wide_null(&dir_entry.name);
- let id_dir = b"DCIM/Camera";
- let info_dir = build_placeholder_create_info(&dir_entry, &wide_dir, id_dir);
- assert_eq!(info_dir.FsMetadata.FileSize, 0);
- assert_eq!(info_dir.FsMetadata.BasicInfo.FileAttributes, FILE_ATTRIBUTE_DIRECTORY.0);
- }
-
- #[test]
- fn normalizes_relative_paths_and_sanitizes_folder_names() {
- let root = Path::new(r"C:\Users\alice\Nectarlink\Pixel 9");
- assert_eq!(relative_path_under_root(root, root), Some(String::new()));
- assert_eq!(
- relative_path_under_root(root, Path::new(r"C:\Users\alice\Nectarlink\Pixel 9\DCIM\Camera\a.jpg")),
- Some("DCIM/Camera/a.jpg".into())
- );
- assert_eq!(
- relative_path_under_root(root, Path::new(r"C:\Users\alice\Nectarlink\Pixel 9 Pro\DCIM")),
- None
- );
-
- assert_eq!(sanitize_folder_name("Pixel 9 (test)"), "Pixel 9 (test)");
- assert_eq!(sanitize_folder_name("My:Phone*1?"), "My Phone 1");
- assert_eq!(sanitize_folder_name("CON"), "CON phone");
- }
-}
diff --git a/desktop/app/src/updater.rs b/desktop/app/src/updater.rs
index 2f8b08c..723d00b 100644
--- a/desktop/app/src/updater.rs
+++ b/desktop/app/src/updater.rs
@@ -65,6 +65,7 @@ pub struct Update {
}
/// The scheduled task the installer registers (see the module docs).
+#[cfg(windows)]
pub const TASK: &str = "Nectarlink Update";
/// What the task runs: `nectarlink-desktop.exe --update-service`.
pub const SERVICE_ARG: &str = "--update-service";
@@ -72,7 +73,9 @@ pub const SERVICE_ARG: &str = "--update-service";
const QUIET_FOR: Duration = Duration::from_secs(10 * 60);
/// While an update waits for a quiet moment, how often to look for one.
const LOOK_FOR_QUIET: Duration = Duration::from_secs(5 * 60);
+#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
+#[cfg(windows)]
const DETACHED_PROCESS: u32 = 0x0000_0008;
static AUTO: AtomicBool = AtomicBool::new(true);
@@ -481,6 +484,7 @@ fn install_with_task(shown: bool) -> Result<(), String> {
/// A hidden PowerShell that waits (up to ten minutes) until `version` is
/// installed and the installer has finished, then opens the app again.
+#[allow(dead_code)]
fn spawn_relauncher(exe: &Path, version: &str, shown: bool) -> Result<(), String> {
#[cfg(windows)]
{
@@ -513,6 +517,7 @@ fn spawn_relauncher(exe: &Path, version: &str, shown: bool) -> Result<(), String
}
}
+#[allow(dead_code)]
fn relauncher_script(exe: &str, version: &str, arguments: &str) -> String {
let start = if arguments.is_empty() {
format!("Start-Process -FilePath '{exe}'")
diff --git a/spikes/s1-qt-rust/cpp/video_surface.cpp b/spikes/s1-qt-rust/cpp/video_surface.cpp
index eb13fae..4dd95c9 100644
--- a/spikes/s1-qt-rust/cpp/video_surface.cpp
+++ b/spikes/s1-qt-rust/cpp/video_surface.cpp
@@ -24,9 +24,9 @@ namespace nl {
namespace {
-#ifdef _WIN32
constexpr quint32 kWidth = 1920;
constexpr quint32 kHeight = 1080;
+#ifdef _WIN32
constexpr quint32 kFps = 60;
#endif
diff --git a/spikes/s1-qt-rust/src/main.rs b/spikes/s1-qt-rust/src/main.rs
index fee51c8..b5b1c53 100644
--- a/spikes/s1-qt-rust/src/main.rs
+++ b/spikes/s1-qt-rust/src/main.rs
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! Spike S1: can Qt Quick + Rust deliver the NectarLink design within the
//! performance budgets? See README.md for what is measured and the results.
-#![cfg_attr(not(windows), allow(unused))]
mod bridge;
mod mica;
diff --git a/spikes/s1-qt-rust/src/video.rs b/spikes/s1-qt-rust/src/video.rs
index f969b08..5352ad8 100644
--- a/spikes/s1-qt-rust/src/video.rs
+++ b/spikes/s1-qt-rust/src/video.rs
@@ -15,7 +15,12 @@ use std::{
Arc,
atomic::{AtomicBool, AtomicU64, Ordering},
},
- thread::{self, JoinHandle},
+ thread::JoinHandle,
+};
+
+#[cfg(windows)]
+use std::{
+ thread,
time::{Duration, Instant},
};
@@ -49,6 +54,7 @@ pub mod ffi {
/// Wakes the Qt item when a new frame is published (coalesced C++-side).
type FrameNotifier;
+ #[allow(dead_code)]
fn notify(self: &FrameNotifier);
}