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 /* gst_check_init:
336  * @argc: (inout) (allow-none): pointer to application's argc
337  * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
338  *
339  * Initialize GStreamer testing
340  *
341  * NOTE: Needs to be called before creating the testsuite
342  * so that the tests can be listed.
343  * */
344 void
gst_check_init(int * argc,char ** argv[])345 gst_check_init (int *argc, char **argv[])
346 {
347   guint timeout_multiplier = 1;
348   GOptionContext *ctx;
349   GError *err = NULL;
350   GOptionEntry options[] = {
351     {"list-tests", 'l', 0, G_OPTION_ARG_NONE, &_gst_check_list_tests,
352         "List tests present in the testsuite", NULL},
353     {NULL}
354   };
355 
356   ctx = g_option_context_new ("gst-check");
357   g_option_context_add_main_entries (ctx, options, NULL);
358   g_option_context_add_group (ctx, gst_init_get_option_group ());
359 
360   if (!g_option_context_parse (ctx, argc, argv, &err)) {
361     if (err)
362       g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
363     else
364       g_printerr ("Error initializing: Unknown error!\n");
365     g_clear_error (&err);
366   }
367   g_option_context_free (ctx);
368 
369   GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");
370 
371   if (atexit (gst_check_deinit) != 0) {
372     GST_ERROR ("failed to set gst_check_deinit as exit function");
373   }
374 
375   if (g_getenv ("GST_TEST_DEBUG"))
376     _gst_check_debug = TRUE;
377 
378   g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
379       NULL);
380   g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
381       gst_check_log_critical_func, NULL);
382   g_log_set_handler ("GStreamer", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
383       gst_check_log_critical_func, NULL);
384   g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
385       gst_check_log_critical_func, NULL);
386   g_log_set_handler ("GLib-GIO", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
387       gst_check_log_critical_func, NULL);
388   g_log_set_handler ("GLib", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
389       gst_check_log_critical_func, NULL);
390   g_test_log_set_fatal_handler (gst_check_log_fatal_func, NULL);
391 
392   print_plugins ();
393 
394 #ifdef TARGET_CPU
395   GST_INFO ("target CPU: %s", TARGET_CPU);
396 #endif
397 
398 #ifdef HAVE_CPU_ARM
399   timeout_multiplier = 10;
400 #endif
401 
402   if (timeout_multiplier > 1) {
403     const gchar *tmult = g_getenv ("CK_TIMEOUT_MULTIPLIER");
404 
405     if (tmult == NULL) {
406       gchar num_str[32];
407 
408       g_snprintf (num_str, sizeof (num_str), "%d", timeout_multiplier);
409       GST_INFO ("slow CPU, setting CK_TIMEOUT_MULTIPLIER to %s", num_str);
410       g_setenv ("CK_TIMEOUT_MULTIPLIER", num_str, TRUE);
411     } else {
412       GST_INFO ("CK_TIMEOUT_MULTIPLIER already set to '%s'", tmult);
413     }
414   }
415 }
416 
417 /* message checking */
418 void
gst_check_message_error(GstMessage * message,GstMessageType type,GQuark domain,gint code)419 gst_check_message_error (GstMessage * message, GstMessageType type,
420     GQuark domain, gint code)
421 {
422   GError *error;
423   gchar *debug;
424 
425   fail_unless (GST_MESSAGE_TYPE (message) == type,
426       "message is of type %s instead of expected type %s",
427       gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
428       gst_message_type_get_name (type));
429   gst_message_parse_error (message, &error, &debug);
430   fail_unless_equals_int (error->domain, domain);
431   fail_unless_equals_int (error->code, code);
432   g_error_free (error);
433   g_free (debug);
434 }
435 
436 /* helper functions */
437 GstFlowReturn
gst_check_chain_func(GstPad * pad,GstObject * parent,GstBuffer * buffer)438 gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
439 {
440   GST_DEBUG_OBJECT (pad, "chain_func: received buffer %p", buffer);
441   buffers = g_list_append (buffers, buffer);
442 
443   g_mutex_lock (&check_mutex);
444   g_cond_signal (&check_cond);
445   g_mutex_unlock (&check_mutex);
446 
447   return GST_FLOW_OK;
448 }
449 
450 /**
451  * gst_check_setup_element:
452  * @factory: factory
453  *
454  * setup an element for a filter test with mysrcpad and mysinkpad
455  *
456  * Returns: (transfer full): a new element
457  */
458 GstElement *
gst_check_setup_element(const gchar * factory)459 gst_check_setup_element (const gchar * factory)
460 {
461   GstElement *element;
462 
463   GST_DEBUG ("setup_element");
464 
465   element = gst_element_factory_make (factory, factory);
466   fail_if (element == NULL, "Could not create a '%s' element", factory);
467   ASSERT_OBJECT_REFCOUNT (element, factory, 1);
468   return element;
469 }
470 
471 void
gst_check_teardown_element(GstElement * element)472 gst_check_teardown_element (GstElement * element)
473 {
474   GST_DEBUG ("teardown_element");
475 
476   fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
477       GST_STATE_CHANGE_SUCCESS, "could not set to null");
478   ASSERT_OBJECT_REFCOUNT (element, "element", 1);
479   gst_object_unref (element);
480 }
481 
482 /**
483  * gst_check_setup_src_pad:
484  * @element: element to setup pad on
485  * @tmpl: pad template
486  *
487  * Does the same as #gst_check_setup_src_pad_by_name with the <emphasis> name </emphasis> parameter equal to "sink".
488  *
489  * Returns: (transfer full): A new pad that can be used to inject data on @element
490  */
491 GstPad *
gst_check_setup_src_pad(GstElement * element,GstStaticPadTemplate * tmpl)492 gst_check_setup_src_pad (GstElement * element, GstStaticPadTemplate * tmpl)
493 {
494   return gst_check_setup_src_pad_by_name (element, tmpl, "sink");
495 }
496 
497 /**
498  * gst_check_setup_src_pad_by_name:
499  * @element: element to setup src pad on
500  * @tmpl: pad template
501  * @name: Name of the @element sink pad that will be linked to the src pad that will be setup
502  *
503  * 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).
504  * Before using the src pad to push data on @element you need to call #gst_check_setup_events on the created src pad.
505  *
506  * Example of how to push a buffer on @element:
507  *
508  * |[<!-- language="C" -->
509  * static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
510  * GST_PAD_SINK,
511  * GST_PAD_ALWAYS,
512  * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
513  * );
514  * static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
515  * GST_PAD_SRC,
516  * GST_PAD_ALWAYS,
517  * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
518  * );
519  *
520  * GstElement * element = gst_check_setup_element ("element");
521  * GstPad * mysrcpad = gst_check_setup_src_pad (element, &srctemplate);
522  * GstPad * mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
523  *
524  * gst_pad_set_active (mysrcpad, TRUE);
525  * gst_pad_set_active (mysinkpad, TRUE);
526  * fail_unless (gst_element_set_state (element, GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
527  *
528  * GstCaps * caps = gst_caps_from_string (YOUR_DESIRED_SINK_CAPS);
529  * gst_check_setup_events (mysrcpad, element, caps, GST_FORMAT_TIME);
530  * gst_caps_unref (caps);
531  *
532  * fail_unless (gst_pad_push (mysrcpad, gst_buffer_new_and_alloc(2)) == GST_FLOW_OK);
533  * ]|
534  *
535  * For very simple input/output test scenarios checkout #gst_check_element_push_buffer_list and #gst_check_element_push_buffer.
536  *
537  * Returns: (transfer full): A new pad that can be used to inject data on @element
538  */
539 GstPad *
gst_check_setup_src_pad_by_name(GstElement * element,GstStaticPadTemplate * tmpl,const gchar * name)540 gst_check_setup_src_pad_by_name (GstElement * element,
541     GstStaticPadTemplate * tmpl, const gchar * name)
542 {
543   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
544   GstPad *srcpad;
545 
546   srcpad = gst_check_setup_src_pad_by_name_from_template (element, ptmpl, name);
547 
548   gst_object_unref (ptmpl);
549 
550   return srcpad;
551 }
552 
553 /**
554  * gst_check_setup_src_pad_from_template:
555  * @element: element to setup pad on
556  * @tmpl: pad template
557  *
558  * Returns: (transfer full): a new pad
559  *
560  * Since: 1.4
561  */
562 GstPad *
gst_check_setup_src_pad_from_template(GstElement * element,GstPadTemplate * tmpl)563 gst_check_setup_src_pad_from_template (GstElement * element,
564     GstPadTemplate * tmpl)
565 {
566   return gst_check_setup_src_pad_by_name_from_template (element, tmpl, "sink");
567 }
568 
569 /**
570  * gst_check_setup_src_pad_by_name_from_template:
571  * @element: element to setup pad on
572  * @tmpl: pad template
573  * @name: name
574  *
575  * Returns: (transfer full): a new pad
576  *
577  * Since: 1.4
578  */
579 GstPad *
gst_check_setup_src_pad_by_name_from_template(GstElement * element,GstPadTemplate * tmpl,const gchar * name)580 gst_check_setup_src_pad_by_name_from_template (GstElement * element,
581     GstPadTemplate * tmpl, const gchar * name)
582 {
583   GstPad *srcpad, *sinkpad;
584 
585   /* sending pad */
586   srcpad = gst_pad_new_from_template (tmpl, "src");
587   GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
588   fail_if (srcpad == NULL, "Could not create a srcpad");
589   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
590 
591   sinkpad = gst_element_get_static_pad (element, name);
592   if (sinkpad == NULL)
593     sinkpad = gst_element_get_request_pad (element, name);
594   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
595       GST_ELEMENT_NAME (element));
596   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
597   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
598       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
599   gst_object_unref (sinkpad);   /* because we got it higher up */
600   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
601 
602   return srcpad;
603 }
604 
605 void
gst_check_teardown_pad_by_name(GstElement * element,const gchar * name)606 gst_check_teardown_pad_by_name (GstElement * element, const gchar * name)
607 {
608   GstPad *pad_peer, *pad_element;
609 
610   /* clean up floating src pad */
611   pad_element = gst_element_get_static_pad (element, name);
612   /* We don't check the refcount here since there *might* be
613    * a pad cache holding an extra reference on pad_element.
614    * To get to a state where no pad cache will exist,
615    * we first unlink that pad. */
616   pad_peer = gst_pad_get_peer (pad_element);
617 
618   if (pad_peer) {
619     if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
620       gst_pad_unlink (pad_peer, pad_element);
621     else
622       gst_pad_unlink (pad_element, pad_peer);
623   }
624 
625   /* pad refs held by both creator and this function (through _get) */
626   ASSERT_OBJECT_REFCOUNT (pad_element, "element pad_element", 2);
627   gst_object_unref (pad_element);
628   /* one more ref is held by element itself */
629 
630   if (pad_peer) {
631     /* pad refs held by both creator and this function (through _get_peer) */
632     ASSERT_OBJECT_REFCOUNT (pad_peer, "check pad_peer", 2);
633     gst_object_unref (pad_peer);
634     gst_object_unref (pad_peer);
635   }
636 }
637 
638 void
gst_check_teardown_src_pad(GstElement * element)639 gst_check_teardown_src_pad (GstElement * element)
640 {
641   gst_check_teardown_pad_by_name (element, "sink");
642 }
643 
644 /**
645  * gst_check_setup_sink_pad:
646  * @element: element to setup pad on
647  * @tmpl: pad template
648  *
649  * Does the same as #gst_check_setup_sink_pad_by_name with the <emphasis> name </emphasis> parameter equal to "src".
650  *
651  * Returns: (transfer full): a new pad that can be used to check the output of @element
652  */
653 GstPad *
gst_check_setup_sink_pad(GstElement * element,GstStaticPadTemplate * tmpl)654 gst_check_setup_sink_pad (GstElement * element, GstStaticPadTemplate * tmpl)
655 {
656   return gst_check_setup_sink_pad_by_name (element, tmpl, "src");
657 }
658 
659 /**
660  * gst_check_setup_sink_pad_by_name:
661  * @element: element to setup pad on
662  * @tmpl: pad template
663  * @name: Name of the @element src pad that will be linked to the sink pad that will be setup
664  *
665  * Creates a new sink pad (based on the given @tmpl) and links it to the given @element src pad
666  * (the pad that matches the given @name).
667  * You can set event/chain/query functions on this pad to check the output of the @element.
668  *
669  * Returns: (transfer full): a new pad that can be used to check the output of @element
670  */
671 GstPad *
gst_check_setup_sink_pad_by_name(GstElement * element,GstStaticPadTemplate * tmpl,const gchar * name)672 gst_check_setup_sink_pad_by_name (GstElement * element,
673     GstStaticPadTemplate * tmpl, const gchar * name)
674 {
675   GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
676   GstPad *sinkpad;
677 
678   sinkpad =
679       gst_check_setup_sink_pad_by_name_from_template (element, ptmpl, name);
680 
681   gst_object_unref (ptmpl);
682 
683   return sinkpad;
684 }
685 
686 /**
687  * gst_check_setup_sink_pad_from_template:
688  * @element: element to setup pad on
689  * @tmpl: pad template
690  *
691  * Returns: (transfer full): a new pad
692  *
693  * Since: 1.4
694  */
695 GstPad *
gst_check_setup_sink_pad_from_template(GstElement * element,GstPadTemplate * tmpl)696 gst_check_setup_sink_pad_from_template (GstElement * element,
697     GstPadTemplate * tmpl)
698 {
699   return gst_check_setup_sink_pad_by_name_from_template (element, tmpl, "src");
700 }
701 
702 /**
703  * gst_check_setup_sink_pad_by_name_from_template:
704  * @element: element to setup pad on
705  * @tmpl: pad template
706  * @name: name
707  *
708  * Returns: (transfer full): a new pad
709  *
710  * Since: 1.4
711  */
712 GstPad *
gst_check_setup_sink_pad_by_name_from_template(GstElement * element,GstPadTemplate * tmpl,const gchar * name)713 gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
714     GstPadTemplate * tmpl, const gchar * name)
715 {
716   GstPad *srcpad, *sinkpad;
717 
718   /* receiving pad */
719   sinkpad = gst_pad_new_from_template (tmpl, "sink");
720   GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
721   fail_if (sinkpad == NULL, "Could not create a sinkpad");
722 
723   srcpad = gst_element_get_static_pad (element, name);
724   if (srcpad == NULL)
725     srcpad = gst_element_get_request_pad (element, name);
726   fail_if (srcpad == NULL, "Could not get source pad from %s",
727       GST_ELEMENT_NAME (element));
728   gst_pad_set_chain_function (sinkpad, gst_check_chain_func);
729 
730   GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
731   fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
732       "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
733   gst_object_unref (srcpad);    /* because we got it higher up */
734   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
735 
736   GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
737   return sinkpad;
738 }
739 
740 void
gst_check_teardown_sink_pad(GstElement * element)741 gst_check_teardown_sink_pad (GstElement * element)
742 {
743   gst_check_teardown_pad_by_name (element, "src");
744 }
745 
746 /**
747  * gst_check_drop_buffers:
748  *
749  * Unref and remove all buffers that are in the global @buffers GList,
750  * emptying the list.
751  */
752 void
gst_check_drop_buffers(void)753 gst_check_drop_buffers (void)
754 {
755   while (buffers != NULL) {
756     gst_buffer_unref (GST_BUFFER (buffers->data));
757     buffers = g_list_delete_link (buffers, buffers);
758   }
759 }
760 
761 /**
762  * gst_check_caps_equal:
763  * @caps1: first caps to compare
764  * @caps2: second caps to compare
765  *
766  * Compare two caps with gst_caps_is_equal and fail unless they are
767  * equal.
768  */
769 void
gst_check_caps_equal(GstCaps * caps1,GstCaps * caps2)770 gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
771 {
772   gchar *name1 = gst_caps_to_string (caps1);
773   gchar *name2 = gst_caps_to_string (caps2);
774 
775   fail_unless (gst_caps_is_equal (caps1, caps2),
776       "caps ('%s') is not equal to caps ('%s')", name1, name2);
777   g_free (name1);
778   g_free (name2);
779 }
780 
781 
782 /**
783  * gst_check_buffer_data:
784  * @buffer: buffer to compare
785  * @data: data to compare to
786  * @size: size of data to compare
787  *
788  * Compare the buffer contents with @data and @size.
789  */
790 void
gst_check_buffer_data(GstBuffer * buffer,gconstpointer data,gsize size)791 gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size)
792 {
793   GstMapInfo info;
794 
795   fail_unless (gst_buffer_map (buffer, &info, GST_MAP_READ));
796   GST_MEMDUMP ("Converted data", info.data, info.size);
797   GST_MEMDUMP ("Expected data", data, size);
798   if (memcmp (info.data, data, size) != 0) {
799     g_print ("\nConverted data:\n");
800     gst_util_dump_mem (info.data, info.size);
801     g_print ("\nExpected data:\n");
802     gst_util_dump_mem (data, size);
803     fail ("buffer contents not equal");
804   }
805   gst_buffer_unmap (buffer, &info);
806 }
807 
808 static gboolean
buffer_event_function(GstPad * pad,GstObject * noparent,GstEvent * event)809 buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
810 {
811   if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
812     GstCaps *event_caps;
813     GstCaps *expected_caps = gst_pad_get_element_private (pad);
814 
815     gst_event_parse_caps (event, &event_caps);
816     fail_unless (gst_caps_is_fixed (expected_caps));
817     fail_unless (gst_caps_is_fixed (event_caps));
818     fail_unless (gst_caps_is_equal_fixed (event_caps, expected_caps));
819     gst_event_unref (event);
820     return TRUE;
821   }
822 
823   return gst_pad_event_default (pad, noparent, event);
824 }
825 
826 /**
827  * gst_check_element_push_buffer_list:
828  * @element_name: name of the element that needs to be created
829  * @buffer_in: (element-type GstBuffer) (transfer full): a list of buffers that needs to be
830  *  pushed to the element
831  * @caps_in: the #GstCaps expected of the sinkpad of the element
832  * @buffer_out: (element-type GstBuffer) (transfer full): a list of buffers that we expect from
833  * the element
834  * @caps_out: the #GstCaps expected of the srcpad of the element
835  * @last_flow_return: the last buffer push needs to give this GstFlowReturn
836  *
837  * Create an element using the factory providing the @element_name and push the
838  * buffers in @buffer_in to this element. The element should create the buffers
839  * equal to the buffers in @buffer_out. We only check the size and the data of
840  * the buffers. This function unrefs the buffers in the two lists.
841  * The last_flow_return parameter indicates the expected flow return value from
842  * pushing the final buffer in the list.
843  * This can be used to set up a test which pushes some buffers and then an
844  * invalid buffer, when the final buffer is expected to fail, for example.
845  */
846 /* FIXME 2.0: rename this function now that there's GstBufferList? */
847 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)848 gst_check_element_push_buffer_list (const gchar * element_name,
849     GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
850     GstCaps * caps_out, GstFlowReturn last_flow_return)
851 {
852   GstElement *element;
853   GstPad *pad_peer;
854   GstPad *sink_pad = NULL;
855   GstPad *src_pad;
856   GstBuffer *buffer;
857 
858   /* check that there are no buffers waiting */
859   gst_check_drop_buffers ();
860   /* create the element */
861   element = gst_check_setup_element (element_name);
862   fail_if (element == NULL, "failed to create the element '%s'", element_name);
863   fail_unless (GST_IS_ELEMENT (element), "the element is no element");
864   /* create the src pad */
865   buffer = GST_BUFFER (buffer_in->data);
866 
867   fail_unless (GST_IS_BUFFER (buffer), "There should be a buffer in buffer_in");
868   src_pad = gst_pad_new ("src", GST_PAD_SRC);
869   if (caps_in) {
870     fail_unless (gst_caps_is_fixed (caps_in));
871     gst_pad_use_fixed_caps (src_pad);
872   }
873   /* activate the pad */
874   gst_pad_set_active (src_pad, TRUE);
875   GST_DEBUG ("src pad activated");
876   gst_check_setup_events (src_pad, element, caps_in, GST_FORMAT_BYTES);
877   pad_peer = gst_element_get_static_pad (element, "sink");
878   fail_if (pad_peer == NULL);
879   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
880       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
881   gst_object_unref (pad_peer);
882   /* don't create the sink_pad if there is no buffer_out list */
883   if (buffer_out != NULL) {
884 
885     GST_DEBUG ("buffer out detected, creating the sink pad");
886     /* get the sink caps */
887     if (caps_out) {
888       gchar *temp;
889 
890       fail_unless (gst_caps_is_fixed (caps_out));
891       temp = gst_caps_to_string (caps_out);
892 
893       GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
894       g_free (temp);
895     }
896 
897     /* get the sink pad */
898     sink_pad = gst_pad_new ("sink", GST_PAD_SINK);
899     fail_unless (GST_IS_PAD (sink_pad));
900     /* configure the sink pad */
901     gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
902     gst_pad_set_active (sink_pad, TRUE);
903     if (caps_out) {
904       gst_pad_set_element_private (sink_pad, caps_out);
905       gst_pad_set_event_function (sink_pad, buffer_event_function);
906     }
907     /* get the peer pad */
908     pad_peer = gst_element_get_static_pad (element, "src");
909     fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
910         "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
911     gst_object_unref (pad_peer);
912   }
913   fail_unless (gst_element_set_state (element,
914           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
915       "could not set to playing");
916   /* push all the buffers in the buffer_in list */
917   fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
918   while (buffer_in != NULL) {
919     GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);
920 
921     fail_unless (GST_IS_BUFFER (next_buffer),
922         "data in buffer_in should be a buffer");
923     /* remove the buffer from the list */
924     buffer_in = g_list_remove (buffer_in, next_buffer);
925     if (buffer_in == NULL) {
926       fail_unless (gst_pad_push (src_pad, next_buffer) == last_flow_return,
927           "we expect something else from the last buffer");
928     } else {
929       fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
930           "Failed to push buffer in");
931     }
932   }
933   fail_unless (gst_element_set_state (element,
934           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
935   /* check that there is a buffer out */
936   fail_unless_equals_int (g_list_length (buffers), g_list_length (buffer_out));
937   while (buffers != NULL) {
938     GstBuffer *new = GST_BUFFER (buffers->data);
939     GstBuffer *orig = GST_BUFFER (buffer_out->data);
940     GstMapInfo newinfo, originfo;
941 
942     fail_unless (gst_buffer_map (new, &newinfo, GST_MAP_READ));
943     fail_unless (gst_buffer_map (orig, &originfo, GST_MAP_READ));
944 
945     GST_LOG ("orig buffer: size %" G_GSIZE_FORMAT, originfo.size);
946     GST_LOG ("new  buffer: size %" G_GSIZE_FORMAT, newinfo.size);
947     GST_MEMDUMP ("orig buffer", originfo.data, originfo.size);
948     GST_MEMDUMP ("new  buffer", newinfo.data, newinfo.size);
949 
950     /* remove the buffers */
951     buffers = g_list_remove (buffers, new);
952     buffer_out = g_list_remove (buffer_out, orig);
953 
954     fail_unless (originfo.size == newinfo.size,
955         "size of the buffers are not the same");
956     fail_unless (memcmp (originfo.data, newinfo.data, newinfo.size) == 0,
957         "data is not the same");
958 #if 0
959     gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
960 #endif
961 
962     gst_buffer_unmap (orig, &originfo);
963     gst_buffer_unmap (new, &newinfo);
964 
965     gst_buffer_unref (new);
966     gst_buffer_unref (orig);
967   }
968   /* teardown the element and pads */
969   gst_pad_set_active (src_pad, FALSE);
970   gst_check_teardown_src_pad (element);
971   gst_pad_set_active (sink_pad, FALSE);
972   gst_check_teardown_sink_pad (element);
973   gst_check_teardown_element (element);
974 }
975 
976 /**
977  * gst_check_element_push_buffer:
978  * @element_name: name of the element that needs to be created
979  * @buffer_in: push this buffer to the element
980  * @caps_in: the #GstCaps expected of the sinkpad of the element
981  * @buffer_out: compare the result with this buffer
982  * @caps_out: the #GstCaps expected of the srcpad of the element
983  *
984  * Create an element using the factory providing the @element_name and
985  * push the @buffer_in to this element. The element should create one buffer
986  * and this will be compared with @buffer_out. We only check the caps
987  * and the data of the buffers. This function unrefs the buffers.
988  */
989 void
gst_check_element_push_buffer(const gchar * element_name,GstBuffer * buffer_in,GstCaps * caps_in,GstBuffer * buffer_out,GstCaps * caps_out)990 gst_check_element_push_buffer (const gchar * element_name,
991     GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
992     GstCaps * caps_out)
993 {
994   GList *in = NULL;
995   GList *out = NULL;
996 
997   in = g_list_append (in, buffer_in);
998   out = g_list_append (out, buffer_out);
999 
1000   gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
1001       GST_FLOW_OK);
1002 }
1003 
1004 void
gst_check_abi_list(GstCheckABIStruct list[],gboolean have_abi_sizes)1005 gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
1006 {
1007   if (have_abi_sizes) {
1008     gboolean ok = TRUE;
1009     gint i;
1010 
1011     for (i = 0; list[i].name; i++) {
1012       if (list[i].size != list[i].abi_size) {
1013         ok = FALSE;
1014         g_print ("sizeof(%s) is %d, expected %d\n",
1015             list[i].name, list[i].size, list[i].abi_size);
1016       }
1017     }
1018     fail_unless (ok, "failed ABI check");
1019   } else {
1020     const gchar *fn;
1021 
1022     if ((fn = g_getenv ("GST_ABI"))) {
1023       GError *err = NULL;
1024       GString *s;
1025       gint i;
1026 
1027       s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
1028       for (i = 0; list[i].name; i++) {
1029         g_string_append_printf (s, "  {\"%s\", sizeof (%s), %d},\n",
1030             list[i].name, list[i].name, list[i].size);
1031       }
1032       g_string_append (s, "  {NULL, 0, 0}\n");
1033       g_string_append (s, "};\n");
1034       if (!g_file_set_contents (fn, s->str, s->len, &err)) {
1035         g_print ("%s", s->str);
1036         g_printerr ("\nFailed to write ABI information: %s\n", err->message);
1037         g_clear_error (&err);
1038       } else {
1039         g_print ("\nWrote ABI information to '%s'.\n", fn);
1040       }
1041       g_string_free (s, TRUE);
1042     } else {
1043       g_print ("No structure size list was generated for this architecture.\n");
1044       g_print ("Run with GST_ABI environment variable set to output header.\n");
1045     }
1046   }
1047 }
1048 
1049 /**
1050  * gst_check_run_suite: (skip)
1051  * @suite: the check test suite
1052  * @name: name
1053  * @fname: file name
1054  *
1055  * Returns: number of failed tests
1056  */
1057 gint
gst_check_run_suite(Suite * suite,const gchar * name,const gchar * fname)1058 gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
1059 {
1060   SRunner *sr;
1061   gchar *xmlfilename = NULL;
1062   gint nf;
1063   GTimer *timer;
1064 
1065   sr = srunner_create (suite);
1066 
1067   if (g_getenv ("GST_CHECK_XML")) {
1068     /* how lucky we are to have __FILE__ end in .c */
1069     xmlfilename = g_strdup_printf ("%sheck.xml", fname);
1070 
1071     srunner_set_xml (sr, xmlfilename);
1072   }
1073 
1074   timer = g_timer_new ();
1075   srunner_run_all (sr, CK_NORMAL);
1076   nf = srunner_ntests_failed (sr);
1077   g_print ("Check suite %s ran in %.3fs (tests failed: %d)\n",
1078       name, g_timer_elapsed (timer, NULL), nf);
1079   g_timer_destroy (timer);
1080   g_free (xmlfilename);
1081   srunner_free (sr);
1082   return nf;
1083 }
1084 
1085 static gboolean
gst_check_have_checks_list(const gchar * env_var_name)1086 gst_check_have_checks_list (const gchar * env_var_name)
1087 {
1088   const gchar *env_val;
1089 
1090   env_val = g_getenv (env_var_name);
1091   return (env_val != NULL && *env_val != '\0');
1092 }
1093 
1094 static gboolean
gst_check_func_is_in_list(const gchar * env_var,const gchar * func_name)1095 gst_check_func_is_in_list (const gchar * env_var, const gchar * func_name)
1096 {
1097   const gchar *gst_checks;
1098   gboolean res = FALSE;
1099   gchar **funcs, **f;
1100 
1101   gst_checks = g_getenv (env_var);
1102 
1103   if (gst_checks == NULL || *gst_checks == '\0')
1104     return FALSE;
1105 
1106   /* only run specified functions */
1107   funcs = g_strsplit (gst_checks, ",", -1);
1108   for (f = funcs; f != NULL && *f != NULL; ++f) {
1109     if (g_pattern_match_simple (*f, func_name)) {
1110       res = TRUE;
1111       break;
1112     }
1113   }
1114   g_strfreev (funcs);
1115   return res;
1116 }
1117 
1118 gboolean
_gst_check_run_test_func(const gchar * func_name)1119 _gst_check_run_test_func (const gchar * func_name)
1120 {
1121   /* if we have a whitelist, run it only if it's in the whitelist */
1122   if (gst_check_have_checks_list ("GST_CHECKS"))
1123     return gst_check_func_is_in_list ("GST_CHECKS", func_name);
1124 
1125   /* if we have a blacklist, run it only if it's not in the blacklist */
1126   if (gst_check_have_checks_list ("GST_CHECKS_IGNORE"))
1127     return !gst_check_func_is_in_list ("GST_CHECKS_IGNORE", func_name);
1128 
1129   /* no filter specified => run all checks */
1130   return TRUE;
1131 }
1132 
1133 /**
1134  * gst_check_setup_events_with_stream_id:
1135  * @srcpad: The src #GstPad to push on
1136  * @element: The #GstElement use to create the stream id
1137  * @caps: (allow-none): #GstCaps in case caps event must be sent
1138  * @format: The #GstFormat of the default segment to send
1139  * @stream_id: A unique identifier for the stream
1140  *
1141  * Push stream-start, caps and segment event, which consist of the minimum
1142  * required events to allow streaming. Caps is optional to allow raw src
1143  * testing.
1144  */
1145 void
gst_check_setup_events_with_stream_id(GstPad * srcpad,GstElement * element,GstCaps * caps,GstFormat format,const gchar * stream_id)1146 gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
1147     GstCaps * caps, GstFormat format, const gchar * stream_id)
1148 {
1149   GstSegment segment;
1150 
1151   gst_segment_init (&segment, format);
1152 
1153   fail_unless (gst_pad_push_event (srcpad,
1154           gst_event_new_stream_start (stream_id)));
1155   if (caps)
1156     fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
1157   fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&segment)));
1158 }
1159 
1160 /**
1161  * gst_check_setup_events:
1162  * @srcpad: The src #GstPad to push on
1163  * @element: The #GstElement use to create the stream id
1164  * @caps: (allow-none): #GstCaps in case caps event must be sent
1165  * @format: The #GstFormat of the default segment to send
1166  *
1167  * Push stream-start, caps and segment event, which consist of the minimum
1168  * required events to allow streaming. Caps is optional to allow raw src
1169  * testing. If @element has more than one src or sink pad, use
1170  * gst_check_setup_events_with_stream_id() instead.
1171  */
1172 void
gst_check_setup_events(GstPad * srcpad,GstElement * element,GstCaps * caps,GstFormat format)1173 gst_check_setup_events (GstPad * srcpad, GstElement * element,
1174     GstCaps * caps, GstFormat format)
1175 {
1176   gchar *stream_id;
1177 
1178   stream_id = gst_pad_create_stream_id (srcpad, element, NULL);
1179   gst_check_setup_events_with_stream_id (srcpad, element, caps, format,
1180       stream_id);
1181   g_free (stream_id);
1182 }
1183 
1184 typedef struct _DestroyedObjectStruct
1185 {
1186   GObject *object;
1187   gboolean destroyed;
1188 } DestroyedObjectStruct;
1189 
1190 static void
weak_notify(DestroyedObjectStruct * destroyed,GObject ** object)1191 weak_notify (DestroyedObjectStruct * destroyed, GObject ** object)
1192 {
1193   destroyed->destroyed = TRUE;
1194 }
1195 
1196 /**
1197  * gst_check_objects_destroyed_on_unref:
1198  * @object_to_unref: The #GObject to unref
1199  * @first_object: (allow-none): The first object that should be destroyed as a
1200  * concequence of unrefing @object_to_unref.
1201  * @... : Additional object that should have been destroyed.
1202  *
1203  * Unrefs @object_to_unref and checks that is has properly been
1204  * destroyed, also checks that the other objects passed in
1205  * parametter have been destroyed as a concequence of
1206  * unrefing @object_to_unref. Last variable argument should be NULL.
1207  *
1208  * Since: 1.6
1209  */
1210 void
gst_check_objects_destroyed_on_unref(gpointer object_to_unref,gpointer first_object,...)1211 gst_check_objects_destroyed_on_unref (gpointer object_to_unref,
1212     gpointer first_object, ...)
1213 {
1214   GObject *object;
1215   GList *objs = NULL, *tmp;
1216   DestroyedObjectStruct *destroyed = g_slice_new0 (DestroyedObjectStruct);
1217 
1218   destroyed->object = object_to_unref;
1219   g_object_weak_ref (object_to_unref, (GWeakNotify) weak_notify, destroyed);
1220   objs = g_list_prepend (objs, destroyed);
1221 
1222   if (first_object) {
1223     va_list varargs;
1224 
1225     object = first_object;
1226 
1227     va_start (varargs, first_object);
1228     while (object) {
1229       destroyed = g_slice_new0 (DestroyedObjectStruct);
1230       destroyed->object = object;
1231       g_object_weak_ref (object, (GWeakNotify) weak_notify, destroyed);
1232       objs = g_list_prepend (objs, destroyed);
1233       object = va_arg (varargs, GObject *);
1234     }
1235     va_end (varargs);
1236   }
1237   gst_object_unref (object_to_unref);
1238 
1239   for (tmp = objs; tmp; tmp = tmp->next) {
1240     DestroyedObjectStruct *destroyed = tmp->data;
1241 
1242     if (!destroyed->destroyed) {
1243       fail_unless (destroyed->destroyed,
1244           "%s_%p is not destroyed, %d refcounts left!",
1245           GST_IS_OBJECT (destroyed->
1246               object) ? GST_OBJECT_NAME (destroyed->object) :
1247           G_OBJECT_TYPE_NAME (destroyed), destroyed->object,
1248           destroyed->object->ref_count);
1249     }
1250     g_slice_free (DestroyedObjectStruct, tmp->data);
1251   }
1252   g_list_free (objs);
1253 }
1254 
1255 /**
1256  * gst_check_object_destroyed_on_unref:
1257  * @object_to_unref: The #GObject to unref
1258  *
1259  * Unrefs @object_to_unref and checks that is has properly been
1260  * destroyed.
1261  *
1262  * Since: 1.6
1263  */
1264 void
gst_check_object_destroyed_on_unref(gpointer object_to_unref)1265 gst_check_object_destroyed_on_unref (gpointer object_to_unref)
1266 {
1267   gst_check_objects_destroyed_on_unref (object_to_unref, NULL, NULL);
1268 }
1269 
1270 /* For ABI compatibility with GStreamer < 1.5 */
1271 /* *INDENT-OFF* */
1272 GST_CHECK_API void
1273 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1274 G_GNUC_PRINTF (4, 5);
1275 /* *INDENT-ON* */
1276 
1277 void
_fail_unless(int result,const char * file,int line,const char * expr,...)1278 _fail_unless (int result, const char *file, int line, const char *expr, ...)
1279 {
1280   gchar *msg;
1281   va_list args;
1282 
1283   if (result) {
1284     _mark_point (file, line);
1285     return;
1286   }
1287 
1288   va_start (args, expr);
1289   msg = g_strdup_vprintf (expr, args);
1290   va_end (args);
1291 
1292   _ck_assert_failed (file, line, msg, NULL);
1293   g_free (msg);
1294 }
1295