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
//! Stream configuration builder pattern tests
//!
//! Comprehensive tests for the `SCStreamConfiguration` builder pattern

use screencapturekit::cg::CGRect;
use screencapturekit::cm::CMTime;
use screencapturekit::stream::configuration::{
    color_matrix, color_space, PixelFormat, SCStreamConfiguration, MAX_QUEUE_DEPTH, MIN_QUEUE_DEPTH,
};

// MARK: - Builder Pattern Tests

#[test]
fn test_builder_new() {
    let config = SCStreamConfiguration::new();
    // Should have some default width/height
    let _ = config.width();
    let _ = config.height();
}

#[test]
fn test_builder_with_width() {
    let config = SCStreamConfiguration::new().with_width(1920);
    assert_eq!(config.width(), 1920);
}

#[test]
fn test_builder_with_height() {
    let config = SCStreamConfiguration::new().with_height(1080);
    assert_eq!(config.height(), 1080);
}

#[test]
fn test_builder_chaining() {
    let config = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080)
        .with_pixel_format(PixelFormat::BGRA)
        .with_shows_cursor(true)
        .with_captures_audio(false);

    assert_eq!(config.width(), 1920);
    assert_eq!(config.height(), 1080);
    assert!(config.shows_cursor());
}

#[test]
fn test_builder_with_queue_depth() {
    let config = SCStreamConfiguration::new().with_queue_depth(8);
    assert_eq!(config.queue_depth(), 8);
}

#[test]
fn test_builder_with_minimum_frame_interval() {
    let frame_interval = CMTime::new(1, 60); // 60 FPS
    let config = SCStreamConfiguration::new().with_minimum_frame_interval(&frame_interval);

    let result = config.minimum_frame_interval();
    if result.is_valid() {
        assert_eq!(result.value, 1);
        assert_eq!(result.timescale, 60);
    }
}

#[test]
fn test_builder_with_fps() {
    let config = SCStreamConfiguration::new().with_fps(60);
    // FPS is derived from minimum_frame_interval
    let fps = config.fps();
    // Note: May be 0 on some macOS versions
    println!("FPS: {fps}");
}

#[test]
fn test_builder_with_scales_to_fit() {
    let config = SCStreamConfiguration::new().with_scales_to_fit(true);
    assert!(config.scales_to_fit());

    let config2 = SCStreamConfiguration::new().with_scales_to_fit(false);
    assert!(!config2.scales_to_fit());
}

#[test]
fn test_builder_with_source_rect() {
    let rect = CGRect::new(0.0, 0.0, 1920.0, 1080.0);
    let config = SCStreamConfiguration::new().with_source_rect(rect);

    let result = config.source_rect();
    // Note: source_rect might return default on some macOS versions
    println!("Source rect: {result:?}");
}

#[test]
fn test_builder_with_destination_rect() {
    let rect = CGRect::new(0.0, 0.0, 960.0, 540.0);
    let config = SCStreamConfiguration::new().with_destination_rect(rect);

    let result = config.destination_rect();
    println!("Destination rect: {result:?}");
}

#[test]
fn test_builder_with_background_color() {
    let config = SCStreamConfiguration::new().with_background_color(1.0, 0.0, 0.0);
    assert_eq!(config.background_color(), Some((1.0, 0.0, 0.0, 1.0)));
}

#[test]
fn test_builder_with_background_color_rgba() {
    let config = SCStreamConfiguration::new().with_background_color_rgba(0.25, 0.5, 0.75, 0.125);
    assert_eq!(config.background_color(), Some((0.25, 0.5, 0.75, 0.125)));
}

#[test]
#[cfg(feature = "macos_14_0")]
fn test_builder_with_stream_name() {
    let config = SCStreamConfiguration::new().with_stream_name(Some("TestStream"));
    let name = config.stream_name();
    // Stream name may not be retrievable on all macOS versions
    println!("Stream name: {name:?}");
}

#[test]
fn test_builder_audio_configuration() {
    let config = SCStreamConfiguration::new()
        .with_captures_audio(true)
        .with_sample_rate(48000)
        .with_channel_count(2);

    assert!(config.captures_audio());
    // Note: sample_rate and channel_count getters may return defaults
}

// MARK: - Multiple Format Tests

#[test]
fn test_all_pixel_formats() {
    let formats = [
        PixelFormat::BGRA,
        PixelFormat::l10r,
        PixelFormat::YCbCr_420v,
        PixelFormat::YCbCr_420f,
    ];

    for format in formats {
        let config = SCStreamConfiguration::new().with_pixel_format(format);
        // Just verify no crash
        let _ = config.width();
    }
}

