adder.c 21.9 KB
Newer Older
1
2
/* GStreamer
 *
3
 * unit test for adder
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
 *
 * 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
24
25
26
27
28
29
30
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_VALGRIND
# include <valgrind/valgrind.h>
#endif

31
32
33
34
#include <unistd.h>

#include <gst/check/gstcheck.h>

35
36
37
38
39
static GMainLoop *main_loop;

static void
message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
{
40
41
  GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
      GST_MESSAGE_SRC (message), message);
42
43
44
45
46

  switch (message->type) {
    case GST_MESSAGE_EOS:
      g_main_loop_quit (main_loop);
      break;
47
48
49
50
51
52
53
54
55
56
    case GST_MESSAGE_WARNING:{
      GError *gerror;
      gchar *debug;

      gst_message_parse_warning (message, &gerror, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
      g_error_free (gerror);
      g_free (debug);
      break;
    }
57
58
59
60
61
62
63
64
65
    case GST_MESSAGE_ERROR:{
      GError *gerror;
      gchar *debug;

      gst_message_parse_error (message, &gerror, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
      g_error_free (gerror);
      g_free (debug);
      g_main_loop_quit (main_loop);
66
      break;
67
68
69
70
71
    }
    default:
      break;
  }
}
72
73


74
75
76
77
78
79
80
static GstFormat format = GST_FORMAT_UNDEFINED;
static gint64 position = -1;

static void
test_event_message_received (GstBus * bus, GstMessage * message,
    GstPipeline * bin)
{
81
82
  GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
      GST_MESSAGE_SRC (message), message);
83
84
85
86
87
88
89
90

  switch (message->type) {
    case GST_MESSAGE_SEGMENT_DONE:
      gst_message_parse_segment_done (message, &format, &position);
      GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
      g_main_loop_quit (main_loop);
      break;
    default:
91
      g_assert_not_reached ();
92
93
94
95
96
      break;
  }
}


97
98
99
GST_START_TEST (test_event)
{
  GstElement *bin, *src1, *src2, *adder, *sink;
100
  GstBus *bus;
101
102
103
  GstEvent *seek_event;
  gboolean res;

104
  GST_INFO ("preparing test");
105

106
107
  /* build pipeline */
  bin = gst_pipeline_new ("pipeline");
108
109
110
  bus = gst_element_get_bus (bin);
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);

111
112
113
  /* FIXME, fakesrc with default setting will produce 0 sized
   * buffers and incompatible caps for adder that will make
   * adder EOS and error out */
114
115
116
117
  src1 = gst_element_factory_make ("audiotestsrc", "src1");
  g_object_set (src1, "wave", 4, NULL); /* silence */
  src2 = gst_element_factory_make ("audiotestsrc", "src2");
  g_object_set (src2, "wave", 4, NULL); /* silence */
118
119
120
121
122
123
124
125
126
127
128
129
  adder = gst_element_factory_make ("adder", "adder");
  sink = gst_element_factory_make ("fakesink", "sink");
  gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);

  res = gst_element_link (src1, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (src2, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (adder, sink);
  fail_unless (res == TRUE, NULL);

  seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
130
      GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
131
      GST_SEEK_TYPE_SET, (GstClockTime) 0,
132
      GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
133

134
135
136
  format = GST_FORMAT_UNDEFINED;
  position = -1;

137
  main_loop = g_main_loop_new (NULL, FALSE);
138
139
  g_signal_connect (bus, "message::segment-done",
      (GCallback) test_event_message_received, bin);
140
141
142
143
  g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);

144
145
  GST_INFO ("starting test");

146
147
148
149
  /* prepare playing */
  res = gst_element_set_state (bin, GST_STATE_PAUSED);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

150
151
152
153
  /* wait for completion */
  res = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

154
155
156
157
158
159
160
  res = gst_element_send_event (bin, seek_event);
  fail_unless (res == TRUE, NULL);

  /* run pipeline */
  res = gst_element_set_state (bin, GST_STATE_PLAYING);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

161
  g_main_loop_run (main_loop);
162

163
164
165
  res = gst_element_set_state (bin, GST_STATE_NULL);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

166
  fail_unless (position == 2 * GST_SECOND, NULL);
167
168

  /* cleanup */
169
170
  g_main_loop_unref (main_loop);
  gst_object_unref (G_OBJECT (bus));
171
172
173
174
175
  gst_object_unref (G_OBJECT (bin));
}

