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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use std::{
    mem,
    os::windows::prelude::AsRawHandle,
    sync::{
        atomic::{self, AtomicBool},
        mpsc, Arc,
    },
    thread::{self, JoinHandle},
};

use parking_lot::Mutex;
use windows::{
    Foundation::AsyncActionCompletedHandler,
    Graphics::Capture::GraphicsCaptureItem,
    Win32::{
        Foundation::{HANDLE, LPARAM, WPARAM},
        System::{
            Threading::{GetCurrentThreadId, GetThreadId},
            WinRT::{
                CreateDispatcherQueueController, DispatcherQueueOptions, RoInitialize,
                RoUninitialize, DQTAT_COM_NONE, DQTYPE_THREAD_CURRENT, RO_INIT_MULTITHREADED,
            },
        },
        UI::WindowsAndMessaging::{
            DispatchMessageW, GetMessageW, PostQuitMessage, PostThreadMessageW, TranslateMessage,
            MSG, WM_QUIT,
        },
    },
};

use crate::{
    frame::Frame,
    graphics_capture_api::{self, GraphicsCaptureApi, InternalCaptureControl},
    settings::Settings,
};

#[derive(thiserror::Error, Debug)]
pub enum CaptureControlError<E> {
    #[error("Failed to join thread")]
    FailedToJoinThread,
    #[error("Thread handle is taken out of struct")]
    ThreadHandleIsTaken,
    #[error("Failed to post thread message")]
    FailedToPostThreadMessage,
    #[error("Stopped handler error: {0}")]
    StoppedHandlerError(E),
    #[error("Windows capture error: {0}")]
    GraphicsCaptureApiError(#[from] GraphicsCaptureApiError<E>),
}

/// Used to control the capture session
pub struct CaptureControl<T: GraphicsCaptureApiHandler + Send + 'static + ?Sized, E> {
    thread_handle: Option<JoinHandle<Result<(), GraphicsCaptureApiError<E>>>>,
    halt_handle: Arc<AtomicBool>,
    callback: Arc<Mutex<T>>,
}

impl<T: GraphicsCaptureApiHandler + Send + 'static, E> CaptureControl<T, E> {
    /// Creates a new Capture Control struct.
    ///
    /// # Arguments
    ///
    /// * `thread_handle` - The join handle for the capture thread.
    /// * `halt_handle` - The atomic boolean used to pause the capture thread.
    /// * `callback` - The mutex-protected callback struct used to call struct methods directly.
    ///
    /// # Returns
    ///
    /// The newly created CaptureControl struct.
    #[must_use]
    pub fn new(
        thread_handle: JoinHandle<Result<(), GraphicsCaptureApiError<E>>>,
        halt_handle: Arc<AtomicBool>,
        callback: Arc<Mutex<T>>,
    ) -> Self {
        Self {
            thread_handle: Some(thread_handle),
            halt_handle,
            callback,
        }
    }

    /// Checks to see if the capture thread is finished.
    ///
    /// # Returns
    ///
    /// `true` if the capture thread is finished, `false` otherwise.
    #[must_use]
    pub fn is_finished(&self) -> bool {
        self.thread_handle
            .as_ref()
            .map_or(true, std::thread::JoinHandle::is_finished)
    }

    /// Gets the join handle for the capture thread.
    ///
    /// # Returns
    ///
    /// The join handle for the capture thread.
    #[must_use]
    pub fn into_thread_handle(self) -> JoinHandle<Result<(), GraphicsCaptureApiError<E>>> {
        self.thread_handle.unwrap()
    }

    /// Gets the halt handle used to pause the capture thread.
    ///
    /// # Returns
    ///
    /// The halt handle used to pause the capture thread.
    #[must_use]
    pub fn halt_handle(&self) -> Arc<AtomicBool> {
        self.halt_handle.clone()
    }

    /// Gets the callback struct used to call struct methods directly.
    ///
    /// # Returns
    ///
    /// The callback struct used to call struct methods directly.
    #[must_use]
    pub fn callback(&self) -> Arc<Mutex<T>> {
        self.callback.clone()
    }

    /// Waits until the capturing thread stops.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the capturing thread stops successfully, an error otherwise.
    pub fn wait(mut self) -> Result<(), CaptureControlError<E>> {
        if let Some(thread_handle) = self.thread_handle.take() {
            match thread_handle.join() {
                Ok(result) => result?,
                Err(_) => {
                    return Err(CaptureControlError::FailedToJoinThread);
                }
            }
        } else {
            return Err(CaptureControlError::ThreadHandleIsTaken);
        }

        Ok(())
    }

    /// Gracefully stops the capture thread.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the capture thread stops successfully, an error otherwise.
    pub fn stop(mut self) -> Result<(), CaptureControlError<E>> {
        self.halt_handle.store(true, atomic::Ordering::Relaxed);

        if let Some(thread_handle) = self.thread_handle.take() {
            let handle = thread_handle.as_raw_handle();
            let handle = HANDLE(handle as isize);
            let therad_id = unsafe { GetThreadId(handle) };

            loop {
                match unsafe {
                    PostThreadMessageW(therad_id, WM_QUIT, WPARAM::default(), LPARAM::default())
                } {
                    Ok(()) => break,
                    Err(e) => {
                        if thread_handle.is_finished() {
                            break;
                        }

                        if e.code().0 != -2_147_023_452 {
                            Err(e).map_err(|_| CaptureControlError::FailedToPostThreadMessage)?;
                        }
                    }
                }
            }

            match thread_handle.join() {
                Ok(result) => result?,
                Err(_) => {
                    return Err(CaptureControlError::FailedToJoinThread);
                }
            }
        } else {
            return Err(CaptureControlError::ThreadHandleIsTaken);
        }

        Ok(())
    }
}

