fix(screencapturekit): report system-stopped capture streams

This commit is contained in:
fufesou 2026-09-08 23:57:18 +08:00
parent 41c8a2a759
commit eaee235cf4
3 changed files with 123 additions and 5 deletions

View file

@ -268,6 +268,8 @@ pub enum StreamError {
/// The device no longer exists. This can happen if the device is disconnected while the /// The device no longer exists. This can happen if the device is disconnected while the
/// program is running. /// program is running.
DeviceNotAvailable, DeviceNotAvailable,
/// The backend interrupted the stream; recreate it to resume capture.
StreamInterrupted { err: BackendSpecificError },
/// See the [`BackendSpecificError`] docs for more information about this error variant. /// See the [`BackendSpecificError`] docs for more information about this error variant.
BackendSpecific { err: BackendSpecificError }, BackendSpecific { err: BackendSpecificError },
} }
@ -275,7 +277,7 @@ pub enum StreamError {
impl Display for StreamError { impl Display for StreamError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::BackendSpecific { err } => err.fmt(f), Self::BackendSpecific { err } | Self::StreamInterrupted { err } => err.fmt(f),
StreamError::DeviceNotAvailable => f.write_str( StreamError::DeviceNotAvailable => f.write_str(
"The requested device is no longer available. For example, it has been unplugged.", "The requested device is no longer available. For example, it has been unplugged.",
), ),

View file

@ -0,0 +1,105 @@
use std::sync::{Arc, Mutex};
use cidre::sc::stream::{Delegate, DelegateImpl};
use cidre::{define_obj_type, ns, objc, sc};
use crate::{BackendSpecificError, StreamError};
// Stream output and stop notifications can arrive on different queues.
pub(super) type ErrorCallback = Arc<Mutex<Box<dyn FnMut(StreamError) + Send + 'static>>>;
define_obj_type!(pub(super) StreamDelegate + DelegateImpl, ErrorCallback, STREAM_DELEGATE);
impl Delegate for StreamDelegate {}
impl StreamDelegate {
fn report_stop(&self, error: &ns::Error) {
(self.inner().lock().unwrap())(stream_stop_error(error));
}
}
#[objc::add_methods]
impl DelegateImpl for StreamDelegate {
// Stream-stop notifications are separate from audio sample-buffer delivery.
// https://developer.apple.com/documentation/screencapturekit/scstreamdelegate/stream(_:didstopwitherror:)
extern "C" fn impl_stream_did_stop_with_err(
&mut self,
_cmd: Option<&objc::Sel>,
_stream: &sc::Stream,
error: &ns::Error,
) {
self.report_stop(error);
}
}
fn stream_stop_error(error: &ns::Error) -> StreamError {
let err = BackendSpecificError {
description: format!(
"ScreenCaptureKit stopped capture: domain={}, code={}, {}",
error.as_cf().domain(),
error.code(),
error.localized_description(),
),
};
// Only the documented system stop requests recreation. Keep intentional user
// cancellation distinct so callers can respect it instead of restarting capture.
// https://developer.apple.com/documentation/screencapturekit/scstreamerror/systemstoppedstream
// https://developer.apple.com/documentation/screencapturekit/scstreamerror/code/userstopped
if error.as_cf().domain().equal(sc::error::domain())
&& error.code() == sc::error::code::SYSTEM_STOPPED_STREAM
{
StreamError::StreamInterrupted { err }
} else {
StreamError::BackendSpecific { err }
}
}
#[cfg(test)]
mod tests {
use super::*;
use cidre::objc::Obj;
fn capture_error(code: isize) -> cidre::arc::R<ns::Error> {
// NSErrorDomain is an NSString backed by the framework's CFString constant.
let domain = unsafe { std::mem::transmute(sc::error::domain()) };
ns::Error::with_domain(domain, code, None)
}
#[test]
fn system_stop_reaches_error_callback_as_an_interruption() {
let (sender, receiver) = std::sync::mpsc::channel();
let callback: ErrorCallback = Arc::new(Mutex::new(Box::new(move |error| {
sender.send(error).unwrap();
})));
let delegate = StreamDelegate::with(callback);
let selector = unsafe { objc::sel_reg_name(c"stream:didStopWithError:".as_ptr().cast()) };
assert!(delegate.responds_to_sel(selector));
delegate.report_stop(&capture_error(sc::error::code::SYSTEM_STOPPED_STREAM));
let error = receiver.try_recv().unwrap();
assert!(matches!(error, StreamError::StreamInterrupted { .. }));
assert!(error.to_string().contains("code=-3821"));
assert!(error
.to_string()
.contains("com.apple.ScreenCaptureKit.SCStreamErrorDomain"));
}
#[test]
fn explicit_user_stop_is_not_an_automatic_restart_request() {
let error = stream_stop_error(&capture_error(sc::error::code::USER_STOPPED));
assert!(matches!(error, StreamError::BackendSpecific { .. }));
assert!(error.to_string().contains("code=-3817"));
}
#[test]
fn matching_code_from_another_domain_is_not_an_interruption() {
let error = ns::Error::with_domain(
ns::ErrorDomain::cocoa(),
sc::error::code::SYSTEM_STOPPED_STREAM,
None,
);
assert!(matches!(
stream_stop_error(&error),
StreamError::BackendSpecific { .. }
));
}
}

View file

@ -20,6 +20,7 @@ pub use enumerate::{
}; };
pub mod enumerate; pub mod enumerate;
mod delegate;
pub fn is_selector_available(cfg: &sc::StreamCfg) -> bool { pub fn is_selector_available(cfg: &sc::StreamCfg) -> bool {
for name in [c"setCapturesAudio:", c"setExcludesCurrentProcessAudio:"] { for name in [c"setCapturesAudio:", c"setExcludesCurrentProcessAudio:"] {
@ -215,13 +216,20 @@ impl Device {
} }
let windows = ns::Array::new(); let windows = ns::Array::new();
let filter = sc::ContentFilter::with_display_excluding_windows(&self.display, &windows); let filter = sc::ContentFilter::with_display_excluding_windows(&self.display, &windows);
let sc_stream = sc::Stream::new(&filter, &cfg); let error_callback: delegate::ErrorCallback =
std::sync::Arc::new(std::sync::Mutex::new(Box::new(error_callback)));
// ScreenCaptureKit can stop capture while the application keeps running.
// The observed system-stop trigger is unknown; without a delegate, callers
// receive no stop notification and can keep a stream that produces no audio.
// https://developer.apple.com/documentation/screencapturekit/scstream/init(filter:configuration:delegate:)
let delegate = delegate::StreamDelegate::with(error_callback.clone());
let sc_stream = sc::Stream::with_delegate::<(), _>(&filter, &cfg, delegate.as_ref());
let inner = CapturerInner { let inner = CapturerInner {
current_data: vec![], current_data: vec![],
config: config.clone(), config: config.clone(),
sample_format, sample_format,
data_callback: Box::new(data_callback), data_callback: Box::new(data_callback),
error_callback: Box::new(error_callback), error_callback,
}; };
let capturer = Capturer::with(inner); let capturer = Capturer::with(inner);
sc_stream sc_stream
@ -233,6 +241,7 @@ impl Device {
Ok(Stream::new(StreamInner { Ok(Stream::new(StreamInner {
_capturer: capturer, _capturer: capturer,
sc_stream, sc_stream,
_delegate: delegate,
playing: false, playing: false,
})) }))
} }
@ -257,6 +266,8 @@ struct StreamInner {
// Keep capturer alive // Keep capturer alive
_capturer: Retained<Capturer>, _capturer: Retained<Capturer>,
sc_stream: Retained<sc::Stream>, sc_stream: Retained<sc::Stream>,
// Keep the stop callback alive until after sc_stream is released.
_delegate: Retained<delegate::StreamDelegate>,
playing: bool, playing: bool,
} }
@ -327,7 +338,7 @@ struct CapturerInner {
config: StreamConfig, config: StreamConfig,
sample_format: SampleFormat, sample_format: SampleFormat,
data_callback: Box<dyn FnMut(&Data, &InputCallbackInfo) + Send + 'static>, data_callback: Box<dyn FnMut(&Data, &InputCallbackInfo) + Send + 'static>,
error_callback: Box<dyn FnMut(StreamError) + Send + 'static>, error_callback: delegate::ErrorCallback,
} }
impl CapturerInner { impl CapturerInner {
@ -337,7 +348,7 @@ impl CapturerInner {
let buf_list = match sample_buf.audio_buf_list::<2>() { let buf_list = match sample_buf.audio_buf_list::<2>() {
Ok(res) => res, Ok(res) => res,
Err(e) => { Err(e) => {
(self.error_callback)(StreamError::BackendSpecific { (self.error_callback.lock().unwrap())(StreamError::BackendSpecific {
err: BackendSpecificError { err: BackendSpecificError {
description: format!("{e}"), description: format!("{e}"),
}, },