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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use fidl_fuchsia_media::*;
use std::{fs, io, path::Path};
use stream_processor_test::*;

/// Represents an CVSD elementary stream.
pub struct CvsdStream {
    data: Vec<u8>,
    chunk_frames: usize,
}

impl CvsdStream {
    /// Constructs an CVSD elementary stream from a file with raw elementary stream data.
    pub fn from_file(filename: impl AsRef<Path>, chunk_frames: usize) -> io::Result<Self> {
        Ok(CvsdStream { data: fs::read(filename)?, chunk_frames })
    }
    /// Constructs an CVSD elementary stream from raw data.
    pub fn from_data(data: Vec<u8>, chunk_frames: usize) -> Self {
        CvsdStream { data, chunk_frames }
    }
}

impl ElementaryStream for CvsdStream {
    fn format_details(&self, version_ordinal: u64) -> FormatDetails {
        FormatDetails {
            format_details_version_ordinal: Some(version_ordinal),
            mime_type: Some(String::from("audio/cvsd")),
            ..Default::default()
        }
    }

    fn is_access_units(&self) -> bool {
        false
    }

    fn stream<'a>(&'a self) -> Box<dyn Iterator<Item = ElementaryStreamChunk> + 'a> {
        Box::new(self.data.chunks(self.chunk_frames).map(|frame| ElementaryStreamChunk {
            start_access_unit: false,
            known_end_access_unit: false,
            data: frame.to_vec(),
            significance: Significance::Audio(AudioSignificance::Encoded),
            timestamp: None,
        }))
    }
}