servo_media_audio/
media_stream_destination_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 servo_media_streams::MediaSocket;
6
7use crate::block::Chunk;
8use crate::node::{AudioNodeEngine, AudioNodeType, BlockInfo, ChannelInfo};
9use crate::sink::AudioSink;
10
11#[derive(AudioNodeCommon)]
12pub(crate) struct MediaStreamDestinationNode {
13    channel_info: ChannelInfo,
14    sink: Box<dyn AudioSink + 'static>,
15}
16
17impl MediaStreamDestinationNode {
18    pub fn new(
19        socket: Box<dyn MediaSocket>,
20        sample_rate: f32,
21        sink: Box<dyn AudioSink + 'static>,
22        channel_info: ChannelInfo,
23    ) -> Self {
24        sink.init_stream(channel_info.count, sample_rate, socket)
25            .expect("init_stream failed");
26        sink.play().expect("Sink didn't start");
27        MediaStreamDestinationNode { channel_info, sink }
28    }
29}
30
31impl AudioNodeEngine for MediaStreamDestinationNode {
32    fn node_type(&self) -> AudioNodeType {
33        AudioNodeType::MediaStreamDestinationNode
34    }
35
36    fn process(&mut self, inputs: Chunk, _: &BlockInfo) -> Chunk {
37        self.sink
38            .push_data(inputs)
39            .expect("Pushing to stream failed");
40        Chunk::default()
41    }
42
43    fn output_count(&self) -> u32 {
44        0
45    }
46}