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
use std::{
    error::Error,
    mem,
    os::windows::prelude::AsRawHandle,
    sync::{
        atomic::{self, AtomicBool},
        mpsc, Arc,
    },
    thread::{self, JoinHandle},
};

use log::{debug, info, trace, warn};
use parking_lot::Mutex;
use windows::{
    Foundation::AsyncActionCompletedHandler,
    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::{GraphicsCaptureApi, InternalCaptureControl, RESULT},
    settings::WindowsCaptureSettings,
};

/// Used To Handle Capture Control Errors
#[derive(thiserror::Error, Eq, PartialEq, Clone, Copy, Debug)]
pub enum CaptureControlError {
    #[error("Failed To Join Thread")]
    FailedToJoinThread,
    #[error("Thread Handle Is Taken Out Of Struct")]
    ThreadHandleIsTaken,
}

/// Struct Used To Control Capture Thread
pub struct CaptureControl<T: WindowsCaptureHandler + Send + 'static> {
    thread_handle: Option<JoinHandle<Result<(), Box<dyn Error + Send + Sync>>>>,
    halt_handle: Arc<AtomicBool>,
    callback: Arc<Mutex<T>>,
}

impl<T: WindowsCaptureHandler + Send + 'static> CaptureControl<T> {
    /// Create A New Capture Control Struct
    #[must_use]
    pub fn new(
        thread_handle: JoinHandle<Result<(), Box<dyn Error + Send + Sync>>>,
        halt_handle: Arc<AtomicBool>,
        callback: Arc<Mutex<T>>,
    ) -> Self {
        Self {
            thread_handle: Some(thread_handle),
            halt_handle,
            callback,
        }
    }

    /// Check To See If Capture Thread Is Finished
    #[must_use]
    pub fn is_finished(&self) -> bool {
        self.thread_handle
            .as_ref()
            .map_or(true, |thread_handle| thread_handle.is_finished())
    }

    /// Get The Halt Handle Used To Pause The Capture Thread
    #[must_use]
    pub fn into_thread_handle(self) -> JoinHandle<Result<(), Box<dyn Error + Send + Sync>>> {
        self.thread_handle.unwrap()
    }

    /// Get The Halt Handle Used To Pause The Capture Thread
    #[must_use]
    pub fn halt_handle(&self) -> Arc<AtomicBool> {
        self.halt_handle.clone()
    }

    /// Get The Callback Struct Used To Call Struct Methods Directly
    #[must_use]
    pub fn callback(&self) -> Arc<Mutex<T>> {
        self.callback.clone()
    }

    /// Wait Until The Capturing Thread Stops
    pub fn wait(mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
        if let Some(thread_handle) = self.thread_handle.take() {
            match thread_handle.join() {
                Ok(result) => result?,
                Err(_) => {
                    return Err(Box::new(CaptureControlError::FailedToJoinThread));
                }
            }
        } else {
            return Err(Box::new(CaptureControlError::ThreadHandleIsTaken));
        }

        Ok(())
    }

    /// Gracefully Stop The Capture Thread
    pub fn stop(mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
        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 == -2147023452 {
                            warn!("Thread Is Not In Message Loop Yet");
                        } else {
                            Err(e)?;
                        }
                    }
                }
            }

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

        Ok(())
    }
}

/// Event Handler Trait
pub trait WindowsCaptureHandler: Sized {
    /// To Get The Message From The Settings
    type Flags;

    /// Starts The Capture And Takes Control Of The Current Thread
    fn start(
        settings: WindowsCaptureSettings<Self::Flags>,
    ) -> Result<(), Box<dyn Error + Send + Sync>>
    where
        Self: Send + 'static,
        <Self as WindowsCaptureHandler>::Flags: Send,
    {
        // Initialize WinRT
        trace!("Initializing WinRT");
        unsafe { RoInitialize(RO_INIT_MULTITHREADED)? };

        // Create A Dispatcher Queue For Current Thread
        trace!("Creating A Dispatcher Queue For Capture Thread");
        let options = DispatcherQueueOptions {
            dwSize: mem::size_of::<DispatcherQueueOptions>() as u32,
            threadType: DQTYPE_THREAD_CURRENT,
            apartmentType: DQTAT_COM_NONE,
        };
        let controller = unsafe { CreateDispatcherQueueController(options)? };

        // Debug Thread ID
        let thread_id = unsafe { GetCurrentThreadId() };
        debug!("Thread ID: {thread_id}");

        // Start Capture
        info!("Starting Capture Thread");
        let callback = Arc::new(Mutex::new(Self::new(settings.flags)?));
        let mut capture = GraphicsCaptureApi::new(
            settings.item,
            callback,
            settings.capture_cursor,
            settings.draw_border,
            settings.color_format,
            thread_id,
        )?;
        capture.start_capture()?;

        // Message Loop
        trace!("Entering Message Loop");
        let mut message = MSG::default();
        unsafe {
            while GetMessageW(&mut message, None, 0, 0).as_bool() {
                TranslateMessage(&message);
                DispatchMessageW(&message);
            }
        }

        // Shutdown Dispatcher Queue
        trace!("Shutting Down Dispatcher Queue");
        let async_action = controller.ShutdownQueueAsync()?;
        async_action.SetCompleted(&AsyncActionCompletedHandler::new(
            move |_, _| -> Result<(), windows::core::Error> {
                unsafe { PostQuitMessage(0) };
                Ok(())
            },
        ))?;

        // Final Message Loop
        trace!("Entering Final Message Loop");
        let mut message = MSG::default();
        unsafe {
            while GetMessageW(&mut message, None, 0, 0).as_bool() {
                TranslateMessage(&message);
                DispatchMessageW(&message);
            }
        }

        // Stop Capturing
        info!("Stopping Capture Thread");
        capture.stop_capture();

        // Uninitialize WinRT
        trace!("Uninitializing WinRT");
        unsafe { RoUninitialize() };

        // Check RESULT
        trace!("Checking RESULT");
        let result = RESULT.take().expect("Failed To Take RESULT");

        result?;

        Ok(())
    }