#[test]
fn test_pixel_format_display() {
    let format = PixelFormat::BGRA;
    let display = format!("{format}");
    assert!(!display.is_empty());

    let format = PixelFormat::YCbCr_420v;
    let display = format!("{format}");
    assert!(!display.is_empty());
}

// MARK: - Edge Cases

#[test]
fn test_zero_dimensions() {
    let config = SCStreamConfiguration::new().with_width(0).with_height(0);
    assert_eq!(config.width(), 0);
    assert_eq!(config.height(), 0);
}

#[test]
fn test_large_dimensions() {
    let config = SCStreamConfiguration::new()
        .with_width(7680) // 8K
        .with_height(4320);
    assert_eq!(config.width(), 7680);
    assert_eq!(config.height(), 4320);
}

#[test]
fn test_odd_dimensions() {
    // Non-standard dimensions
    let config = SCStreamConfiguration::new()
        .with_width(1921)
        .with_height(1081);
    assert_eq!(config.width(), 1921);
    assert_eq!(config.height(), 1081);
}

#[test]
fn test_very_high_fps() {
    let frame_interval = CMTime::new(1, 240); // 240 FPS
    let config = SCStreamConfiguration::new().with_minimum_frame_interval(&frame_interval);
    // Just verify no crash
    let _ = config.minimum_frame_interval();
}

#[test]
fn test_very_low_fps() {
    let frame_interval = CMTime::new(1, 1); // 1 FPS
    let config = SCStreamConfiguration::new().with_minimum_frame_interval(&frame_interval);
    let _ = config.minimum_frame_interval();
}

// MARK: - Configuration Clone and Equality

#[test]
fn test_configuration_clone() {
    let config1 = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080);

    let config2 = config1.clone();
    assert_eq!(config1.width(), config2.width());
    assert_eq!(config1.height(), config2.height());
}

#[test]
fn test_configuration_equality() {
    let config1 = SCStreamConfiguration::new();
    let config2 = SCStreamConfiguration::new();

    // Two separately created configs have different pointers, so they're not equal
    // This tests pointer-based equality
    assert_ne!(config1, config2);

    // `Clone` deep-copies the underlying mutable SCStreamConfiguration, so a
    // clone is a distinct object under this type's identity-based equality.
    let config3 = config1.clone();
    assert_ne!(config1, config3);
}

/// `Clone` must hand back an independent `SCStreamConfiguration`, not another
/// handle on the same mutable Objective-C object: aliasing clones would let
/// one handle silently reconfigure the others (and race, given `Send`/`Sync`).
#[test]
fn test_configuration_clone_is_independent() {
    let original = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080)
        .with_shows_cursor(true)
        .with_queue_depth(5);

    let mut copy = original.clone();
    assert_eq!(copy.width(), 1920);
    assert_eq!(copy.height(), 1080);
    assert!(copy.shows_cursor());
    assert_eq!(copy.queue_depth(), 5);

    copy.set_width(640);
    copy.set_height(480);
    copy.set_shows_cursor(false);
    copy.set_queue_depth(8);

    assert_eq!(original.width(), 1920, "clone must not mutate the original");
    assert_eq!(original.height(), 1080);
    assert!(original.shows_cursor());
    assert_eq!(original.queue_depth(), 5);
}

/// Side-channel state (background colour / colour space / colour matrix lives
/// in the Swift bridge's per-configuration table, not on the native object)
/// has to be carried across the deep copy too.
#[test]
fn test_configuration_clone_copies_side_channel_state() {
    let original = SCStreamConfiguration::new()
        .with_background_color_rgba(0.25, 0.5, 0.75, 1.0)
        .with_color_space_name(color_space::SRGB)
        .with_color_matrix(color_matrix::ITU_R_709_2);

    let copy = original.clone();
    assert_ne!(copy, original, "clone must be an independent object");
    assert_eq!(copy.background_color(), Some((0.25, 0.5, 0.75, 1.0)));
    assert_eq!(copy.color_space_name().as_deref(), Some(color_space::SRGB));
    assert_eq!(
        copy.color_matrix().as_deref(),
        Some(color_matrix::ITU_R_709_2)
    );
}

/// `queueDepth` outside `ScreenCaptureKit`'s documented 3..=8 range is clamped
/// rather than forwarded, because `SCStream` rejects it at `startCapture`
/// time with an opaque `SCStreamErrorFailedToStart`.
#[test]
fn test_queue_depth_is_clamped_to_supported_range() {
    let mut config = SCStreamConfiguration::new();

    config.set_queue_depth(0);
    assert_eq!(config.queue_depth(), MIN_QUEUE_DEPTH);

    config.set_queue_depth(1);
    assert_eq!(config.queue_depth(), MIN_QUEUE_DEPTH);

    config.set_queue_depth(u32::MAX);
    assert_eq!(config.queue_depth(), MAX_QUEUE_DEPTH);

    for depth in MIN_QUEUE_DEPTH..=MAX_QUEUE_DEPTH {
        config.set_queue_depth(depth);
        assert_eq!(config.queue_depth(), depth);
    }
}

