1 | use std::mem; |
2 | use std::ptr; |
3 | |
4 | use super::Ref; |
5 | use ffi::*; |
6 | use libc::c_int; |
7 | |
8 | pub struct Borrow<'a> { |
9 | packet: AVPacket, |
10 | data: &'a [u8], |
11 | } |
12 | |
13 | impl<'a> Borrow<'a> { |
14 | pub fn new(data: &[u8]) -> Borrow { |
15 | unsafe { |
16 | let mut packet: AVPacket = mem::zeroed(); |
17 | |
18 | packet.data = data.as_ptr() as *mut _; |
19 | packet.size = data.len() as c_int; |
20 | |
21 | Borrow { packet, data } |
22 | } |
23 | } |
24 | |
25 | #[inline ] |
26 | pub fn size(&self) -> usize { |
27 | self.packet.size as usize |
28 | } |
29 | |
30 | #[inline ] |
31 | pub fn data(&self) -> Option<&[u8]> { |
32 | Some(self.data) |
33 | } |
34 | } |
35 | |
36 | impl<'a> Ref for Borrow<'a> { |
37 | fn as_ptr(&self) -> *const AVPacket { |
38 | &self.packet |
39 | } |
40 | } |
41 | |
42 | impl<'a> Drop for Borrow<'a> { |
43 | fn drop(&mut self) { |
44 | unsafe { |
45 | self.packet.data = ptr::null_mut(); |
46 | self.packet.size = 0; |
47 | |
48 | av_packet_unref(&mut self.packet); |
49 | } |
50 | } |
51 | } |
52 | |