kael 0.2.0

GPU-accelerated native UI framework for Rust — build desktop apps with Metal, DirectX, and Vulkan rendering
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
use crate::{
    DevicePixels, ForegroundExecutor, SharedString, SourceMetadata,
    platform::{ScreenCaptureFrame, ScreenCaptureSource, ScreenCaptureStream},
    size,
};
use anyhow::{Result, anyhow};
use block2::RcBlock;
use collections::HashMap;
use core_foundation::base::TCFType;
use core_graphics::display::{
    CGDirectDisplayID, CGDisplayBounds, CGDisplayCopyDisplayMode, CGDisplayModeGetPixelHeight,
    CGDisplayModeGetPixelWidth, CGDisplayModeRelease, CGGetActiveDisplayList,
};
use futures::channel::oneshot;
use media::core_media::{CMSampleBuffer, CMSampleBufferRef};
use metal::NSInteger;
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject};
use objc2::{AnyThread, DefinedClass, define_class, msg_send};
use objc2_foundation::{NSObject, NSObjectProtocol, NSString};
use std::ffi::CStr;
use std::{cell::RefCell, ffi::c_void, ptr, rc::Rc};

#[derive(Clone)]
pub struct MacScreenCaptureSource {
    sc_display: *mut AnyObject,
    meta: Option<ScreenMeta>,
}

pub struct MacScreenCaptureStream {
    sc_stream: *mut AnyObject,
    sc_stream_output: *mut AnyObject,
    meta: SourceMetadata,
}

const FRAME_CALLBACK_IVAR: &str = "frame_callback";

#[allow(non_upper_case_globals)]
const SCStreamOutputTypeScreen: NSInteger = 0;

unsafe fn lookup_class(name: &CStr) -> &'static AnyClass {
    AnyClass::get(name).unwrap_or_else(|| panic!("missing class {name:?}"))
}

unsafe fn ns_string_to_str(s: *mut AnyObject) -> String {
    if s.is_null() {
        return String::new();
    }
    let ns: &NSString = unsafe { &*(s as *const NSString) };
    ns.to_string()
}

#[derive(Default)]
struct StreamDelegateIvars;

define_class!(
    #[unsafe(super = NSObject)]
    #[ivars = StreamDelegateIvars]
    #[name = "GPUIStreamDelegate"]
    struct GPUIStreamDelegate;

    unsafe impl NSObjectProtocol for GPUIStreamDelegate {}

    impl GPUIStreamDelegate {
        #[unsafe(method(outputVideoEffectDidStartForStream:))]
        fn output_video_effect_did_start_for_stream(&self, _stream: *mut AnyObject) {}

        #[unsafe(method(outputVideoEffectDidStopForStream:))]
        fn output_video_effect_did_stop_for_stream(&self, _stream: *mut AnyObject) {}

        #[unsafe(method(stream:didStopWithError:))]
        fn stream_did_stop_with_error(
            &self,
            _stream: *mut AnyObject,
            _error: *mut AnyObject,
        ) {
        }
    }
);

impl GPUIStreamDelegate {
    fn new() -> Retained<Self> {
        let this = Self::alloc().set_ivars(StreamDelegateIvars);
        unsafe { msg_send![super(this), init] }
    }
}

#[derive(Default)]
struct StreamOutputIvars {
    callback: std::cell::Cell<*mut c_void>,
}

define_class!(
    #[unsafe(super = NSObject)]
    #[ivars = StreamOutputIvars]
    #[name = "GPUIStreamOutput"]
    struct GPUIStreamOutput;

    unsafe impl NSObjectProtocol for GPUIStreamOutput {}

    impl GPUIStreamOutput {
        #[unsafe(method(stream:didOutputSampleBuffer:ofType:))]
        fn stream_did_output_sample_buffer_of_type(
            &self,
            _stream: *mut AnyObject,
            sample_buffer: *mut AnyObject,
            buffer_type: NSInteger,
        ) {
            if buffer_type != SCStreamOutputTypeScreen {
                return;
            }

            let cb_ptr = self.ivars().callback.get();
            if cb_ptr.is_null() {
                return;
            }

            unsafe {
                let sample_buffer = sample_buffer as CMSampleBufferRef;
                let sample_buffer = CMSampleBuffer::wrap_under_get_rule(sample_buffer);
                if let Some(buffer) = sample_buffer.image_buffer() {
                    let callback: Box<Box<dyn Fn(ScreenCaptureFrame)>> =
                        Box::from_raw(cb_ptr as *mut _);
                    callback(ScreenCaptureFrame(buffer));
                    std::mem::forget(callback);
                }
            }
        }
    }
);

