downloadbuffer, benchmarks: fix error leaks in failure code paths
[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   }
563   fail_unless (memcmp (info.data, data, size) == 0,
564       "buffer contents not equal");
565   gst_buffer_unmap (buffer, &info);
566 }
567
568 static gboolean
569 buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
570 {
571   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
572     GstCaps *event_caps;
573     GstCaps *expected_caps = gst_pad_get_element_private (pad);
574
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);
580     return TRUE;
581   }
582
583   return gst_pad_event_default (pad, noparent, event);
584 }
585
586 /**
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
593  * the element
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
596  *
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.
605  */
606 /* FIXME 2.0: rename this function now that there's GstBufferList? */
607 void
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)
611 {
612   GstElement *element;
613   GstPad *pad_peer;
614   GstPad *sink_pad = NULL;
615   GstPad *src_pad;
616   GstBuffer *buffer;
617
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);
626
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);
629   if (caps_in) {
630     fail_unless (gst_caps_is_fixed (caps_in));
631     gst_pad_use_fixed_caps (src_pad);
632   }
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) {
644
645     GST_DEBUG ("buffer out detected, creating the sink pad");
646     /* get the sink caps */
647     if (caps_out) {
648       gchar *temp;
649
650       fail_unless (gst_caps_is_fixed (caps_out));
651       temp = gst_caps_to_string (caps_out);
652
653       GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
654       g_free (temp);
655     }
656
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);
663     if (caps_out) {
664       gst_pad_set_element_private (sink_pad, caps_out);
665       gst_pad_set_event_function (sink_pad, buffer_event_function);
666     }
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);
672   }
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);
680
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");
688     } else {
689       fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
690           "Failed to push buffer in");
691     }
692   }
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;
701
702     gst_buffer_map (new, &newinfo, GST_MAP_READ);
703     gst_buffer_map (orig, &originfo, GST_MAP_READ);
704
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);
709
710     /* remove the buffers */
711     buffers = g_list_remove (buffers, new);
712     buffer_out = g_list_remove (buffer_out, orig);
713
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");
718 #if 0
719     gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
720 #endif
721
722     gst_buffer_unmap (orig, &originfo);
723     gst_buffer_unmap (new, &newinfo);
724
725     gst_buffer_unref (new);
726     gst_buffer_unref (orig);
727   }
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);
734 }
735
736 /**
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
743  *
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.
748  */
749 void
750 gst_check_element_push_buffer (const gchar * element_name,
751     GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
752     GstCaps * caps_out)
753 {
754   GList *in = NULL;
755   GList *out = NULL;
756
757   in = g_list_append (in, buffer_in);
758   out = g_list_append (out, buffer_out);
759
760   gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
761       GST_FLOW_OK);
762 }
763
764 void
765 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
766 {
767   if (have_abi_sizes) {
768     gboolean ok = TRUE;
769     gint i;
770
771     for (i = 0; list[i].name; i++) {
772       if (list[i].size != list[i].abi_size) {
773         ok = FALSE;
774         g_print ("sizeof(%s) is %d, expected %d\n",
775             list[i].name, list[i].size, list[i].abi_size);
776       }
777     }
778     fail_unless (ok, "failed ABI check");
779   } else {
780     const gchar *fn;
781
782     if ((fn = g_getenv ("GST_ABI"))) {
783       GError *err = NULL;
784       GString *s;
785       gint i;
786
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);
791       }
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);
798       } else {
799         g_print ("\nWrote ABI information to '%s'.\n", fn);
800       }
801       g_string_free (s, TRUE);
802     } else {
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");
805     }
806   }
807 }
808
809 gint
810 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
811 {
812   SRunner *sr;
813   gchar *xmlfilename = NULL;
814   gint nf;
815
816   sr = srunner_create (suite);
817
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);
821
822     srunner_set_xml (sr, xmlfilename);
823   }
824
825   srunner_run_all (sr, CK_NORMAL);
826   nf = srunner_ntests_failed (sr);
827   g_free (xmlfilename);
828   srunner_free (sr);
829   return nf;
830 }
831
832 static gboolean
833 gst_check_have_checks_list (const gchar * env_var_name)
834 {
835   const gchar *env_val;
836
837   env_val = g_getenv (env_var_name);
838   return (env_val != NULL && *env_val != '\0');
839 }
840
841 static gboolean
842 gst_check_func_is_in_list (const gchar * env_var, const gchar * func_name)
843 {
844   const gchar *gst_checks;
845   gboolean res = FALSE;
846   gchar **funcs, **f;
847
848   gst_checks = g_getenv (env_var);
849
850   if (gst_checks == NULL || *gst_checks == '\0')
851     return FALSE;
852
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)) {
857       res = TRUE;
858       break;
859     }
860   }
861   g_strfreev (funcs);
862   return res;
863 }
864
865 gboolean
866 _gst_check_run_test_func (const gchar * func_name)
867 {
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);
871
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);
875
876   /* no filter specified => run all checks */
877   return TRUE;
878 }
879
880 /**
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
887  *
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
890  * testing.
891  */
892 void
893 gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
894     GstCaps * caps, GstFormat format, const gchar * stream_id)
895 {
896   GstSegment segment;
897
898   gst_segment_init (&segment, format);
899
900   fail_unless (gst_pad_push_event (srcpad,
901           gst_event_new_stream_start (stream_id)));
902   if (caps)
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)));
905 }
906
907 /**
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
913  *
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.
918  */
919 void
920 gst_check_setup_events (GstPad * srcpad, GstElement * element,
921     GstCaps * caps, GstFormat format)
922 {
923   gchar *stream_id;
924
925   stream_id = gst_pad_create_stream_id (srcpad, element, NULL);
926   gst_check_setup_events_with_stream_id (srcpad, element, caps, format,
927       stream_id);
928   g_free (stream_id);
929 }
930
931 typedef struct _DestroyedObjectStruct
932 {
933   GObject *object;
934   gboolean destroyed;
935 } DestroyedObjectStruct;
936
937 static void
938 weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
939 {
940   destroyed->destroyed = TRUE;
941 }
942
943 /**
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.
949  *
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.
954  *
955  * Since: 1.6
956  */
957 void
958 gst_check_objects_destroyed_on_unref (gpointer object_to_unref,
959     gpointer first_object, ...)
960 {
961   GObject *object;
962   GList *objs = NULL, *tmp;
963   DestroyedObjectStruct *destroyed = g_slice_new0 (DestroyedObjectStruct);
964
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);
968
969   if (first_object) {
970     va_list varargs;
971
972     object = first_object;
973
974     va_start (varargs, first_object);
975     while (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 *);
981     }
982     va_end (varargs);
983   }
984   gst_object_unref (object_to_unref);
985
986   for (tmp = objs; tmp; tmp = tmp->next) {
987     DestroyedObjectStruct *destroyed = tmp->data;
988
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);
996     }
997     g_slice_free (DestroyedObjectStruct, tmp->data);
998   }
999   g_list_free (objs);
1000 }
1001
1002 /**
1003  * gst_check_object_destroyed_on_unref:
1004  * @object_to_unref: The #GObject to unref
1005  *
1006  * Unrefs @object_to_unref and checks that is has properly been
1007  * destroyed.
1008  *
1009  * Since: 1.6
1010  */
1011 void
1012 gst_check_object_destroyed_on_unref (gpointer object_to_unref)
1013 {
1014   gst_check_objects_destroyed_on_unref (object_to_unref, NULL, NULL);
1015 }
1016
1017 /* For ABI compatibility with GStreamer < 1.5 */
1018 void
1019 _fail_unless (int result, const char *file, int line, const char *expr, ...);
1020
1021 void
1022 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1023 {
1024   gchar *msg;
1025   va_list args;
1026
1027   if (result) {
1028     _mark_point (file, line);
1029     return;
1030   }
1031
1032   va_start (args, expr);
1033   msg = g_strdup_vprintf (expr, args);
1034   va_end (args);
1035
1036   _ck_assert_failed (file, line, msg, NULL);
1037   g_free (msg);
1038 }