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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// 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 glib_ffi::{gboolean, gpointer};
use gst;
use std::cell::RefCell;
use std::mem;
use std::ptr;
use AppSrc;

#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
pub struct AppSrcCallbacks {
    need_data: Option<RefCell<Box<FnMut(&AppSrc, u32) + Send + 'static>>>,
    enough_data: Option<Box<Fn(&AppSrc) + Send + Sync + 'static>>,
    seek_data: Option<Box<Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>>,
    callbacks: ffi::GstAppSrcCallbacks,
}

unsafe impl Send for AppSrcCallbacks {}
unsafe impl Sync for AppSrcCallbacks {}

impl AppSrcCallbacks {
    pub fn new() -> AppSrcCallbacksBuilder {
        skip_assert_initialized!();

        AppSrcCallbacksBuilder {
            need_data: None,
            enough_data: None,
            seek_data: None,
        }
    }
}

#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
pub struct AppSrcCallbacksBuilder {
    need_data: Option<RefCell<Box<FnMut(&AppSrc, u32) + Send + 'static>>>,
    enough_data: Option<Box<Fn(&AppSrc) + Send + Sync + 'static>>,
    seek_data: Option<Box<Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>>,
}

impl AppSrcCallbacksBuilder {
    pub fn need_data<F: FnMut(&AppSrc, u32) + Send + 'static>(self, need_data: F) -> Self {
        Self {
            need_data: Some(RefCell::new(Box::new(need_data))),
            ..self
        }
    }

    pub fn enough_data<F: Fn(&AppSrc) + Send + Sync + 'static>(self, enough_data: F) -> Self {
        Self {
            enough_data: Some(Box::new(enough_data)),
            ..self
        }
    }

    pub fn seek_data<F: Fn(&AppSrc, u64) -> bool + Send + Sync + 'static>(
        self,
        seek_data: F,
    ) -> Self {
        Self {
            seek_data: Some(Box::new(seek_data)),
            ..self
        }
    }

    pub fn build(self) -> AppSrcCallbacks {
        let have_need_data = self.need_data.is_some();
        let have_enough_data = self.enough_data.is_some();
        let have_seek_data = self.seek_data.is_some();

        AppSrcCallbacks {
            need_data: self.need_data,
            enough_data: self.enough_data,
            seek_data: self.seek_data,
            callbacks: ffi::GstAppSrcCallbacks {
                need_data: if have_need_data {
                    Some(trampoline_need_data)
                } else {
                    None
                },
                enough_data: if have_enough_data {
                    Some(trampoline_enough_data)
                } else {
                    None
                },
                seek_data: if have_seek_data {
                    Some(trampoline_seek_data)
                } else {
                    None
                },
                _gst_reserved: [
                    ptr::null_mut(),
                    ptr::null_mut(),
                    ptr::null_mut(),
                    ptr::null_mut(),
                ],
            },
        }
    }
}

unsafe extern "C" fn trampoline_need_data(
    appsrc: *mut ffi::GstAppSrc,
    length: u32,
    callbacks: gpointer,
) {
    let callbacks = &*(callbacks as *const AppSrcCallbacks);

    if let Some(ref need_data) = callbacks.need_data {
        (&mut *need_data.borrow_mut())(&from_glib_borrow(appsrc), length);
    }
}

unsafe extern "C" fn trampoline_enough_data(appsrc: *mut ffi::GstAppSrc, callbacks: gpointer) {
    let callbacks = &*(callbacks as *const AppSrcCallbacks);

    if let Some(ref enough_data) = callbacks.enough_data {
        (*enough_data)(&from_glib_borrow(appsrc));
    }
}

unsafe extern "C" fn trampoline_seek_data(
    appsrc: *mut ffi::GstAppSrc,
    offset: u64,
    callbacks: gpointer,
) -> gboolean {
    let callbacks = &*(callbacks as *const AppSrcCallbacks);

    let ret = if let Some(ref seek_data) = callbacks.seek_data {
        (*seek_data)(&from_glib_borrow(appsrc), offset)
    } else {
        false
    };

    ret.to_glib()
}

