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
use std::sync::{
    atomic::{self, AtomicBool},
    Arc,
};

use parking_lot::Mutex;
use windows::{
    core::{IInspectable, Interface, HSTRING},
    Foundation::{EventRegistrationToken, Metadata::ApiInformation, TypedEventHandler},
    Graphics::{
        Capture::{Direct3D11CaptureFramePool, GraphicsCaptureItem, GraphicsCaptureSession},
        DirectX::{Direct3D11::IDirect3DDevice, DirectXPixelFormat},
    },
    Win32::{
        Foundation::{LPARAM, WPARAM},
        Graphics::Direct3D11::{
            ID3D11Device, ID3D11DeviceContext, ID3D11Texture2D, D3D11_TEXTURE2D_DESC,
        },
        System::WinRT::Direct3D11::IDirect3DDxgiInterfaceAccess,
        UI::WindowsAndMessaging::{PostThreadMessageW, WM_QUIT},
    },
};

use crate::{
    capture::GraphicsCaptureApiHandler,
    d3d11::{self, create_d3d_device, create_direct3d_device, SendDirectX},
    frame::Frame,
    settings::{ColorFormat, CursorCaptureSettings, DrawBorderSettings},
};

#[derive(thiserror::Error, Eq, PartialEq, Clone, Debug)]
pub enum Error {
    #[error("Graphics capture API is not supported")]
    Unsupported,
    #[error("Graphics capture API toggling cursor capture is not supported")]
    CursorConfigUnsupported,
    #[error("Graphics capture API toggling border capture is not supported")]
    BorderConfigUnsupported,
    #[error("Already started")]
    AlreadyStarted,
    #[error("DirectX error: {0}")]
    DirectXError(#[from] d3d11::Error),
    #[error("Windows API error: {0}")]
    WindowsError(#[from] windows::core::Error),
}

/// Used to control the capture session
pub struct InternalCaptureControl {
    stop: Arc<AtomicBool>,
}

impl InternalCaptureControl {
    /// Create a new `InternalCaptureControl` struct.
    ///
    /// # Arguments
    ///
    /// * `stop` - An `Arc<AtomicBool>` indicating whether the capture should stop.
    ///
    /// # Returns
    ///
    /// A new instance of `InternalCaptureControl`.
    #[must_use]
    pub fn new(stop: Arc<AtomicBool>) -> Self {
        Self { stop }
    }

    /// Gracefully stop the capture thread.
    pub fn stop(self) {
        self.stop.store(true, atomic::Ordering::Relaxed);
    }
}

/// Represents the GraphicsCaptureApi struct.
pub struct GraphicsCaptureApi {
    /// The GraphicsCaptureItem associated with the GraphicsCaptureApi.
    item: GraphicsCaptureItem,
    /// The ID3D11Device associated with the GraphicsCaptureApi.
    _d3d_device: ID3D11Device,
    /// The IDirect3DDevice associated with the GraphicsCaptureApi.
    _direct3d_device: IDirect3DDevice,
    /// The ID3D11DeviceContext associated with the GraphicsCaptureApi.
    _d3d_device_context: ID3D11DeviceContext,
    /// The optional Arc<Direct3D11CaptureFramePool> associated with the GraphicsCaptureApi.
    frame_pool: Option<Arc<Direct3D11CaptureFramePool>>,
    /// The optional GraphicsCaptureSession associated with the GraphicsCaptureApi.
    session: Option<GraphicsCaptureSession>,
    /// The Arc<AtomicBool> used to halt the GraphicsCaptureApi.
    halt: Arc<AtomicBool>,
    /// Indicates whether the GraphicsCaptureApi is active or not.
    active: bool,
    /// The EventRegistrationToken associated with the capture closed event.
    capture_closed_event_token: EventRegistrationToken,
    /// The EventRegistrationToken associated with the frame arrived event.
    frame_arrived_event_token: EventRegistrationToken,
}

impl GraphicsCaptureApi {
    /// Create a new Graphics Capture API struct.
    ///
    /// # Arguments
    ///
    /// * `item` - The graphics capture item to capture.
    /// * `callback` - The callback handler for capturing frames.
    /// * `capture_cursor` - Optional flag to capture the cursor.
    /// * `draw_border` - Optional flag to draw a border around the captured region.
    /// * `color_format` - The color format for the captured frames.
    /// * `thread_id` - The ID of the thread where the capture is running.
    /// * `result` - The result of the capture operation.
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the new `GraphicsCaptureApi` struct if successful, or an `Error` if an error occurred.
    pub fn new<
        T: GraphicsCaptureApiHandler<Error = E> + Send + 'static,
        E: Send + Sync + 'static,
    >(
        item: GraphicsCaptureItem,
        callback: Arc<Mutex<T>>,
        cursor_capture: CursorCaptureSettings,
        draw_border: DrawBorderSettings,
        color_format: ColorFormat,
        thread_id: u32,
        result: Arc<Mutex<Option<E>>>,
    ) -> Result<Self, Error> {
        // Check support
        if !Self::is_supported()? {
            return Err(Error::Unsupported);
        }

        if cursor_capture != CursorCaptureSettings::Default
            && !Self::is_cursor_settings_supported()?
        {
            return Err(Error::CursorConfigUnsupported);
        }

        if draw_border != DrawBorderSettings::Default && !Self::is_border_settings_supported()? {
            return Err(Error::BorderConfigUnsupported);
        }

        // Create DirectX devices
        let (d3d_device, d3d_device_context) = create_d3d_device()?;
        let direct3d_device = create_direct3d_device(&d3d_device)?;

        let pixel_format = DirectXPixelFormat(color_format as i32);

        // Create frame pool
        let frame_pool =
            Direct3D11CaptureFramePool::Create(&direct3d_device, pixel_format, 1, item.Size()?)?;
        let frame_pool = Arc::new(frame_pool);

        // Create capture session
        let session = frame_pool.CreateCaptureSession(&item)?;

        // Preallocate memory
        let mut buffer = vec![0u8; 3840 * 2160 * 4];

        // Indicates if the capture is closed
        let halt = Arc::new(AtomicBool::new(false));

        // Set capture session closed event
        let capture_closed_event_token = item.Closed(&TypedEventHandler::<
            GraphicsCaptureItem,
            IInspectable,
        >::new({
            // Init
            let callback_closed = callback.clone();
            let halt_closed = halt.clone();
            let result_closed = result.clone();

            move |_, _| {
                halt_closed.store(true, atomic::Ordering::Relaxed);

                // Notify the struct that the capture session is closed
                if let Err(e) = callback_closed.lock().on_closed() {
                    *result_closed.lock() = Some(e);
                }

                // To stop message loop
                unsafe {
                    PostThreadMessageW(thread_id, WM_QUIT, WPARAM::default(), LPARAM::default())?;
                };

                Result::Ok(())
            }
        }))?;

        // Set frame pool frame arrived event
        let frame_arrived_event_token = frame_pool.FrameArrived(&TypedEventHandler::<
            Direct3D11CaptureFramePool,
            IInspectable,
        >::new({
            // Init
            let frame_pool_recreate = frame_pool.clone();
            let halt_frame_pool = halt.clone();
            let d3d_device_frame_pool = d3d_device.clone();
            let context = d3d_device_context.clone();
            let result_frame_pool = result;

            let mut last_size = item.Size()?;
            let callback_frame_pool = callback;
            let direct3d_device_recreate = SendDirectX::new(direct3d_device.clone());

            move |frame, _| {
                // Return early if the capture is closed
                if halt_frame_pool.load(atomic::Ordering::Relaxed) {
                    return Ok(());
                }

                // Get frame
                let frame = frame
                    .as_ref()
                    .expect("FrameArrived parameter was None this should never happen.")
                    .TryGetNextFrame()?;
                let timespan = frame.SystemRelativeTime()?;

                // Get frame content size
                let frame_content_size = frame.ContentSize()?;

                // Get frame surface
                let frame_surface = frame.Surface()?;

                // Convert surface to texture
                let frame_dxgi_interface = frame_surface.cast::<IDirect3DDxgiInterfaceAccess>()?;
                let frame_texture =
                    unsafe { frame_dxgi_interface.GetInterface::<ID3D11Texture2D>()? };

                // Get texture settings
                let mut desc = D3D11_TEXTURE2D_DESC::default();
                unsafe { frame_texture.GetDesc(&mut desc) }

                // Check if the size has been changed
                if frame_content_size.Width != last_size.Width
                    || frame_content_size.Height != last_size.Height
                {
                    let direct3d_device_recreate = &direct3d_device_recreate;
                    frame_pool_recreate.Recreate(
                        &direct3d_device_recreate.0,
                        pixel_format,
                        1,
                        frame_content_size,
                    )?;

                    last_size = frame_content_size;

                    return Ok(());
                }

                // Set width & height
                let texture_width = desc.Width;
                let texture_height = desc.Height;

                // Create a frame
                let mut frame = Frame::new(
                    &d3d_device_frame_pool,
                    frame_surface,
                    frame_texture,
                    timespan,
                    &context,
                    &mut buffer,
                    texture_width,
                    texture_height,
                    color_format,
                );

                // Init internal capture control
                let stop = Arc::new(AtomicBool::new(false));
                let internal_capture_control = InternalCaptureControl::new(stop.clone());

                // Send the frame to the callback struct
                let result = callback_frame_pool
                    .lock()
                    .on_frame_arrived(&mut frame, internal_capture_control);

                if stop.load(atomic::Ordering::Relaxed) || result.is_err() {
                    if let Err(e) = result {
                        *result_frame_pool.lock() = Some(e);
                    }

                    halt_frame_pool.store(true, atomic::Ordering::Relaxed);

                    // To stop the message loop
                    unsafe {
                        PostThreadMessageW(
                            thread_id,
                            WM_QUIT,
                            WPARAM::default(),
                            LPARAM::default(),
                        )?;
                    };
                }

                Result::Ok(())
            }
        }))?;

        if cursor_capture != CursorCaptureSettings::Default {
            if Self::is_cursor_settings_supported()? {
                match cursor_capture {
                    CursorCaptureSettings::Default => (),
                    CursorCaptureSettings::WithCursor => session.SetIsCursorCaptureEnabled(true)?,
                    CursorCaptureSettings::WithoutCursor => {
                        session.SetIsCursorCaptureEnabled(false)?
                    }
                };
            } else {
                return Err(Error::CursorConfigUnsupported);
            }
        }

        if draw_border != DrawBorderSettings::Default {
            if Self::is_border_settings_supported()? {
                match draw_border {
                    DrawBorderSettings::Default => (),
                    DrawBorderSettings::WithBorder => {
                        session.SetIsBorderRequired(true)?;
                    }
                    DrawBorderSettings::WithoutBorder => session.SetIsBorderRequired(false)?,
                }
            } else {
                return Err(Error::BorderConfigUnsupported);
            }
        }

        Ok(Self {
            item,
            _d3d_device: d3d_device,
            _direct3d_device: direct3d_device,
            _d3d_device_context: d3d_device_context,
            frame_pool: Some(frame_pool),
            session: Some(session),
            halt,
            active: false,
            frame_arrived_event_token,
            capture_closed_event_token,
        })
    }

    /// Start the capture.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the capture started successfully, or an `Error` if an error occurred.
    pub fn start_capture(&mut self) -> Result<(), Error> {
        if self.active {
            return Err(Error::AlreadyStarted);
        }
        self.active = true;

        self.session.as_ref().unwrap().StartCapture()?;

        Ok(())
    }

    /// Stop the capture.
    pub fn stop_capture(mut self) {
        if let Some(frame_pool) = self.frame_pool.take() {
            frame_pool
                .RemoveFrameArrived(self.frame_arrived_event_token)
                .expect("Failed to remove Frame Arrived event handler");

            frame_pool.Close().expect("Failed to Close Frame Pool");
        }

        if let Some(session) = self.session.take() {
            session.Close().expect("Failed to Close Capture Session");
        }

        self.item
            .RemoveClosed(self.capture_closed_event_token)
            .expect("Failed to remove Capture Session Closed event handler");
    }

    /// Get the halt handle.
    ///
    /// # Returns
    ///
    /// Returns an `Arc<AtomicBool>` representing the halt handle.
    #[must_use]
    pub fn halt_handle(&self) -> Arc<AtomicBool> {
        self.halt.clone()
    }

    /// Check if the Windows Graphics Capture API is supported.
    ///
    /// # Returns
    ///
    /// Returns `Ok(true)` if the API is supported, `Ok(false)` if the API is not supported, or an `Error` if an error occurred.
    pub fn is_supported() -> Result<bool, Error> {
        Ok(ApiInformation::IsApiContractPresentByMajor(
            &HSTRING::from("Windows.Foundation.UniversalApiContract"),
            8,
        )? && GraphicsCaptureSession::IsSupported()?)
    }

    /// Check if you can change the cursor capture setting.
    ///
    /// # Returns
    ///
    /// Returns `true` if toggling the cursor capture is supported, `false` otherwise.
    pub fn is_cursor_settings_supported() -> Result<bool, Error> {
        Ok(ApiInformation::IsPropertyPresent(
            &HSTRING::from("Windows.Graphics.Capture.GraphicsCaptureSession"),
            &HSTRING::from("IsCursorCaptureEnabled"),
        )? && Self::is_supported()?)
    }

    /// Check if you can change the border capture setting.
    ///
    /// # Returns
    ///
    /// Returns `true` if toggling the border capture is supported, `false` otherwise.
    pub fn is_border_settings_supported() -> Result<bool, Error> {
        Ok(ApiInformation::IsPropertyPresent(
            &HSTRING::from("Windows.Graphics.Capture.GraphicsCaptureSession"),
            &HSTRING::from("IsBorderRequired"),
        )? && Self::is_supported()?)
    }
}

impl Drop for GraphicsCaptureApi {
    fn drop(&mut self) {
        if let Some(frame_pool) = self.frame_pool.take() {
            frame_pool
                .RemoveFrameArrived(self.frame_arrived_event_token)
                .expect("Failed to remove Frame Arrived event handler");

            frame_pool.Close().expect("Failed to Close Frame Pool");
        }

        if let Some(session) = self.session.take() {
            session.Close().expect("Failed to Close Capture Session");
        }

        self.item
            .RemoveClosed(self.capture_closed_event_token)
            .expect("Failed to remove Capture Session Closed event handler");
    }
}