pub struct SCStream { /* private fields */ }Expand description
SCStream is a lightweight wrapper around the Swift SCStream instance.
It provides direct FFI access to ScreenCaptureKit functionality.
This is the primary and only implementation of SCStream in v1.0+.
All ScreenCaptureKit operations go through Swift FFI bindings.
§Examples
use screencapturekit::prelude::*;
// Get shareable content
let content = SCShareableContent::get()?;
let display = &content.displays()[0];
// Create filter and configuration
let filter = SCContentFilter::builder()
.display(display)
.exclude_windows(&[])
.build();
let mut config = SCStreamConfiguration::default();
config.set_width(1920);
config.set_height(1080);
// Create and start stream
let mut stream = SCStream::new(&filter, &config);
stream.start_capture()?;
// ... capture frames ...
stream.stop_capture()?;Implementations§
Source§impl SCStream
impl SCStream
Sourcepub fn new(
filter: &SCContentFilter,
configuration: &SCStreamConfiguration,
) -> Self
pub fn new( filter: &SCContentFilter, configuration: &SCStreamConfiguration, ) -> Self
Create a new stream with a content filter and configuration
§Panics
Panics if the Swift bridge returns a null stream pointer.
§Examples
use screencapturekit::prelude::*;
let content = SCShareableContent::get()?;
let display = &content.displays()[0];
let filter = SCContentFilter::builder()
.display(display)
.exclude_windows(&[])
.build();
let config = SCStreamConfiguration::default();
let stream = SCStream::new(&filter, &config);pub fn new_with_delegate( filter: &SCContentFilter, configuration: &SCStreamConfiguration, _delegate: impl SCStreamDelegateTrait, ) -> Self
Sourcepub fn add_output_handler(
&mut self,
handler: impl SCStreamOutputTrait + 'static,
of_type: SCStreamOutputType,
) -> Option<usize>
pub fn add_output_handler( &mut self, handler: impl SCStreamOutputTrait + 'static, of_type: SCStreamOutputType, ) -> Option<usize>
Add an output handler to receive captured frames
§Arguments
handler- The handler to receive callbacks. Can be:- A struct implementing
SCStreamOutputTrait - A closure
|CMSampleBuffer, SCStreamOutputType| { ... }
- A struct implementing
of_type- The type of output to receive (Screen, Audio, or Microphone)
§Returns
Returns Some(handler_id) on success, None on failure.
The handler ID can be used with remove_output_handler.
§Examples
Using a struct:
use screencapturekit::prelude::*;
struct MyHandler;
impl SCStreamOutputTrait for MyHandler {
fn did_output_sample_buffer(&self, _sample: CMSampleBuffer, _of_type: SCStreamOutputType) {
println!("Got frame!");
}
}
let mut stream = SCStream::new(&filter, &config);
stream.add_output_handler(MyHandler, SCStreamOutputType::Screen);Using a closure:
use screencapturekit::prelude::*;
let mut stream = SCStream::new(&filter, &config);
stream.add_output_handler(
|_sample, _type| println!("Got frame!"),
SCStreamOutputType::Screen
);Sourcepub fn add_output_handler_with_queue(
&mut self,
handler: impl SCStreamOutputTrait + 'static,
of_type: SCStreamOutputType,
queue: Option<&DispatchQueue>,
) -> Option<usize>
pub fn add_output_handler_with_queue( &mut self, handler: impl SCStreamOutputTrait + 'static, of_type: SCStreamOutputType, queue: Option<&DispatchQueue>, ) -> Option<usize>
Add an output handler with a custom dispatch queue
This allows controlling which thread/queue the handler is called on.
§Arguments
handler- The handler to receive callbacksof_type- The type of output to receivequeue- Optional custom dispatch queue for callbacks
§Panics
Panics if the internal handler registry mutex is poisoned.
§Examples
use screencapturekit::prelude::*;
use screencapturekit::dispatch_queue::{DispatchQueue, DispatchQoS};
let mut stream = SCStream::new(&filter, &config);
let queue = DispatchQueue::new("com.myapp.capture", DispatchQoS::UserInteractive);
stream.add_output_handler_with_queue(
|_sample, _type| println!("Got frame on custom queue!"),
SCStreamOutputType::Screen,
Some(&queue)
);Sourcepub fn remove_output_handler(
&mut self,
id: usize,
_of_type: SCStreamOutputType,
) -> bool
pub fn remove_output_handler( &mut self, id: usize, _of_type: SCStreamOutputType, ) -> bool
Remove an output handler
§Arguments
id- The handler ID returned fromadd_output_handlerof_type- The type of output the handler was registered for
§Panics
Panics if the internal handler registry mutex is poisoned.
§Returns
Returns true if the handler was found and removed, false otherwise.
Sourcepub fn start_capture(&self) -> Result<(), SCError>
pub fn start_capture(&self) -> Result<(), SCError>
Start capturing screen content
This method blocks until the capture operation completes or fails.
§Errors
Returns SCError::CaptureStartFailed if the capture fails to start.
Sourcepub fn stop_capture(&self) -> Result<(), SCError>
pub fn stop_capture(&self) -> Result<(), SCError>
Stop capturing screen content
This method blocks until the capture operation completes or fails.
§Errors
Returns SCError::CaptureStopFailed if the capture fails to stop.
Sourcepub fn update_configuration(
&self,
configuration: &SCStreamConfiguration,
) -> Result<(), SCError>
pub fn update_configuration( &self, configuration: &SCStreamConfiguration, ) -> Result<(), SCError>
Update the stream configuration
This method blocks until the configuration update completes or fails.
§Errors
Returns SCError::StreamError if the configuration update fails.
Sourcepub fn update_content_filter(
&self,
filter: &SCContentFilter,
) -> Result<(), SCError>
pub fn update_content_filter( &self, filter: &SCContentFilter, ) -> Result<(), SCError>
Update the content filter
This method blocks until the filter update completes or fails.
§Errors
Returns SCError::StreamError if the filter update fails.