Skip to content

feat(GTK4/Camera): Add camera frame callback support#1648

Closed
Lebei2046 wants to merge 6 commits into
PerryTS:mainfrom
Lebei2046:feature/camera-callback
Closed

feat(GTK4/Camera): Add camera frame callback support#1648
Lebei2046 wants to merge 6 commits into
PerryTS:mainfrom
Lebei2046:feature/camera-callback

Conversation

@Lebei2046
Copy link
Copy Markdown
Contributor

Summary

Adds camera frame callback support to the GTK4 CameraView implementation, enabling real-time frame processing for use cases like QR code detection and computer vision applications.

Description

This PR enhances the Perry UI framework's CameraView widget with frame callback capabilities, allowing developers to process camera frames in real-time. The implementation includes proper thread safety mechanisms to prevent RefCell panics and supports multiple video formats.

Changes Made

Core Implementation:

  • Added register_frame_callback() and unregister_frame_callback() APIs in camera.rs
  • Implemented thread-safe frame processing using GTK idle handler for deferred JavaScript callback execution
  • Added color space conversion utilities supporting YUY2, BGR, BGRA, ABGR, RGB, RGB16, NV12, and I420 formats

Files Modified:

File Description
crates/perry-ui-gtk4/src/camera.rs Main camera implementation with frame callback support
types/perry/ui/index.d.ts TypeScript type definitions

Technical Details

  • Thread Safety: Callbacks are deferred to GTK's idle handler to avoid RefCell borrow conflicts
  • Frame Processing: Runs at ~60fps with format-agnostic color conversion
  • Error Handling: Proper error propagation and graceful degradation when camera is unavailable

Screenshots/Output

A demo application demonstrating the camera callback feature can be found at chatroom-perry, which showcases real-time QR code detection using the new frame callback API.

Testing

  • Camera preview functionality verified on Linux with V4L2 devices
  • Frame callback registration and execution tested with various video formats
  • Thread safety validated under high frame rates

Related Issues

  • Enables QR code detection feature integration
  • Foundation for future computer vision capabilities

Lebei2046 added 6 commits May 20, 2026 13:27
This patch fixes a linking error where the symbol aws_lc_0_40_0_poly_Rq_mul was undefined when building Perry on Linux x86_64.

Root Cause:
- aws-lc-sys-0.40.0 was missing crypto/hrss/asm/poly_rq_mul.S from its Linux x86_64 build configuration
- This file provides the poly_Rq_mul function called from hrss.c

Changes:
- Added crypto/hrss/asm/poly_rq_mul.S to aws-lc-sys-0.40.0's linux_x86_64 build
- Added [patch.crates-io] section to Cargo.toml to use patched aws-lc-sys
- Removed unused build.rs from perry-stdlib (emitted unconsumed directive)

Long-term fix: Upgrade to aws-lc-sys >= 0.41.0 once available
@proggeramlug
Copy link
Copy Markdown
Contributor

Love this, will dig in soon!

@proggeramlug
Copy link
Copy Markdown
Contributor

Superseded by #1940, which is a cleaned-up, rebased version of this work.

The feature itself (GTK4/GStreamer CameraView preview + frame callbacks) is preserved with your authorship via Co-authored-by. The new PR drops the ~2M-line vendored aws-lc-sys-0.40.0 tree that was committed here (the local poly_rq_mul.S build workaround across 1,961 files — unrelated to cameras and the cause of the merge conflict), rewires onto main's current FFI/dispatch layout, fixes a dangling-pointer hazard in the deferred frame callback (now keeps an owned copy of the frame alive for the synchronous JS call), drops two unused deps (lazy_static, gstreamer-video), and regenerates the API docs so CI is green.

Thanks for the contribution! Closing in favor of #1940.

proggeramlug added a commit that referenced this pull request May 27, 2026
Implements the GTK4/GStreamer CameraView backend on Linux: a
`v4l2src → videoconvert → appsink` pipeline drives live preview and
per-frame callbacks for real-time processing (e.g. QR detection).

- New `crates/perry-ui-gtk4/src/camera.rs`: pipeline, color-space
  converters (YUY2/BGR/BGRA/ABGR/RGB/RGB16/NV12/I420), and the
  register/unregister frame-callback API.
- Replaces the Linux camera no-op stubs with real implementations.
- Adds `cameraRegisterFrameCallback` / `cameraUnregisterFrameCallback`
  dispatch rows + manifest entries + perry/ui .d.ts declarations.

Frame bytes handed to the deferred idle callback are an owned copy, kept
alive for the whole synchronous JS call (matching the audio-callback
buffer-lifetime contract) — a borrowed pointer into `last_frame` would
dangle when the next 16ms tick overwrites it before the lower-priority
idle handler runs.

Co-authored-by: Lebei2046 <lebei2046@users.noreply.github.com>
proggeramlug added a commit that referenced this pull request May 27, 2026
…1940)

Implements the GTK4/GStreamer CameraView backend on Linux: a
`v4l2src → videoconvert → appsink` pipeline drives live preview and
per-frame callbacks for real-time processing (e.g. QR detection).

- New `crates/perry-ui-gtk4/src/camera.rs`: pipeline, color-space
  converters (YUY2/BGR/BGRA/ABGR/RGB/RGB16/NV12/I420), and the
  register/unregister frame-callback API.
- Replaces the Linux camera no-op stubs with real implementations.
- Adds `cameraRegisterFrameCallback` / `cameraUnregisterFrameCallback`
  dispatch rows + manifest entries + perry/ui .d.ts declarations.

Frame bytes handed to the deferred idle callback are an owned copy, kept
alive for the whole synchronous JS call (matching the audio-callback
buffer-lifetime contract) — a borrowed pointer into `last_frame` would
dangle when the next 16ms tick overwrites it before the lower-priority
idle handler runs.

Co-authored-by: Lebei2046 <lebei2046@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants