X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Fdemuxers%2Fmod.rs;h=9c7e46e1f8cdc99db2c411acfe6614382092fd56;hb=b7c882c1ce6f86c07c2340751200e3a060942826;hp=b54e5633440e4aec325dc3c706de84e29ef170be;hpb=ac818eac7671fa8ddfea5aa4fb86fc0b5ab82d2e;p=nihav.git diff --git a/nihav-core/src/demuxers/mod.rs b/nihav-core/src/demuxers/mod.rs index b54e563..9c7e46e 100644 --- a/nihav-core/src/demuxers/mod.rs +++ b/nihav-core/src/demuxers/mod.rs @@ -38,6 +38,8 @@ pub trait DemuxCore<'a>: NAOptionHandler { fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult; /// Seeks to the requested time. fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()>; + /// Returns container duration in milliseconds (zero if not available). + fn get_duration(&self) -> u64; } /// An auxiliary trait to make bytestream reader read packet data. @@ -289,11 +291,11 @@ impl SeekIndex { pub fn new() -> Self { Self::default() } pub fn add_stream(&mut self, id: u32) -> usize { let ret = self.stream_id_to_index(id); - if ret.is_none() { + if let Some(res) = ret { + res + } else { self.seek_info.push(StreamSeekInfo::new(id)); self.seek_info.len() - 1 - } else { - ret.unwrap() } } /// Adds a new stream to the index. @@ -415,6 +417,25 @@ impl<'a> Demuxer<'a> { pub fn get_seek_index(&self) -> &SeekIndex { &self.seek_idx } + /// Returns media duration reported by container or its streams. + /// + /// Duration is in milliseconds and set to zero when it is not available. + pub fn get_duration(&self) -> u64 { + let duration = self.dmx.get_duration(); + if duration != 0 { + return duration; + } + let mut duration = 0; + for stream in self.streams.iter() { + if stream.duration > 0 { + let dur = NATimeInfo::ts_to_time(stream.duration, 1000, stream.tb_num, stream.tb_den); + if duration < dur { + duration = dur; + } + } + } + duration + } } impl<'a> NAOptionHandler for Demuxer<'a> {