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
use ffi::*;
use caps::Caps;
use buffer::Buffer;
use videoframe::VideoFrame;
use std::mem;
use std::ptr;
use reference::Reference;
use miniobject::MiniObject;

unsafe impl Send for Sample {}

#[derive(Clone)]
pub struct Sample{
	sample: MiniObject
}

impl Sample{
	pub unsafe fn new(sample: *mut GstSample) -> Option<Sample>{
	    MiniObject::new_from_gst_miniobject(sample as *mut GstMiniObject)
			.map(|miniobject| Sample{ sample: miniobject })
	}

	/// Get the buffer associated with sample or None when there is no buffer.
    pub fn buffer(&self) -> Option<Buffer>{
        unsafe{
        	let buffer = gst_sample_get_buffer(mem::transmute(self.gst_sample()));
        	if buffer != ptr::null_mut(){
	            Buffer::new(gst_mini_object_ref(buffer as *mut GstMiniObject) as *mut GstBuffer)
	        }else{
	            None
	        }
        }
    }

	/// Get the caps associated with sample or None when there's no caps
    pub fn caps(&self) -> Option<Caps>{
		unsafe{
			let caps = gst_sample_get_caps(mem::transmute(self.gst_sample()));
        	if caps != ptr::null_mut(){
	            Caps::new(gst_mini_object_ref(caps as *mut GstMiniObject) as *mut GstCaps)
	        }else{
	            None
	        }
		}
	}

    /// Get the segment associated with sample
    pub fn segment(&self) -> GstSegment{
        unsafe{
            (*gst_sample_get_segment(mem::transmute(self.gst_sample())))
        }
    }

    /// Get a video frame from this sample if it contains one
    pub fn video_frame(&self) -> Option<VideoFrame>{
        let buffer = match self.buffer(){
            Some(buffer) => buffer,
            None => return None
        };

        let vi = match self.caps(){
            Some(caps) => match caps.video_info(){
                Some(vi) => vi,
                None => return None
            },
            None => return None
        };

        unsafe{ VideoFrame::new(vi, buffer) }
    }

    pub unsafe fn gst_sample(&self) -> *const GstSample{
		self.sample.gst_miniobject() as *const GstSample
	}

    pub unsafe fn gst_sample_mut(&mut self) -> *mut GstSample{
		self.sample.gst_miniobject_mut() as *mut GstSample
	}
}

impl ::Transfer<GstSample> for Sample{
    unsafe fn transfer(self) ->  *mut GstSample{
        self.sample.transfer() as *mut GstSample
    }
}

impl Reference for Sample{
    fn reference(&self) -> Sample{
        Sample{
			sample: self.sample.reference()
		}
    }
}