bevy_iroh 0.4.1

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
//! Cameras and H.264 in a browser: `getUserMedia` in, WebCodecs both ways.
//!
//! The camera is a hidden `<video>` element playing the stream and a canvas it is drawn to
//! once per frame, so the encoder and the preview get the same RGBA bytes. The encoder is
//! asked for Annex B, which is what openh264 on a desktop sends and expects, so a browser
//! and a desktop decode each other without a catalog carrying an `avcC` blob.
//!
//! WebCodecs never answers from the call that asked: every encoded chunk and decoded frame
//! arrives on a later task, and the wrappers here turn that into the shapes the shared
//! pipeline expects.

use std::{
    cell::RefCell,
    collections::HashMap,
    rc::Rc,
    sync::{
        Arc, Weak,
        atomic::{AtomicBool, Ordering},
    },
};

use wasm_bindgen::{JsCast, JsValue, closure::Closure};
use wasm_bindgen_futures::JsFuture;
use web_sys::{
    CanvasRenderingContext2d, EncodedVideoChunk, EncodedVideoChunkInit, EncodedVideoChunkType,
    HtmlCanvasElement, HtmlVideoElement, MediaStream, MediaStreamConstraints, VideoFrame,
    VideoFrameBufferInit, VideoPixelFormat,
};

use super::{describe, exactly, media_devices, now_ms, stop_stream};
use crate::media::video::{
    EncoderShared, MAX_BEHIND_MS, Pixels, RemoteVideo, RgbaFrame, VideoConfig, VideoFrame as Frame,
    VideoPacket, VideoSource,
};

/// The H.264 codec string for a picture of this size: Constrained Baseline, which every
/// hardware decoder implements and openh264 produces, at the lowest level that admits the
/// area. The level is not cosmetic: WebCodecs closes an encoder whose picture exceeds it.
fn h264_codec(width: u32, height: u32) -> String {
    // (level byte, MaxFS in macroblocks) from ITU-T H.264 Table A-1.
    const LEVELS: [(u8, u32); 8] = [
        (0x1f, 3600),
        (0x20, 5120),
        (0x28, 8192),
        (0x2a, 8704),
        (0x32, 22080),
        (0x33, 36864),
        (0x35, 139264),
        (0x3e, 139264),
    ];
    let macroblocks = width.div_ceil(16) * height.div_ceil(16);
    let level = LEVELS
        .iter()
        .find(|(_, max)| macroblocks <= *max)
        .map(|(level, _)| *level)
        .unwrap_or(0x3e);
    format!("avc1.42e0{level:02x}")
}

struct Callbacks {
    _output: Closure<dyn FnMut(JsValue, JsValue)>,
    _error: Closure<dyn FnMut(JsValue)>,
}

// -- camera --------------------------------------------------------------------------------

struct CameraSession {
    video: HtmlVideoElement,
    canvas: HtmlCanvasElement,
    context: CanvasRenderingContext2d,
    stream: Rc<RefCell<Option<MediaStream>>>,
    last_time: f64,
}

thread_local! {
    static CAMERAS: RefCell<HashMap<u64, CameraSession>> = RefCell::new(HashMap::new());
    static NEXT_CAMERA: RefCell<u64> = const { RefCell::new(1) };
    static DECODERS: RefCell<HashMap<u64, Decoder>> = RefCell::new(HashMap::new());
}

/// A camera from `getUserMedia`, as a video source. Frames arrive once the person has said
/// yes; until then `next_frame` is `None`.
pub struct Camera {
    slot: u64,
}

