Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Webcam: offer 4K on the PC only when the phone's camera can do it
Bee90d5047cd319
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.kt b/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.ktindex f2b7410..2c8dc44 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/webcam/CameraEncoder.kt@@ -460,7 +460,8 @@ internal class CameraEncoder(}private fun drainLoop() {- val cfgBytes = webcamConfig(width.toUInt(), height.toUInt(), camera, fps.toUInt())+ val maxHeight = if (supports4k) 2160u else 1080u+ val cfgBytes = webcamConfig(width.toUInt(), height.toUInt(), camera, fps.toUInt(), maxHeight)if (stream.send(VideoPacketKind.CONFIG, 0u, cfgBytes) == MirrorSendResult.CLOSED) {returndiff --git a/core/nectarlink-cli/src/main.rs b/core/nectarlink-cli/src/main.rsindex deadb8b..1726af6 100644--- a/core/nectarlink-cli/src/main.rs+++ b/core/nectarlink-cli/src/main.rs@@ -5055,6 +5055,7 @@ fn stream_webcam(height,camera: camera.into(),fps,+ max_height: 2160,}.to_cbor();if webcam.send(PacketKind::Config, 0, config) == MirrorSend::Closed {diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rsindex 9af299d..9c09fb6 100644--- a/core/nectarlink-core/tests/two_devices.rs+++ b/core/nectarlink-core/tests/two_devices.rs@@ -3215,6 +3215,7 @@ async fn the_phone_camera_streams_to_the_pc_as_a_webcam() {height: 1080,camera: nectarlink_core::WEBCAM_CAMERA_BACK.into(),fps: 30,+ max_height: 2160,};let sender = stream.clone();let sent = tokio::task::spawn_blocking(move || {@@ -3601,9 +3602,15 @@ async fn timeline_records_files_clips_links_photos_recordings_and_sessions_witho.await;let wstream = phone.node.webcam_open(pc_id).await.unwrap();- let wcfg =- WebcamConfig { codec: "h264".into(), width: 1280, height: 720, camera: "back".into(), fps: 30 }- .to_cbor();+ let wcfg = WebcamConfig {+ codec: "h264".into(),+ width: 1280,+ height: 720,+ camera: "back".into(),+ fps: 30,+ max_height: 1080,+ }+ .to_cbor();let _ = tokio::task::block_in_place(|| wstream.send(PacketKind::Config, 0, wcfg));wait_for(&mut pc, "webcam started", |e| match e {NodeEvent::Webcam { device, on: true } if *device == phone_id => Some(()),diff --git a/core/nectarlink-ffi/src/lib.rs b/core/nectarlink-ffi/src/lib.rsindex f2eaa9b..ed07eb9 100644--- a/core/nectarlink-ffi/src/lib.rs+++ b/core/nectarlink-ffi/src/lib.rs@@ -1178,10 +1178,12 @@ pub fn mirror_audio_config(rate: u32, channels: u8) -> Vec<u8> {core::MirrorAudioConfig { codec: core::MIRROR_PCM.into(), rate, channels }.to_cbor()}-/// The bytes of a config packet for an H.264 webcam stream (`docs/protocol/webcam.md`).+/// The bytes of a config packet for an H.264 webcam stream+/// (`docs/protocol/webcam.md`); `max_height` is 2160 when the camera in use+/// can stream 4K, else 1080.#[uniffi::export]-pub fn webcam_config(width: u32, height: u32, camera: String, fps: u32) -> Vec<u8> {- core::WebcamConfig { codec: core::WEBCAM_H264.into(), width, height, camera, fps }.to_cbor()+pub fn webcam_config(width: u32, height: u32, camera: String, fps: u32, max_height: u32) -> Vec<u8> {+ core::WebcamConfig { codec: core::WEBCAM_H264.into(), width, height, camera, fps, max_height }.to_cbor()}/// What a PC asked the phone's camera to stream (`docs/protocol/webcam.md`).diff --git a/core/nectarlink-protocol/src/messages.rs b/core/nectarlink-protocol/src/messages.rsindex d3b5866..439dd24 100644--- a/core/nectarlink-protocol/src/messages.rs+++ b/core/nectarlink-protocol/src/messages.rs@@ -1610,6 +1610,15 @@ pub struct WebcamConfig {/// Frames per second.#[serde(default = "default_webcam_fps")]pub fps: u32,+ /// The highest resolution the camera in use can stream, by its short+ /// side: 2160 when it can do 4K, else 1080. Phones before 0.1.5 don't+ /// say, and fall back to 1080p themselves when asked for 4K it can't do.+ #[serde(default = "default_webcam_max_height")]+ pub max_height: u32,+}++fn default_webcam_max_height() -> u32 {+ 2160}impl WebcamConfig {@@ -5029,11 +5038,27 @@ mod tests {height: 720,camera: webcam::CAMERA_BACK.into(),fps: 30,+ max_height: 1080,};assert!(cfg.is_valid());assert_eq!(WebcamConfig::from_cbor(&cfg.to_cbor()).unwrap(), cfg);assert!(!WebcamConfig { codec: "vp8".into(), ..cfg.clone() }.is_valid());assert!(!WebcamConfig { width: 0, ..cfg }.is_valid());++ // A phone before 0.1.5 doesn't say what its camera can do: 4K stays offered.+ #[derive(Serialize)]+ struct Before {+ codec: &'static str,+ width: u32,+ height: u32,+ camera: &'static str,+ fps: u32,+ }+ let mut old = Vec::new();+ let before =+ Before { codec: webcam::H264, width: 1920, height: 1080, camera: webcam::CAMERA_FRONT, fps: 30 };+ ciborium::into_writer(&before, &mut old).unwrap();+ assert_eq!(WebcamConfig::from_cbor(&old).unwrap().max_height, 2160);}#[test]diff --git a/desktop/app/qml/pages/HomePage.qml b/desktop/app/qml/pages/HomePage.qmlindex b0516bf..e736fe8 100644--- a/desktop/app/qml/pages/HomePage.qml+++ b/desktop/app/qml/pages/HomePage.qml@@ -1852,11 +1852,11 @@ Item {onPicked: (v) => Webcam.setCamera(v)}Segmented {+ // 4K only when the phone's camera in use can stream it.options: [{ value: "720", label: "720p" },- { value: "1080", label: "1080p" },- { value: "2160", label: "4K" }- ]+ { value: "1080", label: "1080p" }+ ].concat(Webcam.uhd ? [{ value: "2160", label: "4K" }] : [])value: String(Webcam.height)onPicked: (v) => Webcam.setResolution(Number(v))}diff --git a/desktop/app/qml/pages/SettingsPage.qml b/desktop/app/qml/pages/SettingsPage.qmlindex 0d4aeb3..6d5e946 100644--- a/desktop/app/qml/pages/SettingsPage.qml+++ b/desktop/app/qml/pages/SettingsPage.qml@@ -1686,11 +1686,11 @@ Item {iconPath: Icons.cameratitle: qsTr("Resolution")Segmented {+ // 4K only when the phone's camera in use can stream it.options: [{ value: "720", label: "720p" },- { value: "1080", label: "1080p" },- { value: "2160", label: "4K" }- ]+ { value: "1080", label: "1080p" }+ ].concat(Webcam.uhd ? [{ value: "2160", label: "4K" }] : [])value: String(Webcam.height)onPicked: (value) => Webcam.setResolution(Number(value))}diff --git a/desktop/app/src/bridge/webcam.rs b/desktop/app/src/bridge/webcam.rsindex f0bc9ed..2a928ed 100644--- a/desktop/app/src/bridge/webcam.rs+++ b/desktop/app/src/bridge/webcam.rs@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-3.0-or-later//! `Webcam`: QML singleton exposing the Windows virtual camera add-on status,-//! webcam preferences (phone, 720p/1080p, mirror), and live stream state.+//! webcam preferences (phone, 720p, 1080p or 4K when the camera can, mirror),+//! and live stream state.use std::pin::Pin;@@ -28,6 +29,7 @@ pub mod qobject {#[qproperty(QString, selected_phone, cxx_name = "selectedPhone")]#[qproperty(QString, selected_phone_name, cxx_name = "selectedPhoneName")]#[qproperty(i32, height)]+ #[qproperty(bool, uhd)]#[qproperty(QString, camera)]#[qproperty(bool, mirror)]#[qproperty(QString, phase)]@@ -72,6 +74,7 @@ pub struct WebcamRust {selected_phone: QString,selected_phone_name: QString,height: i32,+ uhd: bool,camera: QString,mirror: bool,phase: QString,@@ -103,6 +106,7 @@ impl qobject::Webcam {self.as_mut().set_addon_registered(webcam::addon_registered());self.as_mut().set_addon_busy(webcam::addon_busy());self.as_mut().set_height(webcam::height() as i32);+ self.as_mut().set_uhd(webcam::uhd());self.as_mut().set_camera(QString::from(webcam::camera()));self.as_mut().set_mirror(webcam::mirror());diff --git a/desktop/app/src/webcam.rs b/desktop/app/src/webcam.rsindex b5fbd0b..c8e2a4a 100644--- a/desktop/app/src/webcam.rs+++ b/desktop/app/src/webcam.rs@@ -88,6 +88,9 @@ static STATE: Mutex<Option<State>> = Mutex::new(None);static MIRROR: AtomicBool = AtomicBool::new(false);static CAMERA_FRONT: AtomicBool = AtomicBool::new(false);static HEIGHT: AtomicU32 = AtomicU32::new(720);+/// Whether the phone's camera in use can stream 4K, as its last stream said+/// (offered until a phone says it can't).+static UHD: AtomicBool = AtomicBool::new(true);static PREVIEW_ACTIVE: AtomicBool = AtomicBool::new(false);fn state<T>(f: impl FnOnce(&mut State) -> T) -> T {@@ -250,6 +253,12 @@ pub fn height() -> u32 {HEIGHT.load(Ordering::Relaxed)}+/// Whether to offer 4K: the phone's camera in use can stream it, as far as+/// the PC knows.+pub fn uhd() -> bool {+ UHD.load(Ordering::Relaxed)+}+pub fn set_height(h: u32) {let h = normalize_height(h);let prev = HEIGHT.swap(h, Ordering::Relaxed);@@ -614,6 +623,7 @@ impl Video {impl WebcamSink for Sink {fn config(&self, config: WebcamConfig) {follow_stream(config.width, config.height);+ UHD.store(config.max_height >= 2160, Ordering::Relaxed);state(|s| {s.preferred_phone = Some(self.device);s.phase = Phase::Streaming {diff --git a/docs/protocol/webcam.md b/docs/protocol/webcam.mdindex 00cf5de..4aa6103 100644--- a/docs/protocol/webcam.md+++ b/docs/protocol/webcam.md@@ -86,7 +86,7 @@ Followed by a sequence of framed video packets (`core/nectarlink-protocol/src/vi| `kind` | Name | Payload ||---|---|---|-| `0` | `Config` | CBOR `WebcamConfig` (`{ codec: "h264", width: uint, height: uint, camera: "back" \| "front", fps: uint }`), sent at the start of the stream and whenever resolution or camera changes |+| `0` | `Config` | CBOR `WebcamConfig` (`{ codec: "h264", width: uint, height: uint, camera: "back" \| "front", fps: uint, max_height: uint }`), sent at the start of the stream and whenever resolution or camera changes. `max_height` is the highest resolution the camera in use can stream, by its short side: `2160` when it can do 4K, else `1080`. The PC offers 4K only when it is `2160`; when it is absent (phones before 0.1.5) it counts as `2160`. || `1` | `Frame` | Annex B H.264 non-IDR access unit || `2` | `Keyframe` | Annex B H.264 IDR access unit (preceded by SPS and PPS NAL units) |