1 // Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 use glib::translate::*;
10 use gst;
11 use gst_base_sys;
12 use std::io;
13 use std::ops;
14 use Adapter;
15 
16 impl Adapter {
copy(&self, offset: usize, dest: &mut [u8])17     pub fn copy(&self, offset: usize, dest: &mut [u8]) {
18         unsafe {
19             let size = dest.len();
20             gst_base_sys::gst_adapter_copy(
21                 self.to_glib_none().0,
22                 dest.as_mut_ptr() as *mut _,
23                 offset,
24                 size,
25             );
26         }
27     }
28 
push(&self, buf: gst::Buffer)29     pub fn push(&self, buf: gst::Buffer) {
30         unsafe {
31             gst_base_sys::gst_adapter_push(self.to_glib_none().0, buf.into_ptr());
32         }
33     }
34 }
35 
36 impl io::Read for Adapter {
read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error>37     fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
38         let mut len = self.available();
39 
40         if len == 0 {
41             return Err(io::Error::new(
42                 io::ErrorKind::WouldBlock,
43                 format!(
44                     "Missing data: requesting {} but only got {}.",
45                     buf.len(),
46                     len
47                 ),
48             ));
49         }
50 
51         if buf.len() < len {
52             len = buf.len();
53         }
54 
55         self.copy(0, &mut buf[0..len]);
56         self.flush(len);
57         Ok(len)
58     }
59 }
60 
61 #[derive(Debug)]
62 pub struct UniqueAdapter(Adapter);
63 
64 unsafe impl Send for UniqueAdapter {}
65 unsafe impl Sync for UniqueAdapter {}
66 
67 impl UniqueAdapter {
new() -> UniqueAdapter68     pub fn new() -> UniqueAdapter {
69         UniqueAdapter(Adapter::new())
70     }
71 
available(&self) -> usize72     pub fn available(&self) -> usize {
73         self.0.available()
74     }
75 
available_fast(&self) -> usize76     pub fn available_fast(&self) -> usize {
77         self.0.available_fast()
78     }
79 
clear(&mut self)80     pub fn clear(&mut self) {
81         self.0.clear();
82     }
83 
copy_bytes(&self, offset: usize, size: usize) -> Result<glib::Bytes, glib::BoolError>84     pub fn copy_bytes(&self, offset: usize, size: usize) -> Result<glib::Bytes, glib::BoolError> {
85         self.0.copy_bytes(offset, size)
86     }
87 
distance_from_discont(&self) -> u6488     pub fn distance_from_discont(&self) -> u64 {
89         self.0.distance_from_discont()
90     }
91 
92     #[cfg(any(feature = "v1_10", feature = "dox"))]
dts_at_discont(&self) -> gst::ClockTime93     pub fn dts_at_discont(&self) -> gst::ClockTime {
94         self.0.dts_at_discont()
95     }
96 
flush(&mut self, flush: usize)97     pub fn flush(&mut self, flush: usize) {
98         self.0.flush(flush);
99     }
100 
get_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError>101     pub fn get_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
102         self.0.get_buffer(nbytes)
103     }
104 
get_buffer_fast(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError>105     pub fn get_buffer_fast(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
106         self.0.get_buffer_fast(nbytes)
107     }
108 
get_buffer_list(&self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError>109     pub fn get_buffer_list(&self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError> {
110         self.0.get_buffer_list(nbytes)
111     }
112 
get_list(&self, nbytes: usize) -> Vec<gst::Buffer>113     pub fn get_list(&self, nbytes: usize) -> Vec<gst::Buffer> {
114         self.0.get_list(nbytes)
115     }
116 
masked_scan_uint32(&self, mask: u32, pattern: u32, offset: usize, size: usize) -> isize117     pub fn masked_scan_uint32(&self, mask: u32, pattern: u32, offset: usize, size: usize) -> isize {
118         self.0.masked_scan_uint32(mask, pattern, offset, size)
119     }
120 
masked_scan_uint32_peek( &self, mask: u32, pattern: u32, offset: usize, size: usize, ) -> (isize, u32)121     pub fn masked_scan_uint32_peek(
122         &self,
123         mask: u32,
124         pattern: u32,
125         offset: usize,
126         size: usize,
127     ) -> (isize, u32) {
128         self.0.masked_scan_uint32_peek(mask, pattern, offset, size)
129     }
130 
131     #[cfg(any(feature = "v1_10", feature = "dox"))]
offset_at_discont(&self) -> u64132     pub fn offset_at_discont(&self) -> u64 {
133         self.0.offset_at_discont()
134     }
135 
prev_dts(&self) -> (gst::ClockTime, u64)136     pub fn prev_dts(&self) -> (gst::ClockTime, u64) {
137         self.0.prev_dts()
138     }
139 
prev_dts_at_offset(&self, offset: usize) -> (gst::ClockTime, u64)140     pub fn prev_dts_at_offset(&self, offset: usize) -> (gst::ClockTime, u64) {
141         self.0.prev_dts_at_offset(offset)
142     }
143 
144     #[cfg(any(feature = "v1_10", feature = "dox"))]
prev_offset(&self) -> (u64, u64)145     pub fn prev_offset(&self) -> (u64, u64) {
146         self.0.prev_offset()
147     }
148 
prev_pts(&self) -> (gst::ClockTime, u64)149     pub fn prev_pts(&self) -> (gst::ClockTime, u64) {
150         self.0.prev_pts()
151     }
152 
prev_pts_at_offset(&self, offset: usize) -> (gst::ClockTime, u64)153     pub fn prev_pts_at_offset(&self, offset: usize) -> (gst::ClockTime, u64) {
154         self.0.prev_pts_at_offset(offset)
155     }
156 
157     #[cfg(any(feature = "v1_10", feature = "dox"))]
pts_at_discont(&self) -> gst::ClockTime158     pub fn pts_at_discont(&self) -> gst::ClockTime {
159         self.0.pts_at_discont()
160     }
161 
take_buffer(&mut self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError>162     pub fn take_buffer(&mut self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
163         self.0.take_buffer(nbytes)
164     }
165 
take_buffer_fast(&mut self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError>166     pub fn take_buffer_fast(&mut self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
167         self.0.take_buffer_fast(nbytes)
168     }
169 
take_buffer_list(&mut self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError>170     pub fn take_buffer_list(&mut self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError> {
171         self.0.take_buffer_list(nbytes)
172     }
173 
take_list(&mut self, nbytes: usize) -> Vec<gst::Buffer>174     pub fn take_list(&mut self, nbytes: usize) -> Vec<gst::Buffer> {
175         self.0.take_list(nbytes)
176     }
177 
copy(&self, offset: usize, dest: &mut [u8])178     pub fn copy(&self, offset: usize, dest: &mut [u8]) {
179         self.0.copy(offset, dest);
180     }
181 
push(&mut self, buf: gst::Buffer)182     pub fn push(&mut self, buf: gst::Buffer) {
183         self.0.push(buf);
184     }
185 
map(&mut self, nbytes: usize) -> Result<UniqueAdapterMap, glib::error::BoolError>186     pub fn map(&mut self, nbytes: usize) -> Result<UniqueAdapterMap, glib::error::BoolError> {
187         use std::slice;
188 
189         unsafe {
190             let ptr = gst_base_sys::gst_adapter_map(self.0.to_glib_none().0, nbytes);
191             if ptr.is_null() {
192                 Err(glib_bool_error!("size bytes are not available"))
193             } else {
194                 Ok(UniqueAdapterMap(
195                     self,
196                     slice::from_raw_parts(ptr as *const u8, nbytes),
197                 ))
198             }
199         }
200     }
201 }
202 
203 #[derive(Debug)]
204 pub struct UniqueAdapterMap<'a>(&'a UniqueAdapter, &'a [u8]);
205 
206 impl<'a> Drop for UniqueAdapterMap<'a> {
drop(&mut self)207     fn drop(&mut self) {
208         unsafe {
209             gst_base_sys::gst_adapter_unmap((self.0).0.to_glib_none().0);
210         }
211     }
212 }
213 
214 impl<'a> ops::Deref for UniqueAdapterMap<'a> {
215     type Target = [u8];
216 
deref(&self) -> &[u8]217     fn deref(&self) -> &[u8] {
218         self.1
219     }
220 }
221 
222 impl<'a> AsRef<[u8]> for UniqueAdapterMap<'a> {
as_ref(&self) -> &[u8]223     fn as_ref(&self) -> &[u8] {
224         self.1
225     }
226 }
227 
228 impl Default for UniqueAdapter {
default() -> Self229     fn default() -> Self {
230         Self::new()
231     }
232 }
233 
234 impl io::Read for UniqueAdapter {
read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error>235     fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
236         self.0.read(buf)
237     }
238 }
239