bevy_iroh 0.3.0

Share Bevy entities between peers over iroh: replication, rooms, presence and typed messages, with optional voice and video
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
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
763
764
//! Video: a source of frames, H.264 through openh264, one QUIC stream per group of pictures.
//!
//! Streams are independent, so a stalled group cannot hold up the next one, and a subscriber
//! that arrives mid-group is served from the next keyframe, which the encoder is asked for.
//! Each subscriber is fed by a task of its own from a short queue: a peer on a slow link
//! falls behind on its own stream and skips to the next keyframe, and nobody else notices.
//! Decoded frames land in a newest-wins slot that the Bevy side copies into an `Image`.

use std::sync::{
    Arc, Mutex,
    atomic::{AtomicBool, AtomicU64, Ordering},
};

use bevy::platform::time::Instant;

use super::transport::MediaHub;

/// One frame from a [`VideoSource`].
pub struct VideoFrame {
    pub width: u32,
    pub height: u32,
    pub pixels: Pixels,
    /// Milliseconds on any monotonic clock of the source's choosing.
    pub timestamp_ms: u64,
}

pub enum Pixels {
    /// `width * height * 4` bytes.
    Rgba(Vec<u8>),
    /// `width * height * 4` bytes, blue first: what most capture APIs and swapchains hand out.
    Bgra(Vec<u8>),
    /// Planar 4:2:0: a full-size luma plane and two quarter-size chroma planes.
    I420 { y: Vec<u8>, u: Vec<u8>, v: Vec<u8> },
}

/// Where frames come from. Polled by the encoder; hand back the newest frame since the last
/// call, or `None`.
pub trait VideoSource: Send + 'static {
    fn next_frame(&mut self) -> Option<VideoFrame>;
}

/// Encoder settings for one published feed.
#[derive(Debug, Clone)]
pub struct VideoConfig {
    /// `None` picks a rate for the picture's size: about 0.07 bits per pixel per frame, which
    /// is 650 kbps for 640x480 at 30 fps and 2 Mbps for 720p.
    pub bitrate_bps: Option<u32>,
    pub max_fps: f32,
    /// Frames between keyframes when nobody asked for one sooner.
    pub keyframe_interval: u32,
}

impl Default for VideoConfig {
    fn default() -> Self {
        Self {
            bitrate_bps: None,
            max_fps: 30.0,
            keyframe_interval: 60,
        }
    }
}

impl VideoConfig {
    pub fn bitrate_for(&self, width: u32, height: u32) -> u32 {
        self.bitrate_bps
            .unwrap_or_else(|| {
                ((width * height) as f32 * 0.07 * self.max_fps.clamp(1.0, 60.0)) as u32
            })
            .max(100_000)
    }
}

/// An encoded frame with what a decoder needs to know about it.
#[derive(Debug, Clone)]
pub struct VideoPacket {
    pub group: u32,
    pub keyframe: bool,
    pub pts_ms: u64,
    pub data: Vec<u8>,
}

/// What a video track has been through.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct VideoTrackStats {
    /// Frames that arrived.
    pub received: u64,
    /// Frames decoded into a picture.
    pub decoded: u64,
    /// Frames skipped to catch up with a newer group.
    pub skipped: u64,
    /// Frames thrown away because the decoder was behind.
    pub dropped: u64,
}

/// What a published feed has been through.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct FeedStats {
    /// Frames encoded.
    pub encoded: u64,
    /// Frames the network side could not take in time. Each costs a keyframe.
    pub dropped: u64,
}

/// What an encoder shares with the Bevy side.
pub(crate) struct EncoderShared {
    pub stop: AtomicBool,
    /// The picture's size as `width << 32 | height`, once known.
    pub size: AtomicU64,
    pub encoded: AtomicU64,
    pub dropped: AtomicU64,
    /// The newest frame handed to the encoder, as a picture, when a preview is wanted.
    pub preview: Option<Mutex<Option<RgbaFrame>>>,
}

impl EncoderShared {
    pub fn new(preview: bool) -> Self {
        Self {
            stop: AtomicBool::new(false),
            size: AtomicU64::new(0),
            encoded: AtomicU64::new(0),
            dropped: AtomicU64::new(0),
            preview: preview.then(|| Mutex::new(None)),
        }
    }

    pub fn size(&self) -> Option<(u32, u32)> {
        let packed = self.size.load(Ordering::Relaxed);
        (packed != 0).then_some(((packed >> 32) as u32, packed as u32))
    }

