servo_media_traits/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::num::NonZeroU32;
6use std::sync::mpsc::Sender;
7
8use malloc_size_of_derive::MallocSizeOf;
9/// An ID for clients to track instances of Players and AudioContexts belonging to the same tab and mute them simultaneously.
10/// Current tuple implementation matches one of Servo's BrowsingContextId.
11#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, MallocSizeOf)]
12pub struct ClientContextId(u32, NonZeroU32);
13
14impl ClientContextId {
15    pub fn build(a: u32, b: u32) -> ClientContextId {
16        ClientContextId(a, NonZeroU32::new(b).unwrap())
17    }
18}
19
20#[derive(Debug)]
21pub struct MediaInstanceError;
22
23impl std::fmt::Display for MediaInstanceError {
24    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        write!(formatter, "MediaInstanceError")
26    }
27}
28
29impl std::error::Error for MediaInstanceError {}
30
31/// Common functionality for all high level media instances
32/// These currently are WebAudio AudioContexts and Players.
33pub trait MediaInstance: Send {
34    fn get_id(&self) -> usize;
35    fn mute(&self, val: bool) -> Result<(), MediaInstanceError>;
36    fn suspend(&self) -> Result<(), MediaInstanceError>;
37    fn resume(&self) -> Result<(), MediaInstanceError>;
38}
39
40#[derive(MallocSizeOf)]
41pub enum BackendMsg {
42    /// Message to notify about a media instance shutdown.
43    /// The given `usize` is the media instance ID.
44    Shutdown {
45        context: ClientContextId,
46        id: usize,
47        tx_ack: Sender<()>,
48    },
49}