mediaway-device 0.1.8

Device capture: camera, microphone, screen/window, audio playback + hotplug, with Windows/Linux/Web backends as #[cfg]-gated modules
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//! iOS screen capture via `ReplayKit`: `RPScreenRecorder` in-app capture
//! ([`AppleScreenCapture`]) **and** a Broadcast Upload Extension push-sink
//! ([`AppleBroadcastExtensionCapture`]). See
//! [ADR-0004](adr/apple/0004-replaykit-ios-inapp-screen-capture.md).
//!
//! Both entry points share one dispatch routine, [`classify_and_queue_sample_buffer`], and the
//! same `CVPixelBuffer` extraction ([`super::pixel::extract_nv12`]) `apple::camera`/
//! `apple::screencapturekit` already use — no per-entry-point-duplicated frame extraction.
//!
//! Both entry points implement three traits — [`DesktopVideoCapture`] (screen),
//! [`DesktopAudioCapture`] (app audio — "what this app/broadcast is playing"), and
//! [`crate::audio::AudioCapture`] (microphone audio) — since a single `ReplayKit` stream
//! delivers all three tagged by [`RPSampleBufferType`]. See ADR-0004 § Audio inclusion for why
//! `AudioApp`/`AudioMic` map to these two different traits rather than one.
//!
//! `AppleBroadcastExtensionCapture` has **no OS session of its own** — unlike every other
//! backend in this crate, it does not `open()` an OS capture session; the host project's own
//! `.appex` extension target (a genuine Xcode project-structure requirement this crate cannot
//! build or ship) owns the real `RPBroadcastSampleHandler` lifecycle and calls
//! [`AppleBroadcastExtensionCapture::push_sample_buffer`] once per
//! `processSampleBuffer:withType:` invocation — see ADR-0004 § Host-extension contract for the
//! full, real Swift/Objective-C contract this crate cannot fulfill itself.
//!
//! **Zero compile verification, zero real-hardware verification** — see the crate's `apple-ios`
//! CI job and ADR-0004 § "A real, honest verification-gap note" (this domain's ceiling is lower
//! than every other backend's: no CI configuration in this workspace can exercise a real
//! `.appex`/`RPBroadcastSampleHandler` integration).

#![allow(unsafe_code)]

use std::collections::VecDeque;
use std::ffi::c_char;
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::mpsc::sync_channel;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::CaptureError;
use crate::audio::AudioCapture;
use crate::desktop::DesktopAudioCapture;
use crate::desktop::DesktopVideoCapture;
use block2::RcBlock;
use mediaway_common::{
    AudioFrame, Bytes, CodecKind, PixelFormat, Rational, SampleFormat, StreamInfo, VideoFrame,
    VideoFrameStorage, VideoGeometry,
};
use objc2::rc::Retained;
use objc2_core_audio_types::{kAudioFormatFlagIsFloat, kAudioFormatFlagIsNonInterleaved};
use objc2_core_media::{CMAudioFormatDescription, CMSampleBuffer};
use objc2_foundation::NSError;
use objc2_replay_kit::{RPSampleBufferType, RPScreenRecorder};

/// Bounded, drop-oldest queue depth per data kind — mirrors every other backend in this crate.
const QUEUE_CAP: usize = 4;

/// How long `open()`/`close()` wait for `RPScreenRecorder`'s completion handlers — no real
/// device this session to tune against (ADR-0004 § Open questions, shared with ADR-0003's own
/// completion-handler timeout question).
const COMPLETION_TIMEOUT: Duration = Duration::from_secs(10);

struct FrameQueues {
    video: Mutex<VecDeque<VideoFrame>>,
    app_audio: Mutex<VecDeque<AudioFrame>>,
    mic_audio: Mutex<VecDeque<AudioFrame>>,
}

impl FrameQueues {
    const fn new() -> Self {
        Self {
            video: Mutex::new(VecDeque::new()),
            app_audio: Mutex::new(VecDeque::new()),
            mic_audio: Mutex::new(VecDeque::new()),
        }
    }
}

