screencapturekit 9.0.1

Safe Rust bindings for Apple's ScreenCaptureKit framework - screen and audio capture on macOS
Documentation
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! `IOSurface` and pixel buffer tests

#![allow(clippy::unreadable_literal)]
#![allow(clippy::items_after_statements)]
#![allow(clippy::similar_names)]

use screencapturekit::cm::{IOSurface, IOSurfaceLockOptions};
use screencapturekit::cv::CVPixelBufferLockFlags;

#[test]
fn test_iosurface_lock_options_values() {
    assert_eq!(IOSurfaceLockOptions::READ_ONLY.as_u32(), 0x0000_0001);
    assert_eq!(IOSurfaceLockOptions::AVOID_SYNC.as_u32(), 0x0000_0002);
}

#[test]
fn test_iosurface_lock_options_combined() {
    let combined = IOSurfaceLockOptions::READ_ONLY | IOSurfaceLockOptions::AVOID_SYNC;
    assert_eq!(combined.as_u32(), 0x0000_0003);
    assert!(combined.contains(IOSurfaceLockOptions::READ_ONLY));
    assert!(combined.contains(IOSurfaceLockOptions::AVOID_SYNC));
    assert!(combined.is_read_only());
}

#[test]
fn test_iosurface_lock_options_debug() {
    let opt = IOSurfaceLockOptions::READ_ONLY;
    let debug_str = format!("{opt:?}");
    assert!(debug_str.contains("IOSurfaceLockOptions"));
}

#[test]
fn test_cvpixelbuffer_lock_flags_values() {
    assert_eq!(CVPixelBufferLockFlags::READ_ONLY.as_u32(), 0x0000_0001);
    assert_eq!(CVPixelBufferLockFlags::NONE.as_u32(), 0x0000_0000);
    assert!(CVPixelBufferLockFlags::READ_ONLY.is_read_only());
    assert!(!CVPixelBufferLockFlags::NONE.is_read_only());
}

#[test]
fn test_iosurface_send_sync() {
    fn assert_send<T: Send>() {}
    fn assert_sync<T: Sync>() {}
    assert_send::<IOSurface>();
    assert_sync::<IOSurface>();
}

#[test]
fn test_iosurface_create() {
    let surface = IOSurface::create(100, 100, 0x42475241, 4).expect("Failed to create IOSurface");

    assert_eq!(surface.width(), 100);
    assert_eq!(surface.height(), 100);
    assert!(surface.bytes_per_row() >= 400); // At least 100 * 4
    assert_eq!(surface.pixel_format(), 0x42475241);
}

#[test]
fn test_iosurface_plane_count() {
    let surface = IOSurface::create(100, 100, 0x42475241, 4).expect("Failed to create IOSurface");

    // BGRA is single-plane
    assert_eq!(surface.plane_count(), 0);
}

#[test]
fn test_iosurface_is_in_use() {
    let surface = IOSurface::create(50, 50, 0x42475241, 4).expect("Failed to create IOSurface");
    // Initially not in use
    let _ = surface.is_in_use();
}

#[test]
fn test_iosurface_lock_and_access() {
    let surface = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");

    let guard = surface
        .lock(IOSurfaceLockOptions::READ_ONLY)
        .expect("Failed to lock IOSurface");

    assert_eq!(guard.width(), 10);
    assert_eq!(guard.height(), 10);
    assert!(guard.bytes_per_row() >= 40);

    // Test as_slice
    let slice = guard.as_slice();
    assert!(!slice.is_empty());
}

#[test]
fn test_iosurface_lock_guard_row() {
    let surface = IOSurface::create(20, 20, 0x42475241, 4).expect("Failed to create IOSurface");

    let guard = surface
        .lock(IOSurfaceLockOptions::READ_ONLY)
        .expect("Failed to lock IOSurface");

    // Valid row
    let row0 = guard.row(0);
    assert!(row0.is_some());
    assert_eq!(row0.unwrap().len(), guard.bytes_per_row());

    let row19 = guard.row(19);
    assert!(row19.is_some());

    // Invalid row
    let row20 = guard.row(20);
    assert!(row20.is_none());
}

