format/context/input: add seek() method

This commit is contained in:
lummax 2015-10-14 16:22:28 +02:00 committed by meh
parent ef3f821c6f
commit a736c8b438
3 changed files with 49 additions and 0 deletions

View File

@ -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<R: Range<i64>>(&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 {

View File

@ -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;

35
src/util/range.rs Normal file
View File

@ -0,0 +1,35 @@
use std::ops;
pub trait Range<T> {
fn start(&self) -> Option<&T> {
None
}
fn end(&self) -> Option<&T> {
None
}
}
impl<T> Range<T> for ops::Range<T> {
fn start(&self) -> Option<&T> {
Some(&self.start)
}
fn end(&self) -> Option<&T> {
Some(&self.end)
}
}
impl<T> Range<T> for ops::RangeTo<T> {
fn end(&self) -> Option<&T> {
Some(&self.end)
}
}
impl<T> Range<T> for ops::RangeFrom<T> {
fn start(&self) -> Option<&T> {
Some(&self.start)
}
}
impl<T> Range<T> for ops::RangeFull { }