GST_END_TEST;

176
177
178
179
180
181
182
183
184
static guint play_count = 0;
static GstEvent *play_seek_event = NULL;

static void
test_play_twice_message_received (GstBus * bus, GstMessage * message,
    GstPipeline * bin)
{
  gboolean res;

185
186
  GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
      GST_MESSAGE_SRC (message), message);
187
188
189
190
191
192
193
194
195
196
197
198

  switch (message->type) {
    case GST_MESSAGE_SEGMENT_DONE:
      play_count++;
      if (play_count == 1) {
        res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
        fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

        /* prepare playing again */
        res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
        fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

199
200
201
202
203
204
        /* wait for completion */
        res =
            gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
            GST_CLOCK_TIME_NONE);
        fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

205
206
        res = gst_element_send_event (GST_ELEMENT (bin),
            gst_event_ref (play_seek_event));
207
208
209
210
211
212
213
214
215
        fail_unless (res == TRUE, NULL);

        res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
        fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
      } else {
        g_main_loop_quit (main_loop);
      }
      break;
    default:
216
      g_assert_not_reached ();
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
248
249
250
      break;
  }
}


GST_START_TEST (test_play_twice)
{
  GstElement *bin, *src1, *src2, *adder, *sink;
  GstBus *bus;
  gboolean res;

  GST_INFO ("preparing test");

  /* build pipeline */
  bin = gst_pipeline_new ("pipeline");
  bus = gst_element_get_bus (bin);
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);

  src1 = gst_element_factory_make ("audiotestsrc", "src1");
  g_object_set (src1, "wave", 4, NULL); /* silence */
  src2 = gst_element_factory_make ("audiotestsrc", "src2");
  g_object_set (src2, "wave", 4, NULL); /* silence */
  adder = gst_element_factory_make ("adder", "adder");
  sink = gst_element_factory_make ("fakesink", "sink");
  gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);

  res = gst_element_link (src1, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (src2, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (adder, sink);
  fail_unless (res == TRUE, NULL);

  play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
251
      GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
      GST_SEEK_TYPE_SET, (GstClockTime) 0,
      GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);

  play_count = 0;

  main_loop = g_main_loop_new (NULL, FALSE);
  g_signal_connect (bus, "message::segment-done",
      (GCallback) test_play_twice_message_received, bin);
  g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);

  GST_INFO ("starting test");

  /* prepare playing */
  res = gst_element_set_state (bin, GST_STATE_PAUSED);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

270
271
272
273
274
275
  /* wait for completion */
  res =
      gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
      GST_CLOCK_TIME_NONE);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

276
277
278
  res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
  fail_unless (res == TRUE, NULL);

279
280
  GST_INFO ("seeked");

281
282
283
284
285
286
287
288
289
290
291
292
293
  /* run pipeline */
  res = gst_element_set_state (bin, GST_STATE_PLAYING);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  g_main_loop_run (main_loop);

  res = gst_element_set_state (bin, GST_STATE_NULL);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  fail_unless (play_count == 2, NULL);

  /* cleanup */
  g_main_loop_unref (main_loop);
294
  gst_event_ref (play_seek_event);
295
296
297
298
299
300
  gst_object_unref (G_OBJECT (bus));
  gst_object_unref (G_OBJECT (bin));
}

GST_END_TEST;

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
GST_START_TEST (test_play_twice_then_add_and_play_again)
{
  GstElement *bin, *src1, *src2, *src3, *adder, *sink;
  GstBus *bus;
  gboolean res;
  gint i;

  GST_INFO ("preparing test");

  /* build pipeline */
  bin = gst_pipeline_new ("pipeline");
  bus = gst_element_get_bus (bin);
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);

  src1 = gst_element_factory_make ("audiotestsrc", "src1");
  g_object_set (src1, "wave", 4, NULL); /* silence */
  src2 = gst_element_factory_make ("audiotestsrc", "src2");
  g_object_set (src2, "wave", 4, NULL); /* silence */
  adder = gst_element_factory_make ("adder", "adder");
  sink = gst_element_factory_make ("fakesink", "sink");
  gst_bin_add_many (GST_BIN (bin), src1, src2, adder, sink, NULL);

  res = gst_element_link (src1, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (src2, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (adder, sink);
  fail_unless (res == TRUE, NULL);

  play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
      GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_SET, (GstClockTime) 0,
      GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_signal_connect (bus, "message::segment-done",
      (GCallback) test_play_twice_message_received, bin);
  g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);

  /* run it twice */
  for (i = 0; i < 2; i++) {
    play_count = 0;

    GST_INFO ("starting test-loop %d", i);

    /* prepare playing */
    res = gst_element_set_state (bin, GST_STATE_PAUSED);
    fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

    /* wait for completion */
    res =
        gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
        GST_CLOCK_TIME_NONE);
    fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

    res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
    fail_unless (res == TRUE, NULL);

    GST_INFO ("seeked");

    /* run pipeline */
    res = gst_element_set_state (bin, GST_STATE_PLAYING);
    fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

    g_main_loop_run (main_loop);

    res = gst_element_set_state (bin, GST_STATE_READY);
    fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

    fail_unless (play_count == 2, NULL);

    /* plug another source */
    if (i == 0) {
      src3 = gst_element_factory_make ("audiotestsrc", "src3");
      g_object_set (src3, "wave", 4, NULL);     /* silence */
      gst_bin_add (GST_BIN (bin), src3);

      res = gst_element_link (src3, adder);
      fail_unless (res == TRUE, NULL);
    }
  }

  res = gst_element_set_state (bin, GST_STATE_NULL);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  /* cleanup */
  g_main_loop_unref (main_loop);
  gst_event_ref (play_seek_event);
  gst_object_unref (G_OBJECT (bus));
  gst_object_unref (G_OBJECT (bin));
}

GST_END_TEST;


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
static void
test_live_seeking_eos_message_received (GstBus * bus, GstMessage * message,
    GstPipeline * bin)
{
  GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
      GST_MESSAGE_SRC (message), message);

  switch (message->type) {
    case GST_MESSAGE_EOS:
      g_main_loop_quit (main_loop);
      break;
    default:
      g_assert_not_reached ();
      break;
  }
}


/* test failing seeks on live-sources */
GST_START_TEST (test_live_seeking)
{
Stefan Kost's avatar
Stefan Kost committed
419
  GstElement *bin, *src1, *src2, *ac1, *ac2, *adder, *sink;
420
421
422
423
424
  GstBus *bus;
  gboolean res;
  gint i;

  GST_INFO ("preparing test");
425
426
  main_loop = NULL;
  play_seek_event = NULL;
427
428
429
430
431
432

  /* build pipeline */
  bin = gst_pipeline_new ("pipeline");
  bus = gst_element_get_bus (bin);
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);

433
  /* normal audiosources behave differently than audiotestsrc */
434
435
436
437
438
#if 0
  src1 = gst_element_factory_make ("audiotestsrc", "src1");
  g_object_set (src1, "wave", 4, "is-live", TRUE, NULL);        /* silence */
#else
  src1 = gst_element_factory_make ("alsasrc", "src1");
439
440
  if (!src1) {
    GST_INFO ("no audiosrc, skipping");
441
    goto cleanup;
442
  }
443
444
445
  /* Test that the audio source can get to paused, else skip */
  res = gst_element_set_state (src1, GST_STATE_PAUSED);
  (void) gst_element_set_state (src1, GST_STATE_NULL);
446
  (void) gst_element_get_state (src1, NULL, NULL, GST_CLOCK_TIME_NONE);
447
448
449
450
451
  if (res == GST_STATE_CHANGE_FAILURE) {
    gst_object_unref (src1);
    goto cleanup;
  }