#[test]
fn test_iosurface_lock_guard_cursor() {
    use std::io::Read;

    let surface = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");

    let guard = surface
        .lock(IOSurfaceLockOptions::READ_ONLY)
        .expect("Failed to lock IOSurface");

    let mut cursor = guard.cursor();
    let mut buf = [0u8; 4];
    let result = cursor.read_exact(&mut buf);
    assert!(result.is_ok());
}

#[test]
fn test_iosurface_lock_guard_debug() {
    let surface = IOSurface::create(50, 50, 0x42475241, 4).expect("Failed to create IOSurface");

    let guard = surface
        .lock(IOSurfaceLockOptions::READ_ONLY)
        .expect("Failed to lock IOSurface");

    let debug_str = format!("{guard:?}");
    assert!(debug_str.contains("IOSurfaceLockGuard"));
    assert!(debug_str.contains("options"));
    assert!(debug_str.contains("surface_size"));
}

#[test]
fn test_iosurface_debug() {
    let surface = IOSurface::create(100, 100, 0x42475241, 4).expect("Failed to create IOSurface");

    let debug_str = format!("{surface:?}");
    assert!(debug_str.contains("IOSurface"));
    assert!(debug_str.contains("width"));
    assert!(debug_str.contains("height"));
}

#[test]
fn test_iosurface_as_ptr() {
    let surface = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");
    let ptr = surface.as_ptr();
    assert!(!ptr.is_null());
}

#[test]
fn test_pixel_buffer_create_and_lock() {
    use screencapturekit::cv::CVPixelBuffer;

    // Create a test pixel buffer (BGRA format)
    let buffer =
        CVPixelBuffer::create(100, 100, 0x42475241).expect("Failed to create pixel buffer");

    assert_eq!(buffer.width(), 100);
    assert_eq!(buffer.height(), 100);

    // Lock for reading
    let guard = buffer.lock(CVPixelBufferLockFlags::READ_ONLY);
    assert!(guard.is_ok());

    let guard = guard.unwrap();
    assert_eq!(guard.width(), 100);
    assert_eq!(guard.height(), 100);
    assert!(guard.bytes_per_row() >= 400); // At least 100 * 4 bytes

    // Test as_slice
    let slice = guard.as_slice();
    assert!(!slice.is_empty());

    // Test Deref trait
    let _: &[u8] = &guard;
}

#[test]
fn test_pixel_buffer_lock_guard_row_access() {
    use screencapturekit::cv::CVPixelBuffer;

    let buffer = CVPixelBuffer::create(50, 50, 0x42475241).expect("Failed to create pixel buffer");

    let guard = buffer
        .lock(CVPixelBufferLockFlags::READ_ONLY)
        .expect("Failed to lock buffer");

    // Valid row access
    let row0 = guard.row(0);
    assert!(row0.is_some());
    assert_eq!(row0.unwrap().len(), guard.bytes_per_row());

    let row49 = guard.row(49);
    assert!(row49.is_some());

    // Invalid row access
    let row50 = guard.row(50);
    assert!(row50.is_none());

    let row_overflow = guard.row(1000);
    assert!(row_overflow.is_none());
}

#[test]
fn test_pixel_buffer_lock_guard_cursor() {
    use screencapturekit::cv::CVPixelBuffer;
    use std::io::Read;

    let buffer = CVPixelBuffer::create(10, 10, 0x42475241).expect("Failed to create pixel buffer");

    let guard = buffer
        .lock(CVPixelBufferLockFlags::READ_ONLY)
        .expect("Failed to lock buffer");

    let mut cursor = guard.cursor();
    let mut buf = [0u8; 4];
    let result = cursor.read_exact(&mut buf);
    assert!(result.is_ok());
}

