Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Play on phone: find the player in Zen and other browsers
Beedba3ea26cd4e
diff --git a/desktop/app/src/pc_mirror.rs b/desktop/app/src/pc_mirror.rsindex ddd7bd8..8787768 100644--- a/desktop/app/src/pc_mirror.rs+++ b/desktop/app/src/pc_mirror.rs@@ -62,6 +62,10 @@ struct ActiveSession {static ACTIVE: Mutex<Option<ActiveSession>> = Mutex::new(None);+/// How long Play on phone looks for the player's window before streaming+/// the screen instead.+const FIND_WINDOW: Duration = Duration::from_secs(5);+/// The lowest bitrate the stream backs off to on a slow link.const MIN_BITRATE: u32 = 600_000;@@ -690,6 +694,7 @@ fn run_capture_loop(}let watched = options.watch.as_deref().map(crate::play_on_phone::Watched::follow);let mut window_capture: Option<win::window_capture::WindowCapture> = None;+ let mut fell_back = false;let _sound = watched.is_some().then(|| SoundToPhone::start(peer));if watched.is_some() {tracing::info!(%peer, fps, bitrate, "streaming a player to the phone");@@ -701,12 +706,18 @@ fn run_capture_loop(let tick_start = Instant::now();let place = watched.as_ref().and_then(crate::play_on_phone::Watched::place);- if watched.is_some() {- let Some(place) = place else {- // Not found yet: nothing to show.- std::thread::sleep(frame_interval);- continue;- };+ if watched.is_some() && place.is_none() && started.elapsed() < FIND_WINDOW {+ // Not found yet: nothing to show.+ std::thread::sleep(frame_interval);+ continue;+ }+ if watched.is_some() && place.is_none() && !fell_back {+ // The player's window can't be found: the phone gets the screen+ // rather than nothing.+ tracing::info!(%peer, "can't find the player's window; streaming the screen");+ fell_back = true;+ }+ if let Some(place) = place {let r = place.on_screen;let dims =target_dimensions((r.right - r.left) as u32, (r.bottom - r.top) as u32, options.max_size);diff --git a/desktop/app/src/win/browser.rs b/desktop/app/src/win/browser.rsindex 65ce884..e91e903 100644--- a/desktop/app/src/win/browser.rs+++ b/desktop/app/src/win/browser.rs@@ -7,7 +7,8 @@//! address bar as an edit field of class `OmniboxViewViews` and a page's//! elements with their HTML classes, so YouTube's player//! (`video-stream html5-main-video`) can be found exactly; Firefox's-//! address bar has the automation ID `urlbar-input`. Nothing here reads a+//! address bar has the automation ID `urlbar-input`, and Firefox-based+//! browsers (Zen, Floorp...) show only their tab's area, not the page. Nothing here reads a//! page's text: only the address of the tab that plays.use windows::{@@ -51,8 +52,14 @@ pub fn programs_for(player: &str) -> Vec<String> {("opera", "opera.exe"),("vivaldi", "vivaldi.exe"),("firefox", "firefox.exe"),+ ("zen", "zen.exe"),+ ("floorp", "floorp.exe"),+ ("librewolf", "librewolf.exe"),+ ("waterfox", "waterfox.exe"),// Firefox's app ID is a hash of where it's installed; this is the- // one for its default location.+ // one for its default location. Other installs (and Firefox-based+ // browsers) have hashes of their own: their window is found by its+ // title (see `window_for`).("308046b0af4a39cb", "firefox.exe"),];let mut programs: Vec<String> =@@ -74,7 +81,19 @@ pub fn programs_for(player: &str) -> Vec<String> {/// Whether `program` (an executable's file name) is a browser whose/// address bar and page this module can read.pub fn is_browser(program: &str) -> bool {- matches!(program, "chrome.exe" | "msedge.exe" | "brave.exe" | "opera.exe" | "vivaldi.exe" | "firefox.exe")+ matches!(+ program,+ "chrome.exe"+ | "msedge.exe"+ | "brave.exe"+ | "opera.exe"+ | "vivaldi.exe"+ | "firefox.exe"+ | "zen.exe"+ | "floorp.exe"+ | "librewolf.exe"+ | "waterfox.exe"+ )}/// A visible top-level window.@@ -158,8 +177,25 @@ fn program_of(hwnd: HWND) -> Option<String> {/// browser's shows its active tab's), or its only window.pub fn window_for(player: &str, title: &str) -> Option<Window> {let programs = programs_for(player);- let candidates: Vec<Window> = windows().into_iter().filter(|w| programs.contains(&w.program)).collect();- pick(candidates, title)+ let all = windows();+ let candidates: Vec<Window> = all.iter().filter(|w| programs.contains(&w.program)).cloned().collect();+ pick(candidates, title).or_else(|| by_title(all, title))+}++/// The one window (of any program but this app) whose title has the+/// media's title in it, for players whose app ID names no program (a hash,+/// as Firefox-based browsers report).+fn by_title(windows: Vec<Window>, title: &str) -> Option<Window> {+ let wanted = title.trim().to_lowercase();+ if wanted.chars().count() < 3 {+ return None;+ }+ let mut matching = windows+ .into_iter()+ .filter(|w| w.program != "nectarlink-desktop.exe" && w.title.to_lowercase().contains(&wanted));+ let found = matching.next()?;+ // Two windows with that title: can't tell which one plays.+ matching.next().is_none().then_some(found)}fn pick(candidates: Vec<Window>, title: &str) -> Option<Window> {@@ -241,10 +277,19 @@ impl Automation {))}- /// Where the page is in `window`.+ /// Where the page is in `window`: Chromium's page, or the selected+ /// tab's area in Firefox-based browsers (whose pages UI Automation+ /// doesn't show, so their video can't be found).pub fn page_rect(&self, window: &Window) -> Option<RECT> {let root = self.root(window)?;self.rect_of(self.find(&root, UIA_ControlTypePropertyId, VARIANT::from(UIA_DocumentControlTypeId.0)))+ .or_else(|| {+ self.rect_of(self.find(+ &root,+ UIA_ClassNamePropertyId,+ VARIANT::from("browserSidebarContainer deck-selected"),+ ))+ })}fn rect_of(&self, element: Option<IUIAutomationElement>) -> Option<RECT> {@@ -328,6 +373,22 @@ mod tests {Window { hwnd: HWND::default(), program: "chrome.exe".into(), title: title.into() }}+ #[test]+ fn a_hashed_app_id_finds_its_window_by_title() {+ let zen = Window {+ hwnd: HWND::default(),+ program: "zen.exe".into(),+ title: "A Trailer - YouTube - Zen Browser".into(),+ };+ let mail = window("Mail - Google Chrome");+ assert_eq!(by_title(vec![mail.clone(), zen.clone()], "A Trailer").unwrap().program, "zen.exe");+ // Ambiguous or too short: none.+ assert!(by_title(vec![zen.clone(), zen.clone()], "A Trailer").is_none());+ assert!(by_title(vec![zen], "A").is_none());+ assert!(programs_for("F0DC299D809B9700").is_empty());+ assert!(is_browser("zen.exe"));+ }+#[test]fn the_window_with_the_title_is_the_player() {let picked =