Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

example rough mediafoundation encoder

Brendan Allandb4950cf062a
diff --git a/Cargo.lock b/Cargo.lock
index 89f2cc83d..bb903171c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6981,6 +6981,7 @@ dependencies = [
"scap-ffmpeg",
"thiserror 1.0.69",
"windows 0.60.0",
+ "windows-numerics 0.2.0",
]
[[package]]
diff --git a/crates/scap-direct3d/Cargo.toml b/crates/scap-direct3d/Cargo.toml
index 5c95171b9..8e2451161 100644
--- a/crates/scap-direct3d/Cargo.toml
+++ b/crates/scap-direct3d/Cargo.toml
@@ -6,32 +6,35 @@ license = "MIT"
[target.'cfg(windows)'.dependencies]
windows = { workspace = true, features = [
- "System",
- "Graphics_Capture",
- "Graphics_DirectX_Direct3D11",
- "Foundation_Metadata",
- "Win32_Graphics_Gdi",
- "Win32_Graphics_Direct3D",
- "Win32_Graphics_Direct3D11",
- "Win32_Graphics_Dxgi_Common",
- "Win32_Graphics_Dxgi",
- "Win32_System_Com",
- "Win32_System_Com_StructuredStorage",
- "Win32_System_Ole",
- "Win32_System_Threading",
- "Win32_System_WinRT_Direct3D11",
- "Win32_System_WinRT_Graphics_Capture",
- "Win32_UI_WindowsAndMessaging",
- "Win32_Media_MediaFoundation",
- "Win32_Media_DxMediaObjects",
- "Win32_Foundation",
- "Foundation_Collections",
- "Win32_System_Variant",
+ "System",
+ "Graphics_Capture",
+ "Graphics_DirectX_Direct3D11",
+ "Foundation_Metadata",
+ "Win32_Graphics_Gdi",
+ "Win32_Graphics_Direct3D",
+ "Win32_Graphics_Direct3D11",
+ "Win32_Graphics_Dxgi_Common",
+ "Win32_Graphics_Dxgi",
+ "Win32_System_Com",
+ "Win32_System_Com_StructuredStorage",
+ "Win32_System_Ole",
+ "Win32_System_Threading",
+ "Win32_System_WinRT_Direct3D11",
+ "Win32_System_WinRT_Graphics_Capture",
+ "Win32_UI_WindowsAndMessaging",
+ "Win32_Media_MediaFoundation",
+ "Win32_Media_DxMediaObjects",
+ "Win32_Foundation",
+ "Foundation_Collections",
+ "Win32_System_Variant",
+ "Storage_Search",
+ "Storage_Streams",
] }
[dev-dependencies]
cap-displays = { path = "../displays" }
scap-ffmpeg = { path = "../scap-ffmpeg" }
+windows-numerics = "0.2.0"
[lints]
workspace = true
diff --git a/crates/scap-direct3d/examples/cli.rs b/crates/scap-direct3d/examples/cli.rs
index 6054dbe08..6655593bb 100644
--- a/crates/scap-direct3d/examples/cli.rs
+++ b/crates/scap-direct3d/examples/cli.rs
@@ -5,16 +5,36 @@ fn main() {
#[cfg(windows)]
mod windows {
+ use ::windows::Graphics::SizeInt32;
+ use ::windows::Storage::FileAccessMode;
+ use ::windows::Win32::Media::MediaFoundation::{MFSTARTUP_FULL, MFStartup};
+ use ::windows::Win32::System::WinRT::{RO_INIT_MULTITHREADED, RoInitialize};
+ use ::windows::Win32::UI::WindowsAndMessaging::{
+ DispatchMessageW, GetMessageW, MSG, WM_HOTKEY,
+ };
+ use ::windows::{
+ Storage::{CreationCollisionOption, StorageFolder},
+ Win32::{Foundation::MAX_PATH, Storage::FileSystem::GetFullPathNameW},
+ core::HSTRING,
+ };
use cap_displays::*;
use scap_direct3d::{Capturer, PixelFormat, Settings};
- use scap_ffmpeg::*;
- use std::time::Duration;
- use windows::Win32::Graphics::Direct3D11::D3D11_BOX;
+ use std::time::Instant;
+ use std::{path::Path, sync::Arc, time::Duration};
+
+ use super::*;
pub fn main() {
+ unsafe {
+ RoInitialize(RO_INIT_MULTITHREADED).unwrap();
+ }
+ unsafe { MFStartup(MF_VERSION, MFSTARTUP_FULL).unwrap() }
+
let display = Display::primary();
let display = display.raw_handle();
+ let (frame_tx, frame_rx) = std::sync::mpsc::channel();
+
let mut capturer = Capturer::new(
display.try_as_capture_item().unwrap(),
Settings {
@@ -34,13 +54,90 @@ mod windows {
)
.unwrap();
+ let mut encoder_devices = VideoEncoderDevice::enumerate().unwrap();
+ let encoder_device = encoder_devices.swap_remove(0);
+
+ let mut video_encoder = VideoEncoder::new(
+ &encoder_device,
+ capturer.d3d_device().clone(),
+ SizeInt32 {
+ Width: 3340,
+ Height: 1440,
+ },
+ SizeInt32 {
+ Width: 3340,
+ Height: 1440,
+ },
+ 12_000_000,
+ 60,
+ )
+ .unwrap();
+ let output_type = video_encoder.output_type().clone();
+
+ // Create our file
+ let path = unsafe {
+ let mut new_path = vec![0u16; MAX_PATH as usize];
+ let length =
+ GetFullPathNameW(&HSTRING::from("recording.mp4"), Some(&mut new_path), None);
+ new_path.resize(length as usize, 0);
+ String::from_utf16(&new_path).unwrap()
+ };
+ let path = Path::new(&path);
+ let parent_folder_path = path.parent().unwrap();
+ let parent_folder = StorageFolder::GetFolderFromPathAsync(&HSTRING::from(
+ parent_folder_path.as_os_str().to_str().unwrap(),
+ ))
+ .unwrap()
+ .get()
+ .unwrap();
+ let file_name = path.file_name().unwrap();
+ let file = parent_folder
+ .CreateFileAsync(
+ &HSTRING::from(file_name.to_str().unwrap()),
+ CreationCollisionOption::ReplaceExisting,
+ )
+ .unwrap()
+ .get()
+ .unwrap();
+
+ let stream = file
+ .OpenAsync(FileAccessMode::ReadWrite)
+ .unwrap()
+ .get()
+ .unwrap();
+
+ video_encoder.set_sample_requested_callback(move || Ok(frame_rx.recv().ok()));
+
+ let sample_writer = Arc::new(SampleWriter::new(stream, &output_type).unwrap());
+ video_encoder.set_sample_rendered_callback({
+ let sample_writer = sample_writer.clone();
+ move |sample| {
+ dbg!(sample.sample());
+ sample_writer.write(sample.sample())
+ }
+ });
+
+ sample_writer.start().unwrap();
+
+ let mut first_timestamp = None;
+
capturer
.start(
- |frame| {
- dbg!(&frame);
+ move |frame| {
+ let frame_time = frame.inner().SystemRelativeTime().unwrap();
+
+ let first_timestamp = first_timestamp.get_or_insert(frame_time);
+
+ let _ = frame_tx.send(VideoEncoderInputSample::new(
+ ::windows::Foundation::TimeSpan {
+ Duration: frame_time.Duration - first_timestamp.Duration,
+ },
+ frame.texture().clone(),
+ ));
+ // dbg!(&frame);
- let ff_frame = frame.as_ffmpeg()?;
- dbg!(ff_frame.width(), ff_frame.height(), ff_frame.format());
+ // let ff_frame = frame.as_ffmpeg()?;
+ // dbg!(ff_frame.width(), ff_frame.height(), ff_frame.format());
Ok(())
},
@@ -48,10 +145,984 @@ mod windows {
)
.unwrap();
- std::thread::sleep(Duration::from_secs(3));
+ video_encoder.try_start().unwrap();
+ std::thread::sleep(Duration::from_secs(10));
+
+ video_encoder.stop().unwrap();
+ sample_writer.stop().unwrap();
capturer.stop().unwrap();
- std::thread::sleep(Duration::from_secs(3));
+ // std::thread::sleep(Duration::from_secs(3));
+ }
+
+ fn pump_messages() -> ::windows::core::Result<()> {
+ // let _hot_key = HotKey::new(MOD_SHIFT | MOD_CONTROL, 0x52 /* R */)?;
+ // println!("Press SHIFT+CTRL+R to start/stop the recording...");
+ let start = Instant::now();
+ unsafe {
+ let mut message = MSG::default();
+ while GetMessageW(&mut message, None, 0, 0).into() {
+ dbg!(message.message);
+ if start.elapsed().as_secs_f64() > 3.0 {
+ break;
+ }
+ DispatchMessageW(&message);
+ }
+ }
+ Ok(())
+ }
+}
+
+use encoder::*;
+mod encoder {
+ use std::{
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ },
+ thread::JoinHandle,
+ };
+
+ use ::windows::{
+ Foundation::TimeSpan,
+ Graphics::SizeInt32,
+ Win32::{
+ Foundation::E_NOTIMPL,
+ Graphics::Direct3D11::{ID3D11Device, ID3D11Texture2D},
+ Media::MediaFoundation::{
+ IMFAttributes, IMFDXGIDeviceManager, IMFMediaEventGenerator, IMFMediaType,
+ IMFSample, IMFTransform, MEDIA_EVENT_GENERATOR_GET_EVENT_FLAGS,
+ METransformHaveOutput, METransformNeedInput, MF_E_INVALIDMEDIATYPE,
+ MF_E_NO_MORE_TYPES, MF_E_TRANSFORM_TYPE_NOT_SET, MF_EVENT_TYPE,
+ MF_MT_ALL_SAMPLES_INDEPENDENT, MF_MT_AVG_BITRATE, MF_MT_FRAME_RATE,
+ MF_MT_FRAME_SIZE, MF_MT_INTERLACE_MODE, MF_MT_MAJOR_TYPE, MF_MT_PIXEL_ASPECT_RATIO,
+ MF_MT_SUBTYPE, MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, MF_TRANSFORM_ASYNC_UNLOCK,
+ MFCreateDXGIDeviceManager, MFCreateDXGISurfaceBuffer, MFCreateMediaType,
+ MFCreateSample, MFMediaType_Video, MFSTARTUP_FULL, MFStartup,
+ MFT_MESSAGE_COMMAND_FLUSH, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING,
+ MFT_MESSAGE_NOTIFY_END_OF_STREAM, MFT_MESSAGE_NOTIFY_END_STREAMING,
+ MFT_MESSAGE_NOTIFY_START_OF_STREAM, MFT_MESSAGE_SET_D3D_MANAGER,
+ MFT_OUTPUT_DATA_BUFFER, MFT_SET_TYPE_TEST_ONLY, MFVideoFormat_H264,
+ MFVideoFormat_NV12, MFVideoInterlace_Progressive,
+ },
+ },
+ core::{Error, Interface, Result},
+ };
+
+ use super::*;
+
+ // use crate::media::{MF_VERSION, MFSetAttributeRatio, MFSetAttributeSize};
+
+ // use super::encoder_device::VideoEncoderDevice;
+
+ pub struct VideoEncoderInputSample {
+ timestamp: TimeSpan,
+ texture: ID3D11Texture2D,
+ }
+
+ impl VideoEncoderInputSample {
+ pub fn new(timestamp: TimeSpan, texture: ID3D11Texture2D) -> Self {
+ Self { timestamp, texture }
+ }
+ }
+
+ pub struct VideoEncoderOutputSample {
+ sample: IMFSample,
+ }
+
+ impl VideoEncoderOutputSample {
+ pub fn sample(&self) -> &IMFSample {
+ &self.sample
+ }
+ }
+
+ pub struct VideoEncoder {
+ inner: Option<VideoEncoderInner>,
+ output_type: IMFMediaType,
+ started: AtomicBool,
+ should_stop: Arc<AtomicBool>,
+ encoder_thread_handle: Option<JoinHandle<Result<()>>>,
+ }
+
+ struct VideoEncoderInner {
+ _d3d_device: ID3D11Device,
+ _media_device_manager: IMFDXGIDeviceManager,
+ _device_manager_reset_token: u32,
+
+ transform: IMFTransform,
+ event_generator: IMFMediaEventGenerator,
+ input_stream_id: u32,
+ output_stream_id: u32,
+
+ sample_requested_callback:
+ Option<Box<dyn Send + FnMut() -> Result<Option<VideoEncoderInputSample>>>>,
+ sample_rendered_callback:
+ Option<Box<dyn Send + FnMut(VideoEncoderOutputSample) -> Result<()>>>,
+
+ should_stop: Arc<AtomicBool>,
+ }
+
+ impl VideoEncoder {
+ pub fn new(
+ encoder_device: &VideoEncoderDevice,
+ d3d_device: ID3D11Device,
+ input_resolution: SizeInt32,
+ output_resolution: SizeInt32,
+ bit_rate: u32,
+ frame_rate: u32,
+ ) -> Result<Self> {
+ let transform = encoder_device.create_transform()?;
+
+ // Create MF device manager
+ let mut device_manager_reset_token: u32 = 0;
+ let media_device_manager = {
+ let mut media_device_manager = None;
+ unsafe {
+ MFCreateDXGIDeviceManager(
+ &mut device_manager_reset_token,
+ &mut media_device_manager,
+ )?
+ };
+ media_device_manager.unwrap()
+ };
+ unsafe { media_device_manager.ResetDevice(&d3d_device, device_manager_reset_token)? };
+
+ // Setup MFTransform
+ let event_generator: IMFMediaEventGenerator = transform.cast()?;
+ let attributes = unsafe { transform.GetAttributes()? };
+ unsafe {
+ attributes.SetUINT32(&MF_TRANSFORM_ASYNC_UNLOCK, 1)?;
+ attributes.SetUINT32(&MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1)?;
+ };
+
+ let mut number_of_input_streams = 0;
+ let mut number_of_output_streams = 0;
+ unsafe {
+ transform
+ .GetStreamCount(&mut number_of_input_streams, &mut number_of_output_streams)?
+ };
+ let (input_stream_ids, output_stream_ids) = {
+ let mut input_stream_ids = vec![0u32; number_of_input_streams as usize];
+ let mut output_stream_ids = vec![0u32; number_of_output_streams as usize];
+ let result = unsafe {
+ transform.GetStreamIDs(&mut input_stream_ids, &mut output_stream_ids)
+ };
+ match result {
+ Ok(_) => {}
+ Err(error) => {
+ // https://docs.microsoft.com/en-us/windows/win32/api/mftransform/nf-mftransform-imftransform-getstreamids
+ // This method can return E_NOTIMPL if both of the following conditions are true:
+ // * The transform has a fixed number of streams.
+ // * The streams are numbered consecutively from 0 to n – 1, where n is the
+ // number of input streams or output streams. In other words, the first
+ // input stream is 0, the second is 1, and so on; and the first output
+ // stream is 0, the second is 1, and so on.
+ if error.code() == E_NOTIMPL {
+ for i in 0..number_of_input_streams {
+ input_stream_ids[i as usize] = i;
+ }
+ for i in 0..number_of_output_streams {
+ output_stream_ids[i as usize] = i;
+ }
+ } else {
+ return Err(error);
+ }
+ }
+ }
+ (input_stream_ids, output_stream_ids)
+ };
+ let input_stream_id = input_stream_ids[0];
+ let output_stream_id = output_stream_ids[0];
+
+ // TOOD: Avoid this AddRef?
+ unsafe {
+ let temp = media_device_manager.clone();
+ transform.ProcessMessage(MFT_MESSAGE_SET_D3D_MANAGER, std::mem::transmute(temp))?;
+ };
+
+ let output_type = unsafe {
+ let output_type = MFCreateMediaType()?;
+ let attributes: IMFAttributes = output_type.cast()?;
+ output_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
+ output_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_H264)?;
+ output_type.SetUINT32(&MF_MT_AVG_BITRATE, bit_rate)?;
+ MFSetAttributeSize(
+ &attributes,
+ &MF_MT_FRAME_SIZE,
+ output_resolution.Width as u32,
+ output_resolution.Height as u32,
+ )?;
+ MFSetAttributeRatio(&attributes, &MF_MT_FRAME_RATE, frame_rate, 1)?;
+ MFSetAttributeRatio(&attributes, &MF_MT_PIXEL_ASPECT_RATIO, 1, 1)?;
+ output_type
+ .SetUINT32(&MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive.0 as u32)?;
+ output_type.SetUINT32(&MF_MT_ALL_SAMPLES_INDEPENDENT, 1)?;
+ transform.SetOutputType(output_stream_id, &output_type, 0)?;
+ output_type
+ };
+ let input_type: Option<IMFMediaType> = unsafe {
+ let mut count = 0;
+ loop {
+ let result = transform.GetInputAvailableType(input_stream_id, count);
+ if let Err(error) = &result {
+ if error.code() == MF_E_NO_MORE_TYPES {
+ break None;
+ }
+ }
+
+ let input_type = result?;
+ let attributes: IMFAttributes = input_type.cast()?;
+ input_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
+ input_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_NV12)?;
+ MFSetAttributeSize(
+ &attributes,
+ &MF_MT_FRAME_SIZE,
+ input_resolution.Width as u32,
+ input_resolution.Height as u32,
+ )?;
+ MFSetAttributeRatio(&attributes, &MF_MT_FRAME_RATE, 60, 1)?;
+ let result = transform.SetInputType(
+ input_stream_id,
+ &input_type,
+ MFT_SET_TYPE_TEST_ONLY.0 as u32,
+ );
+ if let Err(error) = &result {
+ if error.code() == MF_E_INVALIDMEDIATYPE {
+ count += 1;
+ continue;
+ }
+ }
+ result?;
+ break Some(input_type);
+ }
+ };
+ if let Some(input_type) = input_type {
+ unsafe { transform.SetInputType(input_stream_id, &input_type, 0)? };
+ } else {
+ return Err(Error::new(
+ MF_E_TRANSFORM_TYPE_NOT_SET,
+ "No suitable input type found! Try a different set of encoding settings.",
+ ));
+ }
+
+ let should_stop = Arc::new(AtomicBool::new(false));
+ let inner = VideoEncoderInner {
+ _d3d_device: d3d_device,
+ _media_device_manager: media_device_manager,
+ _device_manager_reset_token: device_manager_reset_token,
+
+ transform,
+ event_generator,
+ input_stream_id,
+ output_stream_id,
+
+ sample_requested_callback: None,
+ sample_rendered_callback: None,
+
+ should_stop: should_stop.clone(),
+ };
+
+ Ok(Self {
+ inner: Some(inner),
+ output_type,
+ started: AtomicBool::new(false),
+ should_stop,
+ encoder_thread_handle: None,
+ })
+ }
+
+ pub fn try_start(&mut self) -> Result<bool> {
+ let mut result = false;
+ if self
+ .started
+ .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
+ .is_ok()
+ {
+ let mut inner = self.inner.take().unwrap();
+
+ // Callbacks must both be set
+ if inner.sample_rendered_callback.is_none()
+ || inner.sample_requested_callback.is_none()
+ {
+ panic!("Sample requested and rendered callbacks must be set before starting");
+ }
+
+ // Start a seperate thread to drive the transform
+ self.encoder_thread_handle = Some(std::thread::spawn(move || -> Result<()> {
+ unsafe { MFStartup(MF_VERSION, MFSTARTUP_FULL)? }
+ let result = inner.encode();
+ if result.is_err() {
+ println!("Recording stopped unexpectedly!");
+ }
+ result
+ }));
+ result = true;
+ }
+ Ok(result)
+ }
+
+ pub fn stop(&mut self) -> Result<()> {
+ if self.started.load(Ordering::SeqCst) {
+ assert!(
+ self.should_stop
+ .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
+ .is_ok()
+ );
+ self.wait_for_completion()?;
+ }
+ Ok(())
+ }
+
+ fn wait_for_completion(&mut self) -> Result<()> {
+ let handle = self.encoder_thread_handle.take().unwrap();
+ handle.join().unwrap()
+ }
+
+ pub fn set_sample_requested_callback<
+ F: 'static + Send + FnMut() -> Result<Option<VideoEncoderInputSample>>,
+ >(
+ &mut self,
+ callback: F,
+ ) {
+ self.inner.as_mut().unwrap().sample_requested_callback = Some(Box::new(callback));
+ }
+
+ pub fn set_sample_rendered_callback<
+ F: 'static + Send + FnMut(VideoEncoderOutputSample) -> Result<()>,
+ >(
+ &mut self,
+ callback: F,
+ ) {
+ self.inner.as_mut().unwrap().sample_rendered_callback = Some(Box::new(callback));
+ }
+
+ pub fn output_type(&self) -> &IMFMediaType {
+ &self.output_type
+ }
+ }
+
+ unsafe impl Send for VideoEncoderInner {}
+ // Workaround for:
+ // warning: constant in pattern `METransformNeedInput` should have an upper case name
+ // --> src\video\encoder.rs:XXX:YY
+ // |
+ // XXX | METransformNeedInput => {
+ // | ^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to upper case: `METRANSFORM_NEED_INPUT`
+ // |
+ // = note: `#[warn(non_upper_case_globals)]` on by default
+ const MEDIA_ENGINE_TRANFORM_NEED_INPUT: MF_EVENT_TYPE = METransformNeedInput;
+ const MEDIA_ENGINE_TRANFORM_HAVE_OUTPUT: MF_EVENT_TYPE = METransformHaveOutput;
+ impl VideoEncoderInner {
+ fn encode(&mut self) -> Result<()> {
+ unsafe {
+ self.transform
+ .ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0)?;
+ self.transform
+ .ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)?;
+ self.transform
+ .ProcessMessage(MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0)?;
+
+ let mut should_exit = false;
+ while !should_exit {
+ let event = self
+ .event_generator
+ .GetEvent(MEDIA_EVENT_GENERATOR_GET_EVENT_FLAGS(0))?;
+
+ let event_type = MF_EVENT_TYPE(event.GetType()? as i32);
+ match event_type {
+ MEDIA_ENGINE_TRANFORM_NEED_INPUT => {
+ should_exit = self.on_transform_input_requested()?;
+ }
+ MEDIA_ENGINE_TRANFORM_HAVE_OUTPUT => {
+ self.on_transform_output_ready()?;
+ }
+ _ => {
+ panic!("Unknown media event type: {}", event_type.0);
+ }
+ }
+ }
+
+ self.transform
+ .ProcessMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, 0)?;
+ self.transform
+ .ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, 0)?;
+ self.transform
+ .ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0)?;
+ }
+ Ok(())
+ }
+
+ fn on_transform_input_requested(&mut self) -> Result<bool> {
+ let mut should_exit = true;
+ if !self.should_stop.load(Ordering::SeqCst) {
+ if let Some(sample) = self.sample_requested_callback.as_mut().unwrap()()? {
+ let input_buffer = unsafe {
+ MFCreateDXGISurfaceBuffer(&ID3D11Texture2D::IID, &sample.texture, 0, false)?
+ };
+ let mf_sample = unsafe { MFCreateSample()? };
+ unsafe {
+ mf_sample.AddBuffer(&input_buffer)?;
+ mf_sample.SetSampleTime(sample.timestamp.Duration)?;
+ self.transform
+ .ProcessInput(self.input_stream_id, &mf_sample, 0)?;
+ };
+ should_exit = false;
+ }
+ }
+ Ok(should_exit)
+ }
+
+ fn on_transform_output_ready(&mut self) -> Result<()> {
+ let mut status = 0;
+ let output_buffer = MFT_OUTPUT_DATA_BUFFER {
+ dwStreamID: self.output_stream_id,
+ ..Default::default()
+ };
+
+ let sample = unsafe {
+ let mut output_buffers = [output_buffer];
+ self.transform
+ .ProcessOutput(0, &mut output_buffers, &mut status)?;
+ output_buffers[0].pSample.as_ref().unwrap().clone()
+ };
+
+ let output_sample = VideoEncoderOutputSample { sample };
+ self.sample_rendered_callback.as_mut().unwrap()(output_sample)?;
+ Ok(())
+ }
+ }
+}
+
+use encoder_device::*;
+mod encoder_device {
+ use ::windows::{
+ Win32::Media::MediaFoundation::{
+ IMFActivate, IMFTransform, MFMediaType_Video, MFT_CATEGORY_VIDEO_ENCODER,
+ MFT_ENUM_FLAG_HARDWARE, MFT_ENUM_FLAG_SORTANDFILTER, MFT_ENUM_FLAG_TRANSCODE_ONLY,
+ MFT_FRIENDLY_NAME_Attribute, MFT_REGISTER_TYPE_INFO, MFVideoFormat_H264,
+ },
+ core::{Interface, Result},
+ };
+
+ use super::*;
+
+ #[derive(Clone)]
+ pub struct VideoEncoderDevice {
+ source: IMFActivate,
+ display_name: String,
+ }
+
+ impl VideoEncoderDevice {
+ pub fn enumerate() -> Result<Vec<VideoEncoderDevice>> {
+ let output_info = MFT_REGISTER_TYPE_INFO {
+ guidMajorType: MFMediaType_Video,
+ guidSubtype: MFVideoFormat_H264,
+ };
+ let encoders = enumerate_mfts(
+ &MFT_CATEGORY_VIDEO_ENCODER,
+ MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_TRANSCODE_ONLY | MFT_ENUM_FLAG_SORTANDFILTER,
+ None,
+ Some(&output_info),
+ )?;
+ let mut encoder_devices = Vec::new();
+ for encoder in encoders {
+ let display_name = if let Some(display_name) =
+ get_string_attribute(&encoder.cast()?, &MFT_FRIENDLY_NAME_Attribute)?
+ {
+ display_name
+ } else {
+ "Unknown".to_owned()
+ };
+ let encoder_device = VideoEncoderDevice {
+ source: encoder,
+ display_name,
+ };
+ encoder_devices.push(encoder_device);
+ }
+ Ok(encoder_devices)
+ }
+
+ pub fn display_name(&self) -> &str {
+ &self.display_name
+ }
+
+ pub fn create_transform(&self) -> Result<IMFTransform> {
+ unsafe { self.source.ActivateObject() }
+ }
+ }
+}
+
+use media::*;
+mod media {
+ use ::windows::{
+ Win32::Media::MediaFoundation::{
+ IMFActivate, IMFAttributes, MF_E_ATTRIBUTENOTFOUND, MFT_ENUM_FLAG,
+ MFT_REGISTER_TYPE_INFO, MFTEnumEx,
+ },
+ core::{Array, GUID, Result},
+ };
+
+ use super::*;
+
+ pub fn enumerate_mfts(
+ category: &GUID,
+ flags: MFT_ENUM_FLAG,
+ input_type: Option<&MFT_REGISTER_TYPE_INFO>,
+ output_type: Option<&MFT_REGISTER_TYPE_INFO>,
+ ) -> Result<Vec<IMFActivate>> {
+ let mut transform_sources = Vec::new();
+ let mfactivate_list = unsafe {
+ let mut data = std::ptr::null_mut();
+ let mut len = 0;
+ MFTEnumEx(
+ *category,
+ flags,
+ input_type.map(|info| info as *const _),
+ output_type.map(|info| info as *const _),
+ &mut data,
+ &mut len,
+ )?;
+ Array::<IMFActivate>::from_raw_parts(data as _, len)
+ };
+ if !mfactivate_list.is_empty() {
+ for mfactivate in mfactivate_list.as_slice() {
+ let transform_source = mfactivate.clone().unwrap();
+ transform_sources.push(transform_source);
+ }
+ }
+ Ok(transform_sources)
+ }
+
+ pub fn get_string_attribute(
+ attributes: &IMFAttributes,
+ attribute_guid: &GUID,
+ ) -> Result<Option<String>> {
+ unsafe {
+ match attributes.GetStringLength(attribute_guid) {
+ Ok(mut length) => {
+ let mut result = vec![0u16; (length + 1) as usize];
+ attributes.GetString(attribute_guid, &mut result, Some(&mut length))?;
+ result.resize(length as usize, 0);
+ Ok(Some(String::from_utf16(&result).unwrap()))
+ }
+ Err(error) => {
+ if error.code() == MF_E_ATTRIBUTENOTFOUND {
+ Ok(None)
+ } else {
+ Err(error)
+ }
+ }
+ }
+ }
+ }
+
+ // These inlined helpers aren't represented in the metadata
+
+ // This is the value for Win7+
+ pub const MF_VERSION: u32 = 131184;
+
+ fn pack_2_u32_as_u64(high: u32, low: u32) -> u64 {
+ ((high as u64) << 32) | low as u64
+ }
+
+ #[allow(non_snake_case)]
+ unsafe fn MFSetAttribute2UINT32asUINT64(
+ attributes: &IMFAttributes,
+ key: &GUID,
+ high: u32,
+ low: u32,
+ ) -> Result<()> {
+ unsafe { attributes.SetUINT64(key, pack_2_u32_as_u64(high, low)) }
+ }
+
+ #[allow(non_snake_case)]
+ pub unsafe fn MFSetAttributeSize(
+ attributes: &IMFAttributes,
+ key: &GUID,
+ width: u32,
+ height: u32,
+ ) -> Result<()> {
+ unsafe { MFSetAttribute2UINT32asUINT64(attributes, key, width, height) }
+ }
+
+ #[allow(non_snake_case)]
+ pub unsafe fn MFSetAttributeRatio(
+ attributes: &IMFAttributes,
+ key: &GUID,
+ numerator: u32,
+ denominator: u32,
+ ) -> Result<()> {
+ unsafe { MFSetAttribute2UINT32asUINT64(attributes, key, numerator, denominator) }
+ }
+}
+
+use encoding_session::*;
+mod encoding_session {
+
+ use std::sync::Arc;
+
+ use ::windows::{
+ Foundation::TimeSpan,
+ Graphics::{
+ Capture::{Direct3D11CaptureFrame, GraphicsCaptureItem, GraphicsCaptureSession},
+ SizeInt32,
+ },
+ Storage::Streams::IRandomAccessStream,
+ Win32::{
+ Graphics::{
+ Direct3D11::{
+ D3D11_BIND_RENDER_TARGET, D3D11_BIND_SHADER_RESOURCE, D3D11_BOX,
+ D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, ID3D11Device, ID3D11DeviceContext,
+ ID3D11RenderTargetView, ID3D11Texture2D,
+ },
+ Dxgi::Common::{DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_NV12, DXGI_SAMPLE_DESC},
+ },
+ Media::MediaFoundation::{
+ IMFMediaType, IMFSample, IMFSinkWriter, MFCreateAttributes,
+ MFCreateMFByteStreamOnStreamEx, MFCreateSinkWriterFromURL,
+ },
+ System::WinRT::Direct3D11::IDirect3DDxgiInterfaceAccess,
+ },
+ core::{HSTRING, Interface, Result},
+ };
+
+ use super::*;
+
+ // capture::CaptureFrameGenerator,
+ // d3d::get_d3d_interface_from_object,
+ // video::{
+ // CLEAR_COLOR,
+ // encoding_session::{VideoEncoderSessionFactory, VideoEncodingSession},
+ // util::ensure_even_size,
+ // },
+ // };
+
+ // use super::{
+ // encoder::{VideoEncoder, VideoEncoderInputSample},
+ // encoder_device::VideoEncoderDevice,
+ // processor::VideoProcessor,
+ // };
+
+ pub struct SampleWriter {
+ _stream: IRandomAccessStream,
+ sink_writer: IMFSinkWriter,
+ sink_writer_stream_index: u32,
+ }
+
+ unsafe impl Send for SampleWriter {}
+ unsafe impl Sync for SampleWriter {}
+ impl SampleWriter {
+ pub fn new(stream: IRandomAccessStream, output_type: &IMFMediaType) -> Result<Self> {
+ let empty_attributes = unsafe {
+ let mut attributes = None;
+ MFCreateAttributes(&mut attributes, 0)?;
+ attributes.unwrap()
+ };
+ let sink_writer = unsafe {
+ let byte_stream = MFCreateMFByteStreamOnStreamEx(&stream)?;
+ MFCreateSinkWriterFromURL(&HSTRING::from(".mp4"), &byte_stream, &empty_attributes)?
+ };
+ let sink_writer_stream_index = unsafe { sink_writer.AddStream(output_type)? };
+ unsafe {
+ sink_writer.SetInputMediaType(
+ sink_writer_stream_index,
+ output_type,
+ &empty_attributes,
+ )?
+ };
+
+ Ok(Self {
+ _stream: stream,
+ sink_writer,
+ sink_writer_stream_index,
+ })
+ }
+
+ pub fn start(&self) -> Result<()> {
+ unsafe { self.sink_writer.BeginWriting() }
+ }
+
+ pub fn stop(&self) -> Result<()> {
+ unsafe { self.sink_writer.Finalize() }
+ }
+
+ pub fn write(&self, sample: &IMFSample) -> Result<()> {
+ unsafe {
+ self.sink_writer
+ .WriteSample(self.sink_writer_stream_index, sample)
+ }
+ }
+ }
+
+ pub fn get_d3d_interface_from_object<S: Interface, R: Interface>(object: &S) -> Result<R> {
+ let access: IDirect3DDxgiInterfaceAccess = object.cast()?;
+ let object = unsafe { access.GetInterface::<R>()? };
+ Ok(object)
+ }
+}
+
+use processor::*;
+mod processor {
+ use ::windows::{
+ Graphics::{RectInt32, SizeInt32},
+ Win32::{
+ Foundation::RECT,
+ Graphics::{
+ Direct3D11::{
+ D3D11_BIND_RENDER_TARGET, D3D11_BIND_SHADER_RESOURCE, D3D11_BIND_VIDEO_ENCODER,
+ D3D11_TEX2D_VPIV, D3D11_TEX2D_VPOV, D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT,
+ D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE, D3D11_VIDEO_PROCESSOR_COLOR_SPACE,
+ D3D11_VIDEO_PROCESSOR_CONTENT_DESC, D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC,
+ D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC_0,
+ D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_0_255,
+ D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_16_235,
+ D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC,
+ D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC_0, D3D11_VIDEO_PROCESSOR_STREAM,
+ D3D11_VIDEO_USAGE_OPTIMAL_QUALITY, D3D11_VPIV_DIMENSION_TEXTURE2D,
+ D3D11_VPOV_DIMENSION_TEXTURE2D, ID3D11Device, ID3D11DeviceContext,
+ ID3D11Texture2D, ID3D11VideoContext, ID3D11VideoDevice, ID3D11VideoProcessor,
+ ID3D11VideoProcessorInputView, ID3D11VideoProcessorOutputView,
+ },
+ Dxgi::Common::{DXGI_FORMAT, DXGI_RATIONAL, DXGI_SAMPLE_DESC},
+ },
+ },
+ core::{Interface, Result},
+ };
+ use windows_numerics::Vector2;
+
+ pub struct VideoProcessor {
+ _d3d_device: ID3D11Device,
+ d3d_context: ID3D11DeviceContext,
+
+ _video_device: ID3D11VideoDevice,
+ video_context: ID3D11VideoContext,
+ video_processor: ID3D11VideoProcessor,
+ video_output_texture: ID3D11Texture2D,
+ video_output: ID3D11VideoProcessorOutputView,
+ video_input_texture: ID3D11Texture2D,
+ video_input: ID3D11VideoProcessorInputView,
+ }
+
+ impl VideoProcessor {
+ pub fn new(
+ d3d_device: ID3D11Device,
+ input_format: DXGI_FORMAT,
+ input_size: SizeInt32,
+ output_format: DXGI_FORMAT,
+ output_size: SizeInt32,
+ ) -> Result<Self> {
+ let d3d_context = unsafe { d3d_device.GetImmediateContext()? };
+
+ // Setup video conversion
+ let video_device: ID3D11VideoDevice = d3d_device.cast()?;
+ let video_context: ID3D11VideoContext = d3d_context.cast()?;
+
+ let video_desc = D3D11_VIDEO_PROCESSOR_CONTENT_DESC {
+ InputFrameFormat: D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE,
+ InputFrameRate: DXGI_RATIONAL {
+ Numerator: 60,
+ Denominator: 1,
+ },
+ InputWidth: input_size.Width as u32,
+ InputHeight: input_size.Height as u32,
+ OutputFrameRate: DXGI_RATIONAL {
+ Numerator: 60,
+ Denominator: 1,
+ },
+ OutputWidth: input_size.Width as u32,
+ OutputHeight: input_size.Height as u32,
+ Usage: D3D11_VIDEO_USAGE_OPTIMAL_QUALITY,
+ };
+ let video_enum = unsafe { video_device.CreateVideoProcessorEnumerator(&video_desc)? };
+
+ let video_processor = unsafe { video_device.CreateVideoProcessor(&video_enum, 0)? };
+
+ let mut color_space = D3D11_VIDEO_PROCESSOR_COLOR_SPACE {
+ _bitfield: 1 | (D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_16_235.0 as u32) << 4, // Usage: 1 (Video processing), Nominal_Range: D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_16_235
+ };
+ unsafe {
+ video_context.VideoProcessorSetOutputColorSpace(&video_processor, &color_space)
+ };
+ color_space._bitfield = 1 | (D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_0_255.0 as u32) << 4; // Usage: 1 (Video processing), Nominal_Range: D3D11_VIDEO_PROCESSOR_NOMINAL_RANGE_0_255
+ unsafe {
+ video_context.VideoProcessorSetStreamColorSpace(&video_processor, 0, &color_space)
+ };
+
+ // If the input and output resolutions don't match, setup the
+ // video processor to preserve the aspect ratio when scaling.
+ if input_size.Width != output_size.Width || input_size.Height != output_size.Height {
+ let dest_rect = compute_dest_rect(&output_size, &input_size);
+ let rect = RECT {
+ left: dest_rect.X,
+ top: dest_rect.Y,
+ right: dest_rect.X + dest_rect.Width,
+ bottom: dest_rect.Y + dest_rect.Height,
+ };
+ unsafe {
+ video_context.VideoProcessorSetStreamDestRect(
+ &video_processor,
+ 0,
+ true,
+ Some(&rect),
+ )
+ };
+ }
+
+ let mut texture_desc = D3D11_TEXTURE2D_DESC {
+ Width: output_size.Width as u32,
+ Height: output_size.Height as u32,
+ ArraySize: 1,
+ MipLevels: 1,
+ Format: output_format,
+ SampleDesc: DXGI_SAMPLE_DESC {
+ Count: 1,
+ ..Default::default()
+ },
+ Usage: D3D11_USAGE_DEFAULT,
+ BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_VIDEO_ENCODER.0) as u32,
+ ..Default::default()
+ };
+ let video_output_texture = unsafe {
+ let mut texture = None;
+ d3d_device.CreateTexture2D(&texture_desc, None, Some(&mut texture))?;
+ texture.unwrap()
+ };
+
+ let output_view_desc = D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC {
+ ViewDimension: D3D11_VPOV_DIMENSION_TEXTURE2D,
+ Anonymous: D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC_0 {
+ Texture2D: D3D11_TEX2D_VPOV { MipSlice: 0 },
+ },
+ };
+ let video_output = unsafe {
+ let mut output = None;
+ video_device.CreateVideoProcessorOutputView(
+ &video_output_texture,
+ &video_enum,
+ &output_view_desc,
+ Some(&mut output),
+ )?;
+ output.unwrap()
+ };
+
+ texture_desc.Width = input_size.Width as u32;
+ texture_desc.Height = input_size.Height as u32;
+ texture_desc.Format = input_format;
+ texture_desc.BindFlags =
+ (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32;
+ let video_input_texture = unsafe {
+ let mut texture = None;
+ d3d_device.CreateTexture2D(&texture_desc, None, Some(&mut texture))?;
+ texture.unwrap()
+ };
+
+ let input_view_desc = D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC {
+ ViewDimension: D3D11_VPIV_DIMENSION_TEXTURE2D,
+ Anonymous: D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC_0 {
+ Texture2D: D3D11_TEX2D_VPIV {
+ MipSlice: 0,
+ ..Default::default()
+ },
+ },
+ ..Default::default()
+ };
+ let video_input = unsafe {
+ let mut input = None;
+ video_device.CreateVideoProcessorInputView(
+ &video_input_texture,
+ &video_enum,
+ &input_view_desc,
+ Some(&mut input),
+ )?;
+ input.unwrap()
+ };
+
+ Ok(Self {
+ _d3d_device: d3d_device,
+ d3d_context,
+
+ _video_device: video_device,
+ video_context,
+ video_processor,
+ video_output_texture,
+ video_output,
+ video_input_texture,
+ video_input,
+ })
+ }
+
+ pub fn output_texture(&self) -> &ID3D11Texture2D {
+ &self.video_output_texture
+ }
+
+ pub fn process_texture(&mut self, input_texture: &ID3D11Texture2D) -> Result<()> {
+ // The caller is responsible for making sure they give us a
+ // texture that matches the input size we were initialized with.
+
+ unsafe {
+ // Copy the texture to the video input texture
+ self.d3d_context
+ .CopyResource(&self.video_input_texture, input_texture);
+
+ // Convert to NV12
+ let video_stream = D3D11_VIDEO_PROCESSOR_STREAM {
+ Enable: true.into(),
+ OutputIndex: 0,
+ InputFrameOrField: 0,
+ pInputSurface: std::mem::transmute_copy(&self.video_input),
+ ..Default::default()
+ };
+ self.video_context.VideoProcessorBlt(
+ &self.video_processor,
+ &self.video_output,
+ 0,
+ &[video_stream],
+ )
+ }
+ }
+ }
+
+ fn compute_scale_factor(output_size: Vector2, input_size: Vector2) -> f32 {
+ let output_ratio = output_size.X / output_size.Y;
+ let input_ratio = input_size.X / input_size.Y;
+
+ let mut scale_factor = output_size.X / input_size.X;
+ if output_ratio > input_ratio {
+ scale_factor = output_size.Y / input_size.Y;
+ }
+
+ scale_factor
+ }
+
+ fn compute_dest_rect(output_size: &SizeInt32, input_size: &SizeInt32) -> RectInt32 {
+ let scale = compute_scale_factor(
+ Vector2 {
+ X: output_size.Width as f32,
+ Y: output_size.Height as f32,
+ },
+ Vector2 {
+ X: input_size.Width as f32,
+ Y: input_size.Height as f32,
+ },
+ );
+ let new_size = SizeInt32 {
+ Width: (input_size.Width as f32 * scale) as i32,
+ Height: (input_size.Height as f32 * scale) as i32,
+ };
+ let mut offset_x = 0;
+ let mut offset_y = 0;
+ if new_size.Width != output_size.Width {
+ offset_x = (output_size.Width - new_size.Width) / 2;
+ }
+ if new_size.Height != output_size.Height {
+ offset_y = (output_size.Height - new_size.Height) / 2;
+ }
+ RectInt32 {
+ X: offset_x,
+ Y: offset_y,
+ Width: new_size.Width,
+ Height: new_size.Height,
+ }
}
}
diff --git a/crates/scap-direct3d/examples/mf_encoder.rs b/crates/scap-direct3d/examples/mf_encoder.rs
index 890bbad6a..6054dbe08 100644
--- a/crates/scap-direct3d/examples/mf_encoder.rs
+++ b/crates/scap-direct3d/examples/mf_encoder.rs
@@ -7,31 +7,11 @@ fn main() {
mod windows {
use cap_displays::*;
use scap_direct3d::{Capturer, PixelFormat, Settings};
- use std::sync::mpsc;
- use std::thread;
- use std::time::{Duration, Instant};
- use windows::Win32::Graphics::Direct3D11::*;
- use windows::Win32::Graphics::Dxgi::*;
- use windows::{
- Win32::{Foundation::*, Media::MediaFoundation::*, System::Com::*},
- core::*,
- };
-
- #[derive(Debug)]
- enum EncoderMessage {
- Frame {
- texture: ID3D11Texture2D,
- timestamp: u64,
- },
- Stop,
- }
+ use scap_ffmpeg::*;
+ use std::time::Duration;
+ use windows::Win32::Graphics::Direct3D11::D3D11_BOX;
pub fn main() {
- unsafe {
- CoInitializeEx(None, COINIT_APARTMENTTHREADED).unwrap();
- MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET).unwrap();
- }
-
let display = Display::primary();
let display = display.raw_handle();
@@ -41,278 +21,37 @@ mod windows {
is_border_required: Some(false),
is_cursor_capture_enabled: Some(true),
pixel_format: PixelFormat::R8G8B8A8Unorm,
+ // crop: Some(D3D11_BOX {
+ // left: 0,
+ // top: 0,
+ // right: 500,
+ // bottom: 400,
+ // front: 0,
+ // back: 1,
+ // }),
..Default::default()
},
)
.unwrap();
- let (tx, rx) = mpsc::channel::<EncoderMessage>();
- let mut encoder_thread_handle = None;
- let start_time = Instant::now();
- let mut frame_count = 0u64;
-
- let mut rx = Some(rx);
-
capturer
.start(
- {
- let tx = tx.clone();
- move |frame| {
- // Start encoder thread on first frame
- if encoder_thread_handle.is_none()
- && let Some(rx) = rx.take()
- {
- let thread_rx = rx;
- let width = frame.width();
- let height = frame.height();
- let d3d_device = frame.d3d_device().clone();
- let d3d_context = frame.d3d_context().clone();
-
- encoder_thread_handle = Some(thread::spawn(move || {
- encoder_thread_main(
- width,
- height,
- d3d_device,
- d3d_context,
- thread_rx,
- )
- }));
- }
-
- let timestamp = frame_count * 10_000_000 / 30; // 100ns units
+ |frame| {
+ dbg!(&frame);
- // Clone the texture to send it to the encoder thread
- let texture = frame.texture().clone();
+ let ff_frame = frame.as_ffmpeg()?;
+ dbg!(ff_frame.width(), ff_frame.height(), ff_frame.format());
- // Send frame to encoder thread
- let _ = tx
- .send(EncoderMessage::Frame { texture, timestamp })
- .map_err(|e| format!("Failed to send frame to encoder: {}", e));
-
- frame_count += 1;
- Ok(())
- }
+ Ok(())
},
|| Ok(()),
)
.unwrap();
- std::thread::sleep(Duration::from_secs(10));
+ std::thread::sleep(Duration::from_secs(3));
capturer.stop().unwrap();
- // Signal encoder thread to stop
- tx.send(EncoderMessage::Stop).unwrap();
- drop(tx); // Close the channel
-
- // Wait for encoder thread to finish
- // if let Some(handle) = encoder_thread_handle {
- // handle.join().expect("Encoder thread panicked");
- // }
-
- println!(
- "Encoded {} frames in {:?} using hardware encoding",
- frame_count,
- start_time.elapsed()
- );
-
- unsafe {
- MFShutdown().unwrap();
- CoUninitialize();
- }
- }
-
- fn encoder_thread_main(
- width: u32,
- height: u32,
- d3d_device: ID3D11Device,
- d3d_context: ID3D11DeviceContext,
- rx: mpsc::Receiver<EncoderMessage>,
- ) {
- // Initialize COM for this thread
- unsafe {
- CoInitializeEx(None, COINIT_APARTMENTTHREADED).unwrap();
- }
-
- let mut encoder = MediaFoundationEncoder::new(width, height, 30, &d3d_device, &d3d_context)
- .expect("Failed to create encoder");
-
- // Process messages from the capturer
- while let Ok(message) = rx.recv() {
- match message {
- EncoderMessage::Frame { texture, timestamp } => {
- if let Err(e) = encoder.encode_frame_from_texture(&texture, timestamp) {
- eprintln!("Failed to encode frame: {}", e);
- }
- }
- EncoderMessage::Stop => {
- break;
- }
- }
- }
-
- // Finalize the encoder
- if let Err(e) = encoder.finalize() {
- eprintln!("Failed to finalize encoder: {}", e);
- }
-
- unsafe {
- CoUninitialize();
- }
- }
-
- struct MediaFoundationEncoder {
- sink_writer: IMFSinkWriter,
- stream_index: u32,
- width: u32,
- height: u32,
- d3d_device: ID3D11Device,
- dxgi_device_manager: IMFDXGIDeviceManager,
- }
-
- impl MediaFoundationEncoder {
- pub fn new(
- width: u32,
- height: u32,
- fps: u32,
- d3d_device: &ID3D11Device,
- _d3d_context: &ID3D11DeviceContext,
- ) -> windows::core::Result<Self> {
- unsafe {
- // Create DXGI Device Manager for hardware encoding
- let mut reset_token = 0u32;
- let mut dxgi_device_manager: Option<IMFDXGIDeviceManager> = None;
- MFCreateDXGIDeviceManager(&mut reset_token, &mut dxgi_device_manager)?;
- let dxgi_device_manager = dxgi_device_manager.unwrap();
-
- // Reset device manager with our D3D11 device
- dxgi_device_manager.ResetDevice(d3d_device, reset_token)?;
-
- // Create sink writer attributes for hardware encoding
- let mut attributes: Option<IMFAttributes> = None;
- MFCreateAttributes(&mut attributes, 10)?;
- let attributes = attributes.unwrap();
-
- // Enable hardware transforms (GPU encoding)
- attributes.SetUINT32(&MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1)?;
- attributes.SetUINT32(&MF_SINK_WRITER_DISABLE_THROTTLING, 1)?;
- attributes.SetUnknown(&MF_SINK_WRITER_D3D_MANAGER, &dxgi_device_manager)?;
-
- // Create sink writer
- let output_url = w!("output.mp4");
- let sink_writer = MFCreateSinkWriterFromURL(output_url, None, Some(&attributes))?;
-
- // Create output media type for H.264 with hardware encoding
- let output_type = MFCreateMediaType()?;
-
- output_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
- output_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_H264)?;
-
- // Set high bitrate for quality
- output_type.SetUINT32(&MF_MT_AVG_BITRATE, width * height * 4)?; // 4 Mbps
- output_type
- .SetUINT32(&MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive.0 as u32)?;
-
- // Set frame size
- let frame_size = ((width as u64) << 32) | (height as u64);
- output_type.SetUINT64(&MF_MT_FRAME_SIZE, frame_size)?;
-
- // Set frame rate
- let frame_rate = ((fps as u64) << 32) | 1u64;
- output_type.SetUINT64(&MF_MT_FRAME_RATE, frame_rate)?;
-
- // Set pixel aspect ratio
- let pixel_aspect_ratio = (1u64 << 32) | 1u64;
- output_type.SetUINT64(&MF_MT_PIXEL_ASPECT_RATIO, pixel_aspect_ratio)?;
-
- // Create input media type - use NV12 for hardware encoding efficiency
- let input_type = MFCreateMediaType()?;
-
- input_type.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;
- input_type.SetGUID(&MF_MT_SUBTYPE, &MFVideoFormat_NV12)?; // NV12 is preferred for hardware encoding
- input_type
- .SetUINT32(&MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive.0 as u32)?;
- input_type.SetUINT64(&MF_MT_FRAME_SIZE, frame_size)?;
- input_type.SetUINT64(&MF_MT_FRAME_RATE, frame_rate)?;
- input_type.SetUINT64(&MF_MT_PIXEL_ASPECT_RATIO, pixel_aspect_ratio)?;
-
- // Add stream
- let mut stream_index = 0u32;
- let stream_index = sink_writer.AddStream(&output_type)?;
- sink_writer.SetInputMediaType(stream_index, &input_type, None)?;
-
- // Start writing
- sink_writer.BeginWriting()?;
-
- Ok(Self {
- sink_writer,
- stream_index,
- width,
- height,
- d3d_device: d3d_device.clone(),
- dxgi_device_manager,
- })
- }
- }
-
- pub fn encode_frame_from_texture(
- &mut self,
- texture: &ID3D11Texture2D,
- timestamp: u64,
- ) -> windows::core::Result<()> {
- unsafe {
- // Get DXGI surface from texture
- let dxgi_surface: IDXGISurface = texture.cast()?;
-
- // Create DXGI buffer directly from surface
- let dxgi_buffer = MFCreateDXGISurfaceBuffer(
- &ID3D11Texture2D::IID,
- &dxgi_surface,
- 0, // subresource index
- false, // bottom up flag
- )?;
-
- // Create sample
- let sample = MFCreateSample()?;
-
- sample.AddBuffer(&dxgi_buffer)?;
- sample.SetSampleTime(timestamp as i64)?;
-
- // Calculate frame duration (100ns units)
- let frame_duration = 10_000_000 / 30; // 30 fps
- sample.SetSampleDuration(frame_duration)?;
-
- // Write sample - this will use hardware encoding
- self.sink_writer.WriteSample(self.stream_index, &sample)?;
-
- dbg!(timestamp);
- Ok(())
- }
- }
-
- pub fn finalize(self) -> windows::core::Result<()> {
- unsafe {
- self.sink_writer.Finalize()?;
- Ok(())
- }
- }
- }
-
- // Ensure we can access the D3D device from Frame
- trait FrameHardwareAccess {
- fn d3d_device(&self) -> &ID3D11Device;
- fn d3d_context(&self) -> &ID3D11DeviceContext;
- }
-
- impl FrameHardwareAccess for scap_direct3d::Frame<'_> {
- fn d3d_device(&self) -> &ID3D11Device {
- // Access the private field through the public accessor we added
- self.d3d_device()
- }
-
- fn d3d_context(&self) -> &ID3D11DeviceContext {
- // Access the private field through the public accessor we added
- self.d3d_context()
- }
+ std::thread::sleep(Duration::from_secs(3));
}
}
diff --git a/crates/scap-direct3d/src/lib.rs b/crates/scap-direct3d/src/lib.rs
index d85be4849..d62f5db4c 100644
--- a/crates/scap-direct3d/src/lib.rs
+++ b/crates/scap-direct3d/src/lib.rs
@@ -123,6 +123,8 @@ pub enum NewCapturerError {
CursorNotSupported,
#[error("UpdateIntervalNotSupported")]
UpdateIntervalNotSupported,
+ #[error("D3DDevice: {0}")]
+ D3DDevice(windows::core::Error),
#[error("CreateRunner/{0}")]
CreateRunner(#[from] StartRunnerError),
#[error("RecvTimeout")]
@@ -136,6 +138,8 @@ pub struct Capturer {
item: GraphicsCaptureItem,
settings: Settings,
thread_handle: Option<JoinHandle<()>>,
+ d3d_device: ID3D11Device,
+ d3d_context: ID3D11DeviceContext,
}
impl Capturer {
@@ -161,15 +165,41 @@ impl Capturer {
return Err(NewCapturerError::UpdateIntervalNotSupported);
}
- let stop_flag = Arc::new(AtomicBool::new(false));
+ let mut d3d_device = None;
+ let mut d3d_context = None;
+
+ unsafe {
+ D3D11CreateDevice(
+ None,
+ D3D_DRIVER_TYPE_HARDWARE,
+ HMODULE::default(),
+ Default::default(),
+ None,
+ D3D11_SDK_VERSION,
+ Some(&mut d3d_device),
+ None,
+ Some(&mut d3d_context),
+ )
+ }
+ .map_err(StartRunnerError::D3DDevice)?;
Ok(Capturer {
- stop_flag,
+ stop_flag: Arc::new(AtomicBool::new(false)),
item,
settings,
thread_handle: None,
+ d3d_device: d3d_device.unwrap(),
+ d3d_context: d3d_context.unwrap(),
})
}
+
+ pub fn d3d_device(&self) -> &ID3D11Device {
+ &self.d3d_device
+ }
+
+ pub fn d3d_context(&self) -> &ID3D11DeviceContext {
+ &self.d3d_context
+ }
}
#[derive(Clone, Debug, thiserror::Error)]
@@ -197,6 +227,9 @@ impl Capturer {
let settings = self.settings.clone();
let stop_flag = self.stop_flag.clone();
+ let d3d_device = self.d3d_device.clone();
+ let d3d_context = self.d3d_context.clone();
+
let thread_handle = std::thread::spawn({
move || {
if let Err(e) = unsafe { RoInitialize(RO_INIT_MULTITHREADED) }
@@ -206,7 +239,15 @@ impl Capturer {
// return Err(CreateRunnerError::FailedToInitializeWinRT);
}
- match Runner::start(item, settings, callback, closed_callback, stop_flag) {
+ match Runner::start(
+ item,
+ settings,
+ callback,
+ closed_callback,
+ stop_flag,
+ d3d_device,
+ d3d_context,
+ ) {
Ok(runner) => {
let _ = started_tx.send(Ok(()));
@@ -450,6 +491,8 @@ impl Runner {
mut callback: impl FnMut(Frame) -> windows::core::Result<()> + Send + 'static,
mut closed_callback: impl FnMut() -> windows::core::Result<()> + Send + 'static,
stop_flag: Arc<AtomicBool>,
+ d3d_device: ID3D11Device,
+ d3d_context: ID3D11DeviceContext,
) -> Result<Self, StartRunnerError> {
let queue_options = DispatcherQueueOptions {
dwSize: std::mem::size_of::<DispatcherQueueOptions>() as u32,
@@ -460,27 +503,6 @@ impl Runner {
let _controller = unsafe { CreateDispatcherQueueController(queue_options) }
.map_err(StartRunnerError::DispatchQueue)?;
- let mut d3d_device = None;
- let mut d3d_context = None;
-
- unsafe {
- D3D11CreateDevice(
- None,
- D3D_DRIVER_TYPE_HARDWARE,
- HMODULE::default(),
- Default::default(),
- None,
- D3D11_SDK_VERSION,
- Some(&mut d3d_device),
- None,
- Some(&mut d3d_context),
- )
- }
- .map_err(StartRunnerError::D3DDevice)?;
-
- let d3d_device = d3d_device.unwrap();
- let d3d_context = d3d_context.unwrap();
-
let direct3d_device = (|| {
let dxgi_device = d3d_device.cast::<IDXGIDevice>()?;
let inspectable = unsafe { CreateDirect3D11DeviceFromDXGIDevice(&dxgi_device) }?;