bevy_iroh 0.4.2

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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
//! Voice and video over the same endpoint. Behind the `media` feature.
//!
//! Put [`Voice`] on a [`Shared`](crate::replicate::Shared) entity and your microphone goes to
//! everyone in the room, announced by replication like any other component. A remote entity
//! that arrives with `Voice` is subscribed to and played back, with gain and pan from where it
//! is relative to the [`AudioListener`]. Nothing to write on the receiving side.
//!
//! [`VideoFeed`] is the same shape for a picture: pair it locally with a [`VideoInput`] naming
//! where frames come from, and a remote one grows a [`VideoImage`] to put on any material.
//!
//! Which microphone, speaker and camera to use is [`MediaSettings`]; the lists to choose from
//! are [`AudioDevices`] and [`CameraDevices`]. Change a setting and the device is reopened
//! while everything published carries on.

pub mod audio;
pub mod devices;
#[cfg(not(target_arch = "wasm32"))]
pub mod native;
#[cfg(feature = "webrtc")]
pub mod rtc;
pub mod transport;
#[cfg(feature = "ui")]
pub mod ui;
#[cfg(all(feature = "v4l2", target_os = "linux"))]
pub mod v4l2;
pub mod video;
#[cfg(target_arch = "wasm32")]
pub mod web;

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

use bevy::prelude::*;
use serde::{Deserialize, Serialize};

pub use audio::{AudioOutput, AudioSource, Mixer, RemoteTrack, Running, VoiceDecoder};
pub use devices::{AudioDevice, AudioDevices, CameraDevice, CameraDevices, Permission};
#[cfg(not(target_arch = "wasm32"))]
pub use native::{Microphone, Speaker};
#[cfg(feature = "webrtc")]
pub use rtc::{MediaPath, RtcSettings, RtcSignal};
pub use transport::{ALPN, Link, MediaHub, RtcLink, TrackKind};
pub use video::{
    FeedStats, Pixels, RemoteVideo, RgbaFrame, TestPattern, VideoConfig, VideoFrame, VideoSource,
    VideoTrackStats,
};

use crate::{
    node::Iroh,
    replicate::{NetId, Owner, Remote, ReplicateAppExt, Shared},
};

/// This entity speaks: publish the microphone under its id. Replicated, so peers subscribe.
#[derive(Component, Debug, Clone, Default, Serialize, Deserialize)]
pub struct Voice {
    /// Nothing is sent while muted. Replicated, so peers can show it.
    pub muted: bool,
}

/// Loudness for a meter, 0 to 1 on a decibel scale (-60 dB to 0 dB), with a 12 ms attack and
/// a 300 ms release. On local `Voice` entities from the microphone, on remote ones from what
/// was decoded. Loudness is not linear: a meter on raw amplitude sits at the bottom for
/// everything anybody actually says.
#[derive(Component, Debug, Clone, Copy, Default, Deref)]
pub struct VoiceLevel(pub f32);

/// What a remote voice has been through, updated every frame. `received` advancing between
/// two looks is the sign of life; `starved` advancing is the network or the sender.
#[derive(Component, Debug, Clone, Copy, Default, Deref)]
pub struct VoiceStats(pub audio::TrackStats);

/// The microphone's level for a meter in a corner of the screen, 0 to 1: the square root of
/// the loudest sample since last frame, which spreads the quiet end, falling at 2.5 per
/// second so a syllable stays on screen long enough to be seen. Zero while every local
/// [`Voice`] is muted, and whenever the microphone is not open.
#[derive(Resource, Debug, Clone, Copy, Default, Deref)]
pub struct MicLevel(pub f32);

/// This entity shows a picture: publish frames under its id. Replicated, so peers subscribe.
/// Pair it locally with a [`VideoInput`] naming where the frames come from. The size is what
/// the source produces; it is corrected once the first frame is seen.
#[derive(Component, Debug, Clone, Serialize, Deserialize)]
pub struct VideoFeed {
    pub width: u32,
    pub height: u32,
}

impl VideoFeed {
    pub fn new(width: u32, height: u32) -> Self {
        Self { width, height }
    }
}

