2017-09-25 01:41:56 -04:00
|
|
|
use std::io::Cursor;
|
2017-09-28 22:11:27 -04:00
|
|
|
use std::sync::Arc;
|
2017-09-25 01:41:56 -04:00
|
|
|
use webm::*;
|
2017-09-22 23:58:03 -04:00
|
|
|
|
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-28 22:11:27 -04:00
|
|
|
bytes: Arc<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
|
2017-09-29 00:07:56 -04:00
|
|
|
bytes: [u8;16],
|
|
|
|
bytes_used: u8
|
2017-09-22 23:58:03 -04:00
|
|
|
},
|
|
|
|
ClusterBody {
|
2017-09-28 22:11:27 -04:00
|
|
|
bytes: Arc<B>
|
2017-09-22 23:58:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 01:41:56 -04:00
|
|
|
impl<B: AsRef<[u8]>> Chunk<B> {
|
|
|
|
pub fn update_timecode(&mut self, timecode: u64) {
|
2017-09-29 00:07:56 -04:00
|
|
|
if let &mut Chunk::ClusterHead {ref mut start, ref mut end, ref mut bytes, ref mut bytes_used, ..} = self {
|
2017-09-25 01:41:56 -04:00
|
|
|
let delta = *end - *start;
|
|
|
|
*start = timecode;
|
|
|
|
*end = *start + delta;
|
|
|
|
let mut cursor = Cursor::new(bytes as &mut [u8]);
|
|
|
|
// buffer is sized so these should never fail
|
|
|
|
encode_webm_element(&WebmElement::Cluster, &mut cursor).unwrap();
|
|
|
|
encode_webm_element(&WebmElement::Timecode(timecode), &mut cursor).unwrap();
|
2017-09-29 00:07:56 -04:00
|
|
|
*bytes_used = cursor.position() as u8;
|
2017-09-25 01:41:56 -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-29 00:07:56 -04:00
|
|
|
&Chunk::ClusterHead {ref bytes, bytes_used, ..} => bytes[..bytes_used as usize].as_ref(),
|
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::*;
|
|
|
|
|
|
|
|
#[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]
|
|
|
|
};
|
2017-09-25 01:41:56 -04:00
|
|
|
chunk.update_timecode(u64::max_value());
|
2017-09-22 23:58:03 -04:00
|
|
|
}
|
|
|
|
}
|