1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::{
ffi::CStr,
ops::{Deref, Drop},
ptr::{self, NonNull},
slice,
};
use crate::{avutil::AVMem, error::*, ffi, shared::*};
wrap!(AVIOContext: ffi::AVIOContext);
pub struct AVIOContextURL(AVIOContext);
impl Deref for AVIOContextURL {
type Target = AVIOContext;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for AVIOContextURL {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AVIOContextURL {
pub fn open(url: &CStr, flags: u32) -> Result<Self> {
let mut io_context = ptr::null_mut();
unsafe { ffi::avio_open(&mut io_context, url.as_ptr(), flags as _) }
.upgrade()
.map_err(RsmpegError::AVIOOpenError)?;
Ok(Self(unsafe {
AVIOContext::from_raw(NonNull::new(io_context).unwrap())
}))
}
}
impl Drop for AVIOContextURL {
fn drop(&mut self) {
unsafe { ffi::avio_close(self.as_mut_ptr()) }
.upgrade()
.unwrap();
}
}
pub struct AVIOContextCustom {
inner: AVIOContext,
_opaque: Box<Opaque>,
}
impl Deref for AVIOContextCustom {
type Target = AVIOContext;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::ops::DerefMut for AVIOContextCustom {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
pub type ReadPacketCallback = Box<dyn FnMut(&mut Vec<u8>, &mut [u8]) -> i32 + Send + 'static>;
pub type WritePacketCallback = Box<dyn FnMut(&mut Vec<u8>, &[u8]) -> i32 + Send + 'static>;
pub type SeekCallback = Box<dyn FnMut(&mut Vec<u8>, i64, i32) -> i64 + Send + 'static>;
pub struct Opaque {
data: Vec<u8>,
read_packet: Option<ReadPacketCallback>,
write_packet: Option<WritePacketCallback>,
seek: Option<SeekCallback>,
}
impl AVIOContextCustom {
pub fn alloc_context(
mut buffer: AVMem,
write_flag: bool,
data: Vec<u8>,
read_packet: Option<ReadPacketCallback>,
write_packet: Option<WritePacketCallback>,
seek: Option<SeekCallback>,
) -> Self {
let (read_packet_c, write_packet_c, seek_c) = {
use std::os::raw::c_void;
unsafe extern "C" fn read_c(opaque: *mut c_void, data: *mut u8, len: i32) -> i32 {
let buf = unsafe { slice::from_raw_parts_mut(data, len as usize) };
let opaque = unsafe { (opaque as *mut Opaque).as_mut() }.unwrap();
opaque.read_packet.as_mut().unwrap()(&mut opaque.data, buf)
}
unsafe extern "C" fn write_c(opaque: *mut c_void, data: *mut u8, len: i32) -> i32 {
let buf = unsafe { slice::from_raw_parts(data, len as usize) };
let opaque = unsafe { (opaque as *mut Opaque).as_mut() }.unwrap();
opaque.write_packet.as_mut().unwrap()(&mut opaque.data, buf)
}
unsafe extern "C" fn seek_c(opaque: *mut c_void, offset: i64, whence: i32) -> i64 {
let opaque = unsafe { (opaque as *mut Opaque).as_mut() }.unwrap();
opaque.seek.as_mut().unwrap()(&mut opaque.data, offset, whence)
}
(
read_packet.is_some().then(|| read_c as _),
write_packet.is_some().then(|| write_c as _),
seek.is_some().then(|| seek_c as _),
)
};
let mut opaque = Box::new(Opaque {
data,
read_packet,
write_packet,
seek,
});
let context = unsafe {
ffi::avio_alloc_context(
buffer.as_mut_ptr(),
buffer.len as _,
if write_flag { 1 } else { 0 },
&mut *opaque as *mut _ as _,
read_packet_c,
write_packet_c,
seek_c,
)
}
.upgrade()
.unwrap();
let _ = buffer.into_raw();
Self {
inner: unsafe { AVIOContext::from_raw(context) },
_opaque: opaque,
}
}
}
impl Drop for AVIOContextCustom {
fn drop(&mut self) {
if let Some(buffer) = NonNull::new(self.buffer) {
let _ = unsafe { AVMem::from_raw(buffer) };
}
unsafe { ffi::avio_context_free(&mut self.as_mut_ptr()) };
}
}