gstcheck.c 38.2 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2 3 4
/* GStreamer
 *
 * Common code for GStreamer unittests
 *
5
 * Copyright (C) 2004,2006 Thomas Vander Stichele <thomas at apestaart dot org>
6
 * Copyright (C) 2008 Thijs Vermeir <thijsvermeir@gmail.com>
Wim Taymans's avatar
Wim Taymans committed
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
20 21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Wim Taymans's avatar
Wim Taymans committed
22
 */
23 24
/**
 * SECTION:gstcheck
25
 * @title: GstCheck
26
 * @short_description: Common code for GStreamer unit tests
27
 *
28 29
 * These macros and functions are for internal use of the unit tests found
 * inside the 'check' directories of various GStreamer packages.
30
 *
31 32
 * One notable feature is that one can use the environment variables GST_CHECKS
 * 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 36 37
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Wim Taymans's avatar
Wim Taymans committed
38 39 40

#include "gstcheck.h"

41
GST_DEBUG_CATEGORY (check_debug);
Wim Taymans's avatar
Wim Taymans committed
42 43 44 45 46 47 48 49 50

/* logging function for tests
 * a test uses g_message() to log a debug line
 * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
 * messages
 */

gboolean _gst_check_threads_running = FALSE;
GList *thread_list = NULL;
51 52 53
GMutex mutex;
GCond start_cond;               /* used to notify main thread of thread startups */
GCond sync_cond;                /* used to synchronize all threads and main thread */
Wim Taymans's avatar
Wim Taymans committed
54

55
GList *buffers = NULL;
56 57
GMutex check_mutex;
GCond check_cond;
58

59
/* FIXME 2.0: shouldn't _gst_check_debug be static? Not used anywhere */
Wim Taymans's avatar
Wim Taymans committed
60 61
gboolean _gst_check_debug = FALSE;
gboolean _gst_check_raised_critical = FALSE;
62
gboolean _gst_check_raised_warning = FALSE;
Wim Taymans's avatar
Wim Taymans committed
63
gboolean _gst_check_expecting_log = FALSE;
64
gboolean _gst_check_list_tests = FALSE;
65 66 67
static GQueue _gst_check_log_filters = G_QUEUE_INIT;
static GMutex _gst_check_log_filters_mutex;

68 69
struct _GstCheckLogFilter
{
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  gchar *log_domain;
  GLogLevelFlags log_level;
  GRegex *regex;
  GstCheckLogFilterFunc func;
  gpointer user_data;
  GDestroyNotify destroy;
};


static gboolean
gst_check_match_log_filter (const GstCheckLogFilter * filter,
    const gchar * log_domain, GLogLevelFlags log_level, const gchar * message)
{
  if (g_strcmp0 (log_domain, filter->log_domain) != 0)
    return FALSE;

  if ((log_level & filter->log_level) == 0)
    return FALSE;

  if (!g_regex_match (filter->regex, message, 0, NULL))
    return FALSE;

  return TRUE;
}

static GstCheckLogFilter *
gst_check_alloc_log_filter (const gchar * log_domain, GLogLevelFlags log_level,
    GRegex * regex, GstCheckLogFilterFunc func, gpointer user_data,
    GDestroyNotify destroy_data)
{
  GstCheckLogFilter *filter;

  filter = g_slice_new (GstCheckLogFilter);
  filter->log_domain = g_strdup (log_domain);
  filter->log_level = log_level;
  filter->regex = regex;
  filter->func = func;
  filter->user_data = user_data;
  filter->destroy = destroy_data;

  return filter;
}

static void
gst_check_free_log_filter (GstCheckLogFilter * filter)
{
  if (!filter)
    return;

  g_free (filter->log_domain);
  g_regex_unref (filter->regex);
  if (filter->destroy)
    filter->destroy (filter->user_data);
  g_slice_free (GstCheckLogFilter, filter);
}


/**
128
 * gst_check_add_log_filter: (skip)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
 * @log_domain: the log domain of the message
 * @log_level: the log level of the message
 * @regex: (transfer full): a #GRegex to match the message
 * @func: the function to call for matching messages
 * @user_data: the user data to pass to @func
 * @destroy_data: #GDestroyNotify for @user_data
 *
 * Add a callback @func to be called for all log messages that matches
 * @log_domain, @log_level and @regex. If @func is NULL the
 * matching logs will be silently discarded by GstCheck.
 *
 * MT safe.
 *
 * Returns: A filter that can be passed to @gst_check_remove_log_filter.
 *
 * Since: 1.12
 **/
GstCheckLogFilter *
gst_check_add_log_filter (const gchar * log_domain, GLogLevelFlags log_level,
    GRegex * regex, GstCheckLogFilterFunc func, gpointer user_data,
    GDestroyNotify destroy_data)
{
  GstCheckLogFilter *filter;

  g_return_val_if_fail (regex != NULL, NULL);

  filter = gst_check_alloc_log_filter (log_domain, log_level, regex,
      func, user_data, destroy_data);
  g_mutex_lock (&_gst_check_log_filters_mutex);
  g_queue_push_tail (&_gst_check_log_filters, filter);
  g_mutex_unlock (&_gst_check_log_filters_mutex);

  return filter;
}