impl Camera {
    pub fn open(id: Option<&str>, width: u32, height: u32, fps: f32) -> Result<Self, String> {
        let document = web_sys::window()
            .and_then(|w| w.document())
            .ok_or("no document")?;
        let video: HtmlVideoElement = document
            .create_element("video")
            .map_err(describe)?
            .dyn_into()
            .map_err(|_| "not a video element")?;
        video.set_autoplay(true);
        video.set_muted(true);
        let _ = video.set_attribute("playsinline", "");
        let canvas: HtmlCanvasElement = document
            .create_element("canvas")
            .map_err(describe)?
            .dyn_into()
            .map_err(|_| "not a canvas")?;
        let options = js_sys::Object::new();
        let _ = js_sys::Reflect::set(&options, &"willReadFrequently".into(), &JsValue::TRUE);
        let context: CanvasRenderingContext2d = canvas
            .get_context_with_context_options("2d", &options)
            .map_err(describe)?
            .ok_or("no 2d context")?
            .dyn_into()
            .map_err(|_| "not a 2d context")?;
        let slot = NEXT_CAMERA.with_borrow_mut(|n| {
            let s = *n;
            *n += 1;
            s
        });
        let stream = Rc::new(RefCell::new(None));
        CAMERAS.with_borrow_mut(|cameras| {
            cameras.insert(
                slot,
                CameraSession {
                    video: video.clone(),
                    canvas,
                    context,
                    stream: stream.clone(),
                    last_time: -1.0,
                },
            )
        });
        let id = id.map(str::to_string);
        wasm_bindgen_futures::spawn_local(async move {
            match acquire(id.as_deref(), width, height, fps).await {
                Ok(s) => {
                    video.set_src_object(Some(&s));
                    let _ = video.play();
                    *stream.borrow_mut() = Some(s);
                    bevy::log::info!("bevy_iroh: camera open");
                }
                Err(e) => bevy::log::error!("bevy_iroh: camera: {e}"),
            }
        });
        Ok(Self { slot })
    }
}

async fn acquire(
    id: Option<&str>,
    width: u32,
    height: u32,
    fps: f32,
) -> Result<MediaStream, String> {
    let devices = media_devices()?;
    let video = match id {
        Some(_) => exactly(id)?,
        None => js_sys::Object::new().into(),
    };
    let ideal = |v: f64| -> JsValue {
        let o = js_sys::Object::new();
        let _ = js_sys::Reflect::set(&o, &"ideal".into(), &v.into());
        o.into()
    };
    let _ = js_sys::Reflect::set(&video, &"width".into(), &ideal(width as f64));
    let _ = js_sys::Reflect::set(&video, &"height".into(), &ideal(height as f64));
    let _ = js_sys::Reflect::set(&video, &"frameRate".into(), &ideal(fps as f64));
    let constraints = MediaStreamConstraints::new();
    constraints.set_video(&video);
    constraints.set_audio(&JsValue::FALSE);
    let promise = devices
        .get_user_media_with_constraints(&constraints)
        .map_err(describe)?;
    Ok(JsFuture::from(promise)
        .await
        .map_err(describe)?
        .unchecked_into())
}

impl VideoSource for Camera {
    fn next_frame(&mut self) -> Option<Frame> {
        CAMERAS.with_borrow_mut(|cameras| {
            let session = cameras.get_mut(&self.slot)?;
            // HAVE_CURRENT_DATA or better, and a frame we have not drawn yet.
            if session.video.ready_state() < 2 {
                return None;
            }
            let time = session.video.current_time();
            if time == session.last_time {
                return None;
            }
            session.last_time = time;
            let (w, h) = (session.video.video_width(), session.video.video_height());
            if w < 16 || h < 16 {
                return None;
            }
            // Even sizes: what an H.264 encoder wants.
            let (w, h) = (w & !1, h & !1);
            if session.canvas.width() != w || session.canvas.height() != h {
                session.canvas.set_width(w);
                session.canvas.set_height(h);
            }
            session
                .context
                .draw_image_with_html_video_element_and_dw_and_dh(
                    &session.video,
                    0.0,
                    0.0,
                    w as f64,
                    h as f64,
                )
                .ok()?;
            let data = session
                .context
                .get_image_data(0, 0, w as i32, h as i32)
                .ok()?
                .data();
            Some(Frame {
                width: w,
                height: h,
                pixels: Pixels::Rgba(data.0),
                timestamp_ms: now_ms(),
            })
        })
    }
}

impl Drop for Camera {
    fn drop(&mut self) {
        CAMERAS.with_borrow_mut(|cameras| {
            if let Some(session) = cameras.remove(&self.slot) {
                session.video.set_src_object(None);
                if let Some(stream) = session.stream.borrow().as_ref() {
                    stop_stream(stream);
                }
                bevy::log::info!("bevy_iroh: camera closed");
            }
        });
    }
}

