Skip to content

Commit

Permalink
Fix windows capture restart (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrynull authored Feb 7, 2024
1 parent fe6db9a commit 4503802
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
7 changes: 1 addition & 6 deletions src/capture/capturer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 16 additions & 23 deletions src/capture/wgc/wgc_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ use tokio_util::sync::CancellationToken;

pub struct WGCScreenCapture {
config: Config,
engine: CaptureEngine,
engine: Option<CaptureEngine>,
selected_display: Display,
session: Option<GraphicsCaptureSession>,
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);
Expand All @@ -55,7 +55,6 @@ impl CaptureEngine {
)
.unwrap();
Self {
item,
frame_pool,
duplicator,
}
Expand All @@ -67,17 +66,17 @@ impl ScreenCapture for WGCScreenCapture {
fn new(config: Config) -> Result<ScreenCaptureImpl> {
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(
Expand All @@ -87,31 +86,28 @@ 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::<Direct3D11CaptureFrame>(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(())
}
}))?;

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));
Expand Down Expand Up @@ -143,13 +139,16 @@ impl ScreenCapture for WGCScreenCapture {
}
});

self.engine.replace(engine);

Ok(())
}

async fn stop_capture(&mut self) -> Result<()> {
if let Some(session) = self.session.take() {
session.Close()?;
}
self.engine.take();
Ok(())
}
}
Expand All @@ -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(())
}
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4503802

Please sign in to comment.