medea-jason 0.14.2

Client library for Medea media server.
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
//! Wrapper around [MediaStreamTrack][1].
//!
//! [1]: https://w3.org/TR/mediacapture-streams#mediastreamtrack

use std::{cell::RefCell, rc::Rc, time::Duration};

use derive_more::{Debug, with_trait::AsRef};
use futures::{StreamExt as _, future, stream::LocalBoxStream};
use js_sys::{Error as JsError, Reflect};
use medea_reactive::ObservableCell;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;

use crate::{
    media::{
        FacingMode, MediaKind, MediaSourceKind, NoiseSuppressionLevel,
        track::MediaStreamTrackState,
    },
    platform::{self, wasm::utils::EventListener},
};

/// Wrapper around [MediaStreamTrack][1] received from a
/// [getUserMedia()][2]/[getDisplayMedia()][3] request.
///
/// [1]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
/// [2]: https://w3.org/TR/mediacapture-streams#dom-mediadevices-getusermedia
/// [3]: https://w3.org/TR/screen-capture/#dom-mediadevices-getdisplaymedia
#[derive(AsRef, Debug)]
pub struct MediaStreamTrack {
    /// Underlying [MediaStreamTrack][1].
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    #[as_ref(forward)]
    sys_track: Rc<web_sys::MediaStreamTrack>,

    /// Kind of the underlying [MediaStreamTrack][1].
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    kind: MediaKind,

    /// Media source kind of this [MediaStreamTrack][1].
    ///
    /// [`None`] for remote tracks.
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    source_kind: Option<MediaSourceKind>,

    /// Listener for an [ended][1] event.
    ///
    /// [1]: https://tinyurl.com/w3-streams#event-mediastreamtrack-ended
    on_ended: RefCell<
        Option<EventListener<web_sys::MediaStreamTrack, web_sys::Event>>,
    >,

    /// Listener of audio level [changes][1] in this [`MediaStreamTrack`] (if
    /// it's a local one).
    ///
    /// [1]: https://tinyurl.com/w3-streams#event-mediastreamtrack-ended
    #[expect(clippy::type_complexity, reason = "not really")]
    #[debug(skip)]
    on_audio_level: Rc<RefCell<Option<Box<dyn FnMut(i32)>>>>,

    /// [`AudioLevelWatcher`] of the underlying [MediaStreamTrack][1].
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    audio_level_watcher: Rc<RefCell<Option<AudioLevelWatcher>>>,
}

impl MediaStreamTrack {
    /// Creates a new [`MediaStreamTrack`].
    #[must_use]
    pub fn new<T>(sys_track: T, source_kind: Option<MediaSourceKind>) -> Self
    where
        web_sys::MediaStreamTrack: From<T>,
    {
        let sys_track = web_sys::MediaStreamTrack::from(sys_track);
        let kind = match sys_track.kind().as_ref() {
            "audio" => MediaKind::Audio,
            "video" => MediaKind::Video,
            _ => unreachable!(),
        };
        Self {
            sys_track: Rc::new(sys_track),
            source_kind,
            kind,
            on_ended: RefCell::new(None),
            on_audio_level: Rc::new(RefCell::new(None)),
            audio_level_watcher: Rc::new(RefCell::new(None)),
        }
    }

    /// Returns [`id`] of the underlying [MediaStreamTrack][2].
    ///
    /// [`id`]: https://w3.org/TR/mediacapture-streams#dom-mediastreamtrack-id
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    #[must_use]
    pub fn id(&self) -> String {
        self.sys_track.id()
    }

    /// Returns this [`MediaStreamTrack`]'s kind (audio/video).
    #[must_use]
    pub const fn kind(&self) -> MediaKind {
        self.kind
    }

    /// Returns [MediaStreamTrackState][1] of the underlying
    /// [MediaStreamTrack][2].
    ///
    /// # Panics
    ///
    /// If [`readyState`][3] property of underlying [MediaStreamTrack][2] is
    /// neither `live` nor `ended`.
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#dom-mediastreamtrackstate
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    /// [3]: https://tinyurl.com/w3-streams#dom-mediastreamtrack-readystate
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn ready_state(&self) -> MediaStreamTrackState {
        let state = self.sys_track.ready_state();
        match state {
            web_sys::MediaStreamTrackState::Live => MediaStreamTrackState::Live,
            web_sys::MediaStreamTrackState::Ended => {
                MediaStreamTrackState::Ended
            }
            _ => {
                unreachable!("unknown `MediaStreamTrackState`: {state:?}")
            }
        }
    }

