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
//! `SCStreamErrorCode` tests
//!
//! Tests for error code handling and conversion

use screencapturekit::error::{SCError, SCStreamErrorCode, SC_STREAM_ERROR_DOMAIN};

#[test]
fn test_error_domain_constant() {
    assert_eq!(
        SC_STREAM_ERROR_DOMAIN,
        "com.apple.ScreenCaptureKit.SCStreamErrorDomain"
    );
}

// MARK: - Error Code Values

#[test]
fn test_error_code_values() {
    // Verify the raw values match Apple's SCStreamError.Code from SCError.h
    assert_eq!(SCStreamErrorCode::UserDeclined as i32, -3801);
    assert_eq!(SCStreamErrorCode::FailedToStart as i32, -3802);
    assert_eq!(SCStreamErrorCode::MissingEntitlements as i32, -3803);
    assert_eq!(
        SCStreamErrorCode::FailedApplicationConnectionInvalid as i32,
        -3804
    );
    assert_eq!(
        SCStreamErrorCode::FailedApplicationConnectionInterrupted as i32,
        -3805
    );
    assert_eq!(
        SCStreamErrorCode::FailedNoMatchingApplicationContext as i32,
        -3806
    );
    assert_eq!(SCStreamErrorCode::AttemptToStartStreamState as i32, -3807);
    assert_eq!(SCStreamErrorCode::AttemptToStopStreamState as i32, -3808);
    assert_eq!(SCStreamErrorCode::AttemptToUpdateFilterState as i32, -3809);
    assert_eq!(SCStreamErrorCode::AttemptToConfigState as i32, -3810);
    assert_eq!(SCStreamErrorCode::InternalError as i32, -3811);
    assert_eq!(SCStreamErrorCode::InvalidParameter as i32, -3812);
    assert_eq!(SCStreamErrorCode::NoWindowList as i32, -3813);
    assert_eq!(SCStreamErrorCode::NoDisplayList as i32, -3814);
    assert_eq!(SCStreamErrorCode::NoCaptureSource as i32, -3815);
    assert_eq!(SCStreamErrorCode::RemovingStream as i32, -3816);
    assert_eq!(SCStreamErrorCode::UserStopped as i32, -3817);
    assert_eq!(SCStreamErrorCode::FailedToStartAudioCapture as i32, -3818);
    assert_eq!(SCStreamErrorCode::FailedToStopAudioCapture as i32, -3819);
    assert_eq!(
        SCStreamErrorCode::FailedToStartMicrophoneCapture as i32,
        -3820
    );
    assert_eq!(SCStreamErrorCode::SystemStoppedStream as i32, -3821);
}

// MARK: - Error Code Conversion

#[test]
fn test_error_code_from_raw_valid() {
    let codes = [
        (-3801, SCStreamErrorCode::UserDeclined),
        (-3802, SCStreamErrorCode::FailedToStart),
        (-3803, SCStreamErrorCode::MissingEntitlements),
        (-3804, SCStreamErrorCode::FailedApplicationConnectionInvalid),
        (
            -3805,
            SCStreamErrorCode::FailedApplicationConnectionInterrupted,
        ),
        (-3806, SCStreamErrorCode::FailedNoMatchingApplicationContext),
        (-3807, SCStreamErrorCode::AttemptToStartStreamState),
        (-3808, SCStreamErrorCode::AttemptToStopStreamState),
        (-3809, SCStreamErrorCode::AttemptToUpdateFilterState),
        (-3810, SCStreamErrorCode::AttemptToConfigState),
        (-3811, SCStreamErrorCode::InternalError),
        (-3812, SCStreamErrorCode::InvalidParameter),
        (-3813, SCStreamErrorCode::NoWindowList),
        (-3814, SCStreamErrorCode::NoDisplayList),
        (-3815, SCStreamErrorCode::NoCaptureSource),
        (-3816, SCStreamErrorCode::RemovingStream),
        (-3817, SCStreamErrorCode::UserStopped),
        (-3818, SCStreamErrorCode::FailedToStartAudioCapture),
        (-3819, SCStreamErrorCode::FailedToStopAudioCapture),
        (-3820, SCStreamErrorCode::FailedToStartMicrophoneCapture),
        (-3821, SCStreamErrorCode::SystemStoppedStream),
    ];

    for (raw, expected) in codes {
        let result = SCStreamErrorCode::from_raw(raw);
        assert_eq!(result, Some(expected), "Failed for raw value {raw}");
    }
}

#[test]
fn test_error_code_from_raw_invalid() {
    let invalid_codes = [0, 1, -1, -3800, -3822, -9999, i32::MAX, i32::MIN];

    for code in invalid_codes {
        let result = SCStreamErrorCode::from_raw(code);
        assert!(result.is_none(), "Should be None for invalid code {code}");
    }
}

