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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Benchmarks for screen capture operations
//!
//! Run with: `cargo bench`
//!
//! These benchmarks require screen recording permission and a display to capture.
//!
//! # Benchmark Categories
//!
//! 1. **API Overhead** - Measures the cost of creating objects (filter, config, etc.)
//! 2. **Frame Capture** - Measures throughput and latency of actual frame capture
//! 3. **Data Access** - Measures pixel buffer and `IOSurface` access patterns
//! 4. **Screenshots** - Measures single-frame capture performance (macOS 14.0+)

#![allow(clippy::cast_possible_truncation)]

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use screencapturekit::cm::CMTime;
use screencapturekit::cv::CVPixelBufferLockFlags;
use screencapturekit::prelude::*;
use screencapturekit::shareable_content::SCShareableContent;
use screencapturekit::stream::configuration::SCStreamConfiguration;
use screencapturekit::stream::content_filter::SCContentFilter;
use std::hint::black_box;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

// Initialize CoreGraphics to prevent CGS_REQUIRE_INIT crashes
fn cg_init() {
    extern "C" {
        fn sc_initialize_core_graphics();
    }
    unsafe { sc_initialize_core_graphics() }
}

// =============================================================================
// API Overhead Benchmarks
// =============================================================================

fn bench_shareable_content_get(c: &mut Criterion) {
    cg_init();

    c.bench_function("api/SCShareableContent::get", |b| {
        b.iter(|| {
            let content = SCShareableContent::get();
            black_box(content)
        });
    });
}

fn bench_shareable_content_with_options(c: &mut Criterion) {
    cg_init();

    c.bench_function("api/SCShareableContent::create", |b| {
        b.iter(|| {
            let content = SCShareableContent::create()
                .with_exclude_desktop_windows(true)
                .with_on_screen_windows_only(true)
                .get();
            black_box(content)
        });
    });
}

fn bench_content_filter_creation(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");

    c.bench_function("api/SCContentFilter::create", |b| {
        b.iter(|| {
            let filter = SCContentFilter::create()
                .with_display(&display)
                .with_excluding_windows(&[])
                .build();
            black_box(filter)
        });
    });
}

fn bench_stream_configuration_creation(c: &mut Criterion) {
    c.bench_function("api/SCStreamConfiguration::new", |b| {
        b.iter(|| {
            let config = SCStreamConfiguration::new()
                .with_width(1920)
                .with_height(1080)
                .with_shows_cursor(true);
            black_box(config)
        });
    });
}

fn bench_stream_creation(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");
    let filter = SCContentFilter::create()
        .with_display(&display)
        .with_excluding_windows(&[])
        .build();
    let config = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080);

    c.bench_function("api/SCStream::new", |b| {
        b.iter(|| {
            let stream = SCStream::new(&filter, &config);
            black_box(stream)
        });
    });
}

// =============================================================================
// Frame Capture Benchmarks
// =============================================================================

