📹 Client webcam capture: dedicated capture thread with frame rate limiting and network transmission
More...
📹 Client webcam capture: dedicated capture thread with frame rate limiting and network transmission
The capture system follows a producer-consumer pattern:
- Producer: Webcam capture thread reads frames from camera
- Processing: Frame resizing and format conversion pipeline
- Consumer: Network transmission thread sends processed frames
- Rate Limiting: Frame rate control to prevent bandwidth overload
Threading Model
Capture operations run in a dedicated thread:
- Main Thread: Manages thread lifecycle and coordination
- Capture Thread: Continuous frame capture and processing loop
- Synchronization: Atomic flags coordinate thread shutdown
- Resource Management: Clean webcam and memory resource cleanup
Frame Processing Pipeline
Raw webcam frames undergo comprehensive processing:
- Capture: Read raw frame from webcam device
- Validation: Check frame validity and dimensions
- Aspect Ratio: Calculate optimal resize dimensions
- Resizing: Scale frame to network-optimal size (800x600 max)
- Serialization: Pack frame data into network packet format
- Transmission: Send via IMAGE_FRAME packet to server
Frame Rate Management
Implements intelligent frame rate limiting:
- Capture Rate: 144 FPS to support high-refresh displays (~6.9ms intervals)
- Timing Control: Monotonic clock for accurate frame intervals
- Adaptive Delays: Dynamic sleep adjustment for consistent timing
- Display Support: High capture rate enables smooth playback on promotion displays
Image Resizing Strategy
Optimizes frame size for network efficiency:
- Maximum Dimensions: 800x600 pixels for reasonable bandwidth
- Aspect Ratio Preservation: Maintain original camera aspect ratio
- Intelligent Scaling: Fit-to-bounds algorithm for optimal sizing
- Quality Balance: Large enough for server flexibility, small enough for efficiency
Packet Format and Transmission
Frame data serialized for network transmission:
- Header: Width and height as 32-bit network byte order integers
- Payload: RGB pixel data as continuous byte array
- Size Limits: Enforce maximum packet size for protocol compliance
- Error Handling: Graceful handling of oversized frames
Platform Compatibility
Uses webcam abstraction layer for cross-platform support:
- Linux: Video4Linux2 (V4L2) webcam interface
- macOS: AVFoundation framework integration
- Windows: DirectShow API wrapper (stub implementation)
- Fallback: Test pattern generation when webcam unavailable
Integration Points
- main.c: Capture subsystem lifecycle management
- server.c: Network packet transmission and connection monitoring
- webcam.c: Low-level webcam device interface and frame reading
- image.c: Image processing and memory management functions
Error Handling
Capture errors handled with appropriate recovery:
- Device Errors: Webcam unavailable or device busy (continue with warnings)
- Memory Errors: Frame allocation failures (skip frame, continue)
- Network Errors: Transmission failures (trigger connection loss detection)
- Processing Errors: Invalid frames or resize failures (skip and retry)
Resource Management
Careful resource lifecycle management:
- Webcam Device: Proper initialization and cleanup
- Frame Buffers: Automatic memory management with leak prevention
- Thread Resources: Clean thread termination and resource release
- Network Buffers: Efficient packet buffer allocation and cleanup
- Author
- Zachary Fogg me@zf.nosp@m.o.gg
- Date
- 2025
Definition in file capture.c.