• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @title: GstCheck
26  * @short_description: Common code for GStreamer unit tests
27  *
28  * These macros and functions are for internal use of the unit tests found
29  * inside the 'check' directories of various GStreamer packages.
30  *
31  * One notable feature is that one can use the environment variables GST_CHECKS
32  * and GST_CHECKS_IGNORE to select which tests to run or skip. Both variables
33  * can contain a comma separated list of test name globs (e.g. test_*).
34  */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include "gstcheck.h"
40 
41 GST_DEBUG_CATEGORY (check_debug);
42 
43 /* logging function for tests
44  * a test uses g_message() to log a debug line
45  * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
46  * messages
47  */
48 
49 gboolean _gst_check_threads_running = FALSE;
50 GList *thread_list = NULL;
51 GMutex mutex;
52 GCond start_cond;               /* used to notify main thread of thread startups */
53 GCond sync_cond;                /* used to synchronize all threads and main thread */
54 
55 GList *buffers = NULL;
56 GMutex check_mutex;
57 GCond check_cond;
58 
59 /* FIXME 2.0: shouldn't _gst_check_debug be static? Not used anywhere */
60 gboolean _gst_check_debug = FALSE;
61 gboolean _gst_check_raised_critical = FALSE;
62 gboolean _gst_check_raised_warning = FALSE;
63 gboolean _gst_check_expecting_log = FALSE;
64 gboolean _gst_check_list_tests = FALSE;
65 static GQueue _gst_check_log_filters = G_QUEUE_INIT;
66 static GMutex _gst_check_log_filters_mutex;
67 
68 struct _GstCheckLogFilter
69 {
70   gchar *log_domain;
71   GLogLevelFlags log_level;
72   GRegex *regex;
73   GstCheckLogFilterFunc func;
74   gpointer user_data;
75   GDestroyNotify destroy;
76 };
77 
78 
79 static gboolean
gst_check_match_log_filter(const GstCheckLogFilter * filter,const gchar * log_domain,GLogLevelFlags log_level,const gchar * message)80 gst_check_match_log_filter (const GstCheckLogFilter * filter,
81     const gchar * log_domain, GLogLevelFlags log_level, const gchar * message)
82 {
83   if (g_strcmp0 (log_domain, filter->log_domain) != 0)
84     return FALSE;
85 
86   if ((log_level & filter->log_level) == 0)
87     return FALSE;
88 
89   if (!g_regex_match (filter->regex, message, 0, NULL))
90     return FALSE;
91 
92   return TRUE;
93 }
94 
95 static GstCheckLogFilter *
gst_check_alloc_log_filter(const gchar * log_domain,GLogLevelFlags log_level,GRegex * regex,GstCheckLogFilterFunc func,gpointer user_data,GDestroyNotify destroy_data)96 gst_check_alloc_log_filter (const gchar * log_domain, GLogLevelFlags log_level,
97     GRegex * regex, GstCheckLogFilterFunc func, gpointer user_data,
98     GDestroyNotify destroy_data)
99 {
100   GstCheckLogFilter *filter;
101 
102   filter = g_slice_new (GstCheckLogFilter);
103   filter->log_domain = g_strdup (log_domain);
104   filter->log_level = log_level;
105   filter->regex = regex;
106   filter->func = func;
107   filter->user_data = user_data;
108   filter->destroy = destroy_data;
109 
110   return filter;
111 }
112 
113 static void
gst_check_free_log_filter(GstCheckLogFilter * filter)114 gst_check_free_log_filter (GstCheckLogFilter * filter)
115 {
116   if (!filter)
117     return;
118 
119   g_free (filter->log_domain);
120   g_regex_unref (filter->regex);
121   if (filter->destroy)
122     filter->destroy (filter->user_data);
123   g_slice_free (GstCheckLogFilter, filter);
124 }
125 
126 
127 /**
128  * gst_check_add_log_filter: (skip)
129  * @log_domain: the log domain of the message
130  * @log_level: the log level of the message
131  * @regex: (transfer full): a #GRegex to match the message
132  * @func: the function to call for matching messages
133  * @user_data: the user data to pass to @func
134  * @destroy_data: #GDestroyNotify for @user_data
135  *
136  * Add a callback @func to be called for all log messages that matches
137  * @log_domain, @log_level and @regex. If @func is NULL the
138  * matching logs will be silently discarded by GstCheck.
139  *
140  * MT safe.
141  *
142  * Returns: A filter that can be passed to @gst_check_remove_log_filter.
143  *
144  * Since: 1.12
145  **/
146 GstCheckLogFilter *
gst_check_add_log_filter(const gchar * log_domain,GLogLevelFlags log_level,GRegex * regex,GstCheckLogFilterFunc func,gpointer user_data,GDestroyNotify destroy_data)147 gst_check_add_log_filter (const gchar * log_domain, GLogLevelFlags log_level,
148     GRegex * regex, GstCheckLogFilterFunc func, gpointer user_data,
149     GDestroyNotify destroy_data)
150 {
151   GstCheckLogFilter *filter;
152 
153   g_return_val_if_fail (regex != NULL, NULL);
154 
155   filter = gst_check_alloc_log_filter (log_domain, log_level, regex,
156       func, user_data, destroy_data);
157   g_mutex_lock (&_gst_check_log_filters_mutex);
158   g_queue_push_tail (&_gst_check_log_filters, filter);
159   g_mutex_unlock (&_gst_check_log_filters_mutex);
160 
161   return filter;
162 }
163 
164 /**
165  * gst_check_remove_log_filter:
166  * @filter: Filter returned by @gst_check_add_log_filter
167  *
168  * Remove a filter that has been added by @gst_check_add_log_filter.
169  *
170  * MT safe.
171  *
172  * Since: 1.12
173  **/
174 void
gst_check_remove_log_filter(GstCheckLogFilter * filter)175 gst_check_remove_log_filter (GstCheckLogFilter * filter)
176 {
177   g_mutex_lock (&_gst_check_log_filters_mutex);
178   g_queue_remove (&_gst_check_log_filters, filter);
179   gst_check_free_log_filter (filter);
180   g_mutex_unlock (&_gst_check_log_filters_mutex);
181 }
182 
183 /**
184  * gst_check_clear_log_filter:
185  *
186  * Clear all filters added by @gst_check_add_log_filter.
187  *
188  * MT safe.
189  *
190  * Since: 1.12
191  **/
192 void
gst_check_clear_log_filter(void)193 gst_check_clear_log_filter (void)
194 {
195   g_mutex_lock (&_gst_check_log_filters_mutex);
196   g_queue_foreach (&_gst_check_log_filters,
197       (GFunc) gst_check_free_log_filter, NULL);
198   g_queue_clear (&_gst_check_log_filters);
199   g_mutex_unlock (&_gst_check_log_filters_mutex);
200 }
201 
202 typedef struct
203 {
204   const gchar *domain;
205   const gchar *message;
206   GLogLevelFlags level;
207   gboolean discard;
208 } LogFilterApplyData;
209 
210 static void
gst_check_apply_log_filter(GstCheckLogFilter * filter,LogFilterApplyData * data)211 gst_check_apply_log_filter (GstCheckLogFilter * filter,
212     LogFilterApplyData * data)
213 {
214   if (gst_check_match_log_filter (filter, data->domain, data->level,
215           data->message)) {
216     if (filter->func)
217       data->discard |= filter->func (data->domain, data->level,
218           data->message, filter->user_data);
219     else
220       data->discard = TRUE;
221   }
222 }
223 
224 static gboolean
gst_check_filter_log_filter(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message)225 gst_check_filter_log_filter (const gchar * log_domain,
226     GLogLevelFlags log_level, const gchar * message)
227 {
228   LogFilterApplyData data;
229 
230   data.domain = log_domain;
231   data.level = log_level;
232   data.message = message;
233   data.discard = FALSE;
234 
235   g_mutex_lock (&_gst_check_log_filters_mutex);
236   g_queue_foreach (&_gst_check_log_filters, (GFunc) gst_check_apply_log_filter,
237       &data);
238   g_mutex_unlock (&_gst_check_log_filters_mutex);
239 
240   if (data.discard)
241     GST_DEBUG ("Discarding message: %s", message);
242 
243   return data.discard;
244 }
245 
246 static gboolean
gst_check_log_fatal_func(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)247 gst_check_log_fatal_func (const gchar * log_domain, GLogLevelFlags log_level,
248     const gchar * message, gpointer user_data)
249 {
250   if (gst_check_filter_log_filter (log_domain, log_level, message))
251     return FALSE;
252 
253   return TRUE;
254 }
255 
256 
gst_check_log_message_func(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)257 static void gst_check_log_message_func
258     (const gchar * log_domain, GLogLevelFlags log_level,
259     const gchar * message, gpointer user_data)
260 {
261   if (gst_check_filter_log_filter (log_domain, log_level, message))
262     return;
263 
264   if (_gst_check_debug) {
265     g_print ("%s\n", message);
266   }
267 }
268 
gst_check_log_critical_func(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)269 static void gst_check_log_critical_func
270     (const gchar * log_domain, GLogLevelFlags log_level,
271     const gchar * message, gpointer user_data)
272 {
273   if (gst_check_filter_log_filter (log_domain, log_level, message))
274     return;
275 
276   if (!_gst_check_expecting_log) {
277     gchar *trace;
278 
279     g_print ("\n\nUnexpected critical/warning: %s\n", message);
280 
281     trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
282     if (trace) {
283       g_print ("\nStack trace:\n%s\n", trace);
284       g_free (trace);
285     }
286     fail ("Unexpected critical/warning: %s", message);
287   }
288 
289   if (_gst_check_debug) {
290     g_print ("\nExpected critical/warning: %s\n", message);
291   }
292 
293   if (log_level & G_LOG_LEVEL_CRITICAL)
294     _gst_check_raised_critical = TRUE;
295   if (log_level & G_LOG_LEVEL_WARNING)
296     _gst_check_raised_warning = TRUE;
297 }
298 
299 static gint
sort_plugins(GstPlugin * a,GstPlugin * b)300 sort_plugins (GstPlugin * a, GstPlugin * b)
301 {
302   int ret;
303 
304   ret = strcmp (gst_plugin_get_source (a), gst_plugin_get_source (b));
305   if (ret == 0)
306     ret = strcmp (gst_plugin_get_name (a), gst_plugin_get_name (b));
307   return ret;
308 }
309 
310 static void
print_plugins(void)311 print_plugins (void)
312 {
313   GList *plugins, *l;
314 
315   plugins = gst_registry_get_plugin_list (gst_registry_get ());
316   plugins = g_list_sort (plugins, (GCompareFunc) sort_plugins);
317   for (l = plugins; l != NULL; l = l->next) {
318     GstPlugin *plugin = GST_PLUGIN (l->data);
319 
320     if (strcmp (gst_plugin_get_source (plugin), "BLACKLIST") != 0) {
321       GST_LOG ("%20s@%s", gst_plugin_get_name (plugin),
322           GST_STR_NULL (gst_plugin_get_filename (plugin)));
323     }
324   }
325   gst_plugin_list_free (plugins);
326 }
327 
328 static void
gst_check_deinit(void)329 gst_check_deinit (void)
330 {
331   gst_deinit ();
332   gst_check_clear_log_filter ();
333 }
334 
335 static const gchar *log_domains[] = {
336   "GLib-GObject",
337   "GLib-GIO",
338   "GLib",
339   "GStreamer-AdaptiveDemux",
340   "GStreamer-Allocators",
341   "GStreamer-App",
342   "GStreamer-Audio",
343   "GStreamer-AudioBad",
344   "GStreamer-Base",
345   "GStreamer-BaseCameraBinSrc",
346   "GStreamer-Check",
347   "GStreamer-CodecParsers",
348   "GStreamer-Codecs",
349   "GStreamer-Controller",
350   "GStreamer-D3D11",
351   "GStreamer",
352   "GStreamer-FFT",
353   "GStreamer-GL",
354   "GStreamer-InsertBin",
355   "GStreamer-ISOFF",
356   "GStreamer-MpegTS",
357   "GStreamer-Net",
358   "GStreamer-OpenCV",
359   "GStreamer-PBUtils",
360   "GStreamer-Photography",
361   "GStreamer-Play",
362   "GStreamer-Player",
363   "GStreamer-RIFF",
364   "GStreamer-RTP",
365   "GStreamer-RTSP",
366   "GStreamer-RTSP-Server",
367   "GStreamer-SCTP",
368   "GStreamer-SDP",
369   "GStreamer-Tag",
370   "GStreamer-Transcoder",
371   "GStreamer-UriDownloader",
372   "GStreamer-VA",
373   "GStreamer-Video",
374   "GStreamer-Vulkan",
375   "GStreamer-Vulkan",
376   "GStreamer-Wayland",
377   "GStreamer-WebRTC",
378   "GStreamer-WinRT",
379 };
380 
381 /* gst_check_init:
382  * @argc: (inout) (allow-none): pointer to application's argc
383  * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
384  *
385  * Initialize GStreamer testing
386  *
387  * NOTE: Needs to be called before creating the testsuite
388  * so that the tests can be listed.
389  * */
390 void
gst_check_init(int * argc,char ** argv[])391 gst_check_init (int *argc, char **argv[])
392 {
393   guint timeout_multiplier = 1;
394   GOptionContext *ctx;
395   GError *err = NULL;
396   GOptionEntry options[] = {
397     {"list-tests", 'l', 0, G_OPTION_ARG_NONE, &_gst_check_list_tests,
398         "List tests present in the testsuite", NULL},
399     {NULL}
400   };
401   guint i;
402 
403   ctx = g_option_context_new ("gst-check");
404   g_option_context_add_main_entries (ctx, options, NULL);
405   g_option_context_add_group (ctx, gst_init_get_option_group ());
406 
407   if (!g_option_context_parse (ctx, argc, argv, &err)) {
408     if (err)
409       g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
410     else
411       g_printerr ("Error initializing: Unknown error!\n");
412     g_clear_error (&err);
413   }
414   g_option_context_free (ctx);
415 
416   GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
417 
418   if (atexit (gst_check_deinit) != 0) {
419     GST_ERROR ("failed to set gst_check_deinit as exit function");
420   }
421 
422   if (g_getenv ("GST_TEST_DEBUG"))
423     _gst_check_debug = TRUE;
424 
425   g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
426       NULL);
427   g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
428       gst_check_log_critical_func, NULL);
429 
430   for (i = 0; i < G_N_ELEMENTS (log_domains); ++i) {
431     g_log_set_handler (log_domains[i],
432         G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
433         gst_check_log_critical_func, NULL);
434   }
435 
436   g_test_log_set_fatal_handler (gst_check_log_fatal_func, NULL);
437 
438   print_plugins ();
439 
440 #ifdef TARGET_CPU
441   GST_INFO ("target CPU: %s", TARGET_CPU);
442 #endif
443 
444 #ifdef HAVE_CPU_ARM
445   timeout_multiplier = 10;
446 #endif
447 
448   if (timeout_multiplier > 1) {
449     const gchar *tmult = g_getenv ("CK_TIMEOUT_MULTIPLIER");
450 
451     if (tmult == NULL) {
452       gchar num_str[32];
453 
454       g_snprintf (num_str, sizeof (num_str), "%d", timeout_multiplier);
455       GST_INFO ("slow CPU, setting CK_TIMEOUT_MULTIPLIER to %s", num_str);
456       g_setenv ("CK_TIMEOUT_MULTIPLIER", num_str, TRUE);
457     } else {
458       GST_INFO ("CK_TIMEOUT_MULTIPLIER already set to '%s'", tmult);
459     }
460   }
461 }
462 
463 /* message checking */
464 void
gst_check_message_error(GstMessage * message,GstMessageType type,GQuark domain,gint code)465 gst_check_message_error (GstMessage * message, GstMessageType type,
466     GQuark domain, gint code)
467 {
468   GError *error;
469   gchar *debug;
470 
471   fail_unless (GST_MESSAGE_TYPE (message) == type,
472       "message is of type %s instead of expected type %s",
473       gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
474       gst_message_type_get_name (type));
475   gst_message_parse_error (message, &error, &debug);
476   fail_unless_equals_int (error->domain, domain);
477   fail_unless_equals_int (error->code, code);
478   g_error_free (error);
479   g_free (debug);
480 }
481 
482 /* helper functions */
483 /**
484  * gst_check_chain_func:
485  *
486  * A fake chain function that appends the buffer to the internal list of
487  * buffers.
488  */
489 GstFlowReturn
gst_check_chain_func(GstPad * pad,GstObject * parent,GstBuffer * buffer)490 gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
491 {
492   GST_DEBUG_OBJECT (pad, "chain_func: received buffer %p", buffer);
493   buffers = g_list_append (buffers, buffer);
494 
495   g_mutex_lock (&check_mutex);
496   g_cond_signal (&check_cond);
497   g_mutex_unlock (&check_mutex);
498 
499   return GST_FLOW_OK;
500 }
501 
502 /**
503  * gst_check_setup_element:
504  * @factory: factory
505  *
506  * setup an element for a filter test with mysrcpad and mysinkpad
507  *
508  * Returns: (transfer full): a new element
509  */
510 GstElement *
gst_check_setup_element(const gchar * factory)511 gst_check_setup_element (const gchar * factory)
512 {
513   GstElement *element;
514 
515   GST_DEBUG ("setup_element");
516 
517   element = gst_element_factory_make (factory, factory);
518   fail_if (element == NULL, "Could not create a '%s' element", factory);
519   ASSERT_OBJECT_REFCOUNT (element, factory, 1);
520   return element;
521 }
522 
523 void
gst_check_teardown_element(GstElement * element)524 gst_check_teardown_element (GstElement * element)
525 {
526   GST_DEBUG ("teardown_element");
527 
528   fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
529       GST_STATE_CHANGE_SUCCESS, "could not set to null");
530   ASSERT_OBJECT_REFCOUNT (element, "element", 1);
531   gst_object_unref (element);
532 }
533 
534 /**
535  * gst_check_setup_src_pad:
536  * @element: element to setup pad on
537  * @tmpl: pad template
538  *
539  * Does the same as #gst_check_setup_src_pad_by_name with the <emphasis> name </emphasis> parameter equal to "sink".
540  *
541  * Returns: (transfer full): A new pad that can be used to inject data on @element
542  */
543 GstPad *
gst_check_setup_src_pad(GstElement * element,GstStaticPadTemplate * tmpl)544 gst_check_setup_src_pad (GstElement * element, GstStaticPadTemplate * tmpl)
545 {
546   return gst_check_setup_src_pad_by_name (element, tmpl, "sink");
547 }
548 
549 /**
550  * gst_check_setup_src_pad_by_name:
551  * @element: element to setup src pad on
552  * @tmpl: pad template
553  * @name: Name of the @element sink pad that will be linked to the src pad that will be setup
554  *
555  * 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).
556  * Before using the src pad to push data on @element you need to call #gst_check_setup_events on the created src pad.
557  *
558  * Example of how to push a buffer on @element:
559  *
560  * |[<!-- language="C" -->
561  * static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
562  * GST_PAD_SINK,
563  * GST_PAD_ALWAYS,
564  * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
565  * );
566  * static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
567  * GST_PAD_SRC,
568  * GST_PAD_ALWAYS,
569  * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
570  * );
571  *
572  * GstElement * element = gst_check_setup_element ("element");
573  * GstPad * mysrcpad = gst_check_setup_src_pad (element, &srctemplate);
574  * GstPad * mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
575  *
576  * gst_pad_set_active (mysrcpad, TRUE);
577  * gst_pad_set_active (mysinkpad, TRUE);
578  * fail_unless (gst_element_set_state (element, GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
579  *
580  * GstCaps * caps = gst_caps_from_string (YOUR_DESIRED_SINK_CAPS);
581  * gst_check_setup_events (mysrcpad, element, caps, GST_FORMAT_TIME);
582  * gst_caps_unref (caps);
583  *
584  * fail_unless (gst_pad_push (mysrcpad, gst_buffer_new_and_alloc(2)) == GST_FLOW_OK);
585  * ]|
586  *
587  * For very simple input/output test scenarios checkout #gst_check_element_push_buffer_list and #gst_check_element_push_buffer.
588  *
589  * Returns: (transfer full): A new pad that can be used to inject data on @element
590  */
591 GstPad *
gst_check_setup_src_pad_by_name(GstElement * element,GstStaticPadTemplate * tmpl,const gchar * name)592 gst_check_setup_src_pad_by_name (GstElement * element,
593     GstStaticPadTemplate * tmpl, const gchar * name)
594 {
595   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
596   GstPad *srcpad;
597 
598   srcpad = gst_check_setup_src_pad_by_name_from_template (element, ptmpl, name);
599 
600   gst_object_unref (ptmpl);
601 
602   return srcpad;
603 }
604 
605 /**
606  * gst_check_setup_src_pad_from_template:
607  * @element: element to setup pad on
608  * @tmpl: pad template
609  *
610  * Returns: (transfer full): a new pad
611  *
612  * Since: 1.4
613  */
614 GstPad *
gst_check_setup_src_pad_from_template(GstElement * element,GstPadTemplate * tmpl)615 gst_check_setup_src_pad_from_template (GstElement * element,
616     GstPadTemplate * tmpl)
617 {
618   return gst_check_setup_src_pad_by_name_from_template (element, tmpl, "sink");
619 }
620 
621 /**
622  * gst_check_setup_src_pad_by_name_from_template:
623  * @element: element to setup pad on
624  * @tmpl: pad template
625  * @name: name
626  *
627  * Returns: (transfer full): a new pad
628  *
629  * Since: 1.4
630  */
631 GstPad *
gst_check_setup_src_pad_by_name_from_template(GstElement * element,GstPadTemplate * tmpl,const gchar * name)632 gst_check_setup_src_pad_by_name_from_template (GstElement * element,
633     GstPadTemplate * tmpl, const gchar * name)
634 {
635   GstPad *srcpad, *sinkpad;
636 
637   /* sending pad */
638   srcpad = gst_pad_new_from_template (tmpl, "src");
639   GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
640   fail_if (srcpad == NULL, "Could not create a srcpad");
641   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
642 
643   sinkpad = gst_element_get_static_pad (element, name);
644   if (sinkpad == NULL)
645     sinkpad = gst_element_request_pad_simple (element, name);
646   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
647       GST_ELEMENT_NAME (element));
648   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
649       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
650   gst_object_unref (sinkpad);   /* because we got it higher up */
651 
652   return srcpad;
653 }
654 
655 void
gst_check_teardown_pad_by_name(GstElement * element,const gchar * name)656 gst_check_teardown_pad_by_name (GstElement * element, const gchar * name)
657 {
658   GstPad *pad_peer, *pad_element;
659 
660   /* clean up floating src pad */
661   pad_element = gst_element_get_static_pad (element, name);
662   /* We don't check the refcount here since there *might* be
663    * a pad cache holding an extra reference on pad_element.
664    * To get to a state where no pad cache will exist,
665    * we first unlink that pad. */
666   pad_peer = gst_pad_get_peer (pad_element);
667 
668   if (pad_peer) {
669     if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
670       gst_pad_unlink (pad_peer, pad_element);
671     else
672       gst_pad_unlink (pad_element, pad_peer);
673   }
674 
675   gst_object_unref (pad_element);
676 
677   if (pad_peer) {
678     gst_object_unref (pad_peer);
679     gst_object_unref (pad_peer);
680   }
681 }
682 
683 void
gst_check_teardown_src_pad(GstElement * element)684 gst_check_teardown_src_pad (GstElement * element)
685 {
686   gst_check_teardown_pad_by_name (element, "sink");
687 }
688 
689 /**
690  * gst_check_setup_sink_pad:
691  * @element: element to setup pad on
692  * @tmpl: pad template
693  *
694  * Does the same as #gst_check_setup_sink_pad_by_name with the <emphasis> name </emphasis> parameter equal to "src".
695  *
696  * Returns: (transfer full): a new pad that can be used to check the output of @element
697  */
698 GstPad *
gst_check_setup_sink_pad(GstElement * element,GstStaticPadTemplate * tmpl)699 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * tmpl)
700 {
701   return gst_check_setup_sink_pad_by_name (element, tmpl, "src");
702 }
703 
704 /**
705  * gst_check_setup_sink_pad_by_name:
706  * @element: element to setup pad on
707  * @tmpl: pad template
708  * @name: Name of the @element src pad that will be linked to the sink pad that will be setup
709  *
710  * Creates a new sink pad (based on the given @tmpl) and links it to the given @element src pad
711  * (the pad that matches the given @name).
712  * You can set event/chain/query functions on this pad to check the output of the @element.
713  *
714  * Returns: (transfer full): a new pad that can be used to check the output of @element
715  */
716 GstPad *
gst_check_setup_sink_pad_by_name(GstElement * element,GstStaticPadTemplate * tmpl,const gchar * name)717 gst_check_setup_sink_pad_by_name (GstElement * element,
718     GstStaticPadTemplate * tmpl, const gchar * name)
719 {
720   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
721   GstPad *sinkpad;
722 
723   sinkpad =
724       gst_check_setup_sink_pad_by_name_from_template (element, ptmpl, name);
725 
726   gst_object_unref (ptmpl);
727 
728   return sinkpad;
729 }
730 
731 /**
732  * gst_check_setup_sink_pad_from_template:
733  * @element: element to setup pad on
734  * @tmpl: pad template
735  *
736  * Returns: (transfer full): a new pad
737  *
738  * Since: 1.4
739  */
740 GstPad *
gst_check_setup_sink_pad_from_template(GstElement * element,GstPadTemplate * tmpl)741 gst_check_setup_sink_pad_from_template (GstElement * element,
742     GstPadTemplate * tmpl)
743 {
744   return gst_check_setup_sink_pad_by_name_from_template (element, tmpl, "src");
745 }
746 
747 /**
748  * gst_check_setup_sink_pad_by_name_from_template:
749  * @element: element to setup pad on
750  * @tmpl: pad template
751  * @name: name
752  *
753  * Returns: (transfer full): a new pad
754  *
755  * Since: 1.4
756  */
757 GstPad *
gst_check_setup_sink_pad_by_name_from_template(GstElement * element,GstPadTemplate * tmpl,const gchar * name)758 gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
759     GstPadTemplate * tmpl, const gchar * name)
760 {
761   GstPad *srcpad, *sinkpad;
762 
763   /* receiving pad */
764   sinkpad = gst_pad_new_from_template (tmpl, "sink");
765   GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
766   fail_if (sinkpad == NULL, "Could not create a sinkpad");
767 
768   srcpad = gst_element_get_static_pad (element, name);
769   if (srcpad == NULL)
770     srcpad = gst_element_request_pad_simple (element, name);
771   fail_if (srcpad == NULL, "Could not get source pad from %s",
772       GST_ELEMENT_NAME (element));
773   gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
774 
775   GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
776   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
777       "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
778   gst_object_unref (srcpad);    /* because we got it higher up */
779 
780   GST_DEBUG_OBJECT (element, "set up srcpad");
781   return sinkpad;
782 }
783 
784 void
gst_check_teardown_sink_pad(GstElement * element)785 gst_check_teardown_sink_pad (GstElement * element)
786 {
787   gst_check_teardown_pad_by_name (element, "src");
788 }
789 
790 /**
791  * gst_check_drop_buffers:
792  *
793  * Unref and remove all buffers that are in the global @buffers GList,
794  * emptying the list.
795  */
796 void
gst_check_drop_buffers(void)797 gst_check_drop_buffers (void)
798 {
799   while (buffers != NULL) {
800     gst_buffer_unref (GST_BUFFER (buffers->data));
801     buffers = g_list_delete_link (buffers, buffers);
802   }
803 }
804 
805 /**
806  * gst_check_caps_equal:
807  * @caps1: first caps to compare
808  * @caps2: second caps to compare
809  *
810  * Compare two caps with gst_caps_is_equal and fail unless they are
811  * equal.
812  */
813 void
gst_check_caps_equal(GstCaps * caps1,GstCaps * caps2)814 gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
815 {
816   gchar *name1 = gst_caps_to_string (caps1);
817   gchar *name2 = gst_caps_to_string (caps2);
818 
819   fail_unless (gst_caps_is_equal (caps1, caps2),
820       "caps ('%s') is not equal to caps ('%s')", name1, name2);
821   g_free (name1);
822   g_free (name2);
823 }
824 
825 
826 /**
827  * gst_check_buffer_data:
828  * @buffer: buffer to compare
829  * @data: data to compare to
830  * @size: size of data to compare
831  *
832  * Compare the buffer contents with @data and @size.
833  */
834 void
gst_check_buffer_data(GstBuffer * buffer,gconstpointer data,gsize size)835 gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size)
836 {
837   GstMapInfo info;
838 
839   fail_unless (gst_buffer_map (buffer, &info, GST_MAP_READ));
840   GST_MEMDUMP ("Converted data", info.data, info.size);
841   GST_MEMDUMP ("Expected data", data, size);
842   if (info.size != size) {
843     fail ("buffer sizes not equal: expected %" G_GSIZE_FORMAT " got %"
844         G_GSIZE_FORMAT, size, info.size);
845   }
846   if (memcmp (info.data, data, size) != 0) {
847     g_print ("\nConverted data:\n");
848     gst_util_dump_mem (info.data, info.size);
849     g_print ("\nExpected data:\n");
850     gst_util_dump_mem (data, size);
851     fail ("buffer contents not equal");
852   }
853   gst_buffer_unmap (buffer, &info);
854 }
855 
856 static gboolean
buffer_event_function(GstPad * pad,GstObject * noparent,GstEvent * event)857 buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
858 {
859   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
860     GstCaps *event_caps;
861     GstCaps *expected_caps = gst_pad_get_element_private (pad);
862 
863     gst_event_parse_caps (event, &event_caps);
864     fail_unless (gst_caps_is_fixed (expected_caps));
865     fail_unless (gst_caps_is_fixed (event_caps));
866     fail_unless (gst_caps_is_equal_fixed (event_caps, expected_caps));
867     gst_event_unref (event);
868     return TRUE;
869   }
870 
871   return gst_pad_event_default (pad, noparent, event);
872 }
873 
874 /**
875  * gst_check_element_push_buffer_list:
876  * @element_name: name of the element that needs to be created
877  * @buffer_in: (element-type GstBuffer) (transfer full): a list of buffers that needs to be
878  *  pushed to the element
879  * @caps_in: the #GstCaps expected of the sinkpad of the element
880  * @buffer_out: (element-type GstBuffer) (transfer full): a list of buffers that we expect from
881  * the element
882  * @caps_out: the #GstCaps expected of the srcpad of the element
883  * @last_flow_return: the last buffer push needs to give this GstFlowReturn
884  *
885  * Create an element using the factory providing the @element_name and push the
886  * buffers in @buffer_in to this element. The element should create the buffers
887  * equal to the buffers in @buffer_out. We only check the size and the data of
888  * the buffers. This function unrefs the buffers in the two lists.
889  * The last_flow_return parameter indicates the expected flow return value from
890  * pushing the final buffer in the list.
891  * This can be used to set up a test which pushes some buffers and then an
892  * invalid buffer, when the final buffer is expected to fail, for example.
893  */
894 /* FIXME 2.0: rename this function now that there's GstBufferList? */
895 void
gst_check_element_push_buffer_list(const gchar * element_name,GList * buffer_in,GstCaps * caps_in,GList * buffer_out,GstCaps * caps_out,GstFlowReturn last_flow_return)896 gst_check_element_push_buffer_list (const gchar * element_name,
897     GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
898     GstCaps * caps_out, GstFlowReturn last_flow_return)
899 {
900   GstElement *element;
901   GstPad *pad_peer;
902   GstPad *sink_pad = NULL;
903   GstPad *src_pad;
904   GstBuffer *buffer;
905 
906   /* check that there are no buffers waiting */
907   gst_check_drop_buffers ();
908   /* create the element */
909   element = gst_check_setup_element (element_name);
910   fail_if (element == NULL, "failed to create the element '%s'", element_name);
911   fail_unless (GST_IS_ELEMENT (element), "the element is no element");
912   /* create the src pad */
913   buffer = GST_BUFFER (buffer_in->data);
914 
915   fail_unless (GST_IS_BUFFER (buffer), "There should be a buffer in buffer_in");
916   src_pad = gst_pad_new ("src", GST_PAD_SRC);
917   if (caps_in) {
918     fail_unless (gst_caps_is_fixed (caps_in));
919     gst_pad_use_fixed_caps (src_pad);
920   }
921   /* activate the pad */
922   gst_pad_set_active (src_pad, TRUE);
923   GST_DEBUG ("src pad activated");
924   gst_check_setup_events (src_pad, element, caps_in, GST_FORMAT_BYTES);
925   pad_peer = gst_element_get_static_pad (element, "sink");
926   fail_if (pad_peer == NULL);
927   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
928       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
929   gst_object_unref (pad_peer);
930   /* don't create the sink_pad if there is no buffer_out list */
931   if (buffer_out != NULL) {
932 
933     GST_DEBUG ("buffer out detected, creating the sink pad");
934     /* get the sink caps */
935     if (caps_out) {
936       gchar *temp;
937 
938       fail_unless (gst_caps_is_fixed (caps_out));
939       temp = gst_caps_to_string (caps_out);
940 
941       GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
942       g_free (temp);
943     }
944 
945     /* get the sink pad */
946     sink_pad = gst_pad_new ("sink", GST_PAD_SINK);
947     fail_unless (GST_IS_PAD (sink_pad));
948     /* configure the sink pad */
949     gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
950     gst_pad_set_active (sink_pad, TRUE);
951     if (caps_out) {
952       gst_pad_set_element_private (sink_pad, caps_out);
953       gst_pad_set_event_function (sink_pad, buffer_event_function);
954     }
955     /* get the peer pad */
956     pad_peer = gst_element_get_static_pad (element, "src");
957     fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
958         "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
959     gst_object_unref (pad_peer);
960   }
961   fail_unless (gst_element_set_state (element,
962           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
963       "could not set to playing");
964   /* push all the buffers in the buffer_in list */
965   fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
966   while (buffer_in != NULL) {
967     GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);
968 
969     fail_unless (GST_IS_BUFFER (next_buffer),
970         "data in buffer_in should be a buffer");
971     /* remove the buffer from the list */
972     buffer_in = g_list_remove (buffer_in, next_buffer);
973     if (buffer_in == NULL) {
974       fail_unless (gst_pad_push (src_pad, next_buffer) == last_flow_return,
975           "we expect something else from the last buffer");
976     } else {
977       fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
978           "Failed to push buffer in");
979     }
980   }
981   fail_unless (gst_element_set_state (element,
982           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
983   /* check that there is a buffer out */
984   fail_unless_equals_int (g_list_length (buffers), g_list_length (buffer_out));
985   while (buffers != NULL) {
986     GstBuffer *new = GST_BUFFER (buffers->data);
987     GstBuffer *orig = GST_BUFFER (buffer_out->data);
988     GstMapInfo newinfo, originfo;
989 
990     fail_unless (gst_buffer_map (new, &newinfo, GST_MAP_READ));
991     fail_unless (gst_buffer_map (orig, &originfo, GST_MAP_READ));
992 
993     GST_LOG ("orig buffer: size %" G_GSIZE_FORMAT, originfo.size);
994     GST_LOG ("new  buffer: size %" G_GSIZE_FORMAT, newinfo.size);
995     GST_MEMDUMP ("orig buffer", originfo.data, originfo.size);
996     GST_MEMDUMP ("new  buffer", newinfo.data, newinfo.size);
997 
998     /* remove the buffers */
999     buffers = g_list_remove (buffers, new);
1000     buffer_out = g_list_remove (buffer_out, orig);
1001 
1002     fail_unless (originfo.size == newinfo.size,
1003         "size of the buffers are not the same");
1004     fail_unless (memcmp (originfo.data, newinfo.data, newinfo.size) == 0,
1005         "data is not the same");
1006 #if 0
1007     gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
1008 #endif
1009 
1010     gst_buffer_unmap (orig, &originfo);
1011     gst_buffer_unmap (new, &newinfo);
1012 
1013     gst_buffer_unref (new);
1014     gst_buffer_unref (orig);
1015   }
1016   /* teardown the element and pads */
1017   gst_pad_set_active (src_pad, FALSE);
1018   gst_check_teardown_src_pad (element);
1019   gst_pad_set_active (sink_pad, FALSE);
1020   gst_check_teardown_sink_pad (element);
1021   gst_check_teardown_element (element);
1022 }
1023 
1024 /**
1025  * gst_check_element_push_buffer:
1026  * @element_name: name of the element that needs to be created
1027  * @buffer_in: push this buffer to the element
1028  * @caps_in: the #GstCaps expected of the sinkpad of the element
1029  * @buffer_out: compare the result with this buffer
1030  * @caps_out: the #GstCaps expected of the srcpad of the element
1031  *
1032  * Create an element using the factory providing the @element_name and
1033  * push the @buffer_in to this element. The element should create one buffer
1034  * and this will be compared with @buffer_out. We only check the caps
1035  * and the data of the buffers. This function unrefs the buffers.
1036  */
1037 void
gst_check_element_push_buffer(const gchar * element_name,GstBuffer * buffer_in,GstCaps * caps_in,GstBuffer * buffer_out,GstCaps * caps_out)1038 gst_check_element_push_buffer (const gchar * element_name,
1039     GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
1040     GstCaps * caps_out)
1041 {
1042   GList *in = NULL;
1043   GList *out = NULL;
1044 
1045   in = g_list_append (in, buffer_in);
1046   out = g_list_append (out, buffer_out);
1047 
1048   gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
1049       GST_FLOW_OK);
1050 }
1051 
1052 /**
1053  * gst_check_abi_list:
1054  * @list: A list of GstCheckABIStruct to be verified
1055  * @have_abi_sizes: Whether there is a reference ABI size already specified,
1056  * if it is %FALSE and the `GST_ABI` environment variable is set, usable code
1057  * for @list will be printed.
1058  *
1059  * Verifies that reference values and current values are equals in @list.
1060  */
1061 void
gst_check_abi_list(GstCheckABIStruct list[],gboolean have_abi_sizes)1062 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
1063 {
1064   if (have_abi_sizes) {
1065     gboolean ok = TRUE;
1066     gint i;
1067 
1068     for (i = 0; list[i].name; i++) {
1069       if (list[i].size != list[i].abi_size) {
1070         ok = FALSE;
1071         g_print ("sizeof(%s) is %d, expected %d\n",
1072             list[i].name, list[i].size, list[i].abi_size);
1073       }
1074     }
1075     fail_unless (ok, "failed ABI check");
1076   } else {
1077     const gchar *fn;
1078 
1079     if ((fn = g_getenv ("GST_ABI"))) {
1080       GError *err = NULL;
1081       GString *s;
1082       gint i;
1083 
1084       s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
1085       for (i = 0; list[i].name; i++) {
1086         g_string_append_printf (s, "  {\"%s\", sizeof (%s), %d},\n",
1087             list[i].name, list[i].name, list[i].size);
1088       }
1089       g_string_append (s, "  {NULL, 0, 0}\n");
1090       g_string_append (s, "};\n");
1091       if (!g_file_set_contents (fn, s->str, s->len, &err)) {
1092         g_print ("%s", s->str);
1093         g_printerr ("\nFailed to write ABI information: %s\n", err->message);
1094         g_clear_error (&err);
1095       } else {
1096         g_print ("\nWrote ABI information to '%s'.\n", fn);
1097       }
1098       g_string_free (s, TRUE);
1099     } else {
1100       g_print ("No structure size list was generated for this architecture.\n");
1101       g_print ("Run with GST_ABI environment variable set to output header.\n");
1102     }
1103   }
1104 }
1105 
1106 /**
1107  * gst_check_run_suite: (skip)
1108  * @suite: the check test suite
1109  * @name: name
1110  * @fname: file name
1111  *
1112  * Returns: number of failed tests
1113  */
1114 gint
gst_check_run_suite(Suite * suite,const gchar * name,const gchar * fname)1115 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
1116 {
1117   SRunner *sr;
1118   gchar *xmlfilename = NULL;
1119   gint nf;
1120   GTimer *timer;
1121 
1122   sr = srunner_create (suite);
1123 
1124   if (g_getenv ("GST_CHECK_XML")) {
1125     /* how lucky we are to have __FILE__ end in .c */
1126     xmlfilename = g_strdup_printf ("%sheck.xml", fname);
1127 
1128     srunner_set_xml (sr, xmlfilename);
1129   }
1130 
1131   timer = g_timer_new ();
1132   srunner_run_all (sr, CK_NORMAL);
1133   nf = srunner_ntests_failed (sr);
1134   g_print ("Check suite %s ran in %.3fs (tests failed: %d)\n",
1135       name, g_timer_elapsed (timer, NULL), nf);
1136   g_timer_destroy (timer);
1137   g_free (xmlfilename);
1138   srunner_free (sr);
1139   g_thread_pool_stop_unused_threads ();
1140   return nf;
1141 }
1142 
1143 static gboolean
gst_check_have_checks_list(const gchar * env_var_name)1144 gst_check_have_checks_list (const gchar * env_var_name)
1145 {
1146   const gchar *env_val;
1147 
1148   env_val = g_getenv (env_var_name);
1149   return (env_val != NULL && *env_val != '\0');
1150 }
1151 
1152 static gboolean
gst_check_func_is_in_list(const gchar * env_var,const gchar * func_name)1153 gst_check_func_is_in_list (const gchar * env_var, const gchar * func_name)
1154 {
1155   const gchar *gst_checks;
1156   gboolean res = FALSE;
1157   gchar **funcs, **f;
1158 
1159   gst_checks = g_getenv (env_var);
1160 
1161   if (gst_checks == NULL || *gst_checks == '\0')
1162     return FALSE;
1163 
1164   /* only run specified functions */
1165   funcs = g_strsplit (gst_checks, ",", -1);
1166   for (f = funcs; f != NULL && *f != NULL; ++f) {
1167     if (g_pattern_match_simple (*f, func_name)) {
1168       res = TRUE;
1169       break;
1170     }
1171   }
1172   g_strfreev (funcs);
1173   return res;
1174 }
1175 
1176 gboolean
_gst_check_run_test_func(const gchar * func_name)1177 _gst_check_run_test_func (const gchar * func_name)
1178 {
1179   /* if we have a whitelist, run it only if it's in the whitelist */
1180   if (gst_check_have_checks_list ("GST_CHECKS"))
1181     return gst_check_func_is_in_list ("GST_CHECKS", func_name);
1182 
1183   /* if we have a blacklist, run it only if it's not in the blacklist */
1184   if (gst_check_have_checks_list ("GST_CHECKS_IGNORE"))
1185     return !gst_check_func_is_in_list ("GST_CHECKS_IGNORE", func_name);
1186 
1187   /* no filter specified => run all checks */
1188   return TRUE;
1189 }
1190 
1191 /**
1192  * gst_check_setup_events_with_stream_id:
1193  * @srcpad: The src #GstPad to push on
1194  * @element: The #GstElement use to create the stream id
1195  * @caps: (allow-none): #GstCaps in case caps event must be sent
1196  * @format: The #GstFormat of the default segment to send
1197  * @stream_id: A unique identifier for the stream
1198  *
1199  * Push stream-start, caps and segment event, which consist of the minimum
1200  * required events to allow streaming. Caps is optional to allow raw src
1201  * testing.
1202  */
1203 void
gst_check_setup_events_with_stream_id(GstPad * srcpad,GstElement * element,GstCaps * caps,GstFormat format,const gchar * stream_id)1204 gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
1205     GstCaps * caps, GstFormat format, const gchar * stream_id)
1206 {
1207   GstSegment segment;
1208 
1209   gst_segment_init (&segment, format);
1210 
1211   fail_unless (gst_pad_push_event (srcpad,
1212           gst_event_new_stream_start (stream_id)));
1213   if (caps)
1214     fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
1215   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&segment)));
1216 }
1217 
1218 /**
1219  * gst_check_setup_events:
1220  * @srcpad: The src #GstPad to push on
1221  * @element: The #GstElement use to create the stream id
1222  * @caps: (allow-none): #GstCaps in case caps event must be sent
1223  * @format: The #GstFormat of the default segment to send
1224  *
1225  * Push stream-start, caps and segment event, which consist of the minimum
1226  * required events to allow streaming. Caps is optional to allow raw src
1227  * testing. If @element has more than one src or sink pad, use
1228  * gst_check_setup_events_with_stream_id() instead.
1229  */
1230 void
gst_check_setup_events(GstPad * srcpad,GstElement * element,GstCaps * caps,GstFormat format)1231 gst_check_setup_events (GstPad * srcpad, GstElement * element,
1232     GstCaps * caps, GstFormat format)
1233 {
1234   gchar *stream_id;
1235 
1236   stream_id = gst_pad_create_stream_id (srcpad, element, NULL);
1237   gst_check_setup_events_with_stream_id (srcpad, element, caps, format,
1238       stream_id);
1239   g_free (stream_id);
1240 }
1241 
1242 typedef struct _DestroyedObjectStruct
1243 {
1244   GObject *object;
1245   gboolean destroyed;
1246 } DestroyedObjectStruct;
1247 
1248 static void
weak_notify(DestroyedObjectStruct * destroyed,GObject ** object)1249 weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
1250 {
1251   destroyed->destroyed = TRUE;
1252 }
1253 
1254 /**
1255  * gst_check_objects_destroyed_on_unref:
1256  * @object_to_unref: The #GObject to unref
1257  * @first_object: (allow-none): The first object that should be destroyed as a
1258  * concequence of unrefing @object_to_unref.
1259  * @... : Additional object that should have been destroyed.
1260  *
1261  * Unrefs @object_to_unref and checks that is has properly been
1262  * destroyed, also checks that the other objects passed in
1263  * parameter have been destroyed as a concequence of
1264  * unrefing @object_to_unref. Last variable argument should be NULL.
1265  *
1266  * Since: 1.6
1267  */
1268 void
gst_check_objects_destroyed_on_unref(gpointer object_to_unref,gpointer first_object,...)1269 gst_check_objects_destroyed_on_unref (gpointer object_to_unref,
1270     gpointer first_object, ...)
1271 {
1272   GObject *object;
1273   GList *objs = NULL, *tmp;
1274   DestroyedObjectStruct *destroyed = g_slice_new0 (DestroyedObjectStruct);
1275 
1276   destroyed->object = object_to_unref;
1277   g_object_weak_ref (object_to_unref, (GWeakNotify) weak_notify, destroyed);
1278   objs = g_list_prepend (objs, destroyed);
1279 
1280   if (first_object) {
1281     va_list varargs;
1282 
1283     object = first_object;
1284 
1285     va_start (varargs, first_object);
1286     while (object) {
1287       destroyed = g_slice_new0 (DestroyedObjectStruct);
1288       destroyed->object = object;
1289       g_object_weak_ref (object, (GWeakNotify) weak_notify, destroyed);
1290       objs = g_list_prepend (objs, destroyed);
1291       object = va_arg (varargs, GObject *);
1292     }
1293     va_end (varargs);
1294   }
1295   gst_object_unref (object_to_unref);
1296 
1297   for (tmp = objs; tmp; tmp = tmp->next) {
1298     DestroyedObjectStruct *destroyed = tmp->data;
1299 
1300     if (!destroyed->destroyed) {
1301       fail_unless (destroyed->destroyed,
1302           "%s_%p is not destroyed, %d refcounts left!",
1303           GST_IS_OBJECT (destroyed->
1304               object) ? GST_OBJECT_NAME (destroyed->object) :
1305           G_OBJECT_TYPE_NAME (destroyed), destroyed->object,
1306           destroyed->object->ref_count);
1307     }
1308     g_slice_free (DestroyedObjectStruct, tmp->data);
1309   }
1310   g_list_free (objs);
1311 }
1312 
1313 /**
1314  * gst_check_object_destroyed_on_unref:
1315  * @object_to_unref: The #GObject to unref
1316  *
1317  * Unrefs @object_to_unref and checks that is has properly been
1318  * destroyed.
1319  *
1320  * Since: 1.6
1321  */
1322 void
gst_check_object_destroyed_on_unref(gpointer object_to_unref)1323 gst_check_object_destroyed_on_unref (gpointer object_to_unref)
1324 {
1325   gst_check_objects_destroyed_on_unref (object_to_unref, NULL, NULL);
1326 }
1327 
1328 /* For ABI compatibility with GStreamer < 1.5 */
1329 /* *INDENT-OFF* */
1330 GST_CHECK_API void
1331 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1332 G_GNUC_PRINTF (4, 5);
1333 /* *INDENT-ON* */
1334 
1335 void
_fail_unless(int result,const char * file,int line,const char * expr,...)1336 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1337 {
1338   gchar *msg;
1339   va_list args;
1340 
1341   if (result) {
1342     _mark_point (file, line);
1343     return;
1344   }
1345 
1346   va_start (args, expr);
1347   msg = g_strdup_vprintf (expr, args);
1348   va_end (args);
1349 
1350   _ck_assert_failed (file, line, msg, NULL);
1351   g_free (msg);
1352 }
1353