From 7c8fa35807d3b3cda991715d9735e151367b2fb6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 4 Nov 2019 10:15:12 -0500 Subject: track the time offset during parsing --- src/frame.rs | 7 ++++--- src/parser.rs | 23 +++++++++++++++-------- src/reader.rs | 7 +++++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/frame.rs b/src/frame.rs index 17c6c5a..c617056 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -1,8 +1,9 @@ /// Represents a single ttyrec frame. /// -/// Ttyrec files are a raw concatenation of frames. Note that the `time` field -/// in each frame is the time since the start of the entire file, and it is -/// invalid for the `time` fields in a ttyrec file to be decreasing. +/// Ttyrec files are a raw concatenation of frames. Note that the timestamps +/// in the frame are absolute timestamps, and only the difference between them +/// is relevant. The base timestamp can be anything (common choices are 0 or +/// the actual time that the frame was generated). /// /// Frame objects are typically created via the `Creator`, `Parser`, or /// `Reader` classes. diff --git a/src/parser.rs b/src/parser.rs index 7acbbe9..45b900e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -25,10 +25,11 @@ impl Header { /// call the `add_bytes` method to add them to the internal buffer. At any /// point, you can call `next_frame` to then return the next complete frame if /// one has been read. -#[derive(Debug, Clone)] +#[derive(Debug, Default, Clone)] pub struct Parser { reading: std::collections::VecDeque, read_state: Option
, + offset: Option, } impl Parser { @@ -91,16 +92,22 @@ impl Parser { let time = header.time(); self.read_state = None; + if self.offset.is_none() { + self.offset = Some(time); + } Some(crate::frame::Frame { time, data }) } -} -impl Default for Parser { - fn default() -> Self { - Self { - reading: std::collections::VecDeque::new(), - read_state: None, - } + /// How much the timestamps in this file should be offset by. + /// + /// Ttyrec files are allowed to be generated by just inserting the current + /// absolute timestamp as the header. This means that during playback, we + /// need to take the timestamp of the first frame as the start time, and + /// each frame timestamp after that should be offset by that same amount. + /// + /// Returns `None` if no frames have been read yet. + pub fn offset(&self) -> Option { + self.offset } } diff --git a/src/reader.rs b/src/reader.rs index fd87cd5..ff5f7f9 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -46,4 +46,11 @@ impl Reader { } } } + + /// How much the timestamps in this file should be offset by. + /// + /// See `Parser::offset`. + pub fn offset(&self) -> Option { + self.parser.offset() + } } -- cgit v1.2.3