#[derive(thiserror::Error, Eq, PartialEq, Clone, Debug)]
pub enum GraphicsCaptureApiError<E> {
    #[error("Failed to join thread")]
    FailedToJoinThread,
    #[error("Failed to initialize WinRT")]
    FailedToInitWinRT,
    #[error("Failed to create dispatcher queue controller")]
    FailedToCreateDispatcherQueueController,
    #[error("Failed to shutdown dispatcher queue")]
    FailedToShutdownDispatcherQueue,
    #[error("Failed to set dispatcher queue completed handler")]
    FailedToSetDispatcherQueueCompletedHandler,
    #[error("Failed to convert item to GraphicsCaptureItem")]
    ItemConvertFailed,
    #[error("Graphics capture error")]
    GraphicsCaptureApiError(graphics_capture_api::Error),
    #[error("New handler error")]
    NewHandlerError(E),
    #[error("Frame handler error")]
    FrameHandlerError(E),
}

/// A trait representing a graphics capture handler.

pub trait GraphicsCaptureApiHandler: Sized {
    /// The type of flags used to get the values from the settings.
    type Flags;

    /// The type of error that can occur during capture, the error will be returned from `CaptureControl` and `start` functions.
    type Error: Send + Sync;

    /// Starts the capture and takes control of the current thread.
    ///
    /// # Arguments
    ///
    /// * `settings` - The capture settings.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the capture was successful, otherwise returns an error of type `GraphicsCaptureApiError`.
    fn start<T: TryInto<GraphicsCaptureItem>>(
        settings: Settings<Self::Flags, T>,
    ) -> Result<(), GraphicsCaptureApiError<Self::Error>>
    where
        Self: Send + 'static,
        <Self as GraphicsCaptureApiHandler>::Flags: Send,
    {
        // Initialize WinRT
        unsafe {
            RoInitialize(RO_INIT_MULTITHREADED)
                .map_err(|_| GraphicsCaptureApiError::FailedToInitWinRT)?;
        };

        // Create a dispatcher queue for the current thread
        let options = DispatcherQueueOptions {
            dwSize: u32::try_from(mem::size_of::<DispatcherQueueOptions>()).unwrap(),
            threadType: DQTYPE_THREAD_CURRENT,
            apartmentType: DQTAT_COM_NONE,
        };
        let controller = unsafe {
            CreateDispatcherQueueController(options)
                .map_err(|_| GraphicsCaptureApiError::FailedToCreateDispatcherQueueController)?
        };

        // Get current thread ID
        let thread_id = unsafe { GetCurrentThreadId() };

        // Start capture
        let result = Arc::new(Mutex::new(None));
        let callback = Arc::new(Mutex::new(
            Self::new(settings.flags).map_err(GraphicsCaptureApiError::NewHandlerError)?,
        ));

        let item = settings
            .item
            .try_into()
            .map_err(|_| GraphicsCaptureApiError::ItemConvertFailed)?;

        let mut capture = GraphicsCaptureApi::new(
            item,
            callback,
            settings.cursor_capture,
            settings.draw_border,
            settings.color_format,
            thread_id,
            result.clone(),
        )
        .map_err(GraphicsCaptureApiError::GraphicsCaptureApiError)?;
        capture
            .start_capture()
            .map_err(GraphicsCaptureApiError::GraphicsCaptureApiError)?;

        // Message loop
        let mut message = MSG::default();
        unsafe {
            while GetMessageW(&mut message, None, 0, 0).as_bool() {
                let _ = TranslateMessage(&message);
                DispatchMessageW(&message);
            }
        }

        // Shutdown dispatcher queue
        let async_action = controller
            .ShutdownQueueAsync()
            .map_err(|_| GraphicsCaptureApiError::FailedToShutdownDispatcherQueue)?;
        async_action
            .SetCompleted(&AsyncActionCompletedHandler::new(
                move |_, _| -> Result<(), windows::core::Error> {
                    unsafe { PostQuitMessage(0) };
                    Ok(())
                },
            ))
            .map_err(|_| GraphicsCaptureApiError::FailedToSetDispatcherQueueCompletedHandler)?;

        // Final message loop
        let mut message = MSG::default();
        unsafe {
            while GetMessageW(&mut message, None, 0, 0).as_bool() {
                let _ = TranslateMessage(&message);
                DispatchMessageW(&message);
            }
        }

        // Stop capture
        capture.stop_capture();

        // Uninitialize WinRT
        unsafe { RoUninitialize() };

        // Check handler result
        if let Some(e) = result.lock().take() {
            return Err(GraphicsCaptureApiError::FrameHandlerError(e));
        }

        Ok(())
    }

