-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcapturer.rs
199 lines (166 loc) · 4.74 KB
/
capturer.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::time::Instant;
use super::{engine::EngineSender, peer::channel::StartScreenOpts};
use crate::rtc::{peer::channel::PeerChannelCommand, peer2::BweCfg};
use cap::capturer::{CapturerOutput, ScreenCapturer};
use flume::{Receiver, Sender};
use str0m::{
bwe::Bitrate,
media::{MediaKind, MediaTime},
};
use tauri::AppHandle;
pub enum RunLoopEvent {
Start(StartScreenOpts),
Stop,
SetBitrate(Bitrate),
RequestKeyFrame,
Destroy,
}
pub struct CaptureController {
run_loop_sender: Sender<RunLoopEvent>,
}
impl CaptureController {
pub fn new(_engine_sender: EngineSender, run_loop_sender: Sender<RunLoopEvent>) -> Self {
Self { run_loop_sender }
}
pub async fn stop(&self) {
let _ = self.run_loop_sender.try_send(RunLoopEvent::Stop);
}
pub async fn start(&self, opts: StartScreenOpts) {
let _ = self.run_loop_sender.try_send(RunLoopEvent::Start(opts));
}
pub async fn set_bitrate(&self, bitrate: Bitrate) {
let _ = self
.run_loop_sender
.try_send(RunLoopEvent::SetBitrate(bitrate));
}
pub async fn request_key_frame(&self) {
let _ = self.run_loop_sender.try_send(RunLoopEvent::RequestKeyFrame);
}
}
impl Drop for CaptureController {
fn drop(&mut self) {
let _ = self.run_loop_sender.try_send(RunLoopEvent::Destroy);
}
}
pub struct CapturerRunLoop {
reveiver: Receiver<RunLoopEvent>,
close_receiver: Receiver<()>,
close_sender: Sender<()>,
engine_sender: EngineSender,
frame_sender: Sender<CapturerOutput>,
frame_receiver: Receiver<CapturerOutput>,
capturer: Option<ScreenCapturer>,
running: bool,
start: Instant,
}
impl CapturerRunLoop {
pub fn new(
run_loop_reveiver: Receiver<RunLoopEvent>,
_run_loop_sender: Sender<RunLoopEvent>,
engine_sender: EngineSender,
_app_handle: AppHandle,
) -> Self {
let (close_sender, close_receiver) = flume::bounded::<()>(2);
// This receives frames from our Capturer
let (frame_sender, frame_receiver) = flume::bounded::<CapturerOutput>(100);
Self {
engine_sender,
reveiver: run_loop_reveiver,
close_receiver,
close_sender,
capturer: None,
running: false,
start: Instant::now(),
frame_sender,
frame_receiver,
}
}
pub async fn run(&mut self) {
loop {
tokio::select! {
Ok(event) = self.reveiver.recv_async() => {
match event {
RunLoopEvent::Destroy => {
let _ = self.close_sender.try_send(());
break;
}
_ => {}
}
info!("handling capturer event");
self.handle_command(event).await;
}
// When an encoded frame arrives
Ok(frame) = self.frame_receiver.recv_async() => {
self.tick(frame).await;
}
_ = self.close_receiver.recv_async() => {
break;
}
}
}
info!("RTC screen capturer run loop ended");
}
async fn handle_command(&mut self, event: RunLoopEvent) {
match event {
RunLoopEvent::Start(opts) => {
self.start_capture(opts).await;
}
RunLoopEvent::SetBitrate(bitrate) => {
self
.capturer
.as_mut()
.map(|cap| cap.set_bitrate(bitrate.as_u64() as i32));
}
RunLoopEvent::Stop => {
self.stop_capture().await;
}
RunLoopEvent::RequestKeyFrame => {
self.capturer.as_mut().map(|cap| cap.request_keyframe());
}
_ => {}
}
}
/// Send a frame to engine
async fn tick(&mut self, frame: CapturerOutput) {
if !self.running {
return;
}
let rtp_time = MediaTime::from_seconds(frame.seconds);
let _ = self.engine_sender.try_send(PeerChannelCommand::SendMedia {
kind: MediaKind::Video,
data: frame.data,
rtp_time,
extra: None,
});
}
async fn start_capture(&mut self, opts: StartScreenOpts) {
info!("starting capture");
let mut capturer = ScreenCapturer::new(self.frame_sender.clone());
// let mut config = capturer.config();
if let Some(id) = opts.display_id {
capturer.set_display(id);
}
let Ok(desired_bitrate) = capturer.start().await else {
return;
};
// Send the desired bitrate to the engine to set on BWE for peers
self.send_to_engine(PeerChannelCommand::ConfigBwe(BweCfg::desired(
Bitrate::bps(desired_bitrate as u64),
)));
self.capturer = Some(capturer);
self.start = Instant::now();
self.running = true;
}
async fn stop_capture(&mut self) {
info!("stopping capture");
self.running = false;
// stop capturing
if let Some(mut cap) = self.capturer.take() {
cap.stop().await;
}
}
#[allow(dead_code)]
fn send_to_engine(&mut self, command: PeerChannelCommand) {
let _ = self.engine_sender.try_send(command);
}
}