slightly simplify local_start logic

This commit is contained in:
Scott Lamb 2016-12-30 19:35:50 -08:00
parent 14461fcad9
commit fb057309f5

View File

@ -433,10 +433,11 @@ struct InnerWriter<'a> {
prev_end: Option<recording::Time>, prev_end: Option<recording::Time>,
/// The start time of this segment, based solely on examining the local clock after frames in /// The start time of this segment, based solely on examining the local clock after frames in
/// this segment were received. This is initially `None`, filled in on the second packet in /// this segment were received. Frames can suffer from various kinds of delay (initial
/// the segment, and refined as more packets are received. See `design/time.md` for more /// buffering, encoding, and network transmission), so this time is set to far in the future on
/// information. This will be used as the official start time iff `prev_end` is None. /// construction, given a real value on the first packet, and decreased as less-delayed packets
local_start: Option<recording::Time>, /// are discovered. See design/time.md for details.
local_start: recording::Time,
adjuster: ClockAdjuster, adjuster: ClockAdjuster,
@ -523,7 +524,7 @@ impl<'a> Writer<'a> {
corrupt: false, corrupt: false,
hasher: hash::Hasher::new(hash::Type::SHA1)?, hasher: hash::Hasher::new(hash::Type::SHA1)?,
prev_end: prev.map(|p| p.end_time), prev_end: prev.map(|p| p.end_time),
local_start: None, local_start: recording::Time(i64::max_value()),
adjuster: ClockAdjuster::new(prev.map(|p| p.local_time_delta.0)), adjuster: ClockAdjuster::new(prev.map(|p| p.local_time_delta.0)),
camera_id: camera_id, camera_id: camera_id,
video_sample_entry_id: video_sample_entry_id, video_sample_entry_id: video_sample_entry_id,
@ -540,7 +541,7 @@ impl<'a> Writer<'a> {
if let Some(unflushed) = w.unflushed_sample.take() { if let Some(unflushed) = w.unflushed_sample.take() {
let duration = w.adjuster.adjust((pts_90k - unflushed.pts_90k) as i32); let duration = w.adjuster.adjust((pts_90k - unflushed.pts_90k) as i32);
w.index.add_sample(duration, unflushed.len, unflushed.is_key); w.index.add_sample(duration, unflushed.len, unflushed.is_key);
w.local_start = Some(w.extend_local_start(unflushed.local_time)); w.extend_local_start(unflushed.local_time);
} }
let mut remaining = pkt; let mut remaining = pkt;
while !remaining.is_empty() { while !remaining.is_empty() {
@ -578,12 +579,9 @@ impl<'a> Writer<'a> {
} }
impl<'a> InnerWriter<'a> { impl<'a> InnerWriter<'a> {
fn extend_local_start(&mut self, pkt_local_time: recording::Time) -> recording::Time { fn extend_local_start(&mut self, pkt_local_time: recording::Time) {
let new = pkt_local_time - recording::Duration(self.index.total_duration_90k as i64); let new = pkt_local_time - recording::Duration(self.index.total_duration_90k as i64);
match self.local_start { self.local_start = cmp::min(self.local_start, new);
None => new,
Some(old) => cmp::min(old, new),
}
} }
fn close(mut self, next_pts: Option<i64>) -> Result<PreviousWriter, Error> { fn close(mut self, next_pts: Option<i64>) -> Result<PreviousWriter, Error> {
@ -598,14 +596,14 @@ impl<'a> InnerWriter<'a> {
Some(p) => (p - unflushed.pts_90k) as i32, Some(p) => (p - unflushed.pts_90k) as i32,
}); });
self.index.add_sample(duration, unflushed.len, unflushed.is_key); self.index.add_sample(duration, unflushed.len, unflushed.is_key);
let local_start = self.extend_local_start(unflushed.local_time); self.extend_local_start(unflushed.local_time);
let mut sha1_bytes = [0u8; 20]; let mut sha1_bytes = [0u8; 20];
sha1_bytes.copy_from_slice(&self.hasher.finish()?[..]); sha1_bytes.copy_from_slice(&self.hasher.finish()?[..]);
let start = self.prev_end.unwrap_or(local_start); let start = self.prev_end.unwrap_or(self.local_start);
let end = start + recording::Duration(self.index.total_duration_90k as i64); let end = start + recording::Duration(self.index.total_duration_90k as i64);
let flags = if self.index.has_trailing_zero() { db::RecordingFlags::TrailingZero as i32 } let flags = if self.index.has_trailing_zero() { db::RecordingFlags::TrailingZero as i32 }
else { 0 }; else { 0 };
let local_start_delta = local_start - start; let local_start_delta = self.local_start - start;
let recording = db::RecordingToInsert{ let recording = db::RecordingToInsert{
camera_id: self.camera_id, camera_id: self.camera_id,
sample_file_bytes: self.index.sample_file_bytes, sample_file_bytes: self.index.sample_file_bytes,