// MARK: - Error Code Display

#[test]
fn test_error_code_display() {
    let code = SCStreamErrorCode::UserDeclined;
    let display = format!("{code}");
    assert!(!display.is_empty());
}

#[test]
fn test_all_error_codes_display() {
    let codes = [
        SCStreamErrorCode::UserDeclined,
        SCStreamErrorCode::FailedToStart,
        SCStreamErrorCode::MissingEntitlements,
        SCStreamErrorCode::FailedApplicationConnectionInvalid,
        SCStreamErrorCode::FailedApplicationConnectionInterrupted,
        SCStreamErrorCode::FailedNoMatchingApplicationContext,
        SCStreamErrorCode::AttemptToStartStreamState,
        SCStreamErrorCode::AttemptToStopStreamState,
        SCStreamErrorCode::AttemptToUpdateFilterState,
        SCStreamErrorCode::AttemptToConfigState,
        SCStreamErrorCode::InternalError,
        SCStreamErrorCode::InvalidParameter,
        SCStreamErrorCode::NoWindowList,
        SCStreamErrorCode::NoDisplayList,
        SCStreamErrorCode::NoCaptureSource,
        SCStreamErrorCode::RemovingStream,
        SCStreamErrorCode::UserStopped,
        SCStreamErrorCode::FailedToStartAudioCapture,
        SCStreamErrorCode::FailedToStopAudioCapture,
        SCStreamErrorCode::FailedToStartMicrophoneCapture,
        SCStreamErrorCode::SystemStoppedStream,
    ];

    for code in codes {
        let display = format!("{code}");
        assert!(
            !display.is_empty(),
            "Display should not be empty for {code:?}"
        );
    }
}

// MARK: - Error Code Debug

#[test]
fn test_error_code_debug() {
    let code = SCStreamErrorCode::UserDeclined;
    let debug = format!("{code:?}");
    assert!(debug.contains("UserDeclined"));
}

// MARK: - SCError Integration

#[test]
fn test_scerror_from_stream_error_code() {
    let code = SCStreamErrorCode::UserDeclined;
    let error = SCError::from_stream_error_code(code);

    match error {
        SCError::SCStreamError {
            code: err_code,
            message,
        } => {
            assert_eq!(err_code, code);
            assert!(message.is_none());
        }
        _ => panic!("Expected SCStreamError variant"),
    }
}

#[test]
fn test_scerror_from_stream_error_code_with_message() {
    let code = SCStreamErrorCode::InvalidParameter;
    let error = SCError::from_stream_error_code_with_message(code, "width must be positive");

    match error {
        SCError::SCStreamError {
            code: err_code,
            message,
        } => {
            assert_eq!(err_code, code);
            assert_eq!(message, Some("width must be positive".to_string()));
        }
        _ => panic!("Expected SCStreamError variant"),
    }
}

#[test]
fn test_scerror_from_error_code() {
    // Valid error code
    let error = SCError::from_error_code(-3801);
    match error {
        SCError::SCStreamError { code, .. } => {
            assert_eq!(code, SCStreamErrorCode::UserDeclined);
        }
        _ => panic!("Expected SCStreamError variant"),
    }

    // Invalid error code
    let error = SCError::from_error_code(-9999);
    match error {
        SCError::OSError { code, .. } => {
            assert_eq!(code, -9999);
        }
        _ => panic!("Expected OSError variant"),
    }
}

#[test]
fn test_scerror_stream_error_code_getter() {
    let code = SCStreamErrorCode::InternalError;
    let error = SCError::from_stream_error_code(code);

    let retrieved = error.stream_error_code();
    assert_eq!(retrieved, Some(code));

    // Non-SCStreamError should return None
    let other_error = SCError::InternalError("test".to_string());
    assert!(other_error.stream_error_code().is_none());
}

// MARK: - Error Code Equality and Hashing

#[test]
fn test_error_code_equality() {
    assert_eq!(
        SCStreamErrorCode::UserDeclined,
        SCStreamErrorCode::UserDeclined
    );
    assert_ne!(
        SCStreamErrorCode::UserDeclined,
        SCStreamErrorCode::InternalError
    );
}

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

    let mut set = HashSet::new();
    set.insert(SCStreamErrorCode::UserDeclined);
    set.insert(SCStreamErrorCode::InternalError);
    set.insert(SCStreamErrorCode::UserDeclined); // Duplicate

    assert_eq!(set.len(), 2);
}

#[test]
fn test_error_code_clone() {
    let code = SCStreamErrorCode::UserDeclined;
    let cloned = code;
    assert_eq!(code, cloned);
}