452
  /* live sources ignore seeks, force eos after 2 sec (4 buffers half second
453
   * each) - don't use autoaudiosrc, as then we can't set anything here */
454
455
  g_object_set (src1, "num-buffers", 4, "blocksize", 44100, NULL);
#endif
Stefan Kost's avatar
Stefan Kost committed
456
  ac1 = gst_element_factory_make ("audioconvert", "ac1");
457
458
  src2 = gst_element_factory_make ("audiotestsrc", "src2");
  g_object_set (src2, "wave", 4, NULL); /* silence */
Stefan Kost's avatar
Stefan Kost committed
459
  ac2 = gst_element_factory_make ("audioconvert", "ac2");
460
461
  adder = gst_element_factory_make ("adder", "adder");
  sink = gst_element_factory_make ("fakesink", "sink");
Stefan Kost's avatar
Stefan Kost committed
462
  gst_bin_add_many (GST_BIN (bin), src1, ac1, src2, ac2, adder, sink, NULL);
463

Stefan Kost's avatar
Stefan Kost committed
464
  res = gst_element_link (src1, ac1);
465
  fail_unless (res == TRUE, NULL);
Stefan Kost's avatar
Stefan Kost committed
466
467
468
469
470
  res = gst_element_link (ac1, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (src2, ac2);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (ac2, adder);
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (adder, sink);
  fail_unless (res == TRUE, NULL);

  play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
      GST_SEEK_FLAG_FLUSH,
      GST_SEEK_TYPE_SET, (GstClockTime) 0,
      GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::eos",
      (GCallback) test_live_seeking_eos_message_received, bin);

  /* run it twice */
  for (i = 0; i < 2; i++) {

489
490
    GST_INFO ("starting test-loop %d", i);

491
492
    /* prepare playing */
    res = gst_element_set_state (bin, GST_STATE_PAUSED);
493
    fail_unless (res != GST_STATE_CHANGE_FAILURE);
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523

    /* wait for completion */
    res =
        gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
        GST_CLOCK_TIME_NONE);
    fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

    res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
#if 0
    fail_unless (res == TRUE, NULL);
#else
    /* adder is picky, if a single seek fails it totaly fails */
    fail_unless (res == FALSE, NULL);
#endif

    GST_INFO ("seeked");

    /* run pipeline */
    res = gst_element_set_state (bin, GST_STATE_PLAYING);
    fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

    GST_INFO ("playing");

    g_main_loop_run (main_loop);

    res = gst_element_set_state (bin, GST_STATE_NULL);
    fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
  }

  /* cleanup */
524
cleanup:
525
  GST_INFO ("cleaning up");
526
527
528
529
  if (main_loop)
    g_main_loop_unref (main_loop);
  if (play_seek_event)
    gst_event_unref (play_seek_event);
530
531
532
533
534
535
  gst_object_unref (G_OBJECT (bus));
  gst_object_unref (G_OBJECT (bin));
}

GST_END_TEST;

536
/* check if adding pads work as expected */
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
GST_START_TEST (test_add_pad)
{
  GstElement *bin, *src1, *src2, *adder, *sink;
  GstBus *bus;
  gboolean res;

  GST_INFO ("preparing test");

  /* build pipeline */
  bin = gst_pipeline_new ("pipeline");
  bus = gst_element_get_bus (bin);
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);

  src1 = gst_element_factory_make ("audiotestsrc", "src1");
  g_object_set (src1, "num-buffers", 4, NULL);
  g_object_set (src1, "wave", 4, NULL); /* silence */
  src2 = gst_element_factory_make ("audiotestsrc", "src2");
  /* one buffer less, we connect with 1 buffer of delay */
  g_object_set (src2, "num-buffers", 3, NULL);
  g_object_set (src2, "wave", 4, NULL); /* silence */
  adder = gst_element_factory_make ("adder", "adder");
  sink = gst_element_factory_make ("fakesink", "sink");
  gst_bin_add_many (GST_BIN (bin), src1, adder, sink, NULL);

  res = gst_element_link (src1, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (adder, sink);
  fail_unless (res == TRUE, NULL);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
      bin);
  g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);

  GST_INFO ("starting test");

  /* prepare playing */
  res = gst_element_set_state (bin, GST_STATE_PAUSED);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  /* wait for completion */
  res =
      gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
      GST_CLOCK_TIME_NONE);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  /* add other element */
  gst_bin_add_many (GST_BIN (bin), src2, NULL);

  /* now link the second element */
  res = gst_element_link (src2, adder);
  fail_unless (res == TRUE, NULL);

  /* set to PAUSED as well */
  res = gst_element_set_state (src2, GST_STATE_PAUSED);

  /* now play all */
  res = gst_element_set_state (bin, GST_STATE_PLAYING);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  g_main_loop_run (main_loop);

  res = gst_element_set_state (bin, GST_STATE_NULL);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  /* cleanup */
  g_main_loop_unref (main_loop);
  gst_object_unref (G_OBJECT (bus));
  gst_object_unref (G_OBJECT (bin));
}

