Add Real-Time Encoded Frame Streaming and Network Transmission Support #151
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Description
Overview
This pull request introduces a robust, extensible real-time streaming architecture to the Windows Capture library. The new implementation enables direct access to encoded video (and audio) frames in memory, allowing for immediate transmission over network protocols such as TCP and UDP, without the need to write intermediate files. This addresses the requirements outlined in Issue #134 and lays the groundwork for advanced live streaming, remote desktop, and distributed video processing use cases.
Key Features
StreamingVideoEncoder:
A new encoder type that exposes encoded frames as soon as they are available, via a callback interface, rather than persisting them to disk. This enables low-latency, in-memory processing and transmission.
FrameCallback Trait:
A thread-safe, extensible callback interface for handling encoded video and audio frames, as well as stream lifecycle events. This allows users to implement custom frame consumers, including network transmission, in-memory analytics, or integration with external streaming protocols.
Network Transmission Utilities:
Built-in support for TCP and UDP streaming is provided via the
NetworkCallbackand supporting types. These utilities handle connection management, frame serialization, and rate limiting, and are designed for extensibility to support future protocols (e.g., WebRTC, RTMP).Backward Compatibility:
The existing file-based encoding API and usage patterns remain fully supported. The new streaming functionality is opt-in and does not introduce breaking changes.
Technical Details
EncodedFrame and EncodedAudioFrame:
New data structures encapsulate encoded frame data, timestamps, frame type (key, delta, bidirectional), and relevant metadata. This enables precise synchronization and downstream processing.
Threading and Synchronization:
The streaming encoder and callback system are designed for thread safety and high throughput. Internal synchronization primitives ensure that frame delivery and resource cleanup are robust under concurrent workloads.
Error Handling:
All new APIs propagate errors using idiomatic Rust error types, with comprehensive coverage for network failures, encoding errors, and callback exceptions. Resource cleanup is guaranteed even in the presence of errors.
Performance Considerations:
The implementation leverages hardware acceleration where available, minimizes memory copies, and supports configurable frame rates and buffer sizes. Network transmission is rate-limited and supports backpressure.
Extensibility:
The architecture is designed to support additional protocols and custom frame consumers with minimal changes. The callback interface can be implemented for advanced use cases such as WebRTC, RTMP, or custom analytics pipelines.
Usage
StreamingVideoEncodercan be instantiated with a user-definedFrameCallbackimplementation.NetworkCallbacksupports TCP and UDP streaming out of the box.examples/streaming.rs) demonstrates real-time streaming and network transmission.Migration
VideoEncoderwithStreamingVideoEncoderand provide aFrameCallbackimplementation.Testing and Validation
Future Work
Summary
This PR provides a production-grade, extensible foundation for real-time video streaming and network transmission in the Windows Capture library, enabling a wide range of advanced use cases while maintaining backward compatibility and high performance.