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
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ffi;
use glib::translate::*;
use gst;
use std::io;
use Adapter;

impl Adapter {
    /// Copies `size` bytes of data starting at `offset` out of the buffers
    /// contained in `Adapter` into an array `dest` provided by the caller.
    ///
    /// The array `dest` should be large enough to contain `size` bytes.
    /// The user should check that the adapter has (`offset` + `size`) bytes
    /// available before calling this function.
    /// ## `dest`
    ///
    ///  the memory to copy into
    /// ## `offset`
    /// the bytes offset in the adapter to start from
    /// ## `size`
    /// the number of bytes to copy
    pub fn copy(&self, offset: usize, dest: &mut [u8]) {
        unsafe {
            let size = dest.len();
            ffi::gst_adapter_copy(
                self.to_glib_none().0,
                dest.as_mut_ptr() as *mut _,
                offset,
                size,
            );
        }
    }

    /// Adds the data from `buf` to the data stored inside `self` and takes
    /// ownership of the buffer.
    /// ## `buf`
    /// a `gst::Buffer` to add to queue in the adapter
    pub fn push(&self, buf: gst::Buffer) {
        unsafe {
            ffi::gst_adapter_push(self.to_glib_none().0, buf.into_ptr());
        }
    }
}

impl io::Read for Adapter {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
        let mut len = self.available();

        if len == 0 {
            return Err(io::Error::new(
                io::ErrorKind::WouldBlock,
                format!(
                    "Missing data: requesting {} but only got {}.",
                    buf.len(),
                    len
                ),
            ));
        }

        if buf.len() < len {
            len = buf.len();
        }

        self.copy(0, &mut buf[0..len]);
        self.flush(len);
        Ok(len)
    }
}