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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::fmt;

use crate::ffi;
use glib::translate::from_glib;
use gst::prelude::*;

/// Metadata type that holds information about the positioning, size,
/// transparency and composition operator of a video frame in the timeline
/// composition.
#[repr(transparent)]
#[doc(alias = "GESFrameCompositionMeta")]
pub struct FrameCompositionMeta(ffi::GESFrameCompositionMeta);

unsafe impl Send for FrameCompositionMeta {}

unsafe impl Sync for FrameCompositionMeta {}

impl FrameCompositionMeta {
    #[inline]
    pub fn alpha(&self) -> f64 {
        self.0.alpha
    }

    #[inline]
    pub fn set_alpha(&mut self, alpha: f64) {
        self.0.alpha = alpha;
    }

    #[inline]
    pub fn position(&self) -> (f64, f64) {
        (self.0.posx, self.0.posy)
    }

    #[inline]
    pub fn set_position(&mut self, posx: f64, posy: f64) {
        self.0.posx = posx;
        self.0.posy = posy;
    }

    #[inline]
    pub fn pos_x(&self) -> f64 {
        self.0.posx
    }

    #[inline]
    pub fn set_pos_x(&mut self, pos_x: f64) {
        self.0.posx = pos_x;
    }

    #[inline]
    pub fn pos_y(&self) -> f64 {
        self.0.posy
    }

    #[inline]
    pub fn set_pos_y(&mut self, pos_y: f64) {
        self.0.posy = pos_y;
    }

    #[inline]
    pub fn size(&self) -> (f64, f64) {
        (self.0.width, self.0.height)
    }

    pub fn set_size(&mut self, width: f64, height: f64) {
        self.0.width = width;
        self.0.height = height;
    }

    #[inline]
    pub fn width(&self) -> f64 {
        self.0.width
    }

    #[inline]
    pub fn set_width(&mut self, width: f64) {
        self.0.width = width;
    }

    #[inline]
    pub fn height(&self) -> f64 {
        self.0.height
    }

    #[inline]
    pub fn set_height(&mut self, height: f64) {
        self.0.height = height;
    }

    #[inline]
    pub fn zorder(&self) -> u32 {
        self.0.zorder
    }

    #[inline]
    pub fn set_zorder(&mut self, zorder: u32) {
        self.0.zorder = zorder;
    }

    #[inline]
    pub fn operator(&self) -> i32 {
        self.0.operator
    }

    #[inline]
    pub fn set_operator(&mut self, operator: i32) {
        self.0.operator = operator;
    }
}

unsafe impl MetaAPI for FrameCompositionMeta {
    type GstType = ffi::GESFrameCompositionMeta;

    #[doc(alias = "ges_frame_composition_meta_api_get_type")]
    #[inline]
    fn meta_api() -> glib::Type {
        unsafe { from_glib(ffi::ges_frame_composition_meta_api_get_type()) }
    }
}

impl fmt::Debug for FrameCompositionMeta {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("FrameCompositionMeta")
            .field("pos-x", &self.pos_x())
            .field("pos-y", &self.pos_y())
            .field("width", &self.width())
            .field("height", &self.height())
            .field("zorder", &self.zorder())
            .field("alpha", &self.alpha())
            .field("operator", &self.operator())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn add_composition_meta(
        buffer: &mut gst::BufferRef,
        position: (f64, f64),
        size: (f64, f64),
        alpha: f64,
        zorder: u32,
        operator: i32,
    ) -> Result<gst::MetaRefMut<FrameCompositionMeta, gst::meta::Standalone>, glib::BoolError> {
        assert_initialized_main_thread!();

        unsafe {
            let meta = ffi::ges_buffer_add_frame_composition_meta(buffer.as_mut_ptr());

            if meta.is_null() {
                return Err(glib::bool_error!("Failed to add frame composition meta"));
            }

            let mut result = FrameCompositionMeta::from_mut_ptr(buffer, meta);
            result.0.posx = position.0;
            result.0.posy = position.1;
            result.0.width = size.0;
            result.0.height = size.1;
            result.0.alpha = alpha;
            result.0.zorder = zorder;
            result.0.operator = operator;
            Ok(result)
        }
    }

    #[test]
    fn test_add_get_meta() {
        gst::init().unwrap();
        crate::init().unwrap();

        let mut buffer = gst::Buffer::with_size(320 * 240 * 4).unwrap();
        {
            let _meta = add_composition_meta(
                buffer.get_mut().unwrap(),
                (42., 42.),
                (20., 22.),
                0.42,
                2,
                42,
            )
            .unwrap();
        }

        {
            let meta = buffer.meta::<FrameCompositionMeta>().unwrap();
            assert_eq!(meta.position(), (42., 42.));
            assert_eq!(meta.size(), (20., 22.));
            assert_eq!(meta.alpha(), 0.42);
            assert_eq!(meta.zorder(), 2);
            assert_eq!(meta.operator(), 42);
        }
    }
}