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
use ffi::*;
use std::mem;
use std::ptr;

use reference::Reference;

pub struct MiniObject{
    miniobject: *mut GstMiniObject,
}

impl Drop for MiniObject{
	fn drop(&mut self){
        unsafe{
			gst_mini_object_unref(self.miniobject);
		}
	}
}

#[repr(u32)]
#[derive(Copy,Clone,Debug)]
pub enum LockFlags{
    Read = GST_LOCK_FLAG_READ,
    Write = GST_LOCK_FLAG_WRITE,
    Exclusive = GST_LOCK_FLAG_EXCLUSIVE,
    Last = GST_LOCK_FLAG_LAST,
}

impl MiniObject{
    pub unsafe fn new_from_gst_miniobject(miniobject: *mut GstMiniObject) -> Option<MiniObject>{
        if miniobject != ptr::null_mut(){
            Some(MiniObject{
                miniobject: miniobject
            })
        }else{
            None
        }
    }

    pub fn lock<F:FnMut(&mut MiniObject)>(&mut self, flags: &[LockFlags], mut f: F) -> bool{
        let flags = flags.iter().fold(0,|ret, flag| ret | *flag as u32);
        unsafe{
            if gst_mini_object_lock(self.miniobject, flags) != 0{
                f(self);
                gst_mini_object_unlock(self.miniobject, flags);
                true
            }else{
                false
            }
        }
    }

    pub fn make_writable(self) -> MiniObject{
        unsafe{
            MiniObject{
                miniobject: gst_mini_object_make_writable(self.miniobject)
            }
        }
    }

    pub unsafe fn gst_miniobject(&self) -> *const GstMiniObject{
        self.miniobject as *const GstMiniObject
    }

    pub unsafe fn gst_miniobject_mut(&mut self) -> *mut GstMiniObject{
        self.miniobject
    }
}

impl Clone for MiniObject{
	fn clone(&self) -> MiniObject{
		unsafe{
			MiniObject{ miniobject: gst_mini_object_copy(self.miniobject as *mut GstMiniObject) }
		}
	}
}

impl Reference for MiniObject{
    fn reference(&self) -> MiniObject{
        unsafe{
			MiniObject{ miniobject: gst_mini_object_ref(self.miniobject) }
		}
    }
}

impl ::Transfer<GstMiniObject> for MiniObject{
    unsafe fn transfer(self) ->  *mut GstMiniObject{
        let miniobject = self.miniobject;
		mem::forget(self);
        miniobject
    }
}