From 45e9054f363a106d4e5d6dc5c2b3f57ac03eb4b7 Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Wed, 4 Apr 2018 20:19:28 -0400 Subject: [PATCH] Move redundant fixer implementation into example code --- src/bin/resynth.rs | 47 +++++++++++++++++++++++++++++++++++++++---- src/timecode_fixer.rs | 43 +-------------------------------------- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/bin/resynth.rs b/src/bin/resynth.rs index c66e163..1289585 100644 --- a/src/bin/resynth.rs +++ b/src/bin/resynth.rs @@ -3,7 +3,6 @@ extern crate lab_ebml; use std::io::{Cursor, stdout, Write}; use lab_ebml::webm::*; use lab_ebml::webm::WebmElement::*; -use lab_ebml::timecode_fixer::TimecodeFixer; const SRC_FILE: &'static [u8] = include_bytes!("../data/test1.webm"); @@ -31,8 +30,7 @@ pub fn main() { } } - let mut output = Vec::new(); - let mut cursor = Cursor::new(output); + let mut cursor = Cursor::new(Vec::new()); let mut fixer = TimecodeFixer::new(); @@ -48,8 +46,49 @@ pub fn main() { encode_webm_element(fixer.process(element), &mut cursor).unwrap(); } - output = cursor.into_inner(); + let mut output = cursor.into_inner(); stdout().write_all(&output).unwrap(); output.clear(); } + +pub struct TimecodeFixer { + pub current_offset: u64, + pub last_cluster_base: u64, + pub last_observed_timecode: u64, + pub assumed_duration: u64 +} + +impl TimecodeFixer { + pub fn new() -> TimecodeFixer { + TimecodeFixer { + current_offset: 0, + last_cluster_base: 0, + last_observed_timecode: 0, + assumed_duration: 33 + } + } + + pub fn process<'b>(&mut self, element: &WebmElement<'b>) -> WebmElement<'b> { + match element { + &WebmElement::Timecode(timecode) => { + // detect a jump backwards in the source, meaning we need to recalculate our offset + if timecode < self.last_cluster_base { + let next_timecode = self.last_observed_timecode + self.assumed_duration; + self.current_offset = next_timecode - timecode; + } + + // remember the source timecode to detect future jumps + self.last_cluster_base = timecode; + + // return adjusted timecode + WebmElement::Timecode(timecode + self.current_offset) + }, + &WebmElement::SimpleBlock(block) => { + self.last_observed_timecode = self.last_cluster_base + (block.timecode as u64); + *element + }, + _ => *element + } + } +} diff --git a/src/timecode_fixer.rs b/src/timecode_fixer.rs index 07d1047..9974bbc 100644 --- a/src/timecode_fixer.rs +++ b/src/timecode_fixer.rs @@ -1,48 +1,7 @@ -use chunk::Chunk; use futures::Async; use futures::stream::Stream; -use webm::WebmElement; -pub struct TimecodeFixer { - pub current_offset: u64, - pub last_cluster_base: u64, - pub last_observed_timecode: u64, - pub assumed_duration: u64 -} - -impl TimecodeFixer { - pub fn new() -> TimecodeFixer { - TimecodeFixer { - current_offset: 0, - last_cluster_base: 0, - last_observed_timecode: 0, - assumed_duration: 33 - } - } - - pub fn process<'b>(&mut self, element: &WebmElement<'b>) -> WebmElement<'b> { - match element { - &WebmElement::Timecode(timecode) => { - // detect a jump backwards in the source, meaning we need to recalculate our offset - if timecode < self.last_cluster_base { - let next_timecode = self.last_observed_timecode + self.assumed_duration; - self.current_offset = next_timecode - timecode; - } - - // remember the source timecode to detect future jumps - self.last_cluster_base = timecode; - - // return adjusted timecode - WebmElement::Timecode(timecode + self.current_offset) - }, - &WebmElement::SimpleBlock(block) => { - self.last_observed_timecode = self.last_cluster_base + (block.timecode as u64); - *element - }, - _ => *element - } - } -} +use chunk::Chunk; pub struct ChunkTimecodeFixer { stream: S,