1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::fmt::Debug;
use std::{error::Error, fmt::Display};

use crate::platform::platform_impl::{ImplAudioCaptureConfig, ImplCaptureAccessToken, ImplCaptureConfig, ImplCaptureStream};
use crate::capturable_content::Capturable;
use crate::prelude::{AudioChannelCount, AudioFrame, AudioSampleRate, CapturableDisplay, CapturableWindow, VideoFrame};
use crate::util::Size;

/// Represents an event in a capture stream
#[derive(Debug)]
pub enum StreamEvent {
    /// This event is produced when the stream receives a new audio packet
    Audio(AudioFrame),
    /// This event is produced when the stream receives a new video frame
    Video(VideoFrame),
    /// This event is produced when the stream goes idle - IE when no new frames are expected for some time, like when a window minimizes
    Idle,
    /// This event is produced once at the end of the stream
    End,
}

/// This represents an error during a stream, for example a failure to retrieve a video or audio frame
#[derive(Debug, Clone)]
pub enum StreamError {
    Other(String),
}

impl Display for StreamError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Other(message) => f.write_fmt(format_args!("StreamError::Other(\"{}\")", message))
        }
    }
}

impl Error for StreamError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }

    fn description(&self) -> &str {
        "description() is deprecated; use Display"
    }

    fn cause(&self) -> Option<&dyn Error> {
        self.source()
    }
}

/// This represents an error when creating a capture stream
#[derive(Debug, Clone)]
pub enum StreamCreateError {
    Other(String),
    /// The supplied pixel format is unsupported by the implementation
    UnsupportedPixelFormat,
    //GpuLost,
    /// Requested features are not authorized
    UnauthorizedFeature(String),
}

unsafe impl Send for StreamCreateError {}
unsafe impl Sync for StreamCreateError {}


impl Display for StreamCreateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Other(message) => f.write_fmt(format_args!("StreamCreateError::Other(\"{}\")", message)),
            Self::UnsupportedPixelFormat => f.write_fmt(format_args!("StreamCreateError::UnsupportedPixelFormat")),
            Self::UnauthorizedFeature(feature) => f.write_fmt(format_args!("StreamCreateError::UnauthorizedFeature({})", feature)),
        }
    }
}

impl Error for StreamCreateError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }

    fn description(&self) -> &str {
        "description() is deprecated; use Display"
    }

    fn cause(&self) -> Option<&dyn Error> {
        self.source()
    }
}

/// This represents an error while stopping a stream
#[derive(Debug)]
pub enum StreamStopError {
    Other(String),
    /// The stream was already stopped
    AlreadyStopped,
    //GpuLost,
}

unsafe impl Send for StreamStopError {}
unsafe impl Sync for StreamStopError {}

impl Display for StreamStopError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Other(message) => f.write_fmt(format_args!("StreamStopError::Other(\"{}\")", message)),
            Self::AlreadyStopped => f.write_fmt(format_args!("StreamStopError::AlreadyStopped")),
        }
    }
}

impl Error for StreamStopError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }

    fn description(&self) -> &str {
        "description() is deprecated; use Display"
    }

    fn cause(&self) -> Option<&dyn Error> {
        self.source()
    }
}

/// Configuration settings for audio streams
#[derive(Clone, Debug)]
#[allow(unused)]
pub struct AudioCaptureConfig {
    pub(crate) sample_rate: AudioSampleRate, 
    pub(crate) channel_count: AudioChannelCount,
    pub(crate) impl_capture_audio_config: ImplAudioCaptureConfig,
}

impl AudioCaptureConfig {
    /// Creates a new audio capture config with default settings:
    /// * 24000 Hz
    /// * Mono
    pub fn new() -> Self {
        Self {
            sample_rate: AudioSampleRate::Hz24000,
            channel_count: AudioChannelCount::Mono,
            impl_capture_audio_config: ImplAudioCaptureConfig::new()
        }
    }
}

/// The pixel format of returned video frames
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CapturePixelFormat {
    /// One plane, 4 channels, 8 bits per channel: { b: u8, g: u8, r: u8, a: u8 }, full range: [0, 255]
    Bgra8888,
    /// One plane, 4 channels, 10 bits per color channel, two bits for alpha: { a: u2, r: u10, g: u10, b: u10 }, rgb range: [0, 1023], alpha range: [0, 3]
    Argb2101010,
    /// Two planes:
    /// * 1 channel, luminance (Y), 8 bits per pixel, video range: [16, 240]
    /// * 2 channels, chrominance (CbCr) 8 bits bits per channel per two pixels vertically, range: [0, 255]
    V420,
    /// Two planes:
    /// * 1 channel, luminance (Y), 8 bits per pixel, full range: [0, 255]
    /// * 2 channels, chrominance (CbCr) 8 bits bits per channel per two pixels vertically, range: [0, 255]
    F420,
}

