Skip to content

Commit

Permalink
win_mf: remove CameraFrame struct
Browse files Browse the repository at this point in the history
  • Loading branch information
payload committed Jul 30, 2023
1 parent e232d05 commit 259603f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 deletions.
18 changes: 6 additions & 12 deletions src/win_mf/camera.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use super::mf::{
self, activate_to_media_source, capture_engine_prepare_sample_callback,
capture_engine_sink_get_media_type, capture_engine_stop_preview, co_initialize_multithreaded,
enum_device_sources, init_capture_engine, new_capture_engine, sample_to_locked_buffer,
CameraFrame, CaptureEngineEvent, CaptureEventCallback, CaptureSampleCallback,
};
use super::mf::*;

use std::sync::mpsc::*;

Expand All @@ -23,7 +18,7 @@ pub struct Camera {

#[derive(Debug)]
pub struct Frame {
frame: mf::CameraFrame,
buffer: LockedBuffer,
}

pub struct FrameData<'a> {
Expand All @@ -33,7 +28,7 @@ pub struct FrameData<'a> {
impl Camera {
pub fn new_default_device() -> Self {
co_initialize_multithreaded();
mf::media_foundation_startup().expect("media_foundation_startup");
media_foundation_startup().expect("media_foundation_startup");

let engine = new_capture_engine().unwrap();
let (event_tx, event_rx) = channel::<CaptureEngineEvent>();
Expand Down Expand Up @@ -75,8 +70,7 @@ impl Camera {
let height = mt.frame_height();
sample_to_locked_buffer(&sample, width, height).ok()
})
.map(|sample| CameraFrame { sample })
.map(|frame| Frame { frame })
.map(|buffer: LockedBuffer| Frame { buffer })
}
}

Expand All @@ -92,11 +86,11 @@ impl Camera {

impl Frame {
pub fn data(&self) -> FrameData {
FrameData { data: self.frame.data() }
FrameData { data: self.buffer.data() }
}

pub fn size_u32(&self) -> (u32, u32) {
self.frame.size_u32()
(self.buffer.width, self.buffer.height)
}
}

Expand Down
27 changes: 3 additions & 24 deletions src/win_mf/mf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,6 @@ impl Device {
}
}

#[derive(Debug)]
pub struct CameraFrame {
pub sample: LockedBuffer,
}

impl CameraFrame {
pub fn data(&self) -> &[u8] {
self.sample.data()
}

pub fn size_u32(&self) -> (u32, u32) {
(self.sample.width, self.sample.height)
}
}

impl std::fmt::Display for CameraFrame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}x{} {}", self.sample.width, self.sample.height, self.sample.len)
}
}

pub(crate) fn enum_device_sources() -> Vec<IMFActivate> {
unsafe {
let source_type = &MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE;
Expand Down Expand Up @@ -248,14 +227,14 @@ pub fn sample_to_locked_buffer(
#[derive(Debug)]
pub struct LockedBuffer {
buffer: IMF2DBuffer2,
width: u32,
height: u32,
pub(crate) width: u32,
pub(crate) height: u32,
scanline0: *mut u8,
len: usize,
}

impl LockedBuffer {
fn data(&self) -> &[u8] {
pub(crate) fn data(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.scanline0, self.len) }
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ fn frame_data() {
assert_eq!(a, b);
}

#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
// linux_v4l2: ioctl VIDIOC_REQBUFS fails with Device Busy, Chromium also fails in this case, no alternative on this level
// win_mf: fails to get frames because "The video recording device is preempted by another immersice application"
#[test]
fn two_cameras_start_and_wait_for_frames() {
let camera1 = Camera::new_default_device();
Expand Down

0 comments on commit 259603f

Please sign in to comment.