struct StreamInfos {
    /// Set once, from the first frame of its kind — video geometry / audio format do not
    /// change mid-session in practice, so "first frame wins" avoids the awkward lifetime shape
    /// a `Mutex<StreamInfo>` would force on `stream_info()`'s `&StreamInfo` return type.
    video: std::sync::OnceLock<StreamInfo>,
    app_audio: std::sync::OnceLock<StreamInfo>,
    mic_audio: std::sync::OnceLock<StreamInfo>,
}

impl StreamInfos {
    const fn new() -> Self {
        Self {
            video: std::sync::OnceLock::new(),
            app_audio: std::sync::OnceLock::new(),
            mic_audio: std::sync::OnceLock::new(),
        }
    }
}

/// Placeholder returned by `stream_info()` before the first frame of a kind has arrived, or
/// after the session is closed — mirrors every other backend's `closed_video_info` shape.
fn unknown_video_info() -> &'static StreamInfo {
    use std::sync::OnceLock;
    static INFO: OnceLock<StreamInfo> = OnceLock::new();
    INFO.get_or_init(|| StreamInfo::Video {
        id: 0,
        codec: CodecKind::RawVideo,
        time_base: Rational::new(1, 30),
        geometry: VideoGeometry {
            width: 0,
            height: 0,
        },
        extra_data: Bytes::new(),
    })
}

/// Placeholder returned by `stream_info()` before the first frame of a kind has arrived, or
/// after the session is closed — mirrors every other backend's `closed_audio_info` shape.
fn unknown_audio_info() -> &'static StreamInfo {
    use std::sync::OnceLock;
    static INFO: OnceLock<StreamInfo> = OnceLock::new();
    INFO.get_or_init(|| StreamInfo::Audio {
        id: 0,
        codec: CodecKind::RawAudio,
        time_base: Rational::new(1, 48_000),
        sample_rate: 0,
        channels: 0,
        extra_data: Bytes::new(),
    })
}

/// Dispatches one `(CMSampleBuffer, RPSampleBufferType)` delivery to the right queue —
/// [`AppleScreenCapture`]'s in-app `block2` closure and
/// [`AppleBroadcastExtensionCapture::push_sample_buffer`] both call this, per ADR-0004's "one
/// shared dispatch helper" decision.
///
/// # Safety
///
/// `sample_buffer` must be a valid, non-null `CMSampleBuffer` for the duration of this call —
/// the caller's own `ReplayKit` callback/extension contract.
unsafe fn classify_and_queue_sample_buffer(
    queues: &FrameQueues,
    infos: &StreamInfos,
    next_pts: &AtomicI64,
    sample_buffer: &CMSampleBuffer,
    kind: RPSampleBufferType,
) {
    match kind {
        RPSampleBufferType::Video => {
            // SAFETY: caller's contract (this fn's own `# Safety`).
            let Some((data, width, height)) =
                (unsafe { super::pixel::extract_nv12(sample_buffer) })
            else {
                return;
            };
            let pts = next_pts.fetch_add(1, Ordering::Relaxed);
            let _ = infos.video.set(StreamInfo::Video {
                id: 0,
                codec: CodecKind::RawVideo,
                time_base: Rational::new(1, 30),
                geometry: VideoGeometry { width, height },
                extra_data: Bytes::new(),
            });
            let frame = VideoFrame {
                pts,
                duration: 1,
                width,
                height,
                format: PixelFormat::Nv12,
                storage: VideoFrameStorage::Cpu { data },
            };
            push_bounded(&queues.video, frame);
        }
        RPSampleBufferType::AudioApp => {
            // SAFETY: caller's contract.
            if let Some(frame) = unsafe { extract_pcm(sample_buffer) } {
                let _ = infos.app_audio.set(audio_stream_info(&frame));
                push_bounded(&queues.app_audio, frame);
            }
        }
        RPSampleBufferType::AudioMic => {
            // SAFETY: caller's contract.
            if let Some(frame) = unsafe { extract_pcm(sample_buffer) } {
                let _ = infos.mic_audio.set(audio_stream_info(&frame));
                push_bounded(&queues.mic_audio, frame);
            }
        }
        _ => {}
    }
}