// -- encode --------------------------------------------------------------------------------

struct WebEncoder {
    inner: web_sys::VideoEncoder,
    size: (u32, u32),
    _callbacks: Callbacks,
}

impl WebEncoder {
    fn new(
        width: u32,
        height: u32,
        config: &VideoConfig,
        out: tokio::sync::mpsc::Sender<Arc<VideoPacket>>,
        keyframe_wanted: Arc<AtomicBool>,
        shared: Arc<EncoderShared>,
    ) -> Result<Self, String> {
        let group = Rc::new(RefCell::new(0u32));
        let on_output = Closure::wrap(Box::new(move |chunk: JsValue, _meta: JsValue| {
            let chunk: EncodedVideoChunk = chunk.unchecked_into();
            let mut data = vec![0u8; chunk.byte_length() as usize];
            if chunk.copy_to_with_u8_slice(&mut data).is_err() {
                return;
            }
            let keyframe = chunk.type_() == EncodedVideoChunkType::Key;
            let mut g = group.borrow_mut();
            if keyframe {
                *g = g.wrapping_add(1);
            }
            shared.encoded.fetch_add(1, Ordering::Relaxed);
            let packet = Arc::new(VideoPacket {
                group: *g,
                keyframe,
                pts_ms: (chunk.timestamp() / 1000.0) as u64,
                data,
            });
            if let Err(tokio::sync::mpsc::error::TrySendError::Full(_)) = out.try_send(packet) {
                shared.dropped.fetch_add(1, Ordering::Relaxed);
                keyframe_wanted.store(true, Ordering::Relaxed);
            }
        }) as Box<dyn FnMut(JsValue, JsValue)>);
        let on_error = Closure::wrap(Box::new(|error: JsValue| {
            bevy::log::error!("bevy_iroh: video encoder: {}", describe(error));
        }) as Box<dyn FnMut(JsValue)>);
        let init = web_sys::VideoEncoderInit::new(
            on_error.as_ref().unchecked_ref(),
            on_output.as_ref().unchecked_ref(),
        );
        let inner = web_sys::VideoEncoder::new(&init).map_err(describe)?;
        let cfg = web_sys::VideoEncoderConfig::new(&h264_codec(width, height), height, width);
        cfg.set_bitrate(config.bitrate_for(width, height));
        cfg.set_framerate(config.max_fps as f64);
        cfg.set_latency_mode(web_sys::LatencyMode::Realtime);
        let avc = js_sys::Object::new();
        js_sys::Reflect::set(&avc, &"format".into(), &"annexb".into()).map_err(describe)?;
        js_sys::Reflect::set(&cfg, &"avc".into(), &avc).map_err(describe)?;
        inner.configure(&cfg).map_err(describe)?;
        bevy::log::info!(
            "bevy_iroh: encoding {width}x{height} video at {} kbps",
            config.bitrate_for(width, height) / 1000
        );
        Ok(Self {
            inner,
            size: (width, height),
            _callbacks: Callbacks {
                _output: on_output,
                _error: on_error,
            },
        })
    }

    fn encode(&self, frame: &Frame, keyframe: bool) -> Result<(), String> {
        let (w, h) = (frame.width, frame.height);
        let (format, bytes): (VideoPixelFormat, Vec<u8>) = match &frame.pixels {
            Pixels::Rgba(d) => (VideoPixelFormat::Rgba, d.clone()),
            Pixels::Bgra(d) => (VideoPixelFormat::Bgra, d.clone()),
            Pixels::I420 { y, u, v } => {
                let mut all = Vec::with_capacity(y.len() + u.len() + v.len());
                all.extend_from_slice(y);
                all.extend_from_slice(u);
                all.extend_from_slice(v);
                (VideoPixelFormat::I420, all)
            }
        };
        let mut bytes = bytes;
        let init =
            VideoFrameBufferInit::new_with_f64(h, w, format, frame.timestamp_ms as f64 * 1000.0);
        let vf = VideoFrame::new_with_u8_slice_and_video_frame_buffer_init(&mut bytes, &init)
            .map_err(describe)?;
        let options = web_sys::VideoEncoderEncodeOptions::new();
        options.set_key_frame(keyframe);
        let result = self.inner.encode_with_options(&vf, &options);
        // A `VideoFrame` holds a buffer the browser will not reclaim on GC alone.
        vf.close();
        result.map_err(describe)
    }
}

