Git Product home page Git Product logo

Comments (11)

NiiightmareXD avatar NiiightmareXD commented on May 19, 2024

where do you call blocking_kind method? the error is from there

from windows-capture.

themujahidkhan avatar themujahidkhan commented on May 19, 2024

Now, I'm getting different errors.

Can you tell me how to start the recording with a library? Which module or function is responsible for recording? How do I access them inside my main.rs file to call it from the front end?

from windows-capture.

NiiightmareXD avatar NiiightmareXD commented on May 19, 2024

Now, I'm getting different errors.

Can you tell me how to start the recording with a library? Which module or function is responsible for recording? How do I access them inside my main.rs file to call it from the front end?

there is a example in readme

from windows-capture.

eythaann avatar eythaann commented on May 19, 2024

Hi, I'm also having problems in tauri, I literally copy and paste the init example and make it a fn and does not works, but my error is:
[2024-03-29][11:13:16][ERROR][komorebi_ui] Screen Capture Failed: FailedToInitWinRT
https://learn.microsoft.com/en-us/windows/win32/api/roapi/nf-roapi-roinitialize

use std::{
    io::{self, Write},
    time::Instant,
};

use windows::Win32::Foundation::HWND;
use windows_capture::{
    capture::GraphicsCaptureApiHandler,
    encoder::{VideoEncoder, VideoEncoderQuality, VideoEncoderType},
    frame::Frame,
    graphics_capture_api::InternalCaptureControl,
    monitor::Monitor,
    settings::{ColorFormat, CursorCaptureSettings, DrawBorderSettings, Settings},
};

// This struct will be used to handle the capture events.
struct Capture {
    // The video encoder that will be used to encode the frames.
    encoder: Option<VideoEncoder>,
    // To measure the time the capture has been running
    start: Instant,
}

impl GraphicsCaptureApiHandler for Capture {
    // The type of flags used to get the values from the settings.
    type Flags = String;

    // The type of error that can occur during capture, the error will be returned from `CaptureControl` and `start` functions.
    type Error = Box<dyn std::error::Error + Send + Sync>;

    // Function that will be called to create the struct. The flags can be passed from settings.
    fn new(message: Self::Flags) -> Result<Self, Self::Error> {
        println!("Got The Flag: {message}");

        let encoder = VideoEncoder::new(
            VideoEncoderType::Mp4,
            VideoEncoderQuality::HD1080p,
            1920,
            1080,
            "video.mp4",
        )?;

        Ok(Self {
            encoder: Some(encoder),
            start: Instant::now(),
        })
    }

    // Called every time a new frame is available.
    fn on_frame_arrived(
        &mut self,
        frame: &mut Frame,
        capture_control: InternalCaptureControl,
    ) -> Result<(), Self::Error> {
        print!(
            "\rRecording for: {} seconds",
            self.start.elapsed().as_secs()
        );
        io::stdout().flush()?;

        // Send the frame to the video encoder
        self.encoder.as_mut().unwrap().send_frame(frame)?;

        // Note: The frame has other uses too for example you can save a single for to a file like this:
        // frame.save_as_image("frame.png", ImageFormat::Png)?;
        // Or get the raw data like this so you have full control:
        // let data = frame.buffer()?;

        // Stop the capture after 6 seconds
        if self.start.elapsed().as_secs() >= 6 {
            // Finish the encoder and save the video.
            self.encoder.take().unwrap().finish()?;

            capture_control.stop();

            // Because there wasn't any new lines in previous prints
            println!();
        }

        Ok(())
    }

    // Optional handler called when the capture item (usually a window) closes.
    fn on_closed(&mut self) -> Result<(), Self::Error> {
        println!("Capture Session Closed");

        Ok(())
    }
}

pub fn capture_window(_hwnd: HWND) -> Result<(), ()> {
    /* let window = Window::from_raw_hwnd(hwnd); */
    let primary_monitor = Monitor::primary().unwrap();

    let settings = Settings::new(
        // Item To Captue
        primary_monitor,
        // Capture Cursor Settings
        CursorCaptureSettings::Default,
        // Draw Borders Settings
        DrawBorderSettings::Default,
        // The desired color format for the captured frame.
        ColorFormat::Rgba8,
        // Additional flags for the capture settings that will be passed to user defined `new` function.
        "Yea This Works".to_string(),
    )
    .unwrap();

    // Starts the capture and takes control of the current thread.
    // The errors from handler trait will end up here
    Capture::start(settings).expect("Screen Capture Failed");
    Ok(())
}

from windows-capture.

NiiightmareXD avatar NiiightmareXD commented on May 19, 2024

Hey, can you check if the start_free_threaded function works or not? also, what is your Windows version?

from windows-capture.

eythaann avatar eythaann commented on May 19, 2024

I'm using win11 23h2, and let me see what I found related to start_free_threaded

from windows-capture.

eythaann avatar eythaann commented on May 19, 2024

hmmm start_free_threaded isn't called in any part of the code I see, maybe is my editor not matching the reference or maybe a macro? but I don't find where start_free_threaded is called.

from windows-capture.

NiiightmareXD avatar NiiightmareXD commented on May 19, 2024

no I mean't instead of Capture::start use Capture::start_free_threaded.

because I think Tauri initializes the WinRT in the main thread itself so you should run the capture in another thread.

Capture::start_free_threaded returns a handle to the capturing thread which you can use to call the struct or get the thread handle and etc.

from windows-capture.

eythaann avatar eythaann commented on May 19, 2024

ohh, let me try this

from windows-capture.

eythaann avatar eythaann commented on May 19, 2024

yeap that is, the thread. but now it freeze on send_frame "2" was never printed and error is not emitted:

self.encoder.as_mut().unwrap().send_frame(frame).expect("Failed to send frame");
println!("\r\n2");

from windows-capture.

NiiightmareXD avatar NiiightmareXD commented on May 19, 2024

Ok... thats weird I have to try to do the same in Tauri

from windows-capture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.