Focus on Webm case specifically to get working code

can retry generic EBML later
This commit is contained in:
Tangent 128 2018-04-02 18:37:47 -04:00
parent 11bb7f4412
commit 56a7284e32
6 changed files with 39 additions and 53 deletions

View File

@ -32,7 +32,7 @@ impl Service for WebmServer {
let stream: BodyStream<Vec<u8>> = Box::new( let stream: BodyStream<Vec<u8>> = Box::new(
repeat(()).take(10) repeat(()).take(10)
.map(|()| .map(|()|
parse_webm(SRC_FILE).into_iter().chunk_webm() parse_webm(SRC_FILE).chunk_webm()
).flatten() ).flatten()
.fix_timecodes() .fix_timecodes()
.map_err(|err| match err { .map_err(|err| match err {

View File

@ -1,9 +1,7 @@
use futures::{Async, Stream}; use futures::{Async, Stream};
use std::io::Cursor; use std::io::Cursor;
use std::marker::PhantomData;
use std::mem; use std::mem;
use std::sync::Arc; use std::sync::Arc;
use ebml::EbmlEventSource;
use webm::*; use webm::*;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -89,13 +87,12 @@ pub enum ChunkingError<E> {
OtherError(E) OtherError(E)
} }
pub struct WebmChunker<'a, S: EbmlEventSource<'a>> { pub struct WebmChunker<S: WebmEventSource> {
source: S, source: S,
state: ChunkerState, state: ChunkerState
_marker: PhantomData<&'a [u8]>
} }
impl<'a, S: EbmlEventSource<'a, Event = WebmElement<'a>>> Stream for WebmChunker<'a, S> impl<'a, S: WebmEventSource> Stream for WebmChunker<S>
{ {
type Item = Chunk; type Item = Chunk;
type Error = ChunkingError<S::Error>; type Error = ChunkingError<S::Error>;
@ -212,16 +209,15 @@ impl<'a, S: EbmlEventSource<'a, Event = WebmElement<'a>>> Stream for WebmChunker
} }
} }
pub trait WebmStream<'a, T: EbmlEventSource<'a, Event = WebmElement<'a>>> { pub trait WebmStream<T: WebmEventSource> {
fn chunk_webm(self) -> WebmChunker<'a, T>; fn chunk_webm(self) -> WebmChunker<T>;
} }
impl<'a, T: EbmlEventSource<'a, Event = WebmElement<'a>>> WebmStream<'a, T> for T { impl<'a, T: WebmEventSource> WebmStream<T> for T {
fn chunk_webm(self) -> WebmChunker<'a, T> { fn chunk_webm(self) -> WebmChunker<T> {
WebmChunker { WebmChunker {
source: self, source: self,
state: ChunkerState::BuildingHeader(Cursor::new(Vec::new())), state: ChunkerState::BuildingHeader(Cursor::new(Vec::new()))
_marker: PhantomData
} }
} }
} }

View File

