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

Make Capturer thread safe #42

Open
snylonue opened this issue Sep 21, 2021 · 0 comments
Open

Make Capturer thread safe #42

snylonue opened this issue Sep 21, 2021 · 0 comments

Comments

@snylonue
Copy link

I'm using Capturer::frame() in a async code, which blocks other code because I had to wait until a Frame is returned.
I have tried to run such code with smol::unblock()

use scrap::{Capturer as RealCapturer, Display};

pub struct Screenshot {
    pub capturer: RealCapturer,
}

impl Screenshot {
    pub fn new() -> Result<Self, std::io::Error> {
        Ok(Self { capturer: RealCapturer::new(Display::primary()?)? })
    }
    pub fn capturer_size(&self) -> (usize, usize) {
        (self.capturer.width(), self.capturer.height())
    }
    pub async fn capture(&mut self) -> Result<Vec<u8>, std::io::Error> {
        smol::unblock(|| loop {
            match self.capturer.frame() {
                Ok(frame) => break Ok(frame.to_vec()), // sometimes returns an empty frame
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {},
                Err(e) => break Err(e),
            }
        }).await
    }
}

but failed as Capture doesn't implements Send

`*mut winapi::d3d11::ID3D11Device` cannot be sent between threads safely
within `Screenshot`, the trait `std::marker::Send` is not implemented for `*mut winapi::d3d11::ID3D11Device`
required because of the requirements on the impl of `std::marker::Send` for `&mut Screenshot`
1 redundant requirements hidden
required because of the requirements on the impl of `std::marker::Send` for `&mut &mut Screenshot`
required because it appears within the type `[closure@src\screenshot.rs:21:23: 27:10]`
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

No branches or pull requests

1 participant