2017-09-22 23:58:03 -04:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2017-09-25 00:22:41 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum Chunk<B: AsRef<[u8]> = Vec<u8>> {
|
2017-09-22 23:58:03 -04:00
|
|
|
Headers {
|
2017-09-25 00:22:41 -04:00
|
|
|
bytes: Rc<B>
|
2017-09-22 23:58:03 -04:00
|
|
|
},
|
|
|
|
ClusterHead {
|
|
|
|
keyframe: bool,
|
|
|
|
start: u64,
|
|
|
|
end: u64,
|
|
|
|
// space for a Cluster tag and a Timecode tag
|
|
|
|
bytes: [u8;16]
|
|
|
|
},
|
|
|
|
ClusterBody {
|
2017-09-25 00:22:41 -04:00
|
|
|
bytes: Rc<B>
|
2017-09-22 23:58:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 00:22:41 -04:00
|
|
|
impl<B: AsRef<[u8]>> AsRef<[u8]> for Chunk<B> {
|
2017-09-22 23:58:03 -04:00
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
match self {
|
2017-09-25 00:22:41 -04:00
|
|
|
&Chunk::Headers {ref bytes, ..} => bytes.as_ref().as_ref(),
|
2017-09-22 23:58:03 -04:00
|
|
|
&Chunk::ClusterHead {ref bytes, ..} => bytes,
|
2017-09-25 00:22:41 -04:00
|
|
|
&Chunk::ClusterBody {ref bytes, ..} => bytes.as_ref().as_ref()
|
2017-09-22 23:58:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use chunk::*;
|
|
|
|
use std::io::Cursor;
|
|
|
|
use webm::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enough_space_for_header() {
|
2017-09-25 00:22:41 -04:00
|
|
|
let mut chunk: Chunk = Chunk::ClusterHead {
|
2017-09-22 23:58:03 -04:00
|
|
|
keyframe: false,
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
bytes: [0;16]
|
|
|
|
};
|
|
|
|
if let Chunk::ClusterHead {ref mut bytes, ..} = chunk {
|
|
|
|
let mut cursor = Cursor::new(bytes as &mut [u8]);
|
|
|
|
encode_webm_element(&WebmElement::Cluster, &mut cursor).unwrap();
|
|
|
|
encode_webm_element(&WebmElement::Timecode(u64::max_value()), &mut cursor).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|