fix live view

This broke with the media vs wall duration split, part of #34.
This commit is contained in:
Scott Lamb
2020-08-07 10:16:06 -07:00
parent 036e8427e6
commit b9c08b18a4
6 changed files with 104 additions and 99 deletions

View File

@@ -521,7 +521,7 @@ pub struct LiveSegment {
/// The pts, relative to the start of the recording, of the start and end of this live segment,
/// in 90kHz units.
pub off_90k: Range<i32>,
pub media_off_90k: Range<i32>,
}
#[derive(Clone, Debug, Default)]

View File

@@ -43,25 +43,24 @@ pub const MAX_RECORDING_WALL_DURATION: i64 = 5 * 60 * TIME_UNITS_PER_SEC;
pub use base::time::Time;
pub use base::time::Duration;
/// Converts from a wall time offset into a recording to a media time offset.
pub fn wall_to_media(wall_off_90k: i32, wall_duration_90k: i32, media_duration_90k: i32) -> i32 {
debug_assert!(wall_off_90k <= wall_duration_90k,
"wall_off_90k={} wall_duration_90k={} media_duration_90k={}",
wall_off_90k, wall_duration_90k, media_duration_90k);
if wall_duration_90k == 0 {
return 0;
/// Converts from a wall time offset into a recording to a media time offset or vice versa.
pub fn rescale(from_off_90k: i32, from_duration_90k: i32, to_duration_90k: i32) -> i32 {
debug_assert!(from_off_90k <= from_duration_90k,
"from_off_90k={} from_duration_90k={} to_duration_90k={}",
from_off_90k, from_duration_90k, to_duration_90k);
if from_duration_90k == 0 {
return 0; // avoid a divide by zero.
}
// The intermediate values here may overflow i32, so use an i64 instead. The max wall
// time is recording::MAX_RECORDING_WALL_DURATION; the max media duration should be
// roughly the same (design limit of 500 ppm correction). The final result should fit
// within i32.
i32::try_from(i64::from(wall_off_90k) *
i64::from(media_duration_90k) /
i64::from(wall_duration_90k))
.map_err(|_| format!("wall_to_media overflow: {} * {} / {} > i32::max_value()",
wall_off_90k, media_duration_90k,
wall_duration_90k))
i32::try_from(i64::from(from_off_90k) *
i64::from(to_duration_90k) /
i64::from(from_duration_90k))
.map_err(|_| format!("rescale overflow: {} * {} / {} > i32::max_value()",
from_off_90k, to_duration_90k, from_duration_90k))
.unwrap()
}

View File

@@ -560,7 +560,7 @@ struct InnerWriter<F: FileWriter> {
/// The pts, relative to the start of this segment and in 90kHz units, up until which live
/// segments have been sent out. Initially 0.
completed_live_segment_off_90k: i32,
completed_live_segment_media_off_90k: i32,
hasher: blake3::Hasher,
@@ -634,7 +634,7 @@ impl<'a, C: Clocks + Clone, D: DirWriter> Writer<'a, C, D> {
r,
e: recording::SampleIndexEncoder::new(),
id,
completed_live_segment_off_90k: 0,
completed_live_segment_media_off_90k: 0,
hasher: blake3::Hasher::new(),
local_start: recording::Time(i64::max_value()),
unindexed_sample: None,
@@ -686,9 +686,9 @@ impl<'a, C: Clocks + Clone, D: DirWriter> Writer<'a, C, D> {
if is_key {
self.db.lock().send_live_segment(self.stream_id, db::LiveSegment {
recording: w.id.recording(),
off_90k: w.completed_live_segment_off_90k .. d,
media_off_90k: w.completed_live_segment_media_off_90k .. d,
}).unwrap();
w.completed_live_segment_off_90k = d;
w.completed_live_segment_media_off_90k = d;
}
}
let mut remaining = pkt;
@@ -726,7 +726,7 @@ fn clamp(v: i64, min: i64, max: i64) -> i64 {
}
impl<F: FileWriter> InnerWriter<F> {
/// Returns the total duration of the `RecordingToInsert` (needed for live view path).
/// Returns the total media duration of the `RecordingToInsert` (needed for live view path).
fn add_sample(&mut self, duration_90k: i32, bytes: i32, is_key: bool,
pkt_local_time: recording::Time) -> Result<i32, Error> {
let mut l = self.r.lock();
@@ -772,7 +772,7 @@ impl<F: FileWriter> InnerWriter<F> {
// This always ends a live segment.
db.lock().send_live_segment(stream_id, db::LiveSegment {
recording: self.id.recording(),
off_90k: self.completed_live_segment_off_90k .. d,
media_off_90k: self.completed_live_segment_media_off_90k .. d,
}).unwrap();
let wall_duration;
{