#[test]
fn test_error_code_copy() {
    let code = SCStreamErrorCode::UserDeclined;
    let copied: SCStreamErrorCode = code;
    assert_eq!(code, copied);
}

// MARK: - SCStreamError Display

#[test]
fn test_scstream_error_display_without_message() {
    let error = SCError::from_stream_error_code(SCStreamErrorCode::UserDeclined);
    let display = format!("{error}");

    assert!(display.contains("SCStream"));
}

#[test]
fn test_scstream_error_display_with_message() {
    let error = SCError::from_stream_error_code_with_message(
        SCStreamErrorCode::InvalidParameter,
        "width must be > 0",
    );
    let display = format!("{error}");

    assert!(display.contains("SCStream"));
    assert!(display.contains("width must be > 0"));
}

// MARK: - Error Categories

#[test]
fn test_audio_related_errors() {
    let audio_errors = [
        SCStreamErrorCode::FailedToStartAudioCapture,
        SCStreamErrorCode::FailedToStopAudioCapture,
        SCStreamErrorCode::FailedToStartMicrophoneCapture,
    ];

    for code in audio_errors {
        let error = SCError::from_stream_error_code(code);
        let display = format!("{error}");
        assert!(!display.is_empty());
    }
}

#[test]
fn test_stream_lifecycle_errors() {
    let lifecycle_errors = [
        SCStreamErrorCode::FailedToStart,
        SCStreamErrorCode::AttemptToStartStreamState,
        SCStreamErrorCode::AttemptToStopStreamState,
        SCStreamErrorCode::SystemStoppedStream,
        SCStreamErrorCode::UserStopped,
    ];

    for code in lifecycle_errors {
        let error = SCError::from_stream_error_code(code);
        let display = format!("{error}");
        assert!(!display.is_empty());
    }
}

#[test]
fn test_configuration_errors() {
    let config_errors = [
        SCStreamErrorCode::InvalidParameter,
        SCStreamErrorCode::NoWindowList,
        SCStreamErrorCode::NoDisplayList,
        SCStreamErrorCode::NoCaptureSource,
        SCStreamErrorCode::AttemptToConfigState,
        SCStreamErrorCode::AttemptToUpdateFilterState,
    ];

    for code in config_errors {
        let error = SCError::from_stream_error_code(code);
        let display = format!("{error}");
        assert!(!display.is_empty());
    }
}

// MARK: - New Error Codes (macOS 13.0+, 15.0+)

#[test]
fn test_macos_13_audio_errors() {
    // FailedToStartAudioCapture and FailedToStopAudioCapture are macOS 13.0+
    let audio_start = SCStreamErrorCode::FailedToStartAudioCapture;
    let audio_stop = SCStreamErrorCode::FailedToStopAudioCapture;

    assert_eq!(audio_start as i32, -3818);
    assert_eq!(audio_stop as i32, -3819);

    // Verify they can be created from raw values
    assert_eq!(
        SCStreamErrorCode::from_raw(-3818),
        Some(SCStreamErrorCode::FailedToStartAudioCapture)
    );
    assert_eq!(
        SCStreamErrorCode::from_raw(-3819),
        Some(SCStreamErrorCode::FailedToStopAudioCapture)
    );
}

#[test]
fn test_macos_15_errors() {
    // FailedToStartMicrophoneCapture and SystemStoppedStream are macOS 15.0+
    let mic = SCStreamErrorCode::FailedToStartMicrophoneCapture;
    let system_stopped = SCStreamErrorCode::SystemStoppedStream;

    assert_eq!(mic as i32, -3820);
    assert_eq!(system_stopped as i32, -3821);

    // Verify they can be created from raw values
    assert_eq!(
        SCStreamErrorCode::from_raw(-3820),
        Some(SCStreamErrorCode::FailedToStartMicrophoneCapture)
    );
    assert_eq!(
        SCStreamErrorCode::from_raw(-3821),
        Some(SCStreamErrorCode::SystemStoppedStream)
    );
}

#[test]
fn test_entitlements_error() {
    let code = SCStreamErrorCode::MissingEntitlements;
    assert_eq!(code as i32, -3803);

    let error = SCError::from_stream_error_code(code);
    let display = format!("{error}");
    assert!(display.to_lowercase().contains("entitlement"));
}

#[test]
fn test_application_connection_errors() {
    let codes = [
        (SCStreamErrorCode::FailedApplicationConnectionInvalid, -3804),
        (
            SCStreamErrorCode::FailedApplicationConnectionInterrupted,
            -3805,
        ),
        (SCStreamErrorCode::FailedNoMatchingApplicationContext, -3806),
    ];

    for (code, expected_value) in codes {
        assert_eq!(code as i32, expected_value);
        assert_eq!(SCStreamErrorCode::from_raw(expected_value), Some(code));
    }
}