try out wayland

This commit is contained in:
rustdesk
2021-07-23 17:52:38 +08:00
parent d83163b6a0
commit c15c44788c
12 changed files with 972 additions and 3 deletions

View File

@@ -0,0 +1,117 @@
use crate::common::{
wayland,
x11::{self, Frame},
};
use std::io;
pub enum Capturer {
X11(x11::Capturer),
WAYLAND(wayland::Capturer),
}
impl Capturer {
pub fn new(display: Display, yuv: bool) -> io::Result<Capturer> {
Ok(match display {
Display::X11(d) => Capturer::X11(x11::Capturer::new(d, yuv)?),
Display::WAYLAND(d) => Capturer::WAYLAND(wayland::Capturer::new(d, yuv)?),
})
}
pub fn width(&self) -> usize {
match self {
Capturer::X11(d) => d.width(),
Capturer::WAYLAND(d) => d.width(),
}
}
pub fn height(&self) -> usize {
match self {
Capturer::X11(d) => d.height(),
Capturer::WAYLAND(d) => d.height(),
}
}
pub fn frame<'a>(&'a mut self, timeout_ms: u32) -> io::Result<Frame<'a>> {
match self {
Capturer::X11(d) => d.frame(timeout_ms),
Capturer::WAYLAND(d) => d.frame(timeout_ms),
}
}
}
pub enum Display {
X11(x11::Display),
WAYLAND(wayland::Display),
}
#[inline]
fn is_wayland() -> bool {
std::env::var("IS_WAYLAND").is_ok()
|| std::env::var("XDG_SESSION_TYPE") == Ok("wayland".to_owned())
}
impl Display {
pub fn primary() -> io::Result<Display> {
Ok(if is_wayland() {
Display::WAYLAND(wayland::Display::primary()?)
} else {
Display::X11(x11::Display::primary()?)
})
}
pub fn all() -> io::Result<Vec<Display>> {
Ok(if is_wayland() {
wayland::Display::all()?
.drain(..)
.map(|x| Display::WAYLAND(x))
.collect()
} else {
x11::Display::all()?
.drain(..)
.map(|x| Display::X11(x))
.collect()
})
}
pub fn width(&self) -> usize {
match self {
Display::X11(d) => d.width(),
Display::WAYLAND(d) => d.width(),
}
}
pub fn height(&self) -> usize {
match self {
Display::X11(d) => d.height(),
Display::WAYLAND(d) => d.height(),
}
}
pub fn origin(&self) -> (i32, i32) {
match self {
Display::X11(d) => d.origin(),
Display::WAYLAND(d) => d.origin(),
}
}
pub fn is_online(&self) -> bool {
match self {
Display::X11(d) => d.is_online(),
Display::WAYLAND(d) => d.is_online(),
}
}
pub fn is_primary(&self) -> bool {
match self {
Display::X11(d) => d.is_primary(),
Display::WAYLAND(d) => d.is_primary(),
}
}
pub fn name(&self) -> String {
match self {
Display::X11(d) => d.name(),
Display::WAYLAND(d) => d.name(),
}
}
}

View File

@@ -5,8 +5,17 @@ cfg_if! {
mod quartz;
pub use self::quartz::*;
} else if #[cfg(x11)] {
cfg_if! {
if #[cfg(feature="wayland")] {
mod linux;
mod wayland;
mod x11;
pub use self::x11::*;
pub use self::linux::*;
} else {
mod x11;
pub use self::x11::*;
}
}
} else if #[cfg(dxgi)] {
mod dxgi;
pub use self::dxgi::*;

View File

@@ -0,0 +1,81 @@
use crate::common::x11::Frame;
use crate::wayland::{capturable::*, *};
use std::io;
pub struct Capturer(Display, Box<dyn Recorder>, bool, Vec<u8>);
fn map_err<E: ToString>(err: E) -> io::Error {
io::Error::new(io::ErrorKind::Other, err.to_string())
}
impl Capturer {
pub fn new(display: Display, yuv: bool) -> io::Result<Capturer> {
let r = display.0.recorder(false).map_err(map_err)?;
Ok(Capturer(display, r, yuv, Default::default()))
}
pub fn width(&self) -> usize {
self.0.width()
}
pub fn height(&self) -> usize {
self.0.height()
}
pub fn frame<'a>(&'a mut self, timeout_ms: u32) -> io::Result<Frame<'a>> {
match self.1.capture(timeout_ms as _).map_err(map_err)? {
PixelProvider::BGR0(w, h, x) => Ok(Frame(if self.2 {
crate::common::bgra_to_i420(w as _, h as _, &x, &mut self.3);
&self.3[..]
} else {
x
})),
PixelProvider::NONE => Err(std::io::ErrorKind::WouldBlock.into()),
_ => Err(map_err("Invalid data")),
}
}
}
pub struct Display(pipewire::PipeWireCapturable);
impl Display {
pub fn primary() -> io::Result<Display> {
let mut all = Display::all()?;
if all.is_empty() {
return Err(io::ErrorKind::NotFound.into());
}
Ok(all.remove(0))
}
pub fn all() -> io::Result<Vec<Display>> {
Ok(pipewire::get_capturables(false)
.map_err(map_err)?
.drain(..)
.map(|x| Display(x))
.collect())
}
pub fn width(&self) -> usize {
self.0.size.0
}
pub fn height(&self) -> usize {
self.0.size.1
}
pub fn origin(&self) -> (i32, i32) {
self.0.position
}
pub fn is_online(&self) -> bool {
true
}
pub fn is_primary(&self) -> bool {
false
}
pub fn name(&self) -> String {
"".to_owned()
}
}

View File

@@ -21,7 +21,7 @@ impl Capturer {
}
}
pub struct Frame<'a>(&'a [u8]);
pub struct Frame<'a>(pub(crate) &'a [u8]);
impl<'a> ops::Deref for Frame<'a> {
type Target = [u8];