waycap_rs/pipeline/
builder.rs1use crate::{
2 types::{
3 config::{AudioEncoder, QualityPreset, VideoEncoder},
4 error::{Result, WaycapError},
5 },
6 Capture,
7};
8
9pub struct CaptureBuilder {
10 video_encoder: Option<VideoEncoder>,
11 audio_encoder: Option<AudioEncoder>,
12 quality_preset: Option<QualityPreset>,
13 include_cursor: bool,
14 include_audio: bool,
15 target_fps: u64,
16}
17
18impl Default for CaptureBuilder {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl CaptureBuilder {
25 pub fn new() -> Self {
26 Self {
27 video_encoder: None,
28 audio_encoder: None,
29 quality_preset: None,
30 include_cursor: false,
31 include_audio: false,
32 target_fps: 60,
33 }
34 }
35
36 pub fn with_video_encoder(mut self, encoder: VideoEncoder) -> Self {
37 self.video_encoder = Some(encoder);
38 self
39 }
40
41 pub fn with_audio_encoder(mut self, encoder: AudioEncoder) -> Self {
42 self.audio_encoder = Some(encoder);
43 self
44 }
45
46 pub fn with_cursor_shown(mut self) -> Self {
47 self.include_cursor = true;
48 self
49 }
50
51 pub fn with_audio(mut self) -> Self {
52 self.include_audio = true;
53 self
54 }
55
56 pub fn with_quality_preset(mut self, quality: QualityPreset) -> Self {
57 self.quality_preset = Some(quality);
58 self
59 }
60
61 pub fn with_target_fps(mut self, fps: u64) -> Self {
62 self.target_fps = fps;
63 self
64 }
65
66 pub fn build(self) -> Result<Capture> {
67 let video_encoder = match self.video_encoder {
68 Some(enc) => enc,
69 None => {
70 return Err(WaycapError::Init(
71 "Video encoder was not specified".to_string(),
72 ))
73 }
74 };
75
76 let quality = match self.quality_preset {
77 Some(qual) => qual,
78 None => QualityPreset::Medium,
79 };
80
81 let audio_encoder = if self.include_audio {
82 match self.audio_encoder {
83 Some(enc) => enc,
84 None => {
85 return Err(WaycapError::Init(
86 "Include audio specified but no audio encoder chosen.".to_string(),
87 ))
88 }
89 }
90 } else {
91 AudioEncoder::Opus
92 };
93
94 Capture::new(
95 video_encoder,
96 audio_encoder,
97 quality,
98 self.include_cursor,
99 self.include_audio,
100 self.target_fps,
101 )
102 }
103}