    /// Returns a [`deviceId`][1] of the underlying [MediaStreamTrack][2].
    ///
    /// # Panics
    ///
    /// If the underlying [MediaStreamTrack][2] doesn't have [`deviceId`][1].
    ///
    /// [1]: https://tinyurl.com/w3-streams#dom-mediatracksettings-deviceid
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    #[must_use]
    pub fn device_id(&self) -> Option<String> {
        self.sys_track.get_settings().get_device_id()
    }

    /// Return a [`facingMode`][1] of the underlying [MediaStreamTrack][2].
    ///
    /// [1]: https://tinyurl.com/w3-streams#dom-mediatracksettings-facingmode
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    #[must_use]
    pub fn facing_mode(&self) -> Option<FacingMode> {
        let facing_mode = self.sys_track.get_settings().get_facing_mode()?;
        match facing_mode.as_ref() {
            "user" => Some(FacingMode::User),
            "environment" => Some(FacingMode::Environment),
            "left" => Some(FacingMode::Left),
            "right" => Some(FacingMode::Right),
            _ => {
                log::error!("Unknown `FacingMode`: {facing_mode}");
                None
            }
        }
    }

    /// Returns a [`height`][1] of the underlying [MediaStreamTrack][2].
    ///
    /// [1]: https://tinyurl.com/w3-streams#dom-mediatracksettings-height
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    #[must_use]
    pub fn height(&self) -> Option<u32> {
        let h = self.sys_track.get_settings().get_height()?;
        h.try_into().ok()
    }

    /// Return a [`width`][1] of the underlying [MediaStreamTrack][2].
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#dom-mediatracksettings-width
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    #[must_use]
    pub fn width(&self) -> Option<u32> {
        let w = self.sys_track.get_settings().get_width()?;
        w.try_into().ok()
    }

    /// Changes an [`enabled`][1] attribute in the underlying
    /// [MediaStreamTrack][2].
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#dom-mediastreamtrack-enabled
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    pub fn set_enabled(&self, enabled: bool) {
        self.sys_track.set_enabled(enabled);
    }

