diff --git a/src/error.rs b/src/error.rs index 6c31aad..24514d8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -268,6 +268,8 @@ pub enum StreamError { /// The device no longer exists. This can happen if the device is disconnected while the /// program is running. 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. BackendSpecific { err: BackendSpecificError }, } @@ -275,7 +277,7 @@ pub enum StreamError { impl Display for StreamError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { - Self::BackendSpecific { err } => err.fmt(f), + Self::BackendSpecific { err } | Self::StreamInterrupted { err } => err.fmt(f), StreamError::DeviceNotAvailable => f.write_str( "The requested device is no longer available. For example, it has been unplugged.", ), diff --git a/src/host/screencapturekit/delegate.rs b/src/host/screencapturekit/delegate.rs new file mode 100644 index 0000000..fdd70b9 --- /dev/null +++ b/src/host/screencapturekit/delegate.rs @@ -0,0 +1,117 @@ +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>>; + +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 { + // 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 mut delegate = StreamDelegate::with(callback); + let selector = unsafe { objc::sel_reg_name(c"stream:didStopWithError:".as_ptr().cast()) }; + assert!(delegate.responds_to_sel(selector)); + // The filter uses NSObject's initializer; no capture session is started. + let filter = unsafe { sc::ContentFilter::cls().new() }; + let stream = + sc::Stream::with_delegate::<(), _>(&filter, &sc::StreamCfg::new(), delegate.as_ref()); + let stop_error = capture_error(sc::error::code::SYSTEM_STOPPED_STREAM); + delegate.stream_did_stop_with_err(&stream, &stop_error); + 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")); + assert!(error + .to_string() + .contains(&stop_error.localized_description().to_string())); + assert!(matches!( + receiver.try_recv(), + Err(std::sync::mpsc::TryRecvError::Empty) + )); + } + + #[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 { .. } + )); + } +} diff --git a/src/host/screencapturekit/mod.rs b/src/host/screencapturekit/mod.rs index 4796a4c..8ce17d7 100644 --- a/src/host/screencapturekit/mod.rs +++ b/src/host/screencapturekit/mod.rs @@ -20,6 +20,7 @@ pub use enumerate::{ }; pub mod enumerate; +mod delegate; pub fn is_selector_available(cfg: &sc::StreamCfg) -> bool { for name in [c"setCapturesAudio:", c"setExcludesCurrentProcessAudio:"] { @@ -215,13 +216,20 @@ impl Device { } let windows = ns::Array::new(); 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 { current_data: vec![], config: config.clone(), sample_format, data_callback: Box::new(data_callback), - error_callback: Box::new(error_callback), + error_callback, }; let capturer = Capturer::with(inner); sc_stream @@ -233,6 +241,7 @@ impl Device { Ok(Stream::new(StreamInner { _capturer: capturer, sc_stream, + _delegate: delegate, playing: false, })) } @@ -257,6 +266,8 @@ struct StreamInner { // Keep capturer alive _capturer: Retained, sc_stream: Retained, + // Keep the stop callback alive until after sc_stream is released. + _delegate: Retained, playing: bool, } @@ -327,7 +338,7 @@ struct CapturerInner { config: StreamConfig, sample_format: SampleFormat, data_callback: Box, - error_callback: Box, + error_callback: delegate::ErrorCallback, } impl CapturerInner { @@ -337,7 +348,7 @@ impl CapturerInner { let buf_list = match sample_buf.audio_buf_list::<2>() { Ok(res) => res, Err(e) => { - (self.error_callback)(StreamError::BackendSpecific { + (self.error_callback.lock().unwrap())(StreamError::BackendSpecific { err: BackendSpecificError { description: format!("{e}"), },