codec/context: make destructors safe

This commit is contained in:
meh
2015-09-17 17:33:31 +02:00
parent 19548810dd
commit 9736980b02
10 changed files with 166 additions and 134 deletions

View File

@ -5,6 +5,7 @@ use std::ffi::CString;
use ffi::*;
use ::{Error, Codec, StreamMut, Dictionary, format};
use super::common::Context;
use super::destructor;
pub struct Output {
ptr: *mut AVFormatContext,
@ -15,7 +16,7 @@ unsafe impl Send for Output { }
impl Output {
pub unsafe fn wrap(ptr: *mut AVFormatContext) -> Self {
Output { ptr: ptr, ctx: Context::wrap(ptr) }
Output { ptr: ptr, ctx: Context::wrap(ptr, destructor::Mode::Output) }
}
pub unsafe fn as_ptr(&self) -> *const AVFormatContext {
@ -74,7 +75,9 @@ impl Output {
panic!("out of memory");
}
StreamMut::wrap(ptr)
let index = (*self.ctx.as_ptr()).nb_streams - 1;
StreamMut::wrap(&mut self.ctx, index as usize)
}
}
@ -99,14 +102,6 @@ impl DerefMut for Output {
}
}
impl Drop for Output {
fn drop(&mut self) {
unsafe {
avformat_free_context(self.as_mut_ptr());
}
}
}
pub fn dump(ctx: &Output, index: i32, url: Option<&str>) {
let url = url.map(|u| CString::new(u).unwrap());