Move redundant fixer implementation into example code
This commit is contained in:
parent
7b9df1e269
commit
45e9054f36
2 changed files with 44 additions and 46 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<S> {
|
||||
stream: S,
|
||||
|
|
Loading…
Reference in a new issue