check: Call gst_deinit() at exit of all processes
[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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "gstcheck.h"
35
36 GST_DEBUG_CATEGORY (check_debug);
37
38 /* logging function for tests
39  * a test uses g_message() to log a debug line
40  * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
41  * messages
42  */
43
44 gboolean _gst_check_threads_running = FALSE;
45 GList *thread_list = NULL;
46 GMutex mutex;
47 GCond start_cond;               /* used to notify main thread of thread startups */
48 GCond sync_cond;                /* used to synchronize all threads and main thread */
49
50 GList *buffers = NULL;
51 GMutex check_mutex;
52 GCond check_cond;
53
54 /* FIXME 0.11: shouldn't _gst_check_debug be static? Not used anywhere */
55 gboolean _gst_check_debug = FALSE;
56 gboolean _gst_check_raised_critical = FALSE;
57 gboolean _gst_check_raised_warning = FALSE;
58 gboolean _gst_check_expecting_log = FALSE;
59
60 static void gst_check_log_message_func
61     (const gchar * log_domain, GLogLevelFlags log_level,
62     const gchar * message, gpointer user_data)
63 {
64   if (_gst_check_debug) {
65     g_print ("%s", message);
66   }
67 }
68
69 static void gst_check_log_critical_func
70     (const gchar * log_domain, GLogLevelFlags log_level,
71     const gchar * message, gpointer user_data)
72 {
73   if (!_gst_check_expecting_log) {
74     g_print ("\n\nUnexpected critical/warning: %s\n", message);
75     fail ("Unexpected critical/warning: %s", message);
76   }
77
78   if (_gst_check_debug) {
79     g_print ("\nExpected critical/warning: %s\n", message);
80   }
81
82   if (log_level & G_LOG_LEVEL_CRITICAL)
83     _gst_check_raised_critical = TRUE;
84   if (log_level & G_LOG_LEVEL_WARNING)
85     _gst_check_raised_warning = TRUE;
86 }
87
88 static gint
89 sort_plugins (GstPlugin * a, GstPlugin * b)
90 {
91   int ret;
92
93   ret = strcmp (gst_plugin_get_source (a), gst_plugin_get_source (b));
94   if (ret == 0)
95     ret = strcmp (gst_plugin_get_name (a), gst_plugin_get_name (b));
96   return ret;
97 }
98
99 static void
100 print_plugins (void)
101 {
102   GList *plugins, *l;
103
104   plugins = gst_registry_get_plugin_list (gst_registry_get ());
105   plugins = g_list_sort (plugins, (GCompareFunc) sort_plugins);
106   for (l = plugins; l != NULL; l = l->next) {
107     GstPlugin *plugin = GST_PLUGIN (l->data);
108
109     if (strcmp (gst_plugin_get_source (plugin), "BLACKLIST") != 0) {
110       GST_LOG ("%20s@%s", gst_plugin_get_name (plugin),
111           GST_STR_NULL (gst_plugin_get_filename (plugin)));
112     }
113   }
114   gst_plugin_list_free (plugins);
115 }
116
117 /* initialize GStreamer testing */
118 void
119 gst_check_init (int *argc, char **argv[])
120 {
121   guint timeout_multiplier = 1;
122
123   gst_init (argc, argv);
124
125   GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
126
127   if (atexit (gst_deinit) != 0) {
128     GST_ERROR ("failed to set gst_deinit as exit function");
129   }
130
131   if (g_getenv ("GST_TEST_DEBUG"))
132     _gst_check_debug = TRUE;
133
134   g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
135       NULL);
136   g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
137       gst_check_log_critical_func, NULL);
138   g_log_set_handler ("GStreamer", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
139       gst_check_log_critical_func, NULL);
140   g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
141       gst_check_log_critical_func, NULL);
142   g_log_set_handler ("GLib-GIO", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
143       gst_check_log_critical_func, NULL);
144   g_log_set_handler ("GLib", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
145       gst_check_log_critical_func, NULL);
146
147   print_plugins ();
148
149 #ifdef TARGET_CPU
150   GST_INFO ("target CPU: %s", TARGET_CPU);
151 #endif
152
153 #ifdef HAVE_CPU_ARM
154   timeout_multiplier = 10;
155 #endif
156
157   if (timeout_multiplier > 1) {
158     const gchar *tmult = g_getenv ("CK_TIMEOUT_MULTIPLIER");
159
160     if (tmult == NULL) {
161       gchar num_str[32];
162
163       g_snprintf (num_str, sizeof (num_str), "%d", timeout_multiplier);
164       GST_INFO ("slow CPU, setting CK_TIMEOUT_MULTIPLIER to %s", num_str);
165       g_setenv ("CK_TIMEOUT_MULTIPLIER", num_str, TRUE);
166     } else {
167       GST_INFO ("CK_TIMEOUT_MULTIPLIER already set to '%s'", tmult);
168     }
169   }
170 }
171
172 /* message checking */
173 void
174 gst_check_message_error (GstMessage * message, GstMessageType type,
175     GQuark domain, gint code)
176 {
177   GError *error;
178   gchar *debug;
179
180   fail_unless (GST_MESSAGE_TYPE (message) == type,
181       "message is of type %s instead of expected type %s",
182       gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
183       gst_message_type_get_name (type));
184   gst_message_parse_error (message, &error, &debug);
185   fail_unless_equals_int (error->domain, domain);
186   fail_unless_equals_int (error->code, code);
187   g_error_free (error);
188   g_free (debug);
189 }
190
191 /* helper functions */
192 GstFlowReturn
193 gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
194 {
195   GST_DEBUG_OBJECT (pad, "chain_func: received buffer %p", buffer);
196   buffers = g_list_append (buffers, buffer);
197
198   g_mutex_lock (&check_mutex);
199   g_cond_signal (&check_cond);
200   g_mutex_unlock (&check_mutex);
201
202   return GST_FLOW_OK;
203 }
204
205 /**
206  * gst_check_setup_element:
207  * @factory: factory
208  *
209  * setup an element for a filter test with mysrcpad and mysinkpad
210  *
211  * Returns: (transfer full): a new element
212  */
213 GstElement *
214 gst_check_setup_element (const gchar * factory)
215 {
216   GstElement *element;
217
218   GST_DEBUG ("setup_element");
219
220   element = gst_element_factory_make (factory, factory);
221   fail_if (element == NULL, "Could not create a '%s' element", factory);
222   ASSERT_OBJECT_REFCOUNT (element, factory, 1);
223   return element;
224 }
225
226 void
227 gst_check_teardown_element (GstElement * element)
228 {
229   GST_DEBUG ("teardown_element");
230
231   fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
232       GST_STATE_CHANGE_SUCCESS, "could not set to null");
233   ASSERT_OBJECT_REFCOUNT (element, "element", 1);
234   gst_object_unref (element);
235 }
236
237 /**
238  * gst_check_setup_src_pad:
239  * @element: element to setup pad on
240  * @tmpl: pad template
241  *
242  * Returns: (transfer full): a new pad
243  */
244 GstPad *
245 gst_check_setup_src_pad (GstElement * element, GstStaticPadTemplate * tmpl)
246 {
247   return gst_check_setup_src_pad_by_name (element, tmpl, "sink");
248 }
249
250 /**
251  * gst_check_setup_src_pad_by_name:
252  * @element: element to setup pad on
253  * @tmpl: pad template
254  * @name: name
255  *
256  * Returns: (transfer full): a new pad
257  */
258 GstPad *
259 gst_check_setup_src_pad_by_name (GstElement * element,
260     GstStaticPadTemplate * tmpl, const gchar * name)
261 {
262   GstPad *srcpad, *sinkpad;
263
264   /* sending pad */
265   srcpad = gst_pad_new_from_static_template (tmpl, "src");
266   GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
267   fail_if (srcpad == NULL, "Could not create a srcpad");
268   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
269
270   sinkpad = gst_element_get_static_pad (element, name);
271   if (sinkpad == NULL)
272     sinkpad = gst_element_get_request_pad (element, name);
273   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
274       GST_ELEMENT_NAME (element));
275   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
276   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
277       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
278   gst_object_unref (sinkpad);   /* because we got it higher up */
279   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
280
281   return srcpad;
282 }
283
284 void
285 gst_check_teardown_pad_by_name (GstElement * element, const gchar * name)
286 {
287   GstPad *pad_peer, *pad_element;
288
289   /* clean up floating src pad */
290   pad_element = gst_element_get_static_pad (element, name);
291   /* We don't check the refcount here since there *might* be
292    * a pad cache holding an extra reference on pad_element.
293    * To get to a state where no pad cache will exist,
294    * we first unlink that pad. */
295   pad_peer = gst_pad_get_peer (pad_element);
296
297   if (pad_peer) {
298     if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
299       gst_pad_unlink (pad_peer, pad_element);
300     else
301       gst_pad_unlink (pad_element, pad_peer);
302   }
303
304   /* pad refs held by both creator and this function (through _get) */
305   ASSERT_OBJECT_REFCOUNT (pad_element, "element pad_element", 2);
306   gst_object_unref (pad_element);
307   /* one more ref is held by element itself */
308
309   if (pad_peer) {
310     /* pad refs held by both creator and this function (through _get_peer) */
311     ASSERT_OBJECT_REFCOUNT (pad_peer, "check pad_peer", 2);
312     gst_object_unref (pad_peer);
313     gst_object_unref (pad_peer);
314   }
315 }
316
317 void
318 gst_check_teardown_src_pad (GstElement * element)
319 {
320   gst_check_teardown_pad_by_name (element, "sink");
321 }
322
323 /**
324  * gst_check_setup_sink_pad:
325  * @element: element to setup pad on
326  * @tmpl: pad template
327  *
328  * Returns: (transfer full): a new pad
329  */
330 GstPad *
331 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * tmpl)
332 {
333   return gst_check_setup_sink_pad_by_name (element, tmpl, "src");
334 }
335
336 /**
337  * gst_check_setup_sink_pad_by_name:
338  * @element: element to setup pad on
339  * @tmpl: pad template
340  * @name: name
341  *
342  * Returns: (transfer full): a new pad
343  */
344 GstPad *
345 gst_check_setup_sink_pad_by_name (GstElement * element,
346     GstStaticPadTemplate * tmpl, const gchar * name)
347 {
348   GstPad *srcpad, *sinkpad;
349
350   /* receiving pad */
351   sinkpad = gst_pad_new_from_static_template (tmpl, "sink");
352   GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
353   fail_if (sinkpad == NULL, "Could not create a sinkpad");
354
355   srcpad = gst_element_get_static_pad (element, name);
356   if (srcpad == NULL)
357     srcpad = gst_element_get_request_pad (element, name);
358   fail_if (srcpad == NULL, "Could not get source pad from %s",
359       GST_ELEMENT_NAME (element));
360   gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
361
362   GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
363   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
364       "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
365   gst_object_unref (srcpad);    /* because we got it higher up */
366   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
367
368   GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
369   return sinkpad;
370 }
371
372 void
373 gst_check_teardown_sink_pad (GstElement * element)
374 {
375   gst_check_teardown_pad_by_name (element, "src");
376 }
377
378 /**
379  * gst_check_drop_buffers:
380  *
381  * Unref and remove all buffers that are in the global @buffers GList,
382  * emptying the list.
383  */
384 void
385 gst_check_drop_buffers (void)
386 {
387   while (buffers != NULL) {
388     gst_buffer_unref (GST_BUFFER (buffers->data));
389     buffers = g_list_delete_link (buffers, buffers);
390   }
391 }
392
393 /**
394  * gst_check_caps_equal:
395  * @caps1: first caps to compare
396  * @caps2: second caps to compare
397  *
398  * Compare two caps with gst_caps_is_equal and fail unless they are
399  * equal.
400  */
401 void
402 gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
403 {
404   gchar *name1 = gst_caps_to_string (caps1);
405   gchar *name2 = gst_caps_to_string (caps2);
406
407   fail_unless (gst_caps_is_equal (caps1, caps2),
408       "caps ('%s') is not equal to caps ('%s')", name1, name2);
409   g_free (name1);
410   g_free (name2);
411 }
412
413
414 /**
415  * gst_check_buffer_data:
416  * @buffer: buffer to compare
417  * @data: data to compare to
418  * @size: size of data to compare
419  *
420  * Compare the buffer contents with @data and @size.
421  */
422 void
423 gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size)
424 {
425   GstMapInfo info;
426
427   gst_buffer_map (buffer, &info, GST_MAP_READ);
428   GST_MEMDUMP ("Converted data", info.data, info.size);
429   GST_MEMDUMP ("Expected data", data, size);
430   if (memcmp (info.data, data, size) != 0) {
431     g_print ("\nConverted data:\n");
432     gst_util_dump_mem (info.data, info.size);
433     g_print ("\nExpected data:\n");
434     gst_util_dump_mem (data, size);
435   }
436   fail_unless (memcmp (info.data, data, size) == 0,
437       "buffer contents not equal");
438   gst_buffer_unmap (buffer, &info);
439 }
440
441 static gboolean
442 buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
443 {
444   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
445     GstCaps *event_caps;
446     GstCaps *expected_caps = gst_pad_get_element_private (pad);
447
448     gst_event_parse_caps (event, &event_caps);
449     fail_unless (gst_caps_is_fixed (expected_caps));
450     fail_unless (gst_caps_is_fixed (event_caps));
451     fail_unless (gst_caps_is_equal_fixed (event_caps, expected_caps));
452     gst_event_unref (event);
453     return TRUE;
454   }
455
456   return gst_pad_event_default (pad, noparent, event);
457 }
458
459 /**
460  * gst_check_element_push_buffer_list:
461  * @element_name: name of the element that needs to be created
462  * @buffer_in: (element-type GstBuffer) (transfer full): a list of buffers that needs to be
463  *  puched to the element
464  * @buffer_out: (element-type GstBuffer) (transfer full): a list of buffers that we expect from
465  * the element
466  * @last_flow_return: the last buffer push needs to give this GstFlowReturn
467  *
468  * Create an @element with the factory with the name and push the buffers in
469  * @buffer_in to this element. The element should create the buffers equal to
470  * the buffers in @buffer_out. We only check the caps, size and the data of the
471  * buffers. This function unrefs the buffers in the two lists.
472  * The last_flow_return parameter indicates the expected flow return value from
473  * pushing the final buffer in the list.
474  * This can be used to set up a test which pushes some buffers and then an
475  * invalid buffer, when the final buffer is expected to fail, for example.
476  */
477 /* FIXME 0.11: rename this function now that there's GstBufferList? */
478 void
479 gst_check_element_push_buffer_list (const gchar * element_name,
480     GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
481     GstCaps * caps_out, GstFlowReturn last_flow_return)
482 {
483   GstElement *element;
484   GstPad *pad_peer;
485   GstPad *sink_pad = NULL;
486   GstPad *src_pad;
487   GstBuffer *buffer;
488
489   /* check that there are no buffers waiting */
490   gst_check_drop_buffers ();
491   /* create the element */
492   element = gst_check_setup_element (element_name);
493   fail_if (element == NULL, "failed to create the element '%s'", element_name);
494   fail_unless (GST_IS_ELEMENT (element), "the element is no element");
495   /* create the src pad */
496   buffer = GST_BUFFER (buffer_in->data);
497
498   fail_unless (GST_IS_BUFFER (buffer), "There should be a buffer in buffer_in");
499   src_pad = gst_pad_new ("src", GST_PAD_SRC);
500   if (caps_in) {
501     fail_unless (gst_caps_is_fixed (caps_in));
502     gst_pad_use_fixed_caps (src_pad);
503   }
504   /* activate the pad */
505   gst_pad_set_active (src_pad, TRUE);
506   GST_DEBUG ("src pad activated");
507   gst_check_setup_events (src_pad, element, caps_in, GST_FORMAT_BYTES);
508   pad_peer = gst_element_get_static_pad (element, "sink");
509   fail_if (pad_peer == NULL);
510   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
511       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
512   gst_object_unref (pad_peer);
513   /* don't create the sink_pad if there is no buffer_out list */
514   if (buffer_out != NULL) {
515
516     GST_DEBUG ("buffer out detected, creating the sink pad");
517     /* get the sink caps */
518     if (caps_out) {
519       gchar *temp;
520
521       fail_unless (gst_caps_is_fixed (caps_out));
522       temp = gst_caps_to_string (caps_out);
523
524       GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
525       g_free (temp);
526     }
527
528     /* get the sink pad */
529     sink_pad = gst_pad_new ("sink", GST_PAD_SINK);
530     fail_unless (GST_IS_PAD (sink_pad));
531     /* configure the sink pad */
532     gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
533     gst_pad_set_active (sink_pad, TRUE);
534     if (caps_out) {
535       gst_pad_set_element_private (sink_pad, caps_out);
536       gst_pad_set_event_function (sink_pad, buffer_event_function);
537     }
538     /* get the peer pad */
539     pad_peer = gst_element_get_static_pad (element, "src");
540     fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
541         "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
542     gst_object_unref (pad_peer);
543   }
544   fail_unless (gst_element_set_state (element,
545           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
546       "could not set to playing");
547   /* push all the buffers in the buffer_in list */
548   fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
549   while (buffer_in != NULL) {
550     GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);
551
552     fail_unless (GST_IS_BUFFER (next_buffer),
553         "data in buffer_in should be a buffer");
554     /* remove the buffer from the list */
555     buffer_in = g_list_remove (buffer_in, next_buffer);
556     if (buffer_in == NULL) {
557       fail_unless (gst_pad_push (src_pad, next_buffer) == last_flow_return,
558           "we expect something else from the last buffer");
559     } else {
560       fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
561           "Failed to push buffer in");
562     }
563   }
564   fail_unless (gst_element_set_state (element,
565           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
566   /* check that there is a buffer out */
567   fail_unless_equals_int (g_list_length (buffers), g_list_length (buffer_out));
568   while (buffers != NULL) {
569     GstBuffer *new = GST_BUFFER (buffers->data);
570     GstBuffer *orig = GST_BUFFER (buffer_out->data);
571     GstMapInfo newinfo, originfo;
572
573     gst_buffer_map (new, &newinfo, GST_MAP_READ);
574     gst_buffer_map (orig, &originfo, GST_MAP_READ);
575
576     GST_LOG ("orig buffer: size %" G_GSIZE_FORMAT, originfo.size);
577     GST_LOG ("new  buffer: size %" G_GSIZE_FORMAT, newinfo.size);
578     GST_MEMDUMP ("orig buffer", originfo.data, originfo.size);
579     GST_MEMDUMP ("new  buffer", newinfo.data, newinfo.size);
580
581     /* remove the buffers */
582     buffers = g_list_remove (buffers, new);
583     buffer_out = g_list_remove (buffer_out, orig);
584
585     fail_unless (originfo.size == newinfo.size,
586         "size of the buffers are not the same");
587     fail_unless (memcmp (originfo.data, newinfo.data, newinfo.size) == 0,
588         "data is not the same");
589 #if 0
590     gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
591 #endif
592
593     gst_buffer_unmap (orig, &originfo);
594     gst_buffer_unmap (new, &newinfo);
595
596     gst_buffer_unref (new);
597     gst_buffer_unref (orig);
598   }
599   /* teardown the element and pads */
600   gst_pad_set_active (src_pad, FALSE);
601   gst_check_teardown_src_pad (element);
602   gst_pad_set_active (sink_pad, FALSE);
603   gst_check_teardown_sink_pad (element);
604   gst_check_teardown_element (element);
605 }
606
607 /**
608  * gst_check_element_push_buffer:
609  * @element_name: name of the element that needs to be created
610  * @buffer_in: push this buffer to the element
611  * @buffer_out: compare the result with this buffer
612  *
613  * Create an @element with the factory with the name and push the
614  * @buffer_in to this element. The element should create one buffer
615  * and this will be compared with @buffer_out. We only check the caps
616  * and the data of the buffers. This function unrefs the buffers.
617  */
618 void
619 gst_check_element_push_buffer (const gchar * element_name,
620     GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
621     GstCaps * caps_out)
622 {
623   GList *in = NULL;
624   GList *out = NULL;
625
626   in = g_list_append (in, buffer_in);
627   out = g_list_append (out, buffer_out);
628
629   gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
630       GST_FLOW_OK);
631 }
632
633 void
634 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
635 {
636   if (have_abi_sizes) {
637     gboolean ok = TRUE;
638     gint i;
639
640     for (i = 0; list[i].name; i++) {
641       if (list[i].size != list[i].abi_size) {
642         ok = FALSE;
643         g_print ("sizeof(%s) is %d, expected %d\n",
644             list[i].name, list[i].size, list[i].abi_size);
645       }
646     }
647     fail_unless (ok, "failed ABI check");
648   } else {
649     const gchar *fn;
650
651     if ((fn = g_getenv ("GST_ABI"))) {
652       GError *err = NULL;
653       GString *s;
654       gint i;
655
656       s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
657       for (i = 0; list[i].name; i++) {
658         g_string_append_printf (s, "  {\"%s\", sizeof (%s), %d},\n",
659             list[i].name, list[i].name, list[i].size);
660       }
661       g_string_append (s, "  {NULL, 0, 0}\n");
662       g_string_append (s, "};\n");
663       if (!g_file_set_contents (fn, s->str, s->len, &err)) {
664         g_print ("%s", s->str);
665         g_printerr ("\nFailed to write ABI information: %s\n", err->message);
666       } else {
667         g_print ("\nWrote ABI information to '%s'.\n", fn);
668       }
669       g_string_free (s, TRUE);
670     } else {
671       g_print ("No structure size list was generated for this architecture.\n");
672       g_print ("Run with GST_ABI environment variable set to output header.\n");
673     }
674   }
675 }
676
677 gint
678 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
679 {
680   SRunner *sr;
681   gchar *xmlfilename = NULL;
682   gint nf;
683
684   sr = srunner_create (suite);
685
686   if (g_getenv ("GST_CHECK_XML")) {
687     /* how lucky we are to have __FILE__ end in .c */
688     xmlfilename = g_strdup_printf ("%sheck.xml", fname);
689
690     srunner_set_xml (sr, xmlfilename);
691   }
692
693   srunner_run_all (sr, CK_NORMAL);
694   nf = srunner_ntests_failed (sr);
695   g_free (xmlfilename);
696   srunner_free (sr);
697   return nf;
698 }
699
700 gboolean
701 _gst_check_run_test_func (const gchar * func_name)
702 {
703   const gchar *gst_checks;
704   gboolean res = FALSE;
705   gchar **funcs, **f;
706
707   gst_checks = g_getenv ("GST_CHECKS");
708
709   /* no filter specified => run all checks */
710   if (gst_checks == NULL || *gst_checks == '\0')
711     return TRUE;
712
713   /* only run specified functions */
714   funcs = g_strsplit (gst_checks, ",", -1);
715   for (f = funcs; f != NULL && *f != NULL; ++f) {
716     if (g_pattern_match_simple (*f, func_name)) {
717       res = TRUE;
718       break;
719     }
720   }
721   g_strfreev (funcs);
722   return res;
723 }
724
725 /**
726  * gst_check_setup_events_with_stream_id:
727  * @srcpad: The src #GstPad to push on
728  * @element: The #GstElement use to create the stream id
729  * @caps: (allow-none): #GstCaps in case caps event must be sent
730  * @format: The #GstFormat of the default segment to send
731  * @stream_id: A unique identifier for the stream
732  *
733  * Push stream-start, caps and segment event, which concist of the minimum
734  * required events to allow streaming. Caps is optional to allow raw src
735  * testing.
736  */
737 void
738 gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
739     GstCaps * caps, GstFormat format, const gchar * stream_id)
740 {
741   GstSegment segment;
742
743   gst_segment_init (&segment, format);
744
745   fail_unless (gst_pad_push_event (srcpad,
746           gst_event_new_stream_start (stream_id)));
747   if (caps)
748     fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
749   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&segment)));
750 }
751
752 /**
753  * gst_check_setup_events:
754  * @srcpad: The src #GstPad to push on
755  * @element: The #GstElement use to create the stream id
756  * @caps: (allow-none): #GstCaps in case caps event must be sent
757  * @format: The #GstFormat of the default segment to send
758  *
759  * Push stream-start, caps and segment event, which concist of the minimum
760  * required events to allow streaming. Caps is optional to allow raw src
761  * testing. If @element has more than one src or sink pad, use
762  * gst_check_setup_events_with_stream_id() instead.
763  */
764 void
765 gst_check_setup_events (GstPad * srcpad, GstElement * element,
766     GstCaps * caps, GstFormat format)
767 {
768   gchar *stream_id;
769
770   stream_id = gst_pad_create_stream_id (srcpad, element, NULL);
771   gst_check_setup_events_with_stream_id (srcpad, element, caps, format,
772       stream_id);
773   g_free (stream_id);
774 }