/// The local half of a [`VideoFeed`]: the source and how to encode it. Not replicated.
#[derive(Component)]
pub struct VideoInput {
    kind: InputKind,
    pub config: VideoConfig,
    /// Also keep a [`VideoImage`] of what is being sent on this entity, so the app can show
    /// the person their own picture the same way it shows everyone else's.
    pub preview: bool,
}

enum InputKind {
    /// The camera named in [`MediaSettings::camera`], reopened when that changes.
    Camera,
    Source(Mutex<Option<Box<dyn VideoSource>>>),
}

impl VideoInput {
    /// Frames from your own source.
    pub fn new(source: impl VideoSource) -> Self {
        Self {
            kind: InputKind::Source(Mutex::new(Some(Box::new(source)))),
            config: VideoConfig::default(),
            preview: false,
        }
    }

    /// Frames from the camera chosen in [`MediaSettings`]: a `bevy_v4l2` device on Linux
    /// with the `v4l2` feature, `getUserMedia` in a page. Opened at about the size the
    /// entity's [`VideoFeed`] asks for.
    pub fn camera() -> Self {
        Self {
            kind: InputKind::Camera,
            config: VideoConfig::default(),
            preview: true,
        }
    }

    pub fn with_config(mut self, config: VideoConfig) -> Self {
        self.config = config;
        self
    }

    pub fn with_preview(mut self, preview: bool) -> Self {
        self.preview = preview;
        self
    }
}

/// On a remote [`VideoFeed`] entity once its first frame has decoded, and on a local one
/// with a preview: the picture, kept current. Put it on any material.
#[derive(Component, Debug, Clone, Deref)]
pub struct VideoImage(pub Handle<Image>);

/// What a remote video feed has been through, updated every frame.
#[derive(Component, Debug, Clone, Copy, Default, Deref)]
pub struct VideoStats(pub VideoTrackStats);

/// What a local video feed has been through.
#[derive(Component, Debug, Clone, Copy, Default, Deref)]
pub struct VideoFeedStats(pub FeedStats);

/// Where the ears are. Put it on the camera. Without one, the listener is at the origin.
#[derive(Component, Debug, Clone, Copy, Default)]
pub struct AudioListener;

/// Which devices media uses. Change a field and the device is reopened; what is published
/// keeps its track ids, so peers hear the switch as a moment's silence and nothing else.
#[derive(Resource)]
pub struct MediaSettings {
    pub microphone: MicrophoneChoice,
    pub speaker: SpeakerChoice,
    pub camera: CameraChoice,
    /// Metres at which a voice fades to silence.
    pub voice_range: f32,
    /// Master gain on everything heard, 0 to 1 (and above, if you must).
    pub output_volume: f32,
}

impl Default for MediaSettings {
    fn default() -> Self {
        Self {
            microphone: MicrophoneChoice::Default,
            speaker: SpeakerChoice::Default,
            camera: CameraChoice::Default,
            voice_range: 10.0,
            output_volume: 1.0,
        }
    }
}

pub enum MicrophoneChoice {
    /// The host's default input.
    Default,
    None,
    /// One from [`AudioDevices::microphones`], by id.
    Device(String),
    /// Your own source: a test tone, a file, a different capture library.
    Custom(Mutex<Option<Box<dyn AudioSource>>>),
}