/// Benchmark steady-state frame throughput at various resolutions.
///
/// **What this measures:** the per-frame wall time once a stream is up
/// and running. The stream is created and started **once per
/// resolution** (outside `iter_custom`), then each `iter_custom` call
/// blocks until `iters` additional frames have arrived. This excludes
/// the one-off setup/teardown cost (which is dominated by Swift
/// async machinery and is measured separately by
/// `bench_stream_startup`) and isolates the dispatch + delivery path.
///
/// **What this measures, in user terms:** is the host able to deliver
/// the configured framerate at this resolution? At 60fps the
/// per-frame budget is ~16.6 ms; if the bench shows times much higher
/// than that, the OS is failing to keep up (or the dispatch path is
/// adding meaningful overhead).
///
/// **What previous versions of this benchmark got wrong:** the bench
/// recreated the stream every iteration and inserted a 100 ms sleep,
/// then reported total wall time as throughput. The sleep dominated;
/// the per-frame cost was <1% of the measurement.
fn bench_frame_throughput(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");

    let resolutions: [(u32, u32, &str); 4] = [
        (640, 480, "480p"),
        (1280, 720, "720p"),
        (1920, 1080, "1080p"),
        (3840, 2160, "4K"),
    ];

    let mut group = c.benchmark_group("throughput");
    group.sample_size(10);
    group.measurement_time(Duration::from_secs(10));

    for (width, height, label) in resolutions {
        // Skip 4K if display is smaller
        if width > display.width() || height > display.height() {
            continue;
        }

        let pixels = u64::from(width * height);
        group.throughput(Throughput::Elements(pixels));

        group.bench_with_input(
            BenchmarkId::new("frames", label),
            &(width, height),
            |b, &(w, h)| {
                let filter = SCContentFilter::create()
                    .with_display(&display)
                    .with_excluding_windows(&[])
                    .build();

                let config = SCStreamConfiguration::new()
                    .with_width(w)
                    .with_height(h)
                    .with_pixel_format(PixelFormat::BGRA)
                    .with_minimum_frame_interval(&CMTime::new(1, 120)); // request 120 FPS max

                let frame_count = Arc::new(AtomicUsize::new(0));
                let frame_count_clone = Arc::clone(&frame_count);

                let handler = move |_sample: CMSampleBuffer, output_type: SCStreamOutputType| {
                    if matches!(output_type, SCStreamOutputType::Screen) {
                        frame_count_clone.fetch_add(1, Ordering::Relaxed);
                    }
                };

                // Start the stream ONCE outside iter_custom so setup cost
                // doesn't pollute the measurement.
                let mut stream = SCStream::new(&filter, &config);
                stream.add_output_handler(handler, SCStreamOutputType::Screen);
                stream.start_capture().expect("Failed to start capture");

                // Warmup: drop the first second of frames so we measure
                // steady-state, not first-frame latency.
                let warmup_target = frame_count.load(Ordering::Relaxed) + 30;
                let warmup_deadline = Instant::now() + Duration::from_secs(2);
                while frame_count.load(Ordering::Relaxed) < warmup_target
                    && Instant::now() < warmup_deadline
                {
                    std::thread::sleep(Duration::from_micros(500));
                }

                b.iter_custom(|iters| {
                    let target = frame_count.load(Ordering::Relaxed) + iters as usize;
                    let start = Instant::now();
                    let timeout = Duration::from_secs(30);

                    while frame_count.load(Ordering::Relaxed) < target {
                        if start.elapsed() > timeout {
                            break;
                        }
                        std::thread::sleep(Duration::from_micros(100));
                    }

                    let elapsed = start.elapsed();
                    black_box(frame_count.load(Ordering::Relaxed));
                    elapsed
                });

                stream.stop_capture().expect("Failed to stop capture");
            },
        );
    }

    group.finish();
}

