mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-09-18 10:21:03 +03:00
* first improve Signed-off-by: 21pages <sunboeasy@gmail.com> * fix(qos): avoid reducing FPS for transient network jitter Require consecutive bad samples to confirm congestion before normal FPS reductions. Prefer bitrate reduction when ABR is available and allow an outstanding probe to complete before evaluating its effect. Recover FPS faster on fresh good samples while preserving severe-delay and timeout protection. Add regression coverage for jitter, bandwidth changes, and multiple viewers. Signed-off-by: 21pages <sunboeasy@gmail.com> * test(qos): virtual clock and a closed-loop link simulation Tests drove time by moving `Instant`s into the past, which panics on a host that booted less than two minutes ago, and the ABR smoke had to detect ratio changes to keep its fake clock consistent. `VideoQoS` now reads the clock through `now()`; tests set a virtual instant and advance it. `tests/sim.rs` drives the controller the way `Connection` does, over a link with variable frame sizes, wobbling capacity, heavy-tailed jitter, retransmission stalls and link stalls, with both a bitrate-targeted (VP8/VP9/AV1) and a fixed-rate (hardware) encoder model. It prints one table row per scenario; the assertions arrive with the controller changes. The short-stall smoke sweeps the stall phase instead of three fixed values. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): keep jittery but healthy links fast, drain congestion with bitrate A weak home Wi-Fi with plenty of capacity but frequent jitter and the odd stall ended up at about 10 fps: single bad replies, the two second probe timeout and a slow climb back each took their share. The controller now treats a transient stall and a saturated link differently. - The probe timeout no longer slams the frame rate to 2. Every second the probe stays out beyond the first halves it instead, and the late reply that finally closes the probe does not reduce again. - After a reduction, good replies return halfway, then fully, to the level held before it. A restored level that congests within five replies becomes a lower ceiling, so a real capacity drop converges instead of oscillating. - VP8, VP9 and AV1 run CBR against timestamps: fewer frames only means bigger frames. While the bitrate can still be reduced the frame rate keeps its floor, and three bad replies in a row confirm congestion and halve the bitrate instead of stepping it down by a fifth every three seconds. - `bitrate_first` now means the bitrate can actually still drop; at the floor, congestion during the adjustment cooldown reduces the frame rate (Greptile). - `avg_delay()` subtracts the baseline with `saturating_sub`; at exactly the baseline it returned the whole delay, which kept the bitrate of a stable high-RTT link from ever recovering. - `HISTORY_DELAY_LEN` kept three samples, not two. - Every ratio adjustment resets the dynamic screen counters, so a long congestion episode cannot make a static screen look dynamic (CodeRabbit). - One `qos_trace` debug line per probe reply and per timeout, for replay through `tests/sim.rs`. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): adapt each viewer from its own target, not the shared minimum `user_network_delay` started every viewer's adaptation from `self.fps`, the minimum over all viewers. One congested viewer therefore pulled the others' targets down with it, and when it recovered the stream stayed low until the others had climbed back on their own. The per-viewer memory added for restores made the coupling worse: a viewer recorded another viewer's low rate as its own pre-congestion level (Greptile). Each viewer now adapts from its own `delay.fps`, falling back to the shared value only for its first reply; `adjust_fps` keeps aggregating the minimum. The replay test now fails on a malformed trace value instead of dropping it (CodeRabbit). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * test(qos): paired network trace, bitrate-conserving encoder, twenty seeds The simulator drew every random quantity from one stream, so two controllers with the same seed saw different links as soon as they produced a different number of frames; the A/B was not paired. The link trace (capacity wobble, stalls, loss events) is now generated before the run from a network stream of its own, and encoder noise and probe jitter have separate streams. The CBR model let a scene change add three frames' worth of data every five seconds without clawing it back, which raised the offered load of any controller that lowered the frame rate (up to +27% at 2 fps). The encoder now repays the surplus over the following frames and the size spread has mean one, so the long-term load no longer depends on the frame rate. Every scenario runs over twenty seeds and the assertions bound the distribution: median of the mean target, worst p10, p90 of the time below half the limit and of the queue p95. The bounds state what the product needs, not what one seed produced. New columns: produced and delivered frames per second, delivered frame age, sustained recovery (target at the limit and queue under 200 ms held for five seconds), cold-start minimum and time to 90% of the limit. The replay advances by recorded `t=` deltas when present and is labelled as the open-loop, FPS-only diagnostic it is. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): baseline from the first reply, bitrate cuts on confirmation only Two findings from the design re-check. The baseline needed ten replies before it was used, so a stable 180 or 300 ms link spent its first ten seconds read as congested: the frame rate fell to 5 and the bitrate was cut before either recovered. The running minimum is the baseline from the first reply on; the smoothed estimate takes over once the window is full. A single reply a second above the baseline, or a single timer tick with the probe out for two seconds, cut the bitrate by a fifth. A static screen never earns an increase back, so repeated stalls ratcheted its ratio down and the next dynamic episode started from there. Bitrate cuts now need two bad replies in a row, or a probe still outstanding at the second tick past two seconds; the frame rate brake and the severe-reply rule are unchanged. An elevated but unconfirmed delay no longer restarts the ratio cooldown, so a confirmation on the next reply is acted on at once. `qos_trace` lines carry a millisecond timestamp for the replay test. The simulator asserts the intercontinental cold start: minimum target at INIT_FPS and 90% of the limit within ten seconds. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * test(qos): held-out seeds and parameter sensitivity as guards against tuning The scenario bounds now live in one function shared by the CI run over seeds 1 to 20 and by `robustness.rs`, whose two ignored tests apply the same bounds to seeds 21 to 120 in blocks of twenty and halve or double each scenario parameter in turn. Anyone changing a controller constant or a bound runs them; a bound that fails on unseen seeds was fitted to the CI seeds. At this head every held-out block passes, with medians within a few percent of the CI seeds, while master fails five of five blocks in every home Wi-Fi row. The sweep keeps the lead over master in all fourteen variants for the frame-rate metrics and shows two limits worth knowing: at 6 Mbps of capacity both controllers hold about 2.4 s of queue p95, and at a drop to 1.5 Mbps both are poor because the 1 Mbps bitrate floor leaves little to drain with. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * test(qos): frame age bounds, wall-clock scene changes, time-indexed probe jitter The queue metric divides the queued bytes by the nominal capacity, so during a link stall it reports how long the backlog takes to drain afterwards, not how long the frame at the head has already waited. Frame age, the time a delivered frame spent in the shared path, was computed but not bounded. It is now bounded per scenario, as a regression bound set from the scenario rather than from a run: 1.5 s on the home Wi-Fi rows (isolated stalls of up to 2.5 s are tolerated, a sustained multi-second backlog is not), 100 ms on a clean link, 150 ms on a stable high-RTT link (frame age excludes the round trip, so RTT earns no allowance), and the same bound as the queue on the bandwidth-drop and mobile rows. Two residual couplings between controller decisions and the exogenous inputs are removed: scene changes follow the wall clock instead of the frame count, and probe jitter is a per-second table drawn before the run, so two controllers with the same seed meet the same content timeline and the same jitter. The moderate-congestion smoke profile now asserts that the frame rate actually drops, and the held-out test builds its combined summary from the block reports instead of simulating every seed twice. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * test(qos): remove the placeholder viewer from trace replay Clear the synthetic viewer created by smoke::session so it cannot cap replayed FPS at 15 when the recorded connection ID differs from 1. Add a regression test verifying identical FPS sequences for connection IDs 1 and 1652, both recovering to 30 FPS. Signed-off-by: 21pages <sunboeasy@gmail.com> * fix(qos): cap each viewer by its own limit, judge bitrate steps per viewer Two places still let one viewer's state leak into another's. The per-viewer target was clamped by `highest_fps()`, the minimum of every viewer's limit, before being stored, and a new viewer started from the shared stream rate. A viewer that lowered its limit dragged the others' targets down with it, and when it left the stream stayed there until the others had climbed back; a viewer joining a congested session started at the congested rate. Each viewer now starts at INIT_FPS, is capped by its own limit only, and `adjust_fps` keeps applying the shared limit to the aggregate. `adjust_ratio` paired the maximum delay over viewers with any viewer's confirmation, so one viewer's unconfirmed 1200 ms spike and another viewer's two 200 ms replies produced a 20% cut, and a third mild reply a halving, when each viewer on its own called for five percent. Each viewer's own delay and confirmation now decide the step it calls for, and the stream takes the most conservative one; increases still need every viewer below the threshold. Single-viewer behaviour is unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * test(qos): replay advances by the wall clock across connections The replay kept a last timestamp per connection id, so a log with several viewers each writing once a second advanced the virtual clock once per line and replayed several times slower than it was recorded. It now advances by the delta between consecutive lines whatever their connection. The replay is a plain function over the log text, with the environment-driven test as its entry point, so the time axis can be tested directly. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): first reply keeps the ratio cooldown, closing a viewer re-aggregates A viewer's first TestDelay reply called `adjust_ratio` with no cooldown check. With the per-viewer steps, that scan finds the other viewers' still confirmed evidence, so a viewer joining right after a cut halved the bitrate a second time inside the three seconds the cut is meant to be observed for. The first reply is now one more trigger of the periodic adjustment and keeps its cooldown; a fresh session still adjusts on its first reply, since its controller was created long before. Linux was never on this path. `on_connection_close` removed the viewer without re-aggregating, so the stream stayed at the departed viewer's rate until the next tick; the remaining viewers are aggregated at once. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): the newcomer guard belongs to the viewer that joined Re-aggregating on close applied the one-second INIT_FPS guard that the departing viewer had set when it joined, so a viewer that connected and dropped within a second throttled the others to 15 fps for the rest of that second. The guard now records which viewer set it and is cleared when that viewer leaves; a genuinely new viewer is still capped for its first second. The first-reply ratio adjustment's platform switch is a field instead of a `cfg!` inside the condition, so the cooldown regression test exercises the path on Linux CI as well. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): every newcomer carries its own start-up guard The start-up guard had one slot, so a second viewer joining within a second overwrote the first one's join time, and when the second viewer left the first one's window was released early. The join time now lives in the viewer's own entry, `adjust_fps` caps the stream while any viewer is inside its first second, and a departed viewer takes its guard with its entry; no clearing logic is needed (Greptile). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): adapt delay baselines and speed up FPS recovery - Relearn stable baseline increases from recent fresh replies. - Require fresh congestion confirmation between ordinary FPS reductions. - Keep automatic FPS reductions above a 5 FPS floor, respecting lower caps. - Restore FPS after two good replies, with rollback on renewed congestion. - Add regression tests for baseline changes, jitter, and FPS recovery. Signed-off-by: 21pages <sunboeasy@gmail.com> * test(qos): align FPS floor tests with the 5 FPS minimum - Remove the unregistered sustained tests with outdated expectations. - Test severe delays and timeouts independently, including lower user caps and recovery. - Move the 700 kbps scenario into active adaptation tests, checking the FPS floor and recovery after bandwidth returns. Signed-off-by: 21pages <sunboeasy@gmail.com> * fix(qos): a timeout never lifts a target, and the invariants as property tests The timeout brake floored its output at MIN_FPS + 1, so a viewer whose target had already reached 1 fps was lifted to 2 by the next tick past two seconds: bad evidence raising the frame rate. Inherited from master, where the timeout set the whole stream to MIN_FPS + 1 outright. The brake now never exceeds the target it found, whatever the elapsed time it is told. The controller's six invariants become property tests over random sessions (150 seeds, 300 steps, one to three viewers, ABR on and off): a viewer's target is independent of other viewers; bad evidence never raises a target or the ratio; joins and leaves only change the aggregation; a bitrate cut is owned by a viewer's own evidence and not spent again by a newcomer; targets stay within their caps and the stream is their aggregation; a braked probe's late reply does not brake again. The timeout case is also pinned exhaustively over every target and a range of elapsed times. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * feat(qos): log the encode and send pipeline behind RUSTDESK_QOS_VERBOSE The controller's target frame rate is neither the rate the encoder produced nor the rate the send path accepted, and two facts kept that gap invisible. libvpx drops frames on its own when the bitrate cannot carry them, so a capture round is not a delivered frame. The video send is inline in the connection's message loop, so a slow write stalls capture and the delay probe alike, and the recorded delay cannot tell the two apart. `qos_video` reports, per second and per display, the capture rounds, the frames that actually reached a connection, and the longest wait for the previous frame to be picked up. `qos_send` reports, per second and per connection, how long `stream.send().await` blocked and how deep the video queue is. Both carry `t=`, so they join with `qos_trace` offline; `replay` filters on `qos_trace` and ignores them. The default log level is `debug`, so an unconditional line would land in every user's log file once a second forever. Neither line is emitted unless `RUSTDESK_QOS_VERBOSE` is set, nothing enables it implicitly, and without it the timing calls are skipped as well. TestDelay, the controller and every threshold are untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PARvswNPeZ88LVT7Ew5hkp * fix(qos): speed up FPS ramp-up on clean connections Double startup FPS after every two fresh low-excess-delay replies, up to the viewer's cap. End acceleration on queue growth, timeout, or FPS reduction. Add regression tests for startup speed, viewer caps, congestion, timeouts, and multiple viewers. Signed-off-by: 21pages <sunboeasy@gmail.com> --------- Signed-off-by: 21pages <sunboeasy@gmail.com> Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1467 lines
49 KiB
Rust
1467 lines
49 KiB
Rust
// 24FPS (actually 23.976FPS) is what video professionals ages ago determined to be the
|
|
// slowest playback rate that still looks smooth enough to feel real.
|
|
// Our eyes can see a slight difference and even though 30FPS actually shows
|
|
// more information and is more realistic.
|
|
// 60FPS is commonly used in game, teamviewer 12 support this for video editing user.
|
|
|
|
// how to capture with mouse cursor:
|
|
// https://docs.microsoft.com/zh-cn/windows/win32/direct3ddxgi/desktop-dup-api?redirectedfrom=MSDN
|
|
|
|
// RECORD: The following Project has implemented audio capture, hardware codec and mouse cursor drawn.
|
|
// https://github.com/PHZ76/DesktopSharing
|
|
|
|
// dxgi memory leak issue
|
|
// https://stackoverflow.com/questions/47801238/memory-leak-in-creating-direct2d-device
|
|
// but per my test, it is more related to AcquireNextFrame,
|
|
// https://forums.developer.nvidia.com/t/dxgi-outputduplication-memory-leak-when-using-nv-but-not-amd-drivers/108582
|
|
|
|
// to-do:
|
|
// https://slhck.info/video/2017/03/01/rate-control.html
|
|
|
|
use super::{display_service::check_display_changed, service::ServiceTmpl, video_qos::VideoQoS, *};
|
|
#[cfg(target_os = "linux")]
|
|
use crate::common::SimpleCallOnReturn;
|
|
#[cfg(target_os = "linux")]
|
|
use crate::platform::linux::is_x11;
|
|
use crate::privacy_mode::{get_privacy_mode_conn_id, INVALID_PRIVACY_MODE_CONN_ID};
|
|
#[cfg(windows)]
|
|
use crate::{
|
|
platform::windows::is_process_consent_running,
|
|
privacy_mode::{is_current_privacy_mode_impl, PRIVACY_MODE_IMPL_WIN_MAG},
|
|
ui_interface::is_installed,
|
|
};
|
|
use hbb_common::{
|
|
anyhow::anyhow,
|
|
config,
|
|
tokio::sync::{
|
|
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
|
|
Mutex as TokioMutex,
|
|
},
|
|
};
|
|
#[cfg(feature = "hwcodec")]
|
|
use scrap::hwcodec::{HwRamEncoder, HwRamEncoderConfig};
|
|
#[cfg(feature = "vram")]
|
|
use scrap::vram::{VRamEncoder, VRamEncoderConfig};
|
|
#[cfg(not(windows))]
|
|
use scrap::Capturer;
|
|
use scrap::{
|
|
aom::AomEncoderConfig,
|
|
codec::{Encoder, EncoderCfg},
|
|
record::{Recorder, RecorderContext},
|
|
vpxcodec::{VpxEncoderConfig, VpxVideoCodecId},
|
|
CodecFormat, Display, EncodeInput, TraitCapturer, TraitPixelBuffer,
|
|
};
|
|
#[cfg(windows)]
|
|
use std::sync::Once;
|
|
use std::{
|
|
collections::HashSet,
|
|
io::ErrorKind::WouldBlock,
|
|
ops::{Deref, DerefMut},
|
|
time::{self, Duration, Instant},
|
|
};
|
|
|
|
pub const OPTION_REFRESH: &'static str = "refresh";
|
|
|
|
type FrameFetchedNotifierSender = UnboundedSender<(i32, Option<Instant>)>;
|
|
type FrameFetchedNotifierReceiver = Arc<TokioMutex<UnboundedReceiver<(i32, Option<Instant>)>>>;
|
|
|
|
lazy_static::lazy_static! {
|
|
static ref FRAME_FETCHED_NOTIFIERS: Mutex<HashMap<usize, (FrameFetchedNotifierSender, FrameFetchedNotifierReceiver)>> = Mutex::new(HashMap::default());
|
|
|
|
// display_idx -> set of conn id.
|
|
// Used to record which connections need to be notified when
|
|
// 1. A new frame is received from a web client.
|
|
// Because web client does not send the display index in message `VideoReceived`.
|
|
// 2. The client is closing.
|
|
static ref DISPLAY_CONN_IDS: Arc<Mutex<HashMap<usize, HashSet<i32>>>> = Default::default();
|
|
pub static ref VIDEO_QOS: Arc<Mutex<VideoQoS>> = Default::default();
|
|
pub static ref IS_UAC_RUNNING: Arc<Mutex<bool>> = Default::default();
|
|
pub static ref IS_FOREGROUND_WINDOW_ELEVATED: Arc<Mutex<bool>> = Default::default();
|
|
static ref SCREENSHOTS: Mutex<HashMap<(VideoSource, usize), Screenshot>> = Default::default();
|
|
}
|
|
|
|
struct Screenshot {
|
|
sid: String,
|
|
tx: Sender,
|
|
restore_vram: bool,
|
|
}
|
|
|
|
#[inline]
|
|
pub fn notify_video_frame_fetched(display_idx: usize, conn_id: i32, frame_tm: Option<Instant>) {
|
|
if let Some(notifier) = FRAME_FETCHED_NOTIFIERS.lock().unwrap().get(&display_idx) {
|
|
notifier.0.send((conn_id, frame_tm)).ok();
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn notify_video_frame_fetched_by_conn_id(conn_id: i32, frame_tm: Option<Instant>) {
|
|
let vec_display_idx: Vec<usize> = {
|
|
let display_conn_ids = DISPLAY_CONN_IDS.lock().unwrap();
|
|
display_conn_ids
|
|
.iter()
|
|
.filter_map(|(display_idx, conn_ids)| {
|
|
if conn_ids.contains(&conn_id) {
|
|
Some(*display_idx)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect()
|
|
};
|
|
let notifiers = FRAME_FETCHED_NOTIFIERS.lock().unwrap();
|
|
for display_idx in vec_display_idx {
|
|
if let Some(notifier) = notifiers.get(&display_idx) {
|
|
notifier.0.send((conn_id, frame_tm)).ok();
|
|
}
|
|
}
|
|
}
|
|
|
|
struct VideoFrameController {
|
|
display_idx: usize,
|
|
cur: Instant,
|
|
send_conn_ids: HashSet<i32>,
|
|
}
|
|
|
|
impl VideoFrameController {
|
|
fn new(display_idx: usize) -> Self {
|
|
Self {
|
|
display_idx,
|
|
cur: Instant::now(),
|
|
send_conn_ids: HashSet::new(),
|
|
}
|
|
}
|
|
|
|
fn reset(&mut self) {
|
|
self.send_conn_ids.clear();
|
|
}
|
|
|
|
fn set_send(&mut self, tm: Instant, conn_ids: HashSet<i32>) {
|
|
if !conn_ids.is_empty() {
|
|
self.cur = tm;
|
|
self.send_conn_ids = conn_ids;
|
|
DISPLAY_CONN_IDS
|
|
.lock()
|
|
.unwrap()
|
|
.insert(self.display_idx, self.send_conn_ids.clone());
|
|
}
|
|
}
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
async fn try_wait_next(&mut self, fetched_conn_ids: &mut HashSet<i32>, timeout_millis: u64) {
|
|
if self.send_conn_ids.is_empty() {
|
|
return;
|
|
}
|
|
|
|
let timeout_dur = Duration::from_millis(timeout_millis as u64);
|
|
let receiver = {
|
|
match FRAME_FETCHED_NOTIFIERS
|
|
.lock()
|
|
.unwrap()
|
|
.get(&self.display_idx)
|
|
{
|
|
Some(notifier) => notifier.1.clone(),
|
|
None => {
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
let mut receiver_guard = receiver.lock().await;
|
|
match tokio::time::timeout(timeout_dur, receiver_guard.recv()).await {
|
|
Err(_) => {
|
|
// break if timeout
|
|
// log::error!("blocking wait frame receiving timeout {}", timeout_millis);
|
|
}
|
|
Ok(Some((id, instant))) => {
|
|
if let Some(tm) = instant {
|
|
log::trace!("Channel recv latency: {}", tm.elapsed().as_secs_f32());
|
|
}
|
|
fetched_conn_ids.insert(id);
|
|
}
|
|
Ok(None) => {
|
|
// this branch would never be reached
|
|
}
|
|
}
|
|
while !receiver_guard.is_empty() {
|
|
if let Some((id, instant)) = receiver_guard.recv().await {
|
|
if let Some(tm) = instant {
|
|
log::trace!("Channel recv latency: {}", tm.elapsed().as_secs_f32());
|
|
}
|
|
fetched_conn_ids.insert(id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
|
|
pub enum VideoSource {
|
|
Monitor,
|
|
Camera,
|
|
}
|
|
|
|
impl VideoSource {
|
|
pub fn service_name_prefix(&self) -> &'static str {
|
|
match self {
|
|
VideoSource::Monitor => "monitor",
|
|
VideoSource::Camera => "camera",
|
|
}
|
|
}
|
|
|
|
pub fn is_monitor(&self) -> bool {
|
|
matches!(self, VideoSource::Monitor)
|
|
}
|
|
|
|
pub fn is_camera(&self) -> bool {
|
|
matches!(self, VideoSource::Camera)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct VideoService {
|
|
sp: GenericService,
|
|
idx: usize,
|
|
source: VideoSource,
|
|
}
|
|
|
|
impl Deref for VideoService {
|
|
type Target = ServiceTmpl<ConnInner>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.sp
|
|
}
|
|
}
|
|
|
|
impl DerefMut for VideoService {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.sp
|
|
}
|
|
}
|
|
|
|
pub fn get_service_name(source: VideoSource, idx: usize) -> String {
|
|
format!("{}{}", source.service_name_prefix(), idx)
|
|
}
|
|
|
|
pub fn new(source: VideoSource, idx: usize) -> GenericService {
|
|
let _ = FRAME_FETCHED_NOTIFIERS
|
|
.lock()
|
|
.unwrap()
|
|
.entry(idx)
|
|
.or_insert_with(|| {
|
|
let (tx, rx) = unbounded_channel();
|
|
(tx, Arc::new(TokioMutex::new(rx)))
|
|
});
|
|
let vs = VideoService {
|
|
sp: GenericService::new(get_service_name(source, idx), true),
|
|
idx,
|
|
source,
|
|
};
|
|
GenericService::run(&vs, run);
|
|
vs.sp
|
|
}
|
|
|
|
// Capturer object is expensive, avoiding to create it frequently.
|
|
fn create_capturer(
|
|
privacy_mode_id: i32,
|
|
display: Display,
|
|
_current: usize,
|
|
_portable_service_running: bool,
|
|
) -> ResultType<Box<dyn TraitCapturer>> {
|
|
#[cfg(not(windows))]
|
|
let c: Option<Box<dyn TraitCapturer>> = None;
|
|
#[cfg(windows)]
|
|
let mut c: Option<Box<dyn TraitCapturer>> = None;
|
|
if privacy_mode_id > 0 {
|
|
#[cfg(windows)]
|
|
{
|
|
// Windows Mode 1 can cover every local monitor with overlay windows,
|
|
// but the legacy magnifier capture backend is still single-monitor
|
|
// constrained. Keep display-switch gating aligned with that backend
|
|
// limit, not just the overlay coverage.
|
|
if let Some(c1) = crate::privacy_mode::win_mag::create_capturer(
|
|
privacy_mode_id,
|
|
display.origin(),
|
|
display.width(),
|
|
display.height(),
|
|
)? {
|
|
c = Some(Box::new(c1));
|
|
}
|
|
}
|
|
}
|
|
|
|
match c {
|
|
Some(c1) => return Ok(c1),
|
|
None => {
|
|
#[cfg(windows)]
|
|
{
|
|
log::debug!("Create capturer dxgi|gdi");
|
|
return crate::portable_service::client::create_capturer(
|
|
_current,
|
|
display,
|
|
_portable_service_running,
|
|
);
|
|
}
|
|
#[cfg(not(windows))]
|
|
{
|
|
log::debug!("Create capturer from scrap");
|
|
return Ok(Box::new(
|
|
Capturer::new(display).with_context(|| "Failed to create capturer")?,
|
|
));
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
// This function works on privacy mode. Windows only for now.
|
|
pub fn test_create_capturer(
|
|
privacy_mode_id: i32,
|
|
display_idx: usize,
|
|
timeout_millis: u64,
|
|
) -> String {
|
|
let test_begin = Instant::now();
|
|
loop {
|
|
let err = match Display::all() {
|
|
Ok(mut displays) => {
|
|
if displays.len() <= display_idx {
|
|
anyhow!(
|
|
"Failed to get display {}, the displays' count is {}",
|
|
display_idx,
|
|
displays.len()
|
|
)
|
|
} else {
|
|
let display = displays.remove(display_idx);
|
|
match create_capturer(privacy_mode_id, display, display_idx, false) {
|
|
Ok(_) => return "".to_owned(),
|
|
Err(e) => e,
|
|
}
|
|
}
|
|
}
|
|
Err(e) => e.into(),
|
|
};
|
|
if test_begin.elapsed().as_millis() >= timeout_millis as _ {
|
|
return err.to_string();
|
|
}
|
|
std::thread::sleep(Duration::from_millis(300));
|
|
}
|
|
}
|
|
|
|
// Note: This function is extremely expensive, do not call it frequently.
|
|
#[cfg(windows)]
|
|
fn check_uac_switch(privacy_mode_id: i32, capturer_privacy_mode_id: i32) -> ResultType<()> {
|
|
if capturer_privacy_mode_id != INVALID_PRIVACY_MODE_CONN_ID
|
|
&& is_current_privacy_mode_impl(PRIVACY_MODE_IMPL_WIN_MAG)
|
|
{
|
|
if !is_installed() {
|
|
if privacy_mode_id != capturer_privacy_mode_id {
|
|
if !is_process_consent_running()? {
|
|
bail!("consent.exe is not running");
|
|
}
|
|
}
|
|
if is_process_consent_running()? {
|
|
bail!("consent.exe is running");
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) struct CapturerInfo {
|
|
pub origin: (i32, i32),
|
|
pub width: usize,
|
|
pub height: usize,
|
|
pub ndisplay: usize,
|
|
pub current: usize,
|
|
pub privacy_mode_id: i32,
|
|
pub _capturer_privacy_mode_id: i32,
|
|
pub capturer: Box<dyn TraitCapturer>,
|
|
}
|
|
|
|
impl Deref for CapturerInfo {
|
|
type Target = Box<dyn TraitCapturer>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.capturer
|
|
}
|
|
}
|
|
|
|
impl DerefMut for CapturerInfo {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.capturer
|
|
}
|
|
}
|
|
|
|
fn get_capturer_monitor(
|
|
current: usize,
|
|
portable_service_running: bool,
|
|
) -> ResultType<CapturerInfo> {
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
if !is_x11() {
|
|
return super::wayland::get_capturer_for_display(current);
|
|
}
|
|
}
|
|
|
|
let mut displays = Display::all()?;
|
|
let ndisplay = displays.len();
|
|
if ndisplay <= current {
|
|
bail!(
|
|
"Failed to get display {}, displays len: {}",
|
|
current,
|
|
ndisplay
|
|
);
|
|
}
|
|
|
|
let display = displays.remove(current);
|
|
|
|
#[cfg(target_os = "linux")]
|
|
if let Display::X11(inner) = &display {
|
|
if let Err(err) = inner.get_shm_status() {
|
|
log::warn!(
|
|
"MIT-SHM extension not working properly on select X11 server: {:?}",
|
|
err
|
|
);
|
|
}
|
|
}
|
|
|
|
let (origin, width, height) = (display.origin(), display.width(), display.height());
|
|
let name = display.name();
|
|
log::debug!(
|
|
"#displays={}, current={}, origin: {:?}, width={}, height={}, cpus={}/{}, name:{}",
|
|
ndisplay,
|
|
current,
|
|
&origin,
|
|
width,
|
|
height,
|
|
num_cpus::get_physical(),
|
|
num_cpus::get(),
|
|
&name,
|
|
);
|
|
|
|
let privacy_mode_id = get_privacy_mode_conn_id().unwrap_or(INVALID_PRIVACY_MODE_CONN_ID);
|
|
#[cfg(not(windows))]
|
|
let capturer_privacy_mode_id = privacy_mode_id;
|
|
#[cfg(windows)]
|
|
let mut capturer_privacy_mode_id = privacy_mode_id;
|
|
#[cfg(windows)]
|
|
{
|
|
if capturer_privacy_mode_id != INVALID_PRIVACY_MODE_CONN_ID
|
|
&& is_current_privacy_mode_impl(PRIVACY_MODE_IMPL_WIN_MAG)
|
|
{
|
|
if !is_installed() {
|
|
if is_process_consent_running()? {
|
|
capturer_privacy_mode_id = INVALID_PRIVACY_MODE_CONN_ID;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
log::debug!(
|
|
"Try create capturer with capturer privacy mode id {}",
|
|
capturer_privacy_mode_id,
|
|
);
|
|
|
|
if privacy_mode_id != INVALID_PRIVACY_MODE_CONN_ID {
|
|
if privacy_mode_id != capturer_privacy_mode_id {
|
|
log::info!("In privacy mode, but show UAC prompt window for now");
|
|
} else {
|
|
log::info!("In privacy mode, the peer side cannot watch the screen");
|
|
}
|
|
}
|
|
let capturer = create_capturer(
|
|
capturer_privacy_mode_id,
|
|
display,
|
|
current,
|
|
portable_service_running,
|
|
)?;
|
|
Ok(CapturerInfo {
|
|
origin,
|
|
width,
|
|
height,
|
|
ndisplay,
|
|
current,
|
|
privacy_mode_id,
|
|
_capturer_privacy_mode_id: capturer_privacy_mode_id,
|
|
capturer,
|
|
})
|
|
}
|
|
|
|
fn get_capturer_camera(current: usize) -> ResultType<CapturerInfo> {
|
|
let cameras = camera::Cameras::get_sync_cameras();
|
|
let ncamera = cameras.len();
|
|
if ncamera <= current {
|
|
bail!("Failed to get camera {}, cameras len: {}", current, ncamera,);
|
|
}
|
|
let Some(camera) = cameras.get(current) else {
|
|
bail!(
|
|
"Camera of index {} doesn't exist or platform not supported",
|
|
current
|
|
);
|
|
};
|
|
let capturer = camera::Cameras::get_capturer(current)?;
|
|
let (width, height) = (camera.width as usize, camera.height as usize);
|
|
let origin = (camera.x as i32, camera.y as i32);
|
|
let name = &camera.name;
|
|
let privacy_mode_id = get_privacy_mode_conn_id().unwrap_or(INVALID_PRIVACY_MODE_CONN_ID);
|
|
let _capturer_privacy_mode_id = privacy_mode_id;
|
|
log::debug!(
|
|
"#cameras={}, current={}, origin: {:?}, width={}, height={}, cpus={}/{}, name:{}",
|
|
ncamera,
|
|
current,
|
|
&origin,
|
|
width,
|
|
height,
|
|
num_cpus::get_physical(),
|
|
num_cpus::get(),
|
|
name,
|
|
);
|
|
return Ok(CapturerInfo {
|
|
origin,
|
|
width,
|
|
height,
|
|
ndisplay: ncamera,
|
|
current,
|
|
privacy_mode_id,
|
|
_capturer_privacy_mode_id: privacy_mode_id,
|
|
capturer,
|
|
});
|
|
}
|
|
fn get_capturer(
|
|
source: VideoSource,
|
|
current: usize,
|
|
portable_service_running: bool,
|
|
) -> ResultType<CapturerInfo> {
|
|
match source {
|
|
VideoSource::Monitor => get_capturer_monitor(current, portable_service_running),
|
|
VideoSource::Camera => get_capturer_camera(current),
|
|
}
|
|
}
|
|
|
|
fn run(vs: VideoService) -> ResultType<()> {
|
|
let mut _raii = Raii::new(vs.idx, vs.sp.name());
|
|
// Wayland only support one video capturer for now. It is ok to call ensure_inited() here.
|
|
//
|
|
// ensure_inited() is needed because clear() may be called.
|
|
// to-do: wayland ensure_inited should pass current display index.
|
|
// But for now, we do not support multi-screen capture on wayland.
|
|
#[cfg(target_os = "linux")]
|
|
super::wayland::ensure_inited()?;
|
|
#[cfg(target_os = "linux")]
|
|
let _wayland_call_on_ret = {
|
|
// Increment active display count when starting
|
|
let _display_count = super::wayland::increment_active_display_count();
|
|
|
|
SimpleCallOnReturn {
|
|
b: true,
|
|
f: Box::new(|| {
|
|
// Decrement active display count and only clear if this was the last display
|
|
let remaining_count = super::wayland::decrement_active_display_count();
|
|
if remaining_count == 0 {
|
|
super::wayland::clear();
|
|
}
|
|
}),
|
|
}
|
|
};
|
|
|
|
#[cfg(windows)]
|
|
let last_portable_service_running = crate::portable_service::client::running();
|
|
#[cfg(not(windows))]
|
|
let last_portable_service_running = false;
|
|
|
|
let display_idx = vs.idx;
|
|
let sp = vs.sp;
|
|
let mut c = get_capturer(vs.source, display_idx, last_portable_service_running)?;
|
|
#[cfg(windows)]
|
|
if !scrap::codec::enable_directx_capture() && !c.is_gdi() {
|
|
log::info!("disable dxgi with option, fall back to gdi");
|
|
c.set_gdi();
|
|
}
|
|
let mut video_qos = VIDEO_QOS.lock().unwrap();
|
|
let mut spf = video_qos.spf();
|
|
let mut quality = video_qos.ratio();
|
|
let record_incoming = config::option2bool(
|
|
"allow-auto-record-incoming",
|
|
&Config::get_option("allow-auto-record-incoming"),
|
|
);
|
|
let client_record = video_qos.record();
|
|
drop(video_qos);
|
|
let (mut encoder, encoder_cfg, codec_format, use_i444, recorder) = match setup_encoder(
|
|
&c,
|
|
sp.name(),
|
|
quality,
|
|
client_record,
|
|
record_incoming,
|
|
last_portable_service_running,
|
|
vs.source,
|
|
display_idx,
|
|
) {
|
|
Ok(result) => result,
|
|
Err(err) => {
|
|
log::error!("Failed to create encoder: {err:?}, fallback to VP9");
|
|
Encoder::set_fallback(&EncoderCfg::VPX(VpxEncoderConfig {
|
|
width: c.width as _,
|
|
height: c.height as _,
|
|
quality,
|
|
codec: VpxVideoCodecId::VP9,
|
|
keyframe_interval: None,
|
|
}));
|
|
setup_encoder(
|
|
&c,
|
|
sp.name(),
|
|
quality,
|
|
client_record,
|
|
record_incoming,
|
|
last_portable_service_running,
|
|
vs.source,
|
|
display_idx,
|
|
)?
|
|
}
|
|
};
|
|
#[cfg(feature = "vram")]
|
|
c.set_output_texture(encoder.input_texture());
|
|
#[cfg(target_os = "android")]
|
|
if vs.source.is_monitor() {
|
|
if let Err(e) = check_change_scale(encoder.is_hardware()) {
|
|
try_broadcast_display_changed(&sp, display_idx, &c, true).ok();
|
|
bail!(e);
|
|
}
|
|
}
|
|
VIDEO_QOS.lock().unwrap().store_bitrate(encoder.bitrate());
|
|
VIDEO_QOS
|
|
.lock()
|
|
.unwrap()
|
|
.set_support_changing_quality(&sp.name(), encoder.support_changing_quality());
|
|
log::info!("initial quality: {quality:?}");
|
|
|
|
if sp.is_option_true(OPTION_REFRESH) {
|
|
sp.set_option_bool(OPTION_REFRESH, false);
|
|
}
|
|
|
|
let mut frame_controller = VideoFrameController::new(display_idx);
|
|
|
|
let start = time::Instant::now();
|
|
let mut last_check_displays = time::Instant::now();
|
|
#[cfg(windows)]
|
|
let mut try_gdi = 1;
|
|
#[cfg(windows)]
|
|
log::info!("gdi: {}", c.is_gdi());
|
|
#[cfg(windows)]
|
|
start_uac_elevation_check();
|
|
|
|
#[cfg(target_os = "linux")]
|
|
let mut would_block_count = 0u32;
|
|
let mut yuv = Vec::new();
|
|
let mut mid_data = Vec::new();
|
|
let mut repeat_encode_counter = 0;
|
|
let repeat_encode_max = 10;
|
|
let mut encode_fail_counter = 0;
|
|
let mut first_frame = true;
|
|
let capture_width = c.width;
|
|
let capture_height = c.height;
|
|
let (mut second_instant, mut send_counter) = (Instant::now(), 0);
|
|
// Diagnostics only. `send_counter` counts capture rounds, which is not the
|
|
// number of frames that reached a connection: the encoder's own rate control
|
|
// drops frames when the bitrate cannot carry them. `wait_max_ms` is how long
|
|
// a round waited for the previous frame to be picked up, so a blocked write
|
|
// shows up here as capture stalling rather than as a slow network.
|
|
let (mut sent_counter, mut wait_max_ms) = (0usize, 0u32);
|
|
|
|
while sp.ok() {
|
|
#[cfg(windows)]
|
|
check_uac_switch(c.privacy_mode_id, c._capturer_privacy_mode_id)?;
|
|
check_qos(
|
|
&mut encoder,
|
|
&mut quality,
|
|
&mut spf,
|
|
client_record,
|
|
&mut send_counter,
|
|
&mut sent_counter,
|
|
&mut wait_max_ms,
|
|
&mut second_instant,
|
|
&sp.name(),
|
|
)?;
|
|
if sp.is_option_true(OPTION_REFRESH) {
|
|
if vs.source.is_monitor() {
|
|
let _ = try_broadcast_display_changed(&sp, display_idx, &c, true);
|
|
}
|
|
log::info!("switch to refresh");
|
|
bail!("SWITCH");
|
|
}
|
|
if codec_format != Encoder::negotiated_codec() {
|
|
log::info!(
|
|
"switch due to codec changed, {:?} -> {:?}",
|
|
codec_format,
|
|
Encoder::negotiated_codec()
|
|
);
|
|
bail!("SWITCH");
|
|
}
|
|
#[cfg(windows)]
|
|
if last_portable_service_running != crate::portable_service::client::running() {
|
|
log::info!("switch due to portable service running changed");
|
|
bail!("SWITCH");
|
|
}
|
|
if Encoder::use_i444(&encoder_cfg) != use_i444 {
|
|
log::info!("switch due to i444 changed");
|
|
bail!("SWITCH");
|
|
}
|
|
#[cfg(all(windows, feature = "vram"))]
|
|
if c.is_gdi() && encoder.input_texture() {
|
|
log::info!("changed to gdi when using vram");
|
|
VRamEncoder::set_fallback_gdi(sp.name(), true);
|
|
bail!("SWITCH");
|
|
}
|
|
if vs.source.is_monitor() {
|
|
check_privacy_mode_changed(&sp, display_idx, &c)?;
|
|
}
|
|
#[cfg(windows)]
|
|
{
|
|
if crate::platform::windows::desktop_changed()
|
|
&& !crate::portable_service::client::running()
|
|
{
|
|
bail!("Desktop changed");
|
|
}
|
|
}
|
|
let now = time::Instant::now();
|
|
if vs.source.is_monitor() && last_check_displays.elapsed().as_millis() > 1000 {
|
|
last_check_displays = now;
|
|
// This check may be redundant, but it is better to be safe.
|
|
// The previous check in `sp.is_option_true(OPTION_REFRESH)` block may be enough.
|
|
try_broadcast_display_changed(&sp, display_idx, &c, false)?;
|
|
}
|
|
|
|
frame_controller.reset();
|
|
|
|
let time = now - start;
|
|
let ms = (time.as_secs() * 1000 + time.subsec_millis() as u64) as i64;
|
|
let res = match c.frame(spf) {
|
|
Ok(frame) => {
|
|
repeat_encode_counter = 0;
|
|
if frame.valid() {
|
|
let screenshot_key = (vs.source, display_idx);
|
|
let screenshot = SCREENSHOTS.lock().unwrap().remove(&screenshot_key);
|
|
if let Some(mut screenshot) = screenshot {
|
|
let restore_vram = screenshot.restore_vram;
|
|
let (msg, w, h, data) = match &frame {
|
|
scrap::Frame::PixelBuffer(f) => match get_rgba_from_pixelbuf(f) {
|
|
Ok(rgba) => ("".to_owned(), f.width(), f.height(), rgba),
|
|
Err(e) => {
|
|
let serr = e.to_string();
|
|
log::error!(
|
|
"Failed to convert the pix format into rgba, {}",
|
|
&serr
|
|
);
|
|
(format!("Convert pixfmt: {}", serr), 0, 0, vec![])
|
|
}
|
|
},
|
|
scrap::Frame::Texture(_) => {
|
|
if restore_vram {
|
|
// Already set one time, just ignore to break infinite loop.
|
|
// Though it's unreachable, this branch is kept to avoid infinite loop.
|
|
(
|
|
"Please change codec and try again.".to_owned(),
|
|
0,
|
|
0,
|
|
vec![],
|
|
)
|
|
} else {
|
|
#[cfg(all(windows, feature = "vram"))]
|
|
VRamEncoder::set_not_use(sp.name(), true);
|
|
screenshot.restore_vram = true;
|
|
SCREENSHOTS
|
|
.lock()
|
|
.unwrap()
|
|
.insert(screenshot_key, screenshot);
|
|
_raii.try_vram = false;
|
|
bail!("SWITCH");
|
|
}
|
|
}
|
|
};
|
|
std::thread::spawn(move || {
|
|
handle_screenshot(screenshot, msg, w, h, data);
|
|
});
|
|
if restore_vram {
|
|
bail!("SWITCH");
|
|
}
|
|
}
|
|
|
|
let frame = frame.to(encoder.yuvfmt(), &mut yuv, &mut mid_data)?;
|
|
let send_conn_ids = handle_one_frame(
|
|
display_idx,
|
|
&sp,
|
|
frame,
|
|
ms,
|
|
&mut encoder,
|
|
recorder.clone(),
|
|
&mut encode_fail_counter,
|
|
&mut first_frame,
|
|
capture_width,
|
|
capture_height,
|
|
)?;
|
|
if !send_conn_ids.is_empty() {
|
|
sent_counter += 1;
|
|
}
|
|
frame_controller.set_send(now, send_conn_ids);
|
|
send_counter += 1;
|
|
}
|
|
#[cfg(windows)]
|
|
{
|
|
#[cfg(feature = "vram")]
|
|
if try_gdi == 1 && !c.is_gdi() {
|
|
VRamEncoder::set_fallback_gdi(sp.name(), false);
|
|
}
|
|
try_gdi = 0;
|
|
}
|
|
Ok(())
|
|
}
|
|
Err(err) => Err(err),
|
|
};
|
|
|
|
match res {
|
|
Err(ref e) if e.kind() == WouldBlock => {
|
|
#[cfg(windows)]
|
|
if try_gdi > 0 && !c.is_gdi() {
|
|
if try_gdi > 3 {
|
|
c.set_gdi();
|
|
try_gdi = 0;
|
|
log::info!("No image, fall back to gdi");
|
|
}
|
|
try_gdi += 1;
|
|
}
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
would_block_count += 1;
|
|
if !is_x11() {
|
|
if would_block_count >= 100 {
|
|
// to-do: Unknown reason for WouldBlock 100 times (seconds = 100 * 1 / fps)
|
|
// https://github.com/rustdesk/rustdesk/blob/63e6b2f8ab51743e77a151e2b7ff18816f5fa2fb/libs/scrap/src/common/wayland.rs#L81
|
|
//
|
|
// Do not reset the capturer for now, as it will cause the prompt to show every few minutes.
|
|
// https://github.com/rustdesk/rustdesk/issues/4276
|
|
//
|
|
// super::wayland::clear();
|
|
// bail!("Wayland capturer none 100 times, try restart capture");
|
|
}
|
|
}
|
|
}
|
|
if !encoder.latency_free() && yuv.len() > 0 {
|
|
// yun.len() > 0 means the frame is not texture.
|
|
if repeat_encode_counter < repeat_encode_max {
|
|
repeat_encode_counter += 1;
|
|
let send_conn_ids = handle_one_frame(
|
|
display_idx,
|
|
&sp,
|
|
EncodeInput::YUV(&yuv),
|
|
ms,
|
|
&mut encoder,
|
|
recorder.clone(),
|
|
&mut encode_fail_counter,
|
|
&mut first_frame,
|
|
capture_width,
|
|
capture_height,
|
|
)?;
|
|
if !send_conn_ids.is_empty() {
|
|
sent_counter += 1;
|
|
}
|
|
frame_controller.set_send(now, send_conn_ids);
|
|
send_counter += 1;
|
|
}
|
|
}
|
|
}
|
|
Err(err) => {
|
|
// This check may be redundant, but it is better to be safe.
|
|
// The previous check in `sp.is_option_true(OPTION_REFRESH)` block may be enough.
|
|
if vs.source.is_monitor() {
|
|
try_broadcast_display_changed(&sp, display_idx, &c, true)?;
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
if !c.is_gdi() {
|
|
c.set_gdi();
|
|
log::info!("dxgi error, fall back to gdi: {:?}", err);
|
|
continue;
|
|
}
|
|
return Err(err.into());
|
|
}
|
|
_ => {
|
|
#[cfg(target_os = "linux")]
|
|
{
|
|
would_block_count = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut fetched_conn_ids = HashSet::new();
|
|
let timeout_millis = 3_000u64;
|
|
let wait_begin = Instant::now();
|
|
while wait_begin.elapsed().as_millis() < timeout_millis as _ {
|
|
if vs.source.is_monitor() {
|
|
check_privacy_mode_changed(&sp, display_idx, &c)?;
|
|
}
|
|
frame_controller.try_wait_next(&mut fetched_conn_ids, 300);
|
|
// break if all connections have received current frame
|
|
if fetched_conn_ids.len() >= frame_controller.send_conn_ids.len() {
|
|
break;
|
|
}
|
|
}
|
|
wait_max_ms = wait_max_ms.max(wait_begin.elapsed().as_millis() as u32);
|
|
DISPLAY_CONN_IDS.lock().unwrap().remove(&display_idx);
|
|
|
|
let elapsed = now.elapsed();
|
|
// may need to enable frame(timeout)
|
|
log::trace!("{:?} {:?}", time::Instant::now(), elapsed);
|
|
if elapsed < spf {
|
|
std::thread::sleep(spf - elapsed);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
struct Raii {
|
|
display_idx: usize,
|
|
name: String,
|
|
try_vram: bool,
|
|
}
|
|
|
|
impl Raii {
|
|
fn new(display_idx: usize, name: String) -> Self {
|
|
log::info!("new video service: {}", name);
|
|
VIDEO_QOS.lock().unwrap().new_display(name.clone());
|
|
Raii {
|
|
display_idx,
|
|
name,
|
|
try_vram: true,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for Raii {
|
|
fn drop(&mut self) {
|
|
log::info!("stop video service: {}", self.name);
|
|
#[cfg(feature = "vram")]
|
|
if self.try_vram {
|
|
VRamEncoder::set_not_use(self.name.clone(), false);
|
|
}
|
|
#[cfg(feature = "vram")]
|
|
Encoder::update(scrap::codec::EncodingUpdate::Check);
|
|
VIDEO_QOS.lock().unwrap().remove_display(&self.name);
|
|
DISPLAY_CONN_IDS.lock().unwrap().remove(&self.display_idx);
|
|
}
|
|
}
|
|
|
|
fn setup_encoder(
|
|
c: &CapturerInfo,
|
|
name: String,
|
|
quality: f32,
|
|
client_record: bool,
|
|
record_incoming: bool,
|
|
last_portable_service_running: bool,
|
|
source: VideoSource,
|
|
display_idx: usize,
|
|
) -> ResultType<(
|
|
Encoder,
|
|
EncoderCfg,
|
|
CodecFormat,
|
|
bool,
|
|
Arc<Mutex<Option<Recorder>>>,
|
|
)> {
|
|
let encoder_cfg = get_encoder_config(
|
|
&c,
|
|
name.to_string(),
|
|
quality,
|
|
client_record || record_incoming,
|
|
last_portable_service_running,
|
|
source,
|
|
);
|
|
Encoder::set_fallback(&encoder_cfg);
|
|
let codec_format = Encoder::negotiated_codec();
|
|
let recorder = get_recorder(record_incoming, display_idx, source == VideoSource::Camera);
|
|
let use_i444 = Encoder::use_i444(&encoder_cfg);
|
|
let encoder = Encoder::new(encoder_cfg.clone(), use_i444)?;
|
|
Ok((encoder, encoder_cfg, codec_format, use_i444, recorder))
|
|
}
|
|
|
|
fn get_encoder_config(
|
|
c: &CapturerInfo,
|
|
_name: String,
|
|
quality: f32,
|
|
record: bool,
|
|
_portable_service: bool,
|
|
_source: VideoSource,
|
|
) -> EncoderCfg {
|
|
#[cfg(all(windows, feature = "vram"))]
|
|
if _portable_service || c.is_gdi() || _source == VideoSource::Camera {
|
|
log::info!("gdi:{}, portable:{}", c.is_gdi(), _portable_service);
|
|
VRamEncoder::set_not_use(_name, true);
|
|
}
|
|
#[cfg(feature = "vram")]
|
|
Encoder::update(scrap::codec::EncodingUpdate::Check);
|
|
// https://www.wowza.com/community/t/the-correct-keyframe-interval-in-obs-studio/95162
|
|
let keyframe_interval = if record { Some(240) } else { None };
|
|
let negotiated_codec = Encoder::negotiated_codec();
|
|
match negotiated_codec {
|
|
CodecFormat::H264 | CodecFormat::H265 => {
|
|
#[cfg(feature = "vram")]
|
|
if let Some(feature) = VRamEncoder::try_get(&c.device(), negotiated_codec) {
|
|
return EncoderCfg::VRAM(VRamEncoderConfig {
|
|
device: c.device(),
|
|
width: c.width,
|
|
height: c.height,
|
|
quality,
|
|
feature,
|
|
keyframe_interval,
|
|
});
|
|
}
|
|
#[cfg(feature = "hwcodec")]
|
|
if let Some(hw) = HwRamEncoder::try_get(negotiated_codec) {
|
|
return EncoderCfg::HWRAM(HwRamEncoderConfig {
|
|
name: hw.name,
|
|
mc_name: hw.mc_name,
|
|
width: c.width,
|
|
height: c.height,
|
|
quality,
|
|
keyframe_interval,
|
|
});
|
|
}
|
|
EncoderCfg::VPX(VpxEncoderConfig {
|
|
width: c.width as _,
|
|
height: c.height as _,
|
|
quality,
|
|
codec: VpxVideoCodecId::VP9,
|
|
keyframe_interval,
|
|
})
|
|
}
|
|
format @ (CodecFormat::VP8 | CodecFormat::VP9) => EncoderCfg::VPX(VpxEncoderConfig {
|
|
width: c.width as _,
|
|
height: c.height as _,
|
|
quality,
|
|
codec: if format == CodecFormat::VP8 {
|
|
VpxVideoCodecId::VP8
|
|
} else {
|
|
VpxVideoCodecId::VP9
|
|
},
|
|
keyframe_interval,
|
|
}),
|
|
CodecFormat::AV1 => EncoderCfg::AOM(AomEncoderConfig {
|
|
width: c.width as _,
|
|
height: c.height as _,
|
|
quality,
|
|
keyframe_interval,
|
|
}),
|
|
_ => EncoderCfg::VPX(VpxEncoderConfig {
|
|
width: c.width as _,
|
|
height: c.height as _,
|
|
quality,
|
|
codec: VpxVideoCodecId::VP9,
|
|
keyframe_interval,
|
|
}),
|
|
}
|
|
}
|
|
|
|
fn get_recorder(
|
|
record_incoming: bool,
|
|
display_idx: usize,
|
|
camera: bool,
|
|
) -> Arc<Mutex<Option<Recorder>>> {
|
|
#[cfg(windows)]
|
|
let root = crate::platform::is_root();
|
|
#[cfg(not(windows))]
|
|
let root = false;
|
|
let recorder = if record_incoming {
|
|
use crate::hbbs_http::record_upload;
|
|
|
|
let tx = if record_upload::is_enable() {
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
|
record_upload::run(rx);
|
|
Some(tx)
|
|
} else {
|
|
None
|
|
};
|
|
Recorder::new(RecorderContext {
|
|
server: true,
|
|
id: Config::get_id(),
|
|
dir: crate::ui_interface::video_save_directory(root),
|
|
display_idx,
|
|
camera,
|
|
tx,
|
|
})
|
|
.map_or(Default::default(), |r| Arc::new(Mutex::new(Some(r))))
|
|
} else {
|
|
Default::default()
|
|
};
|
|
|
|
recorder
|
|
}
|
|
|
|
#[cfg(target_os = "android")]
|
|
fn check_change_scale(hardware: bool) -> ResultType<()> {
|
|
use base::config::keys::OPTION_ENABLE_ANDROID_SOFTWARE_ENCODING_HALF_SCALE as SCALE_SOFT;
|
|
|
|
// isStart flag is set at the end of startCapture() in Android, wait it to be set.
|
|
let n = 60; // 3s
|
|
for i in 0..n {
|
|
if scrap::is_start() == Some(true) {
|
|
log::info!("start flag is set");
|
|
break;
|
|
}
|
|
log::info!("wait for start, {i}");
|
|
std::thread::sleep(Duration::from_millis(50));
|
|
if i == n - 1 {
|
|
log::error!("wait for start timeout");
|
|
}
|
|
}
|
|
let screen_size = scrap::screen_size();
|
|
let scale_soft = hbb_common::config::option2bool(SCALE_SOFT, &Config::get_option(SCALE_SOFT));
|
|
let half_scale = !hardware && scale_soft;
|
|
log::info!("hardware: {hardware}, scale_soft: {scale_soft}, screen_size: {screen_size:?}",);
|
|
scrap::android::call_main_service_set_by_name(
|
|
"half_scale",
|
|
Some(half_scale.to_string().as_str()),
|
|
None,
|
|
)
|
|
.ok();
|
|
let old_scale = screen_size.2;
|
|
let new_scale = scrap::screen_size().2;
|
|
log::info!("old_scale: {old_scale}, new_scale: {new_scale}");
|
|
if old_scale != new_scale {
|
|
log::info!("switch due to scale changed, {old_scale} -> {new_scale}");
|
|
// switch is not a must, but it is better to do so.
|
|
bail!("SWITCH");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn check_privacy_mode_changed(
|
|
sp: &GenericService,
|
|
display_idx: usize,
|
|
ci: &CapturerInfo,
|
|
) -> ResultType<()> {
|
|
let privacy_mode_id_2 = get_privacy_mode_conn_id().unwrap_or(INVALID_PRIVACY_MODE_CONN_ID);
|
|
if ci.privacy_mode_id != privacy_mode_id_2 {
|
|
if privacy_mode_id_2 != INVALID_PRIVACY_MODE_CONN_ID {
|
|
let msg_out = crate::common::make_privacy_mode_msg(
|
|
back_notification::PrivacyModeState::PrvOnByOther,
|
|
"".to_owned(),
|
|
);
|
|
sp.send_to_others(msg_out, privacy_mode_id_2);
|
|
}
|
|
log::info!("switch due to privacy mode changed");
|
|
try_broadcast_display_changed(&sp, display_idx, ci, true).ok();
|
|
bail!("SWITCH");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[inline]
|
|
fn handle_one_frame(
|
|
display: usize,
|
|
sp: &GenericService,
|
|
frame: EncodeInput,
|
|
ms: i64,
|
|
encoder: &mut Encoder,
|
|
recorder: Arc<Mutex<Option<Recorder>>>,
|
|
encode_fail_counter: &mut usize,
|
|
first_frame: &mut bool,
|
|
width: usize,
|
|
height: usize,
|
|
) -> ResultType<HashSet<i32>> {
|
|
sp.snapshot(|sps| {
|
|
// so that new sub and old sub share the same encoder after switch
|
|
if sps.has_subscribes() {
|
|
log::info!("switch due to new subscriber");
|
|
bail!("SWITCH");
|
|
}
|
|
Ok(())
|
|
})?;
|
|
|
|
let mut send_conn_ids: HashSet<i32> = Default::default();
|
|
let first = *first_frame;
|
|
*first_frame = false;
|
|
match encoder.encode_to_message(frame, ms) {
|
|
Ok(mut vf) => {
|
|
*encode_fail_counter = 0;
|
|
vf.display = display as _;
|
|
let mut msg = Message::new();
|
|
msg.set_video_frame(vf);
|
|
recorder
|
|
.lock()
|
|
.unwrap()
|
|
.as_mut()
|
|
.map(|r| r.write_message(&msg, width, height));
|
|
send_conn_ids = sp.send_video_frame(msg);
|
|
}
|
|
Err(e) => {
|
|
*encode_fail_counter += 1;
|
|
// Encoding errors are not frequent except on Android
|
|
if !cfg!(target_os = "android") {
|
|
log::error!("encode fail: {e:?}, times: {}", *encode_fail_counter,);
|
|
}
|
|
let max_fail_times = if cfg!(target_os = "android") && encoder.is_hardware() {
|
|
9
|
|
} else {
|
|
3
|
|
};
|
|
let repeat = !encoder.latency_free();
|
|
// repeat encoders can reach max_fail_times on the first frame
|
|
if (first && !repeat) || *encode_fail_counter >= max_fail_times {
|
|
*encode_fail_counter = 0;
|
|
if encoder.is_hardware() {
|
|
encoder.disable();
|
|
log::error!("switch due to encoding fails, first frame: {first}, error: {e:?}");
|
|
bail!("SWITCH");
|
|
}
|
|
}
|
|
match e.to_string().as_str() {
|
|
scrap::codec::ENCODE_NEED_SWITCH => {
|
|
encoder.disable();
|
|
log::error!("switch due to encoder need switch");
|
|
bail!("SWITCH");
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
Ok(send_conn_ids)
|
|
}
|
|
|
|
#[inline]
|
|
pub fn refresh() {
|
|
#[cfg(target_os = "android")]
|
|
Display::refresh_size();
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn start_uac_elevation_check() {
|
|
static START: Once = Once::new();
|
|
START.call_once(|| {
|
|
if !crate::platform::is_installed() && !crate::platform::is_root() {
|
|
std::thread::spawn(|| loop {
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
if let Ok(uac) = is_process_consent_running() {
|
|
*IS_UAC_RUNNING.lock().unwrap() = uac;
|
|
}
|
|
if !crate::platform::is_elevated(None).unwrap_or(false) {
|
|
if let Ok(elevated) = crate::platform::is_foreground_window_elevated() {
|
|
*IS_FOREGROUND_WINDOW_ELEVATED.lock().unwrap() = elevated;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
#[inline]
|
|
fn try_broadcast_display_changed(
|
|
sp: &GenericService,
|
|
display_idx: usize,
|
|
cap: &CapturerInfo,
|
|
refresh: bool,
|
|
) -> ResultType<()> {
|
|
if refresh {
|
|
// Get display information immediately.
|
|
crate::display_service::check_displays_changed().ok();
|
|
}
|
|
if let Some(display) = check_display_changed(
|
|
cap.ndisplay,
|
|
cap.current,
|
|
(cap.origin.0, cap.origin.1, cap.width, cap.height),
|
|
) {
|
|
log::info!("Display {} changed", display);
|
|
if let Some(msg_out) =
|
|
make_display_changed_msg(display_idx, Some(display), VideoSource::Monitor)
|
|
{
|
|
let msg_out = Arc::new(msg_out);
|
|
sp.send_shared(msg_out.clone());
|
|
// switch display may occur before the first video frame, add snapshot to send to new subscribers
|
|
sp.snapshot(move |sps| {
|
|
sps.send_shared(msg_out.clone());
|
|
Ok(())
|
|
})?;
|
|
bail!("SWITCH");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn make_display_changed_msg(
|
|
display_idx: usize,
|
|
opt_display: Option<DisplayInfo>,
|
|
source: VideoSource,
|
|
) -> Option<Message> {
|
|
let display = match opt_display {
|
|
Some(d) => d,
|
|
None => match source {
|
|
VideoSource::Monitor => display_service::get_display_info(display_idx)?,
|
|
VideoSource::Camera => camera::Cameras::get_sync_cameras()
|
|
.get(display_idx)?
|
|
.clone(),
|
|
},
|
|
};
|
|
let mut misc = Misc::new();
|
|
misc.set_switch_display(SwitchDisplay {
|
|
display: display_idx as _,
|
|
x: display.x,
|
|
y: display.y,
|
|
width: display.width,
|
|
height: display.height,
|
|
cursor_embedded: match source {
|
|
VideoSource::Monitor => display_service::capture_cursor_embedded(),
|
|
VideoSource::Camera => false,
|
|
},
|
|
#[cfg(not(target_os = "android"))]
|
|
resolutions: Some(SupportedResolutions {
|
|
resolutions: match source {
|
|
VideoSource::Monitor => {
|
|
if display.name.is_empty() {
|
|
vec![]
|
|
} else {
|
|
crate::platform::resolutions(&display.name)
|
|
}
|
|
}
|
|
VideoSource::Camera => camera::Cameras::get_camera_resolution(display_idx)
|
|
.ok()
|
|
.into_iter()
|
|
.collect(),
|
|
},
|
|
..SupportedResolutions::default()
|
|
})
|
|
.into(),
|
|
original_resolution: display.original_resolution,
|
|
..Default::default()
|
|
});
|
|
let mut msg_out = Message::new();
|
|
msg_out.set_misc(misc);
|
|
Some(msg_out)
|
|
}
|
|
|
|
/// Per-second pipeline diagnostics, off unless `RUSTDESK_QOS_VERBOSE` is set.
|
|
/// The default log level is `debug`, so an unconditional line here would land in
|
|
/// every user's log file once a second forever. Nothing enables it implicitly.
|
|
pub(crate) fn qos_diag_verbose() -> bool {
|
|
static VERBOSE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
|
*VERBOSE.get_or_init(|| std::env::var("RUSTDESK_QOS_VERBOSE").is_ok())
|
|
}
|
|
|
|
fn check_qos(
|
|
encoder: &mut Encoder,
|
|
ratio: &mut f32,
|
|
spf: &mut Duration,
|
|
client_record: bool,
|
|
send_counter: &mut usize,
|
|
sent_counter: &mut usize,
|
|
wait_max_ms: &mut u32,
|
|
second_instant: &mut Instant,
|
|
name: &str,
|
|
) -> ResultType<()> {
|
|
let mut video_qos = VIDEO_QOS.lock().unwrap();
|
|
*spf = video_qos.spf();
|
|
if *ratio != video_qos.ratio() {
|
|
*ratio = video_qos.ratio();
|
|
if encoder.support_changing_quality() {
|
|
allow_err!(encoder.set_quality(*ratio));
|
|
video_qos.store_bitrate(encoder.bitrate());
|
|
} else {
|
|
// Now only vaapi doesn't support changing quality
|
|
if !video_qos.in_vbr_state() && !video_qos.latest_quality().is_custom() {
|
|
log::info!("switch to change quality");
|
|
bail!("SWITCH");
|
|
}
|
|
}
|
|
}
|
|
if client_record != video_qos.record() {
|
|
log::info!("switch due to record changed");
|
|
bail!("SWITCH");
|
|
}
|
|
if second_instant.elapsed() > Duration::from_secs(1) {
|
|
*second_instant = Instant::now();
|
|
video_qos.update_display_data(&name, *send_counter);
|
|
// Diagnostics only, joined with `qos_trace` on `t`: the controller's target
|
|
// is not the rate the encoder produced, and neither is the rate the send
|
|
// path accepted.
|
|
if qos_diag_verbose() {
|
|
log::debug!(
|
|
"qos_video t={} display={name} captured={} sent={} wait_max={}",
|
|
hbb_common::get_time(),
|
|
*send_counter,
|
|
*sent_counter,
|
|
*wait_max_ms
|
|
);
|
|
}
|
|
*send_counter = 0;
|
|
*sent_counter = 0;
|
|
*wait_max_ms = 0;
|
|
}
|
|
drop(video_qos);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn set_take_screenshot(source: VideoSource, display_idx: usize, sid: String, tx: Sender) {
|
|
SCREENSHOTS.lock().unwrap().insert(
|
|
(source, display_idx),
|
|
Screenshot {
|
|
sid,
|
|
tx,
|
|
restore_vram: false,
|
|
},
|
|
);
|
|
}
|
|
|
|
// We need to this function, because the `stride` may be larger than `width * 4`.
|
|
fn get_rgba_from_pixelbuf<'a>(pixbuf: &scrap::PixelBuffer<'a>) -> ResultType<Vec<u8>> {
|
|
let w = pixbuf.width();
|
|
let h = pixbuf.height();
|
|
let stride = pixbuf.stride();
|
|
let Some(s) = stride.get(0) else {
|
|
bail!("Invalid pixel buf stride.")
|
|
};
|
|
|
|
if *s == w * 4 {
|
|
let mut rgba = vec![];
|
|
scrap::convert(pixbuf, scrap::Pixfmt::RGBA, &mut rgba)?;
|
|
Ok(rgba)
|
|
} else {
|
|
let bgra = pixbuf.data();
|
|
let mut bit_flipped = Vec::with_capacity(w * h * 4);
|
|
for y in 0..h {
|
|
for x in 0..w {
|
|
let i = s * y + 4 * x;
|
|
bit_flipped.extend_from_slice(&[bgra[i + 2], bgra[i + 1], bgra[i], bgra[i + 3]]);
|
|
}
|
|
}
|
|
Ok(bit_flipped)
|
|
}
|
|
}
|
|
|
|
fn handle_screenshot(screenshot: Screenshot, msg: String, w: usize, h: usize, data: Vec<u8>) {
|
|
let mut response = ScreenshotResponse::new();
|
|
response.sid = screenshot.sid;
|
|
if msg.is_empty() {
|
|
if data.is_empty() {
|
|
response.msg = "Failed to take screenshot, please try again later.".to_owned();
|
|
} else {
|
|
fn encode_png(width: usize, height: usize, rgba: Vec<u8>) -> ResultType<Vec<u8>> {
|
|
let mut png = Vec::new();
|
|
let mut encoder =
|
|
repng::Options::smallest(width as _, height as _).build(&mut png)?;
|
|
encoder.write(&rgba)?;
|
|
encoder.finish()?;
|
|
Ok(png)
|
|
}
|
|
match encode_png(w as _, h as _, data) {
|
|
Ok(png) => {
|
|
response.data = png.into();
|
|
}
|
|
Err(e) => {
|
|
response.msg = format!("Error encoding png: {}", e);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
response.msg = msg;
|
|
}
|
|
let mut msg_out = Message::new();
|
|
msg_out.set_screenshot_response(response);
|
|
if let Err(e) = screenshot
|
|
.tx
|
|
.send((hbb_common::tokio::time::Instant::now(), Arc::new(msg_out)))
|
|
{
|
|
log::error!("Failed to send screenshot, {}", e);
|
|
}
|
|
}
|