unsafe extern "C" fn destroy_callbacks(ptr: gpointer) {
    Box::<AppSrcCallbacks>::from_raw(ptr as *mut _);
}

impl AppSrc {
    /// Adds a buffer to the queue of buffers that the appsrc element will
    /// push to its source pad. This function takes ownership of the buffer.
    ///
    /// When the block property is TRUE, this function can block until free
    /// space becomes available in the queue.
    /// ## `buffer`
    /// a `gst::Buffer` to push
    ///
    /// # Returns
    ///
    /// `gst::FlowReturn::Ok` when the buffer was successfuly queued.
    /// `gst::FlowReturn::Flushing` when `self` is not PAUSED or PLAYING.
    /// `gst::FlowReturn::Eos` when EOS occured.
    pub fn push_buffer(&self, buffer: gst::Buffer) -> gst::FlowReturn {
        unsafe {
            from_glib(ffi::gst_app_src_push_buffer(
                self.to_glib_none().0,
                buffer.into_ptr(),
            ))
        }
    }

    /// Adds a buffer list to the queue of buffers and buffer lists that the
    /// appsrc element will push to its source pad. This function takes ownership
    /// of `buffer_list`.
    ///
    /// When the block property is TRUE, this function can block until free
    /// space becomes available in the queue.
    ///
    /// Feature: `v1_14`
    ///
    /// ## `buffer_list`
    /// a `gst::BufferList` to push
    ///
    /// # Returns
    ///
    /// `gst::FlowReturn::Ok` when the buffer list was successfuly queued.
    /// `gst::FlowReturn::Flushing` when `self` is not PAUSED or PLAYING.
    /// `gst::FlowReturn::Eos` when EOS occured.
    #[cfg(any(feature = "v1_14", feature = "dox"))]
    pub fn push_buffer_list(&self, list: gst::BufferList) -> gst::FlowReturn {
        unsafe {
            from_glib(ffi::gst_app_src_push_buffer_list(
                self.to_glib_none().0,
                list.into_ptr(),
            ))
        }
    }

    /// Set callbacks which will be executed when data is needed, enough data has
    /// been collected or when a seek should be performed.
    /// This is an alternative to using the signals, it has lower overhead and is thus
    /// less expensive, but also less flexible.
    ///
    /// If callbacks are installed, no signals will be emitted for performance
    /// reasons.
    /// ## `callbacks`
    /// the callbacks
    /// ## `user_data`
    /// a user_data argument for the callbacks
    /// ## `notify`
    /// a destroy notify function
    pub fn set_callbacks(&self, callbacks: AppSrcCallbacks) {
        unsafe {
            ffi::gst_app_src_set_callbacks(
                self.to_glib_none().0,
                mut_override(&callbacks.callbacks),
                Box::into_raw(Box::new(callbacks)) as *mut _,
                Some(destroy_callbacks),
            );
        }
    }

    /// Configure the `min` and `max` latency in `src`. If `min` is set to -1, the
    /// default latency calculations for pseudo-live sources will be used.
    /// ## `min`
    /// the min latency
    /// ## `max`
    /// the max latency
    pub fn set_latency(&self, min: gst::ClockTime, max: gst::ClockTime) {
        unsafe {
            ffi::gst_app_src_set_latency(self.to_glib_none().0, min.to_glib(), max.to_glib());
        }
    }

    /// Retrieve the min and max latencies in `min` and `max` respectively.
    /// ## `min`
    /// the min latency
    /// ## `max`
    /// the max latency
    pub fn get_latency(&self) -> (gst::ClockTime, gst::ClockTime) {
        unsafe {
            let mut min = mem::uninitialized();
            let mut max = mem::uninitialized();
            ffi::gst_app_src_get_latency(self.to_glib_none().0, &mut min, &mut max);
            (from_glib(min), from_glib(max))
        }
    }
}