/// Benchmark stream startup cost: `SCStream::new` + `start_capture` +
/// first-frame arrival.
///
/// This is the per-stream one-off cost that `bench_frame_throughput`
/// deliberately excludes. Useful for monitoring regressions in the
/// async start path through the Swift bridge.
fn bench_stream_startup(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");

    let mut group = c.benchmark_group("startup");
    group.sample_size(10);

    group.bench_function("create_start_first_frame", |b| {
        let filter = SCContentFilter::create()
            .with_display(&display)
            .with_excluding_windows(&[])
            .build();

        let config = SCStreamConfiguration::new()
            .with_width(640)
            .with_height(480)
            .with_pixel_format(PixelFormat::BGRA);

        b.iter_custom(|iters| {
            let mut total = Duration::ZERO;
            for _ in 0..iters {
                let first_frame = Arc::new(AtomicU64::new(0));
                let first_frame_clone = Arc::clone(&first_frame);

                let start = Instant::now();
                let handler = move |_sample: CMSampleBuffer, output_type: SCStreamOutputType| {
                    if matches!(output_type, SCStreamOutputType::Screen) {
                        // Use Relaxed swap pattern instead of SeqCst CAS — much
                        // cheaper, and we don't care about ordering with other
                        // memory because `first_frame` is the only signal.
                        if first_frame_clone.load(Ordering::Relaxed) == 0 {
                            let _ = first_frame_clone.compare_exchange(
                                0,
                                start.elapsed().as_nanos() as u64,
                                Ordering::Relaxed,
                                Ordering::Relaxed,
                            );
                        }
                    }
                };

                let mut stream = SCStream::new(&filter, &config);
                stream.add_output_handler(handler, SCStreamOutputType::Screen);
                stream.start_capture().expect("Failed to start capture");

                // Wait for first frame, with a generous timeout.
                let deadline = Instant::now() + Duration::from_secs(2);
                while first_frame.load(Ordering::Relaxed) == 0 && Instant::now() < deadline {
                    std::thread::sleep(Duration::from_micros(100));
                }

                let nanos = first_frame.load(Ordering::Relaxed);
                if nanos > 0 {
                    total += Duration::from_nanos(nanos);
                }

                stream.stop_capture().expect("Failed to stop capture");
            }
            total
        });
    });

    group.finish();
}

/// Benchmark frame latency (time from capture request to callback)
fn bench_frame_latency(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");

    let mut group = c.benchmark_group("latency");
    group.sample_size(20);

    group.bench_function("first_frame", |b| {
        let filter = SCContentFilter::create()
            .with_display(&display)
            .with_excluding_windows(&[])
            .build();

        let config = SCStreamConfiguration::new()
            .with_width(1920)
            .with_height(1080)
            .with_pixel_format(PixelFormat::BGRA);

        b.iter_custom(|iters| {
            let mut total_latency = Duration::ZERO;

            for _ in 0..iters {
                let first_frame_time = Arc::new(AtomicU64::new(0));
                let first_frame_time_clone = Arc::clone(&first_frame_time);
                let start_time = Instant::now();

                let handler = move |_sample: CMSampleBuffer, output_type: SCStreamOutputType| {
                    if matches!(output_type, SCStreamOutputType::Screen) {
                        // Only record the first frame
                        first_frame_time_clone
                            .compare_exchange(
                                0,
                                start_time.elapsed().as_nanos() as u64,
                                Ordering::SeqCst,
                                Ordering::Relaxed,
                            )
                            .ok();
                    }
                };

                let mut stream = SCStream::new(&filter, &config);
                stream.add_output_handler(handler, SCStreamOutputType::Screen);

                stream.start_capture().expect("Failed to start capture");

                // Wait for first frame (with timeout)
                let timeout = Duration::from_secs(2);
                let poll_start = Instant::now();
                while first_frame_time.load(Ordering::Relaxed) == 0 {
                    if poll_start.elapsed() > timeout {
                        break;
                    }
                    std::thread::sleep(Duration::from_micros(100));
                }

                stream.stop_capture().expect("Failed to stop capture");

                let latency_nanos = first_frame_time.load(Ordering::Relaxed);
                if latency_nanos > 0 {
                    total_latency += Duration::from_nanos(latency_nanos);
                }
            }

            total_latency
        });
    });

    group.finish();
}

// =============================================================================
// Data Access Benchmarks
// =============================================================================

