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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use ffi::*;
use bus::Bus;
use util::*;
use pad::Pad;
use caps::Caps;
use reference::Reference;
use object::{Object, Property, FromProperty};
use std::os::raw::c_void;
use std::ops::{Deref, DerefMut};
unsafe impl Sync for GstElement {}
unsafe impl Send for GstElement {}
unsafe impl Sync for Element {}
unsafe impl Send for Element {}
pub struct Element{
element: Object
}
impl Element {
pub fn new(factory_name: &str, element_name: &str) -> Option<Element> {
let cname = CString::new(element_name).unwrap();
let element_cname = CString::new(factory_name).unwrap();
unsafe{
let element_name = if element_name != "" {
cname.as_ptr()
} else {
ptr::null()
};
let element = gst_element_factory_make(element_cname.as_ptr(), element_name);
if element != ptr::null_mut::<GstElement>() {
gst_object_ref_sink(mem::transmute(element));
Some( Element{element: Object::new(element as * mut GstObject).unwrap()} )
} else {
println!("Error creating {} return {:?}", factory_name, element);
None
}
}
}
pub fn factory_make(element: &str, name: &str) -> Option<Element>{
Element::new(element,name)
}
pub unsafe fn new_from_gst_element(element: *mut GstElement) -> Option<Element>{
Object::new(element as * mut GstObject)
.map(|obj| Element{element: obj})
}
pub fn link_many(elements: &[&mut Element]) -> bool {
elements.windows(2).fold(true, |ret, elements| {
let mut e1 = elements[0].reference();
let mut e2 = elements[1].reference();
ret && e1.link(&mut e2)
})
}
pub fn link(&mut self, dst: &mut Element) -> bool{
unsafe{
gst_element_link(self.gst_element_mut(), dst.gst_element_mut()) == 1
}
}
pub fn link_filtered(&mut self, dst: &mut Element, filter: &Caps) -> bool{
unsafe{
gst_element_link_filtered(self.gst_element_mut(),
dst.gst_element_mut(),
filter.gst_caps() as *mut GstCaps) == 1
}
}
pub fn unlink(&mut self, dst: &mut Element){
unsafe{
gst_element_unlink(self.gst_element_mut(), dst.gst_element_mut());
}
}
pub fn bus(&self) -> Option<Bus>{
unsafe{
Bus::new(gst_element_get_bus(mem::transmute(self.gst_element())))
}
}
pub fn set_name(&mut self, name: &str){
let cname = CString::new(name).unwrap();
unsafe{
gst_object_set_name(self.gst_element() as *mut GstObject, cname.as_ptr());
}
}
pub fn set_state(&mut self, state: GstState) -> GstStateChangeReturn{
unsafe{
gst_element_set_state(self.gst_element_mut(), state)
}
}
pub fn get_state(&self, timeout: GstClockTime) -> (GstState, GstState, GstStateChangeReturn){
let mut state: GstState = GST_STATE_NULL;
let mut pending: GstState = GST_STATE_NULL;
unsafe{
let ret = gst_element_get_state(mem::transmute(self.gst_element()), &mut state, &mut pending, timeout);
(state, pending, ret)
}
}
pub unsafe fn send_event(&mut self, event: *mut GstEvent) -> bool{
gst_element_send_event(self.gst_element_mut(), event) == 1
}
pub fn seek_simple(&mut self, format: GstFormat, flags: GstSeekFlags, pos: i64) -> bool{
unsafe{
gst_element_seek_simple(self.gst_element_mut(), format, flags, pos) == 1
}
}
pub fn seek(&mut self, rate: f64, format: GstFormat, flags: GstSeekFlags, start_type: GstSeekType, start: i64, stop_type: GstSeekType, stop: i64) -> bool{
unsafe{
gst_element_seek(self.gst_element_mut(), rate, format, flags, start_type, start, stop_type, stop) == 1
}
}
pub fn query_duration(&self, format: GstFormat) -> Option<i64>{
unsafe{
let mut duration = 0;
if gst_element_query_duration(mem::transmute(self.gst_element()), format, &mut duration) == 1{
Some(duration)
}else{
None
}
}
}
pub fn query_position(&self, format: GstFormat) -> Option<i64>{
unsafe{
let mut pos = 0;
if gst_element_query_position(mem::transmute(self.gst_element()), format, &mut pos) == 1{
Some(pos)
}else{
None
}
}
}
pub fn duration_ns(&self) -> Option<i64>{
self.query_duration(GST_FORMAT_TIME)
}
pub fn duration_s(&self) -> Option<f64>{
let duration_ns = self.duration_ns();
match duration_ns{
Some(t) => Some(ns_to_s(t as u64)),
None => None
}
}
pub fn position_ns(&self) -> Option<i64>{
self.query_position(GST_FORMAT_TIME)
}
pub fn position_pct(&self) -> Option<f64>{
let pos = self.position_ns();
let dur = self.duration_ns();
if dur.is_some() && pos.is_some(){
Some( pos.unwrap() as f64 / dur.unwrap() as f64 )
}else{
None
}
}
pub fn position_s(&self) -> Option<f64>{
if let Some(pos_ns) = self.position_ns(){
Some(ns_to_s(pos_ns as u64))
}else{
None
}
}
pub fn set_position_ns(&mut self, ns: i64) -> bool{
let format = GST_FORMAT_TIME;
let flags = GST_SEEK_FLAG_FLUSH;
self.seek_simple(format, flags, ns)
}
pub fn set_position_s(&mut self, s: f64) -> bool{
self.set_position_ns(s_to_ns(s) as i64)
}
pub fn set_position_pct(&mut self, pct: f64) -> bool{
let dur = self.duration_ns();
match dur{
Some(t) => self.set_position_ns((t as f64 * pct) as i64),
None => false
}
}
pub fn set_speed(&mut self, speed: f64) -> bool{
let format = GST_FORMAT_TIME;
let flags = GST_SEEK_FLAG_SKIP | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH;
if speed==0.0 {
return self.set_state(GST_STATE_PAUSED) != GST_STATE_CHANGE_FAILURE;
}
let pos_opt = self.query_position(GST_FORMAT_TIME);
if pos_opt.is_none(){
return false;
}
let pos = pos_opt.unwrap();
if speed > 0.0 {
self.seek(speed, format,
flags,
GST_SEEK_TYPE_SET,
pos,
GST_SEEK_TYPE_SET,
-1)
} else {
self.seek(speed, format,
flags,
GST_SEEK_TYPE_SET,
0,
GST_SEEK_TYPE_SET,
pos)
}
}
pub fn set_null_state(&mut self) -> GstStateChangeReturn{
self.set_state(GST_STATE_NULL)
}
pub fn set_ready_state(&mut self) -> GstStateChangeReturn{
self.set_state(GST_STATE_READY)
}
pub fn pause(&mut self) -> GstStateChangeReturn{
self.set_state(GST_STATE_PAUSED)
}
pub fn play(&mut self) -> GstStateChangeReturn{
self.set_state(GST_STATE_PLAYING)
}
pub fn is_paused(&self) -> bool{
if let (GST_STATE_PAUSED, _pending, GST_STATE_CHANGE_SUCCESS) = self.get_state(GST_CLOCK_TIME_NONE){
true
}else{
false
}
}
pub fn is_playing(&self) -> bool{
if let (GST_STATE_PLAYING, _pending, GST_STATE_CHANGE_SUCCESS) = self.get_state(GST_CLOCK_TIME_NONE){
true
}else{
false
}
}
pub fn is_null_state(&self) -> bool{
if let (GST_STATE_NULL, _pending, GST_STATE_CHANGE_SUCCESS) = self.get_state(GST_CLOCK_TIME_NONE){
true
}else{
false
}
}
pub fn is_ready_state(&self) -> bool{
if let (GST_STATE_READY, _pending, GST_STATE_CHANGE_SUCCESS) = self.get_state(GST_CLOCK_TIME_NONE){
true
}else{
false
}
}
pub fn static_pad(&mut self, name: &str) -> Option<Pad>{
let cname = CString::new(name).unwrap();
unsafe{
let pad = gst_element_get_static_pad(self.gst_element_mut(), cname.as_ptr());
Pad::new(pad)
}
}
pub unsafe fn gst_element(&self) -> *const GstElement{
self.element.gst_object() as *const GstElement
}
pub unsafe fn gst_element_mut(&mut self) -> *mut GstElement{
self.element.gst_object_mut() as *mut GstElement
}
}
impl ::Transfer for Element{
unsafe fn transfer(self) -> *mut GstElement{
self.element.transfer() as *mut GstElement
}
}
impl Reference for Element{
fn reference(&self) -> Element{
Element{element: self.element.reference()}
}
}
impl AsRef<Object> for Element{
fn as_ref(&self) -> &Object{
&self.element
}
}
impl AsMut<Object> for Element{
fn as_mut(&mut self) -> &mut Object{
&mut self.element
}
}
impl From<Element> for Object{
fn from(b: Element) -> Object{
b.element
}
}
impl Deref for Element{
type Target = Object;
fn deref(&self) -> &Object{
&self.element
}
}
impl DerefMut for Element{
fn deref_mut(&mut self) -> &mut Object{
&mut self.element
}
}
impl ::FromGValue for Element{
fn from_gvalue(value: &GValue) -> Option<Element>{
unsafe{
if g_type_check_value_holds(mem::transmute(value), gst_element_get_type()) != 0{
let ptr = g_value_get_object(value);
gst_object_ref(ptr);
Element::new_from_gst_element(ptr as *mut GstElement)
}else{
None
}
}
}
}
impl<'a> Property for &'a Element{
type Target = *mut GstElement;
#[inline]
fn set_to(&self, key: &str, e: &mut Object){
let cname = CString::new(key).unwrap();
unsafe{
g_object_set(e.gst_object() as *mut c_void, cname.as_ptr(), self.gst_element(), ptr::null::<gchar>());
}
}
}
impl Property for ::Ref<Element>{
type Target = *mut GstElement;
#[inline]
fn set_to(&self, key: &str, e: &mut Object){
let cname = CString::new(key).unwrap();
unsafe{
g_object_set(e.gst_object() as *mut c_void, cname.as_ptr(), self.gst_element(), ptr::null::<gchar>());
}
}
}
impl<'a> FromProperty for ::Ref<Element>{
fn from_property(element: *mut GstElement) -> ::Ref<Element>{
unsafe{
::Ref::new(&Element::new_from_gst_element(element).unwrap())
}
}
}