| 1 | use std::sync::Arc; |
| 2 | use std::sync::atomic::{AtomicU32, Ordering}; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use libwebrtc::desktop_capturer::{CaptureSource, DesktopCaptureSourceType, DesktopCapturer, DesktopCapturerOptions}; |
| 6 | use libwebrtc::native::yuv_helper::argb_to_i420; |
| 7 | use libwebrtc::video_frame::I420Buffer; |
| 8 | |
| 9 | use crate::capture::Capture; |
| 10 | use crate::error::{Error, Result}; |
| 11 | |
| 12 | |
| 13 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 14 | pub enum ShareKind { |
| 15 | Screen, |
| 16 | Window, |
| 17 | } |
| 18 | |
| 19 | impl ShareKind { |
| 20 | fn source_type(self) -> DesktopCaptureSourceType { |
| 21 | match self { |
| 22 | ShareKind::Screen => DesktopCaptureSourceType::Screen, |
| 23 | ShareKind::Window => DesktopCaptureSourceType::Window, |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 29 | pub struct ShareSource { |
| 30 | pub id: u64, |
| 31 | pub kind: ShareKind, |
| 32 | pub title: String, |
| 33 | } |
| 34 | |
| 35 | impl ShareSource { |
| 36 | pub fn label(&self) -> String { |
| 37 | match (self.kind, self.title.trim()) { |
| 38 | (ShareKind::Screen, "") => "Entire screen".to_owned(), |
| 39 | (_, "") => "Window".to_owned(), |
| 40 | (_, title) => title.to_owned(), |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn capturer(kind: ShareKind) -> Option<DesktopCapturer> { |
| 46 | let mut options = DesktopCapturerOptions::new(kind.source_type()); |
| 47 | options.set_include_cursor(true); |
| 48 | DesktopCapturer::new(options) |
| 49 | } |
| 50 | |
| 51 | fn sources_of(kind: ShareKind) -> Vec<(ShareSource, CaptureSource)> { |
| 52 | let Some(capturer) = capturer(kind) else { return Vec::new() }; |
| 53 | capturer |
| 54 | .get_source_list() |
| 55 | .into_iter() |
| 56 | .map(|source| { |
| 57 | ( |
| 58 | ShareSource { |
| 59 | id: source.id(), |
| 60 | kind, |
| 61 | title: source.title(), |
| 62 | }, |
| 63 | source, |
| 64 | ) |
| 65 | }) |
| 66 | .collect() |
| 67 | } |
| 68 | |
| 69 | pub fn list_share_sources() -> Vec<ShareSource> { |
| 70 | [ShareKind::Screen, ShareKind::Window] |
| 71 | .into_iter() |
| 72 | .flat_map(|kind| sources_of(kind).into_iter().map(|(source, _)| source)) |
| 73 | .collect() |
| 74 | } |
| 75 | |
| 76 | fn i420_from_bgra(bytes: &[u8], width: u32, height: u32, stride: u32) -> I420Buffer { |
| 77 | let mut buffer = I420Buffer::new(width, height); |
| 78 | let (stride_y, stride_u, stride_v) = buffer.strides(); |
| 79 | let (plane_y, plane_u, plane_v) = buffer.data_mut(); |
| 80 | argb_to_i420(bytes, stride, plane_y, stride_y, plane_u, stride_u, plane_v, stride_v, width as i32, height as i32); |
| 81 | buffer |
| 82 | } |
| 83 | |
| 84 | pub fn start_screen_capture( |
| 85 | wanted: ShareSource, |
| 86 | fps: Arc<AtomicU32>, |
| 87 | mut deliver: impl FnMut(I420Buffer) + Send + 'static, |
| 88 | failed: impl FnOnce(String) + Send + 'static, |
| 89 | ) -> Result<Capture> { |
| 90 | Capture::spawn_thread("screen-capture", move |stop| { |
| 91 | let Some(source) = sources_of(wanted.kind).into_iter().find(|(source, _)| source.id == wanted.id).map(|(_, raw)| raw) else { |
| 92 | return failed("the shared source is gone".to_owned()); |
| 93 | }; |
| 94 | let Some(mut capturer) = capturer(wanted.kind) else { |
| 95 | return failed("screen capture is not available".to_owned()); |
| 96 | }; |
| 97 | capturer.start_capture(Some(source), move |result| { |
| 98 | let Ok(frame) = result else { return }; |
| 99 | let (width, height) = (frame.width() as u32 & !1, frame.height() as u32 & !1); |
| 100 | if width >= 2 && height >= 2 { |
| 101 | deliver(i420_from_bgra(frame.data(), width, height, frame.stride())); |
| 102 | } |
| 103 | }); |
| 104 | while !stop.load(Ordering::SeqCst) { |
| 105 | capturer.capture_frame(); |
| 106 | std::thread::sleep(Duration::from_secs(1) / fps.load(Ordering::Relaxed).max(1)); |
| 107 | } |
| 108 | }) |
| 109 | .map_err(|error| Error::Webrtc(format!("screen thread: {error}"))) |
| 110 | } |
| 111 | |
| 112 | #[cfg(test)] |
| 113 | mod tests { |
| 114 | use super::*; |
| 115 | |
| 116 | #[test] |
| 117 | fn sources_get_readable_labels() { |
| 118 | let screen = ShareSource { id: 1, kind: ShareKind::Screen, title: String::new() }; |
| 119 | assert_eq!(screen.label(), "Entire screen"); |
| 120 | let window = ShareSource { id: 2, kind: ShareKind::Window, title: " Notes ".into() }; |
| 121 | assert_eq!(window.label(), "Notes"); |
| 122 | } |
| 123 | } |