    /// Starts the capture without taking control of the current thread.
    ///
    /// # Arguments
    ///
    /// * `settings` - The capture settings.
    ///
    /// # Returns
    ///
    /// Returns `Ok(CaptureControl)` if the capture was successful, otherwise returns an error of type `GraphicsCaptureApiError`.
    fn start_free_threaded<T: TryInto<GraphicsCaptureItem> + Send + 'static>(
        settings: Settings<Self::Flags, T>,
    ) -> Result<CaptureControl<Self, Self::Error>, GraphicsCaptureApiError<Self::Error>>
    where
        Self: Send + 'static,
        <Self as GraphicsCaptureApiHandler>::Flags: Send,
    {
        let (halt_sender, halt_receiver) = mpsc::channel::<Arc<AtomicBool>>();
        let (callback_sender, callback_receiver) = mpsc::channel::<Arc<Mutex<Self>>>();

        let thread_handle = thread::spawn(
            move || -> Result<(), GraphicsCaptureApiError<Self::Error>> {
                // Initialize WinRT
                unsafe {
                    RoInitialize(RO_INIT_MULTITHREADED)
                        .map_err(|_| GraphicsCaptureApiError::FailedToInitWinRT)?;
                };

                // Create a dispatcher queue for the current thread
                let options = DispatcherQueueOptions {
                    dwSize: u32::try_from(mem::size_of::<DispatcherQueueOptions>()).unwrap(),
                    threadType: DQTYPE_THREAD_CURRENT,
                    apartmentType: DQTAT_COM_NONE,
                };
                let controller = unsafe {
                    CreateDispatcherQueueController(options).map_err(|_| {
                        GraphicsCaptureApiError::FailedToCreateDispatcherQueueController
                    })?
                };

                // Get current thread ID
                let thread_id = unsafe { GetCurrentThreadId() };

                // Start capture
                let result = Arc::new(Mutex::new(None));
                let callback = Arc::new(Mutex::new(
                    Self::new(settings.flags).map_err(GraphicsCaptureApiError::NewHandlerError)?,
                ));

                let item = settings
                    .item
                    .try_into()
                    .map_err(|_| GraphicsCaptureApiError::ItemConvertFailed)?;

                let mut capture = GraphicsCaptureApi::new(
                    item,
                    callback.clone(),
                    settings.cursor_capture,
                    settings.draw_border,
                    settings.color_format,
                    thread_id,
                    result.clone(),
                )
                .map_err(GraphicsCaptureApiError::GraphicsCaptureApiError)?;
                capture
                    .start_capture()
                    .map_err(GraphicsCaptureApiError::GraphicsCaptureApiError)?;

                // Send halt handle
                let halt_handle = capture.halt_handle();
                halt_sender.send(halt_handle).unwrap();

                // Send callback
                callback_sender.send(callback).unwrap();

                // Message loop
                let mut message = MSG::default();
                unsafe {
                    while GetMessageW(&mut message, None, 0, 0).as_bool() {
                        let _ = TranslateMessage(&message);
                        DispatchMessageW(&message);
                    }
                }

                // Shutdown dispatcher queue
                let async_action = controller
                    .ShutdownQueueAsync()
                    .map_err(|_| GraphicsCaptureApiError::FailedToShutdownDispatcherQueue)?;

                async_action
                    .SetCompleted(&AsyncActionCompletedHandler::new(
                        move |_, _| -> Result<(), windows::core::Error> {
                            unsafe { PostQuitMessage(0) };
                            Ok(())
                        },
                    ))
                    .map_err(|_| {
                        GraphicsCaptureApiError::FailedToSetDispatcherQueueCompletedHandler
                    })?;

                // Final message loop
                let mut message = MSG::default();
                unsafe {
                    while GetMessageW(&mut message, None, 0, 0).as_bool() {
                        let _ = TranslateMessage(&message);
                        DispatchMessageW(&message);
                    }
                }

                // Stop capture
                capture.stop_capture();

                // Uninitialize WinRT
                unsafe { RoUninitialize() };

                // Check handler result
                if let Some(e) = result.lock().take() {
                    return Err(GraphicsCaptureApiError::FrameHandlerError(e));
                }

                Ok(())
            },
        );

        let Ok(halt_handle) = halt_receiver.recv() else {
            match thread_handle.join() {
                Ok(result) => return Err(result.err().unwrap()),
                Err(_) => {
                    return Err(GraphicsCaptureApiError::FailedToJoinThread);
                }
            }
        };

        let Ok(callback) = callback_receiver.recv() else {
            match thread_handle.join() {
                Ok(result) => return Err(result.err().unwrap()),
                Err(_) => {
                    return Err(GraphicsCaptureApiError::FailedToJoinThread);
                }
            }
        };

        Ok(CaptureControl::new(thread_handle, halt_handle, callback))
    }

    /// Function that will be called to create the struct. The flags can be passed from settings.
    ///
    /// # Arguments
    ///
    /// * `flags` - The flags used to create the struct.
    ///
    /// # Returns
    ///
    /// Returns `Ok(Self)` if the struct creation was successful, otherwise returns an error of type `Self::Error`.
    fn new(flags: Self::Flags) -> Result<Self, Self::Error>;

    /// Called every time a new frame is available.
    ///
    /// # Arguments
    ///
    /// * `frame` - A mutable reference to the captured frame.
    /// * `capture_control` - The internal capture control.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the frame processing was successful, otherwise returns an error of type `Self::Error`.
    fn on_frame_arrived(
        &mut self,
        frame: &mut Frame,
        capture_control: InternalCaptureControl,
    ) -> Result<(), Self::Error>;

    /// Optional handler called when the capture item (usually a window) closes.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the handler execution was successful, otherwise returns an error of type `Self::Error`.
    fn on_closed(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}