impl GPUIStreamOutput {
    fn new(callback: *mut c_void) -> Retained<Self> {
        let this = Self::alloc().set_ivars(StreamOutputIvars {
            callback: std::cell::Cell::new(callback),
        });
        unsafe { msg_send![super(this), init] }
    }
}

impl ScreenCaptureSource for MacScreenCaptureSource {
    fn metadata(&self) -> Result<SourceMetadata> {
        let (display_id, size) = unsafe {
            let display_id: CGDirectDisplayID = msg_send![self.sc_display, displayID];
            let display_mode_ref = CGDisplayCopyDisplayMode(display_id);
            let width = CGDisplayModeGetPixelWidth(display_mode_ref);
            let height = CGDisplayModeGetPixelHeight(display_mode_ref);
            CGDisplayModeRelease(display_mode_ref);

            (
                display_id,
                size(DevicePixels(width as i32), DevicePixels(height as i32)),
            )
        };
        let (label, is_main) = self
            .meta
            .clone()
            .map(|meta| (meta.label, meta.is_main))
            .unzip();

        Ok(SourceMetadata {
            id: display_id as u64,
            label,
            is_main,
            resolution: size,
        })
    }

    fn stream(
        &self,
        _foreground_executor: &ForegroundExecutor,
        frame_callback: Box<dyn Fn(ScreenCaptureFrame) + Send>,
    ) -> oneshot::Receiver<Result<Box<dyn ScreenCaptureStream>>> {
        unsafe {
            let stream_alloc: *mut AnyObject = msg_send![lookup_class(c"SCStream"), alloc];
            let filter_alloc: *mut AnyObject = msg_send![lookup_class(c"SCContentFilter"), alloc];
            let configuration_alloc: *mut AnyObject =
                msg_send![lookup_class(c"SCStreamConfiguration"), alloc];

            let excluded_windows: *mut AnyObject = msg_send![lookup_class(c"NSArray"), array];
            let filter: *mut AnyObject = msg_send![
                filter_alloc,
                initWithDisplay: self.sc_display,
                excludingWindows: excluded_windows
            ];
            let configuration: *mut AnyObject = msg_send![configuration_alloc, init];
            let _: *mut AnyObject = msg_send![configuration, setScalesToFit: true];
            let _: *mut AnyObject = msg_send![configuration, setPixelFormat: 0x42475241_u32];

            let delegate = GPUIStreamDelegate::new();
            let callback_ptr = Box::into_raw(Box::new(frame_callback)) as *mut c_void;
            let output = GPUIStreamOutput::new(callback_ptr);

            let meta = self.metadata().unwrap();
            let _: *mut AnyObject =
                msg_send![configuration, setWidth: meta.resolution.width.0 as i64];
            let _: *mut AnyObject =
                msg_send![configuration, setHeight: meta.resolution.height.0 as i64];
            let stream: *mut AnyObject = msg_send![
                stream_alloc,
                initWithFilter: filter,
                configuration: configuration,
                delegate: &*delegate
            ];

            let (tx, rx) = oneshot::channel();

            let mut error: *mut AnyObject = ptr::null_mut();
            let null_q: *mut c_void = ptr::null_mut();
            let _: () = msg_send![
                stream,
                addStreamOutput: &*output,
                type: SCStreamOutputTypeScreen,
                sampleHandlerQueue: null_q,
                error: &mut error
            ];
            if !error.is_null() {
                let message: *mut AnyObject = msg_send![error, localizedDescription];
                let msg = ns_string_to_str(message);
                tx.send(Err(anyhow!("failed to add stream output {msg}")))
                    .ok();
                return rx;
            }

            // Retain stream/output so they outlive this scope; the returned
            // MacScreenCaptureStream owns them and releases on drop.
            let stream_owned: *mut AnyObject = msg_send![stream, retain];
            let output_owned: *mut AnyObject = msg_send![&*output, retain];
            // Keep the delegate alive on the heap so SCStream's weak reference stays valid.
            let _delegate_keep = Box::leak(Box::new(delegate));

            let tx = Rc::new(RefCell::new(Some(tx)));
            let handler = RcBlock::new({
                let tx = tx.clone();
                move |error: *mut AnyObject| {
                    let result = if error.is_null() {
                        let stream = MacScreenCaptureStream {
                            meta: meta.clone(),
                            sc_stream: stream_owned,
                            sc_stream_output: output_owned,
                        };
                        Ok(Box::new(stream) as Box<dyn ScreenCaptureStream>)
                    } else {
                        let message: *mut AnyObject =
                            unsafe { msg_send![error, localizedDescription] };
                        let msg = unsafe { ns_string_to_str(message) };
                        Err(anyhow!("failed to start screen capture stream {msg}"))
                    };
                    if let Some(tx) = tx.borrow_mut().take() {
                        tx.send(result).ok();
                    }
                }
            });
            let _: () = msg_send![stream, startCaptureWithCompletionHandler: &*handler];
            rx
        }
    }
}