    /// Changes a [`readyState`][1] attribute in the underlying
    /// [MediaStreamTrack][2] to [`ended`][3].
    ///
    /// [1]: https://tinyurl.com/w3-streams#dom-mediastreamtrack-readystate
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    /// [3]: https://tinyurl.com/w3-streams#idl-def-MediaStreamTrackState.ended
    pub fn stop(&self) -> impl Future<Output = ()> + 'static + use<> {
        self.sys_track.stop();
        // For platform code uniformity.
        future::ready(())
    }

    /// Returns an [`enabled`][1] attribute of the underlying
    /// [MediaStreamTrack][2].
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#dom-mediastreamtrack-enabled
    /// [2]: https://w3.org/TR/mediacapture-streams#mediastreamtrack
    #[must_use]
    pub fn enabled(&self) -> bool {
        self.sys_track.enabled()
    }

    /// Returns the [`MediaSourceKind`] of this [`MediaStreamTrack`].
    ///
    /// [`None`] for remote [`MediaStreamTrack`]s.
    #[must_use]
    pub const fn source_kind(&self) -> Option<MediaSourceKind> {
        self.source_kind
    }

    /// Detects whether a video track captured from display searching
    /// [specific fields][1] in its settings.
    ///
    /// Only works in Chrome browser at the moment.
    ///
    /// [1]: https://w3.org/TR/screen-capture/#extensions-to-mediatracksettings
    #[must_use]
    pub fn guess_is_from_display(&self) -> bool {
        self.source_kind == Some(MediaSourceKind::Display)
    }

    /// Forks this [`MediaStreamTrack`].
    ///
    /// Creates a new [`MediaStreamTrack`] from this [`MediaStreamTrack`] using
    /// a [`clone()`][1] method. It won't clone current [`MediaStreamTrack`]'s
    /// callbacks.
    ///
    /// [1]: https://w3.org/TR/mediacapture-streams#dom-mediastreamtrack-clone
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn fork(&self) -> Self {
        Self {
            sys_track: Rc::new(web_sys::MediaStreamTrack::clone(
                &self.sys_track,
            )),
            kind: self.kind,
            source_kind: self.source_kind,
            on_ended: RefCell::new(None),
            on_audio_level: Rc::new(RefCell::new(None)),
            audio_level_watcher: Rc::clone(&self.audio_level_watcher),
        }
    }

    /// Sets handler for the [`ended`][1] event on underlying
    /// [`web_sys::MediaStreamTrack`].
    ///
    /// # Panics
    ///
    /// If binding to the [`ended`][1] event fails. Not supposed to ever happen.
    ///
    /// [1]: https://tinyurl.com/w3-streams#event-mediastreamtrack-ended
    pub fn on_ended<F>(&self, f: Option<F>)
    where
        F: 'static + FnOnce(),
    {
        let mut on_ended = self.on_ended.borrow_mut();
        drop(match f {
            None => on_ended.take(),
            Some(f) => on_ended.replace(
                #[expect(clippy::unwrap_used, reason = "shouldn't error ever")]
                EventListener::new_once(
                    Rc::clone(&self.sys_track),
                    "ended",
                    move |_| {
                        f();
                    },
                )
                .unwrap(),
            ),
        });
    }

    /// Indicates whether an `OnAudioLevelChangedCallback` is supported for this
    /// [`MediaStreamTrack`].
    #[must_use]
    pub fn is_on_audio_level_available(&self) -> bool {
        // Only local audio tracks.
        self.kind == MediaKind::Audio && self.source_kind.is_some()
    }

    /// Sets the provided `OnAudioLevelChangedCallback` for this
    /// [`MediaStreamTrack`].
    ///
    /// It's called for live [`MediaStreamTrack`]s when their audio level
    /// changes.
    ///
    /// # Errors
    ///
    /// If platform call errors.
    pub fn on_audio_level_changed<F>(
        &self,
        cb: F,
    ) -> Result<(), platform::Error>
    where
        F: 'static + FnMut(i32),
    {
        if !self.is_on_audio_level_available() {
            return Ok(());
        }

        self.on_audio_level.borrow_mut().replace(Box::new(cb));
        let callback = Rc::clone(&self.on_audio_level);

        let mut sub = {
            let mut audio_level_watcher = self.audio_level_watcher.borrow_mut();
            if let Some(watcher) = audio_level_watcher.as_ref() {
                watcher.subscribe()
            } else {
                let watcher = AudioLevelWatcher::new(&self.sys_track)?;
                let sub = watcher.subscribe();
                *audio_level_watcher = Some(watcher);

                sub
            }
        };

        platform::spawn(async move {
            while let Some(level) = sub.next().await {
                if let Some(callback) = callback.borrow_mut().as_mut() {
                    callback(level);
                } else {
                    break;
                }
            }
        });

        Ok(())
    }

    /// Indicates whether this [`MediaStreamTrack`] supports audio processing
    /// functions:
    /// - [`MediaStreamTrack::is_noise_suppression_enabled()`]
    /// - [`MediaStreamTrack::set_noise_suppression_enabled()`]
    /// - [`MediaStreamTrack::is_echo_cancellation_enabled()`]
    /// - [`MediaStreamTrack::set_echo_cancellation_enabled()`]
    /// - [`MediaStreamTrack::is_auto_gain_control_enabled()`]
    /// - [`MediaStreamTrack::set_auto_gain_control_enabled()`]
    ///
    /// These functions are unavailable and always error:
    /// - [`MediaStreamTrack::get_noise_suppression_level()`]
    /// - [`MediaStreamTrack::set_noise_suppression_level()`]
    /// - [`MediaStreamTrack::is_high_pass_filter_enabled()`]
    /// - [`MediaStreamTrack::set_high_pass_filter_enabled()`]
    pub fn is_audio_processing_available(&self) -> bool {
        if Reflect::get(&self.sys_track, &JsValue::from_str("getCapabilities"))
            .map_or(None, |val| (!val.is_undefined()).then_some(val))
            .is_none()
        {
            return false;
        }
        if Reflect::get(&self.sys_track, &JsValue::from_str("applyConstraints"))
            .map_or(None, |val| (!val.is_undefined()).then_some(val))
            .is_none()
        {
            return false;
        }
        if Reflect::get(&self.sys_track, &JsValue::from_str("getSettings"))
            .map_or(None, |val| (!val.is_undefined()).then_some(val))
            .is_none()
        {
            return false;
        }

        let caps = self.sys_track.get_capabilities();
        caps.get_echo_cancellation().is_some()
            && caps.get_noise_suppression().is_some()
            && caps.get_auto_gain_control().is_some()
    }

    /// Toggles noise suppression for this [`MediaStreamTrack`].
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    pub async fn set_noise_suppression_enabled(
        &self,
        enabled: bool,
    ) -> Result<(), platform::Error> {
        let caps = self.sys_track.get_constraints();
        caps.set_noise_suppression(&JsValue::from(enabled));

        let fut = self
            .sys_track
            .apply_constraints_with_constraints(&caps)
            .map_err(platform::Error::from)?;
        JsFuture::from(fut).await.map_err(platform::Error::from)?;

        if self.is_noise_suppression_enabled().await? != enabled {
            return Err(JsError::new("not supported").into());
        }

        Ok(())
    }

    /// Toggles auto gain control for this [`MediaStreamTrack`].
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    pub async fn set_auto_gain_control_enabled(
        &self,
        enabled: bool,
    ) -> Result<(), platform::Error> {
        let caps = self.sys_track.get_constraints();
        caps.set_auto_gain_control(&JsValue::from(enabled));

        let fut = self
            .sys_track
            .apply_constraints_with_constraints(&caps)
            .map_err(platform::Error::from)?;
        JsFuture::from(fut).await.map_err(platform::Error::from)?;

        // This might not have worked, so we are checking to make sure
        if self.is_auto_gain_control_enabled().await? != enabled {
            return Err(JsError::new("not supported").into());
        }

        Ok(())
    }

    /// Toggles acoustic echo cancellation for this [`MediaStreamTrack`].
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    pub async fn set_echo_cancellation_enabled(
        &self,
        enabled: bool,
    ) -> Result<(), platform::Error> {
        let caps = self.sys_track.get_constraints();
        caps.set_echo_cancellation(&JsValue::from(enabled));

        let fut = self
            .sys_track
            .apply_constraints_with_constraints(&caps)
            .map_err(platform::Error::from)?;
        JsFuture::from(fut).await.map_err(platform::Error::from)?;

        // This might not have worked, so we are checking to make sure
        if self.is_echo_cancellation_enabled().await? != enabled {
            return Err(JsError::new("not supported").into());
        }

        Ok(())
    }

    /// Configures a [`NoiseSuppressionLevel`] for this [`MediaStreamTrack`].
    ///
    /// __NOTE__: Does nothing, as is not supported.
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn set_noise_suppression_level(
        &self,
        _: NoiseSuppressionLevel,
    ) -> Result<(), platform::Error> {
        log::error!("Changing noise suppression level is not available on web");

        Ok(())
    }

    /// Toggles high-pass filter for this [`MediaStreamTrack`].
    ///
    /// __NOTE__: Does nothing, as is not supported.
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn set_high_pass_filter_enabled(
        &self,
        _: bool,
    ) -> Result<(), platform::Error> {
        log::error!("Changing high-pass filter is not available on web");

        Ok(())
    }

    /// Indicates whether noise suppression is enabled for this
    /// [`MediaStreamTrack`].
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn is_noise_suppression_enabled(
        &self,
    ) -> Result<bool, platform::Error> {
        let settings = self.sys_track.get_settings();

        Ok(settings.get_noise_suppression().unwrap_or_default())
    }

    /// Indicates whether echo cancellation is enabled for this
    /// [`MediaStreamTrack`].
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn is_echo_cancellation_enabled(
        &self,
    ) -> Result<bool, platform::Error> {
        let settings = self.sys_track.get_settings();

        Ok(settings.get_echo_cancellation().unwrap_or_default())
    }

    /// Indicates whether auto gain control is enabled for this
    /// [`MediaStreamTrack`].
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn is_auto_gain_control_enabled(
        &self,
    ) -> Result<bool, platform::Error> {
        let settings = self.sys_track.get_settings();

        Ok(settings.get_auto_gain_control().unwrap_or_default())
    }

    /// Returns the current configured [`NoiseSuppressionLevel`] of this
    /// [`MediaStreamTrack`].
    ///
    /// __NOTE__: Panics, as is not supported.
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    ///
    /// # Panics
    ///
    /// Always panics as [`unimplemented`].
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn get_noise_suppression_level(
        &self,
    ) -> Result<NoiseSuppressionLevel, platform::Error> {
        unimplemented!(
            "getting noise suppression level is not available on web",
        )
    }

    /// Indicates whether high-pass filter is enabled for this
    /// [`MediaStreamTrack`].
    ///
    /// __NOTE__: Panics, as is not supported.
    ///
    /// # Errors
    ///
    /// With a [`platform::Error`] if platform call errors.
    ///
    /// # Panics
    ///
    /// Always panics as [`unimplemented`].
    #[expect(clippy::unused_async, reason = "`cfg` code uniformity")]
    pub async fn is_high_pass_filter_enabled(
        &self,
    ) -> Result<bool, platform::Error> {
        unimplemented!("getting high-pass filter is not available on web")
    }
}

