*: format code with rustfmt and fix clippy suggestions

* Add avformat_close_input call to clean up AVFormantContext
* Format code with rustfmt
* Fix clippy lint double_parens
* Fix clippy lint deref_addrof
* Fix clippy lint identity_conversion
* Fix clippy lint match_ref_pats
* Fix clippy lint cast_lossless
* Fix clippy lint cmp_null
* Fix clippy lint clone_on_ref_ptr
* Fix clippy lint map_clone
* Fix clippy lint needless_borrow
* Fix clippy lint needless_pass_by_value
* Fix clippy lints for examples
* Fix clippy lint unused_io_amount
* Fix clippy lint new_without_default
* Ignore inline_always clippy lint
* Add vim temp files to .gitignore
This commit is contained in:
Tadas Barzdžius
2018-04-10 17:06:42 +03:00
committed by meh
parent 20c7ef399a
commit 0bcd4550b8
128 changed files with 10643 additions and 10233 deletions

View File

@ -1,188 +1,190 @@
use std::ffi::CString;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::ptr;
use std::mem;
use std::ffi::CString;
use ffi::*;
use ::{Error, Codec, Stream, Packet, format};
use super::common::Context;
use super::destructor;
use ffi::*;
use util::range::Range;
use {format, Codec, Error, Packet, Stream};
pub struct Input {
ptr: *mut AVFormatContext,
ctx: Context,
ptr: *mut AVFormatContext,
ctx: Context,
}
unsafe impl Send for Input { }
unsafe impl Send for Input {}
impl Input {
pub unsafe fn wrap(ptr: *mut AVFormatContext) -> Self {
Input { ptr: ptr, ctx: Context::wrap(ptr, destructor::Mode::Input) }
}
pub unsafe fn wrap(ptr: *mut AVFormatContext) -> Self {
Input {
ptr: ptr,
ctx: Context::wrap(ptr, destructor::Mode::Input),
}
}
pub unsafe fn as_ptr(&self) -> *const AVFormatContext {
self.ptr as *const _
}
pub unsafe fn as_ptr(&self) -> *const AVFormatContext {
self.ptr as *const _
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFormatContext {
self.ptr
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFormatContext {
self.ptr
}
}
impl Input {
pub fn format(&self) -> format::Input {
unsafe {
format::Input::wrap((*self.as_ptr()).iformat)
}
}
pub fn format(&self) -> format::Input {
unsafe { format::Input::wrap((*self.as_ptr()).iformat) }
}
pub fn video_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_video_codec(self.as_ptr());
pub fn video_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_video_codec(self.as_ptr());
if ptr.is_null() {
None
}
else {
Some(Codec::wrap(ptr))
}
}
}
if ptr.is_null() {
None
} else {
Some(Codec::wrap(ptr))
}
}
}
pub fn audio_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_audio_codec(self.as_ptr());
pub fn audio_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_audio_codec(self.as_ptr());
if ptr.is_null() {
None
}
else {
Some(Codec::wrap(ptr))
}
}
}
if ptr.is_null() {
None
} else {
Some(Codec::wrap(ptr))
}
}
}
pub fn subtitle_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_subtitle_codec(self.as_ptr());
pub fn subtitle_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_subtitle_codec(self.as_ptr());
if ptr.is_null() {
None
}
else {
Some(Codec::wrap(ptr))
}
}
}
if ptr.is_null() {
None
} else {
Some(Codec::wrap(ptr))
}
}
}
pub fn data_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_data_codec(self.as_ptr());
pub fn data_codec(&self) -> Option<Codec> {
unsafe {
let ptr = av_format_get_data_codec(self.as_ptr());
if ptr.is_null() {
None
}
else {
Some(Codec::wrap(ptr))
}
}
}
if ptr.is_null() {
None
} else {
Some(Codec::wrap(ptr))
}
}
}
pub fn probe_score(&self) -> i32 {
unsafe {
av_format_get_probe_score(self.as_ptr())
}
}
pub fn probe_score(&self) -> i32 {
unsafe { av_format_get_probe_score(self.as_ptr()) }
}
pub fn packets(&mut self) -> PacketIter {
PacketIter::new(self)
}
pub fn packets(&mut self) -> PacketIter {
PacketIter::new(self)
}
pub fn pause(&mut self) -> Result<(), Error> {
unsafe {
match av_read_pause(self.as_mut_ptr()) {
0 => Ok(()),
e => Err(Error::from(e)),
}
}
}
pub fn pause(&mut self) -> Result<(), Error> {
unsafe {
match av_read_pause(self.as_mut_ptr()) {
0 => Ok(()),
e => Err(Error::from(e)),
}
}
}
pub fn play(&mut self) -> Result<(), Error> {
unsafe {
match av_read_play(self.as_mut_ptr()) {
0 => Ok(()),
e => Err(Error::from(e)),
}
}
}
pub fn play(&mut self) -> Result<(), Error> {
unsafe {
match av_read_play(self.as_mut_ptr()) {
0 => Ok(()),
e => Err(Error::from(e)),
}
}
}
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)),
}
}
}
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().cloned().unwrap_or(i64::min_value()),
ts,
range.end().cloned().unwrap_or(i64::max_value()),
0,
) {
s if s >= 0 => Ok(()),
e => Err(Error::from(e)),
}
}
}
}
impl Deref for Input {
type Target = Context;
type Target = Context;
fn deref(&self) -> &Self::Target {
&self.ctx
}
fn deref(&self) -> &Self::Target {
&self.ctx
}
}
impl DerefMut for Input {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.ctx
}
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.ctx
}
}
pub struct PacketIter<'a> {
context: &'a mut Input,
context: &'a mut Input,
}
impl<'a> PacketIter<'a> {
pub fn new(context: &mut Input) -> PacketIter {
PacketIter { context: context }
}
pub fn new(context: &mut Input) -> PacketIter {
PacketIter { context: context }
}
}
impl<'a> Iterator for PacketIter<'a> {
type Item = (Stream<'a>, Packet);
type Item = (Stream<'a>, Packet);
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
let mut packet = Packet::empty();
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
let mut packet = Packet::empty();
loop {
match packet.read(self.context) {
Ok(..) => unsafe {
return Some((
Stream::wrap(mem::transmute_copy(&self.context), packet.stream()),
packet));
},
loop {
match packet.read(self.context) {
Ok(..) => unsafe {
return Some((
Stream::wrap(mem::transmute_copy(&self.context), packet.stream()),
packet,
));
},
Err(Error::Eof) =>
return None,
Err(Error::Eof) => return None,
Err(..) =>
()
}
}
}
Err(..) => (),
}
}
}
}
pub fn dump(ctx: &Input, index: i32, url: Option<&str>) {
let url = url.map(|u| CString::new(u).unwrap());
let url = url.map(|u| CString::new(u).unwrap());
unsafe {
av_dump_format(ctx.as_ptr() as *mut _, index,
url.map(|u| u.as_ptr()).unwrap_or(ptr::null()), 0);
}
unsafe {
av_dump_format(
ctx.as_ptr() as *mut _,
index,
url.map(|u| u.as_ptr()).unwrap_or(ptr::null()),
0,
);
}
}