/// `set_fps(0)` means "uncapped". The naive `1/0` `CMTime` it used to build was
/// flagged valid but had a zero timescale, so `CMTimeGetSeconds` divides by
/// zero; `kCMTimeZero` (`0/1`) is the value `ScreenCaptureKit` documents.
#[test]
fn test_set_fps_zero_is_cm_time_zero() {
    let config = SCStreamConfiguration::new().with_fps(0);

    let interval = config.minimum_frame_interval();
    assert_eq!(interval.value, 0);
    assert_eq!(interval.timescale, 1);
    assert!(interval.is_valid());
    assert_eq!(config.fps(), 0);
}

/// Interior NUL bytes cannot cross the C boundary; the fallible setters must
/// say so instead of silently leaving the previous value in place.
#[test]
fn test_interior_nul_strings_are_rejected() {
    let mut config = SCStreamConfiguration::new().with_color_matrix(color_matrix::ITU_R_601_4);

    assert!(config.try_set_color_matrix("ITU_R\0_709_2").is_err());
    assert!(config
        .try_set_color_space_name("kCGColorSpace\0SRGB")
        .is_err());
    #[cfg(feature = "macos_14_0")]
    assert!(config.try_set_stream_name(Some("bad\0name")).is_err());

    assert_eq!(
        config.color_matrix().as_deref(),
        Some(color_matrix::ITU_R_601_4),
        "a rejected value must not disturb the previous one"
    );

    assert!(config
        .try_set_color_matrix(color_matrix::SMPTE_240M_1995)
        .is_ok());
    assert_eq!(
        config.color_matrix().as_deref(),
        Some(color_matrix::SMPTE_240M_1995)
    );
}

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

    let config1 = SCStreamConfiguration::new();
    let config2 = SCStreamConfiguration::new();

    let mut set = HashSet::new();
    set.insert(config1);
    set.insert(config2);
    assert!(!set.is_empty(), "Set should contain configurations");

    // Both should hash to the same value since they're default configs
    // Note: This depends on the Hash implementation
}

// MARK: - Mutable vs Builder Pattern Consistency

#[test]
fn test_mutable_matches_builder() {
    // Using builder pattern
    let builder_config = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080)
        .with_shows_cursor(true);

    // Using mutable pattern
    let mut mutable_config = SCStreamConfiguration::new();
    mutable_config.set_width(1920);
    mutable_config.set_height(1080);
    mutable_config.set_shows_cursor(true);

    assert_eq!(builder_config.width(), mutable_config.width());
    assert_eq!(builder_config.height(), mutable_config.height());
    assert_eq!(builder_config.shows_cursor(), mutable_config.shows_cursor());
}

// MARK: - Feature-Gated Tests

#[test]
#[cfg(feature = "macos_14_0")]
fn test_builder_capture_resolution_type() {
    use screencapturekit::stream::configuration::SCCaptureResolutionType;

    let config =
        SCStreamConfiguration::new().with_capture_resolution_type(SCCaptureResolutionType::Best);

    let result = config.capture_resolution_type();
    println!("Capture resolution type: {result:?}");
}

#[test]
#[cfg(feature = "macos_14_0")]
fn test_all_capture_resolution_types() {
    use screencapturekit::stream::configuration::SCCaptureResolutionType;

    let types = [
        SCCaptureResolutionType::Automatic,
        SCCaptureResolutionType::Best,
        SCCaptureResolutionType::Nominal,
    ];

    for res_type in types {
        let config = SCStreamConfiguration::new().with_capture_resolution_type(res_type);
        let _ = config.capture_resolution_type();
    }
}

#[test]
#[cfg(feature = "macos_14_2")]
fn test_builder_advanced_options() {
    use screencapturekit::stream::configuration::SCPresenterOverlayAlertSetting;

    let config = SCStreamConfiguration::new()
        .with_ignores_shadows_single_window(true)
        .with_should_be_opaque(false)
        .with_includes_child_windows(true)
        .with_presenter_overlay_privacy_alert_setting(SCPresenterOverlayAlertSetting::Never);

    // Verify setters worked (getters may return defaults on older macOS)
    let _ = config.ignores_shadows_single_window();
    let _ = config.should_be_opaque();
    let _ = config.includes_child_windows();
    let _ = config.presenter_overlay_privacy_alert_setting();
}