impl Drop for MacScreenCaptureSource {
    fn drop(&mut self) {
        unsafe {
            let _: () = msg_send![self.sc_display, release];
        }
    }
}

impl ScreenCaptureStream for MacScreenCaptureStream {
    fn metadata(&self) -> Result<SourceMetadata> {
        Ok(self.meta.clone())
    }
}

impl Drop for MacScreenCaptureStream {
    fn drop(&mut self) {
        unsafe {
            let mut error: *mut AnyObject = ptr::null_mut();
            let _: () = msg_send![
                self.sc_stream,
                removeStreamOutput: self.sc_stream_output,
                type: SCStreamOutputTypeScreen,
                error: &mut error
            ];
            if !error.is_null() {
                let message: *mut AnyObject = msg_send![error, localizedDescription];
                log::error!(
                    "failed to remove stream output {}",
                    ns_string_to_str(message)
                );
            }

            let handler = RcBlock::new(move |error: *mut AnyObject| {
                if !error.is_null() {
                    let message: *mut AnyObject = unsafe { msg_send![error, localizedDescription] };
                    log::error!("failed to stop screen capture stream {}", unsafe {
                        ns_string_to_str(message)
                    });
                }
            });
            let _: () = msg_send![self.sc_stream, stopCaptureWithCompletionHandler: &*handler];
            let _: () = msg_send![self.sc_stream, release];
            let _: () = msg_send![self.sc_stream_output, release];
        }
    }
}

#[derive(Clone)]
struct ScreenMeta {
    label: SharedString,
    // Is this the screen with menu bar?
    is_main: bool,
}

unsafe fn screen_id_to_human_label() -> HashMap<CGDirectDisplayID, ScreenMeta> {
    unsafe {
        let screens: *mut AnyObject = msg_send![lookup_class(c"NSScreen"), screens];
        let count: usize = msg_send![screens, count];
        let mut map = HashMap::default();
        let screen_number_key = NSString::from_str("NSScreenNumber");
        for i in 0..count {
            let screen: *mut AnyObject = msg_send![screens, objectAtIndex: i];
            let device_desc: *mut AnyObject = msg_send![screen, deviceDescription];
            if device_desc.is_null() {
                continue;
            }

            let nsnumber: *mut AnyObject =
                msg_send![device_desc, objectForKey: &*screen_number_key];
            if nsnumber.is_null() {
                continue;
            }

            let screen_id: u32 = msg_send![nsnumber, unsignedIntValue];

            let name: *mut AnyObject = msg_send![screen, localizedName];
            if !name.is_null() {
                let label = ns_string_to_str(name);
                map.insert(
                    screen_id,
                    ScreenMeta {
                        label: label.into(),
                        is_main: i == 0,
                    },
                );
            }
        }
        map
    }
}

pub(crate) fn get_sources() -> oneshot::Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>> {
    unsafe {
        let (tx, rx) = oneshot::channel();
        let tx = Rc::new(RefCell::new(Some(tx)));
        let screen_id_to_label = screen_id_to_human_label();
        let block = RcBlock::new(
            move |shareable_content: *mut AnyObject, error: *mut AnyObject| {
                let Some(tx) = tx.borrow_mut().take() else {
                    return;
                };

                let result = if error.is_null() {
                    let displays: *mut AnyObject =
                        unsafe { msg_send![shareable_content, displays] };
                    let count: usize = unsafe { msg_send![displays, count] };
                    let mut result = Vec::new();
                    for i in 0..count {
                        let display: *mut AnyObject =
                            unsafe { msg_send![displays, objectAtIndex: i] };
                        let id: CGDirectDisplayID = unsafe { msg_send![display, displayID] };
                        let meta = screen_id_to_label.get(&id).cloned();
                        let retained: *mut AnyObject = unsafe { msg_send![display, retain] };
                        let source = MacScreenCaptureSource {
                            sc_display: retained,
                            meta,
                        };
                        result.push(Rc::new(source) as Rc<dyn ScreenCaptureSource>);
                    }
                    Ok(result)
                } else {
                    let msg: *mut AnyObject = unsafe { msg_send![error, localizedDescription] };
                    Err(anyhow!("Screen share failed: {}", unsafe {
                        ns_string_to_str(msg)
                    }))
                };
                tx.send(result).ok();
            },
        );

        let _: () = msg_send![
            lookup_class(c"SCShareableContent"),
            getShareableContentExcludingDesktopWindows: true,
            onScreenWindowsOnly: true,
            completionHandler: &*block
        ];
        rx
    }
}

