use std::time::Instant; use futures::Async; use futures::stream::Stream; use chunk::Chunk; pub struct ChunkTimecodeFixer { stream: S, current_offset: u64, last_observed_timecode: u64, assumed_duration: u64 } impl> Stream for ChunkTimecodeFixer { type Item = S::Item; type Error = S::Error; fn poll(&mut self) -> Result>, Self::Error> { let mut poll_chunk = self.stream.poll(); match poll_chunk { Ok(Async::Ready(Some(Chunk::ClusterHead(ref mut cluster_head)))) => { let start = cluster_head.start; if start < self.last_observed_timecode { let next_timecode = self.last_observed_timecode + self.assumed_duration; self.current_offset = next_timecode - start; } cluster_head.update_timecode(start + self.current_offset); self.last_observed_timecode = cluster_head.end; }, _ => {} }; poll_chunk } } pub struct StartingPointFinder { stream: S, seen_header: bool, seen_keyframe: bool } impl> Stream for StartingPointFinder { type Item = S::Item; type Error = S::Error; fn poll(&mut self) -> Result>, Self::Error> { loop { return match self.stream.poll() { Ok(Async::Ready(Some(Chunk::ClusterHead(cluster_head)))) => { if cluster_head.keyframe { self.seen_keyframe = true; } if self.seen_keyframe { Ok(Async::Ready(Some(Chunk::ClusterHead(cluster_head)))) } else { continue; } }, chunk @ Ok(Async::Ready(Some(Chunk::ClusterBody {..}))) => { if self.seen_keyframe { chunk } else { continue; } }, chunk @ Ok(Async::Ready(Some(Chunk::Headers {..}))) => { if self.seen_header { // new stream starting, we don't need a new header but should wait for a safe spot to resume self.seen_keyframe = false; continue; } else { self.seen_header = true; chunk } }, chunk => chunk } }; } } pub struct Throttle { stream: S, start_time: Instant } impl> Stream for Throttle { type Item = S::Item; type Error = S::Error; fn poll(&mut self) -> Result>, Self::Error> { self.stream.poll() } } pub trait ChunkStream where Self : Sized + Stream { fn fix_timecodes(self) -> ChunkTimecodeFixer { ChunkTimecodeFixer { stream: self, current_offset: 0, last_observed_timecode: 0, assumed_duration: 33 } } fn find_starting_point(self) -> StartingPointFinder { StartingPointFinder { stream: self, seen_header: false, seen_keyframe: false } } fn throttle(self) -> Throttle { Throttle { stream: self, start_time: Instant::now() } } } impl> ChunkStream for T {}