aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-11-04 10:15:12 -0500
committerJesse Luehrs <doy@tozt.net>2019-11-04 10:17:56 -0500
commit7c8fa35807d3b3cda991715d9735e151367b2fb6 (patch)
tree2e492ae140a2d343c680cec15fb28090d52bf8de
parente7beaa60f8c246190a8d9089c66d546abadab577 (diff)
downloadttyrec-7c8fa35807d3b3cda991715d9735e151367b2fb6.tar.gz
ttyrec-7c8fa35807d3b3cda991715d9735e151367b2fb6.zip
track the time offset during parsing
-rw-r--r--src/frame.rs7
-rw-r--r--src/parser.rs23
-rw-r--r--src/reader.rs7
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<u8>,
read_state: Option<Header>,
+ offset: Option<std::time::Duration>,
}
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<std::time::Duration> {
+ 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<R: tokio::io::AsyncRead> Reader<R> {
}
}
}
+
+ /// How much the timestamps in this file should be offset by.
+ ///
+ /// See `Parser::offset`.
+ pub fn offset(&self) -> Option<std::time::Duration> {
+ self.parser.offset()
+ }
}