Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Introduce camera-directshow and camera-mediafoundation (#752)

Brendan Allan216571873521
diff --git a/Cargo.lock b/Cargo.lock
index ddc2a029e..81994e9db 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -895,6 +895,16 @@ dependencies = [
"system-deps",
]
+[[package]]
+name = "camera-mediafoundation"
+version = "0.1.0"
+dependencies = [
+ "inquire",
+ "tracing",
+ "windows 0.60.0",
+ "windows-core 0.60.1",
+]
+
[[package]]
name = "camino"
version = "1.1.9"
@@ -950,6 +960,17 @@ dependencies = [
"inquire",
]
+[[package]]
+name = "cap-camera-directshow"
+version = "0.1.0"
+dependencies = [
+ "inquire",
+ "tracing",
+ "tracing-subscriber",
+ "windows 0.60.0",
+ "windows-core 0.60.1",
+]
+
[[package]]
name = "cap-cursor-capture"
version = "0.1.0"
diff --git a/crates/camera-directshow/Cargo.toml b/crates/camera-directshow/Cargo.toml
new file mode 100644
index 000000000..6961d8a62
--- /dev/null
+++ b/crates/camera-directshow/Cargo.toml
@@ -0,0 +1,24 @@
+[package]
+name = "cap-camera-directshow"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+windows-core = { version = "0.60" }
+windows = { version = "0.60", features = [
+ "Win32_System_Com",
+ "Win32_Media_MediaFoundation",
+ "Win32_Media_DirectShow",
+ "Win32_System_Com_StructuredStorage",
+ "Win32_System_Ole",
+ "Win32_System_Variant",
+ "Win32_Media_KernelStreaming",
+] }
+
+[lints]
+workspace = true
+
+[dev-dependencies]
+inquire = "0.7.5"
+tracing.workspace = true
+tracing-subscriber = "0.3.19"
diff --git a/crates/camera-directshow/examples/cli.rs b/crates/camera-directshow/examples/cli.rs
new file mode 100644
index 000000000..e747d4e68
--- /dev/null
+++ b/crates/camera-directshow/examples/cli.rs
@@ -0,0 +1,913 @@
+use cap_camera_dshow::*;
+use std::{
+ cell::RefCell,
+ fmt::Display,
+ mem::ManuallyDrop,
+ ops::Deref,
+ ptr::{self, null_mut},
+ time::Duration,
+};
+use tracing::{error, info, trace};
+use windows::{
+ Win32::{
+ Foundation::{E_NOTIMPL, E_OUTOFMEMORY, E_POINTER, S_FALSE, S_OK, SIZE},
+ Media::{
+ DirectShow::{
+ FILTER_STATE, IAMStreamConfig, IAMVideoControl, IBaseFilter, IBaseFilter_Impl,
+ ICaptureGraphBuilder2, IEnumMediaTypes, IEnumMediaTypes_Impl, IEnumPins,
+ IEnumPins_Impl, IFilterGraph, IGraphBuilder, IMediaControl, IMediaFilter,
+ IMediaFilter_Impl, IMemInputPin, IMemInputPin_Impl, IPin, IPin_Impl, PINDIR_INPUT,
+ PINDIR_OUTPUT, State_Paused, State_Running, State_Stopped, VFW_E_NO_ALLOCATOR,
+ VFW_E_NOT_CONNECTED,
+ },
+ KernelStreaming::{KS_BITMAPINFOHEADER, KS_VIDEOINFOHEADER},
+ MediaFoundation::{
+ AM_MEDIA_TYPE, CLSID_CaptureGraphBuilder2, CLSID_FilterGraph, FORMAT_VideoInfo,
+ MEDIASUBTYPE_YUY2, MEDIATYPE_Video, PIN_CATEGORY_CAPTURE,
+ },
+ },
+ System::Com::{
+ CLSCTX_INPROC_SERVER, CoCreateInstance, CoInitialize, CoTaskMemAlloc, CoTaskMemFree,
+ IMoniker, IPersist_Impl, StructuredStorage::IPropertyBag,
+ },
+ },
+ core::Interface,
+};
+use windows_core::{ComObject, ComObjectInner, GUID, implement};
+
+fn main() {
+ tracing_subscriber::fmt::init();
+
+ unsafe {
+ CoInitialize(None).unwrap();
+
+ let devices = VideoInputDeviceEnumerator::new().unwrap().to_vec();
+
+ let mut devices = devices
+ .iter()
+ .map(VideoDeviceSelectOption)
+ .collect::<Vec<_>>();
+
+ let selected = if devices.len() > 1 {
+ inquire::Select::new("Select a device", devices)
+ .prompt()
+ .unwrap()
+ } else {
+ devices.remove(0)
+ };
+
+ let moniker = selected.0;
+
+ let property_data: IPropertyBag = moniker.BindToStorage(None, None).unwrap();
+ let device_name = property_data
+ .read(windows_core::w!("FriendlyName"), None)
+ .unwrap();
+
+ let device_path = property_data
+ .read(windows_core::w!("DevicePath"), None)
+ .unwrap_or_default();
+
+ let device_name = device_name.to_os_string().unwrap();
+ println!("Info for device '{:?}'", device_name);
+
+ let device_path = device_path.to_os_string();
+ println!("Path: '{:?}'", device_path);
+
+ let filter: IBaseFilter = moniker.BindToObject(None, None).unwrap();
+
+ chromium_main(filter);
+
+ return;
+ }
+}
+
+#[derive(Debug)]
+struct Format {
+ width: i32,
+ height: i32,
+ media_type: AM_MEDIA_TYPE,
+ frame_rates: Vec<f64>,
+}
+
+impl Display for Format {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(
+ f,
+ "{}x{} {} ({:?})",
+ self.width,
+ self.height,
+ unsafe { self.media_type.subtype_str().unwrap_or("unknown") },
+ &self.frame_rates
+ )
+ }
+}
+
+struct VideoDeviceSelectOption<'a>(&'a IMoniker);
+
+impl<'a> Display for VideoDeviceSelectOption<'a> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ let device_name = unsafe {
+ let property_data: IPropertyBag = self.0.BindToStorage(None, None).unwrap();
+
+ let device_name = property_data
+ .read(windows_core::w!("FriendlyName"), None)
+ .unwrap();
+
+ device_name.to_os_string().unwrap()
+ };
+
+ write!(f, "{:?}", device_name)
+ }
+}
+
+// chromium
+
+#[implement(IPin, IMemInputPin)]
+struct SinkInputPin {
+ current_media_type: RefCell<AMMediaType>,
+ connected_pin: RefCell<Option<IPin>>,
+ owner: RefCell<Option<*const IBaseFilter>>,
+}
+
+impl SinkInputPin {
+ unsafe fn get_valid_media_type(&self, index: i32, media_type: &mut AM_MEDIA_TYPE) -> bool {
+ let video_info_header = &mut *(media_type.pbFormat as *mut KS_VIDEOINFOHEADER);
+
+ video_info_header.bmiHeader.biSize = size_of::<KS_BITMAPINFOHEADER>() as u32;
+ video_info_header.bmiHeader.biPlanes = 1;
+ video_info_header.bmiHeader.biClrImportant = 0;
+ video_info_header.bmiHeader.biClrUsed = 0;
+
+ media_type.majortype = MEDIATYPE_Video;
+ media_type.formattype = FORMAT_VideoInfo;
+ media_type.bTemporalCompression = false.into();
+
+ if index == 0 {
+ video_info_header.bmiHeader.biCompression =
+ u32::from_ne_bytes(*"yuy2".as_bytes().first_chunk::<4>().unwrap());
+ video_info_header.bmiHeader.biBitCount = 16;
+ video_info_header.bmiHeader.biWidth = 640;
+ video_info_header.bmiHeader.biHeight = 480;
+ media_type.subtype = MEDIASUBTYPE_YUY2;
+ true
+ } else {
+ false
+ }
+ }
+}
+
+impl IPin_Impl for SinkInputPin_Impl {
+ fn Connect(
+ &self,
+ preceivepin: windows_core::Ref<'_, IPin>,
+ pmt: *const windows::Win32::Media::MediaFoundation::AM_MEDIA_TYPE,
+ ) -> windows_core::Result<()> {
+ trace!("Connect entry");
+ let Some(preceivepin) = preceivepin.as_ref() else {
+ trace!("Connect exit - E_POINTER");
+ return E_POINTER.ok();
+ };
+
+ if pmt.is_null() {
+ trace!("Connect exit - E_POINTER");
+ return E_POINTER.ok();
+ }
+ let media_type = unsafe { &*pmt };
+
+ dbg!(media_type);
+ // self.current_media_type.replace(media_type.clone());
+ self.connected_pin.replace(Some(preceivepin.clone()));
+ let result = unsafe { preceivepin.ReceiveConnection(&self.cast::<IPin>()?, pmt) };
+ trace!("Connect exit");
+ result
+ }
+
+ fn ReceiveConnection(
+ &self,
+ pconnector: windows_core::Ref<'_, IPin>,
+ pmt: *const windows::Win32::Media::MediaFoundation::AM_MEDIA_TYPE,
+ ) -> windows_core::Result<()> {
+ trace!("ReceiveConnection entry");
+ self.current_media_type
+ .replace(AMMediaType::new(unsafe { &*pmt }));
+ self.connected_pin.replace(pconnector.clone());
+ trace!("ReceiveConnection exit");
+ S_OK.ok()
+ }
+
+ fn Disconnect(&self) -> windows_core::Result<()> {
+ trace!("Disconnect entry");
+ let result = match self.connected_pin.borrow_mut().take() {
+ Some(_) => S_OK.ok(),
+ None => VFW_E_NOT_CONNECTED.ok(),
+ };
+ trace!("Disconnect exit");
+ result
+ }
+
+ fn ConnectedTo(&self) -> windows_core::Result<IPin> {
+ trace!("ConnectedTo entry");
+ let result = match self.connected_pin.borrow().as_ref() {
+ Some(connected_pin) => Ok(connected_pin.clone()),
+ None => Err(VFW_E_NOT_CONNECTED.into()),
+ };
+ trace!("ConnectedTo exit");
+ result
+ }
+
+ fn ConnectionMediaType(
+ &self,
+ pmt: *mut windows::Win32::Media::MediaFoundation::AM_MEDIA_TYPE,
+ ) -> windows_core::Result<()> {
+ trace!("ConnectionMediaType entry");
+ self.connected_pin
+ .borrow()
+ .as_ref()
+ .ok_or(VFW_E_NOT_CONNECTED)?;
+
+ unsafe { *pmt = self.current_media_type.borrow().clone().into_inner() };
+
+ trace!("ConnectionMediaType exit");
+ Ok(())
+ }
+
+ fn QueryPinInfo(
+ &self,
+ pinfo: *mut windows::Win32::Media::DirectShow::PIN_INFO,
+ ) -> windows_core::Result<()> {
+ trace!("QueryPinInfo before");
+ unsafe {
+ (*pinfo).dir = PINDIR_INPUT;
+ (*pinfo).pFilter =
+ ManuallyDrop::new(self.owner.borrow().as_ref().map(|v| (&**v).clone()));
+ (*pinfo).achName[0] = '\0' as u16;
+ }
+
+ trace!("QueryPinInfo after");
+ S_OK.ok()
+ }
+
+ fn QueryDirection(
+ &self,
+ ) -> windows_core::Result<windows::Win32::Media::DirectShow::PIN_DIRECTION> {
+ trace!("QueryDirection entry");
+ trace!("QueryDirection exit");
+ Ok(PINDIR_INPUT)
+ }
+
+ fn QueryId(&self) -> windows_core::Result<windows_core::PWSTR> {
+ trace!("QueryId entry");
+ unreachable!()
+ }
+
+ fn QueryAccept(
+ &self,
+ _pmt: *const windows::Win32::Media::MediaFoundation::AM_MEDIA_TYPE,
+ ) -> windows_core::HRESULT {
+ trace!("QueryAccept entry");
+ trace!("QueryAccept exit");
+ S_FALSE
+ }
+
+ fn EnumMediaTypes(
+ &self,
+ ) -> windows_core::Result<windows::Win32::Media::DirectShow::IEnumMediaTypes> {
+ trace!("EnumMediaTypes entry");
+ Ok(TypeEnumerator {
+ index: Default::default(),
+ pin: self,
+ }
+ .into())
+ }
+
+ fn QueryInternalConnections(
+ &self,
+ appin: windows_core::OutRef<'_, IPin>,
+ npin: *mut u32,
+ ) -> windows_core::Result<()> {
+ trace!("QueryInternalConnections entry");
+ Err(E_NOTIMPL.into())
+ }
+
+ fn EndOfStream(&self) -> windows_core::Result<()> {
+ trace!("EndOfStream entry");
+ trace!("EndOfStream exit");
+ S_OK.ok()
+ }
+
+ fn BeginFlush(&self) -> windows_core::Result<()> {
+ trace!("BeginFlush entry");
+ trace!("BeginFlush exit");
+ S_OK.ok()
+ }
+
+ fn EndFlush(&self) -> windows_core::Result<()> {
+ trace!("EndFlush entry");
+ trace!("EndFlush exit");
+ S_OK.ok()
+ }
+
+ fn NewSegment(&self, _tstart: i64, _tstop: i64, _drate: f64) -> windows_core::Result<()> {
+ trace!("NewSegment entry");
+ unreachable!()
+ }
+}
+
+#[implement(IEnumMediaTypes)]
+struct TypeEnumerator<'a> {
+ index: RefCell<i32>,
+ pin: &'a SinkInputPin,
+}
+
+impl<'a> TypeEnumerator<'a> {
+ unsafe fn free_allocated_media_types(allocated: usize, types: *mut *mut AM_MEDIA_TYPE) {
+ for i in 0..allocated {
+ CoTaskMemFree(Some((*(*types.add(i))).pbFormat as *const _));
+ CoTaskMemFree(Some(*types.add(i) as *const _));
+ }
+ }
+}
+
+impl<'a> IEnumMediaTypes_Impl for TypeEnumerator_Impl<'a> {
+ fn Next(
+ &self,
+ cmediatypes: u32,
+ ppmediatypes: *mut *mut AM_MEDIA_TYPE,
+ pcfetched: *mut u32,
+ ) -> windows_core::HRESULT {
+ trace!("TypeEnumerator::Next");
+ dbg!(cmediatypes);
+ dbg!(self.index.borrow());
+
+ let mut types_fetched = 0;
+
+ unsafe {
+ while types_fetched < cmediatypes {
+ let typ = CoTaskMemAlloc(size_of::<AM_MEDIA_TYPE>()) as *mut AM_MEDIA_TYPE;
+ if typ.is_null() {
+ TypeEnumerator::free_allocated_media_types(
+ types_fetched as usize,
+ ppmediatypes,
+ );
+ return E_OUTOFMEMORY;
+ }
+ let typ = &mut *typ;
+
+ *typ = AM_MEDIA_TYPE::default();
+ typ.cbFormat = size_of::<KS_VIDEOINFOHEADER>() as u32;
+
+ let format =
+ CoTaskMemAlloc(size_of::<KS_VIDEOINFOHEADER>()) as *mut KS_VIDEOINFOHEADER;
+ if format.is_null() {
+ CoTaskMemFree(Some((typ as *const AM_MEDIA_TYPE).cast()));
+ TypeEnumerator::free_allocated_media_types(
+ types_fetched as usize,
+ ppmediatypes,
+ );
+ return E_OUTOFMEMORY;
+ }
+ typ.pbFormat = format as *mut _;
+
+ if self.pin.get_valid_media_type(*self.index.borrow(), typ) {
+ self.index.replace_with(|v| *v + 1);
+
+ *ppmediatypes.add(types_fetched as usize) = typ;
+
+ types_fetched += 1;
+ } else {
+ CoTaskMemFree(Some(format as *const _));
+ CoTaskMemFree(Some(typ as *mut _ as *const _));
+ break;
+ }
+ }
+
+ if !pcfetched.is_null() {
+ *pcfetched = types_fetched;
+ }
+ }
+
+ if types_fetched == cmediatypes {
+ S_OK
+ } else {
+ S_FALSE
+ }
+ }
+
+ fn Skip(&self, cmediatypes: u32) -> windows_core::Result<()> {
+ trace!("TypeEnumerator::Skip");
+ self.index.replace_with(|v| (*v) + cmediatypes as i32);
+ S_OK.ok()
+ }
+
+ fn Reset(&self) -> windows_core::Result<()> {
+ trace!("TypeEnumerator::Reset");
+ self.index.replace(0);
+ S_OK.ok()
+ }
+
+ fn Clone(&self) -> windows_core::Result<IEnumMediaTypes> {
+ trace!("TypeEnumerator::Clone");
+ unsafe { self.cast() }
+ }
+}
+
+impl IMemInputPin_Impl for SinkInputPin_Impl {
+ fn GetAllocator(
+ &self,
+ ) -> windows_core::Result<windows::Win32::Media::DirectShow::IMemAllocator> {
+ trace!("GetAllocator entry");
+ trace!("GetAllocator exit");
+ Err(VFW_E_NO_ALLOCATOR.into())
+ }
+
+ fn NotifyAllocator(
+ &self,
+ _pallocator: windows_core::Ref<'_, windows::Win32::Media::DirectShow::IMemAllocator>,
+ _breadonly: windows_core::BOOL,
+ ) -> windows_core::Result<()> {
+ trace!("NotifyAllocator entry");
+ trace!("NotifyAllocator exit");
+ S_OK.ok()
+ }
+
+ fn GetAllocatorRequirements(
+ &self,
+ ) -> windows_core::Result<windows::Win32::Media::DirectShow::ALLOCATOR_PROPERTIES> {
+ trace!("GetAllocatorRequirements entry");
+ trace!("GetAllocatorRequirements exit");
+ Err(E_NOTIMPL.into())
+ }
+
+ fn Receive(
+ &self,
+ psample: windows_core::Ref<'_, windows::Win32::Media::DirectShow::IMediaSample>,
+ ) -> windows_core::Result<()> {
+ let Some(psample) = psample.as_ref() else {
+ return Ok(());
+ };
+
+ let mut ptimestart = 0;
+ let mut ptimeend = 0;
+ unsafe {
+ psample.GetTime(&mut ptimestart, &mut ptimeend).unwrap();
+ };
+
+ let pts = ptimestart;
+ let bytes = unsafe { psample.GetActualDataLength() };
+
+ unsafe {
+ if let Ok(new_media_type) = psample.GetMediaType() {
+ if !new_media_type.is_null() {
+ self.current_media_type
+ .replace(AMMediaType::new(&*new_media_type));
+ }
+ }
+ }
+
+ let media_type = self.current_media_type.borrow();
+
+ let format_str = unsafe { media_type.subtype_str() };
+
+ let video_info =
+ unsafe { &*(media_type.pbFormat as *const _ as *const KS_VIDEOINFOHEADER) };
+
+ println!(
+ "New frame: {}x{}, {pts}pts, {bytes} bytes, {}",
+ video_info.bmiHeader.biWidth,
+ video_info.bmiHeader.biHeight,
+ format_str.unwrap_or("unknown format")
+ );
+
+ Ok(())
+ }
+
+ fn ReceiveMultiple(
+ &self,
+ psamples: *const Option<windows::Win32::Media::DirectShow::IMediaSample>,
+ mut nsamples: i32,
+ ) -> windows_core::Result<i32> {
+ trace!("ReceiveMultiple entry");
+ let mut processed: i32 = 0;
+ while nsamples > 0 {
+ unsafe { self.Receive((&*psamples.offset(processed as isize)).into())? };
+ nsamples -= 1;
+ processed += 1;
+ }
+
+ trace!("ReceiveMultiple exit");
+ Ok(processed)
+ }
+
+ fn ReceiveCanBlock(&self) -> windows_core::Result<()> {
+ trace!("ReceiveCanBlock entry");
+ trace!("ReceiveCanBlock exit");
+ S_FALSE.ok()
+ }
+}
+
+#[implement(IBaseFilter, IMediaFilter)]
+struct SinkFilter {
+ state: RefCell<FILTER_STATE>,
+ owning_graph: RefCell<Option<IFilterGraph>>,
+ input_pin: ComObject<SinkInputPin>,
+}
+
+impl SinkFilter {
+ fn no_of_pins(&self) -> u32 {
+ 1
+ }
+
+ fn get_pin(&self, i: u32) -> Option<IPin> {
+ if i == 0 {
+ Some(unsafe { self.input_pin.get().cast() }.unwrap())
+ } else {
+ None
+ }
+ }
+}
+
+// impl Drop for SinkFilter {
+// fn drop(&mut self) {
+// self.input_pin.owner.borrow_mut().take();
+// }
+// }
+
+impl AsRef<SinkFilter> for SinkFilter {
+ fn as_ref(&self) -> &Self {
+ self
+ }
+}
+
+impl IBaseFilter_Impl for SinkFilter_Impl {
+ fn EnumPins(&self) -> windows_core::Result<windows::Win32::Media::DirectShow::IEnumPins> {
+ trace!("FilterBase::EnumPins");
+ Ok(PinEnumerator {
+ filter: &self.this,
+ index: Default::default(),
+ }
+ .into())
+ }
+
+ fn FindPin(
+ &self,
+ _id: &windows_core::PCWSTR,
+ ) -> windows_core::Result<windows::Win32::Media::DirectShow::IPin> {
+ trace!("FindPin entry");
+ trace!("FindPin exit");
+ Err(E_NOTIMPL.into())
+ }
+
+ fn QueryFilterInfo(
+ &self,
+ pinfo: *mut windows::Win32::Media::DirectShow::FILTER_INFO,
+ ) -> windows_core::Result<()> {
+ trace!("QueryFilterInfo entry");
+ unsafe {
+ (*pinfo).pGraph = ManuallyDrop::new(self.owning_graph.borrow().clone());
+ (*pinfo).achName[0] = '\0' as u16;
+ }
+ trace!("QueryFilterInfo exit");
+ Ok(())
+ }
+
+ fn JoinFilterGraph(
+ &self,
+ pgraph: windows_core::Ref<'_, windows::Win32::Media::DirectShow::IFilterGraph>,
+ pname: &windows_core::PCWSTR,
+ ) -> windows_core::Result<()> {
+ trace!("JoinFilterGraph");
+ *self.owning_graph.borrow_mut() = pgraph.clone();
+
+ Ok(())
+ }
+
+ fn QueryVendorInfo(&self) -> windows_core::Result<windows_core::PWSTR> {
+ trace!("QueryVendorInfo entry");
+ trace!("QueryVendorInfo exit");
+ Ok(windows_core::PWSTR::null())
+ }
+}
+
+impl IMediaFilter_Impl for SinkFilter_Impl {
+ fn Stop(&self) -> windows_core::Result<()> {
+ trace!("Stop entry");
+ self.state.replace(State_Stopped);
+ trace!("Stop exit");
+ Ok(())
+ }
+
+ fn Pause(&self) -> windows_core::Result<()> {
+ trace!("Pause entry");
+ self.state.replace(State_Paused);
+ trace!("Pause exit");
+ Ok(())
+ }
+
+ fn Run(&self, _tstart: i64) -> windows_core::Result<()> {
+ trace!("Run entry");
+ self.state.replace(State_Running);
+ trace!("Run exit");
+ Ok(())
+ }
+
+ fn GetState(
+ &self,
+ _dwmillisecstimeout: u32,
+ ) -> windows_core::Result<windows::Win32::Media::DirectShow::FILTER_STATE> {
+ trace!("GetState entry");
+ trace!("GetState exit");
+ Ok(*self.state.borrow())
+ }
+
+ fn SetSyncSource(
+ &self,
+ _pclock: windows_core::Ref<'_, windows::Win32::Media::IReferenceClock>,
+ ) -> windows_core::Result<()> {
+ trace!("SetSyncSource entry");
+ trace!("SetSyncSource exit");
+ S_OK.ok()
+ }
+
+ fn GetSyncSource(&self) -> windows_core::Result<windows::Win32::Media::IReferenceClock> {
+ trace!("GetSyncSource entry");
+ trace!("GetSyncSource exit");
+ Err(E_NOTIMPL.into())
+ }
+}
+
+impl IPersist_Impl for SinkFilter_Impl {
+ fn GetClassID(&self) -> windows_core::Result<windows_core::GUID> {
+ trace!("GetClassID");
+ unreachable!();
+ }
+}
+
+#[implement(IEnumPins)]
+struct PinEnumerator<'a> {
+ filter: &'a dyn AsRef<SinkFilter>,
+ index: RefCell<u32>,
+}
+
+impl<'a> IEnumPins_Impl for PinEnumerator_Impl<'a> {
+ fn Next(
+ &self,
+ cpins: u32,
+ pppins: windows_core::OutRef<'_, IPin>,
+ pcfetched: *mut u32,
+ ) -> windows_core::HRESULT {
+ let mut pins_fetched = 0;
+
+ let index = *self.index.borrow();
+ let filter = self.filter.as_ref();
+ trace!("IEnumPins Next");
+ if pins_fetched < cpins && filter.no_of_pins() > index {
+ let pin = filter.get_pin(index);
+ self.index.replace_with(|v| *v + 1);
+ pins_fetched += 1;
+ pppins.write(pin).unwrap();
+ }
+
+ if !pcfetched.is_null() {
+ unsafe {
+ *pcfetched = pins_fetched;
+ }
+ }
+
+ if pins_fetched == cpins { S_OK } else { S_FALSE }
+ }
+
+ fn Skip(&self, cpins: u32) -> windows_core::Result<()> {
+ trace!("Skip entry");
+ let filter = self.filter.as_ref();
+ if filter.no_of_pins() - *self.index.borrow() > cpins {
+ self.index.replace_with(|v| *v + 1);
+ trace!("Skip exit - S_OK");
+ return S_OK.ok();
+ }
+
+ self.index.replace(0);
+ trace!("Skip exit - S_FALSE");
+ S_FALSE.ok()
+ }
+
+ fn Reset(&self) -> windows_core::Result<()> {
+ trace!("Reset entry");
+ self.index.replace(0);
+ trace!("Reset exit");
+ S_OK.ok()
+ }
+
+ fn Clone(&self) -> windows_core::Result<IEnumPins> {
+ trace!("Clone entry");
+ let result = unsafe { self.cast() };
+ trace!("Clone exit");
+ result
+ }
+}
+
+unsafe fn chromium_main(capture_filter: IBaseFilter) {
+ let output_capture_pin = capture_filter
+ .get_pin(PINDIR_OUTPUT, PIN_CATEGORY_CAPTURE, GUID::zeroed())
+ .unwrap();
+
+ let stream_config = output_capture_pin.cast::<IAMStreamConfig>().unwrap();
+ let video_control = output_capture_pin.cast::<IAMVideoControl>().ok();
+
+ let mut media_types_iter = stream_config.media_types();
+
+ // println!("Formats: {}", media_types_iter.count());
+
+ let mut formats = Vec::with_capacity(media_types_iter.count() as usize);
+
+ while let Some((media_type, i)) = media_types_iter.next() {
+ let is_video =
+ media_type.majortype == MEDIATYPE_Video && media_type.formattype == FORMAT_VideoInfo;
+
+ if !is_video {
+ continue;
+ }
+
+ // println!("Format {i}:");
+
+ let video_info = &*media_type.video_info();
+
+ let width = video_info.bmiHeader.biWidth;
+ let height = video_info.bmiHeader.biHeight;
+
+ // println!(" Dimensions: {width}x{height}");
+
+ let subtype_str = media_type.subtype_str().unwrap_or("unknown subtype");
+
+ // println!(" Pixel Format: {subtype_str}");
+
+ let mut frame_rates = vec![];
+
+ if let Some(video_control) = &video_control {
+ let time_per_frame_list = video_control.time_per_frame_list(
+ &output_capture_pin,
+ i,
+ SIZE {
+ cx: width,
+ cy: height,
+ },
+ );
+
+ for time_per_frame in time_per_frame_list {
+ if *time_per_frame <= 0 {
+ continue;
+ }
+ frame_rates.push(10_000_000.0 / *time_per_frame as f64)
+ }
+ }
+
+ if frame_rates.is_empty() {
+ let frame_rate = 10_000_000.0 / video_info.AvgTimePerFrame as f64;
+ frame_rates.push(frame_rate);
+ }
+
+ frame_rates
+ .iter_mut()
+ .for_each(|v| *v = (*v * 100.0).round() / 100.0);
+
+ // println!(" Frame Rates: {:?}", frame_rates);
+
+ formats.push(Format {
+ width,
+ height,
+ media_type: media_type.clone(),
+ frame_rates,
+ })
+ }
+
+ if formats.is_empty() {
+ error!("No formats found");
+ return;
+ }
+
+ let selected_format = inquire::Select::new("Select a format", formats)
+ .prompt()
+ .unwrap();
+
+ stream_config
+ .SetFormat(&selected_format.media_type)
+ .unwrap();
+
+ trace!("creating sink filter");
+ let sink_filter = SinkFilter {
+ state: RefCell::new(State_Stopped),
+ owning_graph: RefCell::new(None),
+ input_pin: SinkInputPin {
+ current_media_type: RefCell::new(Default::default()),
+ connected_pin: RefCell::new(None),
+ owner: RefCell::new(None),
+ }
+ .into(),
+ }
+ .into_object();
+ *sink_filter.input_pin.owner.borrow_mut() = Some(&sink_filter.cast::<IBaseFilter>().unwrap());
+ trace!("created sink filter");
+
+ let input_sink_pin = sink_filter.get_pin(0).unwrap();
+
+ trace!("creating graph builder");
+ let graph_builder: IGraphBuilder =
+ CoCreateInstance(&CLSID_FilterGraph, None, CLSCTX_INPROC_SERVER).unwrap();
+ trace!("created graph builder");
+ trace!("creating capture graph builder");
+ let capture_graph_builder: ICaptureGraphBuilder2 =
+ CoCreateInstance(&CLSID_CaptureGraphBuilder2, None, CLSCTX_INPROC_SERVER).unwrap();
+ trace!("created capture graph builder");
+ trace!("creating media control");
+ let media_control = graph_builder.cast::<IMediaControl>().unwrap();
+ trace!("created media control");
+ trace!("setting capture graph");
+ capture_graph_builder
+ .SetFiltergraph(&graph_builder)
+ .unwrap();
+ trace!("set capture graph");
+ trace!("adding capture filter");
+ graph_builder.AddFilter(&capture_filter, None).unwrap();
+ trace!("added capture filter");
+ trace!("creating sink filter");
+ let sink_filter: IBaseFilter = sink_filter.cast().unwrap();
+ trace!("adding sink filter");
+ graph_builder.AddFilter(&sink_filter, None).unwrap();
+ trace!("added sink filter");
+
+ trace!("finding stream config");
+ let mut stream_config = null_mut();
+ capture_graph_builder
+ .FindInterface(
+ Some(&PIN_CATEGORY_CAPTURE),
+ Some(&MEDIATYPE_Video),
+ &capture_filter,
+ &IAMStreamConfig::IID,
+ &mut stream_config,
+ )
+ .unwrap();
+ trace!("found stream config");
+
+ graph_builder
+ .ConnectDirect(&output_capture_pin, &input_sink_pin, None)
+ .unwrap();
+
+ media_control.Run().unwrap();
+
+ std::thread::sleep(Duration::from_secs(10));
+}
+
+#[derive(Default)]
+struct AMMediaType(AM_MEDIA_TYPE);
+
+impl AMMediaType {
+ pub fn new(typ: &AM_MEDIA_TYPE) -> Self {
+ Self(unsafe { copy_media_type(typ) })
+ }
+
+ pub fn into_inner(mut self) -> AM_MEDIA_TYPE {
+ let inner = std::mem::replace(&mut self.0, unsafe { std::mem::uninitialized() });
+ std::mem::forget(self);
+ inner
+ }
+}
+
+impl Deref for AMMediaType {
+ type Target = AM_MEDIA_TYPE;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl Clone for AMMediaType {
+ fn clone(&self) -> Self {
+ Self(unsafe { copy_media_type(&self.0) })
+ }
+}
+
+impl Drop for AMMediaType {
+ fn drop(&mut self) {
+ unsafe {
+ if !self.0.pbFormat.is_null() {
+ CoTaskMemFree(Some(self.0.pbFormat as *mut _));
+ self.0.pbFormat = null_mut();
+ }
+ }
+ }
+}
+
+unsafe fn copy_media_type(src: &AM_MEDIA_TYPE) -> AM_MEDIA_TYPE {
+ let mut dest = src.clone();
+
+ if src.cbFormat > 0 && !src.pbFormat.is_null() {
+ let format_size = src.cbFormat as usize;
+ let new_format = CoTaskMemAlloc(format_size) as *mut u8;
+
+ if !new_format.is_null() {
+ ptr::copy_nonoverlapping(src.pbFormat, new_format, format_size);
+ dest.pbFormat = new_format;
+ }
+ }
+
+ dest
+}
diff --git a/crates/camera-directshow/src/lib.rs b/crates/camera-directshow/src/lib.rs
new file mode 100644
index 000000000..ff2728f41
--- /dev/null
+++ b/crates/camera-directshow/src/lib.rs
@@ -0,0 +1,328 @@
+#![cfg(windows)]
+
+use std::{
+ ffi::{OsString, c_void},
+ ops::Deref,
+ os::windows::ffi::OsStringExt,
+ ptr::{null, null_mut},
+};
+use windows::{
+ Win32::{
+ Foundation::{S_OK, SIZE},
+ Media::{
+ DirectShow::{
+ AMPROPERTY_PIN_CATEGORY, IAMStreamConfig, IAMVideoControl, IBaseFilter,
+ ICreateDevEnum, IPin, PIN_DIRECTION, VIDEO_STREAM_CONFIG_CAPS,
+ },
+ KernelStreaming::{IKsPropertySet, KS_VIDEOINFOHEADER},
+ MediaFoundation::{
+ AM_MEDIA_TYPE, AMPROPSETID_Pin, CLSID_SystemDeviceEnum,
+ CLSID_VideoInputDeviceCategory, MEDIASUBTYPE_ARGB32, MEDIASUBTYPE_I420,
+ MEDIASUBTYPE_IYUV, MEDIASUBTYPE_MJPG, MEDIASUBTYPE_NV12, MEDIASUBTYPE_RGB24,
+ MEDIASUBTYPE_RGB32, MEDIASUBTYPE_UYVY, MEDIASUBTYPE_YUY2,
+ },
+ },
+ System::{
+ Com::{
+ CLSCTX_INPROC_SERVER, CoCreateInstance, IEnumMoniker, IErrorLog, IMoniker,
+ StructuredStorage::IPropertyBag,
+ },
+ Variant::{VARIANT, VT_BSTR},
+ },
+ },
+ core::Interface,
+};
+use windows_core::{GUID, PWSTR};
+
+pub trait IPinExt {
+ unsafe fn matches_category(&self, category: GUID) -> bool;
+ unsafe fn matches_major_type(&self, major_type: GUID) -> bool;
+}
+
+impl IPinExt for IPin {
+ unsafe fn matches_category(&self, category: GUID) -> bool {
+ let ks_property = self.cast::<IKsPropertySet>().unwrap();
+ let mut return_value_size = 0;
+ let mut pin_category = GUID::zeroed();
+ ks_property
+ .Get(
+ &AMPROPSETID_Pin,
+ AMPROPERTY_PIN_CATEGORY.0 as u32,
+ null(),
+ 0,
+ (&mut pin_category) as *mut _ as *mut c_void,
+ std::mem::size_of_val(&pin_category) as u32,
+ &mut return_value_size,
+ )
+ .unwrap();
+
+ return_value_size as usize == std::mem::size_of_val(&category) && pin_category == category
+ }
+ unsafe fn matches_major_type(&self, major_type: GUID) -> bool {
+ let mut connection_media_type = AM_MEDIA_TYPE::default();
+ self.ConnectionMediaType(&mut connection_media_type)
+ .map(|_| connection_media_type.majortype == major_type)
+ .unwrap_or(false)
+ }
+}
+
+pub trait IBaseFilterExt {
+ unsafe fn get_pin(
+ &self,
+ direction: PIN_DIRECTION,
+ category: GUID,
+ major_type: GUID,
+ ) -> Option<IPin>;
+
+ unsafe fn get_pin_by_name(
+ &self,
+ direction: PIN_DIRECTION,
+ name: Option<&PWSTR>,
+ ) -> Option<IPin>;
+}
+
+impl IBaseFilterExt for IBaseFilter {
+ unsafe fn get_pin(
+ &self,
+ direction: PIN_DIRECTION,
+ category: GUID,
+ major_type: GUID,
+ ) -> Option<IPin> {
+ let pin_enum = self.EnumPins().unwrap();
+
+ let _ = pin_enum.Reset();
+
+ let mut pin = [None];
+ while pin_enum.Next(&mut pin, None) == S_OK {
+ let Some(pin) = pin[0].take() else {
+ break;
+ };
+
+ let pin_dir = pin.QueryDirection().unwrap();
+
+ if pin_dir == direction {
+ if (category == GUID::zeroed() || pin.matches_category(category))
+ && (major_type == GUID::zeroed() || pin.matches_major_type(major_type))
+ {
+ return Some(pin);
+ }
+ }
+ }
+
+ None
+ }
+
+ unsafe fn get_pin_by_name(
+ &self,
+ direction: PIN_DIRECTION,
+ name: Option<&PWSTR>,
+ ) -> Option<IPin> {
+ let pin_enum = self.EnumPins().unwrap();
+
+ let _ = pin_enum.Reset();
+
+ dbg!(direction);
+
+ let mut pin = [None];
+ while pin_enum.Next(&mut pin, None) == S_OK {
+ let Some(pin) = pin[0].take() else {
+ break;
+ };
+
+ let pin_dir = pin.QueryDirection().unwrap();
+
+ dbg!(pin_dir);
+
+ if pin_dir == direction {
+ return Some(pin);
+ }
+ }
+
+ None
+ }
+}
+
+pub trait VARIANTExt {
+ unsafe fn to_os_string(&self) -> Option<OsString>;
+}
+
+impl VARIANTExt for VARIANT {
+ unsafe fn to_os_string(&self) -> Option<OsString> {
+ (self.Anonymous.Anonymous.vt == VT_BSTR)
+ .then(|| OsString::from_wide(self.Anonymous.Anonymous.Anonymous.bstrVal.deref()))
+ }
+}
+
+pub struct IAMStreamConfigMediaTypes<'a> {
+ stream_config: &'a IAMStreamConfig,
+ count: u32,
+ caps: VIDEO_STREAM_CONFIG_CAPS,
+ i: i32,
+}
+
+impl<'a> IAMStreamConfigMediaTypes<'a> {
+ pub unsafe fn next(&mut self) -> Option<(&'a AM_MEDIA_TYPEVideo, i32)> {
+ let i = self.i;
+
+ if i >= self.count as i32 {
+ return None;
+ }
+
+ self.i += 1;
+
+ let mut media_type = null_mut();
+
+ self.stream_config
+ .GetStreamCaps(i, &mut media_type, (&raw mut self.caps).cast::<u8>())
+ .unwrap();
+
+ if media_type.is_null() {
+ return None;
+ }
+
+ Some((&*media_type, i))
+ }
+
+ pub fn count(&self) -> u32 {
+ self.count
+ }
+}
+
+pub trait IAMStreamConfigExt {
+ unsafe fn media_types(&self) -> IAMStreamConfigMediaTypes;
+}
+
+impl IAMStreamConfigExt for IAMStreamConfig {
+ unsafe fn media_types(&self) -> IAMStreamConfigMediaTypes {
+ let mut count = 0;
+ self.GetNumberOfCapabilities(&mut count, &mut 0).unwrap();
+
+ IAMStreamConfigMediaTypes {
+ stream_config: &self,
+ count: count as u32,
+ caps: VIDEO_STREAM_CONFIG_CAPS::default(),
+ i: 0,
+ }
+ }
+}
+
+#[allow(non_camel_case_types)]
+type AM_MEDIA_TYPEVideo = AM_MEDIA_TYPE;
+
+#[allow(non_camel_case_types)]
+pub trait AM_MEDIA_TYPEVideoExt {
+ unsafe fn video_info(&self) -> &KS_VIDEOINFOHEADER;
+}
+
+impl AM_MEDIA_TYPEVideoExt for AM_MEDIA_TYPEVideo {
+ unsafe fn video_info(&self) -> &KS_VIDEOINFOHEADER {
+ &*self.pbFormat.cast::<KS_VIDEOINFOHEADER>()
+ }
+}
+
+#[allow(non_camel_case_types)]
+pub trait AM_MEDIA_TYPEExt {
+ unsafe fn subtype_str(&self) -> Option<&'static str>;
+}
+
+impl AM_MEDIA_TYPEExt for AM_MEDIA_TYPE {
+ unsafe fn subtype_str(&self) -> Option<&'static str> {
+ Some(match self.subtype {
+ t if t == MEDIASUBTYPE_I420 => "i420",
+ t if t == MEDIASUBTYPE_IYUV => "iyuv",
+ t if t == MEDIASUBTYPE_RGB24 => "rgb24",
+ t if t == MEDIASUBTYPE_RGB32 => "rgb32",
+ t if t == MEDIASUBTYPE_YUY2 => "yuy2",
+ t if t == MEDIASUBTYPE_MJPG => "mjpg",
+ t if t == MEDIASUBTYPE_UYVY => "uyvy",
+ t if t == MEDIASUBTYPE_ARGB32 => "argb32",
+ t if t == MEDIASUBTYPE_NV12 => "nv12",
+ _ => return None,
+ })
+ }
+}
+
+pub trait IAMVideoControlExt {
+ unsafe fn time_per_frame_list<'a>(&self, pin: &'a IPin, i: i32, dimensions: SIZE) -> &'a [i64];
+}
+
+impl IAMVideoControlExt for IAMVideoControl {
+ unsafe fn time_per_frame_list<'a>(&self, pin: &'a IPin, i: i32, dimensions: SIZE) -> &'a [i64] {
+ let mut time_per_frame_list = null_mut();
+ let mut list_size = 0;
+
+ self.GetFrameRateList(pin, i, dimensions, &mut list_size, &mut time_per_frame_list)
+ .unwrap();
+
+ if list_size > 0 && !time_per_frame_list.is_null() {
+ return std::slice::from_raw_parts(time_per_frame_list, list_size as usize);
+ }
+
+ &[]
+ }
+}
+
+pub trait IPropertyBagExt {
+ unsafe fn read<P0, P2>(&self, pszpropname: P0, perrorlog: P2) -> windows_core::Result<VARIANT>
+ where
+ P0: windows_core::Param<windows_core::PCWSTR>,
+ P2: windows_core::Param<IErrorLog>;
+}
+
+impl IPropertyBagExt for IPropertyBag {
+ unsafe fn read<P0, P2>(&self, pszpropname: P0, perrorlog: P2) -> windows_core::Result<VARIANT>
+ where
+ P0: windows_core::Param<windows_core::PCWSTR>,
+ P2: windows_core::Param<IErrorLog>,
+ {
+ let mut ret = VARIANT::default();
+ self.Read(pszpropname, &mut ret, perrorlog)?;
+ Ok(ret)
+ }
+}
+
+type VideoInputDeviceIMoniker = IMoniker;
+
+pub struct VideoInputDeviceEnumerator {
+ enum_moniker: IEnumMoniker,
+ moniker: [Option<VideoInputDeviceIMoniker>; 1],
+}
+
+impl VideoInputDeviceEnumerator {
+ pub unsafe fn new() -> windows_core::Result<Self> {
+ let create_device_enum: ICreateDevEnum = CoCreateInstance(
+ &CLSID_SystemDeviceEnum,
+ None::<&windows_core::IUnknown>,
+ CLSCTX_INPROC_SERVER,
+ )?;
+
+ let mut enum_moniker = None;
+
+ create_device_enum.CreateClassEnumerator(
+ &CLSID_VideoInputDeviceCategory,
+ &mut enum_moniker,
+ 0,
+ )?;
+
+ Ok(Self {
+ enum_moniker: enum_moniker.unwrap(),
+ moniker: [None],
+ })
+ }
+
+ pub unsafe fn next(&mut self) -> Option<VideoInputDeviceIMoniker> {
+ if self.enum_moniker.Next(&mut self.moniker, None) == S_OK {
+ return self.moniker[0].take();
+ }
+
+ None
+ }
+
+ pub unsafe fn to_vec(mut self) -> Vec<VideoInputDeviceIMoniker> {
+ let mut monikers = Vec::new();
+ while let Some(moniker) = self.next() {
+ monikers.push(moniker);
+ }
+ monikers
+ }
+}
diff --git a/crates/camera-mediafoundation/Cargo.toml b/crates/camera-mediafoundation/Cargo.toml
new file mode 100644
index 000000000..7e3755981
--- /dev/null
+++ b/crates/camera-mediafoundation/Cargo.toml
@@ -0,0 +1,17 @@
+[package]
+name = "camera-mediafoundation"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+windows-core = { version = "0.60" }
+windows = { version = "0.60", features = [
+ "Win32_Media_MediaFoundation",
+ "Win32_Media_DirectShow",
+ "Win32_System_Com",
+] }
+tracing.workspace = true
+inquire = "0.7.5"
+
+[lints]
+workspace = true
diff --git a/crates/camera-mediafoundation/examples/cli.rs b/crates/camera-mediafoundation/examples/cli.rs
new file mode 100644
index 000000000..597cc02d3
--- /dev/null
+++ b/crates/camera-mediafoundation/examples/cli.rs
@@ -0,0 +1,303 @@
+use std::{
+ borrow::Cow,
+ ffi::{OsStr, OsString},
+ fmt::Display,
+ mem::MaybeUninit,
+ ops::{Deref, DerefMut},
+ os::windows::ffi::{OsStrExt, OsStringExt},
+ slice::{self, from_raw_parts},
+ time::Duration,
+};
+
+use tracing::warn;
+use windows::Win32::{Media::MediaFoundation::*, System::Com::CoInitialize};
+use windows_core::{GUID, Interface};
+use windows_core::{PCWSTR, PWSTR};
+
+pub fn main() {
+ std::thread::spawn(|| unsafe {
+ CoInitialize(None).unwrap();
+
+ let mut attributes = None;
+ MFCreateAttributes(&mut attributes, 1).unwrap();
+ let attributes = attributes.unwrap();
+
+ attributes
+ .SetGUID(
+ &MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
+ &MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID,
+ )
+ .unwrap();
+
+ let mut count = 0;
+ let mut devices: MaybeUninit<*mut Option<IMFActivate>> = MaybeUninit::uninit();
+
+ MFEnumDeviceSources(&attributes, devices.as_mut_ptr(), &mut count).unwrap();
+
+ if count == 0 {
+ warn!("No devices found");
+ return;
+ }
+
+ let devices = devices.assume_init();
+
+ let mut device_list = vec![];
+
+ for i in 0..count {
+ let Some(device) = &mut *devices.add(i as usize) else {
+ continue;
+ };
+
+ let mut name_raw = PWSTR(&mut 0);
+ let mut name_length = 0;
+ device
+ .GetAllocatedString(
+ &MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,
+ &mut name_raw,
+ &mut name_length,
+ )
+ .unwrap();
+ let name = OsString::from_wide(from_raw_parts(name_raw.0, name_length as usize));
+
+ let mut id_raw = PWSTR(&mut 0);
+ let mut id_length = 0;
+ device
+ .GetAllocatedString(
+ &MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,
+ &mut id_raw,
+ &mut id_length,
+ )
+ .unwrap();
+ let id = OsString::from_wide(from_raw_parts(id_raw.0, id_length as usize));
+
+ let device = device.ActivateObject::<IMFMediaSource>().unwrap();
+
+ device_list.push(Device {
+ device,
+ id_raw,
+ name_raw,
+ id,
+ name,
+ })
+ }
+
+ let selected = if device_list.len() > 1 {
+ inquire::Select::new("Select a device", device_list)
+ .prompt()
+ .unwrap()
+ } else {
+ device_list.remove(0)
+ };
+
+ let reader = MFCreateSourceReaderFromMediaSource(&selected.device, None).unwrap();
+ let mut stream_index = 0;
+
+ let mut formats = vec![];
+
+ while let Ok(typ) =
+ reader.GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM.0 as u32, stream_index)
+ {
+ let format = Format::new(typ).unwrap();
+ formats.push(format);
+
+ stream_index += 1;
+ }
+
+ let mut selected_format = if formats.len() > 1 {
+ inquire::Select::new("Select a format", formats)
+ .prompt()
+ .unwrap()
+ } else {
+ formats.remove(0)
+ };
+
+ reader
+ .SetCurrentMediaType(
+ MF_SOURCE_READER_FIRST_VIDEO_STREAM.0 as u32,
+ None,
+ &selected_format.inner,
+ )
+ .unwrap();
+
+ reader
+ .SetStreamSelection(MF_SOURCE_READER_FIRST_VIDEO_STREAM.0 as u32, true)
+ .unwrap();
+
+ let stream_index = MF_SOURCE_READER_FIRST_VIDEO_STREAM.0 as u32;
+ loop {
+ let mut imf_sample = None;
+ let mut stream_flags = 0;
+ let imf_sample = loop {
+ reader
+ .ReadSample(
+ stream_index,
+ 0,
+ None,
+ Some(&mut stream_flags),
+ None,
+ Some(&mut imf_sample),
+ )
+ .unwrap();
+
+ if let Some(imf_sample) = imf_sample {
+ break imf_sample;
+ }
+ };
+
+ let pts = imf_sample.GetSampleTime().unwrap();
+ let bytes = imf_sample.GetTotalLength().unwrap();
+ if stream_flags as i32 & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED.0
+ == MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED.0
+ {
+ selected_format =
+ Format::new(reader.GetCurrentMediaType(stream_index).unwrap()).unwrap();
+ }
+
+ println!(
+ "New frame: {}x{}, {pts}pts, {bytes} bytes, {}",
+ selected_format.width,
+ selected_format.height,
+ media_subtype_str(&selected_format.subtype).unwrap_or("unknown format")
+ );
+ }
+ });
+
+ std::thread::sleep(Duration::from_secs(10));
+}
+
+struct Device {
+ device: IMFMediaSource,
+ id_raw: PWSTR,
+ name_raw: PWSTR,
+ id: OsString,
+ name: OsString,
+}
+
+impl Device {
+ fn device_id(&self) -> String {
+ self.id.to_string_lossy().to_string()
+ }
+
+ fn display_name(&self) -> String {
+ self.name.to_string_lossy().to_string()
+ }
+
+ fn model_id(&self) -> String {
+ get_device_model_id(&self.device_id())
+ }
+}
+
+impl Deref for Device {
+ type Target = IMFMediaSource;
+
+ fn deref(&self) -> &Self::Target {
+ &self.device
+ }
+}
+
+impl DerefMut for Device {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.device
+ }
+}
+
+impl Display for Device {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}", self.display_name())
+ }
+}
+
+struct Format {
+ inner: IMFMediaType,
+ width: u32,
+ height: u32,
+ frame_rate_ratio: (u32, u32),
+ frame_rate: f32,
+ subtype: GUID,
+}
+
+impl Format {
+ fn new(inner: IMFMediaType) -> windows_core::Result<Self> {
+ let size = unsafe { inner.GetUINT64(&MF_MT_FRAME_SIZE)? };
+ let width = (size >> 32) as u32;
+ let height = (size & 0xFFFFFFFF) as u32;
+
+ let frame_rate_ratio = {
+ let frame_rate = unsafe { inner.GetUINT64(&MF_MT_FRAME_RATE)? };
+ let numerator = (frame_rate >> 32) as u32;
+ let denominator = frame_rate as u32;
+ (numerator, denominator)
+ };
+ let frame_rate = frame_rate_ratio.0 as f32 / frame_rate_ratio.1 as f32;
+
+ let subtype = unsafe { inner.GetGUID(&MF_MT_SUBTYPE)? };
+
+ Ok(Self {
+ inner,
+ width,
+ height,
+ frame_rate_ratio,
+ frame_rate,
+ subtype,
+ })
+ }
+}
+
+impl Display for Format {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}x{} {}fps", self.width, self.height, self.frame_rate)?;
+ if self.frame_rate_ratio.1 != 1 {
+ write!(
+ f,
+ " ({}/{})",
+ self.frame_rate_ratio.0, self.frame_rate_ratio.1
+ )?
+ }
+ write!(
+ f,
+ " {}",
+ media_subtype_str(&self.subtype).unwrap_or("unknown format")
+ )
+ }
+}
+
+fn get_device_model_id(device_id: &str) -> String {
+ const VID_PID_SIZE: usize = 4;
+
+ let vid_location = device_id.find("vid_");
+ let pid_location = device_id.find("pid_");
+
+ let Some(vid_location) = vid_location else {
+ return String::new();
+ };
+ let Some(pid_location) = pid_location else {
+ return String::new();
+ };
+
+ if vid_location + "vid_".len() + 4 > device_id.len()
+ || pid_location + "pid_".len() + 4 > device_id.len()
+ {
+ return String::new();
+ }
+
+ let id_vendor = &device_id[vid_location + 4..vid_location + 8];
+ let id_product = &device_id[pid_location + 4..pid_location + 8];
+
+ format!("{id_vendor}:{id_product}")
+}
+
+fn media_subtype_str(subtype: &GUID) -> Option<&'static str> {
+ Some(match *subtype {
+ t if t == MFVideoFormat_I420 => "i420",
+ t if t == MFVideoFormat_IYUV => "iyuv",
+ t if t == MFVideoFormat_RGB24 => "rgb24",
+ t if t == MFVideoFormat_RGB32 => "rgb32",
+ t if t == MFVideoFormat_YUY2 => "yuy2",
+ t if t == MFVideoFormat_MJPG => "mjpg",
+ t if t == MFVideoFormat_UYVY => "uyvy",
+ t if t == MFVideoFormat_ARGB32 => "argb32",
+ t if t == MFVideoFormat_NV12 => "nv12",
+ t if t == MFVideoFormat_H264 => "h264",
+ _ => return None,
+ })
+}
diff --git a/crates/camera-mediafoundation/src/lib.rs b/crates/camera-mediafoundation/src/lib.rs
new file mode 100644
index 000000000..9ec07beae
--- /dev/null
+++ b/crates/camera-mediafoundation/src/lib.rs
@@ -0,0 +1 @@
+#![cfg(windows)]