GST_END_TEST;
611

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/* check if removing pads work as expected */
GST_START_TEST (test_remove_pad)
{
  GstElement *bin, *src, *adder, *sink;
  GstBus *bus;
  GstPad *pad;
  gboolean res;

  GST_INFO ("preparing test");

  /* build pipeline */
  bin = gst_pipeline_new ("pipeline");
  bus = gst_element_get_bus (bin);
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);

  src = gst_element_factory_make ("audiotestsrc", "src");
  g_object_set (src, "num-buffers", 4, NULL);
  g_object_set (src, "wave", 4, NULL);
  adder = gst_element_factory_make ("adder", "adder");
  sink = gst_element_factory_make ("fakesink", "sink");
  gst_bin_add_many (GST_BIN (bin), src, adder, sink, NULL);

  res = gst_element_link (src, adder);
  fail_unless (res == TRUE, NULL);
  res = gst_element_link (adder, sink);
  fail_unless (res == TRUE, NULL);

  /* create an unconnected sinkpad in adder */
  pad = gst_element_get_request_pad (adder, "sink%d");
  fail_if (pad == NULL, NULL);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
      bin);
  g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
  g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);

  GST_INFO ("starting test");

  /* prepare playing, this will not preroll as adder is waiting
   * on the unconnected sinkpad. */
  res = gst_element_set_state (bin, GST_STATE_PAUSED);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  /* wait for completion for one second, will return ASYNC */
  res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
  fail_unless (res == GST_STATE_CHANGE_ASYNC, NULL);

  /* get rid of the pad now, adder should stop waiting on it and
   * continue the preroll */
  gst_element_release_request_pad (adder, pad);
  gst_object_unref (pad);

  /* wait for completion, should work now */
  res =
      gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
      GST_CLOCK_TIME_NONE);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  /* now play all */
  res = gst_element_set_state (bin, GST_STATE_PLAYING);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  g_main_loop_run (main_loop);

  res = gst_element_set_state (bin, GST_STATE_NULL);
  fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);

  /* cleanup */
  g_main_loop_unref (main_loop);
  gst_object_unref (G_OBJECT (bus));
  gst_object_unref (G_OBJECT (bin));
}

GST_END_TEST;


690
static Suite *
691
692
693
694
695
696
697
adder_suite (void)
{
  Suite *s = suite_create ("adder");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_event);
698
  tcase_add_test (tc_chain, test_play_twice);
699
  tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
700
  tcase_add_test (tc_chain, test_live_seeking);
701
  tcase_add_test (tc_chain, test_add_pad);
702
  tcase_add_test (tc_chain, test_remove_pad);
703

704
  /* Use a longer timeout */
705
706
707
708
709
710
711
712
713
#ifdef HAVE_VALGRIND
  if (RUNNING_ON_VALGRIND) {
    tcase_set_timeout (tc_chain, 5 * 60);
  } else
#endif
  {
    /* this is shorter than the default 60 seconds?! (tpm) */
    /* tcase_set_timeout (tc_chain, 6); */
  }
714

715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  return s;
}

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

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

  gst_check_init (&argc, &argv);

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

  return nf;
}