servo_media_audio/
media_stream_source_node.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 crate::AudioStreamReader;
6use crate::block::Chunk;
7use crate::node::{AudioNodeEngine, AudioNodeType, BlockInfo, ChannelInfo};
8use crate::param::{Param, ParamType};
9
10#[derive(AudioNodeCommon)]
11pub(crate) struct MediaStreamSourceNode {
12    channel_info: ChannelInfo,
13    reader: Box<dyn AudioStreamReader + Send>,
14    playing: bool,
15}
16
17impl MediaStreamSourceNode {
18    pub fn new(reader: Box<dyn AudioStreamReader + Send>, channel_info: ChannelInfo) -> Self {
19        Self {
20            channel_info,
21            reader,
22            playing: false,
23        }
24    }
25}
26
27impl AudioNodeEngine for MediaStreamSourceNode {
28    fn node_type(&self) -> AudioNodeType {
29        AudioNodeType::MediaStreamSourceNode
30    }
31
32    fn process(&mut self, mut inputs: Chunk, _: &BlockInfo) -> Chunk {
33        debug_assert!(inputs.is_empty());
34
35        if !self.playing {
36            self.playing = true;
37            self.reader.start();
38        }
39
40        let block = self.reader.pull();
41        inputs.blocks.push(block);
42
43        inputs
44    }
45
46    fn input_count(&self) -> u32 {
47        0
48    }
49
50    fn get_param(&mut self, _: ParamType) -> &mut Param {
51        panic!("No params on MediaStreamSourceNode");
52    }
53}