• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit test for videofilter elements
4  *
5  * Copyright (C) <2006> Mark Nauwelaerts <manauw@skynet.be>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include <stdarg.h>
24 
25 #include <gst/video/video.h>
26 #include <gst/check/gstcheck.h>
27 #include <gst/check/gstharness.h>
28 
29 static GstBuffer *
create_test_video_buffer_rgba8(GstVideoInfo * info)30 create_test_video_buffer_rgba8 (GstVideoInfo * info)
31 {
32   guint8 *data;
33   guint i = 0, j, k;
34   gsize stride = GST_VIDEO_INFO_PLANE_STRIDE (info, i);
35 
36   data = g_malloc0 (info->size);
37 
38   for (j = 0; j < GST_VIDEO_INFO_COMP_HEIGHT (info, i); j++) {
39     for (k = 0; k < GST_VIDEO_INFO_COMP_WIDTH (info, i); k++) {
40       data[(j * stride + 4 * k) + 0] = j % 255;
41       data[(j * stride + 4 * k) + 1] = k % 255;
42       data[(j * stride + 4 * k) + 2] = (j + k) % 255;
43       data[(j * stride + 4 * k) + 3] = 255;
44     }
45   }
46 
47   return gst_buffer_new_wrapped (data, info->size);
48 }
49 
GST_START_TEST(test_passthrough)50 GST_START_TEST (test_passthrough)
51 {
52   GstHarness *flip = gst_harness_new ("videoflip");
53   GstVideoInfo in_info, out_info;
54   GstCaps *in_caps, *out_caps;
55   GstEvent *e;
56   GstBuffer *buf;
57 
58   gst_video_info_set_format (&in_info, GST_VIDEO_FORMAT_RGBA, 4, 9);
59   in_caps = gst_video_info_to_caps (&in_info);
60 
61   gst_harness_set_src_caps (flip, in_caps);
62 
63   e = gst_harness_pull_event (flip);
64   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_STREAM_START);
65   gst_event_unref (e);
66   e = gst_harness_pull_event (flip);
67   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_CAPS);
68   gst_event_parse_caps (e, &out_caps);
69   fail_unless (gst_video_info_from_caps (&out_info, out_caps));
70   fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&in_info),
71       GST_VIDEO_INFO_WIDTH (&out_info));
72   fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&in_info),
73       GST_VIDEO_INFO_HEIGHT (&out_info));
74   gst_event_unref (e);
75 
76   e = gst_harness_pull_event (flip);
77   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_SEGMENT);
78   gst_event_unref (e);
79 
80   buf = create_test_video_buffer_rgba8 (&in_info);
81   buf = gst_harness_push_and_pull (flip, buf);
82   fail_unless (buf != NULL);
83   gst_buffer_unref (buf);
84 
85   gst_harness_teardown (flip);
86 }
87 
88 GST_END_TEST;
89 
GST_START_TEST(test_change_method)90 GST_START_TEST (test_change_method)
91 {
92   GstHarness *flip = gst_harness_new ("videoflip");
93   GstVideoInfo in_info, out_info;
94   GstCaps *in_caps, *out_caps;
95   GstEvent *e;
96   GstBuffer *buf;
97 
98   gst_video_info_set_format (&in_info, GST_VIDEO_FORMAT_RGBA, 4, 9);
99   in_caps = gst_video_info_to_caps (&in_info);
100 
101   gst_harness_set_src_caps (flip, in_caps);
102 
103   e = gst_harness_pull_event (flip);
104   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_STREAM_START);
105   gst_event_unref (e);
106   e = gst_harness_pull_event (flip);
107   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_CAPS);
108   gst_event_parse_caps (e, &out_caps);
109   fail_unless (gst_video_info_from_caps (&out_info, out_caps));
110   fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&in_info),
111       GST_VIDEO_INFO_WIDTH (&out_info));
112   fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&in_info),
113       GST_VIDEO_INFO_HEIGHT (&out_info));
114   gst_event_unref (e);
115 
116   e = gst_harness_pull_event (flip);
117   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_SEGMENT);
118   gst_event_unref (e);
119 
120   buf = create_test_video_buffer_rgba8 (&in_info);
121   buf = gst_harness_push_and_pull (flip, buf);
122   fail_unless (buf != NULL);
123   gst_buffer_unref (buf);
124 
125   g_object_set (flip->element, "video-direction", 1 /* 90r */ , NULL);
126 
127   buf = create_test_video_buffer_rgba8 (&in_info);
128   fail_unless_equals_int (gst_harness_push (flip, buf), GST_FLOW_OK);
129   e = gst_harness_pull_event (flip);
130   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_CAPS);
131   gst_event_parse_caps (e, &out_caps);
132   fail_unless (gst_video_info_from_caps (&out_info, out_caps));
133   fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&in_info),
134       GST_VIDEO_INFO_HEIGHT (&out_info));
135   fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&in_info),
136       GST_VIDEO_INFO_WIDTH (&out_info));
137   gst_event_unref (e);
138   buf = gst_harness_pull (flip);
139   fail_unless (buf != NULL);
140   gst_buffer_unref (buf);
141 
142   gst_harness_teardown (flip);
143 }
144 
145 GST_END_TEST;
146 
GST_START_TEST(test_change_method_twice_same_caps_different_method)147 GST_START_TEST (test_change_method_twice_same_caps_different_method)
148 {
149   GstHarness *flip = gst_harness_new ("videoflip");
150   GstVideoInfo in_info, out_info;
151   GstCaps *in_caps, *out_caps;
152   GstEvent *e;
153   GstBuffer *input, *output, *buf;
154   GstMapInfo in_map_info, out_map_info;
155 
156   gst_video_info_set_format (&in_info, GST_VIDEO_FORMAT_RGBA, 4, 9);
157   in_caps = gst_video_info_to_caps (&in_info);
158 
159   gst_harness_set_src_caps (flip, in_caps);
160 
161   e = gst_harness_pull_event (flip);
162   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_STREAM_START);
163   gst_event_unref (e);
164   e = gst_harness_pull_event (flip);
165   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_CAPS);
166   gst_event_parse_caps (e, &out_caps);
167   fail_unless (gst_video_info_from_caps (&out_info, out_caps));
168   fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&in_info),
169       GST_VIDEO_INFO_WIDTH (&out_info));
170   fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&in_info),
171       GST_VIDEO_INFO_HEIGHT (&out_info));
172   gst_event_unref (e);
173 
174   e = gst_harness_pull_event (flip);
175   fail_unless_equals_int (GST_EVENT_TYPE (e), GST_EVENT_SEGMENT);
176   gst_event_unref (e);
177 
178   buf = create_test_video_buffer_rgba8 (&in_info);
179   buf = gst_harness_push_and_pull (flip, buf);
180   fail_unless (buf != NULL);
181   gst_buffer_unref (buf);
182 
183   g_object_set (flip->element, "video-direction", 1 /* 90r */ , NULL);
184   g_object_set (flip->element, "video-direction", 2 /* 180 */ , NULL);
185 
186   input = create_test_video_buffer_rgba8 (&in_info);
187   fail_unless_equals_int (gst_harness_push (flip, gst_buffer_ref (input)),
188       GST_FLOW_OK);
189   /* caps will not change and basetransform won't send updated ones so we
190    * can't check for them */
191   output = gst_harness_pull (flip);
192   fail_unless (output != NULL);
193 
194   fail_unless (gst_buffer_map (input, &in_map_info, GST_MAP_READ));
195   fail_unless (gst_buffer_map (output, &out_map_info, GST_MAP_READ));
196 
197   {
198     gsize top_right = (GST_VIDEO_INFO_WIDTH (&in_info) - 1) * 4;
199     gsize bottom_left =
200         (GST_VIDEO_INFO_HEIGHT (&out_info) -
201         1) * GST_VIDEO_INFO_PLANE_STRIDE (&out_info, 0);
202 
203     fail_unless_equals_int (in_map_info.data[top_right + 0],
204         out_map_info.data[bottom_left + 0]);
205     fail_unless_equals_int (in_map_info.data[top_right + 1],
206         out_map_info.data[bottom_left + 1]);
207     fail_unless_equals_int (in_map_info.data[top_right + 2],
208         out_map_info.data[bottom_left + 2]);
209     fail_unless_equals_int (in_map_info.data[top_right + 3],
210         out_map_info.data[bottom_left + 3]);
211   }
212 
213   gst_buffer_unmap (input, &in_map_info);
214   gst_buffer_unmap (output, &out_map_info);
215 
216   gst_buffer_unref (input);
217   gst_buffer_unref (output);
218 
219   gst_harness_teardown (flip);
220 }
221 
222 GST_END_TEST;
GST_START_TEST(test_stress_change_method)223 GST_START_TEST (test_stress_change_method)
224 {
225   GstHarness *flip = gst_harness_new ("videoflip");
226   GParamSpec *pspec =
227       g_object_class_find_property (G_OBJECT_GET_CLASS (flip->element),
228       "video-direction");
229   GstHarnessThread *thread_identity, *thread_90r;
230   GValue direction_identity = G_VALUE_INIT, direction_90r = G_VALUE_INIT;
231   GstVideoInfo in_info;
232   guint i = 0;
233 #define N_PUSHES 1000
234 
235   gst_video_info_set_format (&in_info, GST_VIDEO_FORMAT_RGBA, 4, 9);
236   gst_harness_set_src_caps (flip, gst_video_info_to_caps (&in_info));
237 
238   g_value_init (&direction_identity, pspec->value_type);
239   g_value_init (&direction_90r, pspec->value_type);
240 
241   fail_unless (gst_value_deserialize_with_pspec (&direction_identity,
242           "identity", pspec));
243   fail_unless (gst_value_deserialize_with_pspec (&direction_90r, "90r", pspec));
244 
245   thread_identity =
246       gst_harness_stress_property_start_full (flip, "video-direction",
247       &direction_identity, 210);
248   thread_90r =
249       gst_harness_stress_property_start_full (flip, "video-direction",
250       &direction_90r, 160);
251 
252   while (i++ < N_PUSHES) {
253     GstBuffer *buf = create_test_video_buffer_rgba8 (&in_info);
254     buf = gst_harness_push_and_pull (flip, buf);
255     fail_unless (buf != NULL);
256     gst_buffer_unref (buf);
257     g_usleep (100);
258   }
259 
260   gst_harness_stress_thread_stop (thread_identity);
261   gst_harness_stress_thread_stop (thread_90r);
262 
263   g_value_unset (&direction_identity);
264   g_value_unset (&direction_90r);
265 
266   gst_harness_teardown (flip);
267 }
268 
269 GST_END_TEST;
270 
271 static Suite *
videoflip_suite(void)272 videoflip_suite (void)
273 {
274   Suite *s = suite_create ("videoflip");
275   TCase *tc_chain = tcase_create ("general");
276 
277   suite_add_tcase (s, tc_chain);
278   tcase_add_test (tc_chain, test_passthrough);
279   tcase_add_test (tc_chain, test_change_method);
280   tcase_add_test (tc_chain,
281       test_change_method_twice_same_caps_different_method);
282   tcase_add_test (tc_chain, test_stress_change_method);
283 
284   return s;
285 }
286 
287 GST_CHECK_MAIN (videoflip);
288