check: don't memcmp twice
[platform/upstream/gstreamer.git] / libs / gst / check / gstcheck.c
1 /* GStreamer
2  *
3  * Common code for GStreamer unittests
4  *
5  * Copyright (C) 2004,2006 Thomas Vander Stichele <thomas at apestaart dot org>
6  * Copyright (C) 2008 Thijs Vermeir <thijsvermeir@gmail.com>
7  *
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.
12  *
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.
17  *
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.
22  */
23 /**
24  * SECTION:gstcheck
25  * @short_description: Common code for GStreamer unit tests
26  *
27  * These macros and functions are for internal use of the unit tests found
28  * inside the 'check' directories of various GStreamer packages.
29  *
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_*).
33  */
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include "gstcheck.h"
39
40 GST_DEBUG_CATEGORY (check_debug);
41
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
45  * messages
46  */
47
48 gboolean _gst_check_threads_running = FALSE;
49 GList *thread_list = NULL;
50 GMutex mutex;
51 GCond start_cond;               /* used to notify main thread of thread startups */
52 GCond sync_cond;                /* used to synchronize all threads and main thread */
53
54 GList *buffers = NULL;
55 GMutex check_mutex;
56 GCond check_cond;
57
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;
63
64 static void gst_check_log_message_func
65     (const gchar * log_domain, GLogLevelFlags log_level,
66     const gchar * message, gpointer user_data)
67 {
68   if (_gst_check_debug) {
69     g_print ("%s\n", message);
70   }
71 }
72
73 static void gst_check_log_critical_func
74     (const gchar * log_domain, GLogLevelFlags log_level,
75     const gchar * message, gpointer user_data)
76 {
77   if (!_gst_check_expecting_log) {
78     g_print ("\n\nUnexpected critical/warning: %s\n", message);
79     fail ("Unexpected critical/warning: %s", message);
80   }
81
82   if (_gst_check_debug) {
83     g_print ("\nExpected critical/warning: %s\n", message);
84   }
85
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;
90 }
91
92 static gint
93 sort_plugins (GstPlugin * a, GstPlugin * b)
94 {
95   int ret;
96
97   ret = strcmp (gst_plugin_get_source (a), gst_plugin_get_source (b));
98   if (ret == 0)
99     ret = strcmp (gst_plugin_get_name (a), gst_plugin_get_name (b));
100   return ret;
101 }
102
103 static void
104 print_plugins (void)
105 {
106   GList *plugins, *l;
107
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);
112
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)));
116     }
117   }
118   gst_plugin_list_free (plugins);
119 }
120
121 /* initialize GStreamer testing */
122 void
123 gst_check_init (int *argc, char **argv[])
124 {
125   guint timeout_multiplier = 1;
126
127   gst_init (argc, argv);
128
129   GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
130
131   if (atexit (gst_deinit) != 0) {
132     GST_ERROR ("failed to set gst_deinit as exit function");
133   }
134
135   if (g_getenv ("GST_TEST_DEBUG"))
136     _gst_check_debug = TRUE;
137
138   g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
139       NULL);
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);
150
151   print_plugins ();
152
153 #ifdef TARGET_CPU
154   GST_INFO ("target CPU: %s", TARGET_CPU);
155 #endif
156
157 #ifdef HAVE_CPU_ARM
158   timeout_multiplier = 10;
159 #endif
160
161   if (timeout_multiplier > 1) {
162     const gchar *tmult = g_getenv ("CK_TIMEOUT_MULTIPLIER");
163
164     if (tmult == NULL) {
165       gchar num_str[32];
166
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);
170     } else {
171       GST_INFO ("CK_TIMEOUT_MULTIPLIER already set to '%s'", tmult);
172     }
173   }
174 }
175
176 /* message checking */
177 void
178 gst_check_message_error (GstMessage * message, GstMessageType type,
179     GQuark domain, gint code)
180 {
181   GError *error;
182   gchar *debug;
183
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);
192   g_free (debug);
193 }
194
195 /* helper functions */
196 GstFlowReturn
197 gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
198 {
199   GST_DEBUG_OBJECT (pad, "chain_func: received buffer %p", buffer);
200   buffers = g_list_append (buffers, buffer);
201
202   g_mutex_lock (&check_mutex);
203   g_cond_signal (&check_cond);
204   g_mutex_unlock (&check_mutex);
205
206   return GST_FLOW_OK;
207 }
208
209 /**
210  * gst_check_setup_element:
211  * @factory: factory
212  *
213  * setup an element for a filter test with mysrcpad and mysinkpad
214  *
215  * Returns: (transfer full): a new element
216  */
217 GstElement *
218 gst_check_setup_element (const gchar * factory)
219 {
220   GstElement *element;
221
222   GST_DEBUG ("setup_element");
223
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);
227   return element;
228 }
229
230 void
231 gst_check_teardown_element (GstElement * element)
232 {
233   GST_DEBUG ("teardown_element");
234
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);
239 }
240
241 /**
242  * gst_check_setup_src_pad:
243  * @element: element to setup pad on
244  * @tmpl: pad template
245  *
246  * Does the same as #gst_check_setup_src_pad_by_name with the <emphasis> name </emphasis> parameter equal to "sink".
247  *
248  * Returns: (transfer full): A new pad that can be used to inject data on @element
249  */
250 GstPad *
251 gst_check_setup_src_pad (GstElement * element, GstStaticPadTemplate * tmpl)
252 {
253   return gst_check_setup_src_pad_by_name (element, tmpl, "sink");
254 }
255
256 /**
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
261  *
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.
264  *
265  * Example of how to push a buffer on @element:
266  *
267  * |[<!-- language="C" -->
268  * static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
269  * GST_PAD_SINK,
270  * GST_PAD_ALWAYS,
271  * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
272  * );
273  * static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
274  * GST_PAD_SRC,
275  * GST_PAD_ALWAYS,
276  * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
277  * );
278  *
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);
282  *
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");
286  *
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);
290  *
291  * fail_unless (gst_pad_push (mysrcpad, gst_buffer_new_and_alloc(2)) == GST_FLOW_OK);
292  * ]|
293  *
294  * For very simple input/output test scenarios checkout #gst_check_element_push_buffer_list and #gst_check_element_push_buffer.
295  *
296  * Returns: (transfer full): A new pad that can be used to inject data on @element
297  */
298 GstPad *
299 gst_check_setup_src_pad_by_name (GstElement * element,
300     GstStaticPadTemplate * tmpl, const gchar * name)
301 {
302   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
303   GstPad *srcpad;
304
305   srcpad = gst_check_setup_src_pad_by_name_from_template (element, ptmpl, name);
306
307   gst_object_unref (ptmpl);
308
309   return srcpad;
310 }
311
312 /**
313  * gst_check_setup_src_pad_from_template:
314  * @element: element to setup pad on
315  * @tmpl: pad template
316  *
317  * Returns: (transfer full): a new pad
318  *
319  * Since: 1.4
320  */
321 GstPad *
322 gst_check_setup_src_pad_from_template (GstElement * element,
323     GstPadTemplate * tmpl)
324 {
325   return gst_check_setup_src_pad_by_name_from_template (element, tmpl, "sink");
326 }
327
328 /**
329  * gst_check_setup_src_pad_by_name_from_template:
330  * @element: element to setup pad on
331  * @tmpl: pad template
332  * @name: name
333  *
334  * Returns: (transfer full): a new pad
335  *
336  * Since: 1.4
337  */
338 GstPad *
339 gst_check_setup_src_pad_by_name_from_template (GstElement * element,
340     GstPadTemplate * tmpl, const gchar * name)
341 {
342   GstPad *srcpad, *sinkpad;
343
344   /* sending pad */
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);
349
350   sinkpad = gst_element_get_static_pad (element, name);
351   if (sinkpad == NULL)
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);
360
361   return srcpad;
362 }
363
364 void
365 gst_check_teardown_pad_by_name (GstElement * element, const gchar * name)
366 {
367   GstPad *pad_peer, *pad_element;
368
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);
376
377   if (pad_peer) {
378     if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
379       gst_pad_unlink (pad_peer, pad_element);
380     else
381       gst_pad_unlink (pad_element, pad_peer);
382   }
383
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 */
388
389   if (pad_peer) {
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);
394   }
395 }
396
397 void
398 gst_check_teardown_src_pad (GstElement * element)
399 {
400   gst_check_teardown_pad_by_name (element, "sink");
401 }
402
403 /**
404  * gst_check_setup_sink_pad:
405  * @element: element to setup pad on
406  * @tmpl: pad template
407  *
408  * Does the same as #gst_check_setup_sink_pad_by_name with the <emphasis> name </emphasis> parameter equal to "src".
409  *
410  * Returns: (transfer full): a new pad that can be used to check the output of @element
411  */
412 GstPad *
413 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * tmpl)
414 {
415   return gst_check_setup_sink_pad_by_name (element, tmpl, "src");
416 }
417
418 /**
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
423  *
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.
427  *
428  * Returns: (transfer full): a new pad that can be used to check the output of @element
429  */
430 GstPad *
431 gst_check_setup_sink_pad_by_name (GstElement * element,
432     GstStaticPadTemplate * tmpl, const gchar * name)
433 {
434   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
435   GstPad *sinkpad;
436
437   sinkpad =
438       gst_check_setup_sink_pad_by_name_from_template (element, ptmpl, name);
439
440   gst_object_unref (ptmpl);
441
442   return sinkpad;
443 }
444
445 /**
446  * gst_check_setup_sink_pad_from_template:
447  * @element: element to setup pad on
448  * @tmpl: pad template
449  *
450  * Returns: (transfer full): a new pad
451  *
452  * Since: 1.4
453  */
454 GstPad *
455 gst_check_setup_sink_pad_from_template (GstElement * element,
456     GstPadTemplate * tmpl)
457 {
458   return gst_check_setup_sink_pad_by_name_from_template (element, tmpl, "src");
459 }
460
461 /**
462  * gst_check_setup_sink_pad_by_name_from_template:
463  * @element: element to setup pad on
464  * @tmpl: pad template
465  * @name: name
466  *
467  * Returns: (transfer full): a new pad
468  *
469  * Since: 1.4
470  */
471 GstPad *
472 gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
473     GstPadTemplate * tmpl, const gchar * name)
474 {
475   GstPad *srcpad, *sinkpad;
476
477   /* receiving pad */
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");
481
482   srcpad = gst_element_get_static_pad (element, name);
483   if (srcpad == NULL)
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);
488
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);
494
495   GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
496   return sinkpad;
497 }
498
499 void
500 gst_check_teardown_sink_pad (GstElement * element)
501 {
502   gst_check_teardown_pad_by_name (element, "src");
503 }
504
505 /**
506  * gst_check_drop_buffers:
507  *
508  * Unref and remove all buffers that are in the global @buffers GList,
509  * emptying the list.
510  */
511 void
512 gst_check_drop_buffers (void)
513 {
514   while (buffers != NULL) {
515     gst_buffer_unref (GST_BUFFER (buffers->data));
516     buffers = g_list_delete_link (buffers, buffers);
517   }
518 }
519
520 /**
521  * gst_check_caps_equal:
522  * @caps1: first caps to compare
523  * @caps2: second caps to compare
524  *
525  * Compare two caps with gst_caps_is_equal and fail unless they are
526  * equal.
527  */
528 void
529 gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
530 {
531   gchar *name1 = gst_caps_to_string (caps1);
532   gchar *name2 = gst_caps_to_string (caps2);
533
534   fail_unless (gst_caps_is_equal (caps1, caps2),
535       "caps ('%s') is not equal to caps ('%s')", name1, name2);
536   g_free (name1);
537   g_free (name2);
538 }
539
540
541 /**
542  * gst_check_buffer_data:
543  * @buffer: buffer to compare
544  * @data: data to compare to
545  * @size: size of data to compare
546  *
547  * Compare the buffer contents with @data and @size.
548  */
549 void
550 gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size)
551 {
552   GstMapInfo info;
553
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);
562     fail ("buffer contents not equal");
563   }
564   gst_buffer_unmap (buffer, &info);
565 }
566
567 static gboolean
568 buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
569 {
570   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
571     GstCaps *event_caps;
572     GstCaps *expected_caps = gst_pad_get_element_private (pad);
573
574     gst_event_parse_caps (event, &event_caps);
575     fail_unless (gst_caps_is_fixed (expected_caps));
576     fail_unless (gst_caps_is_fixed (event_caps));
577     fail_unless (gst_caps_is_equal_fixed (event_caps, expected_caps));
578     gst_event_unref (event);
579     return TRUE;
580   }
581
582   return gst_pad_event_default (pad, noparent, event);
583 }
584
585 /**
586  * gst_check_element_push_buffer_list:
587  * @element_name: name of the element that needs to be created
588  * @buffer_in: (element-type GstBuffer) (transfer full): a list of buffers that needs to be
589  *  pushed to the element
590  * @caps_in: the #GstCaps expected of the sinkpad of the element
591  * @buffer_out: (element-type GstBuffer) (transfer full): a list of buffers that we expect from
592  * the element
593  * @caps_out: the #GstCaps expected of the srcpad of the element
594  * @last_flow_return: the last buffer push needs to give this GstFlowReturn
595  *
596  * Create an element using the factory providing the @element_name and push the
597  * buffers in @buffer_in to this element. The element should create the buffers
598  * equal to the buffers in @buffer_out. We only check the size and the data of
599  * the buffers. This function unrefs the buffers in the two lists.
600  * The last_flow_return parameter indicates the expected flow return value from
601  * pushing the final buffer in the list.
602  * This can be used to set up a test which pushes some buffers and then an
603  * invalid buffer, when the final buffer is expected to fail, for example.
604  */
605 /* FIXME 2.0: rename this function now that there's GstBufferList? */
606 void
607 gst_check_element_push_buffer_list (const gchar * element_name,
608     GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
609     GstCaps * caps_out, GstFlowReturn last_flow_return)
610 {
611   GstElement *element;
612   GstPad *pad_peer;
613   GstPad *sink_pad = NULL;
614   GstPad *src_pad;
615   GstBuffer *buffer;
616
617   /* check that there are no buffers waiting */
618   gst_check_drop_buffers ();
619   /* create the element */
620   element = gst_check_setup_element (element_name);
621   fail_if (element == NULL, "failed to create the element '%s'", element_name);
622   fail_unless (GST_IS_ELEMENT (element), "the element is no element");
623   /* create the src pad */
624   buffer = GST_BUFFER (buffer_in->data);
625
626   fail_unless (GST_IS_BUFFER (buffer), "There should be a buffer in buffer_in");
627   src_pad = gst_pad_new ("src", GST_PAD_SRC);
628   if (caps_in) {
629     fail_unless (gst_caps_is_fixed (caps_in));
630     gst_pad_use_fixed_caps (src_pad);
631   }
632   /* activate the pad */
633   gst_pad_set_active (src_pad, TRUE);
634   GST_DEBUG ("src pad activated");
635   gst_check_setup_events (src_pad, element, caps_in, GST_FORMAT_BYTES);
636   pad_peer = gst_element_get_static_pad (element, "sink");
637   fail_if (pad_peer == NULL);
638   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
639       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
640   gst_object_unref (pad_peer);
641   /* don't create the sink_pad if there is no buffer_out list */
642   if (buffer_out != NULL) {
643
644     GST_DEBUG ("buffer out detected, creating the sink pad");
645     /* get the sink caps */
646     if (caps_out) {
647       gchar *temp;
648
649       fail_unless (gst_caps_is_fixed (caps_out));
650       temp = gst_caps_to_string (caps_out);
651
652       GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
653       g_free (temp);
654     }
655
656     /* get the sink pad */
657     sink_pad = gst_pad_new ("sink", GST_PAD_SINK);
658     fail_unless (GST_IS_PAD (sink_pad));
659     /* configure the sink pad */
660     gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
661     gst_pad_set_active (sink_pad, TRUE);
662     if (caps_out) {
663       gst_pad_set_element_private (sink_pad, caps_out);
664       gst_pad_set_event_function (sink_pad, buffer_event_function);
665     }
666     /* get the peer pad */
667     pad_peer = gst_element_get_static_pad (element, "src");
668     fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
669         "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
670     gst_object_unref (pad_peer);
671   }
672   fail_unless (gst_element_set_state (element,
673           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
674       "could not set to playing");
675   /* push all the buffers in the buffer_in list */
676   fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
677   while (buffer_in != NULL) {
678     GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);
679
680     fail_unless (GST_IS_BUFFER (next_buffer),
681         "data in buffer_in should be a buffer");
682     /* remove the buffer from the list */
683     buffer_in = g_list_remove (buffer_in, next_buffer);
684     if (buffer_in == NULL) {
685       fail_unless (gst_pad_push (src_pad, next_buffer) == last_flow_return,
686           "we expect something else from the last buffer");
687     } else {
688       fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
689           "Failed to push buffer in");
690     }
691   }
692   fail_unless (gst_element_set_state (element,
693           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
694   /* check that there is a buffer out */
695   fail_unless_equals_int (g_list_length (buffers), g_list_length (buffer_out));
696   while (buffers != NULL) {
697     GstBuffer *new = GST_BUFFER (buffers->data);
698     GstBuffer *orig = GST_BUFFER (buffer_out->data);
699     GstMapInfo newinfo, originfo;
700
701     gst_buffer_map (new, &newinfo, GST_MAP_READ);
702     gst_buffer_map (orig, &originfo, GST_MAP_READ);
703
704     GST_LOG ("orig buffer: size %" G_GSIZE_FORMAT, originfo.size);
705     GST_LOG ("new  buffer: size %" G_GSIZE_FORMAT, newinfo.size);
706     GST_MEMDUMP ("orig buffer", originfo.data, originfo.size);
707     GST_MEMDUMP ("new  buffer", newinfo.data, newinfo.size);
708
709     /* remove the buffers */
710     buffers = g_list_remove (buffers, new);
711     buffer_out = g_list_remove (buffer_out, orig);
712
713     fail_unless (originfo.size == newinfo.size,
714         "size of the buffers are not the same");
715     fail_unless (memcmp (originfo.data, newinfo.data, newinfo.size) == 0,
716         "data is not the same");
717 #if 0
718     gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
719 #endif
720
721     gst_buffer_unmap (orig, &originfo);
722     gst_buffer_unmap (new, &newinfo);
723
724     gst_buffer_unref (new);
725     gst_buffer_unref (orig);
726   }
727   /* teardown the element and pads */
728   gst_pad_set_active (src_pad, FALSE);
729   gst_check_teardown_src_pad (element);
730   gst_pad_set_active (sink_pad, FALSE);
731   gst_check_teardown_sink_pad (element);
732   gst_check_teardown_element (element);
733 }
734
735 /**
736  * gst_check_element_push_buffer:
737  * @element_name: name of the element that needs to be created
738  * @buffer_in: push this buffer to the element
739  * @caps_in: the #GstCaps expected of the sinkpad of the element
740  * @buffer_out: compare the result with this buffer
741  * @caps_out: the #GstCaps expected of the srcpad of the element
742  *
743  * Create an element using the factory providing the @element_name and
744  * push the @buffer_in to this element. The element should create one buffer
745  * and this will be compared with @buffer_out. We only check the caps
746  * and the data of the buffers. This function unrefs the buffers.
747  */
748 void
749 gst_check_element_push_buffer (const gchar * element_name,
750     GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
751     GstCaps * caps_out)
752 {
753   GList *in = NULL;
754   GList *out = NULL;
755
756   in = g_list_append (in, buffer_in);
757   out = g_list_append (out, buffer_out);
758
759   gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
760       GST_FLOW_OK);
761 }
762
763 void
764 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
765 {
766   if (have_abi_sizes) {
767     gboolean ok = TRUE;
768     gint i;
769
770     for (i = 0; list[i].name; i++) {
771       if (list[i].size != list[i].abi_size) {
772         ok = FALSE;
773         g_print ("sizeof(%s) is %d, expected %d\n",
774             list[i].name, list[i].size, list[i].abi_size);
775       }
776     }
777     fail_unless (ok, "failed ABI check");
778   } else {
779     const gchar *fn;
780
781     if ((fn = g_getenv ("GST_ABI"))) {
782       GError *err = NULL;
783       GString *s;
784       gint i;
785
786       s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
787       for (i = 0; list[i].name; i++) {
788         g_string_append_printf (s, "  {\"%s\", sizeof (%s), %d},\n",
789             list[i].name, list[i].name, list[i].size);
790       }
791       g_string_append (s, "  {NULL, 0, 0}\n");
792       g_string_append (s, "};\n");
793       if (!g_file_set_contents (fn, s->str, s->len, &err)) {
794         g_print ("%s", s->str);
795         g_printerr ("\nFailed to write ABI information: %s\n", err->message);
796         g_clear_error (&err);
797       } else {
798         g_print ("\nWrote ABI information to '%s'.\n", fn);
799       }
800       g_string_free (s, TRUE);
801     } else {
802       g_print ("No structure size list was generated for this architecture.\n");
803       g_print ("Run with GST_ABI environment variable set to output header.\n");
804     }
805   }
806 }
807
808 gint
809 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
810 {
811   SRunner *sr;
812   gchar *xmlfilename = NULL;
813   gint nf;
814
815   sr = srunner_create (suite);
816
817   if (g_getenv ("GST_CHECK_XML")) {
818     /* how lucky we are to have __FILE__ end in .c */
819     xmlfilename = g_strdup_printf ("%sheck.xml", fname);
820
821     srunner_set_xml (sr, xmlfilename);
822   }
823
824   srunner_run_all (sr, CK_NORMAL);
825   nf = srunner_ntests_failed (sr);
826   g_free (xmlfilename);
827   srunner_free (sr);
828   return nf;
829 }
830
831 static gboolean
832 gst_check_have_checks_list (const gchar * env_var_name)
833 {
834   const gchar *env_val;
835
836   env_val = g_getenv (env_var_name);
837   return (env_val != NULL && *env_val != '\0');
838 }
839
840 static gboolean
841 gst_check_func_is_in_list (const gchar * env_var, const gchar * func_name)
842 {
843   const gchar *gst_checks;
844   gboolean res = FALSE;
845   gchar **funcs, **f;
846
847   gst_checks = g_getenv (env_var);
848
849   if (gst_checks == NULL || *gst_checks == '\0')
850     return FALSE;
851
852   /* only run specified functions */
853   funcs = g_strsplit (gst_checks, ",", -1);
854   for (f = funcs; f != NULL && *f != NULL; ++f) {
855     if (g_pattern_match_simple (*f, func_name)) {
856       res = TRUE;
857       break;
858     }
859   }
860   g_strfreev (funcs);
861   return res;
862 }
863
864 gboolean
865 _gst_check_run_test_func (const gchar * func_name)
866 {
867   /* if we have a whitelist, run it only if it's in the whitelist */
868   if (gst_check_have_checks_list ("GST_CHECKS"))
869     return gst_check_func_is_in_list ("GST_CHECKS", func_name);
870
871   /* if we have a blacklist, run it only if it's not in the blacklist */
872   if (gst_check_have_checks_list ("GST_CHECKS_IGNORE"))
873     return !gst_check_func_is_in_list ("GST_CHECKS_IGNORE", func_name);
874
875   /* no filter specified => run all checks */
876   return TRUE;
877 }
878
879 /**
880  * gst_check_setup_events_with_stream_id:
881  * @srcpad: The src #GstPad to push on
882  * @element: The #GstElement use to create the stream id
883  * @caps: (allow-none): #GstCaps in case caps event must be sent
884  * @format: The #GstFormat of the default segment to send
885  * @stream_id: A unique identifier for the stream
886  *
887  * Push stream-start, caps and segment event, which consist of the minimum
888  * required events to allow streaming. Caps is optional to allow raw src
889  * testing.
890  */
891 void
892 gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
893     GstCaps * caps, GstFormat format, const gchar * stream_id)
894 {
895   GstSegment segment;
896
897   gst_segment_init (&segment, format);
898
899   fail_unless (gst_pad_push_event (srcpad,
900           gst_event_new_stream_start (stream_id)));
901   if (caps)
902     fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
903   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&segment)));
904 }
905
906 /**
907  * gst_check_setup_events:
908  * @srcpad: The src #GstPad to push on
909  * @element: The #GstElement use to create the stream id
910  * @caps: (allow-none): #GstCaps in case caps event must be sent
911  * @format: The #GstFormat of the default segment to send
912  *
913  * Push stream-start, caps and segment event, which consist of the minimum
914  * required events to allow streaming. Caps is optional to allow raw src
915  * testing. If @element has more than one src or sink pad, use
916  * gst_check_setup_events_with_stream_id() instead.
917  */
918 void
919 gst_check_setup_events (GstPad * srcpad, GstElement * element,
920     GstCaps * caps, GstFormat format)
921 {
922   gchar *stream_id;
923
924   stream_id = gst_pad_create_stream_id (srcpad, element, NULL);
925   gst_check_setup_events_with_stream_id (srcpad, element, caps, format,
926       stream_id);
927   g_free (stream_id);
928 }
929
930 typedef struct _DestroyedObjectStruct
931 {
932   GObject *object;
933   gboolean destroyed;
934 } DestroyedObjectStruct;
935
936 static void
937 weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
938 {
939   destroyed->destroyed = TRUE;
940 }
941
942 /**
943  * gst_check_objects_destroyed_on_unref:
944  * @object_to_unref: The #GObject to unref
945  * @first_object: (allow-none): The first object that should be destroyed as a
946  * concequence of unrefing @object_to_unref.
947  * @... : Additional object that should have been destroyed.
948  *
949  * Unrefs @object_to_unref and checks that is has properly been
950  * destroyed, also checks that the other objects passed in
951  * parametter have been destroyed as a concequence of
952  * unrefing @object_to_unref. Last variable argument should be NULL.
953  *
954  * Since: 1.6
955  */
956 void
957 gst_check_objects_destroyed_on_unref (gpointer object_to_unref,
958     gpointer first_object, ...)
959 {
960   GObject *object;
961   GList *objs = NULL, *tmp;
962   DestroyedObjectStruct *destroyed = g_slice_new0 (DestroyedObjectStruct);
963
964   destroyed->object = object_to_unref;
965   g_object_weak_ref (object_to_unref, (GWeakNotify) weak_notify, destroyed);
966   objs = g_list_prepend (objs, destroyed);
967
968   if (first_object) {
969     va_list varargs;
970
971     object = first_object;
972
973     va_start (varargs, first_object);
974     while (object) {
975       destroyed = g_slice_new0 (DestroyedObjectStruct);
976       destroyed->object = object;
977       g_object_weak_ref (object, (GWeakNotify) weak_notify, destroyed);
978       objs = g_list_prepend (objs, destroyed);
979       object = va_arg (varargs, GObject *);
980     }
981     va_end (varargs);
982   }
983   gst_object_unref (object_to_unref);
984
985   for (tmp = objs; tmp; tmp = tmp->next) {
986     DestroyedObjectStruct *destroyed = tmp->data;
987
988     if (!destroyed->destroyed) {
989       fail_unless (destroyed->destroyed,
990           "%s_%p is not destroyed, %d refcounts left!",
991           GST_IS_OBJECT (destroyed->
992               object) ? GST_OBJECT_NAME (destroyed->object) :
993           G_OBJECT_TYPE_NAME (destroyed), destroyed->object,
994           destroyed->object->ref_count);
995     }
996     g_slice_free (DestroyedObjectStruct, tmp->data);
997   }
998   g_list_free (objs);
999 }
1000
1001 /**
1002  * gst_check_object_destroyed_on_unref:
1003  * @object_to_unref: The #GObject to unref
1004  *
1005  * Unrefs @object_to_unref and checks that is has properly been
1006  * destroyed.
1007  *
1008  * Since: 1.6
1009  */
1010 void
1011 gst_check_object_destroyed_on_unref (gpointer object_to_unref)
1012 {
1013   gst_check_objects_destroyed_on_unref (object_to_unref, NULL, NULL);
1014 }
1015
1016 /* For ABI compatibility with GStreamer < 1.5 */
1017 /* *INDENT-OFF* */
1018 void
1019 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1020 G_GNUC_PRINTF (4, 5);
1021 /* *INDENT-ON* */
1022
1023 void
1024 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1025 {
1026   gchar *msg;
1027   va_list args;
1028
1029   if (result) {
1030     _mark_point (file, line);
1031     return;
1032   }
1033
1034   va_start (args, expr);
1035   msg = g_strdup_vprintf (expr, args);
1036   va_end (args);
1037
1038   _ck_assert_failed (file, line, msg, NULL);
1039   g_free (msg);
1040 }