stream_processor_test/
elementary_stream.rsuse fidl_fuchsia_media::FormatDetails;
pub trait ElementaryStream {
fn format_details(&self, version_ordinal: u64) -> FormatDetails;
fn is_access_units(&self) -> bool;
fn stream<'a>(&'a self) -> Box<dyn Iterator<Item = ElementaryStreamChunk> + 'a>;
fn capped_chunks<'a>(
&'a self,
max_size: usize,
) -> Box<dyn Iterator<Item = ElementaryStreamChunk> + 'a> {
Box::new(self.stream().flat_map(move |chunk| CappedSizeChunks {
src: chunk,
offset: 0,
max_size,
}))
}
fn video_frame_count(&self) -> usize {
self.stream()
.filter(|chunk| match chunk.significance {
Significance::Video(VideoSignificance::Picture) => true,
_ => false,
})
.count()
}
}
#[derive(Clone, Debug)]
pub struct ElementaryStreamChunk {
pub start_access_unit: bool,
pub known_end_access_unit: bool,
pub data: Vec<u8>,
pub significance: Significance,
pub timestamp: Option<u64>,
}
#[derive(Copy, Clone, Debug)]
pub enum Significance {
Video(VideoSignificance),
Audio(AudioSignificance),
}
#[derive(Copy, Clone, Debug)]
pub enum VideoSignificance {
Picture,
NotPicture,
}
#[derive(Copy, Clone, Debug)]
pub enum AudioSignificance {
PcmFrames,
Encoded,
}
struct CappedSizeChunks {
src: ElementaryStreamChunk,
offset: usize,
max_size: usize,
}
impl Iterator for CappedSizeChunks {
type Item = ElementaryStreamChunk;
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.src.data.len() {
return None;
}
let len = std::cmp::min(self.src.data.len() - self.offset, self.max_size);
let next_offset = self.offset + len;
let is_first_subchunk = self.offset == 0;
let is_last_subchunk = next_offset == self.src.data.len();
let chunk = ElementaryStreamChunk {
start_access_unit: self.src.start_access_unit && is_first_subchunk,
known_end_access_unit: self.src.known_end_access_unit && is_last_subchunk,
data: self.src.data[self.offset..next_offset].to_vec(),
timestamp: if is_first_subchunk { self.src.timestamp } else { None },
significance: self.src.significance,
};
self.offset = next_offset;
Some(chunk)
}
}
pub struct TimestampedStream<S, I> {
pub source: S,
pub timestamps: I,
}
impl<S, I> ElementaryStream for TimestampedStream<S, I>
where
S: ElementaryStream,
I: Iterator<Item = u64> + Clone,
{
fn format_details(&self, version_ordinal: u64) -> FormatDetails {
self.source.format_details(version_ordinal)
}
fn is_access_units(&self) -> bool {
self.source.is_access_units()
}
fn stream<'a>(&'a self) -> Box<dyn Iterator<Item = ElementaryStreamChunk> + 'a> {
let mut timestamps = self.timestamps.clone();
Box::new(self.source.stream().map(move |mut chunk| {
match chunk.significance {
Significance::Video(VideoSignificance::Picture) => {
chunk.timestamp = timestamps.next();
}
_ => {}
};
chunk
}))
}
}
#[cfg(test)]
mod test {
use super::*;
struct FakeStream {
data: Vec<u8>,
}
impl ElementaryStream for FakeStream {
fn format_details(&self, _version_ordinal: u64) -> FormatDetails {
Default::default()
}
fn is_access_units(&self) -> bool {
true
}
fn stream<'a>(&'a self) -> Box<dyn Iterator<Item = ElementaryStreamChunk> + 'a> {
Box::new(self.data.chunks(20).map(|data| ElementaryStreamChunk {
start_access_unit: true,
known_end_access_unit: true,
data: data.to_vec(),
significance: Significance::Video(VideoSignificance::Picture),
timestamp: None,
}))
}
}
#[fuchsia::test]
fn chunks_are_capped() {
let stream = TimestampedStream {
source: FakeStream { data: (0..).take(100).collect() },
timestamps: 0..,
};
let chunk_size = 10;
for (i, capped_chunk) in stream.capped_chunks(chunk_size).enumerate() {
if i % 2 == 0 {
assert!(capped_chunk.start_access_unit);
assert_eq!(capped_chunk.timestamp, Some(i as u64 / 2));
} else {
assert!(capped_chunk.known_end_access_unit);
assert!(capped_chunk.timestamp.is_none());
}
for j in 0..chunk_size {
assert_eq!(capped_chunk.data[j], (i * chunk_size + j) as u8);
}
}
}
}