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
use block::Chunk;
use node::{AudioNodeEngine, BlockInfo};
use node::{AudioNodeType, ChannelInfo};
use servo_media_streams::MediaSocket;
use sink::AudioSink;

#[derive(AudioNodeCommon)]
pub(crate) struct MediaStreamDestinationNode {
    channel_info: ChannelInfo,
    sink: Box<dyn AudioSink + 'static>,
}

impl MediaStreamDestinationNode {
    pub fn new(
        socket: Box<dyn MediaSocket>,
        sample_rate: f32,
        sink: Box<dyn AudioSink + 'static>,
        channel_info: ChannelInfo,
    ) -> Self {
        sink.init_stream(channel_info.count, sample_rate, socket)
            .expect("init_stream failed");
        sink.play().expect("Sink didn't start");
        MediaStreamDestinationNode { channel_info, sink }
    }
}

impl AudioNodeEngine for MediaStreamDestinationNode {
    fn node_type(&self) -> AudioNodeType {
        AudioNodeType::MediaStreamDestinationNode
    }

    fn process(&mut self, inputs: Chunk, _: &BlockInfo) -> Chunk {
        self.sink
            .push_data(inputs)
            .expect("Pushing to stream failed");
        Chunk::default()
    }

    fn output_count(&self) -> u32 {
        0
    }
}