/**
 * gst_check_remove_log_filter:
 * @filter: Filter returned by @gst_check_add_log_filter
 *
 * Remove a filter that has been added by @gst_check_add_log_filter.
 *
 * MT safe.
 *
 * Since: 1.12
 **/
void
gst_check_remove_log_filter (GstCheckLogFilter * filter)
{
  g_mutex_lock (&_gst_check_log_filters_mutex);
  g_queue_remove (&_gst_check_log_filters, filter);
  gst_check_free_log_filter (filter);
  g_mutex_unlock (&_gst_check_log_filters_mutex);
}

/**
 * gst_check_clear_log_filter:
 *
 * Clear all filters added by @gst_check_add_log_filter.
 *
 * MT safe.
 *
 * Since: 1.12
 **/
void
gst_check_clear_log_filter (void)
{
  g_mutex_lock (&_gst_check_log_filters_mutex);
  g_queue_foreach (&_gst_check_log_filters,
      (GFunc) gst_check_free_log_filter, NULL);
  g_queue_clear (&_gst_check_log_filters);
  g_mutex_unlock (&_gst_check_log_filters_mutex);
}

typedef struct
{
204 205
  const gchar *domain;
  const gchar *message;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  GLogLevelFlags level;
  gboolean discard;
} LogFilterApplyData;

static void
gst_check_apply_log_filter (GstCheckLogFilter * filter,
    LogFilterApplyData * data)
{
  if (gst_check_match_log_filter (filter, data->domain, data->level,
          data->message)) {
    if (filter->func)
      data->discard |= filter->func (data->domain, data->level,
          data->message, filter->user_data);
    else
      data->discard = TRUE;
  }
}

static gboolean
gst_check_filter_log_filter (const gchar * log_domain,
    GLogLevelFlags log_level, const gchar * message)
{
  LogFilterApplyData data;

  data.domain = log_domain;
  data.level = log_level;
  data.message = message;
  data.discard = FALSE;

  g_mutex_lock (&_gst_check_log_filters_mutex);
  g_queue_foreach (&_gst_check_log_filters, (GFunc) gst_check_apply_log_filter,
      &data);
  g_mutex_unlock (&_gst_check_log_filters_mutex);

  if (data.discard)
    GST_DEBUG ("Discarding message: %s", message);

  return data.discard;
}

static gboolean
gst_check_log_fatal_func (const gchar * log_domain, GLogLevelFlags log_level,
248
    const gchar * message, gpointer user_data)
249 250 251 252 253 254 255
{
  if (gst_check_filter_log_filter (log_domain, log_level, message))
    return FALSE;

  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
256

257
static void gst_check_log_message_func
Wim Taymans's avatar
Wim Taymans committed
258 259 260
    (const gchar * log_domain, GLogLevelFlags log_level,
    const gchar * message, gpointer user_data)
{
261 262 263
  if (gst_check_filter_log_filter (log_domain, log_level, message))
    return;

Wim Taymans's avatar
Wim Taymans committed
264
  if (_gst_check_debug) {
265
    g_print ("%s\n", message);
Wim Taymans's avatar
Wim Taymans committed
266 267 268
  }
}

269
static void gst_check_log_critical_func
Wim Taymans's avatar
Wim Taymans committed
270 271 272
    (const gchar * log_domain, GLogLevelFlags log_level,
    const gchar * message, gpointer user_data)
{
273 274 275
  if (gst_check_filter_log_filter (log_domain, log_level, message))
    return;

Wim Taymans's avatar
Wim Taymans committed
276
  if (!_gst_check_expecting_log) {
277 278
    gchar *trace;

Wim Taymans's avatar
Wim Taymans committed
279
    g_print ("\n\nUnexpected critical/warning: %s\n", message);
280 281 282 283 284 285

    trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
    if (trace) {
      g_print ("\nStack trace:\n%s\n", trace);
      g_free (trace);
    }
Wim Taymans's avatar
Wim Taymans committed
286 287 288 289 290 291 292 293 294
    fail ("Unexpected critical/warning: %s", message);
  }

  if (_gst_check_debug) {
    g_print ("\nExpected critical/warning: %s\n", message);
  }

  if (log_level & G_LOG_LEVEL_CRITICAL)
    _gst_check_raised_critical = TRUE;
295 296
  if (log_level & G_LOG_LEVEL_WARNING)
    _gst_check_raised_warning = TRUE;
Wim Taymans's avatar
Wim Taymans committed
297 298
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
static gint
sort_plugins (GstPlugin * a, GstPlugin * b)
{
  int ret;

  ret = strcmp (gst_plugin_get_source (a), gst_plugin_get_source (b));
  if (ret == 0)
    ret = strcmp (gst_plugin_get_name (a), gst_plugin_get_name (b));
  return ret;
}

static void
print_plugins (void)
{
  GList *plugins, *l;

315
  plugins = gst_registry_get_plugin_list (gst_registry_get ());
316 317 318 319 320 321 322 323 324 325 326 327
  plugins = g_list_sort (plugins, (GCompareFunc) sort_plugins);
  for (l = plugins; l != NULL; l = l->next) {
    GstPlugin *plugin = GST_PLUGIN (l->data);

    if (strcmp (gst_plugin_get_source (plugin), "BLACKLIST") != 0) {
      GST_LOG ("%20s@%s", gst_plugin_get_name (plugin),
          GST_STR_NULL (gst_plugin_get_filename (plugin)));
    }
  }
  gst_plugin_list_free (plugins);
}

328 329 330 331 332 333 334
static void
gst_check_deinit (void)
{
  gst_deinit ();
  gst_check_clear_log_filter ();
}

335 336 337 338
/* gst_check_init:
 * @argc: (inout) (allow-none): pointer to application's argc
 * @argv: (inout) (array length=argc) (allow-none): pointer to application's argv
 *
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
339
 * Initialize GStreamer testing
340 341 342 343
 *
 * NOTE: Needs to be called before creating the testsuite
 * so that the tests can be listed.
 * */
Wim Taymans's avatar
Wim Taymans committed
344 345 346
void
gst_check_init (int *argc, char **argv[])
{
347
  guint timeout_multiplier = 1;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  GOptionContext *ctx;
  GError *err = NULL;
  GOptionEntry options[] = {
    {"list-tests", 'l', 0, G_OPTION_ARG_NONE, &_gst_check_list_tests,
        "List tests present in the testsuite", NULL},
    {NULL}
  };

  ctx = g_option_context_new ("gst-check");
  g_option_context_add_main_entries (ctx, options, NULL);
  g_option_context_add_group (ctx, gst_init_get_option_group ());

  if (!g_option_context_parse (ctx, argc, argv, &err)) {
    if (err)
      g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
    else
      g_printerr ("Error initializing: Unknown error!\n");
    g_clear_error (&err);
  }
  g_option_context_free (ctx);
Wim Taymans's avatar
Wim Taymans committed
368

369 370
  GST_DEBUG_CATEGORY_INIT (check_debug, "check", 0, "check regression tests");

371 372
  if (atexit (gst_check_deinit) != 0) {
    GST_ERROR ("failed to set gst_check_deinit as exit function");
373 374
  }

Wim Taymans's avatar
Wim Taymans committed
375 376 377 378 379 380 381 382 383 384 385
  if (g_getenv ("GST_TEST_DEBUG"))
    _gst_check_debug = TRUE;

  g_log_set_handler (NULL, G_LOG_LEVEL_MESSAGE, gst_check_log_message_func,
      NULL);
  g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
      gst_check_log_critical_func, NULL);
  g_log_set_handler ("GStreamer", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
      gst_check_log_critical_func, NULL);
  g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
      gst_check_log_critical_func, NULL);
386 387 388
  g_log_set_handler ("GLib-GIO", G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
      gst_check_log_critical_func, NULL);
  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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

#ifdef TARGET_CPU
  GST_INFO ("target CPU: %s", TARGET_CPU);
#endif

#ifdef HAVE_CPU_ARM
  timeout_multiplier = 10;
#endif

  if (timeout_multiplier > 1) {
    const gchar *tmult = g_getenv ("CK_TIMEOUT_MULTIPLIER");

    if (tmult == NULL) {
      gchar num_str[32];

      g_snprintf (num_str, sizeof (num_str), "%d", timeout_multiplier);
      GST_INFO ("slow CPU, setting CK_TIMEOUT_MULTIPLIER to %s", num_str);
      g_setenv ("CK_TIMEOUT_MULTIPLIER", num_str, TRUE);
    } else {
      GST_INFO ("CK_TIMEOUT_MULTIPLIER already set to '%s'", tmult);
    }
  }
Wim Taymans's avatar
Wim Taymans committed
415
}
416 417 418 419 420 421 422 423 424

/* message checking */
void
gst_check_message_error (GstMessage * message, GstMessageType type,
    GQuark domain, gint code)
{
  GError *error;
  gchar *debug;

425 426 427 428
  fail_unless (GST_MESSAGE_TYPE (message) == type,
      "message is of type %s instead of expected type %s",
      gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
      gst_message_type_get_name (type));
429 430 431 432 433 434
  gst_message_parse_error (message, &error, &debug);
  fail_unless_equals_int (error->domain, domain);
  fail_unless_equals_int (error->code, code);
  g_error_free (error);
  g_free (debug);
}
435 436 437

/* helper functions */
GstFlowReturn
438
gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
439
{
440
  GST_DEBUG_OBJECT (pad, "chain_func: received buffer %p", buffer);
441 442
  buffers = g_list_append (buffers, buffer);

443 444 445
  g_mutex_lock (&check_mutex);
  g_cond_signal (&check_cond);
  g_mutex_unlock (&check_mutex);
446

447 448 449
  return GST_FLOW_OK;
}

450 451 452 453 454 455 456 457
/**
 * gst_check_setup_element:
 * @factory: factory
 *
 * setup an element for a filter test with mysrcpad and mysinkpad
 *
 * Returns: (transfer full): a new element
 */