impl Drop for WebEncoder {
    fn drop(&mut self) {
        let _ = self.inner.close();
    }
}

/// Polls the source on a timer, encodes, hands packets to the publisher.
pub(crate) fn start_encoder(
    mut source: Box<dyn VideoSource>,
    config: VideoConfig,
    keyframe_wanted: Arc<AtomicBool>,
    shared: Arc<EncoderShared>,
    out: tokio::sync::mpsc::Sender<Arc<VideoPacket>>,
) {
    wasm_bindgen_futures::spawn_local(async move {
        let interval = std::time::Duration::from_secs_f32(1.0 / config.max_fps.max(1.0));
        let mut encoder: Option<WebEncoder> = None;
        while !shared.stop.load(Ordering::Relaxed) {
            let started = web_time::Instant::now();
            if let Some(frame) = source.next_frame() {
                shared.saw(&frame);
                let size = (frame.width, frame.height);
                if encoder.as_ref().is_some_and(|e| e.size != size) {
                    encoder = None;
                }
                if encoder.is_none() {
                    match WebEncoder::new(
                        size.0,
                        size.1,
                        &config,
                        out.clone(),
                        keyframe_wanted.clone(),
                        shared.clone(),
                    ) {
                        Ok(e) => encoder = Some(e),
                        Err(e) => {
                            bevy::log::error!("bevy_iroh: h264: {e}");
                            return;
                        }
                    }
                }
                let key = keyframe_wanted.swap(false, Ordering::Relaxed);
                if let Some(enc) = &encoder
                    && let Err(e) = enc.encode(&frame, key)
                {
                    bevy::log::debug!("bevy_iroh: h264 encode: {e}");
                    // A closed codec stays closed: build a fresh one next frame.
                    encoder = None;
                    keyframe_wanted.store(true, Ordering::Relaxed);
                }
            }
            let took = started.elapsed();
            let wait = interval
                .saturating_sub(took)
                .max(std::time::Duration::from_millis(2));
            n0_future::time::sleep(wait).await;
        }
    });
}

// -- decode --------------------------------------------------------------------------------

struct Decoder {
    inner: web_sys::VideoDecoder,
    remote: Weak<RemoteVideo>,
    /// Waiting for a keyframe to start or restart on.
    waiting: bool,
    group: Option<u32>,
    /// Oldest pts still in the decoder's queue, for the latency bound.
    oldest_pts: Option<u64>,
    broken: Rc<RefCell<bool>>,
    _callbacks: Callbacks,
}

/// Frames the browser's decoder may hold before the rest of the group is skipped.
const DECODE_QUEUE: u32 = 8;

fn new_decoder(remote: &Arc<RemoteVideo>) -> Result<Decoder, String> {
    let weak = Arc::downgrade(remote);
    let out_remote = weak.clone();
    let on_output = Closure::wrap(Box::new(move |frame: JsValue, _extra: JsValue| {
        let frame: VideoFrame = frame.unchecked_into();
        // The display size, not the coded one: H.264 codes in 16x16 macroblocks and the
        // padding is not picture.
        let (width, height) = (frame.display_width(), frame.display_height());
        let options = web_sys::VideoFrameCopyToOptions::new();
        options.set_format(VideoPixelFormat::Rgba);
        if let Some(visible) = frame.visible_rect() {
            let rect = web_sys::DomRectInit::new();
            rect.set_x(visible.x());
            rect.set_y(visible.y());
            rect.set_width(visible.width());
            rect.set_height(visible.height());
            options.set_rect(&rect);
        }
        let size = frame
            .allocation_size_with_options(&options)
            .unwrap_or(width * height * 4);
        let pts_ms = (frame.timestamp() / 1000.0) as u64;
        let remote = out_remote.clone();
        wasm_bindgen_futures::spawn_local(async move {
            let mut rgba = vec![0u8; size as usize];
            let copy = frame.copy_to_with_u8_slice_and_options(&mut rgba, &options);
            let done = JsFuture::from(copy).await.is_ok();
            frame.close();
            if done && let Some(remote) = remote.upgrade() {
                rgba.truncate((width * height * 4) as usize);
                remote.deliver(RgbaFrame {
                    width,
                    height,
                    data: rgba,
                    pts_ms,
                });
            }
        });
    }) as Box<dyn FnMut(JsValue, JsValue)>);
    let broken = Rc::new(RefCell::new(false));
    let flag = broken.clone();
    let on_error = Closure::wrap(Box::new(move |error: JsValue| {
        bevy::log::warn!("bevy_iroh: video decoder: {}", describe(error));
        *flag.borrow_mut() = true;
    }) as Box<dyn FnMut(JsValue)>);
    let init = web_sys::VideoDecoderInit::new(
        on_error.as_ref().unchecked_ref(),
        on_output.as_ref().unchecked_ref(),
    );
    let inner = web_sys::VideoDecoder::new(&init).map_err(describe)?;
    // Level 5.1 admits anything up to 4K; a decoder's string is an upper bound, not a
    // promise. No description: the stream is Annex B with SPS and PPS in band.
    let config = web_sys::VideoDecoderConfig::new("avc1.42e033");
    config.set_optimize_for_latency(true);
    inner.configure(&config).map_err(describe)?;
    Ok(Decoder {
        inner,
        remote: weak,
        waiting: true,
        group: None,
        oldest_pts: None,
        broken,
        _callbacks: Callbacks {
            _output: on_output,
            _error: on_error,
        },
    })
}