    pub fn stats(&self) -> FeedStats {
        FeedStats {
            encoded: self.encoded.load(Ordering::Relaxed),
            dropped: self.dropped.load(Ordering::Relaxed),
        }
    }

    pub(crate) fn saw(&self, frame: &VideoFrame) {
        self.size.store(
            ((frame.width as u64) << 32) | frame.height as u64,
            Ordering::Relaxed,
        );
        if let Some(slot) = &self.preview {
            let mut slot = slot.lock().unwrap_or_else(|e| e.into_inner());
            let spare = slot.take().map(|f| f.data).unwrap_or_default();
            *slot = Some(frame.to_rgba(spare));
        }
    }

    pub fn take_preview(&self) -> Option<RgbaFrame> {
        self.preview
            .as_ref()?
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .take()
    }
}

impl VideoFrame {
    /// The frame as RGBA, into `into` if it is the right size.
    pub fn to_rgba(&self, mut into: Vec<u8>) -> RgbaFrame {
        let (w, h) = (self.width as usize, self.height as usize);
        into.resize(w * h * 4, 0);
        match &self.pixels {
            Pixels::Rgba(data) => into.copy_from_slice(&data[..(w * h * 4).min(data.len())]),
            Pixels::Bgra(data) => {
                for (out, px) in into
                    .as_chunks_mut::<4>()
                    .0
                    .iter_mut()
                    .zip(data.as_chunks::<4>().0.iter())
                {
                    out[0] = px[2];
                    out[1] = px[1];
                    out[2] = px[0];
                    out[3] = 255;
                }
            }
            Pixels::I420 { y, u, v } => i420_to_rgba(y, u, v, w, h, &mut into),
        }
        RgbaFrame {
            width: self.width,
            height: self.height,
            data: into,
            pts_ms: self.timestamp_ms,
        }
    }
}

/// BT.601 limited range, which is what cameras hand out.
fn i420_to_rgba(y: &[u8], u: &[u8], v: &[u8], w: usize, h: usize, out: &mut [u8]) {
    let cw = w / 2;
    for row in 0..h {
        let crow = row / 2;
        for col in 0..w {
            let i = row * w + col;
            let c = crow * cw + col / 2;
            let (Some(&yy), Some(&uu), Some(&vv)) = (y.get(i), u.get(c), v.get(c)) else {
                return;
            };
            let yy = (yy as f32 - 16.0) * 1.164;
            let uu = uu as f32 - 128.0;
            let vv = vv as f32 - 128.0;
            let o = i * 4;
            out[o] = (yy + 1.596 * vv).clamp(0.0, 255.0) as u8;
            out[o + 1] = (yy - 0.392 * uu - 0.813 * vv).clamp(0.0, 255.0) as u8;
            out[o + 2] = (yy + 2.017 * uu).clamp(0.0, 255.0) as u8;
            out[o + 3] = 255;
        }
    }
}

// -- encoding (desktop) --------------------------------------------------------------------

/// Frames the network side may hold before the encoder drops one. Two is a frame in flight
/// and a frame waiting; more is latency standing in a queue.
pub(crate) const ENCODER_QUEUE: usize = 2;
/// Frames one subscriber may have queued before it is skipped to the next keyframe.
const SUBSCRIBER_QUEUE: usize = 2;

