use super::StreamIo;
use crate::ffi::*;
use crate::util::interrupt::InterruptGuard;
#[derive(Debug)]
pub enum Mode {
Input,
Output,
InputCustomIo(StreamIo),
OutputCustomIo(StreamIo),
}
pub struct Destructor {
ptr: *mut AVFormatContext,
mode: Mode,
#[allow(dead_code)]
interrupt_guard: Option<InterruptGuard>,
}
impl Destructor {
pub unsafe fn new(ptr: *mut AVFormatContext, mode: Mode) -> Self {
Destructor {
ptr,
mode,
interrupt_guard: None,
}
}
pub unsafe fn new_with_interrupt(
ptr: *mut AVFormatContext,
mode: Mode,
guard: InterruptGuard,
) -> Self {
Destructor {
ptr,
mode,
interrupt_guard: Some(guard),
}
}
}
unsafe impl Send for Destructor {}
unsafe impl Sync for Destructor {}
impl Drop for Destructor {
fn drop(&mut self) {
unsafe {
match self.mode {
Mode::InputCustomIo(_) => {
avformat_close_input(&mut self.ptr);
}
Mode::OutputCustomIo(_) => {
avformat_free_context(self.ptr);
}
Mode::Input => avformat_close_input(&mut self.ptr),
Mode::Output => {
avio_close((*self.ptr).pb);
avformat_free_context(self.ptr);
}
}
}
}
}