From a736c8b438666f9c61e4e81c3cc81454a327b0a1 Mon Sep 17 00:00:00 2001 From: lummax Date: Wed, 14 Oct 2015 16:22:28 +0200 Subject: [PATCH] format/context/input: add `seek()` method --- src/format/context/input.rs | 13 +++++++++++++ src/util/mod.rs | 1 + src/util/range.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/util/range.rs diff --git a/src/format/context/input.rs b/src/format/context/input.rs index bfcf3b8..5bb2787 100644 --- a/src/format/context/input.rs +++ b/src/format/context/input.rs @@ -7,6 +7,7 @@ use ffi::*; use ::{Error, Codec, Stream, Packet, format}; use super::common::Context; use super::destructor; +use util::range::Range; pub struct Input { ptr: *mut AVFormatContext, @@ -115,6 +116,18 @@ impl Input { } } } + + pub fn seek>(&mut self, ts: i64, range: R) -> Result<(), Error> { + unsafe { + match avformat_seek_file(self.as_mut_ptr(), -1, + range.start().map(|v| *v).unwrap_or(i64::min_value()), ts, + range.end().map(|v| *v).unwrap_or(i64::max_value()), 0) + { + s if s >= 0 => Ok(()), + e => Err(Error::from(e)), + } + } + } } impl Deref for Input { diff --git a/src/util/mod.rs b/src/util/mod.rs index eaad696..a9d0beb 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -10,6 +10,7 @@ pub mod chroma; pub mod time; pub mod channel_layout; pub mod option; +pub mod range; use std::ffi::CStr; use std::str::from_utf8_unchecked; diff --git a/src/util/range.rs b/src/util/range.rs new file mode 100644 index 0000000..9c5e5f0 --- /dev/null +++ b/src/util/range.rs @@ -0,0 +1,35 @@ +use std::ops; + +pub trait Range { + fn start(&self) -> Option<&T> { + None + } + + fn end(&self) -> Option<&T> { + None + } +} + +impl Range for ops::Range { + fn start(&self) -> Option<&T> { + Some(&self.start) + } + + fn end(&self) -> Option<&T> { + Some(&self.end) + } +} + +impl Range for ops::RangeTo { + fn end(&self) -> Option<&T> { + Some(&self.end) + } +} + +impl Range for ops::RangeFrom { + fn start(&self) -> Option<&T> { + Some(&self.start) + } +} + +impl Range for ops::RangeFull { }