#[test]
fn test_pixel_buffer_lock_guard_debug() {
    use screencapturekit::cv::CVPixelBuffer;

    let buffer =
        CVPixelBuffer::create(100, 100, 0x42475241).expect("Failed to create pixel buffer");

    let guard = buffer
        .lock(CVPixelBufferLockFlags::READ_ONLY)
        .expect("Failed to lock buffer");

    let debug_str = format!("{guard:?}");
    assert!(debug_str.contains("CVPixelBufferLockGuard"));
    assert!(debug_str.contains("flags"));
    assert!(debug_str.contains("buffer_size"));
}

#[test]
fn test_pixel_buffer_iosurface_trait() {
    use screencapturekit::cv::CVPixelBuffer;

    let buffer =
        CVPixelBuffer::create(100, 100, 0x42475241).expect("Failed to create pixel buffer");

    // Simple pixel buffers might not be backed by IOSurface
    let is_backed = buffer.is_backed_by_io_surface();
    // Just test that the method works, result depends on implementation
    let _ = is_backed;

    let iosurface = buffer.io_surface();
    // iosurface may or may not be available depending on buffer creation
    let _ = iosurface;
}

#[test]
fn test_pixel_buffer_planar_methods() {
    use screencapturekit::cv::CVPixelBuffer;

    let buffer =
        CVPixelBuffer::create(100, 100, 0x42475241).expect("Failed to create pixel buffer");

    let guard = buffer
        .lock(CVPixelBufferLockFlags::READ_ONLY)
        .expect("Failed to lock buffer");

    // BGRA is not planar, so plane_count should be 0
    let plane_count = guard.plane_count();
    assert_eq!(plane_count, 0);
}

#[test]
fn test_pixel_buffer_cursor_extension_trait() {
    use screencapturekit::cv::CVPixelBuffer;
    use screencapturekit::cv::PixelBufferCursorExt;
    use std::io::{Seek, SeekFrom};

    let buffer = CVPixelBuffer::create(10, 10, 0x42475241).expect("Failed to create pixel buffer");

    let guard = buffer
        .lock(CVPixelBufferLockFlags::READ_ONLY)
        .expect("Failed to lock buffer");

    let mut cursor = guard.cursor();

    // Test seek_to_pixel
    let bytes_per_row = guard.bytes_per_row();
    let pos = cursor.seek_to_pixel(0, 0, bytes_per_row);
    assert!(pos.is_ok());
    assert_eq!(pos.unwrap(), 0);

    // Test read_pixel
    cursor.seek(SeekFrom::Start(0)).unwrap();
    let pixel = cursor.read_pixel();
    assert!(pixel.is_ok());
    let pixel = pixel.unwrap();
    assert_eq!(pixel.len(), 4);
}

#[test]
fn test_iosurface_lock_guard_as_ptr() {
    let surface = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");

    let guard = surface
        .lock(IOSurfaceLockOptions::READ_ONLY)
        .expect("Failed to lock IOSurface");

    let ptr = guard.as_ptr();
    assert!(!ptr.is_null());
}

#[test]
fn test_iosurface_equality() {
    let surface1 = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");
    let surface2 = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");

    // Different surfaces should not be equal (different pointers)
    assert_ne!(surface1, surface2);

    // Same surface compared to itself
    assert_eq!(surface1, surface1);
}

#[test]
fn test_iosurface_hash() {
    use std::collections::HashSet;

    let surface1 = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");
    let surface2 = IOSurface::create(10, 10, 0x42475241, 4).expect("Failed to create IOSurface");

    let mut set = HashSet::new();
    set.insert(&surface1);

    assert!(set.contains(&surface1));
    assert!(!set.contains(&surface2));
}

// ============================================================================
// Multi-planar IOSurface tests (YCbCr, L10R)
// ============================================================================

mod multiplanar_tests {
    use screencapturekit::cm::IOSurface;
    use screencapturekit::cm::PlaneProperties;
    use screencapturekit::metal::IOSurfaceMetalExt;