/// Configuration settings for a capture stream
#[derive(Clone, Debug)]
pub struct CaptureConfig {
    pub(crate) target: Capturable,
    pub(crate) output_size: Size,
    pub(crate) show_cursor: bool,
    pub(crate) pixel_format: CapturePixelFormat,
    pub(crate) capture_audio: Option<AudioCaptureConfig>,
    pub(crate) impl_capture_config: ImplCaptureConfig,
    pub(crate) buffer_count: usize,
}

/// Represents an error creating the capture config
#[derive(Debug, Clone)]
pub enum CaptureConfigError {
    /// The pixel format is unsupported by the implementation
    UnsupportedPixelFormat,
    /// The buffer count is out of the valid range for the implementation
    InvalidBufferCount,
}


unsafe impl Send for CaptureConfigError {}
unsafe impl Sync for CaptureConfigError {}

impl Display for CaptureConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnsupportedPixelFormat => f.write_fmt(format_args!("CaptureConfigError::UnsupportedPixelFormat")),
            Self::InvalidBufferCount => f.write_fmt(format_args!("CaptureConfigError::InvalidBufferCount")),
        }
    }
}

impl Error for CaptureConfigError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }

    fn description(&self) -> &str {
        "description() is deprecated; use Display"
    }

    fn cause(&self) -> Option<&dyn Error> {
        self.source()
    }
}

impl CaptureConfig {
    /// Create a capture configuration for a given capturable window
    pub fn with_window(window: CapturableWindow, pixel_format: CapturePixelFormat) -> Result<CaptureConfig, CaptureConfigError> {
        let rect = window.rect();
        Ok(CaptureConfig {
            target: Capturable::Window(window),
            pixel_format,
            output_size: rect.size,
            show_cursor: false,
            impl_capture_config: ImplCaptureConfig::new(),
            capture_audio: None,
            buffer_count: 3,
        })
    }

    /// Create a capture configuration for a given capturable display
    pub fn with_display(display: CapturableDisplay, pixel_format: CapturePixelFormat) -> CaptureConfig {
        let rect = display.rect();
        CaptureConfig {
            target: Capturable::Display(display),
            pixel_format,
            output_size: rect.size,
            show_cursor: false,
            impl_capture_config: ImplCaptureConfig::new(),
            capture_audio: None,
            buffer_count: 3,
        }
    }

    /// Configure the buffer count - the number of frames in the capture queue.
    /// 
    /// Higher numbers mean higher latency, but smoother performance
    pub fn with_buffer_count(self, buffer_count: usize) -> Self {
        Self {
            buffer_count,
            ..self
        }
    }

    /// Configure whether the cursor is visible in the capture
    pub fn with_show_cursor(self, show_cursor: bool) -> Self {
        Self {
            show_cursor,
            ..self
        }
    }

    /// Configure the output texture size - by default, this will match the captured content at the time of enumeration
    pub fn with_output_size(self, output_size: Size) -> Self {
        Self {
            output_size,
            ..self
        }
    }
}

/// Represents an active capture stream
pub struct CaptureStream {
    pub(crate) impl_capture_stream: ImplCaptureStream,
}

unsafe impl Send for CaptureStream {}

/// Represents programmatic capture access
#[derive(Clone, Copy, Debug)]
pub struct CaptureAccessToken {
    pub(crate) impl_capture_access_token: ImplCaptureAccessToken
}

unsafe impl Send for CaptureAccessToken {}
unsafe impl Sync for CaptureAccessToken {}

impl CaptureAccessToken {
    pub fn allows_borderless(&self) -> bool {
        self.impl_capture_access_token.allows_borderless()
    }
}

impl CaptureStream {
    /// Test whether the calling application has permission to capture content
    pub fn test_access(borderless: bool) -> Option<CaptureAccessToken> {
        ImplCaptureStream::check_access(borderless).map(|impl_capture_access_token|
            CaptureAccessToken {
                impl_capture_access_token
            }
        )
    }

    /// Prompt the user for permission to capture content
    pub async fn request_access(borderless: bool) -> Option<CaptureAccessToken> {
        ImplCaptureStream::request_access(borderless).await.map(|impl_capture_access_token|
            CaptureAccessToken {
                impl_capture_access_token
            }
        )
    }

    /// Gets the implementation's supported pixel formats
    pub fn supported_pixel_formats() -> &'static [CapturePixelFormat] {
        ImplCaptureStream::supported_pixel_formats()
    }

    /// Start a new capture stream with the given stream callback
    pub fn new(token: CaptureAccessToken, config: CaptureConfig, callback: impl FnMut(Result<StreamEvent, StreamError>) + Send + 'static) -> Result<Self, StreamCreateError> {
        let boxed_callback = Box::new(callback);
        Ok(Self {
            impl_capture_stream: ImplCaptureStream::new(token.impl_capture_access_token, config, boxed_callback)?
        })
    }

    /// Stop the capture
    pub fn stop(&mut self) -> Result<(), StreamStopError> {
        self.impl_capture_stream.stop()
    }
}