132 lines
3.6 KiB
Rust
132 lines
3.6 KiB
Rust
use std::path::PathBuf;
|
|
use std::time::Instant;
|
|
|
|
use anyhow::{anyhow, Result};
|
|
use windows_capture::capture::{
|
|
CaptureControl, Context as CaptureContext, GraphicsCaptureApiHandler,
|
|
};
|
|
use windows_capture::encoder::{
|
|
AudioSettingsBuilder, ContainerSettingsBuilder, VideoEncoder, VideoSettingsBuilder,
|
|
};
|
|
use windows_capture::frame::Frame;
|
|
use windows_capture::graphics_capture_api::InternalCaptureControl;
|
|
use windows_capture::monitor::Monitor;
|
|
use windows_capture::settings::{
|
|
ColorFormat, CursorCaptureSettings, DirtyRegionSettings, DrawBorderSettings,
|
|
MinimumUpdateIntervalSettings, SecondaryWindowSettings, Settings,
|
|
};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct CaptureSetup {
|
|
pub output_path: PathBuf,
|
|
pub width: u32,
|
|
pub height: u32,
|
|
pub audio: bool,
|
|
}
|
|
|
|
pub struct CaptureHandler {
|
|
encoder: Option<VideoEncoder>,
|
|
}
|
|
|
|
impl GraphicsCaptureApiHandler for CaptureHandler {
|
|
type Flags = CaptureSetup;
|
|
type Error = anyhow::Error;
|
|
|
|
fn new(ctx: CaptureContext<Self::Flags>) -> Result<Self, Self::Error> {
|
|
let setup = ctx.flags;
|
|
let audio = AudioSettingsBuilder::new().disabled(!setup.audio);
|
|
let encoder = VideoEncoder::new(
|
|
VideoSettingsBuilder::new(setup.width, setup.height),
|
|
audio,
|
|
ContainerSettingsBuilder::new(),
|
|
&setup.output_path,
|
|
)
|
|
.map_err(|e| anyhow!("video encoder init: {e}"))?;
|
|
Ok(Self { encoder: Some(encoder) })
|
|
}
|
|
|
|
fn on_frame_arrived(
|
|
&mut self,
|
|
frame: &mut Frame<'_>,
|
|
_capture_control: InternalCaptureControl,
|
|
) -> Result<(), Self::Error> {
|
|
if let Some(enc) = self.encoder.as_mut() {
|
|
enc.send_frame(frame)
|
|
.map_err(|e| anyhow!("send frame: {e}"))?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Drop for CaptureHandler {
|
|
fn drop(&mut self) {
|
|
if let Some(enc) = self.encoder.take() {
|
|
if let Err(e) = enc.finish() {
|
|
tracing::error!("encoder finish failed: {e}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct CaptureSession {
|
|
pub output_path: PathBuf,
|
|
pub started_at: Instant,
|
|
pub width: u32,
|
|
pub height: u32,
|
|
control: CaptureControl<CaptureHandler, anyhow::Error>,
|
|
}
|
|
|
|
pub fn start(output_path: PathBuf, capture_cursor: bool, audio: bool) -> Result<CaptureSession> {
|
|
let monitor = Monitor::primary().map_err(|e| anyhow!("primary monitor: {e:?}"))?;
|
|
let width = monitor
|
|
.width()
|
|
.map_err(|e| anyhow!("monitor width: {e:?}"))?;
|
|
let height = monitor
|
|
.height()
|
|
.map_err(|e| anyhow!("monitor height: {e:?}"))?;
|
|
|
|
let setup = CaptureSetup {
|
|
output_path: output_path.clone(),
|
|
width,
|
|
height,
|
|
audio,
|
|
};
|
|
|
|
let cursor_setting = if capture_cursor {
|
|
CursorCaptureSettings::WithCursor
|
|
} else {
|
|
CursorCaptureSettings::WithoutCursor
|
|
};
|
|
|
|
let settings = Settings::new(
|
|
monitor,
|
|
cursor_setting,
|
|
DrawBorderSettings::Default,
|
|
SecondaryWindowSettings::Default,
|
|
MinimumUpdateIntervalSettings::Default,
|
|
DirtyRegionSettings::Default,
|
|
ColorFormat::Rgba8,
|
|
setup,
|
|
);
|
|
|
|
let control = CaptureHandler::start_free_threaded(settings)
|
|
.map_err(|e| anyhow!("capture start: {e}"))?;
|
|
|
|
Ok(CaptureSession {
|
|
output_path,
|
|
started_at: Instant::now(),
|
|
width,
|
|
height,
|
|
control,
|
|
})
|
|
}
|
|
|
|
impl CaptureSession {
|
|
pub fn stop(self) -> Result<PathBuf> {
|
|
self.control
|
|
.stop()
|
|
.map_err(|e| anyhow!("capture stop: {e}"))?;
|
|
Ok(self.output_path)
|
|
}
|
|
}
|