/// Benchmark pixel buffer locking and access patterns
fn bench_pixel_buffer_access(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");

    let filter = SCContentFilter::create()
        .with_display(&display)
        .with_excluding_windows(&[])
        .build();

    let config = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080)
        .with_pixel_format(PixelFormat::BGRA);

    // Capture a single frame to benchmark access patterns
    let sample_buffer: Arc<std::sync::Mutex<Option<CMSampleBuffer>>> =
        Arc::new(std::sync::Mutex::new(None));
    let sample_buffer_clone = Arc::clone(&sample_buffer);
    let captured = Arc::new(AtomicUsize::new(0));
    let captured_clone = Arc::clone(&captured);

    let handler = move |sample: CMSampleBuffer, output_type: SCStreamOutputType| {
        if matches!(output_type, SCStreamOutputType::Screen)
            && captured_clone
                .compare_exchange(0, 1, Ordering::SeqCst, Ordering::Relaxed)
                .is_ok()
        {
            *sample_buffer_clone.lock().unwrap() = Some(sample);
        }
    };

    let mut stream = SCStream::new(&filter, &config);
    stream.add_output_handler(handler, SCStreamOutputType::Screen);
    stream.start_capture().expect("Failed to start capture");

    // Wait for a frame
    let timeout = Duration::from_secs(2);
    let start = Instant::now();
    while captured.load(Ordering::Relaxed) == 0 && start.elapsed() < timeout {
        std::thread::sleep(Duration::from_millis(10));
    }

    stream.stop_capture().expect("Failed to stop capture");

    let sample = sample_buffer.lock().unwrap().take();
    let Some(sample) = sample else {
        eprintln!("Warning: Could not capture frame for pixel buffer benchmarks");
        return;
    };

    let Some(pixel_buffer) = sample.image_buffer() else {
        eprintln!("Warning: No pixel buffer in captured frame");
        return;
    };

    let mut group = c.benchmark_group("data_access");

    // Benchmark lock/unlock cycle
    group.bench_function("pixel_buffer/lock_unlock", |b| {
        b.iter(|| {
            let guard = pixel_buffer.lock(CVPixelBufferLockFlags::READ_ONLY);
            black_box(guard)
        });
    });

    // Benchmark reading first pixel
    group.bench_function("pixel_buffer/read_first_pixel", |b| {
        b.iter(|| {
            if let Ok(guard) = pixel_buffer.lock(CVPixelBufferLockFlags::READ_ONLY) {
                let slice = guard.as_slice();
                if slice.len() >= 4 {
                    let pixel: [u8; 4] = [slice[0], slice[1], slice[2], slice[3]];
                    black_box(pixel);
                }
            }
        });
    });

    // Benchmark reading all pixels (simulate full-frame processing)
    group.bench_function("pixel_buffer/read_all_pixels", |b| {
        b.iter(|| {
            if let Ok(guard) = pixel_buffer.lock(CVPixelBufferLockFlags::READ_ONLY) {
                let slice = guard.as_slice();
                // Simulate processing by computing a checksum
                let sum: u64 = slice.iter().step_by(1024).map(|&b| u64::from(b)).sum();
                black_box(sum);
            }
        });
    });

    // Benchmark IOSurface access if available
    if pixel_buffer.is_backed_by_io_surface() {
        if let Some(iosurface) = pixel_buffer.io_surface() {
            group.bench_function("iosurface/lock_unlock", |b| {
                use screencapturekit::cm::IOSurfaceLockOptions;
                b.iter(|| {
                    let guard = iosurface.lock(IOSurfaceLockOptions::READ_ONLY);
                    black_box(guard)
                });
            });

            group.bench_function("iosurface/get_properties", |b| {
                b.iter(|| {
                    let width = iosurface.width();
                    let height = iosurface.height();
                    let format = iosurface.pixel_format();
                    let bpr = iosurface.bytes_per_row();
                    black_box((width, height, format, bpr));
                });
            });
        }
    }

    group.finish();
}

// =============================================================================
// Screenshot Benchmarks (macOS 14.0+)
// =============================================================================