/// One packet for `remote`, from the network task.
pub(crate) fn decode(remote: &Arc<RemoteVideo>, packet: VideoPacket) {
    DECODERS.with_borrow_mut(|decoders| {
        let track = remote.track;
        if decoders
            .get(&track)
            .is_some_and(|d| *d.broken.borrow() || d.remote.upgrade().is_none())
        {
            decoders.remove(&track);
        }
        let decoder = match decoders.get_mut(&track) {
            Some(d) => d,
            None => match new_decoder(remote) {
                Ok(d) => {
                    decoders.insert(track, d);
                    decoders.get_mut(&track).expect("just inserted")
                }
                Err(e) => {
                    bevy::log::error!("bevy_iroh: h264 decoder: {e}");
                    return;
                }
            },
        };
        // Stale groups, and a group's tail once a newer one has started.
        match decoder.group {
            Some(g) if packet.group.wrapping_sub(g) > u32::MAX / 2 => return,
            Some(g) if packet.group != g && !packet.keyframe => return,
            _ => {}
        }
        if packet.keyframe {
            decoder.group = Some(packet.group);
            decoder.waiting = false;
            decoder.oldest_pts = None;
        } else if decoder.waiting {
            remote.skipped(1);
            return;
        }
        // Behind by more than a beat: give up on this group and wait for the next keyframe,
        // which the owner is asked for.
        let queued = decoder.inner.decode_queue_size();
        if queued == 0 {
            decoder.oldest_pts = Some(packet.pts_ms);
        }
        let behind = decoder
            .oldest_pts
            .map(|o| packet.pts_ms.saturating_sub(o))
            .unwrap_or(0);
        if !packet.keyframe && (queued > DECODE_QUEUE || behind > MAX_BEHIND_MS) {
            decoder.waiting = true;
            remote.skipped(1);
            return;
        }
        let kind = if packet.keyframe {
            EncodedVideoChunkType::Key
        } else {
            EncodedVideoChunkType::Delta
        };
        let data = js_sys::Uint8Array::from(packet.data.as_slice());
        let init = EncodedVideoChunkInit::new(&data, 0, kind);
        init.set_timestamp_f64(packet.pts_ms as f64 * 1000.0);
        if let Ok(chunk) = EncodedVideoChunk::new(&init)
            && let Err(e) = decoder.inner.decode(&chunk)
        {
            bevy::log::debug!("bevy_iroh: h264 decode: {}", describe(e));
            *decoder.broken.borrow_mut() = true;
        }
    });
}

pub(crate) fn sweep() {
    DECODERS.with_borrow_mut(|decoders| {
        decoders.retain(|_, d| d.remote.upgrade().is_some());
    });
}

impl Drop for Decoder {
    fn drop(&mut self) {
        let _ = self.inner.close();
    }
}