diff --git a/src/bin/loop_server.rs b/src/bin/loop_server.rs index 9ae2ecd..c14e87a 100644 --- a/src/bin/loop_server.rs +++ b/src/bin/loop_server.rs @@ -11,7 +11,7 @@ use futures::stream::repeat; use futures::stream::Stream; use lab_ebml::chunk::{Chunk, WebmStream, ChunkingError}; use lab_ebml::timecode_fixer::ChunkStream; -use lab_ebml::webm_stream::WebmBuffer; +use lab_ebml::stream_parser::StreamEbml; use hyper::{Get, StatusCode}; use hyper::header::ContentType; use hyper::server::{Http, Request, Response, Service}; @@ -34,7 +34,7 @@ impl Service for WebmServer { let stream: BodyStream> = Box::new( repeat(()).take(3) .map(|()| { - WebmBuffer::new(once::<&[u8], ()>(Ok(SRC_FILE))).chunk_webm() + once::<&[u8], ()>(Ok(SRC_FILE)).parse_ebml().chunk_webm() }).flatten() .fix_timecodes() .map_err(|err| match err { diff --git a/src/lib.rs b/src/lib.rs index 89a1de1..2a39830 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,8 @@ pub mod chunk; pub mod ebml; mod iterator; pub mod slice; +pub mod stream_parser; -pub mod webm_stream; pub mod timecode_fixer; pub mod webm; diff --git a/src/webm_stream.rs b/src/stream_parser.rs similarity index 81% rename from src/webm_stream.rs rename to src/stream_parser.rs index 4730473..de13163 100644 --- a/src/webm_stream.rs +++ b/src/stream_parser.rs @@ -12,21 +12,27 @@ pub enum ParsingError { OtherError(E) } -pub struct WebmBuffer { +pub struct EbmlStreamingParser { stream: S, buffer: BytesMut, last_read: usize } -impl, S: Stream> WebmBuffer { - pub fn new(stream: S) -> Self { - WebmBuffer { - stream: stream, +pub trait StreamEbml, S: Stream> { + fn parse_ebml(self) -> EbmlStreamingParser; +} + +impl, S: Stream> StreamEbml for S { + fn parse_ebml(self) -> EbmlStreamingParser { + EbmlStreamingParser { + stream: self, buffer: BytesMut::new(), last_read: 0 } } +} +impl, S: Stream> EbmlStreamingParser { pub fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result>, ParsingError> { // release buffer from previous event self.buffer.advance(self.last_read); @@ -72,11 +78,11 @@ impl, S: Stream> WebmBuffer { } } -impl, S: Stream> EbmlEventSource for WebmBuffer { +impl, S: Stream> EbmlEventSource for EbmlStreamingParser { type Error = ParsingError; fn poll_event<'a, T: FromEbml<'a>>(&'a mut self) -> Result>, Self::Error> { - return WebmBuffer::poll_event(self); + return EbmlStreamingParser::poll_event(self); } }