3 * Common code for GStreamer unittests
5 * Copyright (C) 2004,2006 Thomas Vander Stichele <thomas at apestaart dot org>
6 * Copyright (C) 2008 Thijs Vermeir <thijsvermeir@gmail.com>
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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * @short_description: Common code for GStreamer unit tests
27 * These macros and functions are for internal use of the unit tests found
28 * inside the 'check' directories of various GStreamer packages.
30 * One notable feature is that one can use the environment variables GST_CHECKS
31 * and GST_CHECKS_IGNORE to select which tests to run or skip. Both variables
32 * can contain a comma separated list of test name globs (e.g. test_*).
40 GST_DEBUG_CATEGORY (check_debug);
42 /* logging function for tests
43 * a test uses g_message() to log a debug line
44 * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
48 gboolean _gst_check_threads_running = FALSE;
49 GList *thread_list = NULL;
51 GCond start_cond; /* used to notify main thread of thread startups */
52 GCond sync_cond; /* used to synchronize all threads and main thread */
54 GList *buffers = NULL;
58 /* FIXME 2.0: shouldn't _gst_check_debug be static? Not used anywhere */
59 gboolean _gst_check_debug = FALSE;
60 gboolean _gst_check_raised_critical = FALSE;
61 gboolean _gst_check_raised_warning = FALSE;
62 gboolean _gst_check_expecting_log = FALSE;
64 static void gst_check_log_message_func
65 (const gchar * log_domain, GLogLevelFlags log_level,
66 const gchar * message, gpointer user_data)
68 if (_gst_check_debug) {
69 g_print ("%s\n", message);
73 static void gst_check_log_critical_func
74 (const gchar * log_domain, GLogLevelFlags log_level,
75 const gchar * message, gpointer user_data)
77 if (!_gst_check_expecting_log) {
78 g_print ("\n\nUnexpected critical/warning: %s\n", message);
79 fail ("Unexpected critical/warning: %s", message);
82 if (_gst_check_debug) {
83 g_print ("\nExpected critical/warning: %s\n", message);
86 if (log_level & G_LOG_LEVEL_CRITICAL)
87 _gst_check_raised_critical = TRUE;
88 if (log_level & G_LOG_LEVEL_WARNING)
89 _gst_check_raised_warning = TRUE;
93 sort_plugins (GstPlugin * a, GstPlugin * b)
97 ret = strcmp (gst_plugin_get_source (a), gst_plugin_get_source (b));
99 ret = strcmp (gst_plugin_get_name (a), gst_plugin_get_name (b));
108 plugins = gst_registry_get_plugin_list (gst_registry_get ());
109 plugins = g_list_sort (plugins, (GCompareFunc) sort_plugins);
110 for (l = plugins; l != NULL; l = l->next) {
111 GstPlugin *plugin = GST_PLUGIN (l->data);
113 if (strcmp (gst_plugin_get_source (plugin), "BLACKLIST") != 0) {
114 GST_LOG ("%20s@%s", gst_plugin_get_name (plugin),
115 GST_STR_NULL (gst_plugin_get_filename (plugin)));
118 gst_plugin_list_free (plugins);
121 /* initialize GStreamer testing */
123 gst_check_init (int *argc, char **argv[])
125 guint timeout_multiplier = 1;
127 gst_init (argc, argv);
129 GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
131 if (atexit (gst_deinit) != 0) {
132 GST_ERROR ("failed to set gst_deinit as exit function");
135 if (g_getenv ("GST_TEST_DEBUG"))
136 _gst_check_debug = TRUE;
138 g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
140 g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
141 gst_check_log_critical_func, NULL);
142 g_log_set_handler ("GStreamer", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
143 gst_check_log_critical_func, NULL);
144 g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
145 gst_check_log_critical_func, NULL);
146 g_log_set_handler ("GLib-GIO", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
147 gst_check_log_critical_func, NULL);
148 g_log_set_handler ("GLib", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
149 gst_check_log_critical_func, NULL);
154 GST_INFO ("target CPU: %s", TARGET_CPU);
158 timeout_multiplier = 10;
161 if (timeout_multiplier > 1) {
162 const gchar *tmult = g_getenv ("CK_TIMEOUT_MULTIPLIER");
167 g_snprintf (num_str, sizeof (num_str), "%d", timeout_multiplier);
168 GST_INFO ("slow CPU, setting CK_TIMEOUT_MULTIPLIER to %s", num_str);
169 g_setenv ("CK_TIMEOUT_MULTIPLIER", num_str, TRUE);
171 GST_INFO ("CK_TIMEOUT_MULTIPLIER already set to '%s'", tmult);
176 /* message checking */
178 gst_check_message_error (GstMessage * message, GstMessageType type,
179 GQuark domain, gint code)
184 fail_unless (GST_MESSAGE_TYPE (message) == type,
185 "message is of type %s instead of expected type %s",
186 gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
187 gst_message_type_get_name (type));
188 gst_message_parse_error (message, &error, &debug);
189 fail_unless_equals_int (error->domain, domain);
190 fail_unless_equals_int (error->code, code);
191 g_error_free (error);
195 /* helper functions */
197 gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
199 GST_DEBUG_OBJECT (pad, "chain_func: received buffer %p", buffer);
200 buffers = g_list_append (buffers, buffer);
202 g_mutex_lock (&check_mutex);
203 g_cond_signal (&check_cond);
204 g_mutex_unlock (&check_mutex);
210 * gst_check_setup_element:
213 * setup an element for a filter test with mysrcpad and mysinkpad
215 * Returns: (transfer full): a new element
218 gst_check_setup_element (const gchar * factory)
222 GST_DEBUG ("setup_element");
224 element = gst_element_factory_make (factory, factory);
225 fail_if (element == NULL, "Could not create a '%s' element", factory);
226 ASSERT_OBJECT_REFCOUNT (element, factory, 1);
231 gst_check_teardown_element (GstElement * element)
233 GST_DEBUG ("teardown_element");
235 fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
236 GST_STATE_CHANGE_SUCCESS, "could not set to null");
237 ASSERT_OBJECT_REFCOUNT (element, "element", 1);
238 gst_object_unref (element);
242 * gst_check_setup_src_pad:
243 * @element: element to setup pad on
244 * @tmpl: pad template
246 * Does the same as #gst_check_setup_src_pad_by_name with the <emphasis> name </emphasis> parameter equal to "sink".
248 * Returns: (transfer full): A new pad that can be used to inject data on @element
251 gst_check_setup_src_pad (GstElement * element, GstStaticPadTemplate * tmpl)
253 return gst_check_setup_src_pad_by_name (element, tmpl, "sink");
257 * gst_check_setup_src_pad_by_name:
258 * @element: element to setup src pad on
259 * @tmpl: pad template
260 * @name: Name of the @element sink pad that will be linked to the src pad that will be setup
262 * Creates a new src pad (based on the given @tmpl) and links it to the given @element sink pad (the pad that matches the given @name).
263 * Before using the src pad to push data on @element you need to call #gst_check_setup_events on the created src pad.
265 * Example of how to push a buffer on @element:
267 * |[<!-- language="C" -->
268 * static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
271 * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
273 * static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
276 * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
279 * GstElement * element = gst_check_setup_element ("element");
280 * GstPad * mysrcpad = gst_check_setup_src_pad (element, &srctemplate);
281 * GstPad * mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
283 * gst_pad_set_active (mysrcpad, TRUE);
284 * gst_pad_set_active (mysinkpad, TRUE);
285 * fail_unless (gst_element_set_state (element, GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
287 * GstCaps * caps = gst_caps_from_string (YOUR_DESIRED_SINK_CAPS);
288 * gst_check_setup_events (mysrcpad, element, caps, GST_FORMAT_TIME);
289 * gst_caps_unref (caps);
291 * fail_unless (gst_pad_push (mysrcpad, gst_buffer_new_and_alloc(2)) == GST_FLOW_OK);
294 * For very simple input/output test scenarios checkout #gst_check_element_push_buffer_list and #gst_check_element_push_buffer.
296 * Returns: (transfer full): A new pad that can be used to inject data on @element
299 gst_check_setup_src_pad_by_name (GstElement * element,
300 GstStaticPadTemplate * tmpl, const gchar * name)
302 GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
305 srcpad = gst_check_setup_src_pad_by_name_from_template (element, ptmpl, name);
307 gst_object_unref (ptmpl);
313 * gst_check_setup_src_pad_from_template:
314 * @element: element to setup pad on
315 * @tmpl: pad template
317 * Returns: (transfer full): a new pad
322 gst_check_setup_src_pad_from_template (GstElement * element,
323 GstPadTemplate * tmpl)
325 return gst_check_setup_src_pad_by_name_from_template (element, tmpl, "sink");
329 * gst_check_setup_src_pad_by_name_from_template:
330 * @element: element to setup pad on
331 * @tmpl: pad template
334 * Returns: (transfer full): a new pad
339 gst_check_setup_src_pad_by_name_from_template (GstElement * element,
340 GstPadTemplate * tmpl, const gchar * name)
342 GstPad *srcpad, *sinkpad;
345 srcpad = gst_pad_new_from_template (tmpl, "src");
346 GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
347 fail_if (srcpad == NULL, "Could not create a srcpad");
348 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
350 sinkpad = gst_element_get_static_pad (element, name);
352 sinkpad = gst_element_get_request_pad (element, name);
353 fail_if (sinkpad == NULL, "Could not get sink pad from %s",
354 GST_ELEMENT_NAME (element));
355 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
356 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
357 "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
358 gst_object_unref (sinkpad); /* because we got it higher up */
359 ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
365 gst_check_teardown_pad_by_name (GstElement * element, const gchar * name)
367 GstPad *pad_peer, *pad_element;
369 /* clean up floating src pad */
370 pad_element = gst_element_get_static_pad (element, name);
371 /* We don't check the refcount here since there *might* be
372 * a pad cache holding an extra reference on pad_element.
373 * To get to a state where no pad cache will exist,
374 * we first unlink that pad. */
375 pad_peer = gst_pad_get_peer (pad_element);
378 if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
379 gst_pad_unlink (pad_peer, pad_element);
381 gst_pad_unlink (pad_element, pad_peer);
384 /* pad refs held by both creator and this function (through _get) */
385 ASSERT_OBJECT_REFCOUNT (pad_element, "element pad_element", 2);
386 gst_object_unref (pad_element);
387 /* one more ref is held by element itself */
390 /* pad refs held by both creator and this function (through _get_peer) */
391 ASSERT_OBJECT_REFCOUNT (pad_peer, "check pad_peer", 2);
392 gst_object_unref (pad_peer);
393 gst_object_unref (pad_peer);
398 gst_check_teardown_src_pad (GstElement * element)
400 gst_check_teardown_pad_by_name (element, "sink");
404 * gst_check_setup_sink_pad:
405 * @element: element to setup pad on
406 * @tmpl: pad template
408 * Does the same as #gst_check_setup_sink_pad_by_name with the <emphasis> name </emphasis> parameter equal to "src".
410 * Returns: (transfer full): a new pad that can be used to check the output of @element
413 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * tmpl)
415 return gst_check_setup_sink_pad_by_name (element, tmpl, "src");
419 * gst_check_setup_sink_pad_by_name:
420 * @element: element to setup pad on
421 * @tmpl: pad template
422 * @name: Name of the @element src pad that will be linked to the sink pad that will be setup
424 * Creates a new sink pad (based on the given @tmpl) and links it to the given @element src pad
425 * (the pad that matches the given @name).
426 * You can set event/chain/query functions on this pad to check the output of the @element.
428 * Returns: (transfer full): a new pad that can be used to check the output of @element
431 gst_check_setup_sink_pad_by_name (GstElement * element,
432 GstStaticPadTemplate * tmpl, const gchar * name)
434 GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
438 gst_check_setup_sink_pad_by_name_from_template (element, ptmpl, name);
440 gst_object_unref (ptmpl);
446 * gst_check_setup_sink_pad_from_template:
447 * @element: element to setup pad on
448 * @tmpl: pad template
450 * Returns: (transfer full): a new pad
455 gst_check_setup_sink_pad_from_template (GstElement * element,
456 GstPadTemplate * tmpl)
458 return gst_check_setup_sink_pad_by_name_from_template (element, tmpl, "src");
462 * gst_check_setup_sink_pad_by_name_from_template:
463 * @element: element to setup pad on
464 * @tmpl: pad template
467 * Returns: (transfer full): a new pad
472 gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
473 GstPadTemplate * tmpl, const gchar * name)
475 GstPad *srcpad, *sinkpad;
478 sinkpad = gst_pad_new_from_template (tmpl, "sink");
479 GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
480 fail_if (sinkpad == NULL, "Could not create a sinkpad");
482 srcpad = gst_element_get_static_pad (element, name);
484 srcpad = gst_element_get_request_pad (element, name);
485 fail_if (srcpad == NULL, "Could not get source pad from %s",
486 GST_ELEMENT_NAME (element));
487 gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
489 GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
490 fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
491 "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
492 gst_object_unref (srcpad); /* because we got it higher up */
493 ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
495 GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
500 gst_check_teardown_sink_pad (GstElement * element)
502 gst_check_teardown_pad_by_name (element, "src");
506 * gst_check_drop_buffers:
508 * Unref and remove all buffers that are in the global @buffers GList,
512 gst_check_drop_buffers (void)
514 while (buffers != NULL) {
515 gst_buffer_unref (GST_BUFFER (buffers->data));
516 buffers = g_list_delete_link (buffers, buffers);
521 * gst_check_caps_equal:
522 * @caps1: first caps to compare
523 * @caps2: second caps to compare
525 * Compare two caps with gst_caps_is_equal and fail unless they are
529 gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
531 gchar *name1 = gst_caps_to_string (caps1);
532 gchar *name2 = gst_caps_to_string (caps2);
534 fail_unless (gst_caps_is_equal (caps1, caps2),
535 "caps ('%s') is not equal to caps ('%s')", name1, name2);
542 * gst_check_buffer_data:
543 * @buffer: buffer to compare
544 * @data: data to compare to
545 * @size: size of data to compare
547 * Compare the buffer contents with @data and @size.
550 gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size)
554 gst_buffer_map (buffer, &info, GST_MAP_READ);
555 GST_MEMDUMP ("Converted data", info.data, info.size);
556 GST_MEMDUMP ("Expected data", data, size);
557 if (memcmp (info.data, data, size) != 0) {
558 g_print ("\nConverted data:\n");
559 gst_util_dump_mem (info.data, info.size);
560 g_print ("\nExpected data:\n");
561 gst_util_dump_mem (data, size);
563 fail_unless (memcmp (info.data, data, size) == 0,
564 "buffer contents not equal");
565 gst_buffer_unmap (buffer, &info);
569 buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
571 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
573 GstCaps *expected_caps = gst_pad_get_element_private (pad);
575 gst_event_parse_caps (event, &event_caps);
576 fail_unless (gst_caps_is_fixed (expected_caps));
577 fail_unless (gst_caps_is_fixed (event_caps));
578 fail_unless (gst_caps_is_equal_fixed (event_caps, expected_caps));
579 gst_event_unref (event);
583 return gst_pad_event_default (pad, noparent, event);
587 * gst_check_element_push_buffer_list:
588 * @element_name: name of the element that needs to be created
589 * @buffer_in: (element-type GstBuffer) (transfer full): a list of buffers that needs to be
590 * pushed to the element
591 * @caps_in: the #GstCaps expected of the sinkpad of the element
592 * @buffer_out: (element-type GstBuffer) (transfer full): a list of buffers that we expect from
594 * @caps_out: the #GstCaps expected of the srcpad of the element
595 * @last_flow_return: the last buffer push needs to give this GstFlowReturn
597 * Create an element using the factory providing the @element_name and push the
598 * buffers in @buffer_in to this element. The element should create the buffers
599 * equal to the buffers in @buffer_out. We only check the size and the data of
600 * the buffers. This function unrefs the buffers in the two lists.
601 * The last_flow_return parameter indicates the expected flow return value from
602 * pushing the final buffer in the list.
603 * This can be used to set up a test which pushes some buffers and then an
604 * invalid buffer, when the final buffer is expected to fail, for example.
606 /* FIXME 2.0: rename this function now that there's GstBufferList? */
608 gst_check_element_push_buffer_list (const gchar * element_name,
609 GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
610 GstCaps * caps_out, GstFlowReturn last_flow_return)
614 GstPad *sink_pad = NULL;
618 /* check that there are no buffers waiting */
619 gst_check_drop_buffers ();
620 /* create the element */
621 element = gst_check_setup_element (element_name);
622 fail_if (element == NULL, "failed to create the element '%s'", element_name);
623 fail_unless (GST_IS_ELEMENT (element), "the element is no element");
624 /* create the src pad */
625 buffer = GST_BUFFER (buffer_in->data);
627 fail_unless (GST_IS_BUFFER (buffer), "There should be a buffer in buffer_in");
628 src_pad = gst_pad_new ("src", GST_PAD_SRC);
630 fail_unless (gst_caps_is_fixed (caps_in));
631 gst_pad_use_fixed_caps (src_pad);
633 /* activate the pad */
634 gst_pad_set_active (src_pad, TRUE);
635 GST_DEBUG ("src pad activated");
636 gst_check_setup_events (src_pad, element, caps_in, GST_FORMAT_BYTES);
637 pad_peer = gst_element_get_static_pad (element, "sink");
638 fail_if (pad_peer == NULL);
639 fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
640 "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
641 gst_object_unref (pad_peer);
642 /* don't create the sink_pad if there is no buffer_out list */
643 if (buffer_out != NULL) {
645 GST_DEBUG ("buffer out detected, creating the sink pad");
646 /* get the sink caps */
650 fail_unless (gst_caps_is_fixed (caps_out));
651 temp = gst_caps_to_string (caps_out);
653 GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
657 /* get the sink pad */
658 sink_pad = gst_pad_new ("sink", GST_PAD_SINK);
659 fail_unless (GST_IS_PAD (sink_pad));
660 /* configure the sink pad */
661 gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
662 gst_pad_set_active (sink_pad, TRUE);
664 gst_pad_set_element_private (sink_pad, caps_out);
665 gst_pad_set_event_function (sink_pad, buffer_event_function);
667 /* get the peer pad */
668 pad_peer = gst_element_get_static_pad (element, "src");
669 fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
670 "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
671 gst_object_unref (pad_peer);
673 fail_unless (gst_element_set_state (element,
674 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
675 "could not set to playing");
676 /* push all the buffers in the buffer_in list */
677 fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
678 while (buffer_in != NULL) {
679 GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);
681 fail_unless (GST_IS_BUFFER (next_buffer),
682 "data in buffer_in should be a buffer");
683 /* remove the buffer from the list */
684 buffer_in = g_list_remove (buffer_in, next_buffer);
685 if (buffer_in == NULL) {
686 fail_unless (gst_pad_push (src_pad, next_buffer) == last_flow_return,
687 "we expect something else from the last buffer");
689 fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
690 "Failed to push buffer in");
693 fail_unless (gst_element_set_state (element,
694 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
695 /* check that there is a buffer out */
696 fail_unless_equals_int (g_list_length (buffers), g_list_length (buffer_out));
697 while (buffers != NULL) {
698 GstBuffer *new = GST_BUFFER (buffers->data);
699 GstBuffer *orig = GST_BUFFER (buffer_out->data);
700 GstMapInfo newinfo, originfo;
702 gst_buffer_map (new, &newinfo, GST_MAP_READ);
703 gst_buffer_map (orig, &originfo, GST_MAP_READ);
705 GST_LOG ("orig buffer: size %" G_GSIZE_FORMAT, originfo.size);
706 GST_LOG ("new buffer: size %" G_GSIZE_FORMAT, newinfo.size);
707 GST_MEMDUMP ("orig buffer", originfo.data, originfo.size);
708 GST_MEMDUMP ("new buffer", newinfo.data, newinfo.size);
710 /* remove the buffers */
711 buffers = g_list_remove (buffers, new);
712 buffer_out = g_list_remove (buffer_out, orig);
714 fail_unless (originfo.size == newinfo.size,
715 "size of the buffers are not the same");
716 fail_unless (memcmp (originfo.data, newinfo.data, newinfo.size) == 0,
717 "data is not the same");
719 gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
722 gst_buffer_unmap (orig, &originfo);
723 gst_buffer_unmap (new, &newinfo);
725 gst_buffer_unref (new);
726 gst_buffer_unref (orig);
728 /* teardown the element and pads */
729 gst_pad_set_active (src_pad, FALSE);
730 gst_check_teardown_src_pad (element);
731 gst_pad_set_active (sink_pad, FALSE);
732 gst_check_teardown_sink_pad (element);
733 gst_check_teardown_element (element);
737 * gst_check_element_push_buffer:
738 * @element_name: name of the element that needs to be created
739 * @buffer_in: push this buffer to the element
740 * @caps_in: the #GstCaps expected of the sinkpad of the element
741 * @buffer_out: compare the result with this buffer
742 * @caps_out: the #GstCaps expected of the srcpad of the element
744 * Create an element using the factory providing the @element_name and
745 * push the @buffer_in to this element. The element should create one buffer
746 * and this will be compared with @buffer_out. We only check the caps
747 * and the data of the buffers. This function unrefs the buffers.
750 gst_check_element_push_buffer (const gchar * element_name,
751 GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
757 in = g_list_append (in, buffer_in);
758 out = g_list_append (out, buffer_out);
760 gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
765 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
767 if (have_abi_sizes) {
771 for (i = 0; list[i].name; i++) {
772 if (list[i].size != list[i].abi_size) {
774 g_print ("sizeof(%s) is %d, expected %d\n",
775 list[i].name, list[i].size, list[i].abi_size);
778 fail_unless (ok, "failed ABI check");
782 if ((fn = g_getenv ("GST_ABI"))) {
787 s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
788 for (i = 0; list[i].name; i++) {
789 g_string_append_printf (s, " {\"%s\", sizeof (%s), %d},\n",
790 list[i].name, list[i].name, list[i].size);
792 g_string_append (s, " {NULL, 0, 0}\n");
793 g_string_append (s, "};\n");
794 if (!g_file_set_contents (fn, s->str, s->len, &err)) {
795 g_print ("%s", s->str);
796 g_printerr ("\nFailed to write ABI information: %s\n", err->message);
797 g_clear_error (&err);
799 g_print ("\nWrote ABI information to '%s'.\n", fn);
801 g_string_free (s, TRUE);
803 g_print ("No structure size list was generated for this architecture.\n");
804 g_print ("Run with GST_ABI environment variable set to output header.\n");
810 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
813 gchar *xmlfilename = NULL;
816 sr = srunner_create (suite);
818 if (g_getenv ("GST_CHECK_XML")) {
819 /* how lucky we are to have __FILE__ end in .c */
820 xmlfilename = g_strdup_printf ("%sheck.xml", fname);
822 srunner_set_xml (sr, xmlfilename);
825 srunner_run_all (sr, CK_NORMAL);
826 nf = srunner_ntests_failed (sr);
827 g_free (xmlfilename);
833 gst_check_have_checks_list (const gchar * env_var_name)
835 const gchar *env_val;
837 env_val = g_getenv (env_var_name);
838 return (env_val != NULL && *env_val != '\0');
842 gst_check_func_is_in_list (const gchar * env_var, const gchar * func_name)
844 const gchar *gst_checks;
845 gboolean res = FALSE;
848 gst_checks = g_getenv (env_var);
850 if (gst_checks == NULL || *gst_checks == '\0')
853 /* only run specified functions */
854 funcs = g_strsplit (gst_checks, ",", -1);
855 for (f = funcs; f != NULL && *f != NULL; ++f) {
856 if (g_pattern_match_simple (*f, func_name)) {
866 _gst_check_run_test_func (const gchar * func_name)
868 /* if we have a whitelist, run it only if it's in the whitelist */
869 if (gst_check_have_checks_list ("GST_CHECKS"))
870 return gst_check_func_is_in_list ("GST_CHECKS", func_name);
872 /* if we have a blacklist, run it only if it's not in the blacklist */
873 if (gst_check_have_checks_list ("GST_CHECKS_IGNORE"))
874 return !gst_check_func_is_in_list ("GST_CHECKS_IGNORE", func_name);
876 /* no filter specified => run all checks */
881 * gst_check_setup_events_with_stream_id:
882 * @srcpad: The src #GstPad to push on
883 * @element: The #GstElement use to create the stream id
884 * @caps: (allow-none): #GstCaps in case caps event must be sent
885 * @format: The #GstFormat of the default segment to send
886 * @stream_id: A unique identifier for the stream
888 * Push stream-start, caps and segment event, which consist of the minimum
889 * required events to allow streaming. Caps is optional to allow raw src
893 gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
894 GstCaps * caps, GstFormat format, const gchar * stream_id)
898 gst_segment_init (&segment, format);
900 fail_unless (gst_pad_push_event (srcpad,
901 gst_event_new_stream_start (stream_id)));
903 fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
904 fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&segment)));
908 * gst_check_setup_events:
909 * @srcpad: The src #GstPad to push on
910 * @element: The #GstElement use to create the stream id
911 * @caps: (allow-none): #GstCaps in case caps event must be sent
912 * @format: The #GstFormat of the default segment to send
914 * Push stream-start, caps and segment event, which consist of the minimum
915 * required events to allow streaming. Caps is optional to allow raw src
916 * testing. If @element has more than one src or sink pad, use
917 * gst_check_setup_events_with_stream_id() instead.
920 gst_check_setup_events (GstPad * srcpad, GstElement * element,
921 GstCaps * caps, GstFormat format)
925 stream_id = gst_pad_create_stream_id (srcpad, element, NULL);
926 gst_check_setup_events_with_stream_id (srcpad, element, caps, format,
931 typedef struct _DestroyedObjectStruct
935 } DestroyedObjectStruct;
938 weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
940 destroyed->destroyed = TRUE;
944 * gst_check_objects_destroyed_on_unref:
945 * @object_to_unref: The #GObject to unref
946 * @first_object: (allow-none): The first object that should be destroyed as a
947 * concequence of unrefing @object_to_unref.
948 * @... : Additional object that should have been destroyed.
950 * Unrefs @object_to_unref and checks that is has properly been
951 * destroyed, also checks that the other objects passed in
952 * parametter have been destroyed as a concequence of
953 * unrefing @object_to_unref. Last variable argument should be NULL.
958 gst_check_objects_destroyed_on_unref (gpointer object_to_unref,
959 gpointer first_object, ...)
962 GList *objs = NULL, *tmp;
963 DestroyedObjectStruct *destroyed = g_slice_new0 (DestroyedObjectStruct);
965 destroyed->object = object_to_unref;
966 g_object_weak_ref (object_to_unref, (GWeakNotify) weak_notify, destroyed);
967 objs = g_list_prepend (objs, destroyed);
972 object = first_object;
974 va_start (varargs, first_object);
976 destroyed = g_slice_new0 (DestroyedObjectStruct);
977 destroyed->object = object;
978 g_object_weak_ref (object, (GWeakNotify) weak_notify, destroyed);
979 objs = g_list_prepend (objs, destroyed);
980 object = va_arg (varargs, GObject *);
984 gst_object_unref (object_to_unref);
986 for (tmp = objs; tmp; tmp = tmp->next) {
987 DestroyedObjectStruct *destroyed = tmp->data;
989 if (!destroyed->destroyed) {
990 fail_unless (destroyed->destroyed,
991 "%s_%p is not destroyed, %d refcounts left!",
992 GST_IS_OBJECT (destroyed->
993 object) ? GST_OBJECT_NAME (destroyed->object) :
994 G_OBJECT_TYPE_NAME (destroyed), destroyed->object,
995 destroyed->object->ref_count);
997 g_slice_free (DestroyedObjectStruct, tmp->data);
1003 * gst_check_object_destroyed_on_unref:
1004 * @object_to_unref: The #GObject to unref
1006 * Unrefs @object_to_unref and checks that is has properly been
1012 gst_check_object_destroyed_on_unref (gpointer object_to_unref)
1014 gst_check_objects_destroyed_on_unref (object_to_unref, NULL, NULL);
1017 /* For ABI compatibility with GStreamer < 1.5 */
1019 _fail_unless (int result, const char *file, int line, const char *expr, ...);
1022 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1028 _mark_point (file, line);
1032 va_start (args, expr);
1033 msg = g_strdup_vprintf (expr, args);
1036 _ck_assert_failed (file, line, msg, NULL);