2023-02-09 16:04:36 -08:00
|
|
|
use cctk::{
|
2025-01-29 15:09:07 -08:00
|
|
|
screencopy::{CaptureSession, CaptureSource, ScreencopyState},
|
|
|
|
|
wayland_client::QueueHandle,
|
2023-02-09 16:04:36 -08:00
|
|
|
};
|
2023-11-21 16:15:02 -05:00
|
|
|
use cosmic::cctk;
|
2023-02-09 16:04:36 -08:00
|
|
|
|
2024-01-25 15:58:34 -08:00
|
|
|
use std::sync::{Arc, Mutex};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
2024-01-25 15:44:30 -08:00
|
|
|
use super::{AppData, ScreencopySession, SessionData};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
pub struct Capture {
|
|
|
|
|
pub source: CaptureSource,
|
2024-01-25 15:44:30 -08:00
|
|
|
pub session: Mutex<Option<ScreencopySession>>,
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Capture {
|
2024-01-25 15:44:30 -08:00
|
|
|
pub fn new(source: CaptureSource) -> Arc<Capture> {
|
2024-01-24 15:59:18 -08:00
|
|
|
Arc::new(Capture {
|
|
|
|
|
source,
|
|
|
|
|
session: Mutex::new(None),
|
2024-01-24 15:29:03 -08:00
|
|
|
})
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:29:03 -08:00
|
|
|
// Returns `None` if capture is destroyed
|
2024-01-24 13:58:12 -08:00
|
|
|
// (or if `session` wasn't created with `SessionData`)
|
2025-01-29 15:09:07 -08:00
|
|
|
pub fn for_session(session: &CaptureSession) -> Option<Arc<Self>> {
|
2024-01-24 15:29:03 -08:00
|
|
|
session.data::<SessionData>()?.capture.upgrade()
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:29:03 -08:00
|
|
|
// Start capturing frames
|
2024-03-18 17:57:18 -07:00
|
|
|
pub fn start(self: &Arc<Self>, screencopy_state: &ScreencopyState, qh: &QueueHandle<AppData>) {
|
2024-01-24 15:59:18 -08:00
|
|
|
let mut session = self.session.lock().unwrap();
|
|
|
|
|
if session.is_none() {
|
2024-03-18 17:57:18 -07:00
|
|
|
*session = Some(ScreencopySession::new(self, screencopy_state, qh));
|
2024-01-24 15:29:03 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop capturing. Can be started again with `start`
|
|
|
|
|
pub fn stop(&self) {
|
2024-01-25 15:44:30 -08:00
|
|
|
self.session.lock().unwrap().take();
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
2024-01-24 15:29:03 -08:00
|
|
|
}
|