diff --git a/src/codec/packet/packet.rs b/src/codec/packet/packet.rs index 4d43bc1..427c1d6 100644 --- a/src/codec/packet/packet.rs +++ b/src/codec/packet/packet.rs @@ -127,9 +127,7 @@ impl Packet { #[inline] pub fn set_pts(&mut self, value: Option) { - unsafe { - (*self.as_mut_ptr()).pts = value.unwrap_or(AV_NOPTS_VALUE); - } + self.0.pts = value.unwrap_or(AV_NOPTS_VALUE); } #[inline] @@ -142,9 +140,7 @@ impl Packet { #[inline] pub fn set_dts(&mut self, value: Option) { - unsafe { - (*self.as_mut_ptr()).dts = value.unwrap_or(AV_NOPTS_VALUE); - } + self.0.dts = value.unwrap_or(AV_NOPTS_VALUE); } #[inline] @@ -157,11 +153,21 @@ impl Packet { self.0.duration as i64 } + #[inline] + pub fn set_duration(&mut self, value: i64) { + self.0.duration = value; + } + #[inline] pub fn position(&self) -> isize { self.0.pos as isize } + #[inline] + pub fn set_position(&mut self, value: isize) { + self.0.pos = value as i64 + } + #[inline] pub fn convergence(&self) -> isize { self.0.convergence_duration as isize diff --git a/src/format/context/common.rs b/src/format/context/common.rs index e441681..e937d8a 100644 --- a/src/format/context/common.rs +++ b/src/format/context/common.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::mem; use std::ptr; use std::rc::Rc; @@ -36,12 +37,17 @@ impl Context { } impl Context { + #[inline] + fn nb_streams(&self) -> u32 { + unsafe { (*self.as_ptr()).nb_streams } + } + pub fn stream<'a, 'b>(&'a self, index: usize) -> Option> where 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_streams as usize { + if index >= self.nb_streams() as usize { None } else { Some(Stream::wrap(self, index)) @@ -54,7 +60,7 @@ impl Context { 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_streams as usize { + if index >= self.nb_streams() as usize { None } else { Some(StreamMut::wrap(self, index)) @@ -78,6 +84,7 @@ impl Context { unsafe { (*self.as_ptr()).duration } } + #[inline] pub fn nb_chapters(&self) -> u32 { unsafe { (*self.as_ptr()).nb_chapters } } @@ -87,7 +94,7 @@ impl Context { 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_chapters as usize { + if index >= self.nb_chapters() as usize { None } else { Some(Chapter::wrap(self, index)) @@ -100,7 +107,7 @@ impl Context { 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_chapters as usize { + if index >= self.nb_chapters() as usize { None } else { Some(ChapterMut::wrap(self, index)) @@ -222,7 +229,7 @@ impl<'a> Iterator for StreamIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if self.current >= (*self.context.as_ptr()).nb_streams { + if self.current >= self.context.nb_streams() { return None; } @@ -233,14 +240,12 @@ impl<'a> Iterator for StreamIter<'a> { } fn size_hint(&self) -> (usize, Option) { - unsafe { - let length = (*self.context.as_ptr()).nb_streams as usize; + let length = self.context.nb_streams() as usize; - ( - length - self.current as usize, - Some(length - self.current as usize), - ) - } + ( + length - self.current as usize, + Some(length - self.current as usize), + ) } } @@ -264,13 +269,12 @@ impl<'a> Iterator for StreamIterMut<'a> { type Item = StreamMut<'a>; fn next(&mut self) -> Option<::Item> { + if self.current >= self.context.nb_streams() { + return None; + } + self.current += 1; + unsafe { - if self.current >= (*self.context.as_ptr()).nb_streams { - return None; - } - - self.current += 1; - Some(StreamMut::wrap( mem::transmute_copy(&self.context), (self.current - 1) as usize, @@ -279,14 +283,12 @@ impl<'a> Iterator for StreamIterMut<'a> { } fn size_hint(&self) -> (usize, Option) { - unsafe { - let length = (*self.context.as_ptr()).nb_streams as usize; + let length = self.context.nb_streams() as usize; - ( - length - self.current as usize, - Some(length - self.current as usize), - ) - } + ( + length - self.current as usize, + Some(length - self.current as usize), + ) } } @@ -380,3 +382,14 @@ impl<'a> Iterator for ChapterIterMut<'a> { } impl<'a> ExactSizeIterator for ChapterIterMut<'a> {} + +impl fmt::Debug for Context { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let mut s = fmt.debug_struct("AVFormatContext"); + s.field("bit_rate", &self.bit_rate()); + s.field("duration", &self.duration()); + s.field("nb_chapters", &self.nb_chapters()); + s.field("nb_streams", &self.nb_streams()); + s.finish() + } +} diff --git a/src/format/context/output.rs b/src/format/context/output.rs index 986715d..ceb1ec9 100644 --- a/src/format/context/output.rs +++ b/src/format/context/output.rs @@ -72,11 +72,12 @@ impl Output { pub fn add_stream(&mut self, codec: E) -> Result { unsafe { - let codec = codec.encoder().ok_or(Error::EncoderNotFound)?; - let ptr = avformat_new_stream(self.as_mut_ptr(), codec.as_ptr()); + let codec = codec.encoder(); + let codec = codec.map_or(ptr::null(), |c| c.as_ptr()); + let ptr = avformat_new_stream(self.as_mut_ptr(), codec); if ptr.is_null() { - panic!("out of memory"); + return Err(Error::Unknown); } let index = (*self.ctx.as_ptr()).nb_streams - 1; diff --git a/src/format/stream/stream.rs b/src/format/stream/stream.rs index f7c2e0e..c951bab 100644 --- a/src/format/stream/stream.rs +++ b/src/format/stream/stream.rs @@ -5,6 +5,7 @@ use format::context::common::Context; use libc::c_int; use {DictionaryRef, Discard, Rational}; +#[derive(Debug)] pub struct Stream<'a> { context: &'a Context, index: usize, diff --git a/src/software/resampling/context.rs b/src/software/resampling/context.rs index fc3902c..9b390db 100644 --- a/src/software/resampling/context.rs +++ b/src/software/resampling/context.rs @@ -20,6 +20,8 @@ pub struct Context { output: Definition, } +unsafe impl Send for Context {} + impl Context { #[doc(hidden)] pub unsafe fn as_ptr(&self) -> *const SwrContext { diff --git a/src/util/dictionary/immutable.rs b/src/util/dictionary/immutable.rs index 3781277..677faf1 100644 --- a/src/util/dictionary/immutable.rs +++ b/src/util/dictionary/immutable.rs @@ -1,4 +1,5 @@ use std::ffi::{CStr, CString}; +use std::fmt; use std::marker::PhantomData; use std::ptr; use std::str::from_utf8_unchecked; @@ -58,3 +59,9 @@ impl<'a> IntoIterator for &'a Ref<'a> { self.iter() } } + +impl<'a> fmt::Debug for Ref<'a> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_map().entries(self.iter()).finish() + } +} diff --git a/src/util/dictionary/mutable.rs b/src/util/dictionary/mutable.rs index 5413dfd..241a39d 100644 --- a/src/util/dictionary/mutable.rs +++ b/src/util/dictionary/mutable.rs @@ -1,4 +1,5 @@ use std::ffi::CString; +use std::fmt; use std::marker::PhantomData; use std::ops::Deref; @@ -50,3 +51,9 @@ impl<'a> Deref for Ref<'a> { &self.imm } } + +impl<'a> fmt::Debug for Ref<'a> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + self.imm.fmt(fmt) + } +} diff --git a/src/util/dictionary/owned.rs b/src/util/dictionary/owned.rs index 00d06ed..16f2ba0 100644 --- a/src/util/dictionary/owned.rs +++ b/src/util/dictionary/owned.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::iter::FromIterator; use std::ops::{Deref, DerefMut}; use std::ptr; @@ -126,3 +127,9 @@ impl<'a> Drop for Owned<'a> { } } } + +impl<'a> fmt::Debug for Owned<'a> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(fmt) + } +}