458 459 460 461 462 463 464 465
GstElement *
gst_check_setup_element (const gchar * factory)
{
  GstElement *element;

  GST_DEBUG ("setup_element");

  element = gst_element_factory_make (factory, factory);
466
  fail_if (element == NULL, "Could not create a '%s' element", factory);
467 468 469 470 471 472 473 474 475 476
  ASSERT_OBJECT_REFCOUNT (element, factory, 1);
  return element;
}

void
gst_check_teardown_element (GstElement * element)
{
  GST_DEBUG ("teardown_element");

  fail_unless (gst_element_set_state (element, GST_STATE_NULL) ==
477
      GST_STATE_CHANGE_SUCCESS, "could not set to null");
478 479 480 481
  ASSERT_OBJECT_REFCOUNT (element, "element", 1);
  gst_object_unref (element);
}

482 483 484 485 486
/**
 * gst_check_setup_src_pad:
 * @element: element to setup pad on
 * @tmpl: pad template
 *
487 488 489
 * Does the same as #gst_check_setup_src_pad_by_name with the <emphasis> name </emphasis> parameter equal to "sink".
 *
 * Returns: (transfer full): A new pad that can be used to inject data on @element
490
 */
491
GstPad *
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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
 * @name: Name of the @element sink pad that will be linked to the src pad that will be setup
 *
 * 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).
 * Before using the src pad to push data on @element you need to call #gst_check_setup_events on the created src pad.
 *
 * Example of how to push a buffer on @element:
 *
 * |[<!-- language="C" -->
 * static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
 * GST_PAD_SINK,
 * GST_PAD_ALWAYS,
 * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
 * );
 * static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
 * GST_PAD_SRC,
 * GST_PAD_ALWAYS,
 * GST_STATIC_CAPS (YOUR_CAPS_TEMPLATE_STRING)
 * );
 *
 * GstElement * element = gst_check_setup_element ("element");
 * GstPad * mysrcpad = gst_check_setup_src_pad (element, &srctemplate);
 * GstPad * mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate);
 *
 * gst_pad_set_active (mysrcpad, TRUE);
 * gst_pad_set_active (mysinkpad, TRUE);
 * fail_unless (gst_element_set_state (element, GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
 *
 * GstCaps * caps = gst_caps_from_string (YOUR_DESIRED_SINK_CAPS);
 * gst_check_setup_events (mysrcpad, element, caps, GST_FORMAT_TIME);
 * gst_caps_unref (caps);
 *
 * fail_unless (gst_pad_push (mysrcpad, gst_buffer_new_and_alloc(2)) == GST_FLOW_OK);
 * ]|
534
 *
535 536
 * For very simple input/output test scenarios checkout #gst_check_element_push_buffer_list and #gst_check_element_push_buffer.
 *
537
 * Returns: (transfer full): A new pad that can be used to inject data on @element
538
 */
539 540
GstPad *
gst_check_setup_src_pad_by_name (GstElement * element,
541
    GstStaticPadTemplate * tmpl, const gchar * name)
542 543 544 545
{
  GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
  GstPad *srcpad;

546
  srcpad = gst_check_setup_src_pad_by_name_from_template (element, ptmpl, name);
547 548 549 550 551 552 553 554 555 556 557 558

  gst_object_unref (ptmpl);

  return srcpad;
}

/**
 * gst_check_setup_src_pad_from_template:
 * @element: element to setup pad on
 * @tmpl: pad template
 *
 * Returns: (transfer full): a new pad
559 560
 *
 * Since: 1.4
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
 */
GstPad *
gst_check_setup_src_pad_from_template (GstElement * element,
    GstPadTemplate * tmpl)
{
  return gst_check_setup_src_pad_by_name_from_template (element, tmpl, "sink");
}

/**
 * gst_check_setup_src_pad_by_name_from_template:
 * @element: element to setup pad on
 * @tmpl: pad template
 * @name: name
 *
 * Returns: (transfer full): a new pad
576 577
 *
 * Since: 1.4
578 579 580 581
 */
GstPad *
gst_check_setup_src_pad_by_name_from_template (GstElement * element,
    GstPadTemplate * tmpl, const gchar * name)
582 583 584 585
{
  GstPad *srcpad, *sinkpad;

  /* sending pad */
586
  srcpad = gst_pad_new_from_template (tmpl, "src");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
587
  GST_DEBUG_OBJECT (element, "setting up sending pad %p", srcpad);
588 589 590
  fail_if (srcpad == NULL, "Could not create a srcpad");
  ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);

591 592 593
  sinkpad = gst_element_get_static_pad (element, name);
  if (sinkpad == NULL)
    sinkpad = gst_element_get_request_pad (element, name);
594 595 596 597 598 599 600 601 602 603 604 605
  fail_if (sinkpad == NULL, "Could not get sink pad from %s",
      GST_ELEMENT_NAME (element));
  ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 2);
  fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
      "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
  gst_object_unref (sinkpad);   /* because we got it higher up */
  ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);

  return srcpad;
}

void
Benjamin Otte's avatar
Benjamin Otte committed
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 613 614 615
  /* We don't check the refcount here since there *might* be
   * a pad cache holding an extra reference on pad_element.
   * To get to a state where no pad cache will exist,
   * we first unlink that pad. */
