Skip to main content

waycap_rs/pipeline/
builder.rs

1use crate::{
2    encoders::dynamic_encoder::DynamicEncoder,
3    types::{
4        config::{AudioEncoder, QualityPreset, VideoEncoder},
5        error::Result,
6    },
7    Capture,
8};
9
10pub struct CaptureBuilder {
11    video_encoder: Option<VideoEncoder>,
12    audio_encoder: Option<AudioEncoder>,
13    quality_preset: Option<QualityPreset>,
14    include_cursor: bool,
15    include_audio: bool,
16    target_fps: u64,
17    restore_token: Option<String>,
18}
19
20impl Default for CaptureBuilder {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl CaptureBuilder {
27    pub fn new() -> Self {
28        Self {
29            video_encoder: None,
30            audio_encoder: None,
31            quality_preset: None,
32            include_cursor: false,
33            include_audio: false,
34            target_fps: 60,
35            restore_token: None,
36        }
37    }
38
39    /// Optional: Force use a specific video encoder.
40    /// Default: Uses EGL to determine GPU at runtime.
41    pub fn with_video_encoder(mut self, encoder: VideoEncoder) -> Self {
42        self.video_encoder = Some(encoder);
43        self
44    }
45
46    /// Optional: Force use a specific audio encoder.
47    /// Default: Opus audio encoder.
48    pub fn with_audio_encoder(mut self, encoder: AudioEncoder) -> Self {
49        self.audio_encoder = Some(encoder);
50        self
51    }
52
53    pub fn with_cursor_shown(mut self) -> Self {
54        self.include_cursor = true;
55        self
56    }
57
58    pub fn with_audio(mut self) -> Self {
59        self.include_audio = true;
60        self
61    }
62
63    pub fn with_quality_preset(mut self, quality: QualityPreset) -> Self {
64        self.quality_preset = Some(quality);
65        self
66    }
67
68    /// Optional: Set a target FPS for the recording.
69    /// Default: 60fps
70    pub fn with_target_fps(mut self, fps: u64) -> Self {
71        self.target_fps = fps;
72        self
73    }
74
75    /// Optional: Provide a restore token from a previous session to skip the
76    /// screen-recording permission prompt. Retrieve the token after a successful
77    /// build via [`crate::Capture::restore_token`].
78    pub fn with_restore_token(mut self, token: String) -> Self {
79        self.restore_token = Some(token);
80        self
81    }
82
83    pub fn build(self) -> Result<Capture<DynamicEncoder>> {
84        let quality = match self.quality_preset {
85            Some(qual) => qual,
86            None => QualityPreset::Medium,
87        };
88
89        let audio_encoder = if self.include_audio {
90            match self.audio_encoder {
91                Some(enc) => enc,
92                None => AudioEncoder::Opus,
93            }
94        } else {
95            AudioEncoder::Opus
96        };
97
98        Capture::new(
99            self.video_encoder,
100            audio_encoder,
101            quality,
102            self.include_cursor,
103            self.include_audio,
104            self.target_fps,
105            self.restore_token,
106        )
107    }
108}