fn audio_stream_info(frame: &AudioFrame) -> StreamInfo {
    StreamInfo::Audio {
        id: 0,
        codec: CodecKind::RawAudio,
        time_base: Rational::new(1, frame.sample_rate.max(1)),
        sample_rate: frame.sample_rate,
        channels: frame.channels,
        extra_data: Bytes::new(),
    }
}

fn push_bounded<T>(queue: &Mutex<VecDeque<T>>, item: T) {
    if let Ok(mut q) = queue.lock() {
        if q.len() >= QUEUE_CAP {
            let _ = q.pop_front();
        }
        q.push_back(item);
    }
}

/// Extract one interleaved `F32` PCM [`AudioFrame`] from `sample_buffer`'s `CMBlockBuffer` +
/// `CMAudioFormatDescription`. Returns `None` for anything not provably interleaved `F32` PCM —
/// never silently mis-reads a layout it didn't verify.
///
/// # Safety
///
/// `sample_buffer` must be a valid, non-null `CMSampleBuffer` for the duration of this call.
unsafe fn extract_pcm(sample_buffer: &CMSampleBuffer) -> Option<AudioFrame> {
    // SAFETY: caller's contract.
    let format_description = unsafe { sample_buffer.format_description() }?;
    let audio_description = format_description
        .downcast::<CMAudioFormatDescription>()
        .ok()?;
    // `CMAudioFormatDescription` has no inherent `stream_basic_description` method — the real API
    // is the free function `CMAudioFormatDescriptionGetStreamBasicDescription`.
    //
    // SAFETY: `audio_description` is a valid, just-downcast format description.
    let asbd_ptr = unsafe {
        objc2_core_media::CMAudioFormatDescriptionGetStreamBasicDescription(&audio_description)
    };
    if asbd_ptr.is_null() {
        return None;
    }
    // SAFETY: `asbd_ptr` is non-null (checked above); `CMAudioFormatDescriptionGetStreamBasicDescription`'s
    // documented contract guarantees it points to a valid, readable `AudioStreamBasicDescription`
    // for the format description's own lifetime, which outlives this read.
    let asbd = unsafe { *asbd_ptr };
    if asbd.mFormatFlags & kAudioFormatFlagIsFloat == 0 {
        return None;
    }
    if asbd.mFormatFlags & kAudioFormatFlagIsNonInterleaved != 0 {
        return None;
    }
    let channels = asbd.mChannelsPerFrame;
    if channels == 0 || asbd.mSampleRate <= 0.0 {
        return None;
    }

    // SAFETY: caller's contract.
    let block_buffer = unsafe { sample_buffer.data_buffer() }?;
    // SAFETY: `block_buffer` is a valid, just-obtained `CMBlockBuffer`.
    let total_len = unsafe { block_buffer.data_length() };
    let mut length_at_offset: usize = 0;
    let mut data_ptr: *mut c_char = std::ptr::null_mut();
    // `data_pointer`'s real signature takes raw out-parameter pointers (`*mut usize`,
    // `*mut *mut c_char`), not `Option<&mut T>` — a null pointer means "don't care" for
    // `total_length_out`.
    //
    // SAFETY: `block_buffer` is valid; `length_at_offset`/`data_ptr` are valid local out-params,
    // each a valid pointer for the call's duration.
    let status = unsafe {
        block_buffer.data_pointer(
            0,
            &raw mut length_at_offset,
            std::ptr::null_mut(),
            &raw mut data_ptr,
        )
    };
    if status != 0 || data_ptr.is_null() || length_at_offset != total_len {
        // Not a single contiguous range — never silently mis-read a discontiguous buffer.
        return None;
    }
    // SAFETY: `data_ptr` is non-null and readable for `total_len` bytes — the documented
    // `CMBlockBufferGetDataPointer` contract, checked (`length_at_offset == total_len`) above.
    let bytes = unsafe { std::slice::from_raw_parts(data_ptr.cast::<u8>(), total_len) };

    let bytes_per_frame = 4usize.saturating_mul(channels as usize);
    let num_frames = total_len.checked_div(bytes_per_frame).unwrap_or(0);

    // `asbd.mSampleRate > 0.0` is checked above; real audio sample rates (e.g. 44100/48000) are
    // always small positive integers, exact in `u32`.
    #[allow(
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss,
        reason = "mSampleRate > 0.0 checked above; real sample rates are small positive integers"
    )]
    let sample_rate = asbd.mSampleRate as u32;

    Some(AudioFrame {
        pts: 0,
        duration: u64::try_from(num_frames).unwrap_or(0),
        sample_rate,
        channels: u16::try_from(channels).unwrap_or(0),
        format: SampleFormat::F32,
        data: Bytes::copy_from_slice(bytes),
    })
}

