]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - libs/gst/check/gstcheck.c
docs: fix gtk-doc chunk for gst_check_element_push_buffer_list()
[glsdk/gstreamer0-10.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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, 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  */
31 #include "gstcheck.h"
33 GST_DEBUG_CATEGORY (check_debug);
35 /* logging function for tests
36  * a test uses g_message() to log a debug line
37  * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
38  * messages
39  */
41 gboolean _gst_check_threads_running = FALSE;
42 GList *thread_list = NULL;
43 GMutex *mutex;
44 GCond *start_cond;              /* used to notify main thread of thread startups */
45 GCond *sync_cond;               /* used to synchronize all threads and main thread */
47 GList *buffers = NULL;
48 GMutex *check_mutex = NULL;
49 GCond *check_cond = NULL;
51 /* FIXME 0.11: shouldn't _gst_check_debug be static? Not used anywhere */
52 gboolean _gst_check_debug = FALSE;
53 gboolean _gst_check_raised_critical = FALSE;
54 gboolean _gst_check_raised_warning = FALSE;
55 gboolean _gst_check_expecting_log = FALSE;
57 static void gst_check_log_message_func
58     (const gchar * log_domain, GLogLevelFlags log_level,
59     const gchar * message, gpointer user_data)
60 {
61   if (_gst_check_debug) {
62     g_print ("%s", message);
63   }
64 }
66 static void gst_check_log_critical_func
67     (const gchar * log_domain, GLogLevelFlags log_level,
68     const gchar * message, gpointer user_data)
69 {
70   if (!_gst_check_expecting_log) {
71     g_print ("\n\nUnexpected critical/warning: %s\n", message);
72     fail ("Unexpected critical/warning: %s", message);
73   }
75   if (_gst_check_debug) {
76     g_print ("\nExpected critical/warning: %s\n", message);
77   }
79   if (log_level & G_LOG_LEVEL_CRITICAL)
80     _gst_check_raised_critical = TRUE;
81   if (log_level & G_LOG_LEVEL_WARNING)
82     _gst_check_raised_warning = TRUE;
83 }
85 /* initialize GStreamer testing */
86 void
87 gst_check_init (int *argc, char **argv[])
88 {
89   gst_init (argc, argv);
91   GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
93   if (g_getenv ("GST_TEST_DEBUG"))
94     _gst_check_debug = TRUE;
96   g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
97       NULL);
98   g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
99       gst_check_log_critical_func, NULL);
100   g_log_set_handler ("GStreamer", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
101       gst_check_log_critical_func, NULL);
102   g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
103       gst_check_log_critical_func, NULL);
104   g_log_set_handler ("Gst-Phonon", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
105       gst_check_log_critical_func, NULL);
107   check_cond = g_cond_new ();
108   check_mutex = g_mutex_new ();
111 /* message checking */
112 void
113 gst_check_message_error (GstMessage * message, GstMessageType type,
114     GQuark domain, gint code)
116   GError *error;
117   gchar *debug;
119   fail_unless (GST_MESSAGE_TYPE (message) == type,
120       "message is of type %s instead of expected type %s",
121       gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
122       gst_message_type_get_name (type));
123   gst_message_parse_error (message, &error, &debug);
124   fail_unless_equals_int (error->domain, domain);
125   fail_unless_equals_int (error->code, code);
126   g_error_free (error);
127   g_free (debug);
130 /* helper functions */
131 GstFlowReturn
132 gst_check_chain_func (GstPad * pad, GstBuffer * buffer)
134   GST_DEBUG ("chain_func: received buffer %p", buffer);
135   buffers = g_list_append (buffers, buffer);
137   g_mutex_lock (check_mutex);
138   g_cond_signal (check_cond);
139   g_mutex_unlock (check_mutex);
141   return GST_FLOW_OK;
144 /* setup an element for a filter test with mysrcpad and mysinkpad */
145 GstElement *
146 gst_check_setup_element (const gchar * factory)
148   GstElement *element;
150   GST_DEBUG ("setup_element");
152   element = gst_element_factory_make (factory, factory);
153   fail_if (element == NULL, "Could not create a '%s' element", factory);
154   ASSERT_OBJECT_REFCOUNT (element, factory, 1);
155   return element;
158 void
159 gst_check_teardown_element (GstElement * element)
161   GST_DEBUG ("teardown_element");
163   fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
164       GST_STATE_CHANGE_SUCCESS, "could not set to null");
165   ASSERT_OBJECT_REFCOUNT (element, "element", 1);
166   gst_object_unref (element);
169 /* FIXME: set_caps isn't that useful
170  */
171 GstPad *
172 gst_check_setup_src_pad (GstElement * element,
173     GstStaticPadTemplate * template, GstCaps * caps)
175   GstPad *srcpad;
177   srcpad = gst_check_setup_src_pad_by_name (element, template, "sink");
178   if (caps)
179     fail_unless (gst_pad_set_caps (srcpad, caps), "could not set caps on pad");
180   return srcpad;
183 GstPad *
184 gst_check_setup_src_pad_by_name (GstElement * element,
185     GstStaticPadTemplate * template, gchar * name)
187   GstPad *srcpad, *sinkpad;
189   /* sending pad */
190   srcpad = gst_pad_new_from_static_template (template, "src");
191   GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
192   fail_if (srcpad == NULL, "Could not create a srcpad");
193   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
195   sinkpad = gst_element_get_static_pad (element, name);
196   if (sinkpad == NULL)
197     sinkpad = gst_element_get_request_pad (element, name);
198   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
199       GST_ELEMENT_NAME (element));
200   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
201   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
202       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
203   gst_object_unref (sinkpad);   /* because we got it higher up */
204   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
206   return srcpad;
209 void
210 gst_check_teardown_pad_by_name (GstElement * element, gchar * name)
212   GstPad *pad_peer, *pad_element;
214   /* clean up floating src pad */
215   pad_element = gst_element_get_static_pad (element, name);
216   ASSERT_OBJECT_REFCOUNT (pad_element, "pad", 2);
217   pad_peer = gst_pad_get_peer (pad_element);
219   if (pad_peer) {
220     if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
221       gst_pad_unlink (pad_peer, pad_element);
222     else
223       gst_pad_unlink (pad_element, pad_peer);
225     /* caps could have been set, make sure they get unset */
226     gst_pad_set_caps (pad_peer, NULL);
227   }
229   /* pad refs held by both creator and this function (through _get) */
230   ASSERT_OBJECT_REFCOUNT (pad_element, "element pad_element", 2);
231   gst_object_unref (pad_element);
232   /* one more ref is held by element itself */
234   if (pad_peer) {
235     /* pad refs held by both creator and this function (through _get_peer) */
236     ASSERT_OBJECT_REFCOUNT (pad_peer, "check pad_peer", 2);
237     gst_object_unref (pad_peer);
238     gst_object_unref (pad_peer);
239   }
242 void
243 gst_check_teardown_src_pad (GstElement * element)
245   gst_check_teardown_pad_by_name (element, "sink");
248 /* FIXME: set_caps isn't that useful; might want to check if fixed,
249  * then use set_use_fixed or somesuch */
250 GstPad *
251 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * template,
252     GstCaps * caps)
254   GstPad *sinkpad;
256   sinkpad = gst_check_setup_sink_pad_by_name (element, template, "src");
257   if (caps)
258     fail_unless (gst_pad_set_caps (sinkpad, caps), "Could not set pad caps");
259   return sinkpad;
262 GstPad *
263 gst_check_setup_sink_pad_by_name (GstElement * element,
264     GstStaticPadTemplate * template, gchar * name)
266   GstPad *srcpad, *sinkpad;
268   /* receiving pad */
269   sinkpad = gst_pad_new_from_static_template (template, "sink");
270   GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
271   fail_if (sinkpad == NULL, "Could not create a sinkpad");
273   srcpad = gst_element_get_static_pad (element, name);
274   if (srcpad == NULL)
275     srcpad = gst_element_get_request_pad (element, name);
276   fail_if (srcpad == NULL, "Could not get source pad from %s",
277       GST_ELEMENT_NAME (element));
278   gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
280   GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
281   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
282       "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
283   gst_object_unref (srcpad);    /* because we got it higher up */
284   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
286   GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
287   return sinkpad;
290 void
291 gst_check_teardown_sink_pad (GstElement * element)
293   gst_check_teardown_pad_by_name (element, "src");
296 /**
297  * gst_check_drop_buffers:
298  *
299  * Unref and remove all buffers that are in the global @buffers GList,
300  * emptying the list.
301  *
302  * Since: 0.10.18
303  */
304 void
305 gst_check_drop_buffers (void)
307   GstBuffer *temp_buffer;
309   while (g_list_length (buffers)) {
310     temp_buffer = GST_BUFFER (buffers->data);
311     gst_buffer_unref (temp_buffer);
312     buffers = g_list_delete_link (buffers, buffers);
313   }
316 /**
317  * gst_check_caps_equal:
318  * @caps1: first caps to compare
319  * @caps2: second caps to compare
320  *
321  * Compare two caps with gst_caps_is_equal and fail unless they are
322  * equal.
323  *
324  * Since: 0.10.18
325  */
326 void
327 gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
329   gchar *name1 = gst_caps_to_string (caps1);
330   gchar *name2 = gst_caps_to_string (caps2);
332   fail_unless (gst_caps_is_equal (caps1, caps2),
333       "caps ('%s') is not equal to caps ('%s')", name1, name2);
334   g_free (name1);
335   g_free (name2);
338 /**
339  * gst_check_element_push_buffer_list:
340  * @element_name: name of the element that needs to be created
341  * @buffer_in: a list of buffers that needs to be puched to the element
342  * @buffer_out: a list of buffers that we expect from the element
343  * @last_flow_return: the last buffer push needs to give this GstFlowReturn
344  *
345  * Create an @element with the factory with the name and push the buffers in
346  * @buffer_in to this element. The element should create the buffers equal to
347  * the buffers in @buffer_out. We only check the caps, size and the data of the
348  * buffers. This function unrefs the buffers in the two lists.
349  * The last_flow_return parameter indicates the expected flow return value from
350  * pushing the final buffer in the list.
351  * This can be used to set up a test which pushes some buffers and then an
352  * invalid buffer, when the final buffer is expected to fail, for example.
353  * 
354  * Since: 0.10.18
355  */
356 /* FIXME 0.11: rename this function now that there's GstBufferList? */
357 void
358 gst_check_element_push_buffer_list (const gchar * element_name,
359     GList * buffer_in, GList * buffer_out, GstFlowReturn last_flow_return)
361   GstCaps *sink_caps;
362   GstCaps *src_caps = NULL;
363   GstElement *element;
364   GstPad *pad_peer;
365   GstPad *sink_pad = NULL;
366   GstPad *src_pad;
367   GstBuffer *buffer;
369   /* check that there are no buffers waiting */
370   gst_check_drop_buffers ();
371   /* create the element */
372   element = gst_check_setup_element (element_name);
373   fail_if (element == NULL, "failed to create the element '%s'", element_name);
374   fail_unless (GST_IS_ELEMENT (element), "the element is no element");
375   /* create the src pad */
376   buffer = GST_BUFFER (buffer_in->data);
378   fail_unless (GST_IS_BUFFER (buffer), "There should be a buffer in buffer_in");
379   src_caps = GST_BUFFER_CAPS (buffer);
380   src_pad = gst_pad_new (NULL, GST_PAD_SRC);
381   gst_pad_set_caps (src_pad, src_caps);
382   pad_peer = gst_element_get_static_pad (element, "sink");
383   fail_if (pad_peer == NULL);
384   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
385       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
386   gst_object_unref (pad_peer);
387   /* activate the pad */
388   gst_pad_set_active (src_pad, TRUE);
389   GST_DEBUG ("src pad activated");
390   /* don't create the sink_pad if there is no buffer_out list */
391   if (buffer_out != NULL) {
392     gchar *temp;
394     GST_DEBUG ("buffer out detected, creating the sink pad");
395     /* get the sink caps */
396     sink_caps = GST_BUFFER_CAPS (GST_BUFFER (buffer_out->data));
397     fail_unless (GST_IS_CAPS (sink_caps), "buffer out don't have caps");
398     temp = gst_caps_to_string (sink_caps);
400     GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
401     g_free (temp);
402     fail_unless (gst_caps_is_fixed (sink_caps), "we need fixed caps");
403     /* get the sink pad */
404     sink_pad = gst_pad_new (NULL, GST_PAD_SINK);
405     fail_unless (GST_IS_PAD (sink_pad));
406     gst_pad_set_caps (sink_pad, sink_caps);
407     /* get the peer pad */
408     pad_peer = gst_element_get_static_pad (element, "src");
409     fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
410         "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
411     gst_object_unref (pad_peer);
412     /* configure the sink pad */
413     gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
414     gst_pad_set_active (sink_pad, TRUE);
415   }
416   fail_unless (gst_element_set_state (element,
417           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
418       "could not set to playing");
419   /* push all the buffers in the buffer_in list */
420   fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
421   while (g_list_length (buffer_in) > 0) {
422     GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);
424     fail_unless (GST_IS_BUFFER (next_buffer),
425         "data in buffer_in should be a buffer");
426     /* remove the buffer from the list */
427     buffer_in = g_list_remove (buffer_in, next_buffer);
428     if (g_list_length (buffer_in) == 0) {
429       fail_unless (gst_pad_push (src_pad, next_buffer) == last_flow_return,
430           "we expect something else from the last buffer");
431     } else {
432       fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
433           "Failed to push buffer in");
434     }
435   }
436   fail_unless (gst_element_set_state (element,
437           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
438   /* check that there is a buffer out */
439   fail_unless (g_list_length (buffers) == g_list_length (buffer_out),
440       "We expected %d buffers, but there are %d buffers",
441       g_list_length (buffer_out), g_list_length (buffers));
442   while (g_list_length (buffers) > 0) {
443     GstBuffer *new = GST_BUFFER (buffers->data);
444     GstBuffer *orig = GST_BUFFER (buffer_out->data);
446     /* remove the buffers */
447     buffers = g_list_remove (buffers, new);
448     buffer_out = g_list_remove (buffer_out, orig);
449     fail_unless (GST_BUFFER_SIZE (orig) == GST_BUFFER_SIZE (new),
450         "size of the buffers are not the same");
451     fail_unless (memcmp (GST_BUFFER_DATA (orig), GST_BUFFER_DATA (new),
452             GST_BUFFER_SIZE (new)) == 0, "data is not the same");
453     gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
454     gst_buffer_unref (new);
455     gst_buffer_unref (orig);
456   }
457   /* teardown the element and pads */
458   gst_pad_set_active (src_pad, FALSE);
459   gst_check_teardown_src_pad (element);
460   gst_pad_set_active (sink_pad, FALSE);
461   gst_check_teardown_sink_pad (element);
462   gst_check_teardown_element (element);
465 /**
466  * gst_check_element_push_buffer:
467  * @element_name: name of the element that needs to be created
468  * @buffer_in: push this buffer to the element
469  * @buffer_out: compare the result with this buffer
470  *
471  * Create an @element with the factory with the name and push the
472  * @buffer_in to this element. The element should create one buffer
473  * and this will be compared with @buffer_out. We only check the caps
474  * and the data of the buffers. This function unrefs the buffers.
475  * 
476  * Since: 0.10.18
477  */
478 void
479 gst_check_element_push_buffer (const gchar * element_name,
480     GstBuffer * buffer_in, GstBuffer * buffer_out)
482   GList *in = NULL;
483   GList *out = NULL;
485   in = g_list_append (in, buffer_in);
486   out = g_list_append (out, buffer_out);
488   gst_check_element_push_buffer_list (element_name, in, out, GST_FLOW_OK);
491 void
492 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
494   if (have_abi_sizes) {
495     gboolean ok = TRUE;
496     gint i;
498     for (i = 0; list[i].name; i++) {
499       if (list[i].size != list[i].abi_size) {
500         ok = FALSE;
501         g_print ("sizeof(%s) is %d, expected %d\n",
502             list[i].name, list[i].size, list[i].abi_size);
503       }
504     }
505     fail_unless (ok, "failed ABI check");
506   } else {
507     const gchar *fn;
509     if ((fn = g_getenv ("GST_ABI"))) {
510       GError *err = NULL;
511       GString *s;
512       gint i;
514       s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
515       for (i = 0; list[i].name; i++) {
516         g_string_append_printf (s, "  {\"%s\", sizeof (%s), %d},\n",
517             list[i].name, list[i].name, list[i].size);
518       }
519       g_string_append (s, "  {NULL, 0, 0}\n");
520       g_string_append (s, "};\n");
521       if (!g_file_set_contents (fn, s->str, s->len, &err)) {
522         g_print ("%s", s->str);
523         g_printerr ("\nFailed to write ABI information: %s\n", err->message);
524       } else {
525         g_print ("\nWrote ABI information to '%s'.\n", fn);
526       }
527       g_string_free (s, TRUE);
528     } else {
529       g_print ("No structure size list was generated for this architecture.\n");
530       g_print ("Run with GST_ABI environment variable set to output header.\n");
531     }
532   }
535 gint
536 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
538   gint nf;
540   SRunner *sr = srunner_create (suite);
542   if (g_getenv ("GST_CHECK_XML")) {
543     /* how lucky we are to have __FILE__ end in .c */
544     gchar *xmlfilename = g_strdup_printf ("%sheck.xml", fname);
546     srunner_set_xml (sr, xmlfilename);
547   }
549   srunner_run_all (sr, CK_NORMAL);
550   nf = srunner_ntests_failed (sr);
551   srunner_free (sr);
552   return nf;
555 gboolean
556 _gst_check_run_test_func (const gchar * func_name)
558   const gchar *gst_checks;
559   gboolean res = FALSE;
560   gchar **funcs, **f;
562   gst_checks = g_getenv ("GST_CHECKS");
564   /* no filter specified => run all checks */
565   if (gst_checks == NULL || *gst_checks == '\0')
566     return TRUE;
568   /* only run specified functions */
569   funcs = g_strsplit (gst_checks, ",", -1);
570   for (f = funcs; f != NULL && *f != NULL; ++f) {
571     if (strcmp (*f, func_name) == 0) {
572       res = TRUE;
573       break;
574     }
575   }
576   g_strfreev (funcs);
577   return res;