Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / shapewipe.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <unistd.h>
22
23 #include <gst/check/gstcheck.h>
24
25 gboolean have_eos = FALSE;
26
27 /* For ease of programming we use globals to keep refs for our floating
28  * src and sink pads we create; otherwise we always have to do get_pad,
29  * get_peer, and then remove references in every test function */
30 GstPad *myvideosrcpad, *mymasksrcpad, *mysinkpad;
31
32
33 #define SHAPEWIPE_VIDEO_CAPS_STRING    \
34     "video/x-raw-yuv, " \
35     "format = (GstFourcc)AYUV, " \
36     "width = 400, " \
37     "height = 400, " \
38     "framerate = 0/1"
39
40 #define SHAPEWIPE_MASK_CAPS_STRING    \
41     "video/x-raw-gray, " \
42     "bpp = 8, " \
43     "depth = 8, " \
44     "width = 400, " \
45     "height = 400, " \
46     "framerate = 0/1"
47
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
49     GST_PAD_SINK,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS (SHAPEWIPE_VIDEO_CAPS_STRING)
52     );
53 static GstStaticPadTemplate videosrctemplate =
54 GST_STATIC_PAD_TEMPLATE ("videosrc",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS (SHAPEWIPE_VIDEO_CAPS_STRING)
58     );
59 static GstStaticPadTemplate masksrctemplate =
60 GST_STATIC_PAD_TEMPLATE ("masksrc",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS (SHAPEWIPE_MASK_CAPS_STRING)
64     );
65
66
67 static GstBuffer *output = NULL;
68
69 static GstFlowReturn
70 on_chain (GstPad * pad, GstBuffer * buffer)
71 {
72   g_return_val_if_fail (output == NULL, GST_FLOW_ERROR);
73
74   output = buffer;
75   return GST_FLOW_OK;
76 }
77
78 GST_START_TEST (test_general)
79 {
80   GstElement *shapewipe;
81   GstPad *p;
82   GstCaps *caps;
83   GstBuffer *mask, *input;
84   guint i, j;
85   guint8 *data;
86
87   myvideosrcpad =
88       gst_pad_new_from_static_template (&videosrctemplate, "videosrc");
89   gst_pad_set_active (myvideosrcpad, TRUE);
90   caps = gst_caps_from_string (SHAPEWIPE_VIDEO_CAPS_STRING);
91   gst_pad_set_caps (myvideosrcpad, caps);
92   gst_caps_unref (caps);
93
94   mymasksrcpad = gst_pad_new_from_static_template (&masksrctemplate, "masksrc");
95   gst_pad_set_active (mymasksrcpad, TRUE);
96   caps = gst_caps_from_string (SHAPEWIPE_MASK_CAPS_STRING);
97   gst_pad_set_caps (mymasksrcpad, caps);
98   gst_caps_unref (caps);
99
100   mysinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
101   gst_pad_set_chain_function (mysinkpad, on_chain);
102   gst_pad_set_active (mysinkpad, TRUE);
103   caps = gst_caps_from_string (SHAPEWIPE_VIDEO_CAPS_STRING);
104   gst_pad_set_caps (mysinkpad, caps);
105   gst_caps_unref (caps);
106
107   shapewipe = gst_element_factory_make ("shapewipe", NULL);
108   fail_unless (shapewipe != NULL);
109
110   p = gst_element_get_static_pad (shapewipe, "video_sink");
111   fail_unless (gst_pad_link (myvideosrcpad, p) == GST_PAD_LINK_OK);
112   gst_object_unref (p);
113   p = gst_element_get_static_pad (shapewipe, "mask_sink");
114   fail_unless (gst_pad_link (mymasksrcpad, p) == GST_PAD_LINK_OK);
115   gst_object_unref (p);
116   p = gst_element_get_static_pad (shapewipe, "src");
117   fail_unless (gst_pad_link (p, mysinkpad) == GST_PAD_LINK_OK);
118   gst_object_unref (p);
119
120   fail_unless (gst_element_set_state (shapewipe,
121           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
122
123   mask = gst_buffer_new_and_alloc (400 * 400);
124   caps = gst_caps_from_string (SHAPEWIPE_MASK_CAPS_STRING);
125   gst_buffer_set_caps (mask, caps);
126   gst_caps_unref (caps);
127   data = GST_BUFFER_DATA (mask);
128   for (i = 0; i < 400; i++) {
129     for (j = 0; j < 400; j++) {
130       if (i < 100 && j < 100)
131         data[0] = 0;
132       else if (i < 200 && j < 200)
133         data[0] = 85;
134       else if (i < 300 && j < 300)
135         data[0] = 170;
136       else
137         data[0] = 254;
138       data++;
139     }
140   }
141
142   fail_unless (gst_pad_push (mymasksrcpad, mask) == GST_FLOW_OK);
143
144   input = gst_buffer_new_and_alloc (400 * 400 * 4);
145   caps = gst_caps_from_string (SHAPEWIPE_VIDEO_CAPS_STRING);
146   gst_buffer_set_caps (input, caps);
147   gst_caps_unref (caps);
148   data = GST_BUFFER_DATA (input);
149   for (i = 0; i < 400; i++) {
150     for (j = 0; j < 400; j++) {
151       /* This is green */
152       data[0] = 255;            /* A */
153       data[1] = 173;            /* Y */
154       data[2] = 42;             /* U */
155       data[3] = 26;             /* V */
156       data += 4;
157     }
158   }
159
160   g_object_set (G_OBJECT (shapewipe), "position", 0.0, NULL);
161   output = NULL;
162   fail_unless (gst_pad_push (myvideosrcpad,
163           gst_buffer_ref (input)) == GST_FLOW_OK);
164   fail_unless (output != NULL);
165   data = GST_BUFFER_DATA (output);
166   for (i = 0; i < 400; i++) {
167     for (j = 0; j < 400; j++) {
168       fail_unless_equals_int (data[0], 255);    /* A */
169       fail_unless_equals_int (data[1], 173);    /* Y */
170       fail_unless_equals_int (data[2], 42);     /* U */
171       fail_unless_equals_int (data[3], 26);     /* V */
172       data += 4;
173     }
174   }
175   gst_buffer_unref (output);
176   output = NULL;
177
178   g_object_set (G_OBJECT (shapewipe), "position", 0.1, NULL);
179   output = NULL;
180   fail_unless (gst_pad_push (myvideosrcpad,
181           gst_buffer_ref (input)) == GST_FLOW_OK);
182   fail_unless (output != NULL);
183   data = GST_BUFFER_DATA (output);
184   for (i = 0; i < 400; i++) {
185     for (j = 0; j < 400; j++) {
186       if (i < 100 && j < 100) {
187         fail_unless_equals_int (data[0], 0);    /* A */
188         fail_unless_equals_int (data[1], 173);  /* Y */
189         fail_unless_equals_int (data[2], 42);   /* U */
190         fail_unless_equals_int (data[3], 26);   /* V */
191       } else {
192         fail_unless_equals_int (data[0], 255);  /* A */
193         fail_unless_equals_int (data[1], 173);  /* Y */
194         fail_unless_equals_int (data[2], 42);   /* U */
195         fail_unless_equals_int (data[3], 26);   /* V */
196       }
197       data += 4;
198     }
199   }
200   gst_buffer_unref (output);
201   output = NULL;
202
203   g_object_set (G_OBJECT (shapewipe), "position", 0.34, NULL);
204   output = NULL;
205   fail_unless (gst_pad_push (myvideosrcpad,
206           gst_buffer_ref (input)) == GST_FLOW_OK);
207   fail_unless (output != NULL);
208   data = GST_BUFFER_DATA (output);
209   for (i = 0; i < 400; i++) {
210     for (j = 0; j < 400; j++) {
211       if (i < 200 && j < 200) {
212         fail_unless_equals_int (data[0], 0);    /* A */
213         fail_unless_equals_int (data[1], 173);  /* Y */
214         fail_unless_equals_int (data[2], 42);   /* U */
215         fail_unless_equals_int (data[3], 26);   /* V */
216       } else {
217         fail_unless_equals_int (data[0], 255);  /* A */
218         fail_unless_equals_int (data[1], 173);  /* Y */
219         fail_unless_equals_int (data[2], 42);   /* U */
220         fail_unless_equals_int (data[3], 26);   /* V */
221       }
222       data += 4;
223     }
224   }
225   gst_buffer_unref (output);
226   output = NULL;
227
228   g_object_set (G_OBJECT (shapewipe), "position", 0.67, NULL);
229   output = NULL;
230   fail_unless (gst_pad_push (myvideosrcpad,
231           gst_buffer_ref (input)) == GST_FLOW_OK);
232   fail_unless (output != NULL);
233   data = GST_BUFFER_DATA (output);
234   for (i = 0; i < 400; i++) {
235     for (j = 0; j < 400; j++) {
236       if (i < 300 && j < 300) {
237         fail_unless_equals_int (data[0], 0);    /* A */
238         fail_unless_equals_int (data[1], 173);  /* Y */
239         fail_unless_equals_int (data[2], 42);   /* U */
240         fail_unless_equals_int (data[3], 26);   /* V */
241       } else {
242         fail_unless_equals_int (data[0], 255);  /* A */
243         fail_unless_equals_int (data[1], 173);  /* Y */
244         fail_unless_equals_int (data[2], 42);   /* U */
245         fail_unless_equals_int (data[3], 26);   /* V */
246       }
247       data += 4;
248     }
249   }
250   gst_buffer_unref (output);
251   output = NULL;
252
253   g_object_set (G_OBJECT (shapewipe), "position", 1.0, NULL);
254   output = NULL;
255   fail_unless (gst_pad_push (myvideosrcpad,
256           gst_buffer_ref (input)) == GST_FLOW_OK);
257   fail_unless (output != NULL);
258   data = GST_BUFFER_DATA (output);
259   for (i = 0; i < 400; i++) {
260     for (j = 0; j < 400; j++) {
261       fail_unless_equals_int (data[0], 0);      /* A */
262       fail_unless_equals_int (data[1], 173);    /* Y */
263       fail_unless_equals_int (data[2], 42);     /* U */
264       fail_unless_equals_int (data[3], 26);     /* V */
265       data += 4;
266     }
267   }
268   gst_buffer_unref (output);
269   output = NULL;
270
271   gst_buffer_unref (input);
272
273   fail_unless (gst_element_set_state (shapewipe,
274           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
275
276   p = gst_element_get_static_pad (shapewipe, "video_sink");
277   fail_unless (gst_pad_unlink (myvideosrcpad, p));
278   gst_object_unref (p);
279   p = gst_element_get_static_pad (shapewipe, "mask_sink");
280   fail_unless (gst_pad_unlink (mymasksrcpad, p));
281   gst_object_unref (p);
282   p = gst_element_get_static_pad (shapewipe, "src");
283   fail_unless (gst_pad_unlink (p, mysinkpad));
284   gst_object_unref (p);
285
286   gst_object_unref (myvideosrcpad);
287   gst_object_unref (mymasksrcpad);
288   gst_object_unref (mysinkpad);
289   gst_object_unref (shapewipe);
290 }
291
292 GST_END_TEST;
293
294 static Suite *
295 shapewipe_suite (void)
296 {
297   Suite *s = suite_create ("shapewipe");
298   TCase *tc_chain = tcase_create ("general");
299
300   suite_add_tcase (s, tc_chain);
301   tcase_set_timeout (tc_chain, 180);
302   tcase_add_test (tc_chain, test_general);
303
304   return s;
305 }
306
307 GST_CHECK_MAIN (shapewipe);