// ───────────────────────── AppleScreenCapture (in-app, RPScreenRecorder) ─────────────────────────

struct InAppSession {
    queues: Arc<FrameQueues>,
    infos: Arc<StreamInfos>,
    recorder: Retained<RPScreenRecorder>,
    // Kept alive for the whole session — `startCaptureWithHandler` retains its own reference,
    // but this crate's own convention (mirrors every other delegate/callback backend) is to
    // hold the block alive explicitly too.
    //
    // `RcBlock<F>` takes exactly one generic parameter — the `dyn Fn(...)` block signature itself
    // — not a separate lifetime/fn-pointer/marker-trait triple.
    _capture_handler:
        RcBlock<dyn Fn(std::ptr::NonNull<CMSampleBuffer>, RPSampleBufferType, *mut NSError)>,
}

/// iOS in-app screen capture (`RPScreenRecorder`), including app audio and microphone audio.
/// See module docs for the trait split.
pub struct AppleScreenCapture {
    inner: Option<InAppSession>,
}

impl AppleScreenCapture {
    /// Open `RPScreenRecorder` in-app capture. Always captures video + app audio + microphone
    /// audio (confirmed with the user — ADR-0004 § Decisions confirmed).
    ///
    /// # Errors
    ///
    /// Returns [`CaptureError::Backend`] on `ReplayKit` failure or completion-handler timeout.
    pub fn open() -> Result<Self, CaptureError> {
        // SAFETY: plain, always-safe-to-call singleton accessor.
        let recorder = unsafe { RPScreenRecorder::sharedRecorder() };
        // SAFETY: `recorder` is the valid shared singleton.
        unsafe { recorder.setMicrophoneEnabled(true) };

        let queues = Arc::new(FrameQueues::new());
        let infos = Arc::new(StreamInfos::new());
        let next_pts = Arc::new(AtomicI64::new(0));
        // clone: capture handler closure needs its own strong refs to push frames
        let queues_cb = Arc::clone(&queues);
        let infos_cb = Arc::clone(&infos);
        let next_pts_cb = Arc::clone(&next_pts);

        let capture_handler: RcBlock<
            dyn Fn(std::ptr::NonNull<CMSampleBuffer>, RPSampleBufferType, *mut NSError),
        > = RcBlock::new(
            move |sample_buffer: std::ptr::NonNull<CMSampleBuffer>,
                  kind: RPSampleBufferType,
                  _error: *mut NSError| {
                // SAFETY: `sample_buffer` is valid for the duration of this callback — Apple's
                // documented `startCaptureWithHandler` contract.
                let sample_buffer = unsafe { sample_buffer.as_ref() };
                // SAFETY: same contract.
                unsafe {
                    classify_and_queue_sample_buffer(
                        &queues_cb,
                        &infos_cb,
                        &next_pts_cb,
                        sample_buffer,
                        kind,
                    );
                }
            },
        );

        start_in_app_capture(&recorder, &capture_handler)?;

        Ok(Self {
            inner: Some(InAppSession {
                queues,
                infos,
                recorder,
                _capture_handler: capture_handler,
            }),
        })
    }

    fn close_inner(&mut self) -> Result<(), CaptureError> {
        let Some(session) = self.inner.take() else {
            return Ok(());
        };
        stop_in_app_capture(&session.recorder)
    }
}