616
  pad_peer = gst_pad_get_peer (pad_element);
617

618 619 620 621 622 623
  if (pad_peer) {
    if (gst_pad_get_direction (pad_element) == GST_PAD_SINK)
      gst_pad_unlink (pad_peer, pad_element);
    else
      gst_pad_unlink (pad_element, pad_peer);
  }
624

625
  /* pad refs held by both creator and this function (through _get) */
626 627
  ASSERT_OBJECT_REFCOUNT (pad_element, "element pad_element", 2);
  gst_object_unref (pad_element);
628 629
  /* one more ref is held by element itself */

630 631 632 633 634 635
  if (pad_peer) {
    /* pad refs held by both creator and this function (through _get_peer) */
    ASSERT_OBJECT_REFCOUNT (pad_peer, "check pad_peer", 2);
    gst_object_unref (pad_peer);
    gst_object_unref (pad_peer);
  }
636 637 638 639 640 641
}

void
gst_check_teardown_src_pad (GstElement * element)
{
  gst_check_teardown_pad_by_name (element, "sink");
642 643
}

644 645 646 647 648
/**
 * gst_check_setup_sink_pad:
 * @element: element to setup pad on
 * @tmpl: pad template
 *
649 650 651
 * Does the same as #gst_check_setup_sink_pad_by_name with the <emphasis> name </emphasis> parameter equal to "src".
 *
 * Returns: (transfer full): a new pad that can be used to check the output of @element
652
 */
653
GstPad *
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 661 662
/**
 * gst_check_setup_sink_pad_by_name:
 * @element: element to setup pad on
 * @tmpl: pad template
663
 * @name: Name of the @element src pad that will be linked to the sink pad that will be setup
664
 *
Stefan Sauer's avatar
Stefan Sauer committed
665
 * Creates a new sink pad (based on the given @tmpl) and links it to the given @element src pad
666 667 668 669
 * (the pad that matches the given @name).
 * You can set event/chain/query functions on this pad to check the output of the @element.
 *
 * Returns: (transfer full): a new pad that can be used to check the output of @element
670
 */
671 672
GstPad *
gst_check_setup_sink_pad_by_name (GstElement * element,
673
    GstStaticPadTemplate * tmpl, const gchar * name)
674 675 676 677 678
{
  GstPadTemplate *ptmpl = gst_static_pad_template_get (tmpl);
  GstPad *sinkpad;

  sinkpad =
679
      gst_check_setup_sink_pad_by_name_from_template (element, ptmpl, name);
680 681 682 683 684 685 686 687 688 689 690 691

  gst_object_unref (ptmpl);

  return sinkpad;
}

/**
 * gst_check_setup_sink_pad_from_template:
 * @element: element to setup pad on
 * @tmpl: pad template
 *
 * Returns: (transfer full): a new pad
692 693
 *
 * Since: 1.4
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
 */
GstPad *
gst_check_setup_sink_pad_from_template (GstElement * element,
    GstPadTemplate * tmpl)
{
  return gst_check_setup_sink_pad_by_name_from_template (element, tmpl, "src");
}

/**
 * gst_check_setup_sink_pad_by_name_from_template:
 * @element: element to setup pad on
 * @tmpl: pad template
 * @name: name
 *
 * Returns: (transfer full): a new pad
709 710
 *
 * Since: 1.4
711 712 713 714
 */
GstPad *
gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
    GstPadTemplate * tmpl, const gchar * name)
715 716 717 718
{
  GstPad *srcpad, *sinkpad;

  /* receiving pad */
719
  sinkpad = gst_pad_new_from_template (tmpl, "sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
720
  GST_DEBUG_OBJECT (element, "setting up receiving pad %p", sinkpad);
721 722
  fail_if (sinkpad == NULL, "Could not create a sinkpad");

723 724 725
  srcpad = gst_element_get_static_pad (element, name);
  if (srcpad == NULL)
    srcpad = gst_element_get_request_pad (element, name);
726 727 728 729
  fail_if (srcpad == NULL, "Could not get source pad from %s",
      GST_ELEMENT_NAME (element));
  gst_pad_set_chain_function (sinkpad, gst_check_chain_func);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
730
  GST_DEBUG_OBJECT (element, "Linking element src pad and receiving sink pad");
731 732 733 734 735
  fail_unless (gst_pad_link (srcpad, sinkpad) == GST_PAD_LINK_OK,
      "Could not link %s source and sink pads", GST_ELEMENT_NAME (element));
  gst_object_unref (srcpad);    /* because we got it higher up */
  ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
736
  GST_DEBUG_OBJECT (element, "set up srcpad, refcount is 1");
737 738 739 740 741 742
  return sinkpad;
}

void
gst_check_teardown_sink_pad (GstElement * element)
{
743
  gst_check_teardown_pad_by_name (element, "src");
744
}
745

746 747 748
/**
 * gst_check_drop_buffers:
 *
749 750
 * Unref and remove all buffers that are in the global @buffers GList,
 * emptying the list.
751 752
 */
void
753
gst_check_drop_buffers (void)
754
{
755 756
  while (buffers != NULL) {
    gst_buffer_unref (GST_BUFFER (buffers->data));
757
    buffers = g_list_delete_link (buffers, buffers);
758 759 760 761 762 763 764 765
  }
}

/**
 * gst_check_caps_equal:
 * @caps1: first caps to compare
 * @caps2: second caps to compare
 *
766 767
 * Compare two caps with gst_caps_is_equal and fail unless they are
 * equal.
768 769 770 771 772 773 774 775 776 777 778 779 780
 */
void
gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2)
{
  gchar *name1 = gst_caps_to_string (caps1);
  gchar *name2 = gst_caps_to_string (caps2);

  fail_unless (gst_caps_is_equal (caps1, caps2),
      "caps ('%s') is not equal to caps ('%s')", name1, name2);
  g_free (name1);
  g_free (name2);
}

Wim Taymans's avatar
Wim Taymans committed
781 782 783 784 785 786 787 788 789 790 791 792

/**
 * gst_check_buffer_data:
 * @buffer: buffer to compare
 * @data: data to compare to
 * @size: size of data to compare
 *
 * Compare the buffer contents with @data and @size.
 */
void
gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size)
{
Wim Taymans's avatar
Wim Taymans committed
793
  GstMapInfo info;
Wim Taymans's avatar
Wim Taymans committed
794

795
  fail_unless (gst_buffer_map (buffer, &info, GST_MAP_READ));
Wim Taymans's avatar
Wim Taymans committed
796
  GST_MEMDUMP ("Converted data", info.data, info.size);
797
  GST_MEMDUMP ("Expected data", data, size);
Wim Taymans's avatar
Wim Taymans committed
798
  if (memcmp (info.data, data, size) != 0) {
799
    g_print ("\nConverted data:\n");
Wim Taymans's avatar
Wim Taymans committed
800
    gst_util_dump_mem (info.data, info.size);
801 802
    g_print ("\nExpected data:\n");
    gst_util_dump_mem (data, size);
Stefan Sauer's avatar
Stefan Sauer committed
803
    fail ("buffer contents not equal");
804
  }
Wim Taymans's avatar
Wim Taymans committed
805
  gst_buffer_unmap (buffer, &info);
Wim Taymans's avatar
Wim Taymans committed
806 807
}

808 809 810 811
static gboolean
buffer_event_function (GstPad * pad, GstObject * noparent, GstEvent * event)
{
  if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
812 813
    GstCaps *event_caps;
    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 820 821 822 823 824 825
    gst_event_unref (event);
    return TRUE;
  }

  return gst_pad_event_default (pad, noparent, event);
}

826
/**
827 828
 * gst_check_element_push_buffer_list:
 * @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 833
 * @buffer_out: (element-type GstBuffer) (transfer full): a list of buffers that we expect from
 * the element
834
 * @caps_out: the #GstCaps expected of the srcpad of the element
835 836
 * @last_flow_return: the last buffer push needs to give this GstFlowReturn
 *
837 838 839 840
 * Create an element using the factory providing the @element_name and push the
 * buffers in @buffer_in to this element. The element should create the buffers
 * equal to the buffers in @buffer_out. We only check the size and the data of
 * the buffers. This function unrefs the buffers in the two lists.
841 842 843 844
 * The last_flow_return parameter indicates the expected flow return value from
 * pushing the final buffer in the list.
 * This can be used to set up a test which pushes some buffers and then an
 * 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 848
void
gst_check_element_push_buffer_list (const gchar * element_name,
849 850
    GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
    GstCaps * caps_out, GstFlowReturn last_flow_return)
851 852 853 854 855
{
  GstElement *element;
  GstPad *pad_peer;
  GstPad *sink_pad = NULL;
  GstPad *src_pad;
856
  GstBuffer *buffer;
857 858 859 860 861 862 863 864

  /* check that there are no buffers waiting */
  gst_check_drop_buffers ();
  /* create the element */
  element = gst_check_setup_element (element_name);
  fail_if (element == NULL, "failed to create the element '%s'", element_name);
  fail_unless (GST_IS_ELEMENT (element), "the element is no element");
  /* 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 869 870 871 872 873 874 875
  src_pad = gst_pad_new ("src", GST_PAD_SRC);
  if (caps_in) {
    fail_unless (gst_caps_is_fixed (caps_in));
    gst_pad_use_fixed_caps (src_pad);
  }
  /* activate the pad */
  gst_pad_set_active (src_pad, TRUE);
  GST_DEBUG ("src pad activated");
876
  gst_check_setup_events (src_pad, element, caps_in, GST_FORMAT_BYTES);
Wim Taymans's avatar
Wim Taymans committed
877
  pad_peer = gst_element_get_static_pad (element, "sink");
878 879 880 881 882 883
  fail_if (pad_peer == NULL);
  fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
      "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
  gst_object_unref (pad_peer);
  /* don't create the sink_pad if there is no buffer_out list */
  if (buffer_out != NULL) {
884

885 886
    GST_DEBUG ("buffer out detected, creating the sink pad");
    /* get the sink caps */
887 888 889 890 891 892 893 894 895
    if (caps_out) {
      gchar *temp;

      fail_unless (gst_caps_is_fixed (caps_out));
      temp = gst_caps_to_string (caps_out);

      GST_DEBUG ("sink caps requested by buffer out: '%s'", temp);
      g_free (temp);
    }
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 901 902
    /* configure the sink pad */
    gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
    gst_pad_set_active (sink_pad, TRUE);
903 904
    if (caps_out) {
      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 */
Wim Taymans's avatar
Wim Taymans committed
908
    pad_peer = gst_element_get_static_pad (element, "src");
909 910 911 912 913 914 915 916 917
    fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
        "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
    gst_object_unref (pad_peer);
  }
  fail_unless (gst_element_set_state (element,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
      "could not set to playing");
  /* push all the buffers in the buffer_in list */
  fail_unless (g_list_length (buffer_in) > 0, "the buffer_in list is empty");
918
  while (buffer_in != NULL) {
919 920 921 922 923 924
    GstBuffer *next_buffer = GST_BUFFER (buffer_in->data);

    fail_unless (GST_IS_BUFFER (next_buffer),
        "data in buffer_in should be a buffer");
    /* remove the buffer from the list */
    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 929 930 931 932 933 934 935
    } else {
      fail_unless (gst_pad_push (src_pad, next_buffer) == GST_FLOW_OK,
          "Failed to push buffer in");
    }
  }
  fail_unless (gst_element_set_state (element,
          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
  /* check that there is a buffer out */
936 937
  fail_unless_equals_int (g_list_length (buffers), g_list_length (buffer_out));
  while (buffers != NULL) {
938 939
    GstBuffer *new = GST_BUFFER (buffers->data);
    GstBuffer *orig = GST_BUFFER (buffer_out->data);
Wim Taymans's avatar
Wim Taymans committed
940
    GstMapInfo newinfo, originfo;
941

942 943
    fail_unless (gst_buffer_map (new, &newinfo, GST_MAP_READ));
    fail_unless (gst_buffer_map (orig, &originfo, GST_MAP_READ));
944

Wim Taymans's avatar
Wim Taymans committed
945 946 947 948
    GST_LOG ("orig buffer: size %" G_GSIZE_FORMAT, originfo.size);
    GST_LOG ("new  buffer: size %" G_GSIZE_FORMAT, newinfo.size);
    GST_MEMDUMP ("orig buffer", originfo.data, originfo.size);
    GST_MEMDUMP ("new  buffer", newinfo.data, newinfo.size);
949

950 951 952
    /* remove the buffers */
    buffers = g_list_remove (buffers, new);
    buffer_out = g_list_remove (buffer_out, orig);
953

Wim Taymans's avatar
Wim Taymans committed
954 955 956
    fail_unless (originfo.size == newinfo.size,
        "size of the buffers are not the same");
    fail_unless (memcmp (originfo.data, newinfo.data, newinfo.size) == 0,
957
        "data is not the same");
Wim Taymans's avatar
Wim Taymans committed
958
#if 0
959
    gst_check_caps_equal (GST_BUFFER_CAPS (orig), GST_BUFFER_CAPS (new));
Wim Taymans's avatar
Wim Taymans committed
960
#endif
961

Wim Taymans's avatar
Wim Taymans committed
962 963
    gst_buffer_unmap (orig, &originfo);
    gst_buffer_unmap (new, &newinfo);
964

965 966 967 968 969 970 971 972 973 974 975 976 977
    gst_buffer_unref (new);
    gst_buffer_unref (orig);
  }
  /* teardown the element and pads */
  gst_pad_set_active (src_pad, FALSE);
  gst_check_teardown_src_pad (element);
  gst_pad_set_active (sink_pad, FALSE);
  gst_check_teardown_sink_pad (element);
  gst_check_teardown_element (element);
}

/**
 * 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 985
 * Create an element using the factory providing the @element_name and
 * push the @buffer_in to this element. The element should create one buffer
986 987 988 989 990
 * and this will be compared with @buffer_out. We only check the caps
 * and the data of the buffers. This function unrefs the buffers.
 */
void
gst_check_element_push_buffer (const gchar * element_name,
991 992
    GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
    GstCaps * caps_out)
993 994 995 996 997 998 999
{
  GList *in = NULL;
  GList *out = NULL;

  in = g_list_append (in, buffer_in);
  out = g_list_append (out, buffer_out);

1000 1001
  gst_check_element_push_buffer_list (element_name, in, caps_in, out, caps_out,
      GST_FLOW_OK);
1002 1003
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
void
gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes)
{
  if (have_abi_sizes) {
    gboolean ok = TRUE;
    gint i;

    for (i = 0; list[i].name; i++) {
      if (list[i].size != list[i].abi_size) {
        ok = FALSE;
        g_print ("sizeof(%s) is %d, expected %d\n",
            list[i].name, list[i].size, list[i].abi_size);
      }
    }
    fail_unless (ok, "failed ABI check");
  } else {
1020 1021 1022 1023 1024
    const gchar *fn;

    if ((fn = g_getenv ("GST_ABI"))) {
      GError *err = NULL;
      GString *s;
1025 1026
      gint i;

1027
      s = g_string_new ("\nGstCheckABIStruct list[] = {\n");
1028
      for (i = 0; list[i].name; i++) {
1029 1030 1031 1032 1033 1034 1035 1036
        g_string_append_printf (s, "  {\"%s\", sizeof (%s), %d},\n",
            list[i].name, list[i].name, list[i].size);
      }
      g_string_append (s, "  {NULL, 0, 0}\n");
      g_string_append (s, "};\n");
      if (!g_file_set_contents (fn, s->str, s->len, &err)) {
        g_print ("%s", s->str);
        g_printerr ("\nFailed to write ABI information: %s\n", err->message);
1037
        g_clear_error (&err);
1038 1039
      } else {
        g_print ("\nWrote ABI information to '%s'.\n", fn);
1040
      }
1041
      g_string_free (s, TRUE);
1042 1043 1044 1045 1046 1047
    } else {
      g_print ("No structure size list was generated for this architecture.\n");
      g_print ("Run with GST_ABI environment variable set to output header.\n");
    }
  }
}
1048

1049 1050 1051 1052 1053 1054 1055 1056
/**
 * gst_check_run_suite: (skip)
 * @suite: the check test suite
 * @name: name
 * @fname: file name
 *
 * Returns: number of failed tests
 */
1057 1058 1059
gint
gst_check_run_suite (Suite * suite, const gchar * name, const gchar * fname)
{
1060 1061
  SRunner *sr;
  gchar *xmlfilename = NULL;
1062
  gint nf;
1063
  GTimer *timer;
1064

1065
  sr = srunner_create (suite);
1066 1067 1068

  if (g_getenv ("GST_CHECK_XML")) {
    /* how lucky we are to have __FILE__ end in .c */
1069
    xmlfilename = g_strdup_printf ("%sheck.xml", fname);
1070 1071 1072 1073

    srunner_set_xml (sr, xmlfilename);
  }

1074
  timer = g_timer_new ();
1075 1076
  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
1077 1078 1079
  g_print ("Check suite %s ran in %.3fs (tests failed: %d)\n",
      name, g_timer_elapsed (timer, NULL), nf);
  g_timer_destroy (timer);
1080
  g_free (xmlfilename);
1081 1082 1083
  srunner_free (sr);
  return nf;
}
1084

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
static gboolean
gst_check_have_checks_list (const gchar * env_var_name)
{
  const gchar *env_val;

  env_val = g_getenv (env_var_name);
  return (env_val != NULL && *env_val != '\0');
}

static gboolean
gst_check_func_is_in_list (const gchar * env_var, const gchar * func_name)
1096 1097 1098 1099 1100
{
  const gchar *gst_checks;
  gboolean res = FALSE;
  gchar **funcs, **f;

1101
  gst_checks = g_getenv (env_var);
1102 1103

  if (gst_checks == NULL || *gst_checks == '\0')
1104
    return FALSE;
1105 1106 1107 1108

  /* only run specified functions */
  funcs = g_strsplit (gst_checks, ",", -1);
  for (f = funcs; f != NULL && *f != NULL; ++f) {
1109
    if (g_pattern_match_simple (*f, func_name)) {
1110 1111 1112 1113 1114 1115 1116
      res = TRUE;
      break;
    }
  }
  g_strfreev (funcs);
  return res;
}
1117

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
gboolean
_gst_check_run_test_func (const gchar * func_name)
{
  /* if we have a whitelist, run it only if it's in the whitelist */
  if (gst_check_have_checks_list ("GST_CHECKS"))
    return gst_check_func_is_in_list ("GST_CHECKS", func_name);

  /* if we have a blacklist, run it only if it's not in the blacklist */
  if (gst_check_have_checks_list ("GST_CHECKS_IGNORE"))
    return !gst_check_func_is_in_list ("GST_CHECKS_IGNORE", func_name);

  /* no filter specified => run all checks */
  return TRUE;
}

1133
/**
1134
 * gst_check_setup_events_with_stream_id:
1135 1136 1137 1138
 * @srcpad: The src #GstPad to push on
 * @element: The #GstElement use to create the stream id
 * @caps: (allow-none): #GstCaps in case caps event must be sent
 * @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 1143
 * required events to allow streaming. Caps is optional to allow raw src
 * testing.
1144 1145
 */
void
1146 1147
gst_check_setup_events_with_stream_id (GstPad * srcpad, GstElement * element,
    GstCaps * caps, GstFormat format, const gchar * stream_id)
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
{
  GstSegment segment;

  gst_segment_init (&segment, format);

  fail_unless (gst_pad_push_event (srcpad,
          gst_event_new_stream_start (stream_id)));
  if (caps)
    fail_unless (gst_pad_push_event (srcpad, gst_event_new_caps (caps)));
  fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&segment)));
1158
}
1159

1160 1161 1162 1163 1164 1165 1166
/**
 * gst_check_setup_events:
 * @srcpad: The src #GstPad to push on
 * @element: The #GstElement use to create the stream id
 * @caps: (allow-none): #GstCaps in case caps event must be sent
 * @format: The #GstFormat of the default segment to send
 *