/// Runs on its own thread: polls the source, encodes, hands packets to the publish task.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn run_encoder(
    mut source: Box<dyn VideoSource>,
    config: VideoConfig,
    keyframe_wanted: Arc<AtomicBool>,
    shared: Arc<EncoderShared>,
    out: tokio::sync::mpsc::Sender<Arc<VideoPacket>>,
) {
    use openh264::{
        encoder::{
            BitRate, Encoder, EncoderConfig, FrameRate, FrameType, IntraFramePeriod,
            RateControlMode, UsageType,
        },
        formats::{BgraSliceU8, RgbaSliceU8, YUVBuffer, YUVSlices, YUVSource},
    };
    let min_gap = std::time::Duration::from_secs_f32(1.0 / config.max_fps.max(1.0));
    let mut encoder: Option<Encoder> = None;
    let mut group: u32 = 0;
    let mut yuv: Option<YUVBuffer> = None;
    let mut reported = false;
    while !shared.stop.load(Ordering::Relaxed) {
        let started = Instant::now();
        let Some(frame) = source.next_frame() else {
            std::thread::sleep(std::time::Duration::from_millis(2));
            continue;
        };
        let (w, h) = (frame.width as usize, frame.height as usize);
        if w < 16 || h < 16 || w % 2 != 0 || h % 2 != 0 {
            tracing::warn!("bevy_iroh: video frames must be even-sized and at least 16x16");
            continue;
        }
        shared.saw(&frame);
        if !reported {
            reported = true;
            // Zero means the source is handing over blank frames, which is indistinguishable
            // downstream from a working pipeline carrying a black picture.
            let brightest = match &frame.pixels {
                Pixels::Rgba(data) | Pixels::Bgra(data) => data.iter().copied().max().unwrap_or(0),
                Pixels::I420 { y, .. } => y.iter().copied().max().unwrap_or(0),
            };
            tracing::info!(
                "bevy_iroh: encoding {w}x{h} video at {} kbps, brightest byte {brightest}",
                config.bitrate_for(frame.width, frame.height) / 1000
            );
        }
        let enc = match &mut encoder {
            Some(e) => e,
            None => {
                let encoder_config = EncoderConfig::new()
                    .bitrate(BitRate::from_bps(
                        config.bitrate_for(frame.width, frame.height),
                    ))
                    .rate_control_mode(RateControlMode::Bitrate)
                    .max_frame_rate(FrameRate::from_hz(config.max_fps))
                    .usage_type(UsageType::CameraVideoRealTime)
                    .intra_frame_period(IntraFramePeriod::from_num_frames(config.keyframe_interval))
                    .skip_frames(true);
                match Encoder::with_api_config(openh264::OpenH264API::from_source(), encoder_config)
                {
                    Ok(e) => encoder.insert(e),
                    Err(e) => {
                        tracing::error!("bevy_iroh: h264 encoder: {e}");
                        return;
                    }
                }
            }
        };
        if keyframe_wanted.swap(false, Ordering::Relaxed) {
            enc.force_intra_frame();
        }
        let pts = openh264::Timestamp::from_millis(frame.timestamp_ms);
        let encoded = match &frame.pixels {
            Pixels::Rgba(data) => {
                if data.len() < w * h * 4 {
                    continue;
                }
                let buffer = yuv.get_or_insert_with(|| YUVBuffer::new(w, h));
                if buffer.dimensions() != (w, h) {
                    *buffer = YUVBuffer::new(w, h);
                }
                buffer.read_rgba8(RgbaSliceU8::new(&data[..w * h * 4], (w, h)));
                enc.encode_at(buffer, pts)
            }
            Pixels::Bgra(data) => {
                if data.len() < w * h * 4 {
                    continue;
                }
                let buffer = yuv.get_or_insert_with(|| YUVBuffer::new(w, h));
                if buffer.dimensions() != (w, h) {
                    *buffer = YUVBuffer::new(w, h);
                }
                buffer.read_bgra8(BgraSliceU8::new(&data[..w * h * 4], (w, h)));
                enc.encode_at(buffer, pts)
            }
            Pixels::I420 { y, u, v } => {
                let (cw, ch) = (w / 2, h / 2);
                if y.len() < w * h || u.len() < cw * ch || v.len() < cw * ch {
                    continue;
                }
                let slices = YUVSlices::new(
                    (&y[..w * h], &u[..cw * ch], &v[..cw * ch]),
                    (w, h),
                    (w, cw, cw),
                );
                enc.encode_at(&slices, pts)
            }
        };
        let bitstream = match encoded {
            Ok(b) => b,
            Err(e) => {
                tracing::debug!("bevy_iroh: h264 encode: {e}");
                continue;
            }
        };
        let keyframe = matches!(bitstream.frame_type(), FrameType::IDR | FrameType::I);
        let data = bitstream.to_vec();
        if data.is_empty() {
            continue;
        }
        if keyframe {
            group = group.wrapping_add(1);
            tracing::debug!(
                "bevy_iroh: video keyframe, group {group}, {} bytes",
                data.len()
            );
        }
        shared.encoded.fetch_add(1, Ordering::Relaxed);
        let packet = Arc::new(VideoPacket {
            group,
            keyframe,
            pts_ms: frame.timestamp_ms,
            data,
        });
        match out.try_send(packet) {
            Ok(()) => {}
            Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {
                // The network side is behind; a queued frame is latency. Drop it, and start
                // a fresh group next so no subscriber decodes past a hole.
                shared.dropped.fetch_add(1, Ordering::Relaxed);
                keyframe_wanted.store(true, Ordering::Relaxed);
            }
            Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => return,
        }
        // Pacing counts the encode against the interval, so a slow encode does not add to it.
        let took = started.elapsed();
        if took < min_gap {
            std::thread::sleep(min_gap - took);
        }
    }
}