impl DesktopVideoCapture for AppleScreenCapture {
    /// Reflects the geometry of the most recently arrived video frame — `unknown_video_info()`
    /// (zeroed placeholder) until the first frame arrives, since `ReplayKit`'s async capture
    /// handler doesn't report format synchronously from `open()` the way most other backends'
    /// setup does.
    fn stream_info(&self) -> &StreamInfo {
        #[allow(
            clippy::option_if_let_else,
            reason = "map_or_else forces 'static vs 'self lifetime clash"
        )]
        if let Some(session) = self.inner.as_ref() {
            session
                .infos
                .video
                .get()
                .unwrap_or_else(|| unknown_video_info())
        } else {
            unknown_video_info()
        }
    }

    fn poll_frame(&mut self) -> Result<Option<VideoFrame>, CaptureError> {
        let Some(session) = self.inner.as_ref() else {
            return Err(CaptureError::Closed);
        };
        let mut q = session
            .queues
            .video
            .lock()
            .map_err(|_| CaptureError::Backend)?;
        Ok(q.pop_front())
    }

    fn release_frame(&mut self) -> Result<(), CaptureError> {
        if self.inner.is_none() {
            return Err(CaptureError::Closed);
        }
        Ok(())
    }

    fn close(&mut self) -> Result<(), CaptureError> {
        self.close_inner()
    }
}

impl DesktopAudioCapture for AppleScreenCapture {
    /// Reflects the format of the most recently arrived app-audio frame — see
    /// [`DesktopVideoCapture::stream_info`]'s doc comment for the same "zeroed until first
    /// frame" reasoning.
    fn stream_info(&self) -> &StreamInfo {
        #[allow(
            clippy::option_if_let_else,
            reason = "map_or_else forces 'static vs 'self lifetime clash"
        )]
        if let Some(session) = self.inner.as_ref() {
            session
                .infos
                .app_audio
                .get()
                .unwrap_or_else(|| unknown_audio_info())
        } else {
            unknown_audio_info()
        }
    }

    fn poll_frame(&mut self) -> Result<Option<AudioFrame>, CaptureError> {
        let Some(session) = self.inner.as_ref() else {
            return Err(CaptureError::Closed);
        };
        let mut q = session
            .queues
            .app_audio
            .lock()
            .map_err(|_| CaptureError::Backend)?;
        Ok(q.pop_front())
    }

    fn close(&mut self) -> Result<(), CaptureError> {
        self.close_inner()
    }
}

impl AudioCapture for AppleScreenCapture {
    /// Reflects the format of the most recently arrived microphone-audio frame — see
    /// [`DesktopVideoCapture::stream_info`]'s doc comment for the same "zeroed until first
    /// frame" reasoning.
    fn stream_info(&self) -> &StreamInfo {
        #[allow(
            clippy::option_if_let_else,
            reason = "map_or_else forces 'static vs 'self lifetime clash"
        )]
        if let Some(session) = self.inner.as_ref() {
            session
                .infos
                .mic_audio
                .get()
                .unwrap_or_else(|| unknown_audio_info())
        } else {
            unknown_audio_info()
        }
    }

    fn poll_frame(&mut self) -> Result<Option<AudioFrame>, CaptureError> {
        let Some(session) = self.inner.as_ref() else {
            return Err(CaptureError::Closed);
        };
        let mut q = session
            .queues
            .mic_audio
            .lock()
            .map_err(|_| CaptureError::Backend)?;
        Ok(q.pop_front())
    }

    fn close(&mut self) -> Result<(), CaptureError> {
        self.close_inner()
    }
}

impl Drop for AppleScreenCapture {
    fn drop(&mut self) {
        let _ = self.close_inner();
    }
}