pub enum SpeakerChoice {
    Default,
    None,
    /// One from [`AudioDevices::speakers`], by id. Ignored in a page, which plays through
    /// whatever the browser plays through.
    Device(String),
    Custom(Mutex<Option<Box<dyn AudioOutput>>>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CameraChoice {
    /// The first camera the machine names.
    Default,
    None,
    /// One from [`CameraDevices::cameras`], by id.
    Device(String),
}

impl MicrophoneChoice {
    pub fn custom(source: impl AudioSource) -> Self {
        MicrophoneChoice::Custom(Mutex::new(Some(Box::new(source))))
    }

    /// What this choice is, for telling two apart. A custom source is a fresh key every time
    /// the settings change, since a new source may have been put in the slot.
    fn key(&self, generation: u64) -> String {
        match self {
            MicrophoneChoice::Default => "default".into(),
            MicrophoneChoice::None => "none".into(),
            MicrophoneChoice::Device(id) => format!("device:{id}"),
            MicrophoneChoice::Custom(_) => format!("custom:{generation}"),
        }
    }
}

impl SpeakerChoice {
    pub fn custom(output: impl AudioOutput) -> Self {
        SpeakerChoice::Custom(Mutex::new(Some(Box::new(output))))
    }

    fn key(&self, generation: u64) -> String {
        match self {
            SpeakerChoice::Default => "default".into(),
            SpeakerChoice::None => "none".into(),
            SpeakerChoice::Device(id) => format!("device:{id}"),
            SpeakerChoice::Custom(_) => format!("custom:{generation}"),
        }
    }
}

impl CameraChoice {
    fn key(&self) -> String {
        match self {
            CameraChoice::Default => "default".into(),
            CameraChoice::None => "none".into(),
            CameraChoice::Device(id) => format!("device:{id}"),
        }
    }

    /// The id to open, or `None` for the default. `Err` when no camera is wanted.
    pub(crate) fn id(&self) -> Result<Option<&str>, ()> {
        match self {
            CameraChoice::Default => Ok(None),
            CameraChoice::None => Err(()),
            CameraChoice::Device(id) => Ok(Some(id)),
        }
    }
}

/// How long after a device fails to open before it is tried again.
const DEVICE_RETRY: f64 = 5.0;

/// The hub as a resource, plus the threads it has started.
#[derive(Resource)]
pub struct Media {
    pub hub: Arc<MediaHub>,
    /// Owners whose session dropped this frame, for every subscribe system to see.
    dead: Vec<iroh::EndpointId>,
    encoder: Option<Encoder>,
    mic_key: String,
    mic_retry_at: f64,
    speaker: Option<Running>,
    speaker_key: String,
    speaker_retry_at: f64,
    camera_key: String,
    /// Bumped every time the settings change, to tell one custom source from the next.
    generation: u64,
    /// The master gain, shared with the mixer, as f32 bits.
    volume: Arc<AtomicU32>,
}

struct Encoder {
    shared: Arc<audio::EncoderShared>,
    #[cfg(not(target_arch = "wasm32"))]
    thread: Option<std::thread::JoinHandle<()>>,
}

impl Drop for Encoder {
    fn drop(&mut self) {
        self.shared.stop.store(true, Ordering::Relaxed);
        #[cfg(not(target_arch = "wasm32"))]
        if let Some(thread) = self.thread.take() {
            let _ = thread.join();
        }
    }
}

/// Per local video feed: the encoder's shared state; dropping it stops the encoder.
#[derive(Component)]
struct Encoding(Arc<video::EncoderShared>);

impl Drop for Encoding {
    fn drop(&mut self) {
        self.0.stop.store(true, Ordering::Relaxed);
    }
}

/// Per local video feed whose camera would not open: when to try again.
#[derive(Component)]
struct CameraRetry(f64);

impl Media {
    pub(crate) fn new(hub: Arc<MediaHub>) -> Self {
        Self {
            hub,
            dead: Vec::new(),
            encoder: None,
            mic_key: String::new(),
            mic_retry_at: 0.0,
            speaker: None,
            speaker_key: String::new(),
            speaker_retry_at: 0.0,
            camera_key: String::new(),
            generation: 0,
            volume: Arc::new(AtomicU32::new(1.0f32.to_bits())),
        }
    }

    fn ensure_encoder(&mut self, settings: &MediaSettings, now: f64) {
        if self.encoder.is_some() || now < self.mic_retry_at {
            return;
        }
        self.mic_key = settings.microphone.key(self.generation);
        let shared = Arc::new(audio::EncoderShared::default());
        #[cfg(not(target_arch = "wasm32"))]
        {
            let source: Option<Box<dyn AudioSource>> = match &settings.microphone {
                MicrophoneChoice::None => None,
                // An empty slot is a source that is not ready yet, not one that failed: the
                // app fills it when its device opens, and this is asked again until then.
                MicrophoneChoice::Custom(slot) => {
                    slot.lock().unwrap_or_else(|e| e.into_inner()).take()
                }
                MicrophoneChoice::Default | MicrophoneChoice::Device(_) => {
                    let id = match &settings.microphone {
                        MicrophoneChoice::Device(id) => Some(id.as_str()),
                        _ => None,
                    };
                    match Microphone::open(id) {
                        Ok(m) => Some(Box::new(m)),
                        Err(e) => {
                            error!("bevy_iroh: microphone: {e}");
                            self.mic_retry_at = now + DEVICE_RETRY;
                            None
                        }
                    }
                }
            };
            let Some(source) = source else {
                return;
            };
            info!("bevy_iroh: microphone at {} Hz", source.sample_rate());
            let (hub, s) = (self.hub.clone(), shared.clone());
            match std::thread::Builder::new()
                .name("bevy_iroh-voice".into())
                .spawn(move || audio::run_encoder(source, hub, s))
            {
                Ok(thread) => {
                    self.encoder = Some(Encoder {
                        shared,
                        thread: Some(thread),
                    })
                }
                Err(e) => {
                    error!("bevy_iroh: voice encoder: {e}");
                    self.mic_retry_at = now + DEVICE_RETRY;
                }
            }
        }
        #[cfg(target_arch = "wasm32")]
        {
            match web::audio::start_encoder(&settings.microphone, self.hub.clone(), shared.clone())
            {
                Ok(true) => self.encoder = Some(Encoder { shared }),
                Ok(false) => {}
                Err(e) => {
                    error!("bevy_iroh: microphone: {e}");
                    self.mic_retry_at = now + DEVICE_RETRY;
                }
            }
        }
    }

    /// Close the microphone so the next local `Voice` opens it again, from whatever
    /// `MediaSettings::microphone` says now. Done for you when the settings change.
    pub fn restart_microphone(&mut self) {
        self.encoder = None;
        self.mic_retry_at = 0.0;
    }

    /// The same for the speaker.
    pub fn restart_speaker(&mut self) {
        self.speaker = None;
        self.speaker_retry_at = 0.0;
    }

    fn ensure_speaker(&mut self, settings: &MediaSettings, now: f64) {
        if self.speaker.is_some() || now < self.speaker_retry_at {
            return;
        }
        self.speaker_key = settings.speaker.key(self.generation);
        let output: Option<Box<dyn AudioOutput>> = match &settings.speaker {
            SpeakerChoice::None => None,
            SpeakerChoice::Custom(slot) => slot.lock().unwrap_or_else(|e| e.into_inner()).take(),
            #[cfg(not(target_arch = "wasm32"))]
            SpeakerChoice::Default => Some(Box::new(Speaker::default())),
            #[cfg(not(target_arch = "wasm32"))]
            SpeakerChoice::Device(id) => Some(Box::new(Speaker::open(Some(id)))),
            #[cfg(target_arch = "wasm32")]
            SpeakerChoice::Default | SpeakerChoice::Device(_) => {
                web::audio::set_sink(match &settings.speaker {
                    SpeakerChoice::Device(id) => Some(id.as_str()),
                    _ => None,
                });
                Some(Box::new(web::audio::Speaker))
            }
        };
        let Some(output) = output else {
            return;
        };
        let mixer = Arc::new(Mutex::new(Mixer::new(
            self.hub.clone(),
            self.volume.clone(),
        )));
        match output.start(mixer) {
            Ok(running) => self.speaker = Some(running),
            Err(e) => {
                error!("bevy_iroh: speaker: {e}");
                self.speaker_retry_at = now + DEVICE_RETRY;
            }
        }
    }

    /// The microphone's meters, if it is open.
    pub(crate) fn mic_meters(&self) -> Option<&audio::EncoderShared> {
        self.encoder.as_ref().map(|e| &*e.shared)
    }
}

/// Per remote voice entity: the subscription's owner, and when to try again if it dropped.
#[derive(Component)]
struct Subscribed {
    owner: iroh::EndpointId,
    retry_at: f64,
}

/// The same for a remote video feed. Separate, since one entity can carry both.
#[derive(Component)]
struct VideoSubscribed {
    owner: iroh::EndpointId,
    retry_at: f64,
}

/// Per local voice entity: the mute flag shared with the encoder.
#[derive(Component)]
struct Published(Arc<AtomicBool>);

pub(crate) struct MediaPlugin {
    pub hub: Arc<MediaHub>,
}

impl Plugin for MediaPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<MediaSettings>()
            .init_resource::<MicLevel>()
            .insert_resource(AudioDevices {
                // A desktop lists its devices at startup; a page waits to be asked.
                scan_wanted: cfg!(not(target_arch = "wasm32")),
                ..default()
            })
            .insert_resource(CameraDevices {
                scan_wanted: cfg!(not(target_arch = "wasm32")),
                ..default()
            })
            .insert_resource(Media::new(self.hub.clone()))
            .replicate::<Voice>()
            .replicate::<VideoFeed>()
            .add_systems(
                Update,
                (
                    devices::scan_audio,
                    devices::scan_cameras,
                    apply_settings,
                    collect_dead,
                    publish,
                    subscribe,
                    spatialize,
                    levels,
                    publish_video,
                    subscribe_video,
                    video_frames,
                )
                    .chain(),
            )
            .add_observer(on_voice_removed)
            .add_observer(on_video_removed);
        #[cfg(target_arch = "wasm32")]
        app.add_systems(Update, (devices::poll_web_devices, web::sweep));
        #[cfg(feature = "webrtc")]
        app.add_plugins(rtc::RtcPlugin);
    }
}

