Skip to content

Commit f863d76

Browse files
committed
imfsample -> avpacket wtf
1 parent 781d3d3 commit f863d76

File tree

24 files changed

+794
-183
lines changed

24 files changed

+794
-183
lines changed

Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/camera-mediafoundation/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ tracing.workspace = true
99
thiserror.workspace = true
1010

1111
[target.'cfg(windows)'.dependencies]
12+
cap-mediafoundation-utils = { path = "../mediafoundation-utils" }
1213
windows-core = { workspace = true }
1314
windows = { workspace = true, features = [
14-
"Win32_Media_MediaFoundation",
15-
"Win32_Media_DirectShow",
16-
"Win32_System_Com",
15+
"Win32_Media_MediaFoundation",
16+
"Win32_Media_DirectShow",
17+
"Win32_System_Com",
1718
] }
1819

1920
[dev-dependencies]

crates/camera-mediafoundation/src/lib.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
#![cfg(windows)]
22
#![allow(non_snake_case)]
33

4+
use cap_mediafoundation_utils::*;
45
use std::{
56
ffi::OsString,
67
fmt::Display,
78
mem::MaybeUninit,
89
ops::{Deref, DerefMut},
910
os::windows::ffi::OsStringExt,
10-
ptr::null_mut,
1111
slice::from_raw_parts,
1212
sync::{
1313
Mutex,
1414
mpsc::{Receiver, Sender, channel},
1515
},
1616
time::{Duration, Instant},
1717
};
18-
1918
use tracing::error;
2019
use windows::Win32::{
2120
Foundation::{S_FALSE, *},
@@ -541,51 +540,6 @@ fn get_device_model_id(device_id: &str) -> Option<String> {
541540
Some(format!("{id_vendor}:{id_product}"))
542541
}
543542

544-
pub trait IMFMediaBufferExt {
545-
fn lock(&self) -> windows_core::Result<IMFMediaBufferLock<'_>>;
546-
}
547-
548-
impl IMFMediaBufferExt for IMFMediaBuffer {
549-
fn lock(&self) -> windows_core::Result<IMFMediaBufferLock<'_>> {
550-
let mut bytes_ptr = null_mut();
551-
let mut size = 0;
552-
553-
unsafe {
554-
self.Lock(&mut bytes_ptr, None, Some(&mut size))?;
555-
}
556-
557-
Ok(IMFMediaBufferLock {
558-
source: self,
559-
bytes: unsafe { std::slice::from_raw_parts_mut(bytes_ptr, size as usize) },
560-
})
561-
}
562-
}
563-
564-
pub struct IMFMediaBufferLock<'a> {
565-
source: &'a IMFMediaBuffer,
566-
bytes: &'a mut [u8],
567-
}
568-
569-
impl<'a> Drop for IMFMediaBufferLock<'a> {
570-
fn drop(&mut self) {
571-
let _ = unsafe { self.source.Unlock() };
572-
}
573-
}
574-
575-
impl<'a> Deref for IMFMediaBufferLock<'a> {
576-
type Target = [u8];
577-
578-
fn deref(&self) -> &Self::Target {
579-
self.bytes
580-
}
581-
}
582-
583-
impl<'a> DerefMut for IMFMediaBufferLock<'a> {
584-
fn deref_mut(&mut self) -> &mut Self::Target {
585-
self.bytes
586-
}
587-
}
588-
589543
pub struct CallbackData {
590544
pub sample: IMFSample,
591545
pub reference_time: Instant,

crates/camera-windows/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ thiserror.workspace = true
99
inquire = "0.7.5"
1010

1111
[target.'cfg(windows)'.dependencies]
12+
cap-mediafoundation-utils = { path = "../mediafoundation-utils" }
1213
cap-camera-mediafoundation = { path = "../camera-mediafoundation" }
1314
cap-camera-directshow = { path = "../camera-directshow" }
1415

crates/camera-windows/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg(windows)]
22

33
use cap_camera_directshow::{AM_MEDIA_TYPEVideoExt, AMMediaType};
4-
use cap_camera_mediafoundation::{IMFMediaBufferExt, IMFMediaBufferLock};
4+
use cap_mediafoundation_utils::*;
55
use std::{
66
ffi::{OsStr, OsString},
77
fmt::{Debug, Display},

crates/cpal-ffmpeg/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2024"
66
[dependencies]
77
cpal.workspace = true
88
ffmpeg.workspace = true
9+
cap-ffmpeg-utils = { path = "../ffmpeg-utils" }
910

1011
[lints]
1112
workspace = true

crates/cpal-ffmpeg/src/lib.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cap_ffmpeg_utils::PlanarData;
12
use cpal::{SampleFormat, StreamConfig};
23
use ffmpeg::format::{Sample, sample};
34

@@ -43,39 +44,3 @@ impl DataExt for ::cpal::Data {
4344
ffmpeg_frame
4445
}
4546
}
46-
47-
pub trait PlanarData {
48-
fn plane_data(&self, index: usize) -> &[u8];
49-
50-
fn plane_data_mut(&mut self, index: usize) -> &mut [u8];
51-
}
52-
53-
impl PlanarData for ffmpeg::frame::Audio {
54-
#[inline]
55-
fn plane_data(&self, index: usize) -> &[u8] {
56-
if index >= self.planes() {
57-
panic!("out of bounds");
58-
}
59-
60-
unsafe {
61-
std::slice::from_raw_parts(
62-
(*self.as_ptr()).data[index],
63-
(*self.as_ptr()).linesize[0] as usize,
64-
)
65-
}
66-
}
67-
68-
#[inline]
69-
fn plane_data_mut(&mut self, index: usize) -> &mut [u8] {
70-
if index >= self.planes() {
71-
panic!("out of bounds");
72-
}
73-
74-
unsafe {
75-
std::slice::from_raw_parts_mut(
76-
(*self.as_mut_ptr()).data[index],
77-
(*self.as_ptr()).linesize[0] as usize,
78-
)
79-
}
80-
}
81-
}

crates/ffmpeg-utils/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "cap-ffmpeg-utils"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
ffmpeg.workspace = true
8+
9+
[lints]
10+
workspace = true

crates/ffmpeg-utils/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pub trait PlanarData {
2+
fn plane_data(&self, index: usize) -> &[u8];
3+
fn plane_data_mut(&mut self, index: usize) -> &mut [u8];
4+
}
5+
6+
impl PlanarData for ffmpeg::frame::Audio {
7+
#[inline]
8+
fn plane_data(&self, index: usize) -> &[u8] {
9+
if index >= self.planes() {
10+
panic!("out of bounds");
11+
}
12+
13+
unsafe {
14+
std::slice::from_raw_parts(
15+
(*self.as_ptr()).data[index],
16+
(*self.as_ptr()).linesize[0] as usize,
17+
)
18+
}
19+
}
20+
21+
#[inline]
22+
fn plane_data_mut(&mut self, index: usize) -> &mut [u8] {
23+
if index >= self.planes() {
24+
panic!("out of bounds");
25+
}
26+
27+
unsafe {
28+
std::slice::from_raw_parts_mut(
29+
(*self.as_mut_ptr()).data[index],
30+
(*self.as_ptr()).linesize[0] as usize,
31+
)
32+
}
33+
}
34+
}

crates/media-info/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ ffmpeg.workspace = true
88
thiserror.workspace = true
99
cpal.workspace = true
1010

11+
cap-ffmpeg-utils = { path = "../ffmpeg-utils" }
12+
1113
[lints]
1214
workspace = true

0 commit comments

Comments
 (0)