fn start_in_app_capture(
    recorder: &RPScreenRecorder,
    capture_handler: &RcBlock<
        dyn Fn(std::ptr::NonNull<CMSampleBuffer>, RPSampleBufferType, *mut NSError),
    >,
) -> Result<(), CaptureError> {
    let (tx, rx) = sync_channel::<Result<(), CaptureError>>(1);
    let tx = Arc::new(Mutex::new(Some(tx)));
    let completion: RcBlock<dyn Fn(*mut NSError)> = RcBlock::new(move |error: *mut NSError| {
        let result = if error.is_null() {
            Ok(())
        } else {
            Err(CaptureError::Backend)
        };
        if let Ok(mut guard) = tx.lock()
            && let Some(tx) = guard.take()
        {
            let _ = tx.send(result);
        }
    });
    // SAFETY: `recorder` is the valid shared singleton; `capture_handler`/`completion` are both
    // valid, kept alive for at least the duration of this call (`capture_handler` for the whole
    // session via the caller's own field, `completion` for this call via this local binding).
    unsafe {
        recorder
            .startCaptureWithHandler_completionHandler(Some(capture_handler), Some(&completion));
    }
    rx.recv_timeout(COMPLETION_TIMEOUT)
        .map_err(|_| CaptureError::Backend)?
}

fn stop_in_app_capture(recorder: &RPScreenRecorder) -> Result<(), CaptureError> {
    let (tx, rx) = sync_channel::<Result<(), CaptureError>>(1);
    let tx = Arc::new(Mutex::new(Some(tx)));
    // `stopCaptureWithHandler`'s completion handler is `Option<&block2::DynBlock<dyn Fn(*mut
    // NSError)>>` per the real generated signature — the same plain (non-`Send`/`Sync`) block
    // shape `startCaptureWithHandler_completionHandler`'s own completion handler uses; this
    // crate's `objc2`/`block2` version has no `Send`/`Sync`-bounded block variant at all.
    let handler: RcBlock<dyn Fn(*mut NSError)> = RcBlock::new(move |error: *mut NSError| {
        let result = if error.is_null() {
            Ok(())
        } else {
            Err(CaptureError::Backend)
        };
        if let Ok(mut guard) = tx.lock()
            && let Some(tx) = guard.take()
        {
            let _ = tx.send(result);
        }
    });
    // SAFETY: `recorder` is the valid shared singleton; `handler` is valid for this call.
    unsafe { recorder.stopCaptureWithHandler(Some(&handler)) };
    rx.recv_timeout(COMPLETION_TIMEOUT)
        .map_err(|_| CaptureError::Backend)?
}

// ─────────────────── AppleBroadcastExtensionCapture (Broadcast Upload Extension sink) ───────────────────

struct ExtensionSession {
    queues: Arc<FrameQueues>,
    infos: Arc<StreamInfos>,
    next_pts: AtomicI64,
}

/// Push-in / pull-out sink for a Broadcast Upload Extension's `RPBroadcastSampleHandler`.
///
/// See module docs and ADR-0004 § Host-extension contract — this type has **no OS session of
/// its own**; the host extension's own real Swift/Objective-C code (this crate cannot write it)
/// calls [`Self::push_sample_buffer`] once per `processSampleBuffer:withType:` invocation.
pub struct AppleBroadcastExtensionCapture {
    inner: Option<ExtensionSession>,
}

impl Default for AppleBroadcastExtensionCapture {
    fn default() -> Self {
        Self::new()
    }
}

impl AppleBroadcastExtensionCapture {
    /// Allocate the shared queues. Does **not** start any OS session — see module docs.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Some(ExtensionSession {
                queues: Arc::new(FrameQueues::new()),
                infos: Arc::new(StreamInfos::new()),
                next_pts: AtomicI64::new(0),
            }),
        }
    }

    /// Hand one sample buffer captured by the host extension's `RPBroadcastSampleHandler` to
    /// this sink. Called from the future `mediaway-ffi` C ABI shim described in ADR-0004 §
    /// FFI boundary — not designed in this crate.
    ///
    /// # Errors
    ///
    /// Returns [`CaptureError::Closed`] if [`Self::close`] was already called.
    pub fn push_sample_buffer(
        &self,
        sample_buffer: &CMSampleBuffer,
        kind: RPSampleBufferType,
    ) -> Result<(), CaptureError> {
        let Some(session) = self.inner.as_ref() else {
            return Err(CaptureError::Closed);
        };
        // SAFETY: caller's contract (this fn's own doc comment) — `sample_buffer` is valid for
        // the duration of the host extension's `processSampleBuffer:withType:` call this
        // forwards.
        unsafe {
            classify_and_queue_sample_buffer(
                &session.queues,
                &session.infos,
                &session.next_pts,
                sample_buffer,
                kind,
            );
        }
        Ok(())
    }

    fn close_inner(&mut self) {
        // No ReplayKit-related teardown — the host extension's own `broadcastFinished`/
        // `finishBroadcastWithError` handlers own that (ADR-0004 § Host-extension contract
        // step 6). This just drops the queues.
        self.inner = None;
    }
}

