3 * unit test for videotestsrc
5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 # include <valgrind/valgrind.h>
34 #include <gst/check/gstcheck.h>
36 /* For ease of programming we use globals to keep refs for our floating
37 * src and sink pads we create; otherwise we always have to do get_pad,
38 * get_peer, and then remove references in every test function */
39 static GstPad *mysinkpad;
42 #define CAPS_TEMPLATE_STRING \
44 "format = (string) UYVY, " \
45 "width = (int) [ 1, MAX ], " \
46 "height = (int) [ 1, MAX ], " \
47 "framerate = (fraction) [ 0/1, MAX ]"
49 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS (CAPS_TEMPLATE_STRING)
56 setup_videotestsrc (void)
58 GstElement *videotestsrc;
60 GST_DEBUG ("setup_videotestsrc");
61 videotestsrc = gst_check_setup_element ("videotestsrc");
62 mysinkpad = gst_check_setup_sink_pad (videotestsrc, &sinktemplate);
63 gst_pad_set_active (mysinkpad, TRUE);
69 cleanup_videotestsrc (GstElement * videotestsrc)
71 GST_DEBUG ("cleanup_videotestsrc");
73 gst_check_drop_buffers ();
75 gst_pad_set_active (mysinkpad, FALSE);
76 gst_check_teardown_sink_pad (videotestsrc);
77 gst_check_teardown_element (videotestsrc);
80 GST_START_TEST (test_all_patterns)
82 GstElement *videotestsrc;
88 videotestsrc = setup_videotestsrc ();
89 oclass = G_OBJECT_GET_CLASS (videotestsrc);
90 property = g_object_class_find_property (oclass, "pattern");
91 fail_unless (G_IS_PARAM_SPEC_ENUM (property));
92 values = G_ENUM_CLASS (g_type_class_ref (property->value_type))->values;
94 while (values[j].value_name) {
95 GST_DEBUG_OBJECT (videotestsrc, "testing pattern %s", values[j].value_name);
97 g_object_set (videotestsrc, "pattern", j, NULL);
99 fail_unless (gst_element_set_state (videotestsrc,
100 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
101 "could not set to playing");
103 g_mutex_lock (check_mutex);
104 while (g_list_length (buffers) < 10) {
105 GST_DEBUG_OBJECT (videotestsrc, "Waiting for more buffers");
106 g_cond_wait (check_cond, check_mutex);
108 g_mutex_unlock (check_mutex);
110 gst_element_set_state (videotestsrc, GST_STATE_READY);
112 gst_check_drop_buffers ();
117 cleanup_videotestsrc (videotestsrc);
123 right_shift_colour (guint32 mask, guint32 pixel)
128 pixel = pixel & mask;
129 while ((mask & 0x01) == 0) {
138 fix_expected_colour (guint32 col_mask, guint8 col_expected)
141 gint last = g_bit_nth_msf (col_mask, -1);
142 gint first = g_bit_nth_lsf (col_mask, -1);
144 mask = 1 << (last - first + 1);
147 g_assert (col_expected == 0x00 || col_expected == 0xff);
149 /* this only works because we only check for all-bits-set or no-bits-set */
150 return col_expected & mask;
154 check_rgb_buf (const guint8 * pixels, guint32 r_mask, guint32 g_mask,
155 guint32 b_mask, guint32 a_mask, guint8 r_expected, guint8 g_expected,
156 guint8 b_expected, guint bpp, guint depth)
158 guint32 pixel, red, green, blue, alpha;
162 pixel = GST_READ_UINT32_BE (pixels);
165 pixel = (GST_READ_UINT8 (pixels) << 16) |
166 (GST_READ_UINT8 (pixels + 1) << 8) |
167 (GST_READ_UINT8 (pixels + 2) << 0);
170 if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
171 pixel = GST_READ_UINT16_LE (pixels);
173 pixel = GST_READ_UINT16_BE (pixels);
176 g_return_if_reached ();
179 red = right_shift_colour (r_mask, pixel);
180 green = right_shift_colour (g_mask, pixel);
181 blue = right_shift_colour (b_mask, pixel);
182 alpha = right_shift_colour (a_mask, pixel);
184 /* can't enable this by default, valgrind will complain about accessing
185 * uninitialised memory for the depth=24,bpp=32 formats ... */
186 /* GST_LOG ("pixels: 0x%02x 0x%02x 0x%02x 0x%02x => pixel = 0x%08x",
187 pixels[0], (guint) pixels[1], pixels[2], pixels[3], pixel); */
189 /* fix up the mask (for rgb15/16) */
191 r_expected = fix_expected_colour (r_mask, r_expected);
192 g_expected = fix_expected_colour (g_mask, g_expected);
193 b_expected = fix_expected_colour (b_mask, b_expected);
196 fail_unless (red == r_expected, "RED: expected 0x%02x, found 0x%02x",
198 fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x",
200 fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x",
203 fail_unless (a_mask == 0 || alpha != 0); /* better than nothing */
207 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
208 GstSample ** p_old_sample)
212 caps = gst_pad_get_current_caps (pad);
215 gst_sample_unref (*p_old_sample);
216 *p_old_sample = gst_sample_new (new_buf, caps, NULL, NULL);
218 gst_caps_unref (caps);
221 /* tests the positioning of pixels within the various RGB pixel layouts */
222 GST_START_TEST (test_rgb_formats)
226 const gchar *pattern_name;
231 } test_patterns[] = {
233 "white", 3, 0xff, 0xff, 0xff}, {
234 "red", 4, 0xff, 0x00, 0x00}, {
235 "green", 5, 0x00, 0xff, 0x00}, {
236 "blue", 6, 0x00, 0x00, 0xff}, {
237 "black", 2, 0x00, 0x00, 0x00}
243 guint32 red_mask, green_mask, blue_mask, alpha_mask;
246 "RGBA", 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff}, {
247 "ARGB", 32, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, {
248 "BGRA", 32, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff}, {
249 "ABGR", 32, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000}, {
250 "RGBx", 32, 24, 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000}, {
251 "xRGB", 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
252 "BGRx", 32, 24, 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000}, {
253 "xBGR", 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
254 "RGB", 24, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
255 "BGR", 24, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
256 "RGB16", 16, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000}, {
257 "RGB15", 16, 15, 0x00007c00, 0x000003e0, 0x0000001f, 0x0000000}
259 GstElement *pipeline, *src, *filter, *sink;
260 GstCaps *template_caps;
261 GstSample *sample = NULL;
265 /* test check function */
266 fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
268 pipeline = gst_pipeline_new ("pipeline");
269 src = gst_check_setup_element ("videotestsrc");
270 filter = gst_check_setup_element ("capsfilter");
271 sink = gst_check_setup_element ("fakesink");
273 gst_bin_add_many (GST_BIN (pipeline), src, filter, sink, NULL);
275 fail_unless (gst_element_link (src, filter));
276 fail_unless (gst_element_link (filter, sink));
278 srcpad = gst_element_get_static_pad (src, "src");
279 template_caps = gst_pad_get_pad_template_caps (srcpad);
281 g_object_set (sink, "signal-handoffs", TRUE, NULL);
282 g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &sample);
284 GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
286 for (i = 0; i < G_N_ELEMENTS (rgb_formats); ++i) {
287 for (e = 0; e < 2; ++e) {
290 caps = gst_caps_new_simple ("video/x-raw",
291 "format", G_TYPE_STRING, rgb_formats[i].nick,
292 "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
293 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
295 if (gst_caps_is_subset (caps, template_caps)) {
296 /* caps are supported, let's run some tests then ... */
297 for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
298 GstStateChangeReturn state_ret;
301 g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
303 GST_INFO ("%5s %u/%u %08x %08x %08x %08x, pattern=%s",
304 rgb_formats[i].nick, rgb_formats[i].bpp, rgb_formats[i].depth,
305 rgb_formats[i].red_mask, rgb_formats[i].green_mask,
306 rgb_formats[i].blue_mask, rgb_formats[i].alpha_mask,
307 test_patterns[p].pattern_name);
309 /* now get videotestsrc to produce a buffer with the given caps */
310 g_object_set (filter, "caps", caps, NULL);
312 state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
313 fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
314 "pipeline _set_state() to PAUSED failed");
315 state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
316 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
317 "pipeline failed going to PAUSED state");
319 state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
320 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
322 fail_unless (sample != NULL);
324 /* check buffer caps */
331 buf = gst_sample_get_buffer (sample);
332 fail_unless (buf != NULL);
333 caps = gst_sample_get_caps (sample);
334 fail_unless (caps != NULL);
336 s = gst_caps_get_structure (caps, 0);
337 format = gst_structure_get_string (s, "format");
338 fail_unless (g_str_equal (format, rgb_formats[i].nick));
340 /* now check the first pixel */
341 gst_buffer_map (buf, &map, GST_MAP_READ);
342 check_rgb_buf (map.data, rgb_formats[i].red_mask,
343 rgb_formats[i].green_mask, rgb_formats[i].blue_mask,
344 rgb_formats[i].alpha_mask, test_patterns[p].r_expected,
345 test_patterns[p].g_expected, test_patterns[p].b_expected,
346 rgb_formats[i].bpp, rgb_formats[i].depth);
347 gst_buffer_unmap (buf, &map);
349 gst_sample_unref (sample);
355 GST_INFO ("videotestsrc doesn't support format %" GST_PTR_FORMAT, caps);
358 gst_caps_unref (caps);
361 gst_caps_unref (template_caps);
362 gst_object_unref (srcpad);
364 gst_object_unref (pipeline);
370 /* FIXME: add tests for YUV formats */
373 videotestsrc_suite (void)
375 Suite *s = suite_create ("videotestsrc");
376 TCase *tc_chain = tcase_create ("general");
378 suite_add_tcase (s, tc_chain);
381 if (RUNNING_ON_VALGRIND) {
382 /* otherwise valgrind errors out when liboil probes CPU extensions
383 * during which it causes SIGILLs etc. to be fired */
384 g_setenv ("OIL_CPU_FLAGS", "0", 0);
385 /* test_rgb_formats takes a bit longer, so increase timeout */
386 tcase_set_timeout (tc_chain, 5 * 60);
390 tcase_add_test (tc_chain, test_all_patterns);
391 tcase_add_test (tc_chain, test_rgb_formats);
396 GST_CHECK_MAIN (videotestsrc);