    /// Create a YCbCr 4:2:0 biplanar surface (like 420v or 420f)
    fn create_ycbcr_420(width: usize, height: usize, full_range: bool) -> Option<IOSurface> {
        // 420v = 0x34323076, 420f = 0x34323066
        let pixel_format: u32 = if full_range { 0x34323066 } else { 0x34323076 };

        // Align dimensions to even for 4:2:0
        let aligned_width = (width + 1) & !1;
        let aligned_height = (height + 1) & !1;

        // Plane 0: Y (luminance) - full resolution, 1 byte per pixel
        let plane0_width = aligned_width;
        let plane0_height = aligned_height;
        let plane0_bpr = (plane0_width + 15) & !15; // 16-byte aligned
        let plane0_size = plane0_bpr * plane0_height;

        // Plane 1: CbCr (chrominance) - half resolution, 2 bytes per pixel
        let plane1_width = aligned_width / 2;
        let plane1_height = aligned_height / 2;
        let plane1_bpr = (plane1_width * 2 + 15) & !15; // 16-byte aligned
        let plane1_size = plane1_bpr * plane1_height;

        let total_size = plane0_size + plane1_size;

        let planes = [
            PlaneProperties {
                width: plane0_width,
                height: plane0_height,
                bytes_per_row: plane0_bpr,
                bytes_per_element: 1,
                offset: 0,
                size: plane0_size,
            },
            PlaneProperties {
                width: plane1_width,
                height: plane1_height,
                bytes_per_row: plane1_bpr,
                bytes_per_element: 2,
                offset: plane0_size,
                size: plane1_size,
            },
        ];

        IOSurface::create_with_properties(
            aligned_width,
            aligned_height,
            pixel_format,
            1, // bytes_per_element for plane 0
            plane0_bpr,
            total_size,
            Some(&planes),
        )
    }

    #[test]
    fn test_ycbcr_420v_creation() {
        let surface = create_ycbcr_420(1920, 1080, false).expect("Failed to create 420v surface");

        assert_eq!(surface.width(), 1920);
        assert_eq!(surface.height(), 1080);
        assert_eq!(surface.pixel_format(), 0x34323076); // '420v'
        assert_eq!(surface.plane_count(), 2);
    }

    #[test]
    fn test_ycbcr_420f_creation() {
        let surface = create_ycbcr_420(1920, 1080, true).expect("Failed to create 420f surface");

        assert_eq!(surface.pixel_format(), 0x34323066); // '420f'
        assert_eq!(surface.plane_count(), 2);
    }

    #[test]
    fn test_ycbcr_plane_dimensions() {
        let surface = create_ycbcr_420(1920, 1080, false).expect("Failed to create 420v surface");

        // Plane 0: Y - full resolution
        assert_eq!(surface.width_of_plane(0), 1920);
        assert_eq!(surface.height_of_plane(0), 1080);

        // Plane 1: CbCr - half resolution
        assert_eq!(surface.width_of_plane(1), 960);
        assert_eq!(surface.height_of_plane(1), 540);
    }

    #[test]
    fn test_ycbcr_bytes_per_row() {
        let surface = create_ycbcr_420(1920, 1080, false).expect("Failed to create 420v surface");

        // Plane 0: Y - 1 byte per pixel, 16-byte aligned
        let plane0_bpr = surface.bytes_per_row_of_plane(0);
        assert!(plane0_bpr >= 1920);
        assert_eq!(plane0_bpr % 16, 0);

        // Plane 1: CbCr - 2 bytes per pixel, 16-byte aligned
        let plane1_bpr = surface.bytes_per_row_of_plane(1);
        assert!(plane1_bpr >= 960 * 2);
        assert_eq!(plane1_bpr % 16, 0);
    }

    #[test]
    fn test_ycbcr_is_biplanar() {
        let surface = create_ycbcr_420(640, 480, false).expect("Failed to create surface");

        // The is_ycbcr_biplanar check uses pixel_format
        use screencapturekit::metal::pixel_format;
        assert!(pixel_format::is_ycbcr_biplanar(surface.pixel_format()));
    }