impl DesktopVideoCapture for AppleBroadcastExtensionCapture {
    /// Reflects the geometry of the most recently pushed video frame — zeroed placeholder until
    /// the host extension's first `push_sample_buffer(_, RPSampleBufferType::Video)` call.
    fn stream_info(&self) -> &StreamInfo {
        #[allow(
            clippy::option_if_let_else,
            reason = "map_or_else forces 'static vs 'self lifetime clash"
        )]
        if let Some(session) = self.inner.as_ref() {
            session
                .infos
                .video
                .get()
                .unwrap_or_else(|| unknown_video_info())
        } else {
            unknown_video_info()
        }
    }

    fn poll_frame(&mut self) -> Result<Option<VideoFrame>, CaptureError> {
        let Some(session) = self.inner.as_ref() else {
            return Err(CaptureError::Closed);
        };
        let mut q = session
            .queues
            .video
            .lock()
            .map_err(|_| CaptureError::Backend)?;
        Ok(q.pop_front())
    }

    fn release_frame(&mut self) -> Result<(), CaptureError> {
        if self.inner.is_none() {
            return Err(CaptureError::Closed);
        }
        Ok(())
    }

    fn close(&mut self) -> Result<(), CaptureError> {
        self.close_inner();
        Ok(())
    }
}

impl DesktopAudioCapture for AppleBroadcastExtensionCapture {
    /// Reflects the format of the most recently pushed app-audio frame — see
    /// [`DesktopVideoCapture::stream_info`]'s doc comment for the same reasoning.
    fn stream_info(&self) -> &StreamInfo {
        #[allow(
            clippy::option_if_let_else,
            reason = "map_or_else forces 'static vs 'self lifetime clash"
        )]
        if let Some(session) = self.inner.as_ref() {
            session
                .infos
                .app_audio
                .get()
                .unwrap_or_else(|| unknown_audio_info())
        } else {
            unknown_audio_info()
        }
    }

    fn poll_frame(&mut self) -> Result<Option<AudioFrame>, CaptureError> {
        let Some(session) = self.inner.as_ref() else {
            return Err(CaptureError::Closed);
        };
        let mut q = session
            .queues
            .app_audio
            .lock()
            .map_err(|_| CaptureError::Backend)?;
        Ok(q.pop_front())
    }

    fn close(&mut self) -> Result<(), CaptureError> {
        self.close_inner();
        Ok(())
    }
}

impl AudioCapture for AppleBroadcastExtensionCapture {
    /// Reflects the format of the most recently pushed microphone-audio frame — see
    /// [`DesktopVideoCapture::stream_info`]'s doc comment for the same reasoning.
    fn stream_info(&self) -> &StreamInfo {
        #[allow(
            clippy::option_if_let_else,
            reason = "map_or_else forces 'static vs 'self lifetime clash"
        )]
        if let Some(session) = self.inner.as_ref() {
            session
                .infos
                .mic_audio
                .get()
                .unwrap_or_else(|| unknown_audio_info())
        } else {
            unknown_audio_info()
        }
    }

    fn poll_frame(&mut self) -> Result<Option<AudioFrame>, CaptureError> {
        let Some(session) = self.inner.as_ref() else {
            return Err(CaptureError::Closed);
        };
        let mut q = session
            .queues
            .mic_audio
            .lock()
            .map_err(|_| CaptureError::Backend)?;
        Ok(q.pop_front())
    }

    fn close(&mut self) -> Result<(), CaptureError> {
        self.close_inner();
        Ok(())
    }
}

impl Drop for AppleBroadcastExtensionCapture {
    fn drop(&mut self) {
        self.close_inner();
    }
}

#[cfg(test)]
#[path = "replaykit_tests.rs"]
mod tests;