Skip to content

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

fix(linux): incorporate linux-review-fixes for Part 4 media, PipeWire portal, and OpenH264 ABI

Jochen Kirstätter4b4f03eec109
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 262c25a..ad0a615 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -169,7 +169,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
libgl1-mesa-dev libxkbcommon-dev libvulkan-dev libx11-dev \
- ffmpeg
+ ffmpeg libopenh264-dev
- name: Install Qt
uses: jurplel/install-qt-action@v4
with:
diff --git a/desktop/app/i18n/rust_strings.h b/desktop/app/i18n/rust_strings.h
index 4933994..f88cc62 100644
--- a/desktop/app/i18n/rust_strings.h
+++ b/desktop/app/i18n/rust_strings.h
@@ -349,10 +349,13 @@ static const char *const nectarlink_rust_strings[] = {
QT_TRANSLATE_NOOP("Mirror", "Mirror recording"), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Mirror screenshot"), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Mirroring is turned off for this phone."), // desktop/app/src/mirror.rs
+ QT_TRANSLATE_NOOP("Mirror", "Recording from %1, saved in Downloads/Nectarlink"), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Recording from %1, saved in Downloads\\Nectarlink"), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Saved %1"), // desktop/app/src/mirror.rs
+ QT_TRANSLATE_NOOP("Mirror", "Saved %1 to Downloads/Nectarlink."), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Saved %1 to Downloads\\Nectarlink."), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Screenshot copied to clipboard."), // desktop/app/src/mirror.rs
+ QT_TRANSLATE_NOOP("Mirror", "Screenshot from %1, saved in Downloads/Nectarlink"), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Screenshot from %1, saved in Downloads\\Nectarlink"), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Sending %1 files…"), // desktop/app/src/mirror.rs
QT_TRANSLATE_NOOP("Mirror", "Sending %1…"), // desktop/app/src/mirror.rs
diff --git a/desktop/app/src/core_host.rs b/desktop/app/src/core_host.rs
index 903578c..527c647 100644
--- a/desktop/app/src/core_host.rs
+++ b/desktop/app/src/core_host.rs
@@ -121,8 +121,18 @@ pub fn compute_capabilities(
caps.push(nectarlink_core::DECK_ACTIONS.into());
caps.push(nectarlink_core::PC_AUDIO.into());
caps.push(nectarlink_core::RECORDER.into());
+ #[cfg(windows)]
caps.push(nectarlink_core::WEBCAM_VIRTUAL.into());
+ #[cfg(target_os = "linux")]
+ if crate::win::vcam::dll_path().is_some() || crate::win::vcam::is_registered() {
+ caps.push(nectarlink_core::WEBCAM_VIRTUAL.into());
+ }
+ #[cfg(windows)]
caps.push(nectarlink_core::MEDIA_STREAM.into());
+ #[cfg(target_os = "linux")]
+ if crate::win::window_capture::is_available() && crate::pc_mirror::is_capture_available() {
+ caps.push(nectarlink_core::MEDIA_STREAM.into());
+ }
caps.push(nectarlink_core::MIC_LISTEN.into());
}
#[cfg(windows)]
diff --git a/desktop/app/src/linux/audio.rs b/desktop/app/src/linux/audio.rs
index 61a30fe..f4a8b32 100644
--- a/desktop/app/src/linux/audio.rs
+++ b/desktop/app/src/linux/audio.rs
@@ -5,13 +5,11 @@
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
-use std::sync::{Mutex, OnceLock, mpsc};
+use std::sync::{OnceLock, mpsc};
use std::time::Duration;
use nectarlink_core::AudioOutputDevice;
-static FALLBACK_SPEAKER: Mutex<(u8, bool)> = Mutex::new((50, false));
-static FALLBACK_MIC_MUTED: Mutex<bool> = Mutex::new(false);
static WATCH_STARTED: OnceLock<()> = OnceLock::new();
/// Parses `wpctl get-volume` output such as `"Volume: 0.45"` or
@@ -94,6 +92,89 @@ pub(crate) fn parse_pactl_sinks(list_out: &str, default_sink: Option<&str>) -> V
out
}
+/// Parses the `Sinks:` section of `wpctl status` into sorted `AudioOutputDevice`
+/// entries (default first, then alphabetical by name).
+pub(crate) fn parse_wpctl_status_sinks(status_out: &str) -> Vec<AudioOutputDevice> {
+ let mut out = Vec::new();
+ let mut in_sinks = false;
+
+ for raw_line in status_out.lines() {
+ let stripped = raw_line
+ .trim_start_matches(|c: char| {
+ c.is_whitespace() || matches!(c, '│' | '├' | '└' | '─' | '|' | '+' | '-')
+ })
+ .trim();
+ if stripped.is_empty() {
+ continue;
+ }
+ if stripped == "Sinks:" {
+ in_sinks = true;
+ continue;
+ }
+ if !in_sinks {
+ continue;
+ }
+ let has_tree_indent =
+ raw_line.starts_with(char::is_whitespace) || raw_line.starts_with(['│', '├', '└', '|']);
+ if !has_tree_indent || stripped.ends_with(':') {
+ break;
+ }
+
+ let (is_default, after_star) = match stripped.strip_prefix('*') {
+ Some(rest) => (true, rest.trim_start()),
+ None => (false, stripped),
+ };
+ let Some((id_part, rest)) = after_star.split_once('.') else {
+ continue;
+ };
+ let id = id_part.trim();
+ if id.is_empty() || !id.chars().all(|c| c.is_ascii_digit()) {
+ continue;
+ }
+ let raw_name = match rest.rfind(" [") {
+ Some(idx) => &rest[..idx],
+ None => rest,
+ };
+ let name = raw_name.trim();
+ if !name.is_empty() {
+ out.push(AudioOutputDevice { id: id.to_owned(), name: name.to_owned(), is_default });
+ }
+ }
+
+ out.sort_by(|a, b| b.is_default.cmp(&a.is_default).then_with(|| a.name.cmp(&b.name)));
+ out
+}
+
+/// Resolves a numeric PipeWire sink node ID for `wpctl set-default` from `target`
+/// (which may already be numeric, a display name in `wpctl status`, or a node name
+/// in `wpctl status --name`).
+pub(crate) fn resolve_wpctl_sink_node_id(
+ status_out: &str,
+ status_name_out: Option<&str>,
+ target: &str,
+) -> Option<String> {
+ let trimmed = target.trim();
+ if trimmed.is_empty() {
+ return None;
+ }
+ if trimmed.chars().all(|c| c.is_ascii_digit()) {
+ return Some(trimmed.to_owned());
+ }
+ for sink in parse_wpctl_status_sinks(status_out) {
+ if sink.id == trimmed || sink.name.eq_ignore_ascii_case(trimmed) {
+ return Some(sink.id);
+ }
+ }
+ if let Some(name_out) = status_name_out {
+ for sink in parse_wpctl_status_sinks(name_out) {
+ if sink.id == trimmed || sink.name.eq_ignore_ascii_case(trimmed) {
+ return Some(sink.id);
+ }
+ }
+ }
+ None
+}
+
/// Returns `true` when a `pactl subscribe` event line indicates a sink, source,
/// or server state/device change (ignoring per-stream `sink-input` / `source-output` noise).
pub(crate) fn parse_pactl_subscribe_line(line: &str) -> bool {
@@ -111,66 +192,56 @@ pub(crate) fn parse_pactl_subscribe_line(line: &str) -> bool {
&& !trimmed.contains("source-output")
}
+const CMD_TIMEOUT: Duration = Duration::from_millis(800);
+
/// Reads the default speaker endpoint's `(volume_percent, muted)`.
pub fn speaker_state() -> Option<(u8, bool)> {
- if let Ok(out) = Command::new("wpctl").args(["get-volume", "@DEFAULT_AUDIO_SINK@"]).output()
+ if let Some(out) = super::run_cmd_timeout("wpctl", &["get-volume", "@DEFAULT_AUDIO_SINK@"], CMD_TIMEOUT)
&& out.status.success()
&& let Some(state) = parse_wpctl_volume(&String::from_utf8_lossy(&out.stdout))
{
- if let Ok(mut guard) = FALLBACK_SPEAKER.lock() {
- *guard = state;
- }
return Some(state);
}
- if let Ok(vol_out) = Command::new("pactl").args(["get-sink-volume", "@DEFAULT_SINK@"]).output()
+ if let Some(vol_out) =
+ super::run_cmd_timeout("pactl", &["get-sink-volume", "@DEFAULT_SINK@"], CMD_TIMEOUT)
&& vol_out.status.success()
{
- let mute_out = Command::new("pactl")
- .args(["get-sink-mute", "@DEFAULT_SINK@"])
- .output()
- .ok()
+ let mute_out = super::run_cmd_timeout("pactl", &["get-sink-mute", "@DEFAULT_SINK@"], CMD_TIMEOUT)
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).into_owned());
if let Some(state) =
parse_pactl_volume_and_mute(&String::from_utf8_lossy(&vol_out.stdout), mute_out.as_deref())
{
- if let Ok(mut guard) = FALLBACK_SPEAKER.lock() {
- *guard = state;
- }
return Some(state);
}
}
- Some(*FALLBACK_SPEAKER.lock().ok()?)
+ None
}
/// Sets the default speaker endpoint's master volume (`0..=100`).
pub fn set_speaker_volume(level: u8) -> Result<(), String> {
let clamped = level.min(100);
- if let Ok(mut guard) = FALLBACK_SPEAKER.lock() {
- guard.0 = clamped;
- }
let arg = format!("{clamped}%");
- let _ = Command::new("wpctl")
- .args(["set-volume", "@DEFAULT_AUDIO_SINK@", &arg])
- .status()
- .and_then(|s| if s.success() { Ok(s) } else { Err(std::io::Error::other("wpctl failed")) })
- .or_else(|_| Command::new("pactl").args(["set-sink-volume", "@DEFAULT_SINK@", &arg]).status());
+ if super::run_cmd_timeout("wpctl", &["set-volume", "@DEFAULT_AUDIO_SINK@", &arg], CMD_TIMEOUT)
+ .is_some_and(|o| o.status.success())
+ {
+ return Ok(());
+ }
+ let _ = super::run_cmd_timeout("pactl", &["set-sink-volume", "@DEFAULT_SINK@", &arg], CMD_TIMEOUT);
Ok(())
}
/// Sets the default speaker endpoint's mute state.
pub fn set_speaker_mute(muted: bool) -> Result<(), String> {
- if let Ok(mut guard) = FALLBACK_SPEAKER.lock() {
- guard.1 = muted;
- }
let flag = if muted { "1" } else { "0" };
- let _ = Command::new("wpctl")
- .args(["set-mute", "@DEFAULT_AUDIO_SINK@", flag])
- .status()
- .and_then(|s| if s.success() { Ok(s) } else { Err(std::io::Error::other("wpctl failed")) })
- .or_else(|_| Command::new("pactl").args(["set-sink-mute", "@DEFAULT_SINK@", flag]).status());
+ if super::run_cmd_timeout("wpctl", &["set-mute", "@DEFAULT_AUDIO_SINK@", flag], CMD_TIMEOUT)
+ .is_some_and(|o| o.status.success())
+ {
+ return Ok(());
+ }
+ let _ = super::run_cmd_timeout("pactl", &["set-sink-mute", "@DEFAULT_SINK@", flag], CMD_TIMEOUT);
Ok(())
}
@@ -203,57 +274,55 @@ pub fn toggle_speaker_mute() -> Result<bool, String> {
/// Reads whether the default capture (microphone) endpoint is muted.
pub fn mic_muted() -> Option<bool> {
- if let Ok(out) = Command::new("wpctl").args(["get-volume", "@DEFAULT_AUDIO_SOURCE@"]).output()
+ if let Some(out) = super::run_cmd_timeout("wpctl", &["get-volume", "@DEFAULT_AUDIO_SOURCE@"], CMD_TIMEOUT)
&& out.status.success()
&& let Some((_, muted)) = parse_wpctl_volume(&String::from_utf8_lossy(&out.stdout))
{
- if let Ok(mut guard) = FALLBACK_MIC_MUTED.lock() {
- *guard = muted;
- }
return Some(muted);
}
- if let Ok(out) = Command::new("pactl").args(["get-source-mute", "@DEFAULT_SOURCE@"]).output()
+ if let Some(out) = super::run_cmd_timeout("pactl", &["get-source-mute", "@DEFAULT_SOURCE@"], CMD_TIMEOUT)
&& out.status.success()
&& let Some(muted) = parse_pactl_mute(&String::from_utf8_lossy(&out.stdout))
{
- if let Ok(mut guard) = FALLBACK_MIC_MUTED.lock() {
- *guard = muted;
- }
return Some(muted);
}
- Some(*FALLBACK_MIC_MUTED.lock().ok()?)
+ None
}
/// Toggles mute on the default capture (microphone) endpoint and returns the new muted state.
pub fn toggle_mic_mute() -> Result<bool, String> {
let current = mic_muted().unwrap_or(false);
let next = !current;
- if let Ok(mut guard) = FALLBACK_MIC_MUTED.lock() {
- *guard = next;
- }
let flag = if next { "1" } else { "0" };
- let _ = Command::new("wpctl")
- .args(["set-mute", "@DEFAULT_AUDIO_SOURCE@", flag])
- .status()
- .and_then(|s| if s.success() { Ok(s) } else { Err(std::io::Error::other("wpctl failed")) })
- .or_else(|_| Command::new("pactl").args(["set-source-mute", "@DEFAULT_SOURCE@", flag]).status());
+ if super::run_cmd_timeout("wpctl", &["set-mute", "@DEFAULT_AUDIO_SOURCE@", flag], CMD_TIMEOUT)
+ .is_some_and(|o| o.status.success())
+ {
+ return Ok(next);
+ }
+ let _ = super::run_cmd_timeout("pactl", &["set-source-mute", "@DEFAULT_SOURCE@", flag], CMD_TIMEOUT);
Ok(next)
}
/// Lists active audio output sinks on this PC, marking the current default
/// output (`is_default: true`) and sorting the default first.
pub fn output_devices() -> Vec<AudioOutputDevice> {
- let default_sink = Command::new("pactl")
- .arg("get-default-sink")
- .output()
- .ok()
+ if let Some(wpctl_out) = super::run_cmd_timeout("wpctl", &["status"], CMD_TIMEOUT)
+ && wpctl_out.status.success()
+ {
+ let parsed = parse_wpctl_status_sinks(&String::from_utf8_lossy(&wpctl_out.stdout));
+ if !parsed.is_empty() {
+ return parsed;
+ }
+ }
+
+ let default_sink = super::run_cmd_timeout("pactl", &["get-default-sink"], CMD_TIMEOUT)
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_owned())
.filter(|s| !s.is_empty());
- if let Ok(list_out) = Command::new("pactl").args(["list", "sinks"]).output()
+ if let Some(list_out) = super::run_cmd_timeout("pactl", &["list", "sinks"], CMD_TIMEOUT)
&& list_out.status.success()
{
let parsed = parse_pactl_sinks(&String::from_utf8_lossy(&list_out.stdout), default_sink.as_deref());
@@ -262,11 +331,7 @@ pub fn output_devices() -> Vec<AudioOutputDevice> {
}
}
- vec![AudioOutputDevice {
- id: "default".to_string(),
- name: "Default Output".to_string(),
- is_default: true,
- }]
+ Vec::new()
}
/// Switches the system default output sink to `id`.
@@ -275,18 +340,33 @@ pub fn set_default_output_device(id: &str) -> Result<(), String> {
if trimmed.is_empty() || trimmed.starts_with('-') || trimmed.chars().any(char::is_control) {
return Err("output device id is invalid".to_string());
}
- let status = Command::new("pactl")
- .args(["set-default-sink", trimmed])
- .status()
- .and_then(|s| if s.success() { Ok(s) } else { Err(std::io::Error::other("pactl failed")) })
- .or_else(|_| Command::new("wpctl").args(["set-default", trimmed]).status())
- .map_err(|e| format!("failed to set default output device: {e}"))?;
- if status.success() { Ok(()) } else { Err(format!("audio backend rejected default sink '{trimmed}'")) }
+ let status_out = super::run_cmd_timeout("wpctl", &["status"], CMD_TIMEOUT)
+ .filter(|o| o.status.success())
+ .map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
+ .unwrap_or_default();
+ let status_name_out = super::run_cmd_timeout("wpctl", &["status", "--name"], CMD_TIMEOUT)
+ .filter(|o| o.status.success())
+ .map(|o| String::from_utf8_lossy(&o.stdout).into_owned());
+
+ if let Some(node_id) = resolve_wpctl_sink_node_id(&status_out, status_name_out.as_deref(), trimmed)
+ && super::run_cmd_timeout("wpctl", &["set-default", &node_id], CMD_TIMEOUT)
+ .is_some_and(|o| o.status.success())
+ {
+ return Ok(());
+ }
+
+ if super::run_cmd_timeout("pactl", &["set-default-sink", trimmed], CMD_TIMEOUT)
+ .is_some_and(|o| o.status.success())
+ {
+ return Ok(());
+ }
+
+ Err(format!("audio backend could not set default output device '{trimmed}'"))
}
/// Subscribes to PipeWire/PulseAudio sink/source/server events and invokes
-/// `changed` after coalescing rapid event bursts over 80 ms.
+/// `changed` after coalescing rapid event bursts over 150 ms.
pub fn watch(changed: fn()) {
if WATCH_STARTED.set(()).is_err() {
return;
@@ -295,9 +375,18 @@ pub fn watch(changed: fn()) {
}
fn watch_thread(changed: fn()) {
- let Ok(mut child) =
- Command::new("pactl").arg("subscribe").stdout(Stdio::piped()).stderr(Stdio::null()).spawn()
- else {
+ use std::os::unix::process::CommandExt;
+
+ let mut cmd = Command::new("pactl");
+ cmd.arg("subscribe").stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null());
+ // SAFETY: `prctl(PR_SET_PDEATHSIG, SIGTERM)` is async-signal-safe.
+ unsafe {
+ cmd.pre_exec(|| {
+ let _ = libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM);
+ Ok(())
+ });
+ }
+ let Ok(mut child) = cmd.spawn() else {
return;
};
let Some(stdout) = child.stdout.take() else {
@@ -317,7 +406,7 @@ fn watch_thread(changed: fn()) {
});
while rx.recv().is_ok() {
- while rx.recv_timeout(Duration::from_millis(80)).is_ok() {}
+ while rx.recv_timeout(Duration::from_millis(150)).is_ok() {}
changed();
}
@@ -369,6 +458,57 @@ Sink #52
assert!(!devices[1].is_default);
}
+ #[test]
+ fn parse_wpctl_status_sinks_parses_pipewire_tree_and_resolves_node_id() {
+ let status = "\
+PipeWire 'pipewire-0' [1.0.5, user@host, cookie:1234]
+ └─ Clients:
+ 32. WirePlumber [1.0.5, user@host, pid:999]
+
+Audio
+ ├─ Devices:
+ │ 42. Built-in Audio [alsa]
+ │
+ ├─ Sinks:
+ │ 46. Built-in Audio Analog Stereo [vol: 0.45]
+ │ * 58. Pixel Buds Pro [vol: 0.80 MUTED]
+ │
+ ├─ Sources:
+ │ * 47. Built-in Audio Analog Stereo [vol: 1.00]
+";
+ let status_name = "\
+Audio
+ ├─ Sinks:
+ │ 46. alsa_output.pci-0000_00_1f.3.analog-stereo [vol: 0.45]
+ │ * 58. bluez_output.AA_BB_CC_DD_EE_FF.1 [vol: 0.80 MUTED]
+ ├─ Sources:
+";
+ let devices = parse_wpctl_status_sinks(status);
+ assert_eq!(devices.len(), 2);
+ assert_eq!(devices[0].id, "58");
+ assert_eq!(devices[0].name, "Pixel Buds Pro");
+ assert!(devices[0].is_default);
+ assert_eq!(devices[1].id, "46");
+ assert_eq!(devices[1].name, "Built-in Audio Analog Stereo");
+ assert!(!devices[1].is_default);
+
+ assert_eq!(resolve_wpctl_sink_node_id(status, Some(status_name), "58").as_deref(), Some("58"));
+ assert_eq!(
+ resolve_wpctl_sink_node_id(status, Some(status_name), "Built-in Audio Analog Stereo").as_deref(),
+ Some("46")
+ );
+ assert_eq!(
+ resolve_wpctl_sink_node_id(
+ status,
+ Some(status_name),
+ "alsa_output.pci-0000_00_1f.3.analog-stereo"
+ )
+ .as_deref(),
+ Some("46")
+ );
+ assert!(resolve_wpctl_sink_node_id(status, Some(status_name), "nonexistent").is_none());
+ }
+
#[test]
fn parse_pactl_subscribe_line_filters_relevant_events() {
assert!(parse_pactl_subscribe_line("Event 'change' on sink #0"));
diff --git a/desktop/app/src/linux/audio_out.rs b/desktop/app/src/linux/audio_out.rs
index bc2a602..bd62c1e 100644
--- a/desktop/app/src/linux/audio_out.rs
+++ b/desktop/app/src/linux/audio_out.rs
@@ -12,7 +12,7 @@ use std::sync::{
atomic::{AtomicBool, Ordering},
};
use std::thread::{self, JoinHandle};
-use std::time::Duration;
+use std::time::{Duration, Instant};
const BYTES_PER_SAMPLE: usize = 2;
const JITTER_START_MS: usize = 40;
@@ -203,19 +203,29 @@ impl Player {
}
pub fn start_for_device(rate: u32, channels: u16, device_id: Option<&str>) -> Result<Player> {
+ let clean_rate = rate.max(8_000);
+ let clean_channels = channels.max(1);
+ let initial_child = spawn_pcm_sink(clean_rate, clean_channels, device_id).ok_or_else(|| {
+ std::io::Error::new(
+ std::io::ErrorKind::NotFound,
+ "no audio playback backend (pacat, pw-play, or aplay) available",
+ )
+ })?;
+
let (tx, rx) = mpsc::sync_channel::<Vec<u8>>(64);
let stop = Arc::new(AtomicBool::new(false));
let thread_stop = Arc::clone(&stop);
- let clean_rate = rate.max(8_000);
- let clean_channels = channels.max(1);
let dev_owned = device_id.map(str::to_owned);
+ let mut child_cell = Some(initial_child);
let handle = thread::Builder::new().name("nectarlink-audio-out".into()).spawn(move || {
let mut jitter = Jitter::new(clean_rate, clean_channels);
let frame_bytes = jitter.frame_bytes;
let frames_per_tick = ((clean_rate as usize) / 100).max(1);
let mut scratch = vec![0u8; frames_per_tick * frame_bytes];
- let mut child = spawn_pcm_sink(clean_rate, clean_channels, dev_owned.as_deref());
+ let mut child = child_cell.take();
+ let tick = Duration::from_millis(10);
+ let mut next_tick = Instant::now() + tick;
while !thread_stop.load(Ordering::Acquire) {
while let Ok(chunk) = rx.try_recv() {
@@ -231,7 +241,13 @@ impl Player {
let _ = proc.wait();
child = spawn_pcm_sink(clean_rate, clean_channels, dev_owned.as_deref());
}
- thread::sleep(Duration::from_millis(10));
+ let now = Instant::now();
+ if next_tick > now {
+ thread::sleep(next_tick - now);
+ } else if now.duration_since(next_tick) > Duration::from_millis(50) {
+ next_tick = now;
+ }
+ next_tick += tick;
}
if let Some(mut proc) = child {
@@ -306,8 +322,14 @@ mod tests {
#[test]
fn player_starts_accepts_pushes_and_drops_cleanly() {
- let player = Player::start(48_000, 2).expect("player should start");
- player.push(vec![0u8; 1920]);
- drop(player);
+ match Player::start(48_000, 2) {
+ Ok(player) => {
+ player.push(vec![0u8; 1920]);
+ drop(player);
+ }
+ Err(e) => {
+ assert_eq!(e.kind(), std::io::ErrorKind::NotFound);
+ }
+ }
}
}
diff --git a/desktop/app/src/linux/bt_hfp.rs b/desktop/app/src/linux/bt_hfp.rs
index fbe97b9..af963a1 100644
--- a/desktop/app/src/linux/bt_hfp.rs
+++ b/desktop/app/src/linux/bt_hfp.rs
@@ -1,11 +1,28 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! Paired Bluetooth device inspection and Hands-Free Profile (HFP) call audio
-//! routing on Linux via BlueZ (`bluetoothctl`) and PipeWire/PulseAudio
-//! (`pactl set-card-profile`).
+//! routing on Linux via BlueZ (`org.bluez` D-Bus `ObjectManager` with
+//! `bluetoothctl` fallback) and PipeWire/PulseAudio (`pactl set-card-profile`).
-use std::process::Command;
+use std::collections::HashMap;
+use std::process::{Command, Stdio};
use std::sync::{Mutex, OnceLock};
+use zbus::zvariant::{OwnedObjectPath, OwnedValue};
+
+const UUID_HFP_AG: &str = "0000111f-0000-1000-8000-00805f9b34fb";
+const UUID_HFP_HF: &str = "0000111e-0000-1000-8000-00805f9b34fb";
+const UUID_A2DP_SOURCE: &str = "0000110a-0000-1000-8000-00805f9b34fb";
+
+type ManagedObjectsMap = HashMap<OwnedObjectPath, HashMap<String, HashMap<String, OwnedValue>>>;
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(crate) struct BluezEndpoint {
+ pub object_path: String,
+ pub mac: String,
+ pub name: String,
+ pub connected: bool,
+}
+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct HfpStatus {
pub available: bool,
@@ -63,6 +80,9 @@ pub(crate) fn bluez_card_name_for_mac(mac: &str) -> String {
format!("bluez_card.{underscored}")
}
+/// Selects the paired Bluetooth `(mac, name)` endpoint whose name matches
+/// `phone_name` (case-insensitive substring match in either direction).
+/// Never falls back to an unrelated device (e.g. a Bluetooth mouse or headset).
pub fn match_endpoint<'a>(
endpoints: &'a [(String, String)],
phone_name: Option<&str>,
@@ -70,19 +90,105 @@ pub fn match_endpoint<'a>(
if endpoints.is_empty() {
return None;
}
- if let Some(needle) = phone_name.map(str::trim).filter(|s| !s.is_empty()) {
- let needle_lower = needle.to_ascii_lowercase();
- if let Some(found) = endpoints.iter().find(|(_, name)| {
- let hay = name.trim().to_ascii_lowercase();
- !hay.is_empty() && (hay.contains(&needle_lower) || needle_lower.contains(&hay))
- }) {
- return Some(found);
+ let needle = phone_name.map(str::trim).filter(|s| !s.is_empty())?;
+ let needle_lower = needle.to_ascii_lowercase();
+ endpoints.iter().find(|(_, name)| {
+ let hay = name.trim().to_ascii_lowercase();
+ !hay.is_empty() && (hay.contains(&needle_lower) || needle_lower.contains(&hay))
+ })
+}
+
+pub(crate) fn is_phone_capable_bluez_device(paired: bool, uuids: &[String]) -> bool {
+ if !paired {
+ return false;
+ }
+ if uuids.is_empty() {
+ return true;
+ }
+ uuids.iter().any(|u| {
+ let lower = u.trim().to_ascii_lowercase();
+ lower == UUID_HFP_AG || lower == UUID_HFP_HF || lower == UUID_A2DP_SOURCE
+ })
+}
+
+pub(crate) fn parse_bluez_managed_objects(objects: &ManagedObjectsMap) -> Vec<BluezEndpoint> {
+ let mut out = Vec::new();
+ for (path, ifaces) in objects {
+ let Some(dev) = ifaces.get("org.bluez.Device1") else {
+ continue;
+ };
+ let paired = dev.get("Paired").and_then(|v| bool::try_from(v).ok()).unwrap_or(false);
+ let uuids: Vec<String> = dev
+ .get("UUIDs")
+ .and_then(|v| v.try_clone().ok())
+ .and_then(|owned| Vec::<String>::try_from(owned).ok())
+ .unwrap_or_default();
+ if !is_phone_capable_bluez_device(paired, &uuids) {
+ continue;
}
+ let Some(mac) = dev
+ .get("Address")
+ .and_then(|v| <&str>::try_from(v).ok())
+ .map(str::trim)
+ .filter(|m| is_valid_bt_mac(m))
+ else {
+ continue;
+ };
+ let name = dev
+ .get("Alias")
+ .and_then(|v| <&str>::try_from(v).ok())
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ .or_else(|| {
+ dev.get("Name")
+ .and_then(|v| <&str>::try_from(v).ok())
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ })
+ .unwrap_or_default()
+ .to_owned();
+ let connected = dev.get("Connected").and_then(|v| bool::try_from(v).ok()).unwrap_or(false);
+ out.push(BluezEndpoint {
+ object_path: path.as_str().to_owned(),
+ mac: mac.to_owned(),
+ name,
+ connected,
+ });
}
- endpoints.first()
+ out.sort_by(|a, b| a.mac.cmp(&b.mac));
+ out
+}
+
+fn enumerate_bluez_dbus_devices() -> Option<Vec<BluezEndpoint>> {
+ super::non_tokio(|| {
+ let conn = zbus::blocking::Connection::system().ok()?;
+ if !super::dbus_name_has_owner(&conn, "org.bluez") {
+ return None;
+ }
+ let reply = conn
+ .call_method(
+ Some("org.bluez"),
+ "/",
+ Some("org.freedesktop.DBus.ObjectManager"),
+ "GetManagedObjects",
+ &(),
+ )
+ .ok()?;
+ let body = reply.body();
+ let objects = body
+ .deserialize::<ManagedObjectsMap>()
+ .ok()
+ .or_else(|| body.deserialize::<(ManagedObjectsMap,)>().ok().map(|(m,)| m))?;
+ Some(parse_bluez_managed_objects(&objects))
+ })
}
fn enumerate_paired_devices() -> Vec<(String, String)> {
+ if let Some(bluez) = enumerate_bluez_dbus_devices()
+ && !bluez.is_empty()
+ {
+ return bluez.into_iter().map(|e| (e.mac, e.name)).collect();
+ }
if let Ok(out) = Command::new("bluetoothctl").args(["devices", "Paired"]).output()
&& out.status.success()
{
@@ -100,6 +206,11 @@ fn enumerate_paired_devices() -> Vec<(String, String)> {
}
fn query_device_connected(mac: &str) -> bool {
+ if let Some(bluez) = enumerate_bluez_dbus_devices()
+ && let Some(found) = bluez.iter().find(|e| e.mac.eq_ignore_ascii_case(mac))
+ {
+ return found.connected;
+ }
Command::new("bluetoothctl")
.args(["info", mac])
.output()
@@ -135,9 +246,14 @@ pub fn probe_status(phone_name: Option<&str>) -> HfpStatus {
}
pub fn connect_audio(phone_name: Option<&str>) -> Result<HfpStatus, String> {
- let endpoints = enumerate_paired_devices();
+ let bluez_devices = enumerate_bluez_dbus_devices().unwrap_or_default();
+ let endpoints: Vec<(String, String)> = if !bluez_devices.is_empty() {
+ bluez_devices.iter().map(|e| (e.mac.clone(), e.name.clone())).collect()
+ } else {
+ enumerate_paired_devices()
+ };
let Some((mac, name)) = match_endpoint(&endpoints, phone_name) else {
- return Ok(probe_status(phone_name));
+ return Err("No paired Bluetooth phone endpoint found on this PC.".to_string());
};
let display = if name.trim().is_empty() {
phone_name.map(str::trim).filter(|s| !s.is_empty()).unwrap_or(mac.as_str()).to_owned()
@@ -145,15 +261,51 @@ pub fn connect_audio(phone_name: Option<&str>) -> Result<HfpStatus, String> {
name.clone()
};
- let _ = Command::new("bluetoothctl").args(["connect", mac]).status();
+ let connected_via_dbus = bluez_devices
+ .iter()
+ .find(|e| e.mac.eq_ignore_ascii_case(mac))
+ .and_then(|e| {
+ super::non_tokio(|| {
+ let conn = zbus::blocking::Connection::system().ok()?;
+ if !super::dbus_name_has_owner(&conn, "org.bluez") {
+ return None;
+ }
+ conn.call_method(
+ Some("org.bluez"),
+ e.object_path.as_str(),
+ Some("org.bluez.Device1"),
+ "Connect",
+ &(),
+ )
+ .ok()
+ })
+ })
+ .is_some();
+ if !connected_via_dbus {
+ let _ = Command::new("bluetoothctl").args(["connect", mac]).status();
+ }
+
let card = bluez_card_name_for_mac(mac);
- let _ = Command::new("pactl")
- .args(["set-card-profile", &card, "headset-head-unit"])
- .status()
- .and_then(|s| if s.success() { Ok(s) } else { Err(std::io::Error::other("profile")) })
- .or_else(|_| {
- Command::new("pactl").args(["set-card-profile", &card, "headset-head-unit-cvsd"]).status()
- });
+ for profile in [
+ "headset-audio-gateway",
+ "handsfree_head_unit",
+ "headset_head_unit",
+ "headset-head-unit",
+ "headset-head-unit-cvsd",
+ "hfp_hf",
+ "a2dp-sink",
+ ] {
+ if Command::new("pactl")
+ .args(["set-card-profile", &card, profile])
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .status()
+ .is_ok_and(|s| s.success())
+ {
+ break;
+ }
+ }
let connected = query_device_connected(mac);
if let Ok(mut guard) = active_slot().lock() {
@@ -172,14 +324,36 @@ pub fn disconnect_audio() {
&& let Some((mac, _)) = guard.take()
{
let card = bluez_card_name_for_mac(&mac);
- let _ = Command::new("pactl").args(["set-card-profile", &card, "a2dp-sink"]).status();
+ let _ = Command::new("pactl")
+ .args(["set-card-profile", &card, "a2dp-sink"])
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .status();
}
}
pub fn open_bluetooth_settings() {
- let _ = Command::new("blueman-manager")
+ let desktop = std::env::var("XDG_CURRENT_DESKTOP")
+ .or_else(|_| std::env::var("DESKTOP_SESSION"))
+ .unwrap_or_default()
+ .to_ascii_lowercase();
+ let is_kde = desktop.contains("kde") || desktop.contains("plasma");
+
+ if is_kde
+ && (Command::new("systemsettings").arg("kcm_bluetooth").spawn().is_ok()
+ || Command::new("kcmshell6").arg("kcm_bluetooth").spawn().is_ok())
+ {
+ return;
+ }
+
+ let _ = Command::new("gnome-control-center")
+ .arg("bluetooth")
.spawn()
- .or_else(|_| Command::new("gnome-control-center").arg("bluetooth").spawn())
+ .or_else(|_| Command::new("systemsettings").arg("kcm_bluetooth").spawn())
+ .or_else(|_| Command::new("kcmshell6").arg("kcm_bluetooth").spawn())
+ .or_else(|_| Command::new("blueman-manager").spawn())
+ .or_else(|_| Command::new("overskride").spawn())
.or_else(|_| Command::new("xdg-open").arg("settings://bluetooth").spawn());
}
@@ -198,10 +372,68 @@ mod tests {
}
#[test]
- fn match_endpoint_falls_back_to_first_when_unmatched() {
- let list = vec![("id-1".to_string(), "Phone A".to_string())];
- let matched = match_endpoint(&list, Some("Other Phone")).unwrap();
- assert_eq!(matched.0, "id-1");
+ fn match_endpoint_returns_none_when_unmatched_or_missing_name() {
+ let list = vec![("id-1".to_string(), "MX Master 3S".to_string())];
+ assert!(match_endpoint(&list, Some("Pixel 9 Pro")).is_none());
+ assert!(match_endpoint(&list, None).is_none());
+ assert!(match_endpoint(&list, Some(" ")).is_none());
+ }
+
+ #[test]
+ fn bluez_managed_objects_parser_filters_unpaired_and_non_audio_hid_devices() {
+ use zbus::zvariant::{ObjectPath, Str, Value};
+
+ let mut objects: ManagedObjectsMap = HashMap::new();
+
+ // 1. Paired phone advertising HFP Audio Gateway
+ let mut phone_props = HashMap::new();
+ phone_props.insert(
+ "Address".to_owned(),
+ OwnedValue::try_from(Value::from(Str::from("AA:BB:CC:DD:EE:FF"))).unwrap(),
+ );
+ phone_props
+ .insert("Alias".to_owned(), OwnedValue::try_from(Value::from(Str::from("Pixel 9 Pro"))).unwrap());
+ phone_props.insert("Paired".to_owned(), OwnedValue::from(true));
+ phone_props.insert("Connected".to_owned(), OwnedValue::from(true));
+ let phone_uuids = vec![Str::from(UUID_HFP_AG)];
+ phone_props.insert("UUIDs".to_owned(), OwnedValue::try_from(Value::from(phone_uuids)).unwrap());
+ let mut phone_ifaces = HashMap::new();
+ phone_ifaces.insert("org.bluez.Device1".to_owned(), phone_props);
+ objects.insert(
+ OwnedObjectPath::from(ObjectPath::from_static_str_unchecked(
+ "/org/bluez/hci0/dev_AA_BB_CC_DD_EE_FF",
+ )),
+ phone_ifaces,
+ );
+
+ // 2. Paired BLE mouse advertising only HID over GATT (00001812-...)
+ let mut mouse_props = HashMap::new();
+ mouse_props.insert(
+ "Address".to_owned(),
+ OwnedValue::try_from(Value::from(Str::from("11:22:33:44:55:66"))).unwrap(),
+ );
+ mouse_props.insert(
+ "Alias".to_owned(),
+ OwnedValue::try_from(Value::from(Str::from("MX Master 3S"))).unwrap(),
+ );
+ mouse_props.insert("Paired".to_owned(), OwnedValue::from(true));
+ mouse_props.insert("Connected".to_owned(), OwnedValue::from(true));
+ let mouse_uuids = vec![Str::from("00001812-0000-1000-8000-00805f9b34fb")];
+ mouse_props.insert("UUIDs".to_owned(), OwnedValue::try_from(Value::from(mouse_uuids)).unwrap());
+ let mut mouse_ifaces = HashMap::new();
+ mouse_ifaces.insert("org.bluez.Device1".to_owned(), mouse_props);
+ objects.insert(
+ OwnedObjectPath::from(ObjectPath::from_static_str_unchecked(
+ "/org/bluez/hci0/dev_11_22_33_44_55_66",
+ )),
+ mouse_ifaces,
+ );
+
+ let parsed = parse_bluez_managed_objects(&objects);
+ assert_eq!(parsed.len(), 1);
+ assert_eq!(parsed[0].mac, "AA:BB:CC:DD:EE:FF");
+ assert_eq!(parsed[0].name, "Pixel 9 Pro");
+ assert!(parsed[0].connected);
}
#[test]
diff --git a/desktop/app/src/linux/h264.rs b/desktop/app/src/linux/h264.rs
index d139355..a56abaa 100644
--- a/desktop/app/src/linux/h264.rs
+++ b/desktop/app/src/linux/h264.rs
@@ -52,15 +52,6 @@ struct AvCodecApi {
struct OpenH264Api {
create_decoder: unsafe extern "C" fn(*mut *mut c_void) -> c_int,
destroy_decoder: unsafe extern "C" fn(*mut c_void),
- initialize: unsafe extern "C" fn(*mut c_void, *const u8) -> c_int,
- uninitialize: unsafe extern "C" fn(*mut c_void) -> c_int,
- decode_frame_no_delay: unsafe extern "C" fn(
- *mut c_void,
- *const c_uchar,
- c_int,
- *mut *mut c_uchar,
- *mut OpenH264BufferInfo,
- ) -> c_int,
}
#[repr(C)]
@@ -92,7 +83,8 @@ unsafe fn load_sym<T: Copy>(lib: *mut c_void, name: &CStr) -> Option<T> {
fn avcodec_api() -> Option<&'static AvCodecApi> {
static API: OnceLock<Option<AvCodecApi>> = OnceLock::new();
API.get_or_init(|| unsafe {
- let avutil_candidates = [c"libavutil.so.58", c"libavutil.so.59", c"libavutil.so.57", c"libavutil.so"];
+ let avutil_candidates =
+ [c"libavutil.so.60", c"libavutil.so.59", c"libavutil.so.58", c"libavutil.so.57", c"libavutil.so"];
let mut avutil = ptr::null_mut();
for name in avutil_candidates {
avutil = libc::dlopen(name.as_ptr(), libc::RTLD_NOW | libc::RTLD_GLOBAL);
@@ -104,8 +96,13 @@ fn avcodec_api() -> Option<&'static AvCodecApi> {
return None;
}
- let avcodec_candidates =
- [c"libavcodec.so.60", c"libavcodec.so.61", c"libavcodec.so.59", c"libavcodec.so"];
+ let avcodec_candidates = [
+ c"libavcodec.so.62",
+ c"libavcodec.so.61",
+ c"libavcodec.so.60",
+ c"libavcodec.so.59",
+ c"libavcodec.so",
+ ];
let mut avcodec = ptr::null_mut();
for name in avcodec_candidates {
avcodec = libc::dlopen(name.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
@@ -122,7 +119,7 @@ fn avcodec_api() -> Option<&'static AvCodecApi> {
load_sym(avcodec, c"avcodec_version")?;
let avutil_major = avutil_version() >> 16;
let avcodec_major = avcodec_version() >> 16;
- if !(56..=59).contains(&avutil_major) || !(58..=61).contains(&avcodec_major) {
+ if !(56..=60).contains(&avutil_major) || !(58..=62).contains(&avcodec_major) {
tracing::warn!(
avcodec_major,
avutil_major,
@@ -155,7 +152,7 @@ fn avcodec_api() -> Option<&'static AvCodecApi> {
fn openh264_api() -> Option<&'static OpenH264Api> {
static API: OnceLock<Option<OpenH264Api>> = OnceLock::new();
API.get_or_init(|| unsafe {
- let candidates = [c"libopenh264.so.7", c"libopenh264.so.6", c"libopenh264.so"];
+ let candidates = [c"libopenh264.so.8", c"libopenh264.so.7", c"libopenh264.so.6", c"libopenh264.so"];
let mut lib = ptr::null_mut();
for name in candidates {
lib = libc::dlopen(name.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
@@ -179,12 +176,6 @@ fn openh264_api() -> Option<&'static OpenH264Api> {
Some(OpenH264Api {
create_decoder: load_sym(lib, c"WelsCreateDecoder")?,
destroy_decoder: load_sym(lib, c"WelsDestroyDecoder")?,
- initialize: load_sym(lib, c"_ZN7WelsDec12CWelsDecoder10InitializeEPK19TagSVCDecodingParam")?,
- uninitialize: load_sym(lib, c"_ZN7WelsDec12CWelsDecoder12UninitializeEv")?,
- decode_frame_no_delay: load_sym(
- lib,
- c"_ZN7WelsDec12CWelsDecoder18DecodeFrameNoDelayEPKhiPPhP13TagBufferInfo",
- )?,
})
})
.as_ref()
@@ -230,7 +221,10 @@ impl Drop for Backend {
&& !dec.is_null()
{
unsafe {
- let _ = (api.uninitialize)(*dec);
+ let vtable = *(*dec as *const *const usize);
+ let uninitialize: unsafe extern "C" fn(*mut c_void) -> c_int =
+ std::mem::transmute(*vtable.add(1));
+ let _ = uninitialize(*dec);
(api.destroy_decoder)(*dec);
}
}
@@ -289,8 +283,11 @@ impl Decoder {
if (api.create_decoder)(&mut dec) != 0 || dec.is_null() {
return None;
}
+ let vtable = *(dec as *const *const usize);
+ let initialize: unsafe extern "C" fn(*mut c_void, *const u8) -> c_int =
+ std::mem::transmute(*vtable.add(0));
let param = [0u8; 64];
- if (api.initialize)(dec, param.as_ptr()) != 0 {
+ if initialize(dec, param.as_ptr()) != 0 {
(api.destroy_decoder)(dec);
return None;
}
@@ -365,22 +362,26 @@ impl Decoder {
}
fn decode_openh264(dec: *mut c_void, data: &[u8], stream_colors: Option<Colors>) -> Result<Vec<Picture>> {
- let api = openh264_api().ok_or_else(|| Error::new(ErrorKind::Unsupported, "OpenH264 unavailable"))?;
+ let _api =
+ openh264_api().ok_or_else(|| Error::new(ErrorKind::Unsupported, "OpenH264 unavailable"))?;
let mut pictures = Vec::new();
// OpenH264's DecodeFrameNoDelay processes one NAL or a full access unit; feeding each
// Annex-B NAL unit in order guarantees SPS, PPS, SEI, and IDR/slice NALs in a single
// packet are all consumed cleanly.
for nal in split_annexb_nals(data) {
unsafe {
+ let vtable = *(dec as *const *const usize);
+ let decode_frame_no_delay: unsafe extern "C" fn(
+ *mut c_void,
+ *const c_uchar,
+ c_int,
+ *mut *mut c_uchar,
+ *mut OpenH264BufferInfo,
+ ) -> c_int = std::mem::transmute(*vtable.add(3));
let mut dst: [*mut c_uchar; 3] = [ptr::null_mut(); 3];
let mut info: OpenH264BufferInfo = std::mem::zeroed();
- let rc = (api.decode_frame_no_delay)(
- dec,
- nal.as_ptr(),
- nal.len() as c_int,
- dst.as_mut_ptr(),
- &mut info,
- );
+ let rc =
+ decode_frame_no_delay(dec, nal.as_ptr(), nal.len() as c_int, dst.as_mut_ptr(), &mut info);
if rc != 0 {
return Err(Error::new(
ErrorKind::InvalidData,
@@ -468,13 +469,16 @@ impl Decoder {
}
}
Backend::OpenH264 { dec } => {
- if let Some(api) = openh264_api()
- && !dec.is_null()
- {
+ if openh264_api().is_some() && !dec.is_null() {
unsafe {
- let _ = (api.uninitialize)(*dec);
+ let vtable = *(*dec as *const *const usize);
+ let initialize: unsafe extern "C" fn(*mut c_void, *const u8) -> c_int =
+ std::mem::transmute(*vtable.add(0));
+ let uninitialize: unsafe extern "C" fn(*mut c_void) -> c_int =
+ std::mem::transmute(*vtable.add(1));
+ let _ = uninitialize(*dec);
let param = [0u8; 64];
- let _ = (api.initialize)(*dec, param.as_ptr());
+ let _ = initialize(*dec, param.as_ptr());
}
}
}
diff --git a/desktop/app/src/linux/media_sessions.rs b/desktop/app/src/linux/media_sessions.rs
index 8977320..c8d74f5 100644
--- a/desktop/app/src/linux/media_sessions.rs
+++ b/desktop/app/src/linux/media_sessions.rs
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! What plays on this PC, for paired phones (`docs/protocol/media.md`): active
-//! MPRIS2 media sessions on Linux (via session D-Bus `org.mpris.MediaPlayer2.*`
-//! with a `playerctl` fallback), artwork resolution/caching, and remote
-//! transport commands.
+//! MPRIS2 media sessions on Linux via native in-process `zbus` D-Bus queries
+//! and signal subscriptions (`PropertiesChanged`, `Seeked`, `NameOwnerChanged`),
+//! artwork resolution/caching, and remote transport commands.
//!
//! NectarLink's own MPRIS session (`org.mpris.MediaPlayer2.nectarlink`) is
//! excluded so a phone never gets its own music echoed back.
@@ -11,14 +11,17 @@ use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
-use std::sync::{Mutex, OnceLock};
+use std::sync::{Mutex, OnceLock, mpsc};
use std::time::{Duration, Instant};
use nectarlink_core::{MediaAction, MediaError, MediaPlayer};
+use zbus::zvariant::{ObjectPath, OwnedValue};
/// Maximum inline artwork payload (256 KiB); larger covers are scaled and JPEG-encoded.
const ART_BYTES: u64 = 256 * 1024;
-const POLL_INTERVAL: Duration = Duration::from_secs(2);
+const ACTIVE_REFRESH_INTERVAL: Duration = Duration::from_secs(3);
+const IDLE_REFRESH_INTERVAL: Duration = Duration::from_secs(10);
+const SIGNAL_COALESCE_WINDOW: Duration = Duration::from_millis(60);
const CALL_PAUSE_TTL: Duration = Duration::from_secs(60 * 30);
const PLAYERCTL_FORMAT: &str = "{{playerName}}\t{{status}}\t{{mpris:trackid}}\t{{xesam:title}}\t{{xesam:artist}}\t{{xesam:album}}\t{{mpris:length}}\t{{position}}\t{{mpris:artUrl}}";
@@ -185,10 +188,14 @@ where
}
fn run(on_change: &OnChange) {
+ let (wake_tx, wake_rx) = mpsc::channel::<()>();
+ spawn_dbus_signal_watchers(wake_tx);
+
let mut art_cache = ArtCache::default();
let mut last: Option<(Vec<MediaPlayer>, Instant)> = None;
loop {
let players = snapshot(&mut art_cache);
+ let any_playing = players.iter().any(|p| p.playing);
let should_emit = match &last {
None => true,
Some((prev, prev_at)) => players_meaningfully_changed(prev, &players, prev_at.elapsed()),
@@ -197,10 +204,78 @@ fn run(on_change: &OnChange) {
on_change(players.clone());
last = Some((players, Instant::now()));
}
- std::thread::sleep(POLL_INTERVAL);
+ let wait_timeout = if any_playing { ACTIVE_REFRESH_INTERVAL } else { IDLE_REFRESH_INTERVAL };
+ if wake_rx.recv_timeout(wait_timeout).is_ok() {
+ while wake_rx.recv_timeout(SIGNAL_COALESCE_WINDOW).is_ok() {}
+ }
}
}
+fn spawn_dbus_signal_watchers(wake_tx: mpsc::Sender<()>) {
+ let tx_props = wake_tx.clone();
+ let _ = std::thread::Builder::new().name("mpris-props-watch".into()).spawn(move || {
+ loop {
+ let Ok(conn) = zbus::blocking::Connection::session() else {
+ std::thread::sleep(Duration::from_secs(5));
+ continue;
+ };
+ let Ok(rule) = zbus::MatchRule::builder()
+ .msg_type(zbus::message::Type::Signal)
+ .path("/org/mpris/MediaPlayer2")
+ .map(|b| b.build())
+ else {
+ return;
+ };
+ let Ok(iter) = zbus::blocking::MessageIterator::for_match_rule(rule, &conn, None) else {
+ std::thread::sleep(Duration::from_secs(5));
+ continue;
+ };
+ for msg in iter {
+ let Ok(msg) = msg else { break };
+ let hdr = msg.header();
+ let member = hdr.member().map(|m| m.as_str()).unwrap_or("");
+ if matches!(member, "PropertiesChanged" | "Seeked") && tx_props.send(()).is_err() {
+ return;
+ }
+ }
+ std::thread::sleep(Duration::from_secs(2));
+ }
+ });
+
+ let _ = std::thread::Builder::new().name("mpris-owner-watch".into()).spawn(move || {
+ loop {
+ let Ok(conn) = zbus::blocking::Connection::session() else {
+ std::thread::sleep(Duration::from_secs(5));
+ continue;
+ };
+ let Ok(rule) = zbus::MatchRule::builder()
+ .msg_type(zbus::message::Type::Signal)
+ .interface("org.freedesktop.DBus")
+ .and_then(|b| b.member("NameOwnerChanged"))
+ .map(|b| b.build())
+ else {
+ return;
+ };
+ let Ok(iter) = zbus::blocking::MessageIterator::for_match_rule(rule, &conn, None) else {
+ std::thread::sleep(Duration::from_secs(5));
+ continue;
+ };
+ for msg in iter {
+ let Ok(msg) = msg else { break };
+ if let Ok((name, _old_owner, _new_owner)) =
+ msg.body().deserialize::<(String, String, String)>()
+ && let Some(suffix) = name.strip_prefix("org.mpris.MediaPlayer2.")
+ && !is_excluded_player(suffix)
+ && wake_tx.send(()).is_err()
+ {
+ return;
+ }
+ }
+ std::thread::sleep(Duration::from_secs(2));
+ }
+ });
+}
+
/// Compares two snapshots of [`MediaPlayer`]s while ignoring normal linear playback
/// position progression so periodic polling does not trigger spurious UI/network updates.
pub(crate) fn players_meaningfully_changed(
@@ -275,10 +350,210 @@ fn snapshot(art_cache: &mut ArtCache) -> Vec<MediaPlayer> {
}
fn query_raw_players() -> Vec<RawMprisPlayer> {
- if let Some(players) = query_via_dbus() {
+ if let Some(players) = query_via_zbus() {
+ return players;
+ }
+ if let Some(players) = query_via_playerctl() {
return players;
}
- query_via_playerctl().unwrap_or_default()
+ query_via_gdbus().unwrap_or_default()
+}
+
+fn deserialize_string_list(msg: &zbus::message::Message) -> Option<Vec<String>> {
+ let body = msg.body();
+ body.deserialize::<Vec<String>>().ok().or_else(|| body.deserialize::<(Vec<String>,)>().ok().map(|(v,)| v))
+}
+
+fn deserialize_properties_map(msg: &zbus::message::Message) -> Option<HashMap<String, OwnedValue>> {
+ let body = msg.body();
+ body.deserialize::<HashMap<String, OwnedValue>>()
+ .ok()
+ .or_else(|| body.deserialize::<(HashMap<String, OwnedValue>,)>().ok().map(|(m,)| m))
+}
+
+fn deserialize_property_value(msg: &zbus::message::Message) -> Option<OwnedValue> {
+ let body = msg.body();
+ body.deserialize::<OwnedValue>().ok().or_else(|| body.deserialize::<(OwnedValue,)>().ok().map(|(v,)| v))
+}
+
+fn val_as_str(v: &OwnedValue) -> Option<String> {
+ if let Ok(s) = <&str>::try_from(v) {
+ let trimmed = s.trim();
+ if !trimmed.is_empty() {
+ return Some(trimmed.to_owned());
+ }
+ }
+ if let Ok(op) = <ObjectPath<'_>>::try_from(&**v) {
+ let s = op.as_str().trim();
+ if !s.is_empty() {
+ return Some(s.to_owned());
+ }
+ }
+ None
+}
+
+fn val_as_artist(v: &OwnedValue) -> Option<String> {
+ if let Some(s) = val_as_str(v) {
+ return Some(s);
+ }
+ if let Ok(cloned) = v.try_clone()
+ && let Ok(list) = Vec::<String>::try_from(cloned)
+ {
+ let joined: Vec<&str> =
+ list.iter().map(String::as_str).map(str::trim).filter(|s| !s.is_empty()).collect();
+ if !joined.is_empty() {
+ return Some(joined.join(", "));
+ }
+ }
+ None
+}
+
+fn val_as_i64(v: &OwnedValue) -> Option<i64> {
+ i64::try_from(v)
+ .ok()
+ .or_else(|| u64::try_from(v).ok().and_then(|u| i64::try_from(u).ok()))
+ .or_else(|| i32::try_from(v).ok().map(i64::from))
+ .or_else(|| u32::try_from(v).ok().map(i64::from))
+ .or_else(|| f64::try_from(v).ok().filter(|f| f.is_finite()).map(|f| f as i64))
+}
+
+fn val_as_bool(v: &OwnedValue) -> Option<bool> {
+ bool::try_from(v).ok()
+}
+
+fn val_as_dict(v: &OwnedValue) -> Option<HashMap<String, OwnedValue>> {
+ let cloned = v.try_clone().ok()?;
+ HashMap::<String, OwnedValue>::try_from(cloned).ok()
+}
+
+pub(crate) fn parse_zbus_player_properties(
+ bus_name: &str,
+ root_props: Option<&HashMap<String, OwnedValue>>,
+ player_props: &HashMap<String, OwnedValue>,
+) -> Option<RawMprisPlayer> {
+ let short_id = bus_name.strip_prefix("org.mpris.MediaPlayer2.").unwrap_or(bus_name);
+ if is_excluded_player(short_id) {
+ return None;
+ }
+
+ let metadata = player_props.get("Metadata").and_then(val_as_dict)?;
+ let raw_title = metadata.get("xesam:title").and_then(val_as_str)?;
+ let status = player_props.get("PlaybackStatus").and_then(val_as_str).unwrap_or_default();
+ let artist = metadata.get("xesam:artist").and_then(val_as_artist);
+ let album = metadata.get("xesam:album").and_then(val_as_str);
+ let art_url = metadata.get("mpris:artUrl").and_then(val_as_str);
+ let duration_us = metadata.get("mpris:length").and_then(val_as_i64).filter(|&v| v > 0);
+ let position_us = player_props.get("Position").and_then(val_as_i64).filter(|&v| v >= 0);
+
+ let duration = duration_us.map(|us| (us as u64) / 1000).filter(|&ms| ms > 0);
+ let position = position_us.map(|us| {
+ let ms = (us as u64) / 1000;
+ duration.map_or(ms, |d| ms.min(d))
+ });
+
+ let identity = root_props.and_then(|r| {
+ r.get("Identity").and_then(val_as_str).or_else(|| r.get("DesktopEntry").and_then(val_as_str))
+ });
+ let mut app = identity.unwrap_or_else(|| readable_player_app(short_id));
+ let mut title = raw_title;
+ if (says_video(short_id) || says_video(&app))
+ && let Some((clean, site)) = title.rsplit_once(" on ")
+ {
+ let site = site.trim();
+ let clean = clean.trim();
+ if !site.is_empty() && !clean.is_empty() && site.len() <= 28 {
+ app = site.to_owned();
+ title = clean.to_owned();
+ }
+ }
+
+ let mut actions = Vec::new();
+ if player_props.get("CanPlay").and_then(val_as_bool).unwrap_or(true) {
+ actions.push("play".to_owned());
+ }
+ if player_props.get("CanPause").and_then(val_as_bool).unwrap_or(true) {
+ actions.push("pause".to_owned());
+ }
+ if player_props.get("CanGoNext").and_then(val_as_bool).unwrap_or(true) {
+ actions.push("next".to_owned());
+ }
+ if player_props.get("CanGoPrevious").and_then(val_as_bool).unwrap_or(true) {
+ actions.push("previous".to_owned());
+ }
+ if player_props.get("CanSeek").and_then(val_as_bool).unwrap_or(duration.is_some()) {
+ actions.push("seek".to_owned());
+ }
+
+ Some(RawMprisPlayer {
+ id: short_id.to_owned(),
+ app,
+ title,
+ artist,
+ album,
+ playing: status.eq_ignore_ascii_case("Playing"),
+ duration,
+ position,
+ actions,
+ art_url,
+ })
+}
+
+fn list_zbus_mpris_buses(conn: &zbus::blocking::Connection) -> Option<Vec<String>> {
+ let reply = conn
+ .call_method(
+ Some("org.freedesktop.DBus"),
+ "/org/freedesktop/DBus",
+ Some("org.freedesktop.DBus"),
+ "ListNames",
+ &(),
+ )
+ .ok()?;
+ let all_names = deserialize_string_list(&reply)?;
+ let mut buses = Vec::new();
+ for name in all_names {
+ let trimmed = name.trim();
+ if let Some(suffix) = trimmed.strip_prefix("org.mpris.MediaPlayer2.")
+ && !is_excluded_player(suffix)
+ && !buses.iter().any(|existing| existing == trimmed)
+ {
+ buses.push(trimmed.to_owned());
+ }
+ }
+ Some(buses)
+}
+
+fn query_via_zbus() -> Option<Vec<RawMprisPlayer>> {
+ let conn = zbus::blocking::Connection::session().ok()?;
+ let buses = list_zbus_mpris_buses(&conn)?;
+ let mut players = Vec::new();
+ for bus in buses {
+ let root_props = conn
+ .call_method(
+ Some(bus.as_str()),
+ "/org/mpris/MediaPlayer2",
+ Some("org.freedesktop.DBus.Properties"),
+ "GetAll",
+ &("org.mpris.MediaPlayer2",),
+ )
+ .ok()
+ .and_then(|msg| deserialize_properties_map(&msg));
+ let Ok(player_msg) = conn.call_method(
+ Some(bus.as_str()),
+ "/org/mpris/MediaPlayer2",
+ Some("org.freedesktop.DBus.Properties"),
+ "GetAll",
+ &("org.mpris.MediaPlayer2.Player",),
+ ) else {
+ continue;
+ };
+ let Some(player_props) = deserialize_properties_map(&player_msg) else {
+ continue;
+ };
+ if let Some(player) = parse_zbus_player_properties(&bus, root_props.as_ref(), &player_props) {
+ players.push(player);
+ }
+ }
+ Some(players)
}
fn query_via_playerctl() -> Option<Vec<RawMprisPlayer>> {
@@ -402,7 +677,6 @@ pub(crate) fn parse_playerctl_line(line: &str) -> Option<RawMprisPlayer> {
/// Extracts `org.mpris.MediaPlayer2.*` bus names from `gdbus ListNames` output,
/// excluding `org.mpris.MediaPlayer2.nectarlink` and `playerctld`.
-#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn parse_mpris_bus_names(gdbus_output: &str) -> Vec<String> {
let mut names = Vec::new();
for token in gdbus_output.split(|c: char| {
@@ -419,7 +693,6 @@ pub(crate) fn parse_mpris_bus_names(gdbus_output: &str) -> Vec<String> {
names
}
-#[cfg_attr(not(test), allow(dead_code))]
fn extract_gvariant_string(text: &str, key: &str) -> Option<String> {
let pattern = format!("'{key}':");
let start = text.find(&pattern)? + pattern.len();
@@ -438,7 +711,6 @@ fn extract_gvariant_string(text: &str, key: &str) -> Option<String> {
if val.is_empty() { None } else { Some(val.to_owned()) }
}
-#[cfg_attr(not(test), allow(dead_code))]
fn extract_gvariant_i64(text: &str, key: &str) -> Option<i64> {
let pattern = format!("'{key}':");
let start = text.find(&pattern)? + pattern.len();
@@ -450,7 +722,6 @@ fn extract_gvariant_i64(text: &str, key: &str) -> Option<i64> {
num_str.parse::<i64>().ok()
}
-#[cfg_attr(not(test), allow(dead_code))]
fn extract_gvariant_bool(text: &str, key: &str) -> Option<bool> {
let pattern = format!("'{key}':");
let start = text.find(&pattern)? + pattern.len();
@@ -464,7 +735,6 @@ fn extract_gvariant_bool(text: &str, key: &str) -> Option<bool> {
}
}
-#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn parse_gdbus_player_properties(bus_name: &str, gdbus_output: &str) -> Option<RawMprisPlayer> {
let short_id = bus_name.strip_prefix("org.mpris.MediaPlayer2.").unwrap_or(bus_name);
if is_excluded_player(short_id) {
@@ -515,172 +785,49 @@ pub(crate) fn parse_gdbus_player_properties(bus_name: &str, gdbus_output: &str)
})
}
-fn value_as_string(val: &zbus::zvariant::Value<'_>) -> Option<String> {
- use zbus::zvariant::Value;
- match val {
- Value::Str(s) => {
- let trimmed = s.as_str().trim();
- if trimmed.is_empty() { None } else { Some(trimmed.to_owned()) }
- }
- Value::Value(inner) => value_as_string(inner),
- Value::Array(arr) => arr.iter().find_map(value_as_string),
- _ => None,
- }
-}
-
-fn value_as_i64(val: &zbus::zvariant::Value<'_>) -> Option<i64> {
- use zbus::zvariant::Value;
- match val {
- Value::I64(v) => Some(*v),
- Value::U64(v) => i64::try_from(*v).ok(),
- Value::I32(v) => Some(i64::from(*v)),
- Value::U32(v) => Some(i64::from(*v)),
- Value::Value(inner) => value_as_i64(inner),
- _ => None,
- }
-}
-
-fn value_as_bool(val: &zbus::zvariant::Value<'_>) -> Option<bool> {
- use zbus::zvariant::Value;
- match val {
- Value::Bool(b) => Some(*b),
- Value::Value(inner) => value_as_bool(inner),
- _ => None,
- }
-}
-
-fn parse_zbus_player_properties(
- bus_name: &str,
- props: &HashMap<String, zbus::zvariant::OwnedValue>,
-) -> Option<RawMprisPlayer> {
- use zbus::zvariant::Value;
-
- let short_id = bus_name.strip_prefix("org.mpris.MediaPlayer2.").unwrap_or(bus_name);
- if is_excluded_player(short_id) {
- return None;
- }
-
- let status = props.get("PlaybackStatus").and_then(|v| value_as_string(v)).unwrap_or_default();
- let position_us = props.get("Position").and_then(|v| value_as_i64(v)).filter(|&v| v >= 0);
-
- let mut raw_title: Option<String> = None;
- let mut artist: Option<String> = None;
- let mut album: Option<String> = None;
- let mut art_url: Option<String> = None;
- let mut duration_us: Option<i64> = None;
-
- if let Some(meta_owned) = props.get("Metadata") {
- let mut meta_val: &Value<'_> = meta_owned;
- while let Value::Value(inner) = meta_val {
- meta_val = inner;
- }
- if let Value::Dict(dict) = meta_val {
- for (k, v) in dict.iter() {
- let Some(key) = value_as_string(k) else {
- continue;
- };
- match key.as_str() {
- "xesam:title" => raw_title = value_as_string(v),
- "xesam:artist" => artist = value_as_string(v),
- "xesam:album" => album = value_as_string(v),
- "mpris:artUrl" => art_url = value_as_string(v),
- "mpris:length" => duration_us = value_as_i64(v).filter(|&n| n > 0),
- _ => {}
- }
- }
- }
- }
-
- let raw_title = raw_title?;
- let duration = duration_us.map(|us| (us as u64) / 1000).filter(|&ms| ms > 0);
- let position = position_us.map(|us| {
- let ms = (us as u64) / 1000;
- duration.map_or(ms, |d| ms.min(d))
- });
-
- let mut app = readable_player_app(short_id);
- let mut title = raw_title;
- if matches!(app.as_str(), "Firefox" | "Chrome" | "Chromium" | "Brave" | "Edge")
- && let Some((clean, site)) = title.rsplit_once(" on ")
- {
- let site = site.trim();
- let clean = clean.trim();
- if !site.is_empty() && !clean.is_empty() && site.len() <= 28 {
- app = site.to_owned();
- title = clean.to_owned();
- }
- }
-
- let mut actions = Vec::new();
- if props.get("CanPlay").and_then(|v| value_as_bool(v)).unwrap_or(true) {
- actions.push("play".to_owned());
- }
- if props.get("CanPause").and_then(|v| value_as_bool(v)).unwrap_or(true) {
- actions.push("pause".to_owned());
- }
- if props.get("CanGoNext").and_then(|v| value_as_bool(v)).unwrap_or(true) {
- actions.push("next".to_owned());
- }
- if props.get("CanGoPrevious").and_then(|v| value_as_bool(v)).unwrap_or(true) {
- actions.push("previous".to_owned());
- }
- if props.get("CanSeek").and_then(|v| value_as_bool(v)).unwrap_or(duration.is_some()) {
- actions.push("seek".to_owned());
- }
-
- Some(RawMprisPlayer {
- id: short_id.to_owned(),
- app,
- title,
- artist,
- album,
- playing: status.eq_ignore_ascii_case("Playing"),
- duration,
- position,
- actions,
- art_url,
- })
-}
-
-fn list_dbus_mpris_buses(conn: &zbus::blocking::Connection) -> Option<Vec<String>> {
- let reply = conn
- .call_method(
- Some("org.freedesktop.DBus"),
+fn list_gdbus_mpris_buses() -> Option<Vec<String>> {
+ let out = Command::new("gdbus")
+ .args([
+ "call",
+ "--session",
+ "--dest",
+ "org.freedesktop.DBus",
+ "--object-path",
"/org/freedesktop/DBus",
- Some("org.freedesktop.DBus"),
- "ListNames",
- &(),
- )
+ "--method",
+ "org.freedesktop.DBus.ListNames",
+ ])
+ .output()
.ok()?;
- let names: Vec<String> = reply.body().deserialize().ok()?;
- let mut buses = Vec::new();
- for name in names {
- let trimmed = name.trim();
- if let Some(suffix) = trimmed.strip_prefix("org.mpris.MediaPlayer2.")
- && !is_excluded_player(suffix)
- && !buses.iter().any(|existing| existing == trimmed)
- {
- buses.push(trimmed.to_owned());
- }
+ if !out.status.success() {
+ return None;
}
- Some(buses)
+ Some(parse_mpris_bus_names(&String::from_utf8_lossy(&out.stdout)))
}
-fn query_via_dbus() -> Option<Vec<RawMprisPlayer>> {
- let conn = zbus::blocking::Connection::session().ok()?;
- let buses = list_dbus_mpris_buses(&conn)?;
+fn query_via_gdbus() -> Option<Vec<RawMprisPlayer>> {
+ let buses = list_gdbus_mpris_buses()?;
let mut players = Vec::new();
for bus in buses {
- if let Ok(reply) = conn.call_method(
- Some(bus.as_str()),
- "/org/mpris/MediaPlayer2",
- Some("org.freedesktop.DBus.Properties"),
- "GetAll",
- &("org.mpris.MediaPlayer2.Player",),
- ) && let Ok(props) = reply.body().deserialize::<HashMap<String, zbus::zvariant::OwnedValue>>()
- && let Some(player) = parse_zbus_player_properties(&bus, &props)
+ if let Ok(out) = Command::new("gdbus")
+ .args([
+ "call",
+ "--session",
+ "--dest",
+ &bus,
+ "--object-path",
+ "/org/mpris/MediaPlayer2",
+ "--method",
+ "org.freedesktop.DBus.Properties.GetAll",
+ "org.mpris.MediaPlayer2.Player",
+ ])
+ .output()
+ && out.status.success()
{
- players.push(player);
+ let text = String::from_utf8_lossy(&out.stdout);
+ if let Some(player) = parse_gdbus_player_properties(&bus, &text) {
+ players.push(player);
+ }
}
}
Some(players)
@@ -695,45 +842,137 @@ fn mpris_bus_name(player: &str) -> String {
}
}
-fn dbus_player_call(bus_name: &str, method: &str) -> bool {
- let Ok(conn) = zbus::blocking::Connection::session() else {
- return false;
- };
- conn.call_method(
- Some(bus_name),
+fn gdbus_player_call(bus_name: &str, method: &str, extra_args: &[&str]) -> bool {
+ let method_full = format!("org.mpris.MediaPlayer2.Player.{method}");
+ let mut cmd = Command::new("gdbus");
+ cmd.args([
+ "call",
+ "--session",
+ "--dest",
+ bus_name,
+ "--object-path",
"/org/mpris/MediaPlayer2",
- Some("org.mpris.MediaPlayer2.Player"),
- method,
- &(),
- )
- .is_ok()
+ "--method",
+ &method_full,
+ ]);
+ cmd.args(extra_args);
+ cmd.output().is_ok_and(|o| o.status.success())
}
-fn dbus_player_seek_to(bus_name: &str, target_us: i64) -> bool {
- let Ok(conn) = zbus::blocking::Connection::session() else {
- return false;
- };
- let current_us = conn
- .call_method(
- Some(bus_name),
- "/org/mpris/MediaPlayer2",
- Some("org.freedesktop.DBus.Properties"),
- "Get",
- &("org.mpris.MediaPlayer2.Player", "Position"),
- )
- .ok()
- .and_then(|reply| reply.body().deserialize::<zbus::zvariant::OwnedValue>().ok())
- .and_then(|v| value_as_i64(&v))
- .unwrap_or(0);
- let offset_us = target_us.saturating_sub(current_us);
- conn.call_method(
- Some(bus_name),
- "/org/mpris/MediaPlayer2",
- Some("org.mpris.MediaPlayer2.Player"),
- "Seek",
- &(offset_us,),
- )
- .is_ok()
+/// Computes the relative MPRIS2 `Seek(Offset: x)` delta in microseconds from a
+/// target position (`target_us`) and current position (`current_us`).
+pub(crate) fn compute_seek_offset_us(target_us: i64, current_us: i64) -> i64 {
+ target_us.saturating_sub(current_us)
+}
+
+fn resolve_zbus_target_bus(conn: &zbus::blocking::Connection, player: &str) -> Option<String> {
+ let trimmed = player.trim();
+ let buses = list_zbus_mpris_buses(conn).unwrap_or_default();
+ if trimmed.is_empty() {
+ return buses.into_iter().next();
+ }
+ let exact = mpris_bus_name(trimmed);
+ if buses.iter().any(|b| b == &exact) {
+ return Some(exact);
+ }
+ let prefix = format!("{exact}.");
+ if let Some(found) = buses.into_iter().find(|b| b.starts_with(&prefix)) {
+ return Some(found);
+ }
+ Some(exact)
+}
+
+fn zbus_player_command(
+ conn: &zbus::blocking::Connection,
+ bus_name: &str,
+ action: MediaAction,
+ position: Option<u64>,
+) -> bool {
+ match action {
+ MediaAction::Play => conn
+ .call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "Play",
+ &(),
+ )
+ .is_ok(),
+ MediaAction::Pause => conn
+ .call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "Pause",
+ &(),
+ )
+ .is_ok(),
+ MediaAction::Next => conn
+ .call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "Next",
+ &(),
+ )
+ .is_ok(),
+ MediaAction::Previous => conn
+ .call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "Previous",
+ &(),
+ )
+ .is_ok(),
+ MediaAction::Seek => {
+ let pos_us = i64::try_from(position.unwrap_or(0)).unwrap_or(0).saturating_mul(1000);
+ if let Ok(meta_msg) = conn.call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.freedesktop.DBus.Properties"),
+ "Get",
+ &("org.mpris.MediaPlayer2.Player", "Metadata"),
+ ) && let Some(meta_val) = deserialize_property_value(&meta_msg)
+ && let Some(meta_dict) = val_as_dict(&meta_val)
+ && let Some(track_id_str) = meta_dict.get("mpris:trackid").and_then(val_as_str)
+ && let Ok(track_id) = ObjectPath::try_from(track_id_str.as_str())
+ && conn
+ .call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "SetPosition",
+ &(track_id, pos_us),
+ )
+ .is_ok()
+ {
+ return true;
+ }
+
+ let cur_us = conn
+ .call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.freedesktop.DBus.Properties"),
+ "Get",
+ &("org.mpris.MediaPlayer2.Player", "Position"),
+ )
+ .ok()
+ .and_then(|msg| deserialize_property_value(&msg))
+ .and_then(|v| val_as_i64(&v))
+ .unwrap_or(0);
+ let offset_us = compute_seek_offset_us(pos_us, cur_us);
+ conn.call_method(
+ Some(bus_name),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "Seek",
+ &(offset_us,),
+ )
+ .is_ok()
+ }
+ }
}
pub fn command(player: &str, action: MediaAction, position: Option<u64>) -> Result<(), MediaError> {
@@ -742,32 +981,18 @@ pub fn command(player: &str, action: MediaAction, position: Option<u64>) -> Resu
return Err(MediaError::Failed("invalid player identifier".into()));
}
- let target_bus = if trimmed.is_empty() {
- zbus::blocking::Connection::session()
- .ok()
- .and_then(|conn| list_dbus_mpris_buses(&conn))
- .and_then(|mut b| b.drain(..).next())
- } else {
- Some(mpris_bus_name(trimmed))
- };
-
- if let Some(ref bus) = target_bus {
- let ok = match action {
- MediaAction::Play => dbus_player_call(bus, "Play"),
- MediaAction::Pause => dbus_player_call(bus, "Pause"),
- MediaAction::Next => dbus_player_call(bus, "Next"),
- MediaAction::Previous => dbus_player_call(bus, "Previous"),
- MediaAction::Seek => {
- let target_us = i64::try_from(position.unwrap_or(0)).unwrap_or(0).saturating_mul(1000);
- dbus_player_seek_to(bus, target_us)
- }
+ if super::non_tokio(|| {
+ let Ok(conn) = zbus::blocking::Connection::session() else {
+ return false;
};
- if ok {
- return Ok(());
- }
+ let Some(target_bus) = resolve_zbus_target_bus(&conn, trimmed) else {
+ return false;
+ };
+ zbus_player_command(&conn, &target_bus, action, position)
+ }) {
+ return Ok(());
}
- // Fallback to `playerctl` when direct D-Bus transport command fails.
let mut cmd = Command::new("playerctl");
if !trimmed.is_empty() {
cmd.args(["--player", trimmed]);
@@ -791,25 +1016,71 @@ pub fn command(player: &str, action: MediaAction, position: Option<u64>) -> Resu
}
}
if cmd.status().is_ok_and(|s| s.success()) {
+ return Ok(());
+ }
+
+ // Fallback to direct `gdbus` MPRIS2 call when D-Bus session or `playerctl` fails.
+ let target_bus = if trimmed.is_empty() {
+ list_gdbus_mpris_buses().and_then(|mut b| b.drain(..).next()).ok_or(MediaError::NotFound)?
+ } else {
+ mpris_bus_name(trimmed)
+ };
+
+ let ok = match action {
+ MediaAction::Play => gdbus_player_call(&target_bus, "Play", &[]),
+ MediaAction::Pause => gdbus_player_call(&target_bus, "Pause", &[]),
+ MediaAction::Next => gdbus_player_call(&target_bus, "Next", &[]),
+ MediaAction::Previous => gdbus_player_call(&target_bus, "Previous", &[]),
+ MediaAction::Seek => {
+ let target_us = i64::try_from(position.unwrap_or(0)).unwrap_or(0).saturating_mul(1000);
+ let offset_str = target_us.to_string();
+ gdbus_player_call(&target_bus, "Seek", &[&offset_str])
+ }
+ };
+ if ok {
Ok(())
- } else if target_bus.is_none() {
- Err(MediaError::NotFound)
} else {
- Err(MediaError::Failed(format!("MPRIS command {action:?} failed for {trimmed}")))
+ Err(MediaError::Failed(format!("MPRIS command {action:?} failed for {target_bus}")))
}
}
pub fn pause_for_call() {
+ let _ = std::thread::Builder::new().name("call-media-pause".into()).spawn(pause_for_call_blocking);
+}
+
+fn pause_for_call_blocking() {
let _serial = CALL_MEDIA.lock().unwrap_or_else(|e| e.into_inner());
let mut paused = Vec::new();
- if let Some(players) = query_via_dbus() {
- for p in players {
- if p.playing {
- let bus = mpris_bus_name(&p.id);
- if dbus_player_call(&bus, "Pause") {
- paused.push(p.id);
- }
+ if let Ok(conn) = zbus::blocking::Connection::session()
+ && let Some(buses) = list_zbus_mpris_buses(&conn)
+ {
+ for bus in buses {
+ let is_playing = conn
+ .call_method(
+ Some(bus.as_str()),
+ "/org/mpris/MediaPlayer2",
+ Some("org.freedesktop.DBus.Properties"),
+ "Get",
+ &("org.mpris.MediaPlayer2.Player", "PlaybackStatus"),
+ )
+ .ok()
+ .and_then(|msg| deserialize_property_value(&msg))
+ .and_then(|v| val_as_str(&v))
+ .is_some_and(|s| s.eq_ignore_ascii_case("Playing"));
+ if is_playing
+ && conn
+ .call_method(
+ Some(bus.as_str()),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "Pause",
+ &(),
+ )
+ .is_ok()
+ {
+ let short_id = bus.strip_prefix("org.mpris.MediaPlayer2.").unwrap_or(&bus).to_owned();
+ paused.push(short_id);
}
}
} else if let Ok(out) = Command::new("playerctl")
@@ -827,6 +1098,15 @@ pub fn pause_for_call() {
}
}
}
+ } else if let Some(players) = query_via_gdbus() {
+ for p in players {
+ if p.playing {
+ let bus = mpris_bus_name(&p.id);
+ if gdbus_player_call(&bus, "Pause", &[]) {
+ paused.push(p.id);
+ }
+ }
+ }
}
if !paused.is_empty()
@@ -837,16 +1117,40 @@ pub fn pause_for_call() {
}
pub fn resume_after_call() {
+ let _ = std::thread::Builder::new().name("call-media-resume".into()).spawn(resume_after_call_blocking);
+}
+
+fn resume_after_call_blocking() {
let _serial = CALL_MEDIA.lock().unwrap_or_else(|e| e.into_inner());
let players = PAUSED_FOR_CALL
.lock()
.ok()
.and_then(|mut g| g.as_mut().map(|p| p.take_valid(Instant::now())))
.unwrap_or_default();
+ if players.is_empty() {
+ return;
+ }
+ let conn = zbus::blocking::Connection::session().ok();
for player in players {
let bus = mpris_bus_name(&player);
- if !dbus_player_call(&bus, "Play") {
- let _ = Command::new("playerctl").args(["--player", &player, "play"]).status();
+ let resumed_via_zbus = conn.as_ref().is_some_and(|c| {
+ c.call_method(
+ Some(bus.as_str()),
+ "/org/mpris/MediaPlayer2",
+ Some("org.mpris.MediaPlayer2.Player"),
+ "Play",
+ &(),
+ )
+ .is_ok()
+ });
+ if !resumed_via_zbus {
+ let ok = Command::new("playerctl")
+ .args(["--player", &player, "play"])
+ .status()
+ .is_ok_and(|s| s.success());
+ if !ok {
+ let _ = gdbus_player_call(&bus, "Play", &[]);
+ }
}
}
}
@@ -954,6 +1258,43 @@ mod tests {
assert!(expired.is_empty());
}
+ #[test]
+ fn parses_zbus_getall_player_properties_and_seek_offset() {
+ use zbus::zvariant::Value;
+
+ let mut meta: HashMap<&str, Value<'_>> = HashMap::new();
+ meta.insert("xesam:title", Value::from("Instant Crush"));
+ meta.insert("xesam:artist", Value::from(vec!["Daft Punk", "Julian Casablancas"]));
+ meta.insert("xesam:album", Value::from("Random Access Memories"));
+ meta.insert("mpris:length", Value::from(337_000_000_i64));
+ meta.insert("mpris:artUrl", Value::from("file:///tmp/ram.jpg"));
+
+ let mut player_props: HashMap<String, OwnedValue> = HashMap::new();
+ player_props
+ .insert("PlaybackStatus".to_owned(), OwnedValue::try_from(Value::from("Playing")).unwrap());
+ player_props
+ .insert("Position".to_owned(), OwnedValue::try_from(Value::from(45_000_000_i64)).unwrap());
+ player_props.insert("Metadata".to_owned(), OwnedValue::try_from(Value::from(meta)).unwrap());
+ player_props.insert("CanPlay".to_owned(), OwnedValue::try_from(Value::from(true)).unwrap());
+ player_props.insert("CanPause".to_owned(), OwnedValue::try_from(Value::from(true)).unwrap());
+ player_props.insert("CanSeek".to_owned(), OwnedValue::try_from(Value::from(true)).unwrap());
+
+ let parsed =
+ parse_zbus_player_properties("org.mpris.MediaPlayer2.spotify", None, &player_props).unwrap();
+ assert_eq!(parsed.id, "spotify");
+ assert_eq!(parsed.app, "Spotify");
+ assert_eq!(parsed.title, "Instant Crush");
+ assert_eq!(parsed.artist.as_deref(), Some("Daft Punk, Julian Casablancas"));
+ assert_eq!(parsed.album.as_deref(), Some("Random Access Memories"));
+ assert!(parsed.playing);
+ assert_eq!(parsed.duration, Some(337_000));
+ assert_eq!(parsed.position, Some(45_000));
+ assert_eq!(parsed.art_url.as_deref(), Some("file:///tmp/ram.jpg"));
+
+ assert_eq!(compute_seek_offset_us(60_000_000, 45_000_000), 15_000_000);
+ assert_eq!(compute_seek_offset_us(10_000_000, 45_000_000), -35_000_000);
+ }
+
#[test]
fn paused_for_call_returns_players_within_ttl() {
let start = Instant::now();
diff --git a/desktop/app/src/linux/mp4.rs b/desktop/app/src/linux/mp4.rs
index 49cabc8..068f857 100644
--- a/desktop/app/src/linux/mp4.rs
+++ b/desktop/app/src/linux/mp4.rs
@@ -153,17 +153,78 @@ pub fn merge_sps_pps(dest: &mut Vec<u8>, incoming: &[u8]) {
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Copy)]
struct RecordedFrame {
- pts_us: u64,
+ pts_100ns: u64,
keyframe: bool,
- data: Vec<u8>,
+ size: u32,
+}
+
+fn temp_payload_path_for(mp4_path: &Path) -> PathBuf {
+ use std::sync::atomic::{AtomicU64, Ordering};
+ static SEQ: AtomicU64 = AtomicU64::new(0);
+ let seq = SEQ.fetch_add(1, Ordering::Relaxed);
+ let parent = mp4_path.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or_else(|| Path::new("."));
+ let stem = mp4_path.file_stem().and_then(|s| s.to_str()).unwrap_or("rec");
+ let pid = std::process::id();
+ parent.join(format!(".{stem}.{pid}.{seq}.mdat.tmp"))
+}
+
+fn open_private_temp_file(path: &Path) -> std::io::Result<File> {
+ if let Some(parent) = path.parent()
+ && !parent.as_os_str().is_empty()
+ {
+ fs::create_dir_all(parent)?;
+ }
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::OpenOptionsExt;
+ fs::OpenOptions::new()
+ .read(true)
+ .write(true)
+ .create(true)
+ .truncate(true)
+ .custom_flags(libc::O_NOFOLLOW)
+ .mode(0o600)
+ .open(path)
+ }
+ #[cfg(not(unix))]
+ {
+ fs::OpenOptions::new().read(true).write(true).create(true).truncate(true).open(path)
+ }
+}
+
+fn annexb_to_avcc_sample(data: &[u8]) -> Vec<u8> {
+ let nals = split_annexb(data);
+ let mut sample_bytes = Vec::with_capacity(data.len());
+ for nal in &nals {
+ if nal.is_empty() || matches!(nal_type(nal), Some(7..=9)) {
+ continue;
+ }
+ let len = nal.len() as u32;
+ sample_bytes.extend_from_slice(&len.to_be_bytes());
+ sample_bytes.extend_from_slice(nal);
+ }
+ if sample_bytes.is_empty() {
+ for nal in &nals {
+ if nal.is_empty() {
+ continue;
+ }
+ let len = nal.len() as u32;
+ sample_bytes.extend_from_slice(&len.to_be_bytes());
+ sample_bytes.extend_from_slice(nal);
+ }
+ }
+ sample_bytes
}
/// Records a mirrored H.264 stream (and optional PCM audio stream) into an `.mp4` file.
#[derive(Debug)]
pub struct MirrorRecorder {
path: PathBuf,
+ payload_tmp_path: PathBuf,
+ payload_file: Option<File>,
+ payload_bytes: u64,
width: u32,
height: u32,
fps: u32,
@@ -188,8 +249,12 @@ impl MirrorRecorder {
) -> MirrorRecorder {
let started_epoch_ms =
SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |d| d.as_millis() as i64);
+ let payload_tmp_path = temp_payload_path_for(&path);
MirrorRecorder {
path,
+ payload_tmp_path,
+ payload_file: None,
+ payload_bytes: 0,
width: width.max(1),
height: height.max(1),
fps: fps.max(1),
@@ -235,7 +300,28 @@ impl MirrorRecorder {
}
}
- /// Processes an incoming Annex-B H.264 access unit.
+ fn write_video_sample(&mut self, pts_100ns: u64, keyframe: bool, data: &[u8]) -> Result<(), String> {
+ let sample_bytes = annexb_to_avcc_sample(data);
+ if sample_bytes.is_empty() {
+ return Ok(());
+ }
+ if self.payload_file.is_none() {
+ let f = open_private_temp_file(&self.payload_tmp_path).map_err(|e| {
+ format!("create temporary MP4 payload {}: {e}", self.payload_tmp_path.display())
+ })?;
+ self.payload_file = Some(f);
+ }
+ if let Some(f) = self.payload_file.as_mut() {
+ f.write_all(&sample_bytes).map_err(|e| format!("write MP4 payload: {e}"))?;
+ }
+ let size = sample_bytes.len() as u32;
+ self.payload_bytes = self.payload_bytes.saturating_add(u64::from(size));
+ self.frames.push(RecordedFrame { pts_100ns, keyframe, size });
+ Ok(())
+ }
+
+ /// Processes an incoming Annex-B H.264 access unit, streaming its AVCC
+ /// payload bytes directly to the temporary `mdat` file on disk.
pub fn on_packet(&mut self, keyframe: bool, time_us: u64, data: &[u8]) -> Result<(), String> {
if data.is_empty() {
return Ok(());
@@ -261,18 +347,9 @@ impl MirrorRecorder {
rel_us = self.last_pts_us + frame_duration_us;
}
self.last_pts_us = rel_us;
+ let pts_100ns = rel_us.saturating_mul(10);
- let payload = if is_keyframe && !has_inband_sps_pps(data) && !self.sps_pps.is_empty() {
- let mut combined = Vec::with_capacity(self.sps_pps.len() + data.len());
- combined.extend_from_slice(&self.sps_pps);
- combined.extend_from_slice(data);
- combined
- } else {
- data.to_vec()
- };
-
- self.frames.push(RecordedFrame { pts_us: rel_us, keyframe: is_keyframe, data: payload });
- Ok(())
+ self.write_video_sample(pts_100ns, is_keyframe, data)
}
/// Processes an incoming 16-bit interleaved PCM audio packet.
@@ -284,16 +361,32 @@ impl MirrorRecorder {
}
/// Finalizes the MP4 container and flushes it to disk.
- pub fn finish(self) -> Result<PathBuf, String> {
- if self.frames.is_empty() {
+ pub fn finish(mut self) -> Result<PathBuf, String> {
+ if self.frames.is_empty() || self.payload_bytes == 0 {
+ self.payload_file = None;
+ let _ = fs::remove_file(&self.payload_tmp_path);
let _ = fs::remove_file(&self.path);
return Err("No video frames were recorded yet.".into());
}
+ if let Some(mut f) = self.payload_file.take() {
+ let _ = f.flush();
+ }
if let Some(parent) = self.path.parent() {
let _ = fs::create_dir_all(parent);
}
- write_mp4_container(&self.path, self.width, self.height, self.fps, &self.sps_pps, &self.frames)?;
+ let write_res = write_mp4_container(
+ &self.path,
+ &self.payload_tmp_path,
+ self.payload_bytes,
+ self.width,
+ self.height,
+ self.fps,
+ &self.sps_pps,
+ &self.frames,
+ );
+ let _ = fs::remove_file(&self.payload_tmp_path);
+ write_res?;
if !self.audio_pcm.is_empty()
&& let Some((rate, channels)) = self.audio_cfg
@@ -303,7 +396,14 @@ impl MirrorRecorder {
mux_audio_with_ffmpeg(&self.path, rate, channels, &self.audio_pcm);
}
- Ok(self.path)
+ Ok(self.path.clone())
+ }
+}
+
+impl Drop for MirrorRecorder {
+ fn drop(&mut self) {
+ self.payload_file = None;
+ let _ = fs::remove_file(&self.payload_tmp_path);
}
}
@@ -398,8 +498,11 @@ fn mp4_full_box(fourcc: &[u8; 4], version: u8, flags: u32, body: &[u8]) -> Vec<u
out
}
+#[allow(clippy::too_many_arguments)]
fn write_mp4_container(
path: &Path,
+ payload_tmp_path: &Path,
+ payload_bytes: u64,
width: u32,
height: u32,
fps: u32,
@@ -407,37 +510,16 @@ fn write_mp4_container(
frames: &[RecordedFrame],
) -> Result<(), String> {
let default_delta = (TIMESCALE / fps.max(1)).max(1);
- let mut mdat_payload = Vec::new();
let mut sample_sizes = Vec::with_capacity(frames.len());
let mut sample_deltas = Vec::with_capacity(frames.len());
let mut sync_samples = Vec::new();
for (idx, frame) in frames.iter().enumerate() {
- let mut sample_bytes = Vec::new();
- for nal in split_annexb(&frame.data) {
- if nal.is_empty() || matches!(nal_type(nal), Some(7..=9)) {
- continue;
- }
- let len = nal.len() as u32;
- sample_bytes.extend_from_slice(&len.to_be_bytes());
- sample_bytes.extend_from_slice(nal);
- }
- if sample_bytes.is_empty() {
- for nal in split_annexb(&frame.data) {
- if nal.is_empty() {
- continue;
- }
- let len = nal.len() as u32;
- sample_bytes.extend_from_slice(&len.to_be_bytes());
- sample_bytes.extend_from_slice(nal);
- }
- }
- mdat_payload.extend_from_slice(&sample_bytes);
- sample_sizes.push(sample_bytes.len() as u32);
+ sample_sizes.push(frame.size);
let delta = if let Some(next) = frames.get(idx + 1) {
- let diff_us = next.pts_us.saturating_sub(frame.pts_us);
- ((diff_us * u64::from(TIMESCALE)) / 1_000_000).max(1) as u32
+ let diff_100ns = next.pts_100ns.saturating_sub(frame.pts_100ns);
+ ((diff_100ns * u64::from(TIMESCALE)) / 10_000_000).max(1) as u32
} else {
default_delta
};
@@ -452,7 +534,7 @@ fn write_mp4_container(
sync_samples.push(1);
}
- let (sps, pps) = extract_sps_and_pps_vecs(sps_pps, frames);
+ let (sps, pps) = extract_sps_and_pps_vecs(sps_pps);
let ftyp = build_ftyp();
let dummy_moov = build_moov(
width.max(1) as u16,
@@ -464,7 +546,9 @@ fn write_mp4_container(
&sample_sizes,
0,
);
- let chunk_offset = (ftyp.len() + dummy_moov.len() + 8) as u32;
+ let use_large_mdat = payload_bytes + 8 > u64::from(u32::MAX);
+ let mdat_header_len = if use_large_mdat { 16 } else { 8 };
+ let chunk_offset = (ftyp.len() + dummy_moov.len() + mdat_header_len) as u32;
let moov = build_moov(
width.max(1) as u16,
height.max(1) as u16,
@@ -475,17 +559,27 @@ fn write_mp4_container(
&sample_sizes,
chunk_offset,
);
- let mdat = mp4_box(b"mdat", &mdat_payload);
+ let mut payload_reader = File::open(payload_tmp_path)
+ .map_err(|e| format!("open temporary MP4 payload {}: {e}", payload_tmp_path.display()))?;
let mut file = File::create(path).map_err(|e| format!("create {}: {e}", path.display()))?;
file.write_all(&ftyp).map_err(|e| e.to_string())?;
file.write_all(&moov).map_err(|e| e.to_string())?;
- file.write_all(&mdat).map_err(|e| e.to_string())?;
+ if use_large_mdat {
+ file.write_all(&1u32.to_be_bytes()).map_err(|e| e.to_string())?;
+ file.write_all(b"mdat").map_err(|e| e.to_string())?;
+ file.write_all(&(payload_bytes + 16).to_be_bytes()).map_err(|e| e.to_string())?;
+ } else {
+ let total = (payload_bytes as u32).saturating_add(8);
+ file.write_all(&total.to_be_bytes()).map_err(|e| e.to_string())?;
+ file.write_all(b"mdat").map_err(|e| e.to_string())?;
+ }
+ std::io::copy(&mut payload_reader, &mut file).map_err(|e| e.to_string())?;
file.flush().map_err(|e| e.to_string())?;
Ok(())
}
-fn extract_sps_and_pps_vecs(sps_pps: &[u8], frames: &[RecordedFrame]) -> (Vec<u8>, Vec<u8>) {
+fn extract_sps_and_pps_vecs(sps_pps: &[u8]) -> (Vec<u8>, Vec<u8>) {
let mut sps = None;
let mut pps = None;
for nal in split_annexb(sps_pps) {
@@ -495,20 +589,6 @@ fn extract_sps_and_pps_vecs(sps_pps: &[u8], frames: &[RecordedFrame]) -> (Vec<u8
_ => {}
}
}
- if sps.is_none() || pps.is_none() {
- for frame in frames {
- for nal in split_annexb(&frame.data) {
- match nal_type(nal) {
- Some(7) if sps.is_none() => sps = Some(nal.to_vec()),
- Some(8) if pps.is_none() => pps = Some(nal.to_vec()),
- _ => {}
- }
- }
- if sps.is_some() && pps.is_some() {
- break;
- }
- }
- }
let sps = sps.unwrap_or_else(|| vec![0x67, 0x42, 0xC0, 0x1F, 0xDA, 0x01, 0x40, 0x16, 0xEC, 0x04, 0x40]);
let pps = pps.unwrap_or_else(|| vec![0x68, 0xCE, 0x3C, 0x80]);
(sps, pps)
diff --git a/desktop/app/src/linux/smtc.rs b/desktop/app/src/linux/smtc.rs
index 86b1cf3..d7ee6fb 100644
--- a/desktop/app/src/linux/smtc.rs
+++ b/desktop/app/src/linux/smtc.rs
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! What plays on a phone, exported to Linux desktop media controls (MPRIS2
-//! provider `org.mpris.MediaPlayer2.nectarlink` and atomic JSON state snapshot).
+//! D-Bus service `org.mpris.MediaPlayer2.nectarlink` on `/org/mpris/MediaPlayer2`
+//! and atomic JSON state snapshot).
//!
//! Mirrors the public API of `win::smtc`: [`start`] registers the command
//! callback and spawns the `"media-controls"` worker thread; [`show`] updates
@@ -14,7 +15,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use nectarlink_core::{DeviceId, MediaAction};
use serde::{Deserialize, Serialize};
-use zbus::zvariant::{ObjectPath, OwnedValue, Str};
+use zbus::zvariant::{ObjectPath, OwnedValue, Str, Value};
use crate::state::PlayerView;
@@ -92,42 +93,71 @@ where
}
fn current_export_state() -> MprisExportState {
- let latest = lock(&LAST_SHOWN).clone();
- build_export_state(latest.as_ref())
+ let shown = lock(&LAST_SHOWN).clone();
+ build_export_state(shown.as_ref())
}
-fn build_mpris_metadata_dict(state: &MprisExportState) -> HashMap<String, OwnedValue> {
- let mut dict = HashMap::new();
+pub(crate) fn build_mpris_metadata_map(state: &MprisExportState) -> HashMap<String, OwnedValue> {
+ let mut map = HashMap::new();
let track_path = if state.enabled {
- "/org/mpris/MediaPlayer2/TrackList/CurrentTrack"
+ "/org/nectarlink/MediaPlayer2/Track/current"
} else {
"/org/mpris/MediaPlayer2/TrackList/NoTrack"
};
- if let Ok(path) = ObjectPath::try_from(track_path) {
- dict.insert("mpris:trackid".to_owned(), OwnedValue::from(path));
+ if let Ok(path) = ObjectPath::try_from(track_path)
+ && let Ok(val) = OwnedValue::try_from(Value::from(path))
+ {
+ map.insert("mpris:trackid".to_owned(), val);
}
- if state.enabled {
- dict.insert("xesam:title".to_owned(), OwnedValue::from(Str::from(state.title.as_str())));
- if let Ok(artists) = zbus::zvariant::Value::from(vec![state.artist.as_str()]).try_to_owned() {
- dict.insert("xesam:artist".to_owned(), artists);
- }
- dict.insert("xesam:album".to_owned(), OwnedValue::from(Str::from(state.album.as_str())));
- if let Some(ref art_url) = state.art_url {
- dict.insert("mpris:artUrl".to_owned(), OwnedValue::from(Str::from(art_url.as_str())));
- }
- if let Some(len_us) = state.length_us
- && let Ok(len_i64) = i64::try_from(len_us)
- {
- dict.insert("mpris:length".to_owned(), OwnedValue::from(len_i64));
- }
+ if !state.enabled {
+ return map;
+ }
+ if let Ok(val) = OwnedValue::try_from(Value::from(Str::from(state.title.as_str()))) {
+ map.insert("xesam:title".to_owned(), val);
+ }
+ let artists: Vec<Str<'_>> =
+ if state.artist.trim().is_empty() { Vec::new() } else { vec![Str::from(state.artist.as_str())] };
+ if let Ok(val) = OwnedValue::try_from(Value::from(artists)) {
+ map.insert("xesam:artist".to_owned(), val);
+ }
+ if let Ok(val) = OwnedValue::try_from(Value::from(Str::from(state.album.as_str()))) {
+ map.insert("xesam:album".to_owned(), val);
+ }
+ if let Some(length_us) = state.length_us {
+ let len_i64 = i64::try_from(length_us).unwrap_or(i64::MAX);
+ map.insert("mpris:length".to_owned(), OwnedValue::from(len_i64));
}
- dict
+ if let Some(art_url) = &state.art_url
+ && let Ok(val) = OwnedValue::try_from(Value::from(Str::from(art_url.as_str())))
+ {
+ map.insert("mpris:artUrl".to_owned(), val);
+ }
+ map
+}
+
+pub(crate) fn build_mpris_player_properties(state: &MprisExportState) -> HashMap<String, OwnedValue> {
+ let mut props = HashMap::new();
+ if let Ok(val) = OwnedValue::try_from(Value::from(Str::from(state.playback_status.as_str()))) {
+ props.insert("PlaybackStatus".to_owned(), val);
+ }
+ if let Ok(meta_val) = OwnedValue::try_from(Value::from(build_mpris_metadata_map(state))) {
+ props.insert("Metadata".to_owned(), meta_val);
+ }
+ let pos_i64 = state.position_us.and_then(|u| i64::try_from(u).ok()).unwrap_or(0);
+ props.insert("Position".to_owned(), OwnedValue::from(pos_i64));
+ props.insert("CanPlay".to_owned(), OwnedValue::from(state.can_play));
+ props.insert("CanPause".to_owned(), OwnedValue::from(state.can_pause));
+ props.insert("CanGoNext".to_owned(), OwnedValue::from(state.can_go_next));
+ props.insert("CanGoPrevious".to_owned(), OwnedValue::from(state.can_go_previous));
+ props.insert("CanSeek".to_owned(), OwnedValue::from(state.can_seek));
+ props.insert("CanControl".to_owned(), OwnedValue::from(state.enabled));
+ props
}
-struct MprisRootIface;
+struct MprisRoot;
#[zbus::interface(name = "org.mpris.MediaPlayer2")]
-impl MprisRootIface {
+impl MprisRoot {
fn raise(&self) {}
fn quit(&self) {}
@@ -168,10 +198,10 @@ impl MprisRootIface {
}
}
-struct MprisPlayerIface;
+struct MprisPlayer;
#[zbus::interface(name = "org.mpris.MediaPlayer2.Player")]
-impl MprisPlayerIface {
+impl MprisPlayer {
fn next(&self) {
let _ = handle_mpris_call("Next", None);
}
@@ -189,7 +219,7 @@ impl MprisPlayerIface {
}
fn stop(&self) {
- let _ = handle_mpris_call("Pause", None);
+ let _ = handle_mpris_call("Stop", None);
}
fn play(&self) {
@@ -197,13 +227,11 @@ impl MprisPlayerIface {
}
fn seek(&self, offset: i64) {
- let current_us = current_export_state().position_us.and_then(|v| i64::try_from(v).ok()).unwrap_or(0);
- let target_us = current_us.saturating_add(offset).max(0);
- let _ = handle_mpris_call("Seek", Some(target_us));
+ let _ = handle_mpris_seek_offset(offset);
}
fn set_position(&self, _track_id: ObjectPath<'_>, position: i64) {
- let _ = handle_mpris_call("SetPosition", Some(position.max(0)));
+ let _ = handle_mpris_call("SetPosition", Some(position));
}
fn open_uri(&self, _uri: &str) {}
@@ -219,28 +247,28 @@ impl MprisPlayerIface {
}
#[zbus(property)]
- fn metadata(&self) -> HashMap<String, OwnedValue> {
- build_mpris_metadata_dict(&current_export_state())
+ fn minimum_rate(&self) -> f64 {
+ 1.0
}
#[zbus(property)]
- fn volume(&self) -> f64 {
+ fn maximum_rate(&self) -> f64 {
1.0
}
#[zbus(property)]
- fn position(&self) -> i64 {
- current_export_state().position_us.and_then(|v| i64::try_from(v).ok()).unwrap_or(0)
+ fn metadata(&self) -> HashMap<String, OwnedValue> {
+ build_mpris_metadata_map(&current_export_state())
}
#[zbus(property)]
- fn minimum_rate(&self) -> f64 {
+ fn volume(&self) -> f64 {
1.0
}
#[zbus(property)]
- fn maximum_rate(&self) -> f64 {
- 1.0
+ fn position(&self) -> i64 {
+ current_export_state().position_us.and_then(|u| i64::try_from(u).ok()).unwrap_or(0)
}
#[zbus(property)]
@@ -274,37 +302,19 @@ impl MprisPlayerIface {
}
}
-fn register_mpris_dbus_service() -> Option<zbus::blocking::Connection> {
- if cfg!(test) {
- return None;
- }
- zbus::blocking::connection::Builder::session()
- .ok()?
- .name("org.mpris.MediaPlayer2.nectarlink")
- .ok()?
- .serve_at("/org/mpris/MediaPlayer2", MprisRootIface)
- .ok()?
- .serve_at("/org/mpris/MediaPlayer2", MprisPlayerIface)
- .ok()?
- .build()
- .ok()
+fn init_mpris_dbus_service() -> Option<zbus::blocking::Connection> {
+ let builder = zbus::blocking::connection::Builder::session().ok()?;
+ let builder = builder.name("org.mpris.MediaPlayer2.nectarlink").ok()?;
+ let builder = builder.serve_at("/org/mpris/MediaPlayer2", MprisRoot).ok()?;
+ let builder = builder.serve_at("/org/mpris/MediaPlayer2", MprisPlayer).ok()?;
+ builder.build().ok()
}
-fn notify_mpris_properties_changed(conn: &zbus::blocking::Connection, state: &MprisExportState) {
- let mut changed: HashMap<&str, OwnedValue> = HashMap::new();
- changed.insert("PlaybackStatus", OwnedValue::from(Str::from(state.playback_status.as_str())));
- if let Ok(meta) = zbus::zvariant::Value::from(build_mpris_metadata_dict(state)).try_to_owned() {
- changed.insert("Metadata", meta);
- }
- changed.insert("CanPlay", OwnedValue::from(state.can_play));
- changed.insert("CanPause", OwnedValue::from(state.can_pause));
- changed.insert("CanGoNext", OwnedValue::from(state.can_go_next));
- changed.insert("CanGoPrevious", OwnedValue::from(state.can_go_previous));
- changed.insert("CanSeek", OwnedValue::from(state.can_seek));
- changed.insert("CanControl", OwnedValue::from(state.enabled));
+fn emit_mpris_properties_changed(conn: &zbus::blocking::Connection, state: &MprisExportState) {
+ let changed = build_mpris_player_properties(state);
let invalidated: Vec<&str> = Vec::new();
let _ = conn.emit_signal(
- Option::<&str>::None,
+ None::<&str>,
"/org/mpris/MediaPlayer2",
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
@@ -313,7 +323,7 @@ fn notify_mpris_properties_changed(conn: &zbus::blocking::Connection, state: &Mp
}
fn run_worker(receiver: mpsc::Receiver<Option<Shown>>) {
- let conn = register_mpris_dbus_service();
+ let mut dbus_conn = if cfg!(test) { None } else { init_mpris_dbus_service() };
while let Ok(first) = receiver.recv() {
let mut latest = first;
while let Ok(next) = receiver.try_recv() {
@@ -321,8 +331,13 @@ fn run_worker(receiver: mpsc::Receiver<Option<Shown>>) {
}
let export = build_export_state(latest.as_ref());
write_mpris_state_file(&export);
- if let Some(ref conn) = conn {
- notify_mpris_properties_changed(conn, &export);
+ if !cfg!(test) {
+ if dbus_conn.is_none() {
+ dbus_conn = init_mpris_dbus_service();
+ }
+ if let Some(conn) = &dbus_conn {
+ emit_mpris_properties_changed(conn, &export);
+ }
}
}
}
@@ -474,7 +489,7 @@ pub fn dispatch_command(action: MediaAction, position_ms: Option<u64>) -> bool {
pub fn handle_mpris_call(method: &str, position_us: Option<i64>) -> bool {
match method.trim() {
"Play" => dispatch_command(MediaAction::Play, None),
- "Pause" => dispatch_command(MediaAction::Pause, None),
+ "Pause" | "Stop" => dispatch_command(MediaAction::Pause, None),
"PlayPause" => {
let currently_playing = lock(&LAST_SHOWN).as_ref().is_some_and(|s| s.view.player.playing);
let action = if currently_playing { MediaAction::Pause } else { MediaAction::Play };
@@ -490,6 +505,19 @@ pub fn handle_mpris_call(method: &str, position_us: Option<i64>) -> bool {
}
}
+/// Handles a relative MPRIS2 `Seek(Offset)` call by adding `offset_us` to the
+/// currently exported playback position and dispatching `MediaAction::Seek`.
+pub fn handle_mpris_seek_offset(offset_us: i64) -> bool {
+ let export = current_export_state();
+ let cur_us = export.position_us.and_then(|u| i64::try_from(u).ok()).unwrap_or(0);
+ let mut target_us = cur_us.saturating_add(offset_us).max(0);
+ if let Some(len_us) = export.length_us.and_then(|u| i64::try_from(u).ok()) {
+ target_us = target_us.min(len_us);
+ }
+ let pos_ms = u64::try_from(target_us / 1000).unwrap_or(0);
+ dispatch_command(MediaAction::Seek, Some(pos_ms))
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -585,4 +613,20 @@ mod tests {
assert!(!cleared_state.enabled);
assert_eq!(cleared_state.playback_status, "Stopped");
}
+
+ #[test]
+ fn builds_mpris_metadata_and_player_properties_dictionaries() {
+ let shown = sample_shown(true);
+ let export = build_export_state(Some(&shown));
+ let meta = build_mpris_metadata_map(&export);
+ assert!(meta.contains_key("mpris:trackid"));
+ assert_eq!(meta.get("xesam:title").and_then(|v| <&str>::try_from(v).ok()), Some("Veridis Quo"));
+ assert_eq!(meta.get("xesam:album").and_then(|v| <&str>::try_from(v).ok()), Some("Discovery"));
+ assert_eq!(meta.get("mpris:length").and_then(|v| i64::try_from(v).ok()), Some(345_000_000));
+
+ let props = build_mpris_player_properties(&export);
+ assert_eq!(props.get("PlaybackStatus").and_then(|v| <&str>::try_from(v).ok()), Some("Playing"));
+ assert_eq!(props.get("CanPlay").and_then(|v| bool::try_from(v).ok()), Some(true));
+ assert_eq!(props.get("CanControl").and_then(|v| bool::try_from(v).ok()), Some(true));
+ }
}
diff --git a/desktop/app/src/linux/sound.rs b/desktop/app/src/linux/sound.rs
index 50457df..b34a8d0 100644
--- a/desktop/app/src/linux/sound.rs
+++ b/desktop/app/src/linux/sound.rs
@@ -6,11 +6,12 @@
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::Mutex;
-use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::thread;
use std::time::{Duration, Instant};
static RINGING: AtomicBool = AtomicBool::new(false);
+static GENERATION: AtomicU64 = AtomicU64::new(0);
static ACTIVE_CHILD: Mutex<Option<Child>> = Mutex::new(None);
const MAX_RING_DURATION: Duration = Duration::from_secs(60);
@@ -128,9 +129,12 @@ pub fn start_ringing() {
if RINGING.swap(true, Ordering::SeqCst) {
return;
}
- let _ = thread::Builder::new().name("nectarlink-alarm".into()).spawn(|| {
+ let my_gen = GENERATION.fetch_add(1, Ordering::SeqCst).wrapping_add(1);
+ let _ = thread::Builder::new().name("nectarlink-alarm".into()).spawn(move || {
let Some(wav_path) = alarm_wav_path() else {
- RINGING.store(false, Ordering::SeqCst);
+ if GENERATION.load(Ordering::SeqCst) == my_gen {
+ RINGING.store(false, Ordering::SeqCst);
+ }
return;
};
let wav_bytes = generate_alarm_wav(1000, 48_000);
@@ -150,19 +154,49 @@ pub fn start_ringing() {
}
let started = Instant::now();
- while RINGING.load(Ordering::SeqCst) && started.elapsed() < MAX_RING_DURATION {
- if let Some(child) = spawn_alarm_player(&wav_path) {
- if let Ok(mut guard) = ACTIVE_CHILD.lock() {
- *guard = Some(child);
+ while RINGING.load(Ordering::SeqCst)
+ && GENERATION.load(Ordering::SeqCst) == my_gen
+ && started.elapsed() < MAX_RING_DURATION
+ {
+ if let Some(mut child) = spawn_alarm_player(&wav_path) {
+ let stored = if let Ok(mut guard) = ACTIVE_CHILD.lock() {
+ if let Some(mut prev) = guard.take() {
+ let _ = prev.kill();
+ let _ = prev.wait();
+ }
+ if RINGING.load(Ordering::SeqCst) && GENERATION.load(Ordering::SeqCst) == my_gen {
+ *guard = Some(child);
+ true
+ } else {
+ let _ = child.kill();
+ let _ = child.wait();
+ false
+ }
+ } else {
+ let _ = child.kill();
+ let _ = child.wait();
+ false
+ };
+ if !stored {
+ break;
}
loop {
- if !RINGING.load(Ordering::SeqCst) || started.elapsed() >= MAX_RING_DURATION {
+ if !RINGING.load(Ordering::SeqCst)
+ || GENERATION.load(Ordering::SeqCst) != my_gen
+ || started.elapsed() >= MAX_RING_DURATION
+ {
kill_active_child();
break;
}
let finished = if let Ok(mut guard) = ACTIVE_CHILD.lock() {
if let Some(c) = guard.as_mut() {
- matches!(c.try_wait(), Ok(Some(_)) | Err(_))
+ match c.try_wait() {
+ Ok(Some(_)) | Err(_) => {
+ let _ = guard.take();
+ true
+ }
+ Ok(None) => false,
+ }
} else {
true
}
@@ -178,22 +212,45 @@ pub fn start_ringing() {
let step = Duration::from_millis(50);
let mut waited = Duration::ZERO;
- while waited < RING_PAUSE && RINGING.load(Ordering::SeqCst) {
+ while waited < RING_PAUSE
+ && RINGING.load(Ordering::SeqCst)
+ && GENERATION.load(Ordering::SeqCst) == my_gen
+ {
thread::sleep(step);
waited += step;
}
}
- kill_active_child();
- let _ = std::fs::remove_file(&wav_path);
- RINGING.store(false, Ordering::SeqCst);
+ if GENERATION.load(Ordering::SeqCst) == my_gen {
+ kill_active_child();
+ let _ = std::fs::remove_file(&wav_path);
+ RINGING.store(false, Ordering::SeqCst);
+ }
});
}
pub fn stop_ringing() {
RINGING.store(false, Ordering::SeqCst);
+ GENERATION.fetch_add(1, Ordering::SeqCst);
kill_active_child();
}
+/// Plays a short non-blocking notification chime on a background thread and
+/// reaps the player process so no zombie child is left behind.
+#[allow(dead_code)]
+pub fn play_chime() {
+ let _ = thread::Builder::new().name("nectarlink-chime".into()).spawn(|| {
+ if let Ok(mut child) = Command::new("canberra-gtk-play")
+ .args(["-i", "message-new-instant"])
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .spawn()
+ {
+ let _ = child.wait();
+ }
+ });
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/desktop/app/src/linux/vcam.rs b/desktop/app/src/linux/vcam.rs
index 7a03bbd..fb09c51 100644
--- a/desktop/app/src/linux/vcam.rs
+++ b/desktop/app/src/linux/vcam.rs
@@ -5,11 +5,11 @@
use std::{
fs::{self, OpenOptions},
io::Write,
- os::fd::AsRawFd,
+ os::{fd::AsRawFd, unix::fs::OpenOptionsExt},
path::{Path, PathBuf},
process::{Command, Stdio},
sync::{
- Arc,
+ Arc, OnceLock,
atomic::{AtomicBool, Ordering},
},
thread,
@@ -105,8 +105,38 @@ pub fn find_v4l2loopback_in_sysfs(sysfs_root: &Path, dev_root: &Path) -> Option<
None
}
+fn has_v4l2loopback_module_installed() -> bool {
+ static INSTALLED: OnceLock<bool> = OnceLock::new();
+ *INSTALLED.get_or_init(|| {
+ if let Ok(release) = fs::read_to_string("/proc/sys/kernel/osrelease") {
+ let rel = release.trim();
+ for root in ["/lib/modules", "/usr/lib/modules"] {
+ let dep = Path::new(root).join(rel).join("modules.dep");
+ if let Ok(contents) = fs::read_to_string(&dep)
+ && contents.contains("v4l2loopback")
+ {
+ return true;
+ }
+ }
+ }
+ for bin in ["/sbin/modinfo", "/usr/sbin/modinfo", "modinfo"] {
+ if let Ok(status) = Command::new(bin)
+ .arg("v4l2loopback")
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .status()
+ && status.success()
+ {
+ return true;
+ }
+ }
+ false
+ })
+}
+
/// Returns the active `v4l2loopback` device node, module sysfs directory, or `modprobe` binary
-/// so the desktop UI knows virtual camera setup is supported on this Linux host.
+/// when `v4l2loopback` is loaded or installed on this Linux host.
pub fn dll_path() -> Option<PathBuf> {
if let Some(dev) = find_v4l2loopback_device() {
return Some(dev);
@@ -115,6 +145,9 @@ pub fn dll_path() -> Option<PathBuf> {
if sys_mod.exists() {
return Some(sys_mod);
}
+ if !has_v4l2loopback_module_installed() {
+ return None;
+ }
for candidate in ["/sbin/modprobe", "/usr/sbin/modprobe", "/bin/modprobe", "/usr/bin/modprobe"] {
let p = PathBuf::from(candidate);
if p.exists() {
@@ -161,19 +194,28 @@ fn run_modprobe(args: &[&str]) -> Result<(), String> {
Err(last_err)
}
-/// Loads `v4l2loopback` with `"NectarLink Webcam"` and writes persistent module config files.
-pub fn register_hklm() -> Result<(), String> {
- if let Some(parent) = Path::new(MODULES_LOAD_CONF).parent() {
+fn write_config_nofollow(path: &Path, content: &[u8]) {
+ if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
- let _ = fs::write(MODULES_LOAD_CONF, "v4l2loopback\n");
-
- if let Some(parent) = Path::new(MODPROBE_CONF).parent() {
- let _ = fs::create_dir_all(parent);
+ if let Ok(mut f) = OpenOptions::new()
+ .write(true)
+ .create(true)
+ .truncate(true)
+ .custom_flags(libc::O_NOFOLLOW)
+ .mode(0o644)
+ .open(path)
+ {
+ let _ = f.write_all(content);
}
- let _ = fs::write(
- MODPROBE_CONF,
- format!("options v4l2loopback exclusive_caps=1 card_label=\"{FRIENDLY_NAME}\"\n"),
+}
+
+/// Loads `v4l2loopback` with `"NectarLink Webcam"` and writes persistent module config files.
+pub fn register_hklm() -> Result<(), String> {
+ write_config_nofollow(Path::new(MODULES_LOAD_CONF), b"v4l2loopback\n");
+ write_config_nofollow(
+ Path::new(MODPROBE_CONF),
+ format!("options v4l2loopback exclusive_caps=1 card_label=\"{FRIENDLY_NAME}\"\n").as_bytes(),
);
run_modprobe(&["v4l2loopback", "exclusive_caps=1", &format!("card_label={FRIENDLY_NAME}")])
@@ -321,8 +363,8 @@ fn v4l2_worker_loop(stop: &AtomicBool) {
while !stop.load(Ordering::Acquire) {
if mapping.is_none() {
- mapping = SharedFrameMapping::open_existing(DEFAULT_MAPPING_NAME)
- .or_else(|_| SharedFrameMapping::open_existing(LOCAL_MAPPING_NAME))
+ mapping = SharedFrameMapping::open_or_create(DEFAULT_MAPPING_NAME)
+ .or_else(|_| SharedFrameMapping::open_or_create(LOCAL_MAPPING_NAME))
.ok();
}
diff --git a/desktop/app/src/linux/vmic.rs b/desktop/app/src/linux/vmic.rs
index e7bdb6c..1a49a50 100644
--- a/desktop/app/src/linux/vmic.rs
+++ b/desktop/app/src/linux/vmic.rs
@@ -9,7 +9,6 @@ use std::io::{ErrorKind, Write};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::{DirBuilderExt, FileTypeExt, MetadataExt, OpenOptionsExt};
use std::path::{Path, PathBuf};
-use std::process::Command;
pub const SOURCE_NAME: &str = "NectarLink_Virtual_Mic";
pub const DEFAULT_DESCRIPTION: &str = "NectarLink Phone Microphone";
@@ -102,8 +101,10 @@ pub fn format_file_arg(fifo_path: &Path) -> Result<String, String> {
Ok(format!("file=\"{s}\""))
}
+const PACTL_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(800);
+
pub fn is_available() -> bool {
- Command::new("pactl").arg("info").output().map(|o| o.status.success()).unwrap_or(false)
+ super::run_cmd_timeout("pactl", &["info"], PACTL_TIMEOUT).is_some_and(|o| o.status.success())
}
/// Ensures that `fifo_path` exists on disk as a Unix FIFO (`0o600`) owned by the
@@ -210,7 +211,7 @@ pub fn parse_stale_pipe_source_modules(pactl_short: &str) -> Vec<u32> {
/// Unloads any leftover NectarLink `module-pipe-source` modules in
/// PulseAudio / PipeWire and removes any legacy `/tmp/nectarlink-vmic.pcm` FIFO.
pub fn cleanup_stale_sources() {
- if let Ok(output) = Command::new("pactl").args(["list", "modules", "short"]).output()
+ if let Some(output) = super::run_cmd_timeout("pactl", &["list", "modules", "short"], PACTL_TIMEOUT)
&& output.status.success()
{
let stdout = String::from_utf8_lossy(&output.stdout);
@@ -244,20 +245,25 @@ pub fn create_source_with_label(
let _ = ensure_fifo(fifo_path);
let clean_channels = channels.clamp(1, 2);
let source_props = format_source_properties_with_channels(phone_name, clean_channels);
- let output = Command::new("pactl")
- .args([
+ let source_name_arg = format!("source_name={SOURCE_NAME}");
+ let rate_arg = format!("rate={}", rate.max(8_000));
+ let channels_arg = format!("channels={clean_channels}");
+ let output = super::run_cmd_timeout(
+ "pactl",
+ &[
"load-module",
"module-pipe-source",
- &format!("source_name={SOURCE_NAME}"),
+ &source_name_arg,
&file_arg,
"format=s16le",
- &format!("rate={}", rate.max(8_000)),
- &format!("channels={clean_channels}"),
+ &rate_arg,
+ &channels_arg,
format_channel_map(clean_channels),
&source_props,
- ])
- .output()
- .map_err(|e| format!("failed to run pactl: {e}"))?;
+ ],
+ PACTL_TIMEOUT,
+ )
+ .ok_or_else(|| "pactl timed out or failed to start".to_string())?;
if !output.status.success() {
return Err(String::from_utf8_lossy(&output.stderr).trim().to_string());
}
@@ -267,14 +273,15 @@ pub fn create_source_with_label(
.map_err(|e| format!("invalid pactl module id: {e}"))?;
// Ensure the virtual source is unmuted and at 100% (0 dB) unity gain in PipeWire/PulseAudio.
- let _ = Command::new("pactl").args(["set-source-mute", SOURCE_NAME, "0"]).status();
- let _ = Command::new("pactl").args(["set-source-volume", SOURCE_NAME, "100%"]).status();
+ let _ = super::run_cmd_timeout("pactl", &["set-source-mute", SOURCE_NAME, "0"], PACTL_TIMEOUT);
+ let _ = super::run_cmd_timeout("pactl", &["set-source-volume", SOURCE_NAME, "100%"], PACTL_TIMEOUT);
Ok(module_id)
}
pub fn destroy_source(module_id: u32) {
- let _ = Command::new("pactl").args(["unload-module", &module_id.to_string()]).status();
+ let id_str = module_id.to_string();
+ let _ = super::run_cmd_timeout("pactl", &["unload-module", &id_str], PACTL_TIMEOUT);
}
/// Default makeup pre-gain (in dB) applied to phone microphone streams on Linux
@@ -381,12 +388,42 @@ impl VirtualMic {
module_id,
input_channels: in_ch,
output_channels: out_ch,
- gain_db: DEFAULT_PHONE_MAKEUP_GAIN_DB,
+ gain_db: 0.0,
muted: false,
last_rms: 0.0,
}
}
+ /// Creates a new `VirtualMic` controller labeled with `phone_name`, failing
+ /// honestly when `pactl load-module module-pipe-source` does not succeed.
+ pub fn open_with_label(
+ rate: u32,
+ input_channels: u8,
+ output_channels: u8,
+ phone_name: Option<&str>,
+ ) -> Result<Self, String> {
+ let in_ch = input_channels.clamp(1, 2);
+ let out_ch = output_channels.clamp(1, 2);
+ let fifo_path = default_fifo_path();
+ ensure_fifo(&fifo_path)?;
+ let fifo_file = open_fifo_nonblocking(&fifo_path)
+ .map_err(|e| format!("open FIFO {}: {e}", fifo_path.display()))?;
+ let module_id = create_source_with_label(rate, out_ch, &fifo_path, phone_name).ok();
+ if !cfg!(test) && module_id.is_none() {
+ return Err("failed to create PipeWire/PulseAudio virtual microphone source".into());
+ }
+ Ok(Self {
+ fifo_path,
+ fifo_file: Some(fifo_file),
+ module_id,
+ input_channels: in_ch,
+ output_channels: out_ch,
+ gain_db: 0.0,
+ muted: false,
+ last_rms: 0.0,
+ })
+ }
+
/// Creates a `VirtualMic` backed by a specific FIFO path (creating the FIFO if needed)
/// without invoking `pactl`.
pub fn open_fifo_only(fifo_path: impl Into<PathBuf>) -> Result<Self, String> {
@@ -464,10 +501,35 @@ impl VirtualMic {
}
}
- /// Processes incoming `s16le` PCM frames (applying `gain_db` and upmixing mono to
- /// stereo when `input_channels == 1 && output_channels == 2`) and writes them to
- /// the pipe FIFO if the virtual source or FIFO handle is active.
+ /// Processes incoming `s16le` PCM frames (applying `gain_db` when non-zero and
+ /// upmixing mono to stereo when `input_channels == 1 && output_channels == 2`)
+ /// and writes them to the pipe FIFO if the virtual source or FIFO handle is active.
pub fn push_pcm(&mut self, pcm: &[u8]) -> Result<f32, String> {
+ if !self.muted && self.gain_db.abs() < f32::EPSILON {
+ let sample_count = pcm.len() / 2;
+ let rms = if sample_count == 0 {
+ 0.0
+ } else {
+ let mut sum_sq = 0.0_f64;
+ for chunk in pcm.as_chunks::<2>().0 {
+ let norm = f64::from(i16::from_le_bytes([chunk[0], chunk[1]])) / 32768.0;
+ sum_sq += norm * norm;
+ }
+ ((sum_sq / (sample_count as f64)).sqrt() as f32).clamp(0.0, 1.0)
+ };
+ self.last_rms = rms;
+ if (self.module_id.is_some() || self.fifo_file.is_some()) && sample_count > 0 {
+ let aligned = &pcm[..sample_count * 2];
+ if self.input_channels == 1 && self.output_channels == 2 {
+ let stereo = mono_to_stereo_s16le(aligned);
+ let _ = self.write_pcm_s16le(&stereo);
+ } else {
+ let _ = self.write_pcm_s16le(aligned);
+ }
+ }
+ return Ok(rms);
+ }
+
let (processed, rms) = process_mic_pcm_s16le(pcm, self.gain_db, self.muted);
self.last_rms = rms;
if (self.module_id.is_some() || self.fifo_file.is_some()) && !processed.is_empty() {
diff --git a/desktop/app/src/media_file.rs b/desktop/app/src/media_file.rs
index c5a0ae8..fbf5a01 100644
--- a/desktop/app/src/media_file.rs
+++ b/desktop/app/src/media_file.rs
@@ -64,18 +64,277 @@ pub(crate) fn pick<'a>(titles: &[&str], recent: &'a [String]) -> Option<&'a Stri
best.map(|(_, name)| name)
}
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
+fn percent_decode_str(input: &str) -> String {
+ let unescaped =
+ input.replace("&amp;", "&").replace("&apos;", "'").replace("&#39;", "'").replace("&quot;", "\"");
+ let bytes = unescaped.as_bytes();
+ let mut out = Vec::with_capacity(bytes.len());
+ let mut i = 0;
+ while i < bytes.len() {
+ if bytes[i] == b'%'
+ && i + 2 < bytes.len()
+ && let (Some(hi), Some(lo)) =
+ ((bytes[i + 1] as char).to_digit(16), (bytes[i + 2] as char).to_digit(16))
+ {
+ out.push(((hi << 4) | lo) as u8);
+ i += 3;
+ continue;
+ }
+ out.push(bytes[i]);
+ i += 1;
+ }
+ String::from_utf8_lossy(&out).into_owned()
+}
+
+/// Resolves a `file://` URI (with percent-encoding and optional XML entity escaping)
+/// into a local filesystem [`PathBuf`].
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
+pub(crate) fn decode_file_uri(uri: &str) -> Option<PathBuf> {
+ let trimmed = uri.trim().trim_matches('\'').trim_matches('"').trim();
+ let after_scheme = trimmed.strip_prefix("file://")?;
+ let path_part = after_scheme
+ .strip_prefix("localhost/")
+ .map(|r| format!("/{r}"))
+ .unwrap_or_else(|| after_scheme.to_owned());
+ let decoded = percent_decode_str(&path_part);
+ if decoded.trim().is_empty() { None } else { Some(PathBuf::from(decoded)) }
+}
+
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
+fn extract_xml_attr<'a>(tag: &'a str, attr: &str) -> Option<&'a str> {
+ for quote in ['"', '\''] {
+ let pattern = format!("{attr}={quote}");
+ if let Some(start) = tag.find(&pattern) {
+ let rest = &tag[start + pattern.len()..];
+ if let Some(end) = rest.find(quote) {
+ return Some(&rest[..end]);
+ }
+ }
+ }
+ None
+}
+
+/// Parses Freedesktop `recently-used.xbel` XML content and returns playable video
+/// file paths ordered from most recently visited/modified to oldest.
+#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
+pub(crate) fn parse_xbel_recent_files(xml: &str) -> Vec<PathBuf> {
+ let mut entries: Vec<(String, usize, PathBuf)> = Vec::new();
+ for (idx, chunk) in xml.split("<bookmark").skip(1).enumerate() {
+ let Some(tag_end) = chunk.find('>') else {
+ continue;
+ };
+ let tag = &chunk[..tag_end];
+ let Some(href) = extract_xml_attr(tag, "href") else {
+ continue;
+ };
+ let Some(path) = decode_file_uri(href) else {
+ continue;
+ };
+ let is_playable = path.file_name().and_then(|n| n.to_str()).is_some_and(playable);
+ if !is_playable {
+ continue;
+ }
+ let timestamp = extract_xml_attr(tag, "visited")
+ .or_else(|| extract_xml_attr(tag, "modified"))
+ .or_else(|| extract_xml_attr(tag, "added"))
+ .unwrap_or("")
+ .to_owned();
+ entries.push((timestamp, idx, path));
+ }
+ entries.sort_by(|a, b| b.0.cmp(&a.0).then_with(|| b.1.cmp(&a.1)));
+ let mut out = Vec::with_capacity(entries.len().min(RECENT));
+ for (_, _, path) in entries {
+ if !out.contains(&path) {
+ out.push(path);
+ if out.len() >= RECENT {
+ break;
+ }
+ }
+ }
+ out
+}
+
+#[cfg(target_os = "linux")]
+fn mpris_xesam_url_for(player: &str, title: &str) -> Option<PathBuf> {
+ use std::collections::HashMap;
+ use zbus::zvariant::OwnedValue;
+
+ let conn = zbus::blocking::Connection::session().ok()?;
+ let reply = conn
+ .call_method(
+ Some("org.freedesktop.DBus"),
+ "/org/freedesktop/DBus",
+ Some("org.freedesktop.DBus"),
+ "ListNames",
+ &(),
+ )
+ .ok()?;
+ let bus_names: Vec<String> = reply
+ .body()
+ .deserialize::<Vec<String>>()
+ .ok()
+ .or_else(|| reply.body().deserialize::<(Vec<String>,)>().ok().map(|(v,)| v))?;
+
+ let player_lower = player.trim().to_ascii_lowercase();
+ for bus in bus_names {
+ let Some(short) = bus.strip_prefix("org.mpris.MediaPlayer2.") else {
+ continue;
+ };
+ let short_lower = short.to_ascii_lowercase();
+ if !player_lower.is_empty()
+ && !short_lower.contains(&player_lower)
+ && !player_lower.contains(&short_lower)
+ {
+ continue;
+ }
+ let Ok(msg) = conn.call_method(
+ Some(bus.as_str()),
+ "/org/mpris/MediaPlayer2",
+ Some("org.freedesktop.DBus.Properties"),
+ "Get",
+ &("org.mpris.MediaPlayer2.Player", "Metadata"),
+ ) else {
+ continue;
+ };
+ let meta_val = msg
+ .body()
+ .deserialize::<OwnedValue>()
+ .ok()
+ .or_else(|| msg.body().deserialize::<(OwnedValue,)>().ok().map(|(v,)| v));
+ let Some(meta_dict) = meta_val
+ .and_then(|v| v.try_clone().ok())
+ .and_then(|owned| HashMap::<String, OwnedValue>::try_from(owned).ok())
+ else {
+ continue;
+ };
+ let Some(url_str) = meta_dict.get("xesam:url").and_then(|v| <&str>::try_from(v).ok()) else {
+ continue;
+ };
+ let Some(path) = decode_file_uri(url_str) else {
+ continue;
+ };
+ if path.is_file() && path.file_name().and_then(|n| n.to_str()).is_some_and(playable) {
+ if title.trim().is_empty() {
+ return Some(path);
+ }
+ if let Some(fname) = path.file_name().and_then(|n| n.to_str())
+ && names(title, fname) > 0
+ {
+ return Some(path);
+ }
+ return Some(path);
+ }
+ }
+ None
+}
+
+#[cfg(target_os = "linux")]
+fn proc_open_playable_files(programs: &[String]) -> Vec<PathBuf> {
+ let mut out = Vec::new();
+ let Ok(proc_dir) = std::fs::read_dir("/proc") else {
+ return out;
+ };
+ for entry in proc_dir.flatten() {
+ let pid_os = entry.file_name();
+ let Some(pid_str) = pid_os.to_str() else {
+ continue;
+ };
+ if !pid_str.chars().all(|c| c.is_ascii_digit()) {
+ continue;
+ }
+ let comm_path = entry.path().join("comm");
+ let comm = std::fs::read_to_string(&comm_path).unwrap_or_default().trim().to_ascii_lowercase();
+ if comm.is_empty() || !programs.iter().any(|p| p.eq_ignore_ascii_case(&comm) || comm.contains(p)) {
+ continue;
+ }
+ let Ok(fd_dir) = std::fs::read_dir(entry.path().join("fd")) else {
+ continue;
+ };
+ for fd_entry in fd_dir.flatten() {
+ if let Ok(target) = std::fs::read_link(fd_entry.path())
+ && target.is_file()
+ && target.file_name().and_then(|n| n.to_str()).is_some_and(playable)
+ && !out.contains(&target)
+ {
+ out.push(target);
+ }
+ }
+ }
+ out
+}
+
+#[cfg(target_os = "linux")]
+fn xbel_recent_playable_files() -> Vec<PathBuf> {
+ let mut candidates = Vec::new();
+ if let Some(data_dir) = dirs::data_local_dir() {
+ candidates.push(data_dir.join("recently-used.xbel"));
+ }
+ if let Some(home) = dirs::home_dir() {
+ let fallback = home.join(".local/share/recently-used.xbel");
+ if !candidates.contains(&fallback) {
+ candidates.push(fallback);
+ }
+ }
+ for xbel_path in candidates {
+ if let Ok(xml) = std::fs::read_to_string(&xbel_path) {
+ let parsed = parse_xbel_recent_files(&xml);
+ if !parsed.is_empty() {
+ return parsed;
+ }
+ }
+ }
+ Vec::new()
+}
+
/// The video file this PC's `player` plays (`title`, as its media session
-/// says), if it is one a phone plays and Windows lists it as opened lately.
+/// says), if it is one a phone plays and the system lists it as opened lately.
pub fn file_for(player: &str, title: &str) -> Option<PathBuf> {
let programs = browser::programs_for(player);
if programs.iter().any(|p| browser::is_browser(p)) {
return None;
}
+
+ #[cfg(target_os = "linux")]
+ if let Some(path) = mpris_xesam_url_for(player, title) {
+ return Some(path);
+ }
+
let windows: Vec<String> =
browser::windows().into_iter().filter(|w| programs.contains(&w.program)).map(|w| w.title).collect();
let mut titles: Vec<&str> = windows.iter().map(String::as_str).collect();
titles.push(title);
+ #[cfg(target_os = "linux")]
+ {
+ let open_files = proc_open_playable_files(&programs);
+ let open_names: Vec<String> = open_files
+ .iter()
+ .filter_map(|p| p.file_name().and_then(|s| s.to_str()).map(str::to_owned))
+ .collect();
+ if let Some(chosen) = pick(&titles, &open_names)
+ && let Some(found) = open_files
+ .into_iter()
+ .find(|p| p.file_name().and_then(|s| s.to_str()) == Some(chosen.as_str()))
+ {
+ return Some(found);
+ }
+
+ let xbel_files: Vec<PathBuf> =
+ xbel_recent_playable_files().into_iter().filter(|p| p.is_file()).collect();
+ let xbel_names: Vec<String> = xbel_files
+ .iter()
+ .filter_map(|p| p.file_name().and_then(|s| s.to_str()).map(str::to_owned))
+ .collect();
+ if let Some(chosen) = pick(&titles, &xbel_names)
+ && let Some(found) = xbel_files
+ .into_iter()
+ .find(|p| p.file_name().and_then(|s| s.to_str()) == Some(chosen.as_str()))
+ {
+ return Some(found);
+ }
+ }
+
let dir = shortcut::recent_dir()?;
let mut links: Vec<(std::time::SystemTime, PathBuf)> = std::fs::read_dir(&dir)
.ok()?
@@ -126,4 +385,27 @@ mod tests {
let titles = ["", "Holiday 2024 extras.mp4 - VLC media player"];
assert_eq!(pick(&titles, &recent).map(String::as_str), Some("Holiday 2024 extras.mp4"));
}
+
+ #[test]
+ fn parses_xbel_recent_files_and_decodes_file_uris() {
+ assert_eq!(
+ decode_file_uri("file:///home/user/Videos/Director&apos;s%20Cut%20(2026).mkv"),
+ Some(PathBuf::from("/home/user/Videos/Director's Cut (2026).mkv"))
+ );
+
+ let xbel = r#"<?xml version="1.0" encoding="UTF-8"?>
+<xbel version="1.0">
+ <bookmark href="file:///home/user/Documents/notes.txt" visited="2026-10-08T10:00:00Z"/>
+ <bookmark href="file:///home/user/Videos/Older%20Clip.mp4" visited="2026-10-07T09:00:00Z"/>
+ <bookmark href="file:///home/user/Videos/Newest%20Movie%202026.mkv" visited="2026-10-08T12:30:00Z"/>
+</xbel>"#;
+ let files = parse_xbel_recent_files(xbel);
+ assert_eq!(
+ files,
+ vec![
+ PathBuf::from("/home/user/Videos/Newest Movie 2026.mkv"),
+ PathBuf::from("/home/user/Videos/Older Clip.mp4"),
+ ]
+ );
+ }
}
diff --git a/desktop/app/src/mirror.rs b/desktop/app/src/mirror.rs
index df3c6c0..5e49654 100644
--- a/desktop/app/src/mirror.rs
+++ b/desktop/app/src/mirror.rs
@@ -435,7 +435,8 @@ pub fn screenshot_file(window: Window) {
let base_name = local_timestamp_filename("Screenshot", "png");
let dir = core_host::downloads_dir();
if let Err(e) = std::fs::create_dir_all(&dir) {
- tracing::warn!(error = %e, "can't create Downloads\\Nectarlink");
+ let subdir = if cfg!(windows) { "Downloads\\Nectarlink" } else { "Downloads/Nectarlink" };
+ tracing::warn!(error = %e, "can't create {subdir}");
set_notice(window, "Couldn't save screenshot");
show_message(tr!("Mirror", "The screenshot couldn't be saved."));
return;
@@ -463,11 +464,16 @@ pub fn screenshot_file(window: Window) {
}
let device_name =
core_host::host().hub.read(|s| s.name_of(&window.device)).unwrap_or_else(|| "your phone".into());
+ let toast_body = if cfg!(windows) {
+ tr!("Mirror", "Screenshot from %1, saved in Downloads\\Nectarlink", device_name)
+ } else {
+ tr!("Mirror", "Screenshot from %1, saved in Downloads/Nectarlink", device_name)
+ };
toast::show(Toast {
device: TOAST_GROUP.into(),
key: path.to_string_lossy().into_owned(),
title: file_name.clone(),
- body: tr!("Mirror", "Screenshot from %1, saved in Downloads\\Nectarlink", device_name),
+ body: toast_body,
attribution: "NectarLink".into(),
icon: None,
image: Some(path),
@@ -478,7 +484,12 @@ pub fn screenshot_file(window: Window) {
call: false,
});
set_notice(window, tr!("Mirror", "Saved %1", file_name));
- show_message(tr!("Mirror", "Saved %1 to Downloads\\Nectarlink.", file_name));
+ let saved_msg = if cfg!(windows) {
+ tr!("Mirror", "Saved %1 to Downloads\\Nectarlink.", file_name)
+ } else {
+ tr!("Mirror", "Saved %1 to Downloads/Nectarlink.", file_name)
+ };
+ show_message(saved_msg);
}
/// Whether `window` is currently recording to an MP4 file.
@@ -518,7 +529,8 @@ pub fn toggle_recording(window: Window) {
let base_name = local_timestamp_filename("Mirror", "mp4");
let dir = core_host::downloads_dir();
if let Err(e) = std::fs::create_dir_all(&dir) {
- tracing::warn!(error = %e, "can't create Downloads\\Nectarlink");
+ let subdir = if cfg!(windows) { "Downloads\\Nectarlink" } else { "Downloads/Nectarlink" };
+ tracing::warn!(error = %e, "can't create {subdir}");
set_notice(window, "Couldn't start recording");
show_message(tr!("Mirror", "Couldn't start recording."));
return;
@@ -605,11 +617,16 @@ pub fn stop_recording(window: Window) {
.hub
.read(|s| s.name_of(&window.device))
.unwrap_or_else(|| "your phone".into());
+ let toast_body = if cfg!(windows) {
+ tr!("Mirror", "Recording from %1, saved in Downloads\\Nectarlink", device_name)
+ } else {
+ tr!("Mirror", "Recording from %1, saved in Downloads/Nectarlink", device_name)
+ };
toast::show(Toast {
device: TOAST_GROUP.into(),
key: path.to_string_lossy().into_owned(),
title: file_name.clone(),
- body: tr!("Mirror", "Recording from %1, saved in Downloads\\Nectarlink", device_name),
+ body: toast_body,
attribution: "NectarLink".into(),
icon: None,
image: None,
@@ -623,7 +640,12 @@ pub fn stop_recording(window: Window) {
call: false,
});
set_notice(window, tr!("Mirror", "Saved %1", file_name));
- show_message(tr!("Mirror", "Saved %1 to Downloads\\Nectarlink.", file_name));
+ let saved_msg = if cfg!(windows) {
+ tr!("Mirror", "Saved %1 to Downloads\\Nectarlink.", file_name)
+ } else {
+ tr!("Mirror", "Saved %1 to Downloads/Nectarlink.", file_name)
+ };
+ show_message(saved_msg);
}
Ok(Err(msg)) => {
set_notice(window, msg.clone());
@@ -1295,10 +1317,14 @@ pub(crate) fn parse_dropped_file_urls(raw_urls: &[String]) -> (Option<Window>, V
.map(|p| String::from(&p))
.filter(|p| !p.is_empty())
.or_else(|| {
- trimmed
- .strip_prefix("file:///")
- .or_else(|| trimmed.strip_prefix("file://"))
- .map(|s| s.replace('/', "\\"))
+ if cfg!(windows) {
+ trimmed
+ .strip_prefix("file:///")
+ .or_else(|| trimmed.strip_prefix("file://"))
+ .map(|s| s.replace('/', "\\"))
+ } else {
+ trimmed.strip_prefix("file://").map(str::to_owned)
+ }
});
let pb = PathBuf::from(local.as_deref().unwrap_or(trimmed));
if pb.exists() {
diff --git a/desktop/app/src/pc_audio_stream.rs b/desktop/app/src/pc_audio_stream.rs
index 5e16422..5c1210e 100644
--- a/desktop/app/src/pc_audio_stream.rs
+++ b/desktop/app/src/pc_audio_stream.rs
@@ -233,6 +233,30 @@ mod imp {
if trimmed.ends_with(".monitor") { trimmed.to_owned() } else { format!("{trimmed}.monitor") }
}
+ /// Builds `pw-record` CLI arguments that capture desktop sink monitor loopback
+ /// (`-P stream.capture.sink=true`) rather than the default microphone input.
+ #[cfg_attr(windows, allow(dead_code))]
+ pub(crate) fn pw_record_loopback_args(monitor: &str) -> Vec<String> {
+ let mut args = vec![
+ "--raw".to_owned(),
+ "--format=f32".to_owned(),
+ "--rate=48000".to_owned(),
+ "--channels=2".to_owned(),
+ "-P".to_owned(),
+ "stream.capture.sink=true".to_owned(),
+ ];
+ let trimmed = monitor.trim();
+ if !trimmed.is_empty() && trimmed != "@DEFAULT_MONITOR@" {
+ let sink_name = trimmed.strip_suffix(".monitor").unwrap_or(trimmed).trim();
+ if !sink_name.is_empty() && !sink_name.starts_with('-') {
+ args.push("--target".to_owned());
+ args.push(sink_name.to_owned());
+ }
+ }
+ args.push("-".to_owned());
+ args
+ }
+
#[cfg(target_os = "linux")]
fn resolve_default_monitor_source() -> String {
let default_sink = std::process::Command::new("pactl")
@@ -249,7 +273,12 @@ mod imp {
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));
+ for _ in 0..30 {
+ if !is_enabled() {
+ break;
+ }
+ std::thread::sleep(Duration::from_millis(100));
+ }
}
}
}
@@ -290,11 +319,8 @@ mod imp {
.stderr(Stdio::null())
.spawn()
.or_else(|_| {
- Command::new("pw-record")
- .args(["--raw", "--format=f32", "--rate=48000", "--channels=2", "-"])
- .stdout(Stdio::piped())
- .stderr(Stdio::null())
- .spawn()
+ let pw_args = pw_record_loopback_args(&monitor);
+ Command::new("pw-record").args(&pw_args).stdout(Stdio::piped()).stderr(Stdio::null()).spawn()
})
.map_err(|e| format!("failed to spawn parec/pw-record: {e}"))?;
@@ -324,6 +350,9 @@ mod imp {
let mut silence_tail_packets: u32 = 0;
while is_enabled() {
+ if target() != wanted && target().is_some_and(|t| peers.contains(&t)) {
+ break;
+ }
if stdout.read_exact(&mut raw_buf).is_err() {
break;
}
@@ -550,6 +579,20 @@ mod imp {
);
assert_eq!(format_monitor_source_name(Some(" ")), "@DEFAULT_MONITOR@");
assert_eq!(format_monitor_source_name(None), "@DEFAULT_MONITOR@");
+
+ let default_pw = pw_record_loopback_args("@DEFAULT_MONITOR@");
+ assert!(default_pw.windows(2).any(|w| w == ["-P", "stream.capture.sink=true"]));
+ assert!(!default_pw.iter().any(|a| a == "--target"));
+ assert_eq!(default_pw.last().map(String::as_str), Some("-"));
+
+ let targeted_pw = pw_record_loopback_args("alsa_output.pci-0000_00_1f.3.analog-stereo.monitor");
+ assert!(targeted_pw.windows(2).any(|w| w == ["-P", "stream.capture.sink=true"]));
+ assert!(
+ targeted_pw
+ .windows(2)
+ .any(|w| w == ["--target", "alsa_output.pci-0000_00_1f.3.analog-stereo"])
+ );
+ assert_eq!(targeted_pw.last().map(String::as_str), Some("-"));
}
#[test]
diff --git a/desktop/app/src/pc_mirror.rs b/desktop/app/src/pc_mirror.rs
index da0b4a6..c3653c6 100644
--- a/desktop/app/src/pc_mirror.rs
+++ b/desktop/app/src/pc_mirror.rs
@@ -94,8 +94,8 @@ mod imp {
/// Whether this PC's display session supports screen capture for `mirror.capture`.
///
- /// On Linux, `pc_mirror` captures the screen via X11 (`XGetImage`), which requires
- /// an X11 or XWayland `DISPLAY`.
+ /// On Linux, `pc_mirror` captures the screen via X11 (`XGetImage`) or Wayland
+ /// (`org.freedesktop.portal.ScreenCast` + PipeWire) and encodes via `libopenh264`.
pub fn is_capture_available() -> bool {
#[cfg(windows)]
{
@@ -103,7 +103,11 @@ mod imp {
}
#[cfg(target_os = "linux")]
{
- std::env::var_os("DISPLAY").is_some()
+ if openh264_enc_api().is_none() {
+ return false;
+ }
+ let has_x11 = std::env::var_os("DISPLAY").is_some() && x11_api().is_some();
+ has_x11 || is_wayland_screencast_available()
}
}
@@ -609,6 +613,24 @@ mod imp {
i_pic_width: std::ffi::c_int,
i_pic_height: std::ffi::c_int,
ui_time_stamp: i64,
+ b_psnr_y: bool,
+ b_psnr_u: bool,
+ b_psnr_v: bool,
+ }
+
+ #[cfg(target_os = "linux")]
+ #[repr(C)]
+ #[derive(Clone, Copy)]
+ struct SLayerBSInfoV20 {
+ ui_temporal_id: u8,
+ ui_spatial_id: u8,
+ ui_quality_id: u8,
+ e_frame_type: std::ffi::c_int,
+ ui_layer_type: u8,
+ i_sub_seq_id: std::ffi::c_int,
+ i_nal_count: std::ffi::c_int,
+ p_nal_length_in_byte: *mut std::ffi::c_int,
+ p_bs_buf: *mut u8,
}
#[cfg(target_os = "linux")]
@@ -624,10 +646,23 @@ mod imp {
i_nal_count: std::ffi::c_int,
p_nal_length_in_byte: *mut std::ffi::c_int,
p_bs_buf: *mut u8,
+ r_psnr: [f32; 3],
+ }
+
+ #[cfg(target_os = "linux")]
+ #[repr(C)]
+ #[derive(Clone, Copy)]
+ struct SFrameBSInfoV20 {
+ i_layer_num: std::ffi::c_int,
+ s_layer_info: [SLayerBSInfoV20; 128],
+ e_frame_type: std::ffi::c_int, // 1 = IDR, 2 = I, 3 = P, 4 = Skip
+ i_frame_size_in_bytes: std::ffi::c_int,
+ ui_time_stamp: i64,
}
#[cfg(target_os = "linux")]
#[repr(C)]
+ #[derive(Clone, Copy)]
struct SFrameBSInfo {
i_layer_num: std::ffi::c_int,
s_layer_info: [SLayerBSInfo; 128],
@@ -636,6 +671,18 @@ mod imp {
ui_time_stamp: i64,
}
+ /// Upstream OpenH264 2.3+ adds `r_psnr: [f32; 3]` to `SLayerBSInfo`, whereas
+ /// Debian/Ubuntu's `libopenh264.so.7` (2.4.1) preserves the 40-byte `so.7`
+ /// `SLayerBSInfoV20` layout despite reporting `u_minor == 4`. Zero-initializing
+ /// this union before `EncodeFrame` lets us inspect whether `v23.e_frame_type`
+ /// (at byte 7176, past the end of `SFrameBSInfoV20`) was populated.
+ #[cfg(target_os = "linux")]
+ #[repr(C)]
+ union FrameBSInfo {
+ v20: SFrameBSInfoV20,
+ v23: SFrameBSInfo,
+ }
+
#[cfg(target_os = "linux")]
#[repr(C)]
struct SBitrateInfo {
@@ -654,6 +701,7 @@ mod imp {
#[cfg(target_os = "linux")]
struct OpenH264EncApi {
+ minor: u32,
create_encoder: unsafe extern "C" fn(*mut *mut std::ffi::c_void) -> std::ffi::c_int,
destroy_encoder: unsafe extern "C" fn(*mut std::ffi::c_void),
}
@@ -668,7 +716,8 @@ mod imp {
fn openh264_enc_api() -> Option<&'static OpenH264EncApi> {
static API: std::sync::OnceLock<Option<OpenH264EncApi>> = std::sync::OnceLock::new();
API.get_or_init(|| unsafe {
- let candidates = [c"libopenh264.so.7", c"libopenh264.so.6", c"libopenh264.so"];
+ let candidates =
+ [c"libopenh264.so.8", c"libopenh264.so.7", c"libopenh264.so.6", c"libopenh264.so"];
let mut lib = std::ptr::null_mut();
for name in candidates {
lib = libc::dlopen(name.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
@@ -690,6 +739,7 @@ mod imp {
return None;
}
Some(OpenH264EncApi {
+ minor: ver.u_minor,
create_encoder: dl_sym(lib, c"WelsCreateSVCEncoder")?,
destroy_encoder: dl_sym(lib, c"WelsDestroySVCEncoder")?,
})
@@ -861,6 +911,7 @@ mod imp {
}
unsafe {
+ let api = openh264_enc_api().ok_or_else(|| "libopenh264 encoder unavailable".to_owned())?;
let y_ptr = self.i420_buf.as_mut_ptr();
let u_ptr = y_ptr.add(y_size);
let v_ptr = y_ptr.add(y_size + quarter);
@@ -876,53 +927,94 @@ mod imp {
i_pic_width: w as std::ffi::c_int,
i_pic_height: h as std::ffi::c_int,
ui_time_stamp: hns / 10_000,
+ b_psnr_y: false,
+ b_psnr_u: false,
+ b_psnr_v: false,
};
- let mut bs_info: SFrameBSInfo = std::mem::zeroed();
let vtable = *(self.encoder as *const *const usize);
+ let mut raw = Vec::new();
+ let e_frame_type: std::ffi::c_int;
+
+ let mut bs_union: FrameBSInfo = std::mem::zeroed();
let encode_frame: unsafe extern "C" fn(
*mut std::ffi::c_void,
*const SSourcePicture,
- *mut SFrameBSInfo,
+ *mut FrameBSInfo,
) -> std::ffi::c_int = std::mem::transmute(*vtable.add(4));
- let rc = encode_frame(self.encoder, &pic, &mut bs_info);
+ let rc = encode_frame(self.encoder, &pic, &mut bs_union);
if rc != 0 {
return Err(format!("OpenH264 EncodeFrame failed: {rc}"));
}
- if bs_info.e_frame_type == 4 || bs_info.i_layer_num <= 0 {
- return Ok(Vec::new());
- }
- let layer_count = (bs_info.i_layer_num as usize).min(bs_info.s_layer_info.len());
- let mut raw = Vec::new();
- for idx in 0..layer_count {
- let layer = &bs_info.s_layer_info[idx];
- if layer.p_bs_buf.is_null()
- || layer.p_nal_length_in_byte.is_null()
- || layer.i_nal_count <= 0
- {
- continue;
+ if api.minor >= 3 && bs_union.v23.e_frame_type != 0 {
+ let bs_info = &bs_union.v23;
+ if bs_info.e_frame_type == 4 || bs_info.i_layer_num <= 0 {
+ return Ok(Vec::new());
+ }
+ e_frame_type = bs_info.e_frame_type;
+ let layer_count = (bs_info.i_layer_num as usize).min(bs_info.s_layer_info.len());
+ for idx in 0..layer_count {
+ let layer = &bs_info.s_layer_info[idx];
+ if layer.p_bs_buf.is_null()
+ || layer.p_nal_length_in_byte.is_null()
+ || layer.i_nal_count <= 0
+ {
+ continue;
+ }
+ let nal_lens = std::slice::from_raw_parts(
+ layer.p_nal_length_in_byte,
+ layer.i_nal_count as usize,
+ );
+ let total_bytes: usize = nal_lens.iter().map(|&n| n.max(0) as usize).sum();
+ if total_bytes == 0 {
+ continue;
+ }
+ let layer_bytes = std::slice::from_raw_parts(layer.p_bs_buf, total_bytes);
+ if layer.ui_layer_type == 0 {
+ self.seq_header.clear();
+ self.seq_header.extend_from_slice(layer_bytes);
+ }
+ raw.extend_from_slice(layer_bytes);
}
- let nal_lens =
- std::slice::from_raw_parts(layer.p_nal_length_in_byte, layer.i_nal_count as usize);
- let total_bytes: usize = nal_lens.iter().map(|&n| n.max(0) as usize).sum();
- if total_bytes == 0 {
- continue;
+ } else {
+ let bs_info = &bs_union.v20;
+ if bs_info.e_frame_type == 4 || bs_info.i_layer_num <= 0 {
+ return Ok(Vec::new());
}
- let layer_bytes = std::slice::from_raw_parts(layer.p_bs_buf, total_bytes);
- if layer.ui_layer_type == 0 {
- self.seq_header.clear();
- self.seq_header.extend_from_slice(layer_bytes);
+ e_frame_type = bs_info.e_frame_type;
+ let layer_count = (bs_info.i_layer_num as usize).min(bs_info.s_layer_info.len());
+ for idx in 0..layer_count {
+ let layer = &bs_info.s_layer_info[idx];
+ if layer.p_bs_buf.is_null()
+ || layer.p_nal_length_in_byte.is_null()
+ || layer.i_nal_count <= 0
+ {
+ continue;
+ }
+ let nal_lens = std::slice::from_raw_parts(
+ layer.p_nal_length_in_byte,
+ layer.i_nal_count as usize,
+ );
+ let total_bytes: usize = nal_lens.iter().map(|&n| n.max(0) as usize).sum();
+ if total_bytes == 0 {
+ continue;
+ }
+ let layer_bytes = std::slice::from_raw_parts(layer.p_bs_buf, total_bytes);
+ if layer.ui_layer_type == 0 {
+ self.seq_header.clear();
+ self.seq_header.extend_from_slice(layer_bytes);
+ }
+ raw.extend_from_slice(layer_bytes);
}
- raw.extend_from_slice(layer_bytes);
}
if raw.is_empty() {
return Ok(Vec::new());
}
- let is_idr = !self.sent_first_frame || bs_info.e_frame_type == 1 || has_h264_idr(&raw);
+ let is_idr = !self.sent_first_frame || e_frame_type == 1 || has_h264_idr(&raw);
let mut bytes = Vec::new();
if is_idr && !has_h264_sps(&raw) && !self.seq_header.is_empty() {
bytes.extend_from_slice(&self.seq_header);
@@ -1273,6 +1365,393 @@ mod imp {
}
}
+ #[cfg(target_os = "linux")]
+ fn has_executable(name: &str) -> bool {
+ let Some(path_var) = std::env::var_os("PATH") else {
+ return false;
+ };
+ std::env::split_paths(&path_var).any(|dir| dir.join(name).is_file())
+ }
+
+ /// Whether gst-launch and its PipeWire source are installed (gstreamer1.0-tools
+ /// and gstreamer1.0-pipewire on Debian and Ubuntu), which Wayland capture uses.
+ #[cfg(target_os = "linux")]
+ pub(crate) fn pipewire_capture_installed() -> bool {
+ has_executable("gst-launch-1.0")
+ && std::process::Command::new("gst-inspect-1.0")
+ .args(["--exists", "pipewiresrc"])
+ .stdin(std::process::Stdio::null())
+ .stdout(std::process::Stdio::null())
+ .stderr(std::process::Stdio::null())
+ .status()
+ .is_ok_and(|s| s.success())
+ }
+
+ #[cfg(target_os = "linux")]
+ fn is_wayland_screencast_available() -> bool {
+ if std::env::var_os("WAYLAND_DISPLAY").is_none() {
+ return false;
+ }
+ static AVAILABLE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
+ *AVAILABLE.get_or_init(|| {
+ if !pipewire_capture_installed() {
+ return false;
+ }
+ let Ok(conn) = zbus::blocking::Connection::session() else {
+ return false;
+ };
+ let Ok(dbus) = zbus::blocking::fdo::DBusProxy::new(&conn) else {
+ return false;
+ };
+ "org.freedesktop.portal.Desktop"
+ .try_into()
+ .ok()
+ .and_then(|name| dbus.name_has_owner(name).ok())
+ .unwrap_or(false)
+ })
+ }
+
+ #[cfg(target_os = "linux")]
+ struct WaylandScreenCaster {
+ _conn: zbus::blocking::Connection,
+ /// The PipeWire connection the portal opened for us; the stream is
+ /// only readable through it.
+ remote: std::os::fd::OwnedFd,
+ node_id: u32,
+ started: Instant,
+ src_w: u32,
+ src_h: u32,
+ dst_w: u32,
+ dst_h: u32,
+ child: Option<std::process::Child>,
+ latest_frame: Arc<Mutex<Option<Vec<u8>>>>,
+ }
+
+ #[cfg(target_os = "linux")]
+ impl Drop for WaylandScreenCaster {
+ fn drop(&mut self) {
+ if let Some(mut child) = self.child.take() {
+ let _ = child.kill();
+ let _ = child.wait();
+ }
+ }
+ }
+
+ #[cfg(target_os = "linux")]
+ fn wait_portal_response(
+ iter: &mut zbus::blocking::MessageIterator,
+ expected_path: &str,
+ ) -> Option<std::collections::HashMap<String, zbus::zvariant::OwnedValue>> {
+ for msg in iter.by_ref() {
+ let msg = msg.ok()?;
+ let hdr = msg.header();
+ if hdr.interface().map(|i| i.as_str()) == Some("org.freedesktop.portal.Request")
+ && hdr.member().map(|m| m.as_str()) == Some("Response")
+ && hdr.path().map(|p| p.as_str()) == Some(expected_path)
+ {
+ let (code, results): (u32, std::collections::HashMap<String, zbus::zvariant::OwnedValue>) =
+ msg.body().deserialize().ok()?;
+ return if code == 0 { Some(results) } else { None };
+ }
+ }
+ None
+ }
+
+ #[cfg(target_os = "linux")]
+ impl WaylandScreenCaster {
+ #[allow(clippy::type_complexity)]
+ fn negotiate_portal() -> Option<(zbus::blocking::Connection, std::os::fd::OwnedFd, u32, u32, u32)> {
+ use std::collections::HashMap;
+ use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value};
+
+ let conn = zbus::blocking::Connection::session().ok()?;
+ let rule = zbus::MatchRule::builder()
+ .msg_type(zbus::message::Type::Signal)
+ .interface("org.freedesktop.portal.Request")
+ .and_then(|b| b.member("Response"))
+ .map(|b| b.build())
+ .ok()?;
+ let dbus = zbus::blocking::fdo::DBusProxy::new(&conn).ok()?;
+ dbus.add_match_rule(rule).ok()?;
+ let mut iter = zbus::blocking::MessageIterator::from(&conn);
+
+ let pid = std::process::id();
+
+ // 1. CreateSession
+ let mut create_opts: HashMap<&str, Value<'_>> = HashMap::new();
+ let req_tok_1 = format!("nectarlink_sc_req1_{pid}");
+ let sess_tok = format!("nectarlink_sc_sess_{pid}");
+ create_opts.insert("handle_token", Value::from(req_tok_1.as_str()));
+ create_opts.insert("session_handle_token", Value::from(sess_tok.as_str()));
+
+ let reply = conn
+ .call_method(
+ Some("org.freedesktop.portal.Desktop"),
+ "/org/freedesktop/portal/desktop",
+ Some("org.freedesktop.portal.ScreenCast"),
+ "CreateSession",
+ &(&create_opts,),
+ )
+ .ok()?;
+ let req_path_1: OwnedObjectPath = reply.body().deserialize().ok()?;
+ let create_res = wait_portal_response(&mut iter, req_path_1.as_str())?;
+ let session_path_str = create_res.get("session_handle").and_then(|v| {
+ String::try_from(v.clone())
+ .ok()
+ .or_else(|| OwnedObjectPath::try_from(v.clone()).ok().map(|p| p.to_string()))
+ })?;
+ let session_obj = ObjectPath::try_from(session_path_str.as_str()).ok()?;
+
+ // 2. SelectSources (types = 1 MONITOR, cursor_mode = 2 EMBEDDED)
+ let mut select_opts: HashMap<&str, Value<'_>> = HashMap::new();
+ let req_tok_2 = format!("nectarlink_sc_req2_{pid}");
+ select_opts.insert("handle_token", Value::from(req_tok_2.as_str()));
+ select_opts.insert("types", Value::from(1u32));
+ select_opts.insert("multiple", Value::from(false));
+ select_opts.insert("cursor_mode", Value::from(2u32));
+ // Ask once while the app runs: the portal hands back a token that
+ // restores the same choice without a new dialog.
+ select_opts.insert("persist_mode", Value::from(1u32));
+ let restore = RESTORE_TOKEN.lock().unwrap_or_else(|e| e.into_inner()).clone();
+ if let Some(token) = restore.as_deref() {
+ select_opts.insert("restore_token", Value::from(token));
+ }
+
+ let reply = conn
+ .call_method(
+ Some("org.freedesktop.portal.Desktop"),
+ "/org/freedesktop/portal/desktop",
+ Some("org.freedesktop.portal.ScreenCast"),
+ "SelectSources",
+ &(&session_obj, &select_opts),
+ )
+ .ok()?;
+ let req_path_2: OwnedObjectPath = reply.body().deserialize().ok()?;
+ wait_portal_response(&mut iter, req_path_2.as_str())?;
+
+ // 3. Start
+ let mut start_opts: HashMap<&str, Value<'_>> = HashMap::new();
+ let req_tok_3 = format!("nectarlink_sc_req3_{pid}");
+ start_opts.insert("handle_token", Value::from(req_tok_3.as_str()));
+
+ let reply = conn
+ .call_method(
+ Some("org.freedesktop.portal.Desktop"),
+ "/org/freedesktop/portal/desktop",
+ Some("org.freedesktop.portal.ScreenCast"),
+ "Start",
+ &(&session_obj, "", &start_opts),
+ )
+ .ok()?;
+ let req_path_3: OwnedObjectPath = reply.body().deserialize().ok()?;
+ let start_res = wait_portal_response(&mut iter, req_path_3.as_str())?;
+ if let Some(token) = start_res.get("restore_token").and_then(|v| String::try_from(v.clone()).ok())
+ {
+ *RESTORE_TOKEN.lock().unwrap_or_else(|e| e.into_inner()) = Some(token);
+ }
+
+ let streams_val = start_res.get("streams")?;
+ let streams: Vec<(u32, HashMap<String, OwnedValue>)> = streams_val.clone().try_into().ok()?;
+ let (node_id, props) = streams.into_iter().next()?;
+ let (src_w, src_h) = props
+ .get("size")
+ .and_then(|v| <(i32, i32)>::try_from(v.clone()).ok())
+ .filter(|&(w, h)| w > 0 && h > 0)
+ .map(|(w, h)| (w as u32, h as u32))
+ .unwrap_or((1920, 1080));
+
+ // The stream is only readable through the PipeWire connection
+ // the portal opens for this session.
+ let empty_opts: HashMap<&str, Value<'_>> = HashMap::new();
+ let reply = conn
+ .call_method(
+ Some("org.freedesktop.portal.Desktop"),
+ "/org/freedesktop/portal/desktop",
+ Some("org.freedesktop.portal.ScreenCast"),
+ "OpenPipeWireRemote",
+ &(&session_obj, &empty_opts),
+ )
+ .ok()?;
+ let remote: zbus::zvariant::OwnedFd = reply.body().deserialize().ok()?;
+ let remote: std::os::fd::OwnedFd = remote.into();
+
+ Some((conn, remote, node_id, src_w, src_h))
+ }
+
+ /// The fd number the portal's PipeWire connection gets in the capture process.
+ const REMOTE_FD: i32 = 3;
+
+ /// Starts gst-launch reading the stream through `remote` and writing
+ /// raw BGRx frames of `dst_w` x `dst_h` to its stdout.
+ #[allow(clippy::type_complexity)]
+ fn spawn_pipeline(
+ remote: &std::os::fd::OwnedFd,
+ node_id: u32,
+ dst_w: u32,
+ dst_h: u32,
+ ) -> Option<(std::process::Child, Arc<Mutex<Option<Vec<u8>>>>)> {
+ use std::io::Read;
+ use std::os::fd::AsRawFd;
+ use std::os::unix::process::CommandExt;
+ use std::process::{Command, Stdio};
+
+ let raw = remote.as_raw_fd();
+ let mut cmd = Command::new("gst-launch-1.0");
+ cmd.args(pipeline_args(node_id, dst_w, dst_h))
+ .stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::null());
+ // SAFETY: runs in the child between fork and exec and only calls
+ // async-signal-safe functions (dup2, fcntl) on descriptors we own.
+ unsafe {
+ cmd.pre_exec(move || {
+ if raw == Self::REMOTE_FD {
+ // Already in place: only let it survive exec.
+ if libc::fcntl(raw, libc::F_SETFD, 0) < 0 {
+ return Err(std::io::Error::last_os_error());
+ }
+ } else if libc::dup2(raw, Self::REMOTE_FD) < 0 {
+ return Err(std::io::Error::last_os_error());
+ }
+ Ok(())
+ });
+ }
+ let mut child = cmd.spawn().ok()?;
+
+ let mut stdout = child.stdout.take()?;
+ let latest_frame: Arc<Mutex<Option<Vec<u8>>>> = Arc::new(Mutex::new(None));
+ let frame_slot = Arc::clone(&latest_frame);
+ let frame_bytes = (dst_w as usize) * (dst_h as usize) * 4;
+
+ std::thread::Builder::new()
+ .name("wayland-screencast-reader".into())
+ .spawn(move || {
+ let mut buf = vec![0u8; frame_bytes];
+ while stdout.read_exact(&mut buf).is_ok() {
+ for chunk in buf.as_chunks_mut::<4>().0 {
+ chunk[3] = 0xFF;
+ }
+ *frame_slot.lock().unwrap_or_else(|e| e.into_inner()) = Some(buf.clone());
+ }
+ })
+ .ok()?;
+
+ Some((child, latest_frame))
+ }
+
+ fn start(dst_w: u32, dst_h: u32) -> Option<Self> {
+ let (conn, remote, node_id, src_w, src_h) = Self::negotiate_portal()?;
+ let (child, latest_frame) = Self::spawn_pipeline(&remote, node_id, dst_w, dst_h)?;
+ Some(Self {
+ _conn: conn,
+ remote,
+ node_id,
+ started: Instant::now(),
+ src_w,
+ src_h,
+ dst_w,
+ dst_h,
+ child: Some(child),
+ latest_frame,
+ })
+ }
+
+ /// Whether the capture process is still running.
+ fn alive(&mut self) -> bool {
+ self.child.as_mut().is_some_and(|c| matches!(c.try_wait(), Ok(None)))
+ }
+
+ fn capture(&mut self, dst_w: u32, dst_h: u32, bgra: &mut Vec<u8>) -> Option<(u32, u32)> {
+ if (self.dst_w, self.dst_h) != (dst_w, dst_h) {
+ if let Some(mut old) = self.child.take() {
+ let _ = old.kill();
+ let _ = old.wait();
+ }
+ let (child, latest_frame) = Self::spawn_pipeline(&self.remote, self.node_id, dst_w, dst_h)?;
+ self.started = Instant::now();
+ self.dst_w = dst_w;
+ self.dst_h = dst_h;
+ self.child = Some(child);
+ self.latest_frame = latest_frame;
+ }
+
+ // The first frame can take a while (the pipeline negotiates
+ // formats); after that a frame is late only for a moment.
+ let wait = if self.started.elapsed() < Duration::from_secs(3) {
+ Duration::from_secs(3).saturating_sub(self.started.elapsed())
+ } else {
+ Duration::from_millis(250)
+ };
+ let deadline = Instant::now() + wait;
+ loop {
+ if let Some(frame) = self.latest_frame.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {
+ bgra.clear();
+ bgra.extend_from_slice(frame);
+ return Some((self.src_w, self.src_h));
+ }
+ if Instant::now() >= deadline {
+ return None;
+ }
+ std::thread::sleep(Duration::from_millis(10));
+ }
+ }
+ }
+
+ #[cfg(target_os = "linux")]
+ static WAYLAND_CASTER: Mutex<Option<WaylandScreenCaster>> = Mutex::new(None);
+
+ /// The ScreenCast portal's restore token, so later sessions in this run
+ /// reuse the screen the user chose instead of asking again.
+ #[cfg(target_os = "linux")]
+ static RESTORE_TOKEN: Mutex<Option<String>> = Mutex::new(None);
+
+ /// gst-launch arguments: read stream `node_id` through the portal's
+ /// PipeWire connection (fd 3) and write raw BGRx frames to stdout.
+ #[cfg(target_os = "linux")]
+ pub(crate) fn pipeline_args(node_id: u32, dst_w: u32, dst_h: u32) -> Vec<String> {
+ [
+ "-q".to_owned(),
+ "pipewiresrc".to_owned(),
+ format!("fd={}", WaylandScreenCaster::REMOTE_FD),
+ format!("path={node_id}"),
+ "do-timestamp=true".to_owned(),
+ "!".to_owned(),
+ "videoconvert".to_owned(),
+ "!".to_owned(),
+ "videoscale".to_owned(),
+ "!".to_owned(),
+ format!("video/x-raw,format=BGRx,width={dst_w},height={dst_h}"),
+ "!".to_owned(),
+ "fdsink".to_owned(),
+ "fd=1".to_owned(),
+ ]
+ .into()
+ }
+
+ #[cfg(target_os = "linux")]
+ fn capture_screen_wayland(dst_w: u32, dst_h: u32, bgra: &mut Vec<u8>) -> Option<(u32, u32)> {
+ if !is_wayland_screencast_available() {
+ return None;
+ }
+ let mut guard = WAYLAND_CASTER.lock().unwrap_or_else(|e| e.into_inner());
+ if guard.is_none() {
+ *guard = WaylandScreenCaster::start(dst_w, dst_h);
+ }
+ let caster = guard.as_mut()?;
+ let res = caster.capture(dst_w, dst_h, bgra);
+ // A late frame is skipped; only a stopped capture ends the session
+ // (starting a new one would ask the user again).
+ if res.is_none() && !caster.alive() {
+ *guard = None;
+ }
+ res
+ }
+
+ #[cfg(target_os = "linux")]
+ fn stop_wayland_screencast() {
+ *WAYLAND_CASTER.lock().unwrap_or_else(|e| e.into_inner()) = None;
+ }
+
#[cfg(target_os = "linux")]
fn capture_screen(
region: Option<crate::win::browser::RECT>,
@@ -1280,6 +1759,12 @@ mod imp {
dst_h: u32,
bgra: &mut Vec<u8>,
) -> Option<(u32, u32)> {
+ if region.is_none()
+ && std::env::var_os("WAYLAND_DISPLAY").is_some()
+ && let Some(dims) = capture_screen_wayland(dst_w, dst_h, bgra)
+ {
+ return Some(dims);
+ }
with_x11_display(|api, dpy| unsafe {
let root = (api.default_root_window)(dpy);
let mut attrs: XWindowAttributes = std::mem::zeroed();
@@ -1611,6 +2096,8 @@ mod imp {
}
}
+ #[cfg(target_os = "linux")]
+ stop_wayland_screencast();
tracing::info!(%peer, "stopped sharing this PC's screen");
crate::input_share::leave_remote();
sender.close();
@@ -1681,6 +2168,17 @@ mod imp {
mod tests {
use super::*;
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn wayland_capture_reads_the_stream_through_the_portal_connection() {
+ let args = pipeline_args(57, 1280, 720);
+ assert_eq!(args[1], "pipewiresrc");
+ assert!(args.contains(&"fd=3".to_owned()), "the portal's PipeWire connection, as fd 3");
+ assert!(args.contains(&"path=57".to_owned()));
+ assert!(args.contains(&"video/x-raw,format=BGRx,width=1280,height=720".to_owned()));
+ assert_eq!(args.last().map(String::as_str), Some("fd=1"));
+ }
+
#[test]
fn computes_target_dimensions_and_nv12() {
let (w, h) = target_dimensions(1920, 1080, 1280);
@@ -1815,10 +2313,6 @@ mod imp {
pub fn on_event(_event: &NodeEvent) {}
pub fn set_mute_while_watched(_on: bool) {}
-
- pub fn pipewire_capture_installed() -> bool {
- false
- }
}
pub use imp::*;
diff --git a/desktop/app/src/phone_mic.rs b/desktop/app/src/phone_mic.rs
index 59e6520..e0dfb4c 100644
--- a/desktop/app/src/phone_mic.rs
+++ b/desktop/app/src/phone_mic.rs
@@ -69,7 +69,9 @@ pub fn phase() -> Phase {
/// Reads the chosen output from the settings.
pub fn init(settings: &crate::settings::Settings) {
#[cfg(target_os = "linux")]
- crate::win::vmic::cleanup_stale_sources();
+ let _ = std::thread::Builder::new()
+ .name("vmic-cleanup".into())
+ .spawn(crate::win::vmic::cleanup_stale_sources);
*OUTPUT.lock().unwrap_or_else(|e| e.into_inner()) = settings.phone_mic_output.clone();
GAIN.store(settings.phone_mic_gain.clamp(MIN_GAIN, MAX_GAIN), Ordering::Relaxed);
}
@@ -160,7 +162,7 @@ pub fn outputs() -> (Vec<AudioOutputDevice>, Option<String>) {
#[allow(unused_mut)]
let mut devices = crate::win::audio::output_devices();
#[cfg(target_os = "linux")]
- {
+ if crate::win::vmic::is_available() {
if !devices.iter().any(|d| d.id == VIRTUAL_MIC_MONO_ID) {
devices.insert(
0,
@@ -322,14 +324,20 @@ impl Sink {
self.player.lock().unwrap_or_else(|e| e.into_inner()).take();
let out_channels = if output.as_deref() == Some(VIRTUAL_MIC_MONO_ID) { 1 } else { 2 };
let phone_name = core_host::host().hub.read(|s| s.name_of(&self.peer));
- let vmic = crate::win::vmic::VirtualMic::new_for_phone_with_channels(
+ match crate::win::vmic::VirtualMic::open_with_label(
config.rate,
config.channels,
out_channels,
phone_name.as_deref(),
- );
- *self.vmic.lock().unwrap_or_else(|e| e.into_inner()) = Some(vmic);
- return;
+ ) {
+ Ok(vmic) => {
+ *self.vmic.lock().unwrap_or_else(|e| e.into_inner()) = Some(vmic);
+ return;
+ }
+ Err(e) => {
+ tracing::warn!(error = %e, "can't create virtual microphone; falling back to speaker output");
+ }
+ }
}
#[cfg(target_os = "linux")]
self.vmic.lock().unwrap_or_else(|e| e.into_inner()).take();
diff --git a/desktop/app/src/recordings.rs b/desktop/app/src/recordings.rs
index a24bef3..e721f0b 100644
--- a/desktop/app/src/recordings.rs
+++ b/desktop/app/src/recordings.rs
@@ -118,29 +118,13 @@ pub fn current_recording_stem() -> String {
#[cfg(not(windows))]
pub fn current_recording_stem() -> String {
- #[repr(C)]
- struct Tm {
- tm_sec: i32,
- tm_min: i32,
- tm_hour: i32,
- tm_mday: i32,
- tm_mon: i32,
- tm_year: i32,
- tm_wday: i32,
- tm_yday: i32,
- tm_isdst: i32,
- tm_gmtoff: i64,
- tm_zone: *const std::ffi::c_char,
- }
- unsafe extern "C" {
- fn time(tloc: *mut i64) -> i64;
- fn localtime_r(timep: *const i64, result: *mut Tm) -> *mut Tm;
- }
+ // SAFETY: `libc::time` and `libc::localtime_r` are thread-safe POSIX calls
+ // operating on stack-allocated `time_t` and `tm` values.
unsafe {
- let mut t: i64 = 0;
- time(&mut t);
- let mut tm = std::mem::zeroed::<Tm>();
- localtime_r(&t, &mut tm);
+ let mut t: libc::time_t = 0;
+ libc::time(&mut t);
+ let mut tm = std::mem::zeroed::<libc::tm>();
+ libc::localtime_r(&t, &mut tm);
format_recording_stem(
(tm.tm_year + 1900) as u16,
(tm.tm_mon + 1) as u16,
@@ -282,42 +266,49 @@ fn move_file(src: &Path, dest: &Path) -> std::io::Result<()> {
Ok(())
}
-/// Converts `src` (`.m4a`) to `dest` in `format` (`Mp3`, `Wav`, or `Flac`)
-/// using `ffmpeg` on Linux.
+/// ffmpeg arguments that convert `src` into `dest` in `format`. The paths
+/// are arguments of their own (never parsed by a shell), and a relative path
+/// starting with `-` gets `./` so ffmpeg can't take it for an option.
+#[cfg(target_os = "linux")]
+pub(crate) fn ffmpeg_args(src: &Path, dest: &Path, format: RecordingFormat) -> Vec<std::ffi::OsString> {
+ fn not_an_option(path: &Path) -> std::ffi::OsString {
+ if path.to_string_lossy().starts_with('-') { Path::new(".").join(path).into() } else { path.into() }
+ }
+ let codec: &[&str] = match format {
+ RecordingFormat::M4a => &["-c", "copy"],
+ RecordingFormat::Mp3 => &["-c:a", "libmp3lame", "-q:a", "2"],
+ RecordingFormat::Wav => &["-c:a", "pcm_s16le"],
+ RecordingFormat::Flac => &["-c:a", "flac"],
+ };
+ let mut args: Vec<std::ffi::OsString> =
+ ["-nostdin", "-v", "error", "-y", "-i"].into_iter().map(Into::into).collect();
+ args.push(not_an_option(src));
+ args.extend(codec.iter().map(Into::into));
+ args.push(not_an_option(dest));
+ args
+}
+
+/// Converts `src` (`.m4a`) to `dest` in `format` with ffmpeg on Linux.
#[cfg(target_os = "linux")]
pub fn convert_audio(src: &Path, dest: &Path, format: RecordingFormat) -> std::io::Result<()> {
- match format {
- RecordingFormat::M4a => {
- fs::copy(src, dest)?;
- Ok(())
- }
- RecordingFormat::Wav | RecordingFormat::Mp3 | RecordingFormat::Flac => {
- let safe_src = if src.is_relative() && src.to_string_lossy().starts_with('-') {
- Path::new(".").join(src)
- } else {
- src.to_path_buf()
- };
- let safe_dest = if dest.is_relative() && dest.to_string_lossy().starts_with('-') {
- Path::new(".").join(dest)
- } else {
- dest.to_path_buf()
- };
- let status = std::process::Command::new("ffmpeg")
- .args(["-v", "error", "-y", "-i"])
- .arg(&safe_src)
- .arg("--")
- .arg(&safe_dest)
- .stdin(std::process::Stdio::null())
- .stdout(std::process::Stdio::null())
- .stderr(std::process::Stdio::null())
- .status()?;
- if status.success() {
- Ok(())
+ if matches!(format, RecordingFormat::M4a) {
+ fs::copy(src, dest)?;
+ return Ok(());
+ }
+ let status = std::process::Command::new("ffmpeg")
+ .args(ffmpeg_args(src, dest, format))
+ .stdin(std::process::Stdio::null())
+ .stdout(std::process::Stdio::null())
+ .stderr(std::process::Stdio::null())
+ .status()
+ .map_err(|e| {
+ if e.kind() == std::io::ErrorKind::NotFound {
+ std::io::Error::other("converting recordings needs ffmpeg")
} else {
- Err(std::io::Error::other(format!("ffmpeg exited with {status}")))
+ e
}
- }
- }
+ })?;
+ if status.success() { Ok(()) } else { Err(std::io::Error::other(format!("ffmpeg exited with {status}"))) }
}
#[cfg(not(any(windows, target_os = "linux")))]
@@ -844,6 +835,28 @@ mod tests {
];
let text = format_markers(&markers);
assert_eq!(text, "00:01.250 Intro\n01:05.000 Marker 2\n01:01:01.005 Wrap-up\n");
+
+ let stem = current_recording_stem();
+ assert!(stem.starts_with("Recording "));
+ assert_eq!(stem_from_phone(&format!("{stem}.m4a")).as_deref(), Some(stem.as_str()));
+
+ #[cfg(target_os = "linux")]
+ {
+ let args = ffmpeg_args(
+ Path::new("-odd name!.m4a"),
+ Path::new("/tmp/out it's.mp3"),
+ RecordingFormat::Mp3,
+ );
+ let args: Vec<String> = args.iter().map(|a| a.to_string_lossy().into_owned()).collect();
+ assert_eq!(args[..5], ["-nostdin", "-v", "error", "-y", "-i"]);
+ assert_eq!(args[5], "./-odd name!.m4a", "a path can't be read as an option");
+ assert!(args.windows(2).any(|w| w == ["-c:a", "libmp3lame"]));
+ assert_eq!(
+ args.last().map(String::as_str),
+ Some("/tmp/out it's.mp3"),
+ "names are passed as they are"
+ );
+ }
}
#[cfg(any(windows, target_os = "linux"))]
diff --git a/desktop/app/src/webcam.rs b/desktop/app/src/webcam.rs
index c8e2a4a..88d0fd6 100644
--- a/desktop/app/src/webcam.rs
+++ b/desktop/app/src/webcam.rs
@@ -382,7 +382,11 @@ fn mapping() -> Option<Arc<SharedFrameMapping>> {
let due = s.global_tried.is_none_or(|at| at.elapsed() >= std::time::Duration::from_secs(1));
if !s.mapping_global && due {
s.global_tried = Some(std::time::Instant::now());
- if let Ok(global) = SharedFrameMapping::open_existing(DEFAULT_MAPPING_NAME) {
+ #[cfg(windows)]
+ let global_res = SharedFrameMapping::open_existing(DEFAULT_MAPPING_NAME);
+ #[cfg(not(windows))]
+ let global_res = SharedFrameMapping::open_or_create(DEFAULT_MAPPING_NAME);
+ if let Ok(global) = global_res {
tracing::debug!("joined the virtual camera's frame mapping");
s.mapping = Some(Arc::new(global));
s.mapping_global = true;
@@ -402,7 +406,11 @@ fn idle_mapping() -> Option<Arc<SharedFrameMapping>> {
let due = s.global_tried.is_none_or(|at| at.elapsed() >= std::time::Duration::from_secs(1));
if !s.mapping_global && due {
s.global_tried = Some(std::time::Instant::now());
- if let Ok(global) = SharedFrameMapping::open_existing(DEFAULT_MAPPING_NAME) {
+ #[cfg(windows)]
+ let global_res = SharedFrameMapping::open_existing(DEFAULT_MAPPING_NAME);
+ #[cfg(not(windows))]
+ let global_res = SharedFrameMapping::open_or_create(DEFAULT_MAPPING_NAME);
+ if let Ok(global) = global_res {
tracing::debug!("joined the virtual camera's frame mapping");
s.mapping = Some(Arc::new(global));
s.mapping_global = true;