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
249
use ffi::*;
use std::ptr;
use std::mem;
use std::sync::mpsc::{Sender,Receiver,TryRecvError,RecvError,SendError,channel};
use std::ops::{Deref, DerefMut};
use std::rc::{Rc, Weak};
use sample::Sample;
use element::Element;
use caps::Caps;
pub enum Message{
NewSample(Sample),
NewPreroll(Sample),
Eos
}
impl Message{
pub fn is_eos(&self) -> bool{
match self{
&Message::Eos => true,
_ => false
}
}
pub fn is_preroll(&self) -> bool{
match self{
&Message::NewPreroll(..) => true,
_ => false
}
}
pub fn is_sample(&self) -> bool{
match self{
&Message::NewSample(..) => true,
_ => false
}
}
}
unsafe impl Sync for AppSink {}
unsafe impl Send for AppSink {}
#[allow(dead_code)]
pub struct AppSink{
appsink: Element,
samples_receiver: Receiver<Message>,
samples_sender: Rc<Sender<Message>>
}
impl AppSink{
pub fn new(name: &str) -> Option<AppSink>{
let (sender,receiver) = channel();
let sender = Rc::new(sender);
let appsink = Element::new("appsink",name);
unsafe{
match appsink{
Some(a) => {
let mut gst_callbacks = GstAppSinkCallbacks{
eos: Some(on_eos_from_source),
new_preroll: Some(on_new_preroll_from_source),
new_sample: Some(on_new_sample_from_source),
_gst_reserved: [ptr::null_mut(); 4]
};
let weak_sender = Box::new(Rc::downgrade(&sender));
gst_app_sink_set_callbacks(a.gst_element() as *mut GstAppSink, &mut gst_callbacks, mem::transmute(weak_sender), None);
Some(AppSink{ appsink: a, samples_receiver: receiver, samples_sender: sender })
},
None => None
}
}
}
pub fn new_from_element(element: Element) -> AppSink{
let (sender,receiver) = channel();
let sender = Rc::new(sender);
unsafe{
let mut gst_callbacks = GstAppSinkCallbacks{
eos: Some(on_eos_from_source),
new_preroll: Some(on_new_preroll_from_source),
new_sample: Some(on_new_sample_from_source),
_gst_reserved: [ptr::null_mut(); 4]
};
let weak_sender = Box::new(Rc::downgrade(&sender));
gst_app_sink_set_callbacks(element.gst_element() as *mut GstAppSink, &mut gst_callbacks, mem::transmute(weak_sender), None);
}
AppSink{ appsink: element, samples_receiver: receiver, samples_sender: sender }
}
pub fn recv(&self) -> Result<Message,RecvError>{
self.samples_receiver.recv()
}
pub fn try_recv(&self) -> Result<Message,TryRecvError>{
self.samples_receiver.try_recv()
}
pub unsafe fn gst_appsink(&self) -> *const GstAppSink{
self.appsink.gst_element() as *const GstAppSink
}
pub unsafe fn gst_appsink_mut(&mut self) -> *mut GstAppSink{
self.appsink.gst_element() as *mut GstAppSink
}
pub fn set_caps(&mut self, caps: Caps){
unsafe{
gst_app_sink_set_caps(self.gst_appsink_mut(), caps.gst_caps() as *const GstCaps);
}
}
pub fn get_caps(&self) -> Option<Caps>{
unsafe{
let caps = gst_app_sink_get_caps(mem::transmute(self.gst_appsink()));
Caps::new(caps)
}
}
pub fn is_eos(&self) -> bool{
unsafe{
gst_app_sink_is_eos(mem::transmute(self.gst_appsink())) == 1
}
}
pub fn set_emit_signals(&mut self, emit: bool){
unsafe{
gst_app_sink_set_emit_signals(self.gst_appsink_mut(), emit as gboolean);
}
}
pub fn get_emit_signals(&self) -> bool{
unsafe{
gst_app_sink_get_emit_signals(mem::transmute(self.gst_appsink())) == 1
}
}
pub fn set_max_buffers(&mut self, max_buffers: u32){
unsafe{
gst_app_sink_set_max_buffers(self.gst_appsink_mut(), max_buffers);
}
}
pub fn max_buffers(&self) -> u32{
unsafe{
gst_app_sink_get_max_buffers(mem::transmute(self.gst_appsink()))
}
}
pub fn set_drop(&mut self, drop: bool){
unsafe{
gst_app_sink_set_drop(self.gst_appsink_mut(), drop as gboolean);
}
}
pub fn get_drop(&self) -> bool{
unsafe{
gst_app_sink_get_drop(mem::transmute(self.gst_appsink())) == 1
}
}
}
extern "C" fn on_new_sample_from_source (elt: *mut GstAppSink, data: gpointer ) -> GstFlowReturn{
unsafe{
let sender = data as *mut Weak<Sender<Message>>;
if let Some(sender) = (*sender).upgrade(){
let sample = gst_app_sink_pull_sample (elt);
match Sample::new(sample){
Some(sample) => {
match sender.send(Message::NewSample(sample)){
Ok(()) => GST_FLOW_OK,
Err(SendError(_msg)) => GST_FLOW_EOS
}
}
None => GST_FLOW_EOS
}
}else{
GST_FLOW_EOS
}
}
}
extern "C" fn on_new_preroll_from_source (elt: *mut GstAppSink, data: gpointer) -> GstFlowReturn{
unsafe{
let sender = data as *mut Weak<Sender<Message>>;
if let Some(sender) = (*sender).upgrade(){
let sample = gst_app_sink_pull_preroll (elt);
match Sample::new(sample){
Some(sample) => {
match sender.send(Message::NewPreroll(sample)){
Ok(()) => GST_FLOW_OK,
Err(SendError(_msg)) => GST_FLOW_EOS
}
}
None => GST_FLOW_EOS
}
}else{
GST_FLOW_EOS
}
}
}
extern "C" fn on_eos_from_source (_elt: *mut GstAppSink, data: gpointer){
unsafe{
let sender = data as *mut Weak<Sender<Message>>;
if let Some(sender) = (*sender).upgrade(){
sender.send(Message::Eos).unwrap();
}
}
}
impl AsRef<Element> for AppSink{
fn as_ref(&self) -> &Element{
&self.appsink
}
}
impl AsMut<Element> for AppSink{
fn as_mut(&mut self) -> &mut Element{
&mut self.appsink
}
}
impl From<AppSink> for Element{
fn from(a: AppSink) -> Element{
a.appsink
}
}
impl Deref for AppSink{
type Target = Element;
fn deref(&self) -> &Element{
&self.appsink
}
}
impl DerefMut for AppSink{
fn deref_mut(&mut self) -> &mut Element{
&mut self.appsink
}
}
impl ::Transfer for AppSink{
unsafe fn transfer(self) -> *mut GstElement{
self.appsink.transfer()
}
}