diff --git a/src/capture/capturer.rs b/src/capture/capturer.rs index f7a8d39..addbf89 100644 --- a/src/capture/capturer.rs +++ b/src/capture/capturer.rs @@ -173,12 +173,7 @@ impl Capturer { } } - fn capture( - &mut self, - args: Args, - config: Config, - #[allow(unused_variables)] shutdown_token: CancellationToken, - ) { + fn capture(&mut self, args: Args, config: Config, shutdown_token: CancellationToken) { let profiler = PerformanceProfiler::new(args.profiler, config.max_fps); let signaller_opt = self.signaller.clone(); let notify_update = self.notify_update.clone(); diff --git a/src/capture/wgc/wgc_capture.rs b/src/capture/wgc/wgc_capture.rs index 644678e..7c51c72 100644 --- a/src/capture/wgc/wgc_capture.rs +++ b/src/capture/wgc/wgc_capture.rs @@ -24,19 +24,19 @@ use tokio_util::sync::CancellationToken; pub struct WGCScreenCapture { config: Config, - engine: CaptureEngine, + engine: Option, selected_display: Display, session: Option, + item: GraphicsCaptureItem, } struct CaptureEngine { - item: GraphicsCaptureItem, frame_pool: Direct3D11CaptureFramePool, duplicator: YuvConverter, } impl CaptureEngine { - fn new(item: GraphicsCaptureItem) -> Self { + fn new(item: &GraphicsCaptureItem) -> Self { let item_size = item.Size().unwrap(); let (device, d3d_device, d3d_context) = d3d::create_direct3d_devices_and_context().unwrap(); let device = Arc::new(device); @@ -55,7 +55,6 @@ impl CaptureEngine { ) .unwrap(); Self { - item, frame_pool, duplicator, } @@ -67,17 +66,17 @@ impl ScreenCapture for WGCScreenCapture { fn new(config: Config) -> Result { let selected_display = Display::online().unwrap()[0].clone(); let item = selected_display.select()?; - let engine = CaptureEngine::new(item); Ok(Self { config, - engine, + engine: None, selected_display, session: None, + item, }) } fn display(&self) -> &dyn DisplayInfo { - &self.engine.item + &self.item } async fn start_capture( @@ -87,23 +86,20 @@ impl ScreenCapture for WGCScreenCapture { mut profiler: PerformanceProfiler, shutdown_token: CancellationToken, ) -> Result<()> { - let session = self - .engine - .frame_pool - .CreateCaptureSession(&self.engine.item)?; + let engine = CaptureEngine::new(&self.item); + + let session = engine.frame_pool.CreateCaptureSession(&self.item)?; let (sender, mut receiver) = tokio::sync::mpsc::channel::(1); - self.engine.frame_pool.FrameArrived(&TypedEventHandler::< + engine.frame_pool.FrameArrived(&TypedEventHandler::< Direct3D11CaptureFramePool, IInspectable, >::new({ move |frame_pool, _| { let frame_pool = frame_pool.as_ref().unwrap(); let frame = frame_pool.TryGetNextFrame()?; - sender - .try_send(frame) - .unwrap_or_else(move |err| warn!("Failed to send frame: {}", err.to_string())); + let _ = sender.try_send(frame); Ok(()) } }))?; @@ -111,7 +107,7 @@ impl ScreenCapture for WGCScreenCapture { session.StartCapture()?; self.session.replace(session); - let mut duplicator = self.engine.duplicator.clone(); + let mut duplicator = engine.duplicator.clone(); let mut ticker = tokio::time::interval(Duration::from_millis((1000 / self.config.max_fps) as u64)); @@ -143,6 +139,8 @@ impl ScreenCapture for WGCScreenCapture { } }); + self.engine.replace(engine); + Ok(()) } @@ -150,6 +148,7 @@ impl ScreenCapture for WGCScreenCapture { if let Some(session) = self.session.take() { session.Close()?; } + self.engine.take(); Ok(()) } } @@ -162,7 +161,7 @@ impl DisplaySelector for WGCScreenCapture { } fn select_display(&mut self, display: &Display) -> Result<()> { - self.engine = CaptureEngine::new(display.select()?); + self.engine = Some(CaptureEngine::new(&display.select()?)); self.selected_display = display.clone(); Ok(()) } @@ -171,9 +170,3 @@ impl DisplaySelector for WGCScreenCapture { Ok(Some(self.selected_display.clone())) } } - -impl Drop for CaptureEngine { - fn drop(&mut self) { - self.frame_pool.Close().unwrap(); - } -} diff --git a/src/main.rs b/src/main.rs index 7c4753b..dd89bfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ async fn main() { }) .level(log::LevelFilter::Info) .level_for("wgpu_core", log::LevelFilter::Warn) - .level_for("wgpu_hal", log::LevelFilter::Warn) + .level_for("wgpu_hal", log::LevelFilter::Off) .level_for("iced_wgpu", log::LevelFilter::Warn) .chain(std::io::stdout()) .apply()