/// Runs on the network runtime: fans packets out to one task per subscriber, each with its
/// own short queue. Subscribers are re-read at every keyframe, which is also when a new one
/// can start, and a subscriber that cannot keep up is skipped to the next keyframe.
pub(crate) async fn run_publisher(
    hub: Arc<MediaHub>,
    track: u64,
    mut packets: tokio::sync::mpsc::Receiver<Arc<VideoPacket>>,
) {
    use std::collections::HashMap;
    struct Feed {
        tx: tokio::sync::mpsc::Sender<Arc<VideoPacket>>,
        /// Set when a packet was dropped for this subscriber: its stream is broken until the
        /// next keyframe.
        broken: Arc<AtomicBool>,
    }
    let mut feeds: HashMap<usize, Feed> = HashMap::new();
    let keyframe_wanted = hub.keyframe_flag(track);
    while let Some(packet) = packets.recv().await {
        if packet.keyframe {
            let subscribers = hub.subscribers_of(track);
            feeds.retain(|id, _| subscribers.iter().any(|c| c.stable_id() == *id));
            for conn in subscribers {
                let id = conn.stable_id();
                if feeds.contains_key(&id) {
                    continue;
                }
                let (tx, rx) = tokio::sync::mpsc::channel(SUBSCRIBER_QUEUE);
                let broken = Arc::new(AtomicBool::new(false));
                n0_future::task::spawn(serve_subscriber(conn, track, rx, broken.clone()));
                feeds.insert(id, Feed { tx, broken });
            }
            tracing::debug!(
                "bevy_iroh: video group {} to {} subscriber(s)",
                packet.group,
                feeds.len()
            );
        }
        let mut gone = Vec::new();
        for (id, feed) in feeds.iter() {
            match feed.tx.try_send(packet.clone()) {
                Ok(()) => {}
                Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {
                    if !feed.broken.swap(true, Ordering::Relaxed) {
                        // The sooner the next keyframe, the sooner this subscriber is back.
                        keyframe_wanted.store(true, Ordering::Relaxed);
                    }
                }
                Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => gone.push(*id),
            }
        }
        for id in gone {
            feeds.remove(&id);
        }
    }
}

async fn serve_subscriber(
    conn: iroh::endpoint::Connection,
    track: u64,
    mut packets: tokio::sync::mpsc::Receiver<Arc<VideoPacket>>,
    broken: Arc<AtomicBool>,
) {
    let mut stream: Option<iroh::endpoint::SendStream> = None;
    while let Some(packet) = packets.recv().await {
        if packet.keyframe {
            if let Some(mut old) = stream.take() {
                let _ = old.finish();
            }
            broken.store(false, Ordering::Relaxed);
            match MediaHub::open_group(&conn, track, packet.group).await {
                Ok(s) => stream = Some(s),
                Err(e) => {
                    tracing::debug!("bevy_iroh: open video group: {e:#}");
                    return;
                }
            }
        } else if broken.load(Ordering::Relaxed) {
            // A hole in this group: the rest of it would decode as garbage.
            if let Some(mut old) = stream.take() {
                let _ = old.finish();
            }
            continue;
        }
        let Some(s) = &mut stream else { continue };
        if MediaHub::write_video_frame(s, &packet).await.is_err() {
            return;
        }
    }
    if let Some(mut s) = stream.take() {
        let _ = s.finish();
    }
}

// -- decoding ------------------------------------------------------------------------------

/// A decoded frame, RGBA.
#[derive(Clone)]
pub struct RgbaFrame {
    pub width: u32,
    pub height: u32,
    pub data: Vec<u8>,
    pub pts_ms: u64,
}

/// Packets more than this far behind the newest keyframe are skipped: a picture that is
/// 150 ms old is not live, and the keyframe is a place to start fresh.
pub(crate) const MAX_BEHIND_MS: u64 = 150;
/// Packets waiting for the decoder before the oldest is dropped.
#[cfg(not(target_arch = "wasm32"))]
const DECODE_QUEUE: usize = 16;

/// A remote video track: packets go to a decoder, the newest decoded frame waits here.
pub struct RemoteVideo {
    #[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]
    pub(crate) track: u64,
    #[cfg(not(target_arch = "wasm32"))]
    packets: Mutex<Option<std::sync::mpsc::SyncSender<VideoPacket>>>,
    latest: Mutex<Option<RgbaFrame>>,
    received: AtomicU64,
    decoded: AtomicU64,
    skipped: AtomicU64,
    dropped: AtomicU64,
}