/// A changed setting reopens the device it names. The rest of the pipeline is untouched: the
/// remote tracks live in the hub, the published tracks keep their ids.
fn apply_settings(
    mut media: ResMut<Media>,
    settings: Res<MediaSettings>,
    mut feeds: Query<Entity, (With<VideoInput>, With<Encoding>)>,
    inputs: Query<&VideoInput>,
    mut commands: Commands,
) {
    if !settings.is_changed() {
        return;
    }
    media.generation += 1;
    let generation = media.generation;
    media
        .volume
        .store(settings.output_volume.max(0.0).to_bits(), Ordering::Relaxed);
    if settings.microphone.key(generation) != media.mic_key {
        media.restart_microphone();
    }
    if settings.speaker.key(generation) != media.speaker_key {
        media.restart_speaker();
    }
    if settings.camera.key() != media.camera_key {
        media.camera_key = settings.camera.key();
        for entity in &mut feeds {
            if matches!(inputs.get(entity).map(|i| &i.kind), Ok(InputKind::Camera)) {
                commands
                    .entity(entity)
                    .remove::<(Encoding, CameraRetry, VideoFeedStats, VideoImage)>();
            }
        }
    }
}

fn publish(
    mut commands: Commands,
    mut media: ResMut<Media>,
    settings: Res<MediaSettings>,
    time: Res<Time<bevy::time::Real>>,
    voices: Query<(Entity, &NetId, &Voice, Option<&Published>), (With<Shared>, Without<Remote>)>,
) {
    let now = time.elapsed_secs_f64();
    for (entity, id, voice, published) in &voices {
        match published {
            Some(p) => p.0.store(voice.muted, Ordering::Relaxed),
            None => {
                let muted = media.hub.publish(id.0);
                muted.store(voice.muted, Ordering::Relaxed);
                commands
                    .entity(entity)
                    .insert((Published(muted), VoiceLevel::default()));
            }
        }
        media.ensure_encoder(&settings, now);
    }
}

