2016-09-17 12:31:35 +00:00
|
|
|
// Copyright (C) 2016 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
// Boston, MA 02110-1301, USA.
|
|
|
|
//
|
|
|
|
|
|
|
|
use buffer::*;
|
2016-12-27 15:47:39 +00:00
|
|
|
use log::*;
|
2016-09-17 12:31:35 +00:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::cmp;
|
2016-12-27 15:47:39 +00:00
|
|
|
use slog::*;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref LOGGER: Logger = {
|
|
|
|
Logger::root(GstDebugDrain::new(None,
|
|
|
|
"rsadapter",
|
|
|
|
0,
|
|
|
|
"Rust buffer adapter"),
|
|
|
|
None)
|
|
|
|
};
|
|
|
|
}
|
2016-09-17 12:31:35 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Adapter {
|
|
|
|
deque: VecDeque<ReadMappedBuffer>,
|
|
|
|
size: usize,
|
|
|
|
skip: usize,
|
|
|
|
scratch: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum AdapterError {
|
|
|
|
NotEnoughData,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Adapter {
|
|
|
|
pub fn new() -> Adapter {
|
|
|
|
Adapter {
|
|
|
|
deque: VecDeque::new(),
|
|
|
|
size: 0,
|
|
|
|
skip: 0,
|
|
|
|
scratch: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, buffer: Buffer) {
|
|
|
|
let size = buffer.get_size();
|
|
|
|
|
|
|
|
self.size += size;
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER,
|
|
|
|
"Storing {:?} of size {}, now have size {}",
|
|
|
|
buffer,
|
|
|
|
size,
|
|
|
|
self.size);
|
2016-09-17 12:31:35 +00:00
|
|
|
self.deque.push_back(buffer.into_read_mapped_buffer().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.deque.clear();
|
|
|
|
self.size = 0;
|
|
|
|
self.skip = 0;
|
|
|
|
self.scratch.clear();
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Cleared adapter");
|
2016-09-17 12:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_available(&self) -> usize {
|
|
|
|
self.size
|
|
|
|
}
|
|
|
|
|
2016-11-16 20:55:54 +00:00
|
|
|
fn copy_data(deque: &VecDeque<ReadMappedBuffer>, skip: usize, data: &mut [u8], size: usize) {
|
|
|
|
let mut skip = skip;
|
|
|
|
let mut left = size;
|
|
|
|
let mut idx = 0;
|
|
|
|
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Copying {} bytes", size);
|
|
|
|
|
2016-11-16 20:55:54 +00:00
|
|
|
for item in deque {
|
|
|
|
let data_item = item.as_slice();
|
|
|
|
|
|
|
|
let to_copy = cmp::min(left, data_item.len() - skip);
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER,
|
|
|
|
"Copying {} bytes from {:?}, {} more to go",
|
|
|
|
to_copy,
|
|
|
|
item,
|
|
|
|
left - to_copy);
|
|
|
|
|
2016-11-16 20:55:54 +00:00
|
|
|
data[idx..idx + to_copy].copy_from_slice(&data_item[skip..skip + to_copy]);
|
|
|
|
skip = 0;
|
|
|
|
idx += to_copy;
|
|
|
|
left -= to_copy;
|
|
|
|
if left == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(left, 0);
|
|
|
|
}
|
|
|
|
|
2016-11-24 14:29:43 +00:00
|
|
|
pub fn peek_into(&self, data: &mut [u8]) -> Result<(), AdapterError> {
|
|
|
|
let size = data.len();
|
|
|
|
|
|
|
|
if self.size < size {
|
2016-12-27 15:47:39 +00:00
|
|
|
debug!(LOGGER,
|
|
|
|
"Peeking {} bytes into, not enough data: have {}",
|
|
|
|
size,
|
|
|
|
self.size);
|
2016-11-24 14:29:43 +00:00
|
|
|
return Err(AdapterError::NotEnoughData);
|
|
|
|
}
|
|
|
|
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Peeking {} bytes into", size);
|
2016-11-24 14:29:43 +00:00
|
|
|
if size == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::copy_data(&self.deque, self.skip, data, size);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:31:35 +00:00
|
|
|
pub fn peek(&mut self, size: usize) -> Result<&[u8], AdapterError> {
|
|
|
|
if self.size < size {
|
2016-12-27 15:47:39 +00:00
|
|
|
debug!(LOGGER,
|
|
|
|
"Peeking {} bytes, not enough data: have {}",
|
|
|
|
size,
|
|
|
|
self.size);
|
2016-09-17 12:31:35 +00:00
|
|
|
return Err(AdapterError::NotEnoughData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if size == 0 {
|
|
|
|
return Ok(&[]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(front) = self.deque.front() {
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Peeking {} bytes, subbuffer of first", size);
|
2016-09-17 12:31:35 +00:00
|
|
|
if front.get_size() - self.skip >= size {
|
|
|
|
return Ok(&front.as_slice()[self.skip..self.skip + size]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Peeking {} bytes, copy to scratch", size);
|
|
|
|
|
2016-09-17 12:31:35 +00:00
|
|
|
self.scratch.truncate(0);
|
|
|
|
self.scratch.reserve(size);
|
|
|
|
{
|
|
|
|
let data = self.scratch.as_mut_slice();
|
2016-11-16 20:55:54 +00:00
|
|
|
Self::copy_data(&self.deque, self.skip, data, size);
|
2016-09-17 12:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.scratch.as_slice())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_buffer(&mut self, size: usize) -> Result<Buffer, AdapterError> {
|
|
|
|
if self.size < size {
|
2016-12-27 15:47:39 +00:00
|
|
|
debug!(LOGGER,
|
|
|
|
"Get buffer of {} bytes, not enough data: have {}",
|
|
|
|
size,
|
|
|
|
self.size);
|
2016-09-17 12:31:35 +00:00
|
|
|
return Err(AdapterError::NotEnoughData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if size == 0 {
|
|
|
|
return Ok(Buffer::new());
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:42:16 +00:00
|
|
|
let sub = self.deque.front().and_then(|front| {
|
|
|
|
if front.get_size() - self.skip >= size {
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Get buffer of {} bytes, subbuffer of first", size);
|
2016-11-25 12:42:16 +00:00
|
|
|
let new = front.get_buffer().copy_region(self.skip, Some(size)).unwrap();
|
|
|
|
Some(new)
|
2016-09-17 12:31:35 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-11-25 12:42:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(s) = sub {
|
2016-09-17 12:31:35 +00:00
|
|
|
self.flush(size).unwrap();
|
2016-11-25 12:42:16 +00:00
|
|
|
return Ok(s);
|
2016-09-17 12:31:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Get buffer of {} bytes, copy into new buffer", size);
|
2016-09-17 12:31:35 +00:00
|
|
|
let mut new = Buffer::new_with_size(size).unwrap();
|
|
|
|
{
|
|
|
|
let mut map = new.map_readwrite().unwrap();
|
|
|
|
let data = map.as_mut_slice();
|
2016-11-16 20:55:54 +00:00
|
|
|
Self::copy_data(&self.deque, self.skip, data, size);
|
2016-09-17 12:31:35 +00:00
|
|
|
}
|
|
|
|
self.flush(size).unwrap();
|
|
|
|
Ok(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn flush(&mut self, size: usize) -> Result<(), AdapterError> {
|
|
|
|
if self.size < size {
|
2016-12-27 15:47:39 +00:00
|
|
|
debug!(LOGGER,
|
|
|
|
"Flush {} bytes, not enough data: have {}",
|
|
|
|
size,
|
|
|
|
self.size);
|
2016-09-17 12:31:35 +00:00
|
|
|
return Err(AdapterError::NotEnoughData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if size == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER, "Flushing {} bytes, have {}", size, self.size);
|
|
|
|
|
2016-09-17 12:31:35 +00:00
|
|
|
let mut left = size;
|
|
|
|
while left > 0 {
|
|
|
|
let front_size = self.deque.front().unwrap().get_size() - self.skip;
|
|
|
|
|
|
|
|
if front_size <= left {
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER,
|
|
|
|
"Flushing whole {:?}, {} more to go",
|
|
|
|
self.deque.front(),
|
|
|
|
left - front_size);
|
2016-09-17 12:31:35 +00:00
|
|
|
self.deque.pop_front();
|
|
|
|
self.size -= front_size;
|
|
|
|
self.skip = 0;
|
|
|
|
left -= front_size;
|
|
|
|
} else {
|
2016-12-27 15:47:39 +00:00
|
|
|
trace!(LOGGER,
|
|
|
|
"Flushing partial {:?}, {} more left",
|
|
|
|
self.deque.front(),
|
|
|
|
front_size - left);
|
2016-09-17 12:31:35 +00:00
|
|
|
self.skip += left;
|
|
|
|
self.size -= left;
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use buffer::*;
|
|
|
|
use std::ptr;
|
|
|
|
use std::os::raw::c_void;
|
|
|
|
|
|
|
|
fn init() {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_init(argc: *mut c_void, argv: *mut c_void);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
gst_init(ptr::null_mut(), ptr::null_mut());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_get() {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let mut a = Adapter::new();
|
|
|
|
|
|
|
|
a.push(Buffer::new_with_size(100).unwrap());
|
|
|
|
assert_eq!(a.get_available(), 100);
|
|
|
|
a.push(Buffer::new_with_size(20).unwrap());
|
|
|
|
assert_eq!(a.get_available(), 120);
|
|
|
|
|
|
|
|
let b = a.get_buffer(20).unwrap();
|
|
|
|
assert_eq!(a.get_available(), 100);
|
|
|
|
assert_eq!(b.get_size(), 20);
|
|
|
|
let b = a.get_buffer(90).unwrap();
|
|
|
|
assert_eq!(a.get_available(), 10);
|
|
|
|
assert_eq!(b.get_size(), 90);
|
|
|
|
|
|
|
|
a.push(Buffer::new_with_size(20).unwrap());
|
|
|
|
assert_eq!(a.get_available(), 30);
|
|
|
|
|
|
|
|
let b = a.get_buffer(20).unwrap();
|
|
|
|
assert_eq!(a.get_available(), 10);
|
|
|
|
assert_eq!(b.get_size(), 20);
|
|
|
|
let b = a.get_buffer(10).unwrap();
|
|
|
|
assert_eq!(a.get_available(), 0);
|
|
|
|
assert_eq!(b.get_size(), 10);
|
|
|
|
|
|
|
|
let b = a.get_buffer(1);
|
|
|
|
assert_eq!(b.err().unwrap(), AdapterError::NotEnoughData);
|
|
|
|
}
|
|
|
|
}
|