Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG 🪲] Using start_free_threaded does not call on_closed #46

Closed
SegaraRai opened this issue May 7, 2024 · 2 comments · Fixed by #47
Closed

[BUG 🪲] Using start_free_threaded does not call on_closed #46

SegaraRai opened this issue May 7, 2024 · 2 comments · Fixed by #47

Comments

@SegaraRai
Copy link
Contributor

Describe the bug 🐛
When starting the capture using GraphicsCaptureApiHandler::start_free_threaded, GraphicsCaptureApiHandler::on_closed, which should be called when the capture ends, such as when the window disappears, is not called. I have confirmed that it is called correctly when starting the capture using GraphicsCaptureApiHandler::start.

Expected behavior 📝
GraphicsCaptureApiHandler::on_closed is called when the capture is finished.

OS 🤖

  • Version: 11
  • Build: 23H2

Additional context ➕

use windows_capture::{
    capture::GraphicsCaptureApiHandler,
    frame::Frame,
    graphics_capture_api::InternalCaptureControl,
    settings::{ColorFormat, CursorCaptureSettings, DrawBorderSettings, Settings},
    window::Window,
};

#[derive(Debug, Default)]
/// A dummy struct to implement the `GraphicsCaptureApiHandler`` trait.
struct Capture;

/// A capture handler implementation for the windows-capture crate.
impl GraphicsCaptureApiHandler for Capture {
    type Flags = ();
    type Error = ();

    fn new(_flags: Self::Flags) -> Result<Self, Self::Error> {
        println!("Capture handler created");
        Ok(Self)
    }

    fn on_frame_arrived(
        &mut self,
        _frame: &mut Frame,
        _control: InternalCaptureControl,
    ) -> Result<(), Self::Error> {
        println!("Frame arrived");
        Ok(())
    }

    fn on_closed(&mut self) -> Result<(), Self::Error> {
        println!("Capture handler closed");
        Ok(())
    }
}

fn main() {
    const TARGET_WINDOW_NAME: &str = "Fork";

    let window = Window::from_contains_name(TARGET_WINDOW_NAME).expect("Failed to find window");

    let settings = Settings::new(
        window,
        CursorCaptureSettings::WithoutCursor,
        DrawBorderSettings::WithoutBorder,
        ColorFormat::Rgba8,
        (),
    );

    // This doesn't return when the window is closed
    let control = Capture::start_free_threaded(settings).expect("Failed to start capture");
    control.wait().expect("Failed to wait for capture"); // For demonstration purposes. We didn't use `wait()` in our actual code.

    // This works
    //Capture::start(settings).expect("Failed to start capture");
}
@SegaraRai
Copy link
Contributor Author

SegaraRai commented May 7, 2024

OK. I compared the differences between start and start_free_threaded visually and was able to make it work properly by applying the following changes. However, I don't fully understand what's happening in the background.
d8db910

I'll submit a PR #47 for now, but since I don't understand it completely, feel free to fix it on your end instead of merging it.
Additionally, feel free to use the code in the PR as you like.

@NiiightmareXD
Copy link
Owner

Hey, thank you for the PR the problem was that the GraphicsCaptureItem should be created in the same capturing thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants