/
simple.rs
174 lines (149 loc) · 4.54 KB
/
simple.rs
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
use super::model::Capturer;
use crate::duplication_context::DuplicationContext;
use crate::error::Error;
use crate::model::Result;
use crate::utils::OutDuplDescExt;
use windows::Win32::Graphics::Direct3D11::D3D11_TEXTURE2D_DESC;
use windows::Win32::Graphics::Dxgi::{
DXGI_OUTDUPL_DESC, DXGI_OUTDUPL_FRAME_INFO, DXGI_OUTDUPL_POINTER_SHAPE_INFO,
};
use windows::Win32::Graphics::{Direct3D11::ID3D11Texture2D, Dxgi::DXGI_OUTPUT_DESC};
/// Capture screen to a `Vec<u8>`.
pub struct SimpleCapturer<'a> {
buffer: Vec<u8>,
ctx: &'a DuplicationContext,
texture: ID3D11Texture2D,
texture_desc: D3D11_TEXTURE2D_DESC,
pointer_shape_buffer: Vec<u8>,
pointer_shape_buffer_size: usize,
}
impl<'a> SimpleCapturer<'a> {
pub fn new(ctx: &'a DuplicationContext) -> Result<Self> {
let (buffer, texture, texture_desc) = Self::allocate(ctx)?;
Ok(Self {
buffer,
ctx,
texture,
texture_desc,
pointer_shape_buffer: Vec::new(),
pointer_shape_buffer_size: 0,
})
}
fn allocate(
ctx: &'a DuplicationContext,
) -> Result<(Vec<u8>, ID3D11Texture2D, D3D11_TEXTURE2D_DESC)> {
let (texture, desc, texture_desc) = ctx.create_readable_texture()?;
let buffer = vec![0u8; desc.calc_buffer_size()];
Ok((buffer, texture, texture_desc))
}
}
impl Capturer for SimpleCapturer<'_> {
fn dxgi_output_desc(&self) -> Result<DXGI_OUTPUT_DESC> {
self.ctx.dxgi_output_desc()
}
fn dxgi_outdupl_desc(&self) -> DXGI_OUTDUPL_DESC {
self.ctx.dxgi_outdupl_desc()
}
fn buffer(&self) -> &[u8] {
&self.buffer
}
fn buffer_mut(&mut self) -> &mut [u8] {
&mut self.buffer
}
fn check_buffer(&self) -> Result<()> {
if self.buffer.len() < self.dxgi_outdupl_desc().calc_buffer_size() {
Err(Error::new("Invalid buffer length"))
} else {
Ok(())
}
}
fn pointer_shape_buffer(&self) -> &[u8] {
&self.pointer_shape_buffer[..self.pointer_shape_buffer_size]
}
fn capture(&mut self) -> Result<DXGI_OUTDUPL_FRAME_INFO> {
self.ctx.capture(
self.buffer.as_mut_ptr(),
self.buffer.len(),
&self.texture,
&self.texture_desc,
)
}
fn safe_capture(&mut self) -> Result<DXGI_OUTDUPL_FRAME_INFO> {
self.check_buffer()?;
self.capture()
}
fn capture_with_pointer_shape(
&mut self,
) -> Result<(
DXGI_OUTDUPL_FRAME_INFO,
Option<DXGI_OUTDUPL_POINTER_SHAPE_INFO>,
)> {
let (frame_info, pointer_shape_info) = self.ctx.capture_with_pointer_shape(
self.buffer.as_mut_ptr(),
self.buffer.len(),
&self.texture,
&self.texture_desc,
&mut self.pointer_shape_buffer,
)?;
if pointer_shape_info.is_some() {
// record the pointer shape buffer size
self.pointer_shape_buffer_size = frame_info.PointerShapeBufferSize as usize;
}
Ok((frame_info, pointer_shape_info))
}
fn safe_capture_with_pointer_shape(
&mut self,
) -> Result<(
DXGI_OUTDUPL_FRAME_INFO,
Option<DXGI_OUTDUPL_POINTER_SHAPE_INFO>,
)> {
self.check_buffer()?;
self.capture_with_pointer_shape()
}
}
impl DuplicationContext {
pub fn simple_capturer(&self) -> Result<SimpleCapturer> {
SimpleCapturer::new(self)
}
}
#[cfg(test)]
mod tests {
use std::{thread, time::Duration};
use crate::{capturer::model::Capturer, manager::Manager, utils::FrameInfoExt};
#[test]
fn simple_capturer() {
let manager = Manager::default().unwrap();
assert_ne!(manager.contexts.len(), 0);
let mut capturer = manager.contexts[0].simple_capturer().unwrap();
// sleep for a while before capture to wait system to update the screen
thread::sleep(Duration::from_millis(100));
let info = capturer.safe_capture().unwrap();
assert!(info.desktop_updated());
let buffer = capturer.buffer();
// ensure buffer not all zero
let mut all_zero = true;
for i in 0..buffer.len() {
if buffer[i] != 0 {
all_zero = false;
break;
}
}
assert!(!all_zero);
// sleep for a while before capture to wait system to update the mouse
thread::sleep(Duration::from_millis(1000));
// check pointer shape
let (frame_info, pointer_shape_info) = capturer.safe_capture_with_pointer_shape().unwrap();
assert!(frame_info.mouse_updated().position_updated);
assert!(pointer_shape_info.is_some());
let pointer_shape_data = capturer.pointer_shape_buffer();
// make sure pointer shape buffer is not all zero
let mut all_zero = true;
for i in 0..pointer_shape_data.len() {
if pointer_shape_data[i] != 0 {
all_zero = false;
break;
}
}
assert!(!all_zero);
}
}