#[cfg(feature = "macos_14_0")]
fn bench_screenshot_capture(c: &mut Criterion) {
    use screencapturekit::screenshot_manager::SCScreenshotManager;

    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");
    let filter = SCContentFilter::create()
        .with_display(&display)
        .with_excluding_windows(&[])
        .build();

    let resolutions: [(u32, u32, &str); 3] = [
        (640, 480, "480p"),
        (1920, 1080, "1080p"),
        (3840, 2160, "4K"),
    ];

    let mut group = c.benchmark_group("screenshot");
    group.sample_size(20);

    for (width, height, label) in resolutions {
        // Skip if larger than display
        if width > display.width() || height > display.height() {
            continue;
        }

        let config = SCStreamConfiguration::new()
            .with_width(width)
            .with_height(height);

        group.bench_with_input(BenchmarkId::new("capture_image", label), &(), |b, ()| {
            b.iter(|| {
                let result = SCScreenshotManager::capture_image(&filter, &config);
                black_box(result)
            });
        });

        group.bench_with_input(
            BenchmarkId::new("capture_sample_buffer", label),
            &(),
            |b, ()| {
                b.iter(|| {
                    let result = SCScreenshotManager::capture_sample_buffer(&filter, &config);
                    black_box(result)
                });
            },
        );
    }

    group.finish();
}

#[cfg(not(feature = "macos_14_0"))]
fn bench_screenshot_capture(_c: &mut Criterion) {
    // Screenshot manager requires macOS 14.0+
}

// =============================================================================
// Stream Lifecycle Benchmarks
// =============================================================================

fn bench_stream_lifecycle(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");

    let filter = SCContentFilter::create()
        .with_display(&display)
        .with_excluding_windows(&[])
        .build();

    let config = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080);

    let mut group = c.benchmark_group("lifecycle");
    group.sample_size(20);

    group.bench_function("start_stop_cycle", |b| {
        b.iter_custom(|iters| {
            let mut total = Duration::ZERO;

            for _ in 0..iters {
                let stream = SCStream::new(&filter, &config);

                let start = Instant::now();
                stream.start_capture().expect("Failed to start");
                stream.stop_capture().expect("Failed to stop");
                total += start.elapsed();

                black_box(&stream);
            }

            total
        });
    });

    group.bench_function("start_capture_only", |b| {
        b.iter_custom(|iters| {
            let mut total = Duration::ZERO;

            for _ in 0..iters {
                let stream = SCStream::new(&filter, &config);

                let start = Instant::now();
                stream.start_capture().expect("Failed to start");
                total += start.elapsed();

                stream.stop_capture().expect("Failed to stop");
                black_box(&stream);
            }

            total
        });
    });

    group.finish();
}

// =============================================================================
// Configuration Update Benchmarks
// =============================================================================

fn bench_configuration_updates(c: &mut Criterion) {
    cg_init();

    let content = SCShareableContent::get().expect("Failed to get shareable content");
    let display = content.displays().into_iter().next().expect("No display");

    let filter = SCContentFilter::create()
        .with_display(&display)
        .with_excluding_windows(&[])
        .build();

    let config = SCStreamConfiguration::new()
        .with_width(1920)
        .with_height(1080);

    let mut group = c.benchmark_group("updates");
    group.sample_size(20);

    group.bench_function("update_configuration", |b| {
        let stream = SCStream::new(&filter, &config);
        stream.start_capture().expect("Failed to start");

        b.iter(|| {
            let new_config = SCStreamConfiguration::new()
                .with_width(1280)
                .with_height(720);
            let result = stream.update_configuration(&new_config);
            black_box(result)
        });

        stream.stop_capture().expect("Failed to stop");
    });

    group.finish();
}

criterion_group!(
    benches,
    // API overhead
    bench_shareable_content_get,
    bench_shareable_content_with_options,
    bench_content_filter_creation,
    bench_stream_configuration_creation,
    bench_stream_creation,
    // Frame capture
    bench_frame_throughput,
    bench_stream_startup,
    bench_frame_latency,
    // Data access
    bench_pixel_buffer_access,
    // Screenshots
    bench_screenshot_capture,
    // Lifecycle
    bench_stream_lifecycle,
    bench_configuration_updates,
);

criterion_main!(benches);