How GraphicsCaptureApiHandler works
mainTo capture screen content, you must implement the GraphicsCaptureApiHandler trait for a custom struct. This trait defines the lifecycle of a capture session through three main methods:
new(ctx: Context<Self::Flags>) -> Result<Self, Self::Error>: Called once to initialize your handler. TheContextcontainsflagswhich are user-defined values (e.g., dimensions) passed from theSettingsobject.on_frame_arrived(&mut self, frame: &mut Frame, capture_control: InternalCaptureControl) -> Result<(), Self::Error>: Called every time a new frame is available. This is where you process frames (e.g., sending them to aVideoEncoder) or decide to stop the capture usingcapture_control.stop().on_closed(&mut self) -> Result<(), Self::Error>: An optional handler called when the capture item (like a window) is closed.
You must also define associated types Flags (data passed to new) and Error (the error type returned by the handler).
impl GraphicsCaptureApiHandler for Capture {
type Flags = (i32, i32);
type Error = Box<dyn std::error::Error + Send + Sync>;
fn new(ctx: Context<Self::Flags>) -> Result<Self, Self::Error> { /* ... */ }
fn on_frame_arrived(&mut self, frame: &mut Frame, capture_control: InternalCaptureControl) -> Result<(), Self::Error> { /* ... */ }
fn on_closed(&mut self) -> Result<(), Self::Error> { /* ... */ }
}