From 24d998947d55228ec37a08cd391983c6239e3c0c Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Thu, 9 Jul 2020 10:42:42 +0200 Subject: [PATCH] use NATimePoint for seeking --- nihav-commonfmt/src/demuxers/avi.rs | 2 +- nihav-commonfmt/src/demuxers/mov.rs | 2 +- nihav-commonfmt/src/demuxers/wav.rs | 12 ++++++-- nihav-core/src/demuxers/mod.rs | 35 ++++++++++++++++------- nihav-game/src/demuxers/bmv.rs | 4 +-- nihav-game/src/demuxers/gdv.rs | 2 +- nihav-game/src/demuxers/vmd.rs | 2 +- nihav-rad/src/demuxers/bink.rs | 2 +- nihav-rad/src/demuxers/smacker.rs | 9 ++++-- nihav-realmedia/src/demuxers/realmedia.rs | 6 ++-- nihav-vivo/src/demuxers/vivo.rs | 2 +- 11 files changed, 52 insertions(+), 26 deletions(-) diff --git a/nihav-commonfmt/src/demuxers/avi.rs b/nihav-commonfmt/src/demuxers/avi.rs index 43d154a..05a0d64 100644 --- a/nihav-commonfmt/src/demuxers/avi.rs +++ b/nihav-commonfmt/src/demuxers/avi.rs @@ -135,7 +135,7 @@ impl<'a> DemuxCore<'a> for AVIDemuxer<'a> { } } - fn seek(&mut self, time: u64, seek_index: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> { let ret = seek_index.find_pos(time); if ret.is_none() { return Err(DemuxerError::SeekError); diff --git a/nihav-commonfmt/src/demuxers/mov.rs b/nihav-commonfmt/src/demuxers/mov.rs index c10bb2d..47cfbe3 100644 --- a/nihav-commonfmt/src/demuxers/mov.rs +++ b/nihav-commonfmt/src/demuxers/mov.rs @@ -962,7 +962,7 @@ impl<'a> DemuxCore<'a> for MOVDemuxer<'a> { Err(DemuxerError::EOF) } - fn seek(&mut self, time: u64, seek_index: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> { let ret = seek_index.find_pos(time); if ret.is_none() { return Err(DemuxerError::SeekError); diff --git a/nihav-commonfmt/src/demuxers/wav.rs b/nihav-commonfmt/src/demuxers/wav.rs index f9c9551..a579279 100644 --- a/nihav-commonfmt/src/demuxers/wav.rs +++ b/nihav-commonfmt/src/demuxers/wav.rs @@ -83,10 +83,16 @@ impl<'a> DemuxCore<'a> for WAVDemuxer<'a> { } } - fn seek(&mut self, time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { if self.block_size != 0 && self.avg_bytes != 0 { - let seek_dst = u64::from(self.avg_bytes) * time / 1000; - let seek_off = seek_dst / (self.block_size as u64) * (self.block_size as u64); + let seek_off = match time { + NATimePoint::Milliseconds(ms) => { + let seek_dst = u64::from(self.avg_bytes) * ms / 1000; + seek_dst / (self.block_size as u64) * (self.block_size as u64) + }, + NATimePoint::PTS(pts) => (self.block_size as u64) * pts, + NATimePoint::None => return Ok(()), + }; self.src.seek(SeekFrom::Start(self.data_pos + seek_off))?; Ok(()) } else { diff --git a/nihav-core/src/demuxers/mod.rs b/nihav-core/src/demuxers/mod.rs index 9a69abb..352c160 100644 --- a/nihav-core/src/demuxers/mod.rs +++ b/nihav-core/src/demuxers/mod.rs @@ -37,7 +37,7 @@ pub trait DemuxCore<'a>: NAOptionHandler { /// Demuxes a packet. fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult; /// Seeks to the requested time. - fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()>; + fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()>; } /// An auxiliary trait to make bytestream reader read packet data. @@ -229,16 +229,31 @@ impl StreamSeekInfo { self.entries.push(entry); } /// Searches for an appropriate seek position before requested time. - pub fn find_pos(&self, time: u64) -> Option { + pub fn find_pos(&self, time: NATimePoint) -> Option { + if time == NATimePoint::None { + return None; + } if !self.entries.is_empty() { // todo something faster like binary search let mut cand = None; for entry in self.entries.iter() { - if entry.time <= time { - cand = Some(*entry); - } else { - break; - } + match time { + NATimePoint::Milliseconds(ms) => { + if entry.time <= ms { + cand = Some(*entry); + } else { + break; + } + }, + NATimePoint::PTS(pts) => { + if entry.pts <= pts { + cand = Some(*entry); + } else { + break; + } + }, + NATimePoint::None => unreachable!(), + }; } cand } else { @@ -309,7 +324,7 @@ impl SeekIndex { self.seek_info[idx.unwrap()].filled = true; } /// Searches for a seek position before requested time. - pub fn find_pos(&self, time: u64) -> Option { + pub fn find_pos(&self, time: NATimePoint) -> Option { let mut cand = None; for str in self.seek_info.iter() { if !str.filled { continue; } @@ -389,8 +404,8 @@ impl<'a> Demuxer<'a> { } } } - /// Seeks to the requested time (in milliseconds) if possible. - pub fn seek(&mut self, time: u64) -> DemuxerResult<()> { + /// Seeks to the requested time if possible. + pub fn seek(&mut self, time: NATimePoint) -> DemuxerResult<()> { if self.seek_idx.skip_index { return Err(DemuxerError::NotPossible); } diff --git a/nihav-game/src/demuxers/bmv.rs b/nihav-game/src/demuxers/bmv.rs index fafd6f0..83f75b1 100644 --- a/nihav-game/src/demuxers/bmv.rs +++ b/nihav-game/src/demuxers/bmv.rs @@ -70,7 +70,7 @@ impl<'a> DemuxCore<'a> for BMVDemuxer<'a> { } } - fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { Err(DemuxerError::NotPossible) } } @@ -218,7 +218,7 @@ impl<'a> DemuxCore<'a> for BMV3Demuxer<'a> { } } - fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { Err(DemuxerError::NotPossible) } } diff --git a/nihav-game/src/demuxers/gdv.rs b/nihav-game/src/demuxers/gdv.rs index 11d7406..af0f857 100644 --- a/nihav-game/src/demuxers/gdv.rs +++ b/nihav-game/src/demuxers/gdv.rs @@ -110,7 +110,7 @@ impl<'a> DemuxCore<'a> for GremlinVideoDemuxer<'a> { } } - fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { Err(DemuxerError::NotPossible) } } diff --git a/nihav-game/src/demuxers/vmd.rs b/nihav-game/src/demuxers/vmd.rs index 3c6e152..f693e73 100644 --- a/nihav-game/src/demuxers/vmd.rs +++ b/nihav-game/src/demuxers/vmd.rs @@ -180,7 +180,7 @@ impl<'a> DemuxCore<'a> for VMDDemuxer<'a> { Ok(pkt) } - fn seek(&mut self, _time: u64, _seek_index: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { Err(DemuxerError::NotPossible) } } diff --git a/nihav-rad/src/demuxers/bink.rs b/nihav-rad/src/demuxers/bink.rs index a5d8169..454290f 100644 --- a/nihav-rad/src/demuxers/bink.rs +++ b/nihav-rad/src/demuxers/bink.rs @@ -152,7 +152,7 @@ impl<'a> DemuxCore<'a> for BinkDemuxer<'a> { Ok(pkt) } - fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()> { let ret = seek_idx.find_pos(time); if ret.is_none() { return Err(DemuxerError::SeekError); diff --git a/nihav-rad/src/demuxers/smacker.rs b/nihav-rad/src/demuxers/smacker.rs index accbfa5..6d1173c 100644 --- a/nihav-rad/src/demuxers/smacker.rs +++ b/nihav-rad/src/demuxers/smacker.rs @@ -251,8 +251,13 @@ impl<'a> DemuxCore<'a> for SmackerVideoDemuxer<'a> { Ok(pkt) } - fn seek(&mut self, time: u64, _seek_idx: &SeekIndex) -> DemuxerResult<()> { - if time == 0 { + fn seek(&mut self, time: NATimePoint, _seek_idx: &SeekIndex) -> DemuxerResult<()> { + let seek_to_start = match time { + NATimePoint::Milliseconds(0) => true, + NATimePoint::PTS(0) => true, + _ => false, + }; + if seek_to_start { let start = self.start; self.src.seek(SeekFrom::Start(start))?; self.cur_frame = 0; diff --git a/nihav-realmedia/src/demuxers/realmedia.rs b/nihav-realmedia/src/demuxers/realmedia.rs index 16c4746..b37c34f 100644 --- a/nihav-realmedia/src/demuxers/realmedia.rs +++ b/nihav-realmedia/src/demuxers/realmedia.rs @@ -740,7 +740,7 @@ impl<'a> DemuxCore<'a> for RealMediaDemuxer<'a> { } #[allow(unused_variables)] - fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()> { self.queued_pkts.clear(); let ret = seek_idx.find_pos(time); if ret.is_none() { @@ -1263,7 +1263,7 @@ println!(" got ainfo {:?}", ainfo); } #[allow(unused_variables)] - fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()> { Err(NotImplemented) } } @@ -1608,7 +1608,7 @@ println!("R1M kind"); } #[allow(unused_variables)] - fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()> { Err(NotImplemented) } } diff --git a/nihav-vivo/src/demuxers/vivo.rs b/nihav-vivo/src/demuxers/vivo.rs index a060d48..3c0ac7d 100644 --- a/nihav-vivo/src/demuxers/vivo.rs +++ b/nihav-vivo/src/demuxers/vivo.rs @@ -129,7 +129,7 @@ impl<'a> DemuxCore<'a> for VivoDemuxer<'a> { } } } - fn seek(&mut self, _time: u64, _seek_idx: &SeekIndex) -> DemuxerResult<()> { + fn seek(&mut self, _time: NATimePoint, _seek_idx: &SeekIndex) -> DemuxerResult<()> { Err(DemuxerError::NotPossible) } } -- 2.39.5