use crate::media_capture::{
    CaptureBackend, CaptureConfig, CaptureDeviceInfo, CaptureDeviceKind, CaptureSession,
    CaptureSessionState, DeviceEnumerator, FrameCallback,
};
use std::sync::atomic::{AtomicU64, Ordering};

pub struct MacScreenCaptureBackend;

impl MacScreenCaptureBackend {
    pub fn new() -> Self {
        Self
    }
}

impl DeviceEnumerator for MacScreenCaptureBackend {
    fn devices(&self, kind: CaptureDeviceKind) -> Result<Vec<CaptureDeviceInfo>> {
        match kind {
            CaptureDeviceKind::Screen => {
                let mut count: u32 = 0;
                let result = unsafe { CGGetActiveDisplayList(0, ptr::null_mut(), &mut count) };
                if result != 0 {
                    return Ok(Vec::new());
                }
                let mut ids = vec![0u32; count as usize];
                let result = unsafe { CGGetActiveDisplayList(count, ids.as_mut_ptr(), &mut count) };
                if result != 0 {
                    return Ok(Vec::new());
                }
                ids.truncate(count as usize);
                let labels = unsafe { screen_id_to_human_label() };
                let devices = ids
                    .into_iter()
                    .map(|id| {
                        let _bounds = unsafe { CGDisplayBounds(id) };
                        let name = labels
                            .get(&id)
                            .map(|m| m.label.to_string())
                            .unwrap_or_else(|| format!("Display {}", id));
                        CaptureDeviceInfo {
                            id: id.to_string(),
                            name,
                            kind: CaptureDeviceKind::Screen,
                            is_available: true,
                        }
                    })
                    .collect();
                Ok(devices)
            }
            CaptureDeviceKind::Window => Ok(Vec::new()),
            _ => Ok(Vec::new()),
        }
    }
}

impl CaptureBackend for MacScreenCaptureBackend {
    fn create_session(&self, config: &CaptureConfig) -> Result<Box<dyn CaptureSession>> {
        Ok(Box::new(MacScreenCaptureSession::new(config.clone())))
    }
}

struct MacScreenCaptureSession {
    config: CaptureConfig,
    state: CaptureSessionState,
    dropped: AtomicU64,
    latency_ms: AtomicU64,
    callback: Option<FrameCallback>,
}

impl MacScreenCaptureSession {
    fn new(config: CaptureConfig) -> Self {
        Self {
            config,
            state: CaptureSessionState::Idle,
            dropped: AtomicU64::new(0),
            latency_ms: AtomicU64::new(0),
            callback: None,
        }
    }
}

impl CaptureSession for MacScreenCaptureSession {
    fn start(&mut self, config: CaptureConfig, callback: FrameCallback) -> Result<()> {
        self.config = config;
        self.state = CaptureSessionState::Starting;
        self.callback = Some(callback);
        self.state = CaptureSessionState::Running;
        Ok(())
    }

    fn pause(&mut self) -> Result<()> {
        self.state = CaptureSessionState::Paused;
        Ok(())
    }

    fn resume(&mut self) -> Result<()> {
        self.state = CaptureSessionState::Running;
        Ok(())
    }

    fn stop(&mut self) -> Result<()> {
        self.state = CaptureSessionState::Stopped;
        self.callback = None;
        Ok(())
    }

    fn state(&self) -> CaptureSessionState {
        self.state
    }

    fn dropped_frame_count(&self) -> u64 {
        self.dropped.load(Ordering::Relaxed)
    }

    fn latency_ms(&self) -> u64 {
        self.latency_ms.load(Ordering::Relaxed)
    }
}