forked from CapSoftware/Cap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.rs
More file actions
168 lines (140 loc) · 5.07 KB
/
Copy pathcli.rs
File metadata and controls
168 lines (140 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
fn main() {
#[cfg(windows)]
windows::main();
#[cfg(not(windows))]
panic!("This example is only available on Windows");
}
#[cfg(windows)]
mod windows {
use cap_camera_directshow::*;
use std::{fmt::Display, time::Duration};
use tracing::error;
use windows::{
Win32::{
Foundation::SIZE,
Media::{DirectShow::*, MediaFoundation::*},
System::Com::*,
},
core::Interface,
};
pub fn main() {
tracing_subscriber::fmt::init();
unsafe {
CoInitialize(None).unwrap();
let devices = VideoInputDeviceIterator::new().unwrap().collect::<Vec<_>>();
let mut devices = devices
.into_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 device = selected.0;
let output_pin = device
.output_pin()
.expect("failed to bind capture filter for selected device")
.clone();
let video_control = output_pin.cast::<IAMVideoControl>().ok();
let formats = device
.media_types()
.unwrap()
.enumerate()
.filter_map(|(i, media_type)| {
let is_video = media_type.majortype == MEDIATYPE_Video
&& media_type.formattype == FORMAT_VideoInfo;
if !is_video {
return None;
}
let video_info = media_type.video_info();
let width = video_info.bmiHeader.biWidth;
let height = video_info.bmiHeader.biHeight;
let mut frame_rates = vec![];
if let Some(video_control) = &video_control {
let time_per_frame_list = video_control.time_per_frame_list(
&output_pin,
i as i32,
SIZE {
cx: width,
cy: height,
},
);
for time_per_frame in time_per_frame_list {
if *time_per_frame <= 0 {
return None;
}
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);
Some(Format {
width,
height,
media_type,
frame_rates,
})
})
.collect::<Vec<_>>();
if formats.is_empty() {
error!("No formats found");
return;
}
let selected_format = inquire::Select::new("Select a format", formats)
.prompt()
.unwrap();
device
.start_capturing(
&selected_format.media_type,
Box::new(|frame| {
let data_length = frame.sample.GetActualDataLength();
println!(
"Frame: data_length={data_length:?}, timestamp={:?}",
frame.timestamp
);
}),
)
.unwrap();
std::thread::sleep(Duration::from_secs(10));
}
}
#[derive(Debug)]
struct Format {
width: i32,
height: i32,
media_type: AMMediaType,
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()
.map(|v| v.to_string())
.unwrap_or(format!("unknown ({:?})", self.media_type.subtype))
},
&self.frame_rates
)
}
}
struct VideoDeviceSelectOption(VideoInputDevice);
impl Display for VideoDeviceSelectOption {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0.name().unwrap())
}
}
}