#[test]
#[cfg(feature = "macos_14_0")]
fn test_builder_ignores_shadow_display_configuration() {
    let config = SCStreamConfiguration::new().with_ignores_shadow_display_configuration(true);
    let _ = config.ignores_shadow_display_configuration();
}

#[test]
#[cfg(feature = "macos_15_0")]
fn test_builder_captures_microphone() {
    let config = SCStreamConfiguration::new().with_captures_microphone(true);
    let result = config.captures_microphone();
    println!("Captures microphone: {result}");
}

#[test]
#[cfg(feature = "macos_15_0")]
fn test_builder_dynamic_range() {
    use screencapturekit::stream::configuration::SCCaptureDynamicRange;

    let ranges = [
        SCCaptureDynamicRange::SDR,
        SCCaptureDynamicRange::HDRLocalDisplay,
        SCCaptureDynamicRange::HDRCanonicalDisplay,
    ];

    for range in ranges {
        let config = SCStreamConfiguration::new().with_capture_dynamic_range(range);
        let result = config.capture_dynamic_range();
        println!("Dynamic range: {result:?}");
    }
}

// MARK: - Audio Configuration Tests

#[test]
fn test_audio_sample_rates() {
    let rates = [22050, 44100, 48000, 96000, 192_000];

    for rate in rates {
        let config = SCStreamConfiguration::new()
            .with_captures_audio(true)
            .with_sample_rate(rate);
        // Just verify no crash
        let _ = config.captures_audio();
    }
}

#[test]
fn test_audio_channel_counts() {
    let counts = [1, 2, 6, 8]; // Mono, Stereo, 5.1, 7.1

    for count in counts {
        let config = SCStreamConfiguration::new()
            .with_captures_audio(true)
            .with_channel_count(count);
        // Just verify no crash
        let _ = config.captures_audio();
    }
}

#[test]
#[cfg(feature = "macos_14_0")]
fn test_excludes_current_process_audio() {
    let config = SCStreamConfiguration::new()
        .with_captures_audio(true)
        .with_excludes_current_process_audio(true);
    let _ = config.excludes_current_process_audio();
}

// MARK: - Color Configuration Tests

#[test]
fn test_color_space_name() {
    let config = SCStreamConfiguration::new().with_color_space_name("kCGColorSpaceSRGB");
    assert_eq!(
        config.color_space_name(),
        Some("kCGColorSpaceSRGB".to_string())
    );
}

#[test]
fn test_color_matrix() {
    let config = SCStreamConfiguration::new().with_color_matrix("kCGColorMatrix709");
    assert_eq!(config.color_matrix(), Some("kCGColorMatrix709".to_string()));
}

// MARK: - Complete Configuration Test

#[test]
fn test_complete_production_configuration() {
    // A realistic production configuration
    let frame_interval = CMTime::new(1, 60);

    let config = SCStreamConfiguration::new()
        // Video
        .with_width(1920)
        .with_height(1080)
        .with_pixel_format(PixelFormat::BGRA)
        .with_minimum_frame_interval(&frame_interval)
        .with_queue_depth(8)
        .with_shows_cursor(true)
        .with_scales_to_fit(false)
        // Audio
        .with_captures_audio(true)
        .with_sample_rate(48000)
        .with_channel_count(2);

    // Verify critical settings
    assert_eq!(config.width(), 1920);
    assert_eq!(config.height(), 1080);
    assert!(config.shows_cursor());
    assert!(config.captures_audio());
    assert_eq!(config.queue_depth(), 8);
}

#[test]
fn test_streaming_configuration() {
    // Configuration optimized for streaming
    let frame_interval = CMTime::new(1, 30);

    let config = SCStreamConfiguration::new()
        .with_width(1280)
        .with_height(720)
        .with_pixel_format(PixelFormat::YCbCr_420v) // Better for encoding
        .with_minimum_frame_interval(&frame_interval)
        .with_queue_depth(3) // Lower latency
        .with_shows_cursor(true)
        .with_captures_audio(true)
        .with_sample_rate(44100)
        .with_channel_count(2);

    assert_eq!(config.width(), 1280);
    assert_eq!(config.height(), 720);
}

#[test]
fn test_screenshot_configuration() {
    // Configuration optimized for single screenshots
    let config = SCStreamConfiguration::new()
        .with_width(3840)
        .with_height(2160)
        .with_pixel_format(PixelFormat::BGRA)
        .with_queue_depth(1)
        .with_shows_cursor(false)
        .with_captures_audio(false);

    assert_eq!(config.width(), 3840);
    assert_eq!(config.height(), 2160);
    assert!(!config.shows_cursor());
    assert!(!config.captures_audio());
}