    #[test]
    fn test_ycbcr_texture_params() {
        let surface = create_ycbcr_420(1920, 1080, false).expect("Failed to create surface");

        let params = surface.texture_params();
        assert_eq!(params.len(), 2);

        // Plane 0: Y
        assert_eq!(params[0].width, 1920);
        assert_eq!(params[0].height, 1080);
        assert_eq!(params[0].plane, 0);

        // Plane 1: CbCr
        assert_eq!(params[1].width, 960);
        assert_eq!(params[1].height, 540);
        assert_eq!(params[1].plane, 1);
    }

    #[test]
    fn test_ycbcr_info() {
        let surface = create_ycbcr_420(640, 480, false).expect("Failed to create surface");

        let info = surface.info();
        assert_eq!(info.width, 640);
        assert_eq!(info.height, 480);
        assert_eq!(info.plane_count, 2);
        assert_eq!(info.planes.len(), 2);

        // Plane 0
        assert_eq!(info.planes[0].index, 0);
        assert_eq!(info.planes[0].width, 640);
        assert_eq!(info.planes[0].height, 480);

        // Plane 1
        assert_eq!(info.planes[1].index, 1);
        assert_eq!(info.planes[1].width, 320);
        assert_eq!(info.planes[1].height, 240);
    }

    #[test]
    fn test_l10r_creation() {
        // L10R = 'l10r' = 0x6c313072, 4 bytes per pixel (10+10+10+2 bits)
        let width = 1920usize;
        let height = 1080usize;
        let bytes_per_element = 4usize;
        let bytes_per_row = (width * bytes_per_element + 15) & !15;
        let alloc_size = bytes_per_row * height;

        let surface = IOSurface::create_with_properties(
            width,
            height,
            0x6c313072, // 'l10r'
            bytes_per_element,
            bytes_per_row,
            alloc_size,
            None,
        )
        .expect("Failed to create L10R surface");

        assert_eq!(surface.width(), 1920);
        assert_eq!(surface.height(), 1080);
        assert_eq!(surface.pixel_format(), 0x6c313072);
        assert_eq!(surface.plane_count(), 0); // Single-plane
    }

    #[test]
    fn test_l10r_texture_params() {
        let width = 1920usize;
        let height = 1080usize;
        let bytes_per_element = 4usize;
        let bytes_per_row = (width * bytes_per_element + 15) & !15;
        let alloc_size = bytes_per_row * height;

        let surface = IOSurface::create_with_properties(
            width,
            height,
            0x6c313072,
            bytes_per_element,
            bytes_per_row,
            alloc_size,
            None,
        )
        .expect("Failed to create L10R surface");

        let params = surface.texture_params();
        assert_eq!(params.len(), 1);
        assert_eq!(params[0].width, 1920);
        assert_eq!(params[0].height, 1080);

        // L10R should use BGR10A2Unorm
        use screencapturekit::metal::MetalPixelFormat;
        assert_eq!(params[0].format, MetalPixelFormat::BGR10A2Unorm);
    }

    #[test]
    fn test_l10r_not_ycbcr() {
        use screencapturekit::metal::pixel_format;
        assert!(!pixel_format::is_ycbcr_biplanar(0x6c313072));
    }

    #[test]
    fn test_plane_properties_debug() {
        let props = PlaneProperties {
            width: 1920,
            height: 1080,
            bytes_per_row: 1920,
            bytes_per_element: 1,
            offset: 0,
            size: 1920 * 1080,
        };

        let debug_str = format!("{props:?}");
        assert!(debug_str.contains("PlaneProperties"));
        assert!(debug_str.contains("width"));
    }

    #[test]
    fn test_plane_properties_clone_eq() {
        let props = PlaneProperties {
            width: 100,
            height: 100,
            bytes_per_row: 128,
            bytes_per_element: 1,
            offset: 0,
            size: 12800,
        };

        let cloned = props;
        assert_eq!(props, cloned);
    }
}