    /// Starts The Capture Without Taking Control Of The Current Thread
    fn start_free_threaded(
        settings: WindowsCaptureSettings<Self::Flags>,
    ) -> Result<CaptureControl<Self>, Box<dyn Error + Send + Sync>>
    where
        Self: Send + 'static,
        <Self as WindowsCaptureHandler>::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<(), Box<dyn Error + Send + Sync>> {
            // Initialize WinRT
            trace!("Initializing WinRT");
            unsafe { RoInitialize(RO_INIT_MULTITHREADED)? };

            // Create A Dispatcher Queue For Current Thread
            trace!("Creating A Dispatcher Queue For Capture Thread");
            let options = DispatcherQueueOptions {
                dwSize: mem::size_of::<DispatcherQueueOptions>() as u32,
                threadType: DQTYPE_THREAD_CURRENT,
                apartmentType: DQTAT_COM_NONE,
            };
            let controller = unsafe { CreateDispatcherQueueController(options)? };

            // Debug Thread ID
            let thread_id = unsafe { GetCurrentThreadId() };
            debug!("Thread ID: {thread_id}");

            // Start Capture
            info!("Starting Capture Thread");
            let callback = Arc::new(Mutex::new(Self::new(settings.flags)?));
            let mut capture = GraphicsCaptureApi::new(
                settings.item,
                callback.clone(),
                settings.capture_cursor,
                settings.draw_border,
                settings.color_format,
                thread_id,
            )?;
            capture.start_capture()?;

            // Send Halt Handle
            trace!("Sending Halt Handle");
            let halt_handle = capture.halt_handle();
            halt_sender.send(halt_handle)?;

            // Send Callback
            trace!("Sending Callback");
            callback_sender.send(callback)?;

            // Message Loop
            trace!("Entering Message Loop");
            let mut message = MSG::default();
            unsafe {
                while GetMessageW(&mut message, None, 0, 0).as_bool() {
                    TranslateMessage(&message);
                    DispatchMessageW(&message);
                }
            }

            // Shutdown Dispatcher Queue
            trace!("Shutting Down Dispatcher Queue");
            let async_action = controller.ShutdownQueueAsync()?;
            async_action.SetCompleted(&AsyncActionCompletedHandler::new(
                move |_, _| -> Result<(), windows::core::Error> {
                    unsafe { PostQuitMessage(0) };
                    Ok(())
                },
            ))?;

            // Final Message Loop
            trace!("Entering Final Message Loop");
            let mut message = MSG::default();
            unsafe {
                while GetMessageW(&mut message, None, 0, 0).as_bool() {
                    TranslateMessage(&message);
                    DispatchMessageW(&message);
                }
            }

            // Stop Capturing
            info!("Stopping Capture Thread");
            capture.stop_capture();

            // Uninitialize WinRT
            trace!("Uninitializing WinRT");
            unsafe { RoUninitialize() };

            // Check RESULT
            trace!("Checking RESULT");
            let result = RESULT.take().expect("Failed To Take RESULT");

            result?;

            Ok(())
        });

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

        let callback = match callback_receiver.recv() {
            Ok(callback_handle) => callback_handle,
            Err(_) => match thread_handle.join() {
                Ok(result) => return Err(result.err().unwrap()),
                Err(_) => {
                    return Err(Box::new(CaptureControlError::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
    fn new(flags: Self::Flags) -> Result<Self, Box<dyn Error + Send + Sync>>;

    /// Called Every Time A New Frame Is Available
    fn on_frame_arrived(
        &mut self,
        frame: &mut Frame,
        capture_control: InternalCaptureControl,
    ) -> Result<(), Box<dyn Error + Send + Sync>>;

    /// Called When The Capture Item Closes Usually When The Window Closes,
    /// Capture Session Will End After This Function Ends
    fn on_closed(&mut self) -> Result<(), Box<dyn Error + Send + Sync>>;
}