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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#[cfg(windows)]
extern crate dxgcap;
#[cfg(not(windows))]
extern crate x11cap;
#[cfg(windows)]
use std::time::Duration;
#[cfg(windows)]
pub type Bgr8 = dxgcap::BGRA8;
#[cfg(not(windows))]
pub type Bgr8 = x11cap::Bgr8;
#[derive(Clone, Debug)]
pub enum CaptureError {
#[cfg(windows)]
AccessDenied,
AccessLost,
#[cfg(windows)]
RefreshFailure,
Timeout,
Fail(String),
}
#[cfg(windows)]
pub struct Capturer {
dxgi_manager: dxgcap::DXGIManager,
width: usize,
height: usize,
image: Option<Vec<Bgr8>>,
}
#[cfg(not(windows))]
pub struct Capturer {
x11_capturer: x11cap::Capturer,
pub image: Option<x11cap::Image>,
}
impl Capturer {
#[cfg(windows)]
pub fn new(capture_src: usize) -> Result<Capturer, String> {
Capturer::new_with_timeout(capture_src, Duration::from_millis(200))
}
#[cfg(windows)]
pub fn new_with_timeout(capture_src: usize, timeout: Duration) -> Result<Capturer, String> {
(timeout.as_secs() as u32)
.checked_mul(1000)
.and_then(|ms| ms.checked_add(timeout.subsec_nanos() / 1_000_000))
.ok_or("Failed to convert the given duration to a legal u32 millisecond value due to \
integer overflow.")
.and_then(|timeout| {
dxgcap::DXGIManager::new(timeout).map(|mut mgr| {
mgr.set_capture_source_index(capture_src);
Capturer {
dxgi_manager: mgr,
width: 0,
height: 0,
image: None,
}
})
})
.map_err(|err| err.to_owned())
}
#[cfg(not(windows))]
pub fn new(capture_src: usize) -> Result<Capturer, String> {
x11cap::Capturer::new(x11cap::CaptureSource::Monitor(capture_src))
.map(|c| Capturer { x11_capturer: c, image: None })
.map_err(|()| "Failed to initialize capturer".to_string())
}
#[cfg(windows)]
pub fn geometry(&self) -> (u32, u32) {
(self.width as u32, self.height as u32)
}
#[cfg(not(windows))]
pub fn geometry(&self) -> (u32, u32) {
let geo = self.x11_capturer.get_geometry();
(geo.width, geo.height)
}
#[cfg(not(windows))]
pub fn position(&self) -> (i32, i32) {
let geo = self.x11_capturer.get_geometry();
(geo.x, geo.y)
}
#[cfg(windows)]
pub fn capture_frame(&mut self) -> Result<Vec<Bgr8>, CaptureError> {
use dxgcap::CaptureError::*;
match self.dxgi_manager.capture_frame() {
Ok((data, (w, h))) => {
self.width = w;
self.height = h;
Ok(data)
}
Err(AccessDenied) => Err(CaptureError::AccessDenied),
Err(AccessLost) => Err(CaptureError::AccessLost),
Err(RefreshFailure) => Err(CaptureError::RefreshFailure),
Err(Timeout) => Err(CaptureError::Timeout),
Err(Fail(e)) => Err(CaptureError::Fail(e.to_string())),
}
}
#[cfg(windows)]
pub fn capture_store_frame(&mut self) -> Result<(), CaptureError> {
use dxgcap::CaptureError::*;
match self.dxgi_manager.capture_frame() {
Ok((data, (w, h))) => {
self.image = Some(data);
self.width = w;
self.height = h;
Ok(())
}
Err(AccessDenied) => Err(CaptureError::AccessDenied),
Err(AccessLost) => Err(CaptureError::AccessLost),
Err(RefreshFailure) => Err(CaptureError::RefreshFailure),
Err(Timeout) => Err(CaptureError::Timeout),
Err(Fail(e)) => Err(CaptureError::Fail(e.to_string())),
}
}
#[cfg(not(windows))]
pub fn capture_frame(&mut self) -> Result<Vec<Bgr8>, CaptureError> {
self.capture_store_frame()
.map(|_| self.get_stored_frame().unwrap().to_vec())
}
#[cfg(not(windows))]
pub fn capture_store_frame(&mut self) -> Result<(), CaptureError> {
match self.x11_capturer.capture_frame() {
Ok(image) => {
self.image = Some(image);
Ok(())
}
Err(x11cap::CaptureError::Fail(e)) => Err(CaptureError::Fail(e.to_string())),
}
}
pub fn get_stored_frame(&self) -> Option<&[Bgr8]> {
self.image.as_ref().map(|img| img.as_slice())
}
}