/// Analyzer of audio track raw data producing audio level ([RMS] loudness).
///
/// [RMS]: https://en.wikipedia.org/wiki/Root_mean_square
#[derive(Debug)]
struct AudioLevelWatcher {
    /// [`web_sys::AudioContext`] holding this [`AudioLevelWatcher`] audio
    /// processing pipeline.
    audio_ctx: web_sys::AudioContext,

    /// Latest audio level value in the `[0;100]` range.
    level: Rc<ObservableCell<i32>>,

    /// [`web_sys::AudioSourceNode`] wrapping the [`MediaStreamTrack`] being
    /// watched.
    src: web_sys::MediaStreamAudioSourceNode,

    /// [`web_sys::AnalyserNode`] processing audio data.
    analyzer: web_sys::AnalyserNode,
}

impl AudioLevelWatcher {
    /// Creates a new [`AudioLevelWatcher`] for the provided
    /// [`web_sys::MediaStreamTrack`].
    #[expect(clippy::unwrap_in_result, reason = "unrelated and intended")]
    fn new(track: &web_sys::MediaStreamTrack) -> Result<Self, platform::Error> {
        /// [`web_sys::AnalyserNode`] FFT size.
        ///
        /// Must be a power of two in the `[32..32768]` range.
        const FFT_SIZE: u32 = 256;

        let audio_ctx = web_sys::AudioContext::new()?;
        let level = Rc::new(ObservableCell::new(0));

        let stream = {
            let stream = web_sys::MediaStream::new()?;
            stream.add_track(track);
            stream
        };
        // TODO: Use `createMediaStreamTrackSource` once available.
        let src = audio_ctx.create_media_stream_source(&stream)?;
        let analyzer = audio_ctx.create_analyser()?;
        analyzer.set_fft_size(FFT_SIZE);
        #[expect(clippy::unwrap_used, reason = "always in bounds")]
        let mut audio_buf = vec![0.0f32; usize::try_from(FFT_SIZE).unwrap()];

        src.connect_with_audio_node(&analyzer)?;
        platform::spawn({
            let level = Rc::clone(&level);
            let analyzer = analyzer.clone();
            let audio_ctx = audio_ctx.clone();
            async move {
                loop {
                    if audio_ctx.state() == web_sys::AudioContextState::Closed {
                        break;
                    }

                    analyzer.get_float_time_domain_data(&mut audio_buf);
                    let mut sum = 0.0;
                    for b in &audio_buf {
                        sum += b * b;
                    }

                    #[expect( // no better way
                        clippy::as_conversions,
                        clippy::cast_precision_loss,
                        reason = "no better way"
                    )]
                    let lvl = (sum / FFT_SIZE as f32).sqrt() * 1000.0;
                    #[expect( // no better way
                        clippy::as_conversions,
                        clippy::cast_possible_truncation,
                        reason = "no better way"
                    )]
                    level.set(lvl.round().clamp(0.0, 100.0) as i32);

                    // Measure every 50 milliseconds.
                    platform::delay_for(Duration::from_millis(50)).await;
                }
            }
        });

        Ok(Self { audio_ctx, level, src, analyzer })
    }

    /// Subscribes to audio level changes of this [`AudioLevelWatcher`].
    fn subscribe(&self) -> LocalBoxStream<'static, i32> {
        self.level.subscribe()
    }
}

impl Drop for AudioLevelWatcher {
    fn drop(&mut self) {
        drop(self.src.disconnect());
        drop(self.analyzer.disconnect());
        if let Ok(close) = self.audio_ctx.close() {
            platform::spawn(async {
                drop(JsFuture::from(close).await);
            });
        }
    }
}