2017-10-01 00:21:33 -04:00
|
|
|
use futures::{Async, Stream};
|
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> {
|
2017-09-29 00:12:49 -04:00
|
|
|
pub fn new_cluster_head(timecode: u64) -> Chunk {
|
|
|
|
let mut chunk = Chunk::ClusterHead {
|
|
|
|
keyframe: false,
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
bytes: [0;16],
|
|
|
|
bytes_used: 0
|
|
|
|
};
|
|
|
|
chunk.update_timecode(timecode);
|
|
|
|
chunk
|
|
|
|
}
|
|
|
|
|
2017-09-25 01:41:56 -04:00
|
|
|
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-29 00:12:49 -04:00
|
|
|
pub fn extend_timespan(&mut self, timecode: u64) {
|
|
|
|
if let &mut Chunk::ClusterHead {start, ref mut end, ..} = self {
|
|
|
|
if timecode > start {
|
|
|
|
*end = timecode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn mark_keyframe(&mut self, new_keyframe: bool) {
|
|
|
|
if let &mut Chunk::ClusterHead {ref mut keyframe, ..} = self {
|
|
|
|
*keyframe = new_keyframe;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-01 00:21:33 -04:00
|
|
|
pub struct WebmChunker<S> {
|
|
|
|
stream: S
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, S: Stream<Item = WebmElement<'a>>> Stream for WebmChunker<S>
|
|
|
|
{
|
|
|
|
type Item = Chunk;
|
|
|
|
type Error = S::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error> {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait WebmStream<T> {
|
|
|
|
fn chunk_webm(self) -> WebmChunker<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Stream<Item = WebmElement<'a>>> WebmStream<T> for T {
|
|
|
|
fn chunk_webm(self) -> WebmChunker<T> {
|
|
|
|
WebmChunker {
|
|
|
|
stream: self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 23:58:03 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use chunk::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enough_space_for_header() {
|
2017-09-30 02:23:27 -04:00
|
|
|
Chunk::<Vec<u8>>::new_cluster_head(u64::max_value());
|
2017-09-22 23:58:03 -04:00
|
|
|
}
|
|
|
|
}
|