const RESUBSCRIBE_AFTER: f64 = 0.5;

fn subscribe(
    mut commands: Commands,
    iroh: Option<Res<Iroh>>,
    mut media: ResMut<Media>,
    settings: Res<MediaSettings>,
    time: Res<Time<bevy::time::Real>>,
    mut voices: Query<
        (Entity, &NetId, &Owner, Option<&mut Subscribed>),
        (With<Voice>, With<Remote>),
    >,
) {
    let Some(iroh) = iroh else { return };
    let now = time.elapsed_secs_f64();
    let dead = media.dead.clone();
    for (entity, id, owner, subscribed) in &mut voices {
        media.ensure_speaker(&settings, now);
        let due = match subscribed {
            None => true,
            Some(mut s) => {
                if dead.contains(&s.owner) {
                    s.retry_at = now + RESUBSCRIBE_AFTER;
                }
                let due = s.retry_at > 0.0 && now >= s.retry_at;
                if due {
                    s.retry_at = 0.0;
                }
                due
            }
        };
        if !due {
            continue;
        }
        let Some(endpoint) = endpoint(&iroh) else {
            continue;
        };
        let (hub, owner_id, track) = (media.hub.clone(), owner.0, id.0);
        iroh.spawn(async move {
            if let Err(e) = hub
                .subscribe(endpoint, owner_id, track, TrackKind::Audio)
                .await
            {
                debug!("bevy_iroh: subscribe to {:016x}: {e:#}", track);
                hub.mark_dead(owner_id);
            }
        });
        commands.entity(entity).insert((
            Subscribed {
                owner: owner.0,
                retry_at: 0.0,
            },
            VoiceLevel::default(),
        ));
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn endpoint(iroh: &Iroh) -> Option<iroh::Endpoint> {
    Some(iroh.endpoint())
}

#[cfg(target_arch = "wasm32")]
fn endpoint(iroh: &Iroh) -> Option<iroh::Endpoint> {
    iroh.endpoint()
}

fn on_voice_removed(
    remove: On<Remove, Voice>,
    iroh: Option<Res<Iroh>>,
    media: Res<Media>,
    q: Query<(&NetId, Has<Remote>, Option<&Subscribed>)>,
) {
    let Ok((id, remote, subscribed)) = q.get(remove.entity) else {
        return;
    };
    if remote {
        if let (Some(iroh), Some(s)) = (iroh, subscribed) {
            let (hub, owner, track) = (media.hub.clone(), s.owner, id.0);
            iroh.spawn(async move { hub.unsubscribe(owner, track).await });
        } else {
            media.hub.forget_remote(id.0);
        }
    } else {
        media.hub.unpublish(id.0);
    }
}

fn spatialize(
    media: Res<Media>,
    settings: Res<MediaSettings>,
    listener: Query<&GlobalTransform, With<AudioListener>>,
    voices: Query<(&NetId, Option<&GlobalTransform>), (With<Voice>, With<Remote>)>,
) {
    // No ears is not the same as ears at the origin: leave every gain as it is.
    let Some(listener) = listener.iter().next() else {
        return;
    };
    let (_, rotation, position) = listener.to_scale_rotation_translation();
    let right = rotation * Vec3::X;
    for (id, transform) in &voices {
        let Some(track) = media.hub.remote(id.0) else {
            continue;
        };
        let Some(transform) = transform else {
            track.set_gain(1.0);
            track.set_pan(0.0);
            continue;
        };
        let offset = transform.translation() - position;
        let distance = offset.length();
        let gain = (1.0 - distance / settings.voice_range.max(0.01)).clamp(0.0, 1.0);
        let pan = if distance > 0.05 {
            right.dot(offset / distance)
        } else {
            0.0
        };
        track.set_gain(gain);
        track.set_pan(pan);
    }
}

fn levels(
    media: Res<Media>,
    time: Res<Time<bevy::time::Real>>,
    mut mic: ResMut<MicLevel>,
    mut commands: Commands,
    mut voices: Query<(
        Entity,
        &NetId,
        &mut VoiceLevel,
        Has<Remote>,
        Option<&mut VoiceStats>,
    )>,
) {
    const FLOOR_DB: f32 = -60.0;
    const ATTACK: f32 = 0.012;
    const RELEASE: f32 = 0.30;
    /// How fast the corner meter falls, per second.
    const MIC_DECAY: f32 = 2.5;
    let dt = time.delta_secs();
    // The corner meter: instant attack, linear release, off the peak.
    let peak = media.mic_meters().map(|m| m.take_peak()).unwrap_or(0.0);
    let shown = peak.min(1.0).sqrt();
    let level = shown.max(mic.0 - MIC_DECAY * dt);
    if (mic.0 - level).abs() > 1e-4 {
        mic.0 = level;
    }
    for (entity, id, mut level, remote, stats) in &mut voices {
        let rms = if remote {
            media.hub.remote(id.0).map(|t| t.level()).unwrap_or(0.0)
        } else {
            media.mic_meters().map(|m| m.level()).unwrap_or(0.0)
        };
        let db = 20.0 * rms.max(1e-9).log10();
        let target = ((db - FLOOR_DB) / -FLOOR_DB).clamp(0.0, 1.0);
        let tau = if target > level.0 { ATTACK } else { RELEASE };
        let alpha = 1.0 - (-dt / tau.max(1e-3)).exp();
        let value = level.0 + (target - level.0) * alpha;
        if (level.0 - value).abs() > 1e-4 {
            level.0 = value;
        }
        if remote && let Some(track) = media.hub.remote(id.0) {
            let now = track.stats();
            match stats {
                Some(mut s) => {
                    if s.0 != now {
                        s.0 = now;
                    }
                }
                None => {
                    commands.entity(entity).insert(VoiceStats(now));
                }
            }
        }
    }
}

fn collect_dead(mut media: ResMut<Media>) {
    media.dead = media.hub.take_dead();
}

fn publish_video(
    mut commands: Commands,
    iroh: Option<Res<Iroh>>,
    media: Res<Media>,
    settings: Res<MediaSettings>,
    time: Res<Time<bevy::time::Real>>,
    mut feeds: Query<
        (
            Entity,
            &NetId,
            &VideoInput,
            &mut VideoFeed,
            Option<&CameraRetry>,
            Option<&Encoding>,
            Option<&mut VideoFeedStats>,
        ),
        (With<Shared>, Without<Remote>),
    >,
) {
    let Some(iroh) = iroh else { return };
    let now = time.elapsed_secs_f64();
    for (entity, id, input, mut feed, retry, encoding, stats) in &mut feeds {
        if let Some(encoding) = encoding {
            // Running: keep the announced size honest and the counters current.
            if let Some((w, h)) = encoding.0.size()
                && (feed.width, feed.height) != (w, h)
            {
                feed.width = w;
                feed.height = h;
            }
            let fresh = encoding.0.stats();
            match stats {
                Some(mut s) => {
                    if s.0 != fresh {
                        s.0 = fresh;
                    }
                }
                None => {
                    commands.entity(entity).insert(VideoFeedStats(fresh));
                }
            }
            continue;
        }
        if retry.is_some_and(|r| now < r.0) {
            continue;
        }
        let source: Option<Box<dyn VideoSource>> = match &input.kind {
            InputKind::Source(slot) => slot.lock().unwrap_or_else(|e| e.into_inner()).take(),
            InputKind::Camera => {
                let Ok(camera) = settings.camera.id() else {
                    continue;
                };
                match open_camera(camera, feed.width, feed.height, input.config.max_fps) {
                    Ok(source) => Some(source),
                    Err(e) => {
                        error!("bevy_iroh: camera: {e}");
                        commands
                            .entity(entity)
                            .insert(CameraRetry(now + DEVICE_RETRY));
                        None
                    }
                }
            }
        };
        let Some(source) = source else {
            continue;
        };
        let track = id.0;
        media.hub.publish(track);
        let keyframe = media.hub.keyframe_flag(track);
        let shared = Arc::new(video::EncoderShared::new(input.preview));
        let (tx, rx) = tokio::sync::mpsc::channel(video::ENCODER_QUEUE);
        let hub = media.hub.clone();
        iroh.spawn(async move { video::run_publisher(hub, track, rx).await });
        #[cfg(not(target_arch = "wasm32"))]
        {
            let (config, s) = (input.config.clone(), shared.clone());
            if let Err(e) = std::thread::Builder::new()
                .name("bevy_iroh-encode".into())
                .spawn(move || video::run_encoder(source, config, keyframe, s, tx))
            {
                error!("bevy_iroh: video encoder: {e}");
                continue;
            }
        }
        #[cfg(target_arch = "wasm32")]
        web::video::start_encoder(source, input.config.clone(), keyframe, shared.clone(), tx);
        commands
            .entity(entity)
            .insert(Encoding(shared))
            .remove::<CameraRetry>();
    }
}

/// The platform's camera, as a source.
fn open_camera(
    id: Option<&str>,
    width: u32,
    height: u32,
    fps: f32,
) -> Result<Box<dyn VideoSource>, String> {
    #[cfg(all(feature = "v4l2", target_os = "linux"))]
    {
        v4l2::Camera::open(id, width, height, fps).map(|c| Box::new(c) as Box<dyn VideoSource>)
    }
    #[cfg(target_arch = "wasm32")]
    {
        web::video::Camera::open(id, width, height, fps)
            .map(|c| Box::new(c) as Box<dyn VideoSource>)
    }
    #[cfg(not(any(all(feature = "v4l2", target_os = "linux"), target_arch = "wasm32")))]
    {
        let _ = (id, width, height, fps);
        Err("no camera backend on this platform: enable the `v4l2` feature on Linux, or give the entity a VideoInput of your own".into())
    }
}

fn subscribe_video(
    mut commands: Commands,
    iroh: Option<Res<Iroh>>,
    media: Res<Media>,
    time: Res<Time<bevy::time::Real>>,
    mut feeds: Query<
        (Entity, &NetId, &Owner, Option<&mut VideoSubscribed>),
        (With<VideoFeed>, With<Remote>),
    >,
) {
    let Some(iroh) = iroh else { return };
    let now = time.elapsed_secs_f64();
    let dead = media.dead.clone();
    for (entity, id, owner, subscribed) in &mut feeds {
        let due = match subscribed {
            None => true,
            Some(mut s) => {
                if dead.contains(&s.owner) {
                    s.retry_at = now + RESUBSCRIBE_AFTER;
                }
                let due = s.retry_at > 0.0 && now >= s.retry_at;
                if due {
                    s.retry_at = 0.0;
                }
                due
            }
        };
        if !due {
            continue;
        }
        let Some(endpoint) = endpoint(&iroh) else {
            continue;
        };
        let (hub, owner_id, track) = (media.hub.clone(), owner.0, id.0);
        iroh.spawn(async move {
            if let Err(e) = hub
                .subscribe(endpoint, owner_id, track, TrackKind::Video)
                .await
            {
                debug!("bevy_iroh: subscribe to video {:016x}: {e:#}", track);
                hub.mark_dead(owner_id);
            }
        });
        commands.entity(entity).insert(VideoSubscribed {
            owner: owner.0,
            retry_at: 0.0,
        });
    }
}

/// Newest decoded frames into images: remote feeds from their decoder, local ones from the
/// preview slot.
fn video_frames(
    mut commands: Commands,
    media: Res<Media>,
    images: Option<ResMut<Assets<Image>>>,
    mut feeds: Query<
        (
            Entity,
            &NetId,
            Option<&VideoImage>,
            Has<Remote>,
            Option<&Encoding>,
            Option<&mut VideoStats>,
        ),
        With<VideoFeed>,
    >,
) {
    let Some(mut images) = images else { return };
    for (entity, id, image, remote, encoding, stats) in &mut feeds {
        let frame = if remote {
            let Some(track) = media.hub.remote_video(id.0) else {
                continue;
            };
            let fresh = track.stats();
            match stats {
                Some(mut s) => {
                    if s.0 != fresh {
                        s.0 = fresh;
                    }
                }
                None => {
                    commands.entity(entity).insert(VideoStats(fresh));
                }
            }
            track.take_frame()
        } else {
            encoding.and_then(|e| e.0.take_preview())
        };
        let Some(frame) = frame else {
            continue;
        };
        // Built from the default image rather than by naming texture types, which come from
        // whichever wgpu this bevy is built against.
        let reusable = image
            .filter(|i| {
                images.get(&i.0).is_some_and(|img| {
                    img.texture_descriptor.size.width == frame.width
                        && img.texture_descriptor.size.height == frame.height
                })
            })
            .map(|i| i.0.clone());
        if let Some(handle) = reusable {
            if let Some(mut img) = images.get_mut(&handle) {
                img.data = Some(frame.data);
            }
            continue;
        }
        let mut img = Image::default();
        img.texture_descriptor.size.width = frame.width;
        img.texture_descriptor.size.height = frame.height;
        img.texture_descriptor.size.depth_or_array_layers = 1;
        img.asset_usage = bevy::asset::RenderAssetUsages::MAIN_WORLD
            | bevy::asset::RenderAssetUsages::RENDER_WORLD;
        img.data = Some(frame.data);
        let handle = images.add(img);
        commands.entity(entity).insert(VideoImage(handle));
    }
}

fn on_video_removed(
    remove: On<Remove, VideoFeed>,
    iroh: Option<Res<Iroh>>,
    media: Res<Media>,
    q: Query<(&NetId, Has<Remote>, Option<&VideoSubscribed>)>,
    mut commands: Commands,
) {
    let Ok((id, remote, subscribed)) = q.get(remove.entity) else {
        return;
    };
    if remote {
        if let (Some(iroh), Some(s)) = (iroh, subscribed) {
            let (hub, owner, track) = (media.hub.clone(), s.owner, id.0);
            iroh.spawn(async move { hub.unsubscribe(owner, track).await });
        } else {
            media.hub.forget_remote(id.0);
        }
    } else {
        media.hub.unpublish(id.0);
        commands
            .entity(remove.entity)
            .remove::<(Encoding, VideoImage, VideoFeedStats)>();
    }
}