GitLab will be down for maintenance this Sunday 13th June, from approx 7-11am UTC. This is for a PostgreSQL migration. See the tracker issue for more informations.

controller.c 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GStreamer
 *
 * unit test for the controller library
 *
 * Copyright (C) <2005> Stefan Kost <ensonic at users dot sf dor net>
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25 26
#endif
#include <gst/gst.h>
27
#include <gst/check/gstcheck.h>
28
#include <gst/controller/gstcontroller.h>
29 30 31 32 33 34 35

/* LOCAL TEST ELEMENT */

enum
{
  ARG_ULONG = 1,
  ARG_DOUBLE,
36
  ARG_BOOLEAN,
37 38 39
  ARG_READONLY,
  ARG_STATIC,
  ARG_CONSTRUCTONLY,
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  ARG_COUNT
};

#define GST_TYPE_TEST_MONO_SOURCE            (gst_test_mono_source_get_type ())
#define GST_TEST_MONO_SOURCE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TEST_MONO_SOURCE, GstTestMonoSource))
#define GST_TEST_MONO_SOURCE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TEST_MONO_SOURCE, GstTestMonoSourceClass))
#define GST_IS_TEST_MONO_SOURCE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_TEST_MONO_SOURCE))
#define GST_IS_TEST_MONO_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_TEST_MONO_SOURCE))
#define GST_TEST_MONO_SOURCE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_TEST_MONO_SOURCE, GstTestMonoSourceClass))

typedef struct _GstTestMonoSource GstTestMonoSource;
typedef struct _GstTestMonoSourceClass GstTestMonoSourceClass;

struct _GstTestMonoSource
{
  GstElement parent;
56 57
  gulong val_ulong;
  gdouble val_double;
58
  gboolean val_boolean;
59 60 61 62 63 64 65 66 67 68 69 70
};
struct _GstTestMonoSourceClass
{
  GstElementClass parent_class;
};

GType gst_test_mono_source_get_type (void);

