Skip to content

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

Webcam: resolution buttons follow the stream on both sides

Beed3206b22de96
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d3dedb0..d144cf5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -52,6 +52,10 @@ change things that need a step from you, and its notes say so.
stays for the whole call, and the number comes from Android's call
service when its broadcast has none; a number not in contacts shows as the
number.
+- **Webcam resolution buttons stay in sync** on the PC and the phone. Each
+ follows the resolution actually streaming, whichever side changed it (or
+ 1080p when the phone can't do 4K); upright, the phone's buttons showed none
+ selected.
- Android: Phone as webcam no longer crashes the app when its preview
comes back (the same fix covers the QR scanner and Continuity Camera).
- Android: on Hive, the category chips at the top of Settings and the
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/webcam/WebcamScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/webcam/WebcamScreen.kt
index 816b4c0..f3475b3 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/webcam/WebcamScreen.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/webcam/WebcamScreen.kt
@@ -394,7 +394,7 @@ fun WebcamScreen(
}
for ((h, label) in resolutions) {
FilterChip(
- selected = session.height == h,
+ selected = session.resolution == h,
onClick = { WebcamService.setResolutionHeight(h) },
label = { Text(label) },
)
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt b/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt
index 834bb32..9ed9e11 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/webcam/WebcamService.kt
@@ -45,7 +45,23 @@ data class WebcamSession(
val hasFlash: Boolean = false,
val torchOn: Boolean = false,
val supports4k: Boolean = CameraEncoder.supports4kEncoder(),
-)
+) {
+ /**
+ * The resolution streaming (720, 1080 or 2160), from the picture's short
+ * side: upright, the phone streams 1080 × 1920 for 1080p.
+ */
+ val resolution: Int get() = resolutionOf(width, height)
+}
+
+/** 720, 1080 or 2160 for a picture of [width] × [height], either way up. */
+internal fun resolutionOf(width: Int, height: Int): Int {
+ val short = minOf(width, height)
+ return when {
+ short >= 2000 -> 2160
+ short >= 1000 -> 1080
+ else -> 720
+ }
+}
/**
* Streams the phone's camera as H.264 to a paired PC (`docs/protocol/webcam.md`).
diff --git a/android/app/src/test/kotlin/app/nectarlink/android/webcam/ResolutionTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/webcam/ResolutionTest.kt
new file mode 100644
index 0000000..63cf16e
--- /dev/null
+++ b/android/app/src/test/kotlin/app/nectarlink/android/webcam/ResolutionTest.kt
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.webcam
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+class ResolutionTest {
+ @Test
+ fun theResolutionComesFromTheShortSide() {
+ assertEquals(720, resolutionOf(1280, 720))
+ assertEquals(1080, resolutionOf(1920, 1080))
+ // Upright, the phone streams portrait.
+ assertEquals(1080, resolutionOf(1080, 1920))
+ assertEquals(2160, resolutionOf(3840, 2160))
+ assertEquals(2160, resolutionOf(2160, 3840))
+ // Encoders round to their block size.
+ assertEquals(1080, resolutionOf(1920, 1088))
+ }
+}
diff --git a/desktop/app/src/webcam.rs b/desktop/app/src/webcam.rs
index ab656eb..b5fbd0b 100644
--- a/desktop/app/src/webcam.rs
+++ b/desktop/app/src/webcam.rs
@@ -102,6 +102,30 @@ fn normalize_height(h: u32) -> u32 {
}
}
+/// 720, 1080 or 2160 for a picture of `width` × `height`, either way up (an
+/// upright phone streams 1080 × 1920 for 1080p).
+fn resolution_of(width: u32, height: u32) -> u32 {
+ match width.min(height) {
+ 2000.. => 2160,
+ 1000.. => 1080,
+ _ => 720,
+ }
+}
+
+/// What the phone is streaming now, chosen here or on the phone (or 1080p
+/// when it can't do 4K): the resolution buttons follow it, and the next
+/// stream starts with it. Doesn't ask the phone again.
+fn follow_stream(width: u32, height: u32) {
+ let h = resolution_of(width, height);
+ let prev = HEIGHT.swap(h, Ordering::Relaxed);
+ #[cfg(not(test))]
+ if prev != h {
+ save_settings(|s| s.webcam_height = h);
+ }
+ #[cfg(test)]
+ let _ = prev;
+}
+
/// Maps a resolution label (`"720"`, `"1080"`, `"2160"`, `"4k"`, `"4K"`) to
/// `(width, height)` in pixels.
pub fn dimensions(res: &str) -> (u16, u16) {
@@ -589,6 +613,7 @@ impl Video {
impl WebcamSink for Sink {
fn config(&self, config: WebcamConfig) {
+ follow_stream(config.width, config.height);
state(|s| {
s.preferred_phone = Some(self.device);
s.phase = Phase::Streaming {
@@ -736,6 +761,17 @@ mod tests {
assert_eq!(dimensions("unknown"), (1280, 720));
}
+ #[test]
+ fn the_resolution_comes_from_the_short_side() {
+ assert_eq!(resolution_of(1280, 720), 720);
+ assert_eq!(resolution_of(1920, 1080), 1080);
+ assert_eq!(resolution_of(1080, 1920), 1080);
+ assert_eq!(resolution_of(3840, 2160), 2160);
+ assert_eq!(resolution_of(2160, 3840), 2160);
+ // Encoders round to their block size.
+ assert_eq!(resolution_of(1920, 1088), 1080);
+ }
+
#[test]
fn selects_and_validates_720_1080_and_2160_resolutions() {
set_resolution("720");