ffmpeg_rs/util/
interrupt.rs

1use std::panic;
2use std::process;
3
4use ffi::*;
5use libc::{c_int, c_void};
6
7pub struct Interrupt {
8    pub interrupt: AVIOInterruptCB,
9}
10
11extern "C" fn callback<F>(opaque: *mut c_void) -> c_int
12where
13    F: FnMut() -> bool,
14{
15    match panic::catch_unwind(|| (unsafe { &mut *(opaque as *mut F) })()) {
16        Ok(ret) => ret as c_int,
17        Err(_) => process::abort(),
18    }
19}
20
21pub fn new<F>(opaque: Box<F>) -> Interrupt
22where
23    F: FnMut() -> bool,
24{
25    let interrupt_cb = AVIOInterruptCB {
26        callback: Some(callback::<F>),
27        opaque: Box::into_raw(opaque) as *mut c_void,
28    };
29    Interrupt {
30        interrupt: interrupt_cb,
31    }
32}