@ -3,7 +3,6 @@ use futures::Async;
use std::error::Error as ErrorTrait; use std::error::Error as ErrorTrait;
use std::fmt::{Display, Formatter, Result as FmtResult}; use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek, SeekFrom}; use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek, SeekFrom};
use std::marker::PhantomData;
pub const EBML_HEAD_ID: u64 = 0x0A45DFA3; pub const EBML_HEAD_ID: u64 = 0x0A45DFA3;
pub const DOC_TYPE_ID: u64 = 0x0282; pub const DOC_TYPE_ID: u64 = 0x0282;
@ -216,9 +215,8 @@ pub fn encode_integer<T: Write>(tag: u64, value: u64, output: &mut T) -> IoResul
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Ebml<Source, Element> { pub struct Ebml<Source> {
pub source: Source, pub source: Source
_marker: PhantomData<fn() -> Element>
} }
pub trait FromEbml<'b>: Sized { pub trait FromEbml<'b>: Sized {
@ -251,13 +249,6 @@ pub trait FromEbml<'b>: Sized {
} }
} }
} }
fn parse<T>(source: T) -> Ebml<T, Self> {
Ebml {
source: source,
_marker: PhantomData
}
}
} }
pub trait EbmlEventSource<'a> { pub trait EbmlEventSource<'a> {

View File

@ -1,32 +1,26 @@
use std::marker::PhantomData;
use futures::Async; use futures::Async;
use ebml::*; use ebml::*;
use webm::*;
pub struct EbmlIterator<'b, T: FromEbml<'b>> { pub struct EbmlCursor<T> {
slice: &'b[u8], source: T,
position: usize, position: usize
_marker: PhantomData<fn() -> T>
} }
impl<'b, E: FromEbml<'b>> IntoIterator for Ebml<&'b[u8], E> { impl<T> EbmlCursor<T> {
type Item = E; pub fn new(source: T) -> Self {
type IntoIter = EbmlIterator<'b, E>; EbmlCursor {
source,
fn into_iter(self) -> EbmlIterator<'b, E> position: 0
{
EbmlIterator {
slice: self.source,
position: 0,
_marker: PhantomData
} }
} }
} }
impl<'b, T: FromEbml<'b>> Iterator for EbmlIterator<'b, T> { impl<'a> Iterator for EbmlCursor<&'a [u8]> {
type Item = T; type Item = WebmElement<'a>;
fn next(&mut self) -> Option<T> { fn next(&mut self) -> Option<WebmElement<'a>> {
match Self::Item::decode_element(&self.slice[self.position..]) { match Self::Item::decode_element(&self.source[self.position..]) {
Err(_) => None, Err(_) => None,
Ok(None) => None, Ok(None) => None,
Ok(Some((element, element_size))) => { Ok(Some((element, element_size))) => {
@ -37,12 +31,11 @@ impl<'b, T: FromEbml<'b>> Iterator for EbmlIterator<'b, T> {
} }
} }
impl<'a, T: FromEbml<'a>> EbmlEventSource<'a> for EbmlIterator<'a, T> { impl<'b, T: AsRef<[u8]>> WebmEventSource for EbmlCursor<T> {
type Event = T;
type Error = Error; type Error = Error;
fn poll_event(&'a mut self) -> Result<Async<Option<T>>, Error> { fn poll_event<'a>(&'a mut self) -> Result<Async<Option<WebmElement<'a>>>, Error> {
match Self::Event::decode_element(&self.slice[self.position..]) { match WebmElement::decode_element(&self.source.as_ref()[self.position..]) {
Err(err) => Err(err), Err(err) => Err(err),
Ok(None) => Ok(Async::Ready(None)), Ok(None) => Ok(Async::Ready(None)),
Ok(Some((element, element_size))) => { Ok(Some((element, element_size))) => {

View File

@ -1,6 +1,8 @@
use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek}; use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek};
use bytes::{BigEndian, BufMut, ByteOrder}; use bytes::{BigEndian, BufMut, ByteOrder};
use futures::Async;
use ebml::*; use ebml::*;
use iterator::EbmlCursor;
const SEGMENT_ID: u64 = 0x08538067; const SEGMENT_ID: u64 = 0x08538067;
const SEEK_HEAD_ID: u64 = 0x014D9B74; const SEEK_HEAD_ID: u64 = 0x014D9B74;
@ -11,8 +13,8 @@ const CLUSTER_ID: u64 = 0x0F43B675;
const TIMECODE_ID: u64 = 0x67; const TIMECODE_ID: u64 = 0x67;
const SIMPLE_BLOCK_ID: u64 = 0x23; const SIMPLE_BLOCK_ID: u64 = 0x23;
pub fn parse_webm<'a, T: 'a>(source: T) -> Ebml<T, WebmElement<'a>> { pub fn parse_webm<T: AsRef<[u8]>>(source: T) -> EbmlCursor<T> {
WebmElement::parse(source) EbmlCursor::new(source)
} }
#[derive(Debug, PartialEq, Copy, Clone)] #[derive(Debug, PartialEq, Copy, Clone)]
@ -125,6 +127,11 @@ pub fn encode_webm_element<T: Write + Seek>(element: &WebmElement, output: &mut
} }
} }
pub trait WebmEventSource {
type Error;
fn poll_event<'a>(&'a mut self) -> Result<Async<Option<WebmElement<'a>>>, Self::Error>;
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use tests::TEST_FILE; use tests::TEST_FILE;

View File

@ -71,12 +71,11 @@ impl<I: AsRef<[u8]>, S: Stream<Item = I>> WebmStream<S> {
} }
} }
impl<'a, I: AsRef<[u8]>, S: Stream<Item = I>> EbmlEventSource<'a> for WebmStream<S> { impl<I: AsRef<[u8]>, S: Stream<Item = I>> WebmEventSource for WebmStream<S> {
type Event = WebmElement<'a>;
type Error = ParsingError<S::Error>; type Error = ParsingError<S::Error>;
fn poll_event(&'a mut self) -> Result<Async<Option<Self::Event>>, Self::Error> { fn poll_event<'a>(&'a mut self) -> Result<Async<Option<WebmElement<'a>>>, Self::Error> {
return WebmStream::poll_event(&mut self); return WebmStream::poll_event(self);
} }
} }