-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathmod.rs
More file actions
175 lines (149 loc) · 4.82 KB
/
mod.rs
File metadata and controls
175 lines (149 loc) · 4.82 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
169
170
171
172
173
174
175
pub mod engine;
use std::{error::Error, sync::mpsc};
use engine::ChannelItem;
use crate::{
frame::{Frame, FrameType, VideoFrame},
has_permission, is_supported,
targets::Target,
};
pub use engine::get_output_frame_size;
#[derive(Debug, Clone, Copy, Default)]
pub enum Resolution {
_480p,
_720p,
_1080p,
_1440p,
_2160p,
_4320p,
#[default]
Captured,
}
impl Resolution {
fn value(&self, aspect_ratio: f32) -> [u32; 2] {
match *self {
Resolution::_480p => [640, (640_f32 / aspect_ratio).floor() as u32],
Resolution::_720p => [1280, (1280_f32 / aspect_ratio).floor() as u32],
Resolution::_1080p => [1920, (1920_f32 / aspect_ratio).floor() as u32],
Resolution::_1440p => [2560, (2560_f32 / aspect_ratio).floor() as u32],
Resolution::_2160p => [3840, (3840_f32 / aspect_ratio).floor() as u32],
Resolution::_4320p => [7680, (7680_f32 / aspect_ratio).floor() as u32],
Resolution::Captured => {
panic!(".value should not be called when Resolution type is Captured")
}
}
}
}
#[derive(Debug, Default, Clone)]
pub struct Point {
pub x: f64,
pub y: f64,
}
#[derive(Debug, Default, Clone)]
pub struct Size {
pub width: f64,
pub height: f64,
}
#[derive(Debug, Default, Clone)]
pub struct Area {
pub origin: Point,
pub size: Size,
}
/// Options passed to the screen capturer
#[derive(Debug, Default, Clone)]
pub struct Options {
pub fps: u32,
pub show_cursor: bool,
pub show_highlight: bool,
pub target: Option<Target>,
pub crop_area: Option<Area>,
pub output_type: FrameType,
pub output_resolution: Resolution,
// excluded targets will only work on macOS
pub excluded_targets: Option<Vec<Target>>,
/// Only implemented for Windows and macOS currently
pub captures_audio: bool,
pub exclude_current_process_audio: bool,
}
/// Screen capturer class
pub struct Capturer {
engine: engine::Engine,
rx: mpsc::Receiver<ChannelItem>,
}
#[derive(Debug)]
pub enum CapturerBuildError {
NotSupported,
PermissionNotGranted,
}
impl std::fmt::Display for CapturerBuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CapturerBuildError::NotSupported => write!(f, "Screen capturing is not supported"),
CapturerBuildError::PermissionNotGranted => {
write!(f, "Permission to capture the screen is not granted")
}
}
}
}
impl Error for CapturerBuildError {}
impl Capturer {
/// Build a new [Capturer] instance with the provided options
pub fn build(options: Options) -> Result<Capturer, CapturerBuildError> {
if !is_supported() {
return Err(CapturerBuildError::NotSupported);
}
if !has_permission() {
return Err(CapturerBuildError::PermissionNotGranted);
}
let (tx, rx) = mpsc::channel();
let engine = engine::Engine::new(&options, tx);
Ok(Capturer { engine, rx })
}
// TODO
// Prevent starting capture if already started
/// Start capturing the frames
pub fn start_capture(&mut self) {
self.engine.start();
}
/// Stop the capturer
pub fn stop_capture(&mut self) {
self.engine.stop();
}
/// Get the next captured frame
pub fn get_next_frame(&self) -> Result<Frame, mpsc::RecvError> {
loop {
let res = self.rx.recv()?;
if let Some(frame) = self.engine.process_channel_item(res) {
return Ok(frame);
}
}
}
/// Attempts to return the next captured frame without blocking.
///
/// Processes all currently available channel items until a usable frame is found.
/// Returns `Ok(None)` if the channel is empty; filtered items are processed transparently.
/// Returns `Err(mpsc::RecvError)` if the capture channel has been disconnected.
pub fn try_get_next_frame(&self) -> Result<Option<Frame>, mpsc::RecvError> {
loop {
match self.rx.try_recv() {
Ok(res) => {
if let Some(frame) = self.engine.process_channel_item(res) {
return Ok(Some(frame));
}
// Item filtered, try next without blocking
}
Err(mpsc::TryRecvError::Empty) => return Ok(None),
Err(mpsc::TryRecvError::Disconnected) => return Err(mpsc::RecvError),
}
}
}
/// Get the dimensions the frames will be captured in
pub fn get_output_frame_size(&mut self) -> [u32; 2] {
self.engine.get_output_frame_size()
}
pub fn raw(&self) -> RawCapturer {
RawCapturer { capturer: self }
}
}
pub struct RawCapturer<'a> {
capturer: &'a Capturer,
}