impl RemoteVideo {
    pub(crate) fn new(track: u64) -> Self {
        Self {
            track,
            #[cfg(not(target_arch = "wasm32"))]
            packets: Mutex::new(None),
            latest: Mutex::new(None),
            received: AtomicU64::new(0),
            decoded: AtomicU64::new(0),
            skipped: AtomicU64::new(0),
            dropped: AtomicU64::new(0),
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn push(self: &Arc<Self>, packet: VideoPacket) {
        self.received.fetch_add(1, Ordering::Relaxed);
        let mut slot = self.packets.lock().unwrap_or_else(|e| e.into_inner());
        if slot.is_none() {
            let (tx, rx) = std::sync::mpsc::sync_channel(DECODE_QUEUE);
            let me = self.clone();
            if std::thread::Builder::new()
                .name("bevy_iroh-decode".into())
                .spawn(move || me.run_decoder(rx))
                .is_ok()
            {
                *slot = Some(tx);
            }
        }
        if let Some(tx) = slot.as_ref() {
            match tx.try_send(packet) {
                Ok(()) => {}
                Err(std::sync::mpsc::TrySendError::Full(_)) => {
                    self.dropped.fetch_add(1, Ordering::Relaxed);
                }
                Err(std::sync::mpsc::TrySendError::Disconnected(_)) => *slot = None,
            }
        }
    }

    #[cfg(target_arch = "wasm32")]
    pub(crate) fn push(self: &Arc<Self>, packet: VideoPacket) {
        self.received.fetch_add(1, Ordering::Relaxed);
        super::web::video::decode(self, packet);
    }

    /// The newest decoded frame, taken. `None` until the first keyframe decodes, and between
    /// frames.
    pub fn take_frame(&self) -> Option<RgbaFrame> {
        self.latest.lock().unwrap_or_else(|e| e.into_inner()).take()
    }

    /// A decoded picture, from whichever decoder produced it.
    pub(crate) fn deliver(&self, frame: RgbaFrame) -> Option<Vec<u8>> {
        if self.decoded.fetch_add(1, Ordering::Relaxed) == 0 {
            let brightest = frame.data.iter().copied().max().unwrap_or(0);
            tracing::info!(
                "bevy_iroh: seeing a {}x{} picture, brightest byte {brightest}",
                frame.width,
                frame.height
            );
        }
        self.latest
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .replace(frame)
            .map(|unshown| unshown.data)
    }

    pub(crate) fn skipped(&self, n: u64) {
        self.skipped.fetch_add(n, Ordering::Relaxed);
    }

    /// Frames decoded so far.
    pub fn decoded(&self) -> u64 {
        self.decoded.load(Ordering::Relaxed)
    }

    pub fn stats(&self) -> VideoTrackStats {
        VideoTrackStats {
            received: self.received.load(Ordering::Relaxed),
            decoded: self.decoded(),
            skipped: self.skipped.load(Ordering::Relaxed),
            dropped: self.dropped.load(Ordering::Relaxed),
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    fn run_decoder(self: Arc<Self>, rx: std::sync::mpsc::Receiver<VideoPacket>) {
        use std::collections::VecDeque;

        use openh264::formats::YUVSource;
        let mut decoder = match openh264::decoder::Decoder::new() {
            Ok(d) => d,
            Err(e) => {
                tracing::error!("bevy_iroh: h264 decoder: {e}");
                return;
            }
        };
        let mut current_group: Option<u32> = None;
        let mut spare: Vec<u8> = Vec::new();
        let mut queue: VecDeque<VideoPacket> = VecDeque::new();
        loop {
            if queue.is_empty() {
                match rx.recv_timeout(std::time::Duration::from_millis(200)) {
                    Ok(packet) => queue.push_back(packet),
                    Err(_) => {
                        // The subscription is gone when this thread holds the last reference.
                        if Arc::strong_count(&self) == 1 {
                            return;
                        }
                        continue;
                    }
                }
            }
            while let Ok(packet) = rx.try_recv() {
                queue.push_back(packet);
            }
            // Behind by more than a beat, with a fresh start waiting: take it.
            if let Some(newest_key) = queue.iter().rposition(|p| p.keyframe)
                && newest_key > 0
            {
                let span = queue[newest_key].pts_ms.saturating_sub(queue[0].pts_ms);
                if span > MAX_BEHIND_MS {
                    queue.drain(..newest_key);
                    self.skipped(newest_key as u64);
                }
            }
            let packet = queue.pop_front().expect("non-empty");
            // Groups can interleave at a boundary; once a newer keyframe has been seen, the
            // tail of the old group is stale.
            match current_group {
                Some(g) if packet.group.wrapping_sub(g) > u32::MAX / 2 => continue,
                Some(g) if packet.group != g && !packet.keyframe => continue,
                _ => {}
            }
            if packet.keyframe {
                current_group = Some(packet.group);
            }
            let decoded = match decoder.decode(&packet.data) {
                Ok(Some(frame)) => frame,
                Ok(None) => continue,
                Err(e) => {
                    tracing::debug!("bevy_iroh: h264 decode: {e}");
                    continue;
                }
            };
            let (w, h) = decoded.dimensions();
            // Reuse the buffer the Bevy side handed back, rather than allocating a full
            // picture per frame.
            let mut rgba = std::mem::take(&mut spare);
            rgba.resize(w * h * 4, 0);
            decoded.write_rgba8(&mut rgba);
            if let Some(unshown) = self.deliver(RgbaFrame {
                width: w as u32,
                height: h as u32,
                data: rgba,
                pts_ms: packet.pts_ms,
            }) {
                spare = unshown;
            }
        }
    }
}

/// A moving test pattern: colour bars sliding at `fps`. For examples and tests, and for a
/// conference client with no camera.
pub struct TestPattern {
    width: u32,
    height: u32,
    fps: f32,
    started: Instant,
    last_frame: Option<u64>,
}

impl TestPattern {
    pub fn new(width: u32, height: u32, fps: f32) -> Self {
        Self {
            width,
            height,
            fps,
            started: Instant::now(),
            last_frame: None,
        }
    }
}

impl VideoSource for TestPattern {
    fn next_frame(&mut self) -> Option<VideoFrame> {
        let elapsed = self.started.elapsed();
        let index = (elapsed.as_secs_f32() * self.fps) as u64;
        if self.last_frame == Some(index) {
            return None;
        }
        self.last_frame = Some(index);
        let (w, h) = (self.width as usize, self.height as usize);
        let shift = (index as usize * 4) % w;
        let mut data = vec![0u8; w * h * 4];
        for y in 0..h {
            for x in 0..w {
                let bar = ((x + shift) * 8 / w) % 8;
                let (r, g, b) = match bar {
                    0 => (235, 235, 235),
                    1 => (235, 235, 16),
                    2 => (16, 235, 235),
                    3 => (16, 235, 16),
                    4 => (235, 16, 235),
                    5 => (235, 16, 16),
                    6 => (16, 16, 235),
                    _ => (16, 16, 16),
                };
                let shade = 1.0 - (y as f32 / h as f32) * 0.4;
                let i = (y * w + x) * 4;
                data[i] = (r as f32 * shade) as u8;
                data[i + 1] = (g as f32 * shade) as u8;
                data[i + 2] = (b as f32 * shade) as u8;
                data[i + 3] = 255;
            }
        }
        Some(VideoFrame {
            width: self.width,
            height: self.height,
            pixels: Pixels::Rgba(data),
            timestamp_ms: elapsed.as_millis() as u64,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bitrate_scales_with_the_picture() {
        let config = VideoConfig::default();
        assert_eq!(config.bitrate_for(640, 480), 645_120);
        assert_eq!(config.bitrate_for(1280, 720), 1_935_360);
        assert_eq!(config.bitrate_for(16, 16), 100_000);
        let fixed = VideoConfig {
            bitrate_bps: Some(300_000),
            ..default()
        };
        assert_eq!(fixed.bitrate_for(1280, 720), 300_000);
    }

    #[test]
    fn i420_grey_is_grey() {
        let (w, h) = (4, 2);
        let y = vec![128u8; w * h];
        let u = vec![128u8; 2];
        let v = vec![128u8; 2];
        let frame = VideoFrame {
            width: 4,
            height: 2,
            pixels: Pixels::I420 { y, u, v },
            timestamp_ms: 0,
        };
        let rgba = frame.to_rgba(Vec::new());
        assert_eq!(rgba.data.len(), w * h * 4);
        let px = &rgba.data[..4];
        assert!(px[0].abs_diff(130) <= 2 && px[1].abs_diff(130) <= 2 && px[2].abs_diff(130) <= 2);
        assert_eq!(px[3], 255);
    }

    fn default<T: Default>() -> T {
        T::default()
    }
}