static void
gst_test_mono_source_get_property (GObject * object,
    guint property_id, GValue * value, GParamSpec * pspec)
{
71
  GstTestMonoSource *self = GST_TEST_MONO_SOURCE (object);
72 73 74

  switch (property_id) {
    case ARG_ULONG:
75
      g_value_set_ulong (value, self->val_ulong);
76
      break;
77 78 79
    case ARG_DOUBLE:
      g_value_set_double (value, self->val_double);
      break;
80 81 82
    case ARG_BOOLEAN:
      g_value_set_boolean (value, self->val_boolean);
      break;
83
    default:
84 85 86 87 88 89 90 91 92
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

static void
gst_test_mono_source_set_property (GObject * object,
    guint property_id, const GValue * value, GParamSpec * pspec)
{
93
  GstTestMonoSource *self = GST_TEST_MONO_SOURCE (object);
94 95 96

  switch (property_id) {
    case ARG_ULONG:
97 98 99 100
      self->val_ulong = g_value_get_ulong (value);
      break;
    case ARG_DOUBLE:
      self->val_double = g_value_get_double (value);
101
      break;
102 103 104 105 106
    case ARG_BOOLEAN:
      self->val_boolean = g_value_get_boolean (value);
      break;
    case ARG_CONSTRUCTONLY:
      break;
107
    default:
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

static void
gst_test_mono_source_class_init (GstTestMonoSourceClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = gst_test_mono_source_set_property;
  gobject_class->get_property = gst_test_mono_source_get_property;

  g_object_class_install_property (gobject_class, ARG_ULONG,
      g_param_spec_ulong ("ulong",
          "ulong prop",
          "ulong number parameter for the test_mono_source",
          0, G_MAXULONG, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
126 127 128 129 130 131

  g_object_class_install_property (gobject_class, ARG_DOUBLE,
      g_param_spec_double ("double",
          "double prop",
          "double number parameter for the test_mono_source",
          0.0, 100.0, 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

  g_object_class_install_property (gobject_class, ARG_BOOLEAN,
      g_param_spec_boolean ("boolean",
          "boolean prop",
          "boolean parameter for the test_mono_source",
          FALSE, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));

  g_object_class_install_property (gobject_class, ARG_READONLY,
      g_param_spec_ulong ("readonly",
          "readonly prop",
          "readonly parameter for the test_mono_source",
          0, G_MAXULONG, 0, G_PARAM_READABLE | GST_PARAM_CONTROLLABLE));

  g_object_class_install_property (gobject_class, ARG_STATIC,
      g_param_spec_ulong ("static",
          "static prop",
          "static parameter for the test_mono_source",
          0, G_MAXULONG, 0, G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, ARG_CONSTRUCTONLY,
      g_param_spec_ulong ("construct-only",
          "construct-only prop",
          "construct-only parameter for the test_mono_source",
          0, G_MAXULONG, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
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
}

static void
gst_test_mono_source_base_init (GstTestMonoSourceClass * klass)
{
  static const GstElementDetails details = {
    "Monophonic source for unit tests",
    "Source/Audio/MonoSource",
    "Use in unit tests",
    "Stefan Kost <ensonic@users.sf.net>"
  };
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_set_details (element_class, &details);
}

GType
gst_test_mono_source_get_type (void)
{
  static GType type = 0;

  if (type == 0) {
    static const GTypeInfo info = {
      (guint16) sizeof (GstTestMonoSourceClass),
      (GBaseInitFunc) gst_test_mono_source_base_init,   // base_init
      NULL,                     // base_finalize
      (GClassInitFunc) gst_test_mono_source_class_init, // class_init
      NULL,                     // class_finalize
      NULL,                     // class_data
      (guint16) sizeof (GstTestMonoSource),
      0,                        // n_preallocs
      NULL,                     // instance_init
      NULL                      // value_table
    };
    type =
        g_type_register_static (GST_TYPE_ELEMENT, "GstTestMonoSource", &info,
        0);
  }
  return type;
}

static gboolean
198
plugin_init (GstPlugin * plugin)
199
{
200 201 202
  gboolean res = TRUE;

  res &= gst_element_register (plugin, "testmonosource", GST_RANK_NONE,
203
      GST_TYPE_TEST_MONO_SOURCE);
204
  return res;
205 206 207 208 209 210
}

GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "gst-test",
    "controller test plugin - several unit test support elements",
211
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN);
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
/*
static void __attribute__ ((constructor))
_gst_plugin_static_init__plugin_init (void)
{
  static GstPluginDesc plugin_desc_ = {
    GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "gst-test",
    "controller test plugin - several unit test support elements",
    plugin_init,
    VERSION,
    GST_LICENSE,
    PACKAGE,
    GST_PACKAGE,
    GST_ORIGIN,
    GST_PADDING_INIT
  };
  _gst_plugin_register_static (&plugin_desc_);
}
*/
233 234
/* TESTS */
/* double init should not harm */
235
GST_START_TEST (controller_init)
236 237 238 239
{
  gst_controller_init (NULL, NULL);
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
240 241
GST_END_TEST;

242
/* tests for an element with no controlled params */
243
GST_START_TEST (controller_new_fail1)
244 245 246 247 248 249
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("fakesrc", "test_source");

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
250
  /* that property should not exist */
251 252 253
  ctrl = gst_controller_new (G_OBJECT (elem), "_schrompf_", NULL);
  fail_unless (ctrl == NULL, NULL);

254
  gst_object_unref (elem);
255 256 257 258
}

GST_END_TEST;

259 260 261 262 263 264 265 266 267 268 269 270
/* tests for an element with controlled params, but none given */
GST_START_TEST (controller_new_fail2)
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* no property given */
  ctrl = gst_controller_new (G_OBJECT (elem), NULL);
  fail_unless (ctrl == NULL, NULL);

271
  gst_object_unref (elem);
272 273 274 275
}

GST_END_TEST;

276 277 278 279 280 281 282 283 284 285 286 287 288
/* tests for readonly params */
GST_START_TEST (controller_new_fail3)
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and but is readonly */
  ASSERT_CRITICAL (ctrl =
      gst_controller_new (G_OBJECT (elem), "readonly", NULL));
  fail_unless (ctrl == NULL, NULL);

289
  gst_object_unref (elem);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

GST_END_TEST;

/* tests for static params */
GST_START_TEST (controller_new_fail4)
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and but is not controlable */
  ASSERT_CRITICAL (ctrl = gst_controller_new (G_OBJECT (elem), "static", NULL));
  fail_unless (ctrl == NULL, NULL);

306
  gst_object_unref (elem);
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
}

GST_END_TEST;

/* tests for static params */
GST_START_TEST (controller_new_fail5)
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and but is construct-only */
  ASSERT_CRITICAL (ctrl =
      gst_controller_new (G_OBJECT (elem), "construct-only", NULL));
  fail_unless (ctrl == NULL, NULL);

324
  gst_object_unref (elem);
325 326 327 328 329
}

GST_END_TEST;


330 331 332 333 334 335 336 337 338 339 340 341
/* tests for an element with controlled params */
GST_START_TEST (controller_new_okay1)
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and should be controllable */
  ctrl = gst_controller_new (G_OBJECT (elem), "ulong", NULL);
  fail_unless (ctrl != NULL, NULL);

342
  GST_INFO ("controller->ref_count=%d", G_OBJECT (ctrl)->ref_count);
343
  g_object_unref (ctrl);
344
  gst_object_unref (elem);
345 346 347 348
}

GST_END_TEST;

349
/* tests for an element with several controlled params */
350
GST_START_TEST (controller_new_okay2)
351 352 353 354 355 356 357 358 359 360
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and should be controllable */
  ctrl = gst_controller_new (G_OBJECT (elem), "ulong", "double", NULL);
  fail_unless (ctrl != NULL, NULL);

361
  GST_INFO ("controller->ref_count=%d", G_OBJECT (ctrl)->ref_count);
362
  g_object_unref (ctrl);
363
  gst_object_unref (elem);
364 365 366 367 368 369
}

GST_END_TEST;

/* controlling several params should return the same controller */
GST_START_TEST (controller_new_okay3)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
{
  GstController *ctrl1, *ctrl2;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and should be controllable */
  ctrl1 = gst_controller_new (G_OBJECT (elem), "ulong", NULL);
  fail_unless (ctrl1 != NULL, NULL);

  /* that property should exist and should be controllable */
  ctrl2 = gst_controller_new (G_OBJECT (elem), "double", NULL);
  fail_unless (ctrl2 != NULL, NULL);
  fail_unless (ctrl1 == ctrl2, NULL);

385
  GST_INFO ("controller->ref_count=%d", G_OBJECT (ctrl1)->ref_count);
386
  g_object_unref (ctrl1);
387
  gst_object_unref (elem);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
}

GST_END_TEST;

/* controlling a params twice should be handled */
GST_START_TEST (controller_param_twice)
{
  GstController *ctrl;
  GstElement *elem;
  gboolean res;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and should be controllable */
  ctrl = gst_controller_new (G_OBJECT (elem), "ulong", "ulong", NULL);
  fail_unless (ctrl != NULL, NULL);

  /* it should have been added at least once, let remove it */
  res = gst_controller_remove_properties (ctrl, "ulong", NULL);
  fail_unless (res, NULL);

  /* removing it agian should not work */
  res = gst_controller_remove_properties (ctrl, "ulong", NULL);
  fail_unless (!res, NULL);

413
  GST_INFO ("controller->ref_count=%d", G_OBJECT (ctrl)->ref_count);
414
  g_object_unref (ctrl);
415
  gst_object_unref (elem);
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
}

GST_END_TEST;

/* tests if we cleanup properly */
GST_START_TEST (controller_finalize)
{
  GstController *ctrl;
  GstElement *elem;

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and should be controllable */
  ctrl = gst_controller_new (G_OBJECT (elem), "ulong", NULL);
  fail_unless (ctrl != NULL, NULL);

  /* free the controller */
  g_object_unref (ctrl);

  /* object shouldn't have a controller anymore */
  ctrl = gst_object_get_controller (G_OBJECT (elem));
437 438
  fail_unless (ctrl == NULL, NULL);

439
  gst_object_unref (elem);
440 441
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
442 443
GST_END_TEST;

444 445
/* test timed value handling without interpolation */
GST_START_TEST (controller_interpolate_none)
446 447 448
{
  GstController *ctrl;
  GstElement *elem;
449 450
  gboolean res;
  GValue val_ulong = { 0, };
451 452 453 454 455 456 457

  elem = gst_element_factory_make ("testmonosource", "test_source");

  /* that property should exist and should be controllable */
  ctrl = gst_controller_new (G_OBJECT (elem), "ulong", NULL);
  fail_unless (ctrl != NULL, NULL);

458 459 460 461 462 463 464 465 466 467 468 469 470
  /* set interpolation mode */
  gst_controller_set_interpolation_mode (ctrl, "ulong", GST_INTERPOLATE_NONE);

  /* set control values */
  g_value_init (&val_ulong, G_TYPE_ULONG);
  g_value_set_ulong (&val_ulong, 0);
  res = gst_controller_set (ctrl, "ulong", 0 * GST_SECOND, &val_ulong);
  fail_unless (res, NULL);
  g_value_set_ulong (&val_ulong, 100);
  res = gst_controller_set (ctrl, "ulong", 2 * GST_SECOND, &val_ulong);
  fail_unless (res, NULL);

  /* now pull in values for some timestamps */
471
  gst_controller_sync_values (ctrl, 0 * GST_SECOND);
472
  fail_unless (GST_TEST_MONO_SOURCE (elem)->val_ulong == 0, NULL);
473
  gst_controller_sync_values (ctrl, 1 * GST_SECOND);
474
  fail_unless (GST_TEST_MONO_SOURCE (elem)->val_ulong == 0, NULL);
475
  gst_controller_sync_values (ctrl, 2 * GST_SECOND);
476 477
  fail_unless (GST_TEST_MONO_SOURCE (elem)->val_ulong == 100, NULL);

478
  GST_INFO ("controller->ref_count=%d", G_OBJECT (ctrl)->ref_count);
479
  g_object_unref (ctrl);
480
  gst_object_unref (elem);
481 482
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
483 484
GST_END_TEST;

485 486 487 488 489 490 491 492 493
/* tests if we can run helper methods against any GObject */
GST_START_TEST (controller_helper_any_gobject)
{
  GstElement *elem;
  gboolean res;

  elem = gst_element_factory_make ("bin", "test_elem");

  /* that element is not controllable */
494
  res = gst_object_sync_values (G_OBJECT (elem), 0LL);
495 496
  fail_unless (res == FALSE, NULL);

497
  gst_object_unref (elem);
498 499 500 501 502
}

GST_END_TEST;


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
503 504
Suite *
gst_controller_suite (void)
505 506 507 508 509 510
{
  Suite *s = suite_create ("Controller");
  TCase *tc = tcase_create ("general");

  suite_add_tcase (s, tc);
  tcase_add_test (tc, controller_init);
511 512
  tcase_add_test (tc, controller_new_fail1);
  tcase_add_test (tc, controller_new_fail2);
513 514 515
  tcase_add_test (tc, controller_new_fail3);
  tcase_add_test (tc, controller_new_fail4);
  tcase_add_test (tc, controller_new_fail5);
516 517
  tcase_add_test (tc, controller_new_okay1);
  tcase_add_test (tc, controller_new_okay2);
518
  tcase_add_test (tc, controller_new_okay3);
519 520 521
  tcase_add_test (tc, controller_param_twice);
  tcase_add_test (tc, controller_finalize);
  tcase_add_test (tc, controller_interpolate_none);
522
  tcase_add_test (tc, controller_helper_any_gobject);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

  return s;
}

int
main (int argc, char **argv)
{
  int nf;

  Suite *s = gst_controller_suite ();
  SRunner *sr = srunner_create (s);

  gst_check_init (&argc, &argv);
  gst_controller_init (NULL, NULL);

  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}