introspection: Assorted minor introspection and documentation fixes
[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_CHECK
31  * and GST_CHECK_IGNORE to select which tests to run or skip. Both variables
32  * can contain a comman 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 0.11: 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", 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  * Returns: (transfer full): a new pad
247  */
248 GstPad *
249 gst_check_setup_src_pad (GstElement * element, GstStaticPadTemplate * tmpl)
250 {
251   return gst_check_setup_src_pad_by_name (element, tmpl, "sink");
252 }
253
254 /**
255  * gst_check_setup_src_pad_by_name:
256  * @element: element to setup pad on
257  * @tmpl: pad template
258  * @name: name
259  *
260  * Returns: (transfer full): a new pad
261  */
262 GstPad *
263 gst_check_setup_src_pad_by_name (GstElement * element,
264     GstStaticPadTemplate * tmpl, const gchar * name)
265 {
266   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
267   GstPad *srcpad;
268
269   srcpad =
270       gst_check_setup_src_pad_by_name_from_template (element, ptmpl, "sink");
271
272   gst_object_unref (ptmpl);
273
274   return srcpad;
275 }
276
277 /**
278  * gst_check_setup_src_pad_from_template:
279  * @element: element to setup pad on
280  * @tmpl: pad template
281  *
282  * Returns: (transfer full): a new pad
283  *
284  * Since: 1.4
285  */
286 GstPad *
287 gst_check_setup_src_pad_from_template (GstElement * element,
288     GstPadTemplate * tmpl)
289 {
290   return gst_check_setup_src_pad_by_name_from_template (element, tmpl, "sink");
291 }
292
293 /**
294  * gst_check_setup_src_pad_by_name_from_template:
295  * @element: element to setup pad on
296  * @tmpl: pad template
297  * @name: name
298  *
299  * Returns: (transfer full): a new pad
300  *
301  * Since: 1.4
302  */
303 GstPad *
304 gst_check_setup_src_pad_by_name_from_template (GstElement * element,
305     GstPadTemplate * tmpl, const gchar * name)
306 {
307   GstPad *srcpad, *sinkpad;
308
309   /* sending pad */
310   srcpad = gst_pad_new_from_template (tmpl, "src");
311   GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
312   fail_if (srcpad == NULL, "Could not create a srcpad");
313   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
314
315   sinkpad = gst_element_get_static_pad (element, name);
316   if (sinkpad == NULL)
317     sinkpad = gst_element_get_request_pad (element, name);
318   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
319       GST_ELEMENT_NAME (element));
320   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
321   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
322       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
323   gst_object_unref (sinkpad);   /* because we got it higher up */
324   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
325
326   return srcpad;
327 }
328
329 void
330 gst_check_teardown_pad_by_name (GstElement * element, const gchar * name)
331 {
332   GstPad *pad_peer, *pad_element;
333
334   /* clean up floating src pad */
335   pad_element = gst_element_get_static_pad (element, name);
336   /* We don't check the refcount here since there *might* be
337    * a pad cache holding an extra reference on pad_element.
338    * To get to a state where no pad cache will exist,
339    * we first unlink that pad. */
340   pad_peer = gst_pad_get_peer (pad_element);
341
342   if (pad_peer) {
343     if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
344       gst_pad_unlink (pad_peer, pad_element);
345     else
346       gst_pad_unlink (pad_element, pad_peer);
347   }
348
349   /* pad refs held by both creator and this function (through _get) */
350   ASSERT_OBJECT_REFCOUNT (pad_element, "element pad_element", 2);
351   gst_object_unref (pad_element);
352   /* one more ref is held by element itself */
353
354   if (pad_peer) {
355     /* pad refs held by both creator and this function (through _get_peer) */
356     ASSERT_OBJECT_REFCOUNT (pad_peer, "check pad_peer", 2);
357     gst_object_unref (pad_peer);
358     gst_object_unref (pad_peer);
359   }
360 }
361
362 void
363 gst_check_teardown_src_pad (GstElement * element)
364 {
365   gst_check_teardown_pad_by_name (element, "sink");
366 }
367
368 /**
369  * gst_check_setup_sink_pad:
370  * @element: element to setup pad on
371  * @tmpl: pad template
372  *
373  * Returns: (transfer full): a new pad
374  */
375 GstPad *
376 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * tmpl)
377 {
378   return gst_check_setup_sink_pad_by_name (element, tmpl, "src");
379 }
380
381 /**
382  * gst_check_setup_sink_pad_by_name:
383  * @element: element to setup pad on
384  * @tmpl: pad template
385  * @name: name
386  *
387  * Returns: (transfer full): a new pad
388  */
389 GstPad *
390 gst_check_setup_sink_pad_by_name (GstElement * element,
391     GstStaticPadTemplate * tmpl, const gchar * name)
392 {
393   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
394   GstPad *sinkpad;
395
396   sinkpad =
397       gst_check_setup_sink_pad_by_name_from_template (element, ptmpl, "src");
398
399   gst_object_unref (ptmpl);
400
401   return sinkpad;
402 }
403
404 /**
405  * gst_check_setup_sink_pad_from_template:
406  * @element: element to setup pad on
407  * @tmpl: pad template
408  *
409  * Returns: (transfer full): a new pad
410  *
411  * Since: 1.4
412  */
413 GstPad *
414 gst_check_setup_sink_pad_from_template (GstElement * element,
415     GstPadTemplate * tmpl)
416 {
417   return gst_check_setup_sink_pad_by_name_from_template (element, tmpl, "src");
418 }
419
420 /**
421  * gst_check_setup_sink_pad_by_name_from_template:
422  * @element: element to setup pad on
423  * @tmpl: pad template
424  * @name: name
425  *
426  * Returns: (transfer full): a new pad
427  *
428  * Since: 1.4
429  */
430 GstPad *
431 gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
432     GstPadTemplate * tmpl, const gchar * name)
433 {
434   GstPad *srcpad, *sinkpad;
435
436   /* receiving pad */
437   sinkpad = gst_pad_new_from_template (tmpl, "sink");
438   GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
439   fail_if (sinkpad == NULL, "Could not create a sinkpad");
440
441   srcpad = gst_element_get_static_pad (element, name);
442   if (srcpad == NULL)
443     srcpad = gst_element_get_request_pad (element, name);
444   fail_if (srcpad == NULL, "Could not get source pad from %s",
445       GST_ELEMENT_NAME (element));
446   gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
447
448   GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
449   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
450       "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
451   gst_object_unref (srcpad);    /* because we got it higher up */
452   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
453
454   GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
455   return sinkpad;
456 }
457
458 void
459 gst_check_teardown_sink_pad (GstElement * element)
460 {
461   gst_check_teardown_pad_by_name (element, "src");
462 }
463
464 /**
465  * gst_check_drop_buffers:
466  *
467  * Unref and remove all buffers that are in the global @buffers GList,
468  * emptying the list.
469  */
470 void
471 gst_check_drop_buffers (void)
472 {
473   while (buffers != NULL) {
474     gst_buffer_unref (GST_BUFFER (buffers->data));
475     buffers = g_list_delete_link (buffers, buffers);
476   }
477 }
478
479 /**
480  * gst_check_caps_equal:
481  * @caps1: first caps to compare
482  * @caps2: second caps to compare
483  *
484  * Compare two caps with gst_caps_is_equal and fail unless they are
485  * equal.
486  */
487 void
488 gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
489 {
490   gchar *name1 = gst_caps_to_string (caps1);
491   gchar *name2 = gst_caps_to_string (caps2);
492
493   fail_unless (gst_caps_is_equal (caps1, caps2),
494       "caps ('%s') is not equal to caps ('%s')", name1, name2);
495   g_free (name1);
496   g_free (name2);
497 }
498
499
500 /**
501  * gst_check_buffer_data:
502  * @buffer: buffer to compare
503  * @data: data to compare to
504  * @size: size of data to compare
505  *
506  * Compare the buffer contents with @data and @size.
507  */
508 void
509 gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size)
510 {
511   GstMapInfo info;
512
513   gst_buffer_map (buffer, &info, GST_MAP_READ);
514   GST_MEMDUMP ("Converted data", info.data, info.size);
515   GST_MEMDUMP ("Expected data", data, size);
516   if (memcmp (info.data, data, size) != 0) {
517     g_print ("\nConverted data:\n");
518     gst_util_dump_mem (info.data, info.size);
519     g_print ("\nExpected data:\n");
520     gst_util_dump_mem (data, size);
521   }
522   fail_unless (memcmp (info.data, data, size) == 0,
523       "buffer contents not equal");
524   gst_buffer_unmap (buffer, &info);
525 }
526
527 static gboolean
528 buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
529 {
530   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
531     GstCaps *event_caps;
532     GstCaps *expected_caps = gst_pad_get_element_private (pad);
533
534     gst_event_parse_caps (event, &event_caps);
535     fail_unless (gst_caps_is_fixed (expected_caps));
536     fail_unless (gst_caps_is_fixed (event_caps));
537     fail_unless (gst_caps_is_equal_fixed (event_caps, expected_caps));
538     gst_event_unref (event);
539     return TRUE;
540   }
541
542   return gst_pad_event_default (pad, noparent, event);
543 }
544
545 /**
546  * gst_check_element_push_buffer_list:
547  * @element_name: name of the element that needs to be created
548  * @buffer_in: (element-type GstBuffer) (transfer full): a list of buffers that needs to be
549  *  pushed to the element
550  * @buffer_out: (element-type GstBuffer) (transfer full): a list of buffers that we expect from
551  * the element
552  * @last_flow_return: the last buffer push needs to give this GstFlowReturn
553  *
554  * Create an @element with the factory with the name and push the buffers in
555  * @buffer_in to this element. The element should create the buffers equal to
556  * the buffers in @buffer_out. We only check the caps, size and the data of the
557  * buffers. This function unrefs the buffers in the two lists.
558  * The last_flow_return parameter indicates the expected flow return value from
559  * pushing the final buffer in the list.
560  * This can be used to set up a test which pushes some buffers and then an
561  * invalid buffer, when the final buffer is expected to fail, for example.
562  */
563 /* FIXME 0.11: rename this function now that there's GstBufferList? */
564 void
565 gst_check_element_push_buffer_list (const gchar * element_name,
566     GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
567     GstCaps * caps_out, GstFlowReturn last_flow_return)
568 {
569   GstElement *element;
570   GstPad *pad_peer;
571   GstPad *sink_pad = NULL;
572   GstPad *src_pad;
573   GstBuffer *buffer;
574
575   /* check that there are no buffers waiting */
576   gst_check_drop_buffers ();
577   /* create the element */
578   element = gst_check_setup_element (element_name);
579   fail_if (element == NULL, "failed to create the element '%s'", element_name);
580   fail_unless (GST_IS_ELEMENT (element), "the element is no element");
581   /* create the src pad */
582   buffer = GST_BUFFER (buffer_in->data);
583
584   fail_unless (GST_IS_BUFFER (buffer), "There should be a buffer in buffer_in");
585   src_pad = gst_pad_new ("src", GST_PAD_SRC);
586   if (caps_in) {
587     fail_unless (gst_caps_is_fixed (caps_in));
588     gst_pad_use_fixed_caps (src_pad);
589   }
590   /* activate the pad */
591   gst_pad_set_active (src_pad, TRUE);
592   GST_DEBUG ("src pad activated");
593   gst_check_setup_events (src_pad, element, caps_in, GST_FORMAT_BYTES);
594   pad_peer = gst_element_get_static_pad (element, "sink");
595   fail_if (pad_peer == NULL);
596   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
597       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
598   gst_object_unref (pad_peer);
599   /* don't create the sink_pad if there is no buffer_out list */
600   if (buffer_out != NULL) {
601
602     GST_DEBUG ("buffer out detected, creating the sink pad");
603     /* get the sink caps */
604     if (caps_out) {
605       gchar *temp;
606
607       fail_unless (gst_caps_is_fixed (caps_out));
608       temp = gst_caps_to_string (caps_out);
609
610       GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
611       g_free (temp);
612     }
613
614     /* get the sink pad */
615     sink_pad = gst_pad_new ("sink", GST_PAD_SINK);
616     fail_unless (GST_IS_PAD (sink_pad));
617     /* configure the sink pad */
618     gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
619     gst_pad_set_active (sink_pad, TRUE);
620     if (caps_out) {
621       gst_pad_set_element_private (sink_pad, caps_out);
622       gst_pad_set_event_function (sink_pad, buffer_event_function);
623     }
624     /* get the peer pad */
625     pad_peer = gst_element_get_static_pad (element, "src");
626     fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
627         "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
628     gst_object_unref (pad_peer);
629   }
630   fail_unless (gst_element_set_state (element,
631           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
632       "could not set to playing");
633   /* push all the buffers in the buffer_in list */
634   fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
635   while (buffer_in != NULL) {
636     GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);
637
638     fail_unless (GST_IS_BUFFER (next_buffer),
639         "data in buffer_in should be a buffer");
640     /* remove the buffer from the list */
641     buffer_in = g_list_remove (buffer_in, next_buffer);
642     if (buffer_in == NULL) {
643       fail_unless (gst_pad_push (src_pad, next_buffer) == last_flow_return,
644           "we expect something else from the last buffer");
645     } else {
646       fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
647           "Failed to push buffer in");
648     }
649   }
650   fail_unless (gst_element_set_state (element,
651           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
652   /* check that there is a buffer out */
653   fail_unless_equals_int (g_list_length (buffers), g_list_length (buffer_out));
654   while (buffers != NULL) {
655     GstBuffer *new = GST_BUFFER (buffers->data);
656     GstBuffer *orig = GST_BUFFER (buffer_out->data);
657     GstMapInfo newinfo, originfo;
658
659     gst_buffer_map (new, &newinfo, GST_MAP_READ);
660     gst_buffer_map (orig, &originfo, GST_MAP_READ);
661
662     GST_LOG ("orig buffer: size %" G_GSIZE_FORMAT, originfo.size);
663     GST_LOG ("new  buffer: size %" G_GSIZE_FORMAT, newinfo.size);
664     GST_MEMDUMP ("orig buffer", originfo.data, originfo.size);
665     GST_MEMDUMP ("new  buffer", newinfo.data, newinfo.size);
666
667     /* remove the buffers */
668     buffers = g_list_remove (buffers, new);
669     buffer_out = g_list_remove (buffer_out, orig);
670
671     fail_unless (originfo.size == newinfo.size,
672         "size of the buffers are not the same");
673     fail_unless (memcmp (originfo.data, newinfo.data, newinfo.size) == 0,
674         "data is not the same");
675 #if 0
676     gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
677 #endif
678
679     gst_buffer_unmap (orig, &originfo);
680     gst_buffer_unmap (new, &newinfo);
681
682     gst_buffer_unref (new);
683     gst_buffer_unref (orig);
684   }
685   /* teardown the element and pads */
686   gst_pad_set_active (src_pad, FALSE);
687   gst_check_teardown_src_pad (element);
688   gst_pad_set_active (sink_pad, FALSE);
689   gst_check_teardown_sink_pad (element);
690   gst_check_teardown_element (element);
691 }
692
693 /**
694  * gst_check_element_push_buffer:
695  * @element_name: name of the element that needs to be created
696  * @buffer_in: push this buffer to the element
697  * @buffer_out: compare the result with this buffer
698  *
699  * Create an @element with the factory with the name and push the
700  * @buffer_in to this element. The element should create one buffer
701  * and this will be compared with @buffer_out. We only check the caps
702  * and the data of the buffers. This function unrefs the buffers.
703  */
704 void
705 gst_check_element_push_buffer (const gchar * element_name,
706     GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
707     GstCaps * caps_out)
708 {
709   GList *in = NULL;
710   GList *out = NULL;
711
712   in = g_list_append (in, buffer_in);
713   out = g_list_append (out, buffer_out);
714
715   gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
716       GST_FLOW_OK);
717 }
718
719 void
720 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
721 {
722   if (have_abi_sizes) {
723     gboolean ok = TRUE;
724     gint i;
725
726     for (i = 0; list[i].name; i++) {
727       if (list[i].size != list[i].abi_size) {
728         ok = FALSE;
729         g_print ("sizeof(%s) is %d, expected %d\n",
730             list[i].name, list[i].size, list[i].abi_size);
731       }
732     }
733     fail_unless (ok, "failed ABI check");
734   } else {
735     const gchar *fn;
736
737     if ((fn = g_getenv ("GST_ABI"))) {
738       GError *err = NULL;
739       GString *s;
740       gint i;
741
742       s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
743       for (i = 0; list[i].name; i++) {
744         g_string_append_printf (s, "  {\"%s\", sizeof (%s), %d},\n",
745             list[i].name, list[i].name, list[i].size);
746       }
747       g_string_append (s, "  {NULL, 0, 0}\n");
748       g_string_append (s, "};\n");
749       if (!g_file_set_contents (fn, s->str, s->len, &err)) {
750         g_print ("%s", s->str);
751         g_printerr ("\nFailed to write ABI information: %s\n", err->message);
752       } else {
753         g_print ("\nWrote ABI information to '%s'.\n", fn);
754       }
755       g_string_free (s, TRUE);
756     } else {
757       g_print ("No structure size list was generated for this architecture.\n");
758       g_print ("Run with GST_ABI environment variable set to output header.\n");
759     }
760   }
761 }
762
763 gint
764 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
765 {
766   SRunner *sr;
767   gchar *xmlfilename = NULL;
768   gint nf;
769
770   sr = srunner_create (suite);
771
772   if (g_getenv ("GST_CHECK_XML")) {
773     /* how lucky we are to have __FILE__ end in .c */
774     xmlfilename = g_strdup_printf ("%sheck.xml", fname);
775
776     srunner_set_xml (sr, xmlfilename);
777   }
778
779   srunner_run_all (sr, CK_NORMAL);
780   nf = srunner_ntests_failed (sr);
781   g_free (xmlfilename);
782   srunner_free (sr);
783   return nf;
784 }
785
786 static gboolean
787 gst_check_have_checks_list (const gchar * env_var_name)
788 {
789   const gchar *env_val;
790
791   env_val = g_getenv (env_var_name);
792   return (env_val != NULL && *env_val != '\0');
793 }
794
795 static gboolean
796 gst_check_func_is_in_list (const gchar * env_var, const gchar * func_name)
797 {
798   const gchar *gst_checks;
799   gboolean res = FALSE;
800   gchar **funcs, **f;
801
802   gst_checks = g_getenv (env_var);
803
804   if (gst_checks == NULL || *gst_checks == '\0')
805     return FALSE;
806
807   /* only run specified functions */
808   funcs = g_strsplit (gst_checks, ",", -1);
809   for (f = funcs; f != NULL && *f != NULL; ++f) {
810     if (g_pattern_match_simple (*f, func_name)) {
811       res = TRUE;
812       break;
813     }
814   }
815   g_strfreev (funcs);
816   return res;
817 }
818
819 gboolean
820 _gst_check_run_test_func (const gchar * func_name)
821 {
822   /* if we have a whitelist, run it only if it's in the whitelist */
823   if (gst_check_have_checks_list ("GST_CHECKS"))
824     return gst_check_func_is_in_list ("GST_CHECKS", func_name);
825
826   /* if we have a blacklist, run it only if it's not in the blacklist */
827   if (gst_check_have_checks_list ("GST_CHECKS_IGNORE"))
828     return !gst_check_func_is_in_list ("GST_CHECKS_IGNORE", func_name);
829
830   /* no filter specified => run all checks */
831   return TRUE;
832 }
833
834 /**
835  * gst_check_setup_events_with_stream_id:
836  * @srcpad: The src #GstPad to push on
837  * @element: The #GstElement use to create the stream id
838  * @caps: (allow-none): #GstCaps in case caps event must be sent
839  * @format: The #GstFormat of the default segment to send
840  * @stream_id: A unique identifier for the stream
841  *
842  * Push stream-start, caps and segment event, which consist of the minimum
843  * required events to allow streaming. Caps is optional to allow raw src
844  * testing.
845  */
846 void
847 gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
848     GstCaps * caps, GstFormat format, const gchar * stream_id)
849 {
850   GstSegment segment;
851
852   gst_segment_init (&segment, format);
853
854   fail_unless (gst_pad_push_event (srcpad,
855           gst_event_new_stream_start (stream_id)));
856   if (caps)
857     fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
858   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&segment)));
859 }
860
861 /**
862  * gst_check_setup_events:
863  * @srcpad: The src #GstPad to push on
864  * @element: The #GstElement use to create the stream id
865  * @caps: (allow-none): #GstCaps in case caps event must be sent
866  * @format: The #GstFormat of the default segment to send
867  *
868  * Push stream-start, caps and segment event, which consist of the minimum
869  * required events to allow streaming. Caps is optional to allow raw src
870  * testing. If @element has more than one src or sink pad, use
871  * gst_check_setup_events_with_stream_id() instead.
872  */
873 void
874 gst_check_setup_events (GstPad * srcpad, GstElement * element,
875     GstCaps * caps, GstFormat format)
876 {
877   gchar *stream_id;
878
879   stream_id = gst_pad_create_stream_id (srcpad, element, NULL);
880   gst_check_setup_events_with_stream_id (srcpad, element, caps, format,
881       stream_id);
882   g_free (stream_id);
883 }