From af96324d1e860fb3167632c4981af13f5e0cdcba Mon Sep 17 00:00:00 2001 From: kess Date: Wed, 14 Oct 2026 00:47:26 -0500 Subject: [PATCH 1/4] feat(camera): scaffold native desktop backend (detection + capability) - add the 'native-desktop' Cargo feature (off by default) with optional GStreamer deps for Linux - add the native_desktop_available command and register it on desktop - frontend: PreviewFrame type, StartPreviewOptions.onFrame, and CameraCapabilities.supportsNativeRecording from the native command The actual capture/preview/record pipeline lands in follow-up commits under the feature. --- Cargo.toml | 10 +++++++ build.rs | 1 + guest-js/index.test.ts | 8 ++++++ guest-js/index.ts | 26 +++++++++++++++++- .../commands/native_desktop_available.toml | 13 +++++++++ permissions/autogenerated/reference.md | 27 +++++++++++++++++++ permissions/default.toml | 1 + permissions/schemas/schema.json | 16 +++++++++-- src/desktop.rs | 19 +++++++++---- src/lib.rs | 10 ++++--- 10 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 permissions/autogenerated/commands/native_desktop_available.toml diff --git a/Cargo.toml b/Cargo.toml index 2f8391b..b084c3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,13 @@ rust-version = "1.77.2" exclude = ["/examples", "/dist-js", "/guest-js", "/node_modules", "/.changes", "/.github"] links = "tauri-plugin-camera-kessdev" +[features] +default = [] +# Native desktop camera backend (capture -> preview frames + native encoder). +# Off by default to avoid requiring the platform dev libraries (e.g. GStreamer +# on Linux) for every build. +native-desktop = ["dep:gstreamer", "dep:gstreamer-app", "dep:glib"] + [dependencies] tauri = { version = "2.11.3" } serde = { version = "1.0", features = ["derive"] } @@ -21,6 +28,9 @@ webview2-com = "0.39" [target.'cfg(target_os = "linux")'.dependencies] webkit2gtk = { version = "2", features = ["v2_40"] } +gstreamer = { version = "0.23", optional = true } +gstreamer-app = { version = "0.23", optional = true } +glib = { version = "0.20", optional = true } [build-dependencies] tauri-plugin = { version = "2.6.3", features = ["build"] } diff --git a/build.rs b/build.rs index eaf7363..5634330 100644 --- a/build.rs +++ b/build.rs @@ -20,6 +20,7 @@ const COMMANDS: &[&str] = &[ "request_permissions", "check_permissions", "open_app_settings", + "native_desktop_available", ]; fn main() { diff --git a/guest-js/index.test.ts b/guest-js/index.test.ts index ffdb9a2..6a5c5a8 100644 --- a/guest-js/index.test.ts +++ b/guest-js/index.test.ts @@ -265,6 +265,14 @@ describe('desktop introspection', () => { expect(caps.focusModes).toEqual(['continuous', 'locked']) }) + it('reports native backend availability in capabilities', async () => { + await stopPreview() + invokeMock.mockResolvedValue(true) + const caps = await getCapabilities() + expect(invokeMock).toHaveBeenCalledWith('plugin:camera-kessdev|native_desktop_available') + expect(caps.supportsNativeRecording).toBe(true) + }) + it('startPreview selects a camera by deviceId on desktop', async () => { const track = { stop: vi.fn() } const stream = { getTracks: () => [track], getVideoTracks: () => [track] } diff --git a/guest-js/index.ts b/guest-js/index.ts index 67acfca..8c9816b 100644 --- a/guest-js/index.ts +++ b/guest-js/index.ts @@ -12,11 +12,24 @@ export { convertFileSrc } export type CameraDirection = 'front' | 'back' export type FlashMode = 'on' | 'off' | 'auto' +/** A preview frame delivered by the native desktop backend. */ +export interface PreviewFrame { + /** Base64-encoded JPEG image data. */ + data: string + width: number + height: number +} + export interface StartPreviewOptions { camera?: CameraDirection /** Specific camera/lens id from `getAvailableCameras()`. Takes precedence over `camera`. */ cameraId?: string windowed?: boolean + /** + * Desktop native backend only: receives live preview frames to render in a + * ``. Ignored by the webview fallback and on mobile. + */ + onFrame?: Channel } export type PhotoFormat = 'jpeg' | 'png' | 'heif' @@ -137,6 +150,8 @@ export interface CameraCapabilities { whiteBalanceModes?: WhiteBalanceMode[] /** Whether an arbitrary color temperature (Kelvin) can be applied. */ supportsWhiteBalanceTemperature?: boolean + /** Whether the native desktop camera backend is available (desktop). */ + supportsNativeRecording?: boolean photoResolutions?: Resolution[] photoAspects?: string[] videoQualities?: VideoQuality[] @@ -538,6 +553,13 @@ export async function getAvailableCameras(): Promise { return enumerateDesktopCameras() } +/** Whether the native desktop camera backend is available for this build. */ +function nativeDesktopAvailable(): Promise { + return Promise.resolve(invoke('plugin:camera-kessdev|native_desktop_available')) + .then((value) => value === true) + .catch(() => false) +} + /** * Returns the declarative capabilities of a camera. * @@ -550,7 +572,9 @@ export async function getCapabilities(cameraId?: string): Promise('plugin:camera-kessdev|get_capabilities', { cameraId }) } - return desktopCapabilities(cameraId) + const caps = desktopCapabilities(cameraId) + caps.supportsNativeRecording = await nativeDesktopAvailable() + return caps } // --------------------------------------------------------------------------- diff --git a/permissions/autogenerated/commands/native_desktop_available.toml b/permissions/autogenerated/commands/native_desktop_available.toml new file mode 100644 index 0000000..46ce8a2 --- /dev/null +++ b/permissions/autogenerated/commands/native_desktop_available.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-native-desktop-available" +description = "Enables the native_desktop_available command without any pre-configured scope." +commands.allow = ["native_desktop_available"] + +[[permission]] +identifier = "deny-native-desktop-available" +description = "Denies the native_desktop_available command without any pre-configured scope." +commands.deny = ["native_desktop_available"] diff --git a/permissions/autogenerated/reference.md b/permissions/autogenerated/reference.md index d6d483a..8df7920 100644 --- a/permissions/autogenerated/reference.md +++ b/permissions/autogenerated/reference.md @@ -25,6 +25,7 @@ Default permissions for the camera plugin - `allow-request-permissions` - `allow-check-permissions` - `allow-open-app-settings` +- `allow-native-desktop-available` ## Permission Table @@ -194,6 +195,32 @@ Denies the get_zoom command without any pre-configured scope. +`camera-kessdev:allow-native-desktop-available` + + + + +Enables the native_desktop_available command without any pre-configured scope. + + + + + + + +`camera-kessdev:deny-native-desktop-available` + + + + +Denies the native_desktop_available command without any pre-configured scope. + + + + + + + `camera-kessdev:allow-open-app-settings` diff --git a/permissions/default.toml b/permissions/default.toml index b140de3..ca7874c 100644 --- a/permissions/default.toml +++ b/permissions/default.toml @@ -22,4 +22,5 @@ permissions = [ "allow-request-permissions", "allow-check-permissions", "allow-open-app-settings", + "allow-native-desktop-available", ] diff --git a/permissions/schemas/schema.json b/permissions/schemas/schema.json index fe24c9b..f047064 100644 --- a/permissions/schemas/schema.json +++ b/permissions/schemas/schema.json @@ -366,6 +366,18 @@ "const": "deny-get-zoom", "markdownDescription": "Denies the get_zoom command without any pre-configured scope." }, + { + "description": "Enables the native_desktop_available command without any pre-configured scope.", + "type": "string", + "const": "allow-native-desktop-available", + "markdownDescription": "Enables the native_desktop_available command without any pre-configured scope." + }, + { + "description": "Denies the native_desktop_available command without any pre-configured scope.", + "type": "string", + "const": "deny-native-desktop-available", + "markdownDescription": "Denies the native_desktop_available command without any pre-configured scope." + }, { "description": "Enables the open_app_settings command without any pre-configured scope.", "type": "string", @@ -547,10 +559,10 @@ "markdownDescription": "Denies the stop_recording command without any pre-configured scope." }, { - "description": "Default permissions for the camera plugin\n#### This default permission set includes:\n\n- `allow-start-preview`\n- `allow-stop-preview`\n- `allow-capture`\n- `allow-flip-camera`\n- `allow-set-flash`\n- `allow-set-zoom`\n- `allow-get-zoom`\n- `allow-set-torch`\n- `allow-set-focus-point`\n- `allow-set-focus-mode`\n- `allow-set-exposure-compensation`\n- `allow-set-exposure-mode`\n- `allow-set-white-balance`\n- `allow-get-available-cameras`\n- `allow-get-capabilities`\n- `allow-start-recording`\n- `allow-stop-recording`\n- `allow-save-to-gallery`\n- `allow-request-permissions`\n- `allow-check-permissions`\n- `allow-open-app-settings`", + "description": "Default permissions for the camera plugin\n#### This default permission set includes:\n\n- `allow-start-preview`\n- `allow-stop-preview`\n- `allow-capture`\n- `allow-flip-camera`\n- `allow-set-flash`\n- `allow-set-zoom`\n- `allow-get-zoom`\n- `allow-set-torch`\n- `allow-set-focus-point`\n- `allow-set-focus-mode`\n- `allow-set-exposure-compensation`\n- `allow-set-exposure-mode`\n- `allow-set-white-balance`\n- `allow-get-available-cameras`\n- `allow-get-capabilities`\n- `allow-start-recording`\n- `allow-stop-recording`\n- `allow-save-to-gallery`\n- `allow-request-permissions`\n- `allow-check-permissions`\n- `allow-open-app-settings`\n- `allow-native-desktop-available`", "type": "string", "const": "default", - "markdownDescription": "Default permissions for the camera plugin\n#### This default permission set includes:\n\n- `allow-start-preview`\n- `allow-stop-preview`\n- `allow-capture`\n- `allow-flip-camera`\n- `allow-set-flash`\n- `allow-set-zoom`\n- `allow-get-zoom`\n- `allow-set-torch`\n- `allow-set-focus-point`\n- `allow-set-focus-mode`\n- `allow-set-exposure-compensation`\n- `allow-set-exposure-mode`\n- `allow-set-white-balance`\n- `allow-get-available-cameras`\n- `allow-get-capabilities`\n- `allow-start-recording`\n- `allow-stop-recording`\n- `allow-save-to-gallery`\n- `allow-request-permissions`\n- `allow-check-permissions`\n- `allow-open-app-settings`" + "markdownDescription": "Default permissions for the camera plugin\n#### This default permission set includes:\n\n- `allow-start-preview`\n- `allow-stop-preview`\n- `allow-capture`\n- `allow-flip-camera`\n- `allow-set-flash`\n- `allow-set-zoom`\n- `allow-get-zoom`\n- `allow-set-torch`\n- `allow-set-focus-point`\n- `allow-set-focus-mode`\n- `allow-set-exposure-compensation`\n- `allow-set-exposure-mode`\n- `allow-set-white-balance`\n- `allow-get-available-cameras`\n- `allow-get-capabilities`\n- `allow-start-recording`\n- `allow-stop-recording`\n- `allow-save-to-gallery`\n- `allow-request-permissions`\n- `allow-check-permissions`\n- `allow-open-app-settings`\n- `allow-native-desktop-available`" } ] } diff --git a/src/desktop.rs b/src/desktop.rs index a72fede..8769c1b 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -10,8 +10,17 @@ pub fn init( /// Access to the camera APIs on desktop. /// -/// On desktop the camera is driven by the webview through -/// `navigator.mediaDevices.getUserMedia` (see `guest-js/index.ts`), so this -/// type carries no capture state and exists only to keep the plugin API -/// symmetric across platforms. -pub struct Camera(AppHandle); +/// By default the camera is driven by the webview through +/// `navigator.mediaDevices.getUserMedia` (see `guest-js/index.ts`). With the +/// `native-desktop` feature the plugin owns the camera natively (capture -> +/// preview frames + native encoder); see `desktop_native`. +pub struct Camera(#[allow(dead_code)] AppHandle); + +/// Whether the native desktop camera backend is compiled into this build. +/// +/// The frontend uses this to decide between the native backend and the webview +/// fallback (`getCapabilities().supportsNativeRecording`). +#[tauri::command] +pub fn native_desktop_available() -> bool { + cfg!(all(target_os = "linux", feature = "native-desktop")) +} diff --git a/src/lib.rs b/src/lib.rs index eff6c33..3e3d027 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ impl> crate::CameraExt for T { /// Initializes the plugin. pub fn init() -> TauriPlugin { - Builder::new("camera-kessdev") + let builder = Builder::new("camera-kessdev") .setup(|app, api| { #[cfg(mobile)] let camera = mobile::init(app, api)?; @@ -51,6 +51,10 @@ pub fn init() -> TauriPlugin { desktop_permissions::install(&webview); #[cfg(not(desktop))] let _ = webview; - }) - .build() + }); + + #[cfg(desktop)] + let builder = builder.invoke_handler(tauri::generate_handler![desktop::native_desktop_available]); + + builder.build() } From 182a22d0dc66e7366046e54eab10f49b76944e03 Mon Sep 17 00:00:00 2001 From: kess Date: Wed, 14 Oct 2026 00:55:53 -0500 Subject: [PATCH 2/4] feat(camera): native desktop camera backend (Linux/GStreamer) - native-desktop feature: capture once, fan out to a live preview (JPEG frames -> Channel) and a native encoder (x264/aac -> mp4), all on GStreamer's own threads so the UI is never blocked - commands native_start_preview/native_stop_preview/native_start_recording/ native_stop_recording registered on desktop under the feature - frontend dispatches to the native backend when available, otherwise the webview fallback; getPreviewStream() returns null with the native backend - README documents the feature; CI installs GStreamer dev libs and checks the feature; change file (minor) --- .changes/native-desktop-recording.md | 10 + .github/workflows/verify.yml | 10 +- Cargo.toml | 3 +- README.md | 31 ++- build.rs | 4 + guest-js/index.test.ts | 37 +++ guest-js/index.ts | 25 +- .../commands/native_start_preview.toml | 13 + .../commands/native_start_recording.toml | 13 + .../commands/native_stop_preview.toml | 13 + .../commands/native_stop_recording.toml | 13 + permissions/autogenerated/reference.md | 108 ++++++++ permissions/default.toml | 4 + permissions/schemas/schema.json | 52 +++- src/desktop.rs | 13 +- src/desktop_native.rs | 235 ++++++++++++++++++ src/lib.rs | 13 +- src/models.rs | 10 + 18 files changed, 598 insertions(+), 9 deletions(-) create mode 100644 .changes/native-desktop-recording.md create mode 100644 permissions/autogenerated/commands/native_start_preview.toml create mode 100644 permissions/autogenerated/commands/native_start_recording.toml create mode 100644 permissions/autogenerated/commands/native_stop_preview.toml create mode 100644 permissions/autogenerated/commands/native_stop_recording.toml create mode 100644 src/desktop_native.rs diff --git a/.changes/native-desktop-recording.md b/.changes/native-desktop-recording.md new file mode 100644 index 0000000..3eaef22 --- /dev/null +++ b/.changes/native-desktop-recording.md @@ -0,0 +1,10 @@ +--- +"tauri-plugin-camera-kessdev": "minor" +"tauri-plugin-camera-kessdev-js": "minor" +--- + +Add an opt-in native desktop camera backend (Cargo feature `native-desktop`); +it owns the camera and captures once, fanning out to a live preview (JPEG frames +delivered to the frontend) and a native encoder, so the UI is not blocked and +`stopRecording()` does not freeze. Expose `startPreview({ onFrame })`, the +`PreviewFrame` type and `getCapabilities().supportsNativeRecording`. diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 9f2bcd0..692278f 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -26,7 +26,9 @@ jobs: librsvg2-dev \ libxdo-dev \ libssl-dev \ - build-essential + build-essential \ + libgstreamer1.0-dev \ + libgstreamer-plugins-base1.0-dev - name: format run: cargo fmt --all -- --check @@ -37,6 +39,12 @@ jobs: - name: test run: cargo test + - name: clippy (native desktop) + run: cargo clippy --features native-desktop --all-targets -- -D warnings + + - name: test (native desktop) + run: cargo test --features native-desktop + - name: check mobile target run: cargo check --target aarch64-linux-android diff --git a/Cargo.toml b/Cargo.toml index b084c3c..ed2aa5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,13 +15,14 @@ default = [] # Native desktop camera backend (capture -> preview frames + native encoder). # Off by default to avoid requiring the platform dev libraries (e.g. GStreamer # on Linux) for every build. -native-desktop = ["dep:gstreamer", "dep:gstreamer-app", "dep:glib"] +native-desktop = ["dep:gstreamer", "dep:gstreamer-app", "dep:glib", "dep:base64"] [dependencies] tauri = { version = "2.11.3" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = "2" +base64 = { version = "0.22", optional = true } [target.'cfg(target_os = "windows")'.dependencies] webview2-com = "0.39" diff --git a/README.md b/README.md index 75293cd..be27b1f 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,33 @@ preview's video track (only the microphone is acquired separately), so the `