pub struct AsyncSCStream { /* private fields */ }Expand description
Async wrapper for SCStream with integrated frame iteration
Provides async methods for stream lifecycle and frame iteration. Executor-agnostic - works with any async runtime.
§Examples
use screencapturekit::async_api::{AsyncSCShareableContent, AsyncSCStream};
use screencapturekit::stream::configuration::SCStreamConfiguration;
use screencapturekit::stream::content_filter::SCContentFilter;
use screencapturekit::stream::output_type::SCStreamOutputType;
let content = AsyncSCShareableContent::get().await?;
let display = &content.displays()[0];
let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
let config = SCStreamConfiguration::new()
.with_width(1920)
.with_height(1080);
let stream = AsyncSCStream::new(&filter, &config, 30, SCStreamOutputType::Screen);
stream.start_capture()?;
// Process frames asynchronously
while let Some(frame) = stream.next().await {
println!("Got frame!");
}Async wrapper for SCStream with integrated frame iteration.
§Back-pressure and frame loss
AsyncSCStream buffers samples in a bounded internal queue sized
by the buffer_capacity argument to AsyncSCStream::new. When the
queue is full and a new sample arrives from ScreenCaptureKit, the
oldest queued sample is dropped to make room — the stream is
lossy by design.
This is the right policy for real-time UI rendering, screen-share
previews, and live encoding: a slow consumer would rather see the
most recent frame than a stale one. It is the wrong policy for
lossless capture (e.g. saving every frame to disk for later
editing) — for that, use the synchronous SCStream
directly, where back-pressure is naturally enforced by Apple’s
queueDepth setting and your handler’s runtime.
To detect when frames are being dropped, watch buffered_count()
against buffer_capacity over time, or instrument your handler
with a per-frame timestamp delta and compare to your expected
frame interval.
Implementations§
Source§impl AsyncSCStream
impl AsyncSCStream
Sourcepub fn new(
filter: &SCContentFilter,
config: &SCStreamConfiguration,
buffer_capacity: usize,
output_type: SCStreamOutputType,
) -> Self
pub fn new( filter: &SCContentFilter, config: &SCStreamConfiguration, buffer_capacity: usize, output_type: SCStreamOutputType, ) -> Self
Create a new async stream
§Arguments
filter- Content filter specifying what to captureconfig- Stream configurationbuffer_capacity- Max frames to buffer (oldest dropped when full)output_type- Type of output (Screen, Audio, Microphone)
Sourcepub fn next(&self) -> NextSample<'_> ⓘ
pub fn next(&self) -> NextSample<'_> ⓘ
Get the next sample buffer asynchronously
Returns None when the stream is closed.
Sourcepub fn try_next(&self) -> Option<CMSampleBuffer>
pub fn try_next(&self) -> Option<CMSampleBuffer>
Try to get a sample without waiting
Sourcepub fn buffered_count(&self) -> usize
pub fn buffered_count(&self) -> usize
Get the number of buffered samples
Sourcepub fn clear_buffer(&self)
pub fn clear_buffer(&self)
Clear all buffered samples
Sourcepub fn start_capture(&self) -> Result<(), SCError>
pub fn start_capture(&self) -> Result<(), SCError>
Start capture (synchronous - returns immediately)
§Errors
Returns an error if capture fails to start.