gstpipeline.c 24.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* GStreamer
 * Copyright (C) 2005 Thomas Vander Stichele <thomas at apestaart dot org>
 *
 * gstpipeline.c: Unit test for GstPipeline
 *
 * 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
18
19
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
20
 */
21
22
23
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
24
25

#include <gst/check/gstcheck.h>
26
#include <gst/gst.h>
27
#include <gst/check/gsttestclock.h>
28

29
/* an empty pipeline can go to PLAYING in one go */
30
31
32
33
34
35
36
37
GST_START_TEST (test_async_state_change_empty)
{
  GstPipeline *pipeline;

  pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
  fail_unless (pipeline != NULL, "Could not create pipeline");

  fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline),
38
          GST_STATE_PLAYING), GST_STATE_CHANGE_SUCCESS);
39

40
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  gst_object_unref (pipeline);
}

GST_END_TEST;

GST_START_TEST (test_async_state_change_fake_ready)
{
  GstPipeline *pipeline;
  GstElement *src, *sink;

  pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
  fail_unless (pipeline != NULL, "Could not create pipeline");

  src = gst_element_factory_make ("fakesrc", NULL);
  sink = gst_element_factory_make ("fakesink", NULL);

  gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
  gst_element_link (src, sink);

  fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline),
61
          GST_STATE_READY), GST_STATE_CHANGE_SUCCESS);
62

63
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
64
65
66
67
68
69
70
71
72
73
  gst_object_unref (pipeline);
}

GST_END_TEST;

GST_START_TEST (test_async_state_change_fake)
{
  GstPipeline *pipeline;
  GstElement *src, *sink;
  GstBus *bus;
74
  gboolean done = FALSE;
75
76
77
78
79
80
81
82
83
84
85
86

  pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
  fail_unless (pipeline != NULL, "Could not create pipeline");

  src = gst_element_factory_make ("fakesrc", NULL);
  sink = gst_element_factory_make ("fakesink", NULL);

  gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
  gst_element_link (src, sink);

  bus = gst_pipeline_get_bus (pipeline);

87
  fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline),
88
89
          GST_STATE_PLAYING), GST_STATE_CHANGE_ASYNC);

90
  while (!done) {
91
    GstMessage *message;
92
    GstState old, new, pending;
93
94
95

    message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1);
    if (message) {
96
      gst_message_parse_state_changed (message, &old, &new, &pending);
97
98
99
100
101
      GST_DEBUG_OBJECT (message->src, "state change from %d to %d", old, new);
      if (message->src == GST_OBJECT (pipeline) && new == GST_STATE_PLAYING)
        done = TRUE;
      gst_message_unref (message);
    }
102
  }
103

104
105
106
  fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline),
          GST_STATE_NULL), GST_STATE_CHANGE_SUCCESS);

107
108
  /* here we don't get the state change messages, because of auto-flush in 
   * the bus */
109

110
  gst_object_unref (bus);
111
112
113
114
115
  gst_object_unref (pipeline);
}

GST_END_TEST;

116
GST_START_TEST (test_get_bus)
117
118
119
120
121
122
123
124
125
126
127
128
{
  GstPipeline *pipeline;
  GstBus *bus;

  pipeline = GST_PIPELINE (gst_pipeline_new (NULL));
  fail_unless (pipeline != NULL, "Could not create pipeline");
  ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);

  bus = gst_pipeline_get_bus (pipeline);
  ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline after get_bus", 1);
  ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);

129
130
131
  /* bindings don't like the floating flag to be set here */
  fail_if (g_object_is_floating (bus));

132
133
134
135
136
137
138
139
  gst_object_unref (pipeline);

  ASSERT_OBJECT_REFCOUNT (bus, "bus after unref pipeline", 1);
  gst_object_unref (bus);
}

GST_END_TEST;

140
static GMainLoop *loop = NULL;
141

142
static gboolean
143
144
145
146
147
148
149
150
151
message_received (GstBus * bus, GstMessage * message, gpointer data)
{
  GstElement *pipeline = GST_ELEMENT (data);
  GstMessageType type = message->type;

  GST_DEBUG ("message received");
  switch (type) {
    case GST_MESSAGE_STATE_CHANGED:
    {
152
      GstState old, new, pending;
153
154

      GST_DEBUG ("state change message received");
155
      gst_message_parse_state_changed (message, &old, &new, &pending);
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
      GST_DEBUG ("new state %d", new);
      if (message->src == GST_OBJECT (pipeline) && new == GST_STATE_PLAYING) {
        GST_DEBUG ("quitting main loop");
        g_main_loop_quit (loop);
      }
    }
      break;
    case GST_MESSAGE_ERROR:
    {
      g_print ("error\n");
    }
      break;
    default:
      break;
  }

  return TRUE;
}

GST_START_TEST (test_bus)
{
  GstElement *pipeline;
  GstElement *src, *sink;
  GstBus *bus;
  guint id;
  GstState current;
182
  GstStateChangeReturn ret;
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

  pipeline = gst_pipeline_new (NULL);
  fail_unless (pipeline != NULL, "Could not create pipeline");
  ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1);

  src = gst_element_factory_make ("fakesrc", NULL);
  fail_unless (src != NULL);
  sink = gst_element_factory_make ("fakesink", NULL);
  fail_unless (sink != NULL);

  gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
  fail_unless (gst_element_link (src, sink));

  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline after get_bus", 1);
  ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);

  id = gst_bus_add_watch (bus, message_received, pipeline);
  ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline after add_watch", 1);
202
  ASSERT_OBJECT_REFCOUNT (bus, "bus after add_watch", 3);
203

204
205
206
  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

207
208
209
210
211
212
213
214
215
216
217
218
219
  loop = g_main_loop_new (NULL, FALSE);
  GST_DEBUG ("going into main loop");
  g_main_loop_run (loop);
  GST_DEBUG ("left main loop");

  /* PLAYING now */

  ASSERT_OBJECT_REFCOUNT_BETWEEN (pipeline, "pipeline after gone to playing", 1,
      3);

  /* cleanup */
  GST_DEBUG ("cleanup");

220
221
222
223
  ret = gst_element_set_state (pipeline, GST_STATE_NULL);
  fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
  ret = gst_element_get_state (pipeline, &current, NULL, GST_CLOCK_TIME_NONE);
  fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
224
  fail_unless (current == GST_STATE_NULL, "state is not NULL but %d", current);
225

226
227
228
229
230
231
  /* We have to ensure that all background threads from thread pools are shut
   * down, or otherwise they might not have had a chance yet to drop
   * their last reference to the pipeline and then the assertion below fails
   */
  gst_task_cleanup_all ();

232
  ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline at start of cleanup", 1);
233
  ASSERT_OBJECT_REFCOUNT (bus, "bus at start of cleanup", 3);
234
235

  fail_unless (g_source_remove (id));
236
  ASSERT_OBJECT_REFCOUNT (bus, "bus after removing source", 2);
237
238
239
240

  GST_DEBUG ("unreffing pipeline");
  gst_object_unref (pipeline);

241
  ASSERT_OBJECT_REFCOUNT (bus, "bus after unref pipeline", 1);
242
243
244
245
246
  gst_object_unref (bus);
}

GST_END_TEST;

247
248
249
GST_START_TEST (test_base_time)
{
  GstElement *pipeline, *fakesrc, *fakesink;
250
251
  GstClockTime base, start;
  gint64 position;
252
253
  GstClock *clock;

254
255
256
  clock = gst_test_clock_new ();
  gst_test_clock_set_time (GST_TEST_CLOCK (clock), 100 * GST_SECOND);

257
258
259
260
  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
  fakesink = gst_element_factory_make ("fakesink", "fakesink");

261
262
  gst_pipeline_use_clock (GST_PIPELINE (pipeline), clock);

263
264
  fail_unless (pipeline && fakesrc && fakesink, "couldn't make elements");

265
266
267
268
  g_object_set (fakesrc, "is-live", (gboolean) TRUE, "do-timestamp", TRUE,
      "format", GST_FORMAT_TIME, "sizetype", 2, "sizemax", 4096, "datarate",
      4096 * 100, NULL);
  g_object_set (fakesink, "sync", TRUE, NULL);
269
270
271
272
273
274
275

  gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
  gst_element_link (fakesrc, fakesink);

  fail_unless (gst_element_set_state (pipeline, GST_STATE_PAUSED)
      == GST_STATE_CHANGE_NO_PREROLL, "expected no-preroll from live pipeline");

276
  fail_unless_equals_uint64 (gst_element_get_start_time (pipeline), 0);
277
278
279
280
281
282
283
284

  /* test the first: that base time is being distributed correctly, timestamps
     are correct relative to the running clock and base time */
  {
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    fail_unless (gst_element_get_state (pipeline, NULL, NULL,
            GST_CLOCK_TIME_NONE)
        == GST_STATE_CHANGE_SUCCESS, "failed state change");
285

286
287
288
289
290
291
292
293
    /* Wait for time for 1s to pass */
    for (;;) {
      gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
      if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
          101 * GST_SECOND)
        break;
      gst_test_clock_crank (GST_TEST_CLOCK (clock));
    }
294

295
296
    /* Now the base time should be exactly the clock time when we start and the
     * position should be at 1s because we waited 1s */
297
    base = gst_element_get_base_time (pipeline);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    fail_unless_equals_uint64 (base, 100 * GST_SECOND);

    fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
            &position));
    fail_unless_equals_uint64 (position, 1 * GST_SECOND);

    /* wait for another 1s of buffers to pass */
    for (;;) {
      gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
      if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
          102 * GST_SECOND)
        break;
      gst_test_clock_crank (GST_TEST_CLOCK (clock));
    }
312

313
    /* set start time by pausing */
314
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
315

316
317
318
    fail_unless (gst_element_get_state (pipeline, NULL, NULL,
            GST_CLOCK_TIME_NONE)
        == GST_STATE_CHANGE_NO_PREROLL, "failed state change");
319

320
321
322
323
324
325
326
327
    start = gst_element_get_start_time (pipeline);
    /* start time should be exactly 2s as that much time advanced, it's
     * the current running time */
    fail_unless_equals_uint64 (start, 2 * GST_SECOND);

    fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
            &position));
    fail_unless_equals_uint64 (position, 2 * GST_SECOND);
328
  }
329

330
331
332
  /* test the second: that the base time is redistributed when we go to PLAYING
     again */
  {
333
334
    /* Set time to 99s in the future */
    gst_test_clock_set_time (GST_TEST_CLOCK (clock), 200 * GST_SECOND);
335
336
337
338
339
340

    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    fail_unless (gst_element_get_state (pipeline, NULL, NULL,
            GST_CLOCK_TIME_NONE)
        == GST_STATE_CHANGE_SUCCESS, "failed state change");

341
342
343
344
345
346
347
348
    /* wait for 1s of buffers to pass */
    for (;;) {
      gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
      if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
          201 * GST_SECOND)
        break;
      gst_test_clock_crank (GST_TEST_CLOCK (clock));
    }
349

350
351
    /* now the base time should have advanced by 98s compared to what it was
     * before (we played 2s between previous and current play and 100s passed) */
352
    base = gst_element_get_base_time (pipeline);
353
354
355
356
357
358
359
360
361
362
    fail_unless_equals_uint64 (base, 198 * GST_SECOND);

    /* wait for 1s of buffers to pass */
    for (;;) {
      gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
      if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
          202 * GST_SECOND)
        break;
      gst_test_clock_crank (GST_TEST_CLOCK (clock));
    }
363

364
    /* set start time by pausing */
365
366
367
368
369
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
    fail_unless (gst_element_get_state (pipeline, NULL, NULL,
            GST_CLOCK_TIME_NONE)
        == GST_STATE_CHANGE_NO_PREROLL, "failed state change");

370
371
372
373
374
375
376
377
    /* start time should now be 4s as that's the amount of time that has
     * passed since we started until we paused above */
    start = gst_element_get_start_time (pipeline);
    fail_unless_equals_uint64 (start, 4 * GST_SECOND);

    fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
            &position));
    fail_unless_equals_uint64 (position, 4 * GST_SECOND);
378
  }
379

380
381
382
  /* test the third: that if I set CLOCK_TIME_NONE as the stream time, that the
     base time is not changed */
  {
383
    GstClockTime oldbase;
384
385

    /* bling */
386
    oldbase = gst_element_get_base_time (pipeline);
387
    gst_element_set_start_time (pipeline, GST_CLOCK_TIME_NONE);
388
389
390
391
392
393

    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    fail_unless (gst_element_get_state (pipeline, NULL, NULL,
            GST_CLOCK_TIME_NONE)
        == GST_STATE_CHANGE_SUCCESS, "failed state change");

394
395
396
397
398
399
400
401
    /* wait for 1s of buffers to pass */
    for (;;) {
      gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
      if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
          203 * GST_SECOND)
        break;
      gst_test_clock_crank (GST_TEST_CLOCK (clock));
    }
402

403
    /* now the base time should be the same as it was */
404
405
406

    base = gst_element_get_base_time (pipeline);

407
408
409
410
411
412
413
414
    /* wait for 1s of buffers to pass */
    for (;;) {
      gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
      if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
          204 * GST_SECOND)
        break;
      gst_test_clock_crank (GST_TEST_CLOCK (clock));
    }
415

416
417
    /* set start time by pausing */
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
418
419
420
421
422

    fail_unless (gst_element_get_state (pipeline, NULL, NULL,
            GST_CLOCK_TIME_NONE)
        == GST_STATE_CHANGE_NO_PREROLL, "failed state change");

423
    fail_unless (gst_element_get_start_time (pipeline)
424
425
426
427
428
        == GST_CLOCK_TIME_NONE, "stream time was reset");

    fail_unless (base == oldbase, "base time was reset");
  }

429
430
  gst_element_set_state (pipeline, GST_STATE_NULL);

431
  gst_object_unref (pipeline);
432
  gst_object_unref (clock);
433
434
435
436
}

GST_END_TEST;

437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
static gpointer
pipeline_thread (gpointer data)
{
  GstElement *pipeline, *src, *sink;

  src = gst_element_factory_make ("fakesrc", NULL);
  g_object_set (src, "num-buffers", 20, NULL);
  sink = gst_element_factory_make ("fakesink", NULL);
  g_object_set (sink, "sync", TRUE, NULL);
  pipeline = gst_pipeline_new (NULL);
  gst_bin_add (GST_BIN (pipeline), src);
  gst_bin_add (GST_BIN (pipeline), sink);
  gst_element_link (src, sink);
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
  g_usleep (G_USEC_PER_SEC / 10);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return NULL;
}

GST_START_TEST (test_concurrent_create)
{
  GThread *threads[30];
  int i;

  for (i = 0; i < G_N_ELEMENTS (threads); ++i) {
463
    threads[i] = g_thread_try_new ("gst-check", pipeline_thread, NULL, NULL);
464
465
466
467
468
469
470
471
472
  }
  for (i = 0; i < G_N_ELEMENTS (threads); ++i) {
    if (threads[i])
      g_thread_join (threads[i]);
  }
}

GST_END_TEST;

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
GST_START_TEST (test_pipeline_in_pipeline)
{
  GstElement *pipeline, *bin, *fakesrc, *fakesink;
  GstMessage *msg;

  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  bin = gst_element_factory_make ("pipeline", "pipeline-as-bin");
  fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
  fakesink = gst_element_factory_make ("fakesink", "fakesink");

  fail_unless (pipeline && bin && fakesrc && fakesink);

  g_object_set (fakesrc, "num-buffers", 100, NULL);

  gst_bin_add (GST_BIN (pipeline), bin);
  gst_bin_add_many (GST_BIN (bin), fakesrc, fakesink, NULL);
  gst_element_link (fakesrc, fakesink);

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);

  msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline), -1,
      GST_MESSAGE_EOS);
  gst_message_unref (msg);

  gst_element_set_state (pipeline, GST_STATE_NULL);

  gst_object_unref (pipeline);
}

GST_END_TEST;

505
506
507
508
509
510
511
GST_START_TEST (test_pipeline_reset_start_time)
{
  GstElement *pipeline, *fakesrc, *fakesink;
  GstState state;
  GstClock *clock;
  gint64 position;

512
  clock = gst_test_clock_new ();
513
514
515
516
  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
  fakesink = gst_element_factory_make ("fakesink", "fakesink");

517
518
  gst_pipeline_use_clock (GST_PIPELINE (pipeline), clock);

519
520
521
522
523
  /* no more than 100 buffers per second */
  g_object_set (fakesrc, "do-timestamp", TRUE, "format", GST_FORMAT_TIME,
      "sizetype", 2, "sizemax", 4096, "datarate", 4096 * 100, NULL);

  g_object_set (fakesink, "sync", TRUE, NULL);
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540

  fail_unless (pipeline && fakesrc && fakesink);

  gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
  gst_element_link (fakesrc, fakesink);

  fail_unless (gst_element_get_start_time (fakesink) == 0);

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL, -1),
      GST_STATE_CHANGE_SUCCESS);
  fail_unless_equals_int (state, GST_STATE_PLAYING);

  /* We just started and never paused, start time must be 0 */
  fail_unless (gst_element_get_start_time (fakesink) == 0);

541
542
543
544
545
546
547
548
  /* Wait for time to reach 50 msecs */
  for (;;) {
    gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
    if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
        50 * GST_MSECOND)
      break;
    gst_test_clock_crank (GST_TEST_CLOCK (clock));
  }
549

550
  /* We waited 50ms, so the position should be now == 50ms */
551
552
  fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
          &position));
553
  fail_unless_equals_uint64 (position, 50 * GST_MSECOND);
554
555
556
557
558
559
560
561
562

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PAUSED),
      GST_STATE_CHANGE_ASYNC);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL, -1),
      GST_STATE_CHANGE_SUCCESS);
  fail_unless_equals_int (state, GST_STATE_PAUSED);

  /* And now after pausing the start time should be bigger than the last
   * position */
563
564
  fail_unless_equals_uint64 (gst_element_get_start_time (fakesink),
      50 * GST_MSECOND);
565
566
  fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
          &position));
567
  fail_unless_equals_uint64 (position, 50 * GST_MSECOND);
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_READY),
      GST_STATE_CHANGE_SUCCESS);

  /* When going back to ready the start time should be reset everywhere */
  fail_unless (gst_element_get_start_time (fakesink) == 0);

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PAUSED),
      GST_STATE_CHANGE_ASYNC);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL, -1),
      GST_STATE_CHANGE_SUCCESS);
  fail_unless_equals_int (state, GST_STATE_PAUSED);

  /* And the start time should still be set to 0 when we go to paused for the
   * first time. Same goes for the position */
  fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
          &position));
585
  fail_unless_equals_uint64 (position, 0 * GST_MSECOND);
586
587
588
589
590
591

  fail_unless (gst_element_get_start_time (fakesink) == 0);

  gst_element_set_state (pipeline, GST_STATE_NULL);

  gst_object_unref (pipeline);
592
  gst_object_unref (clock);
593
594
595
596
}

GST_END_TEST;

597
598
599
600
601
602
603
604
605
606
607
608
GST_START_TEST (test_pipeline_processing_deadline)
{
  GstElement *pipeline, *fakesrc, *queue, *fakesink;
  GstState state;
  GstClock *clock;
  gint64 position;
  GstQuery *q;
  gboolean live;
  GstClockTime min, max;
  GstBus *bus;
  GstMessage *msg;

609
  clock = gst_test_clock_new ();
610
611
612
613
614
  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
  queue = gst_element_factory_make ("queue", "queue");
  fakesink = gst_element_factory_make ("fakesink", "fakesink");

615
616
  gst_pipeline_use_clock (GST_PIPELINE (pipeline), clock);

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
  /* no more than 100 buffers per second */
  g_object_set (fakesrc, "do-timestamp", TRUE, "format", GST_FORMAT_TIME,
      "sizetype", 2, "sizemax", 4096, "datarate", 4096 * 100, "is-live", TRUE,
      NULL);

  g_object_set (fakesink, "sync", TRUE, NULL);

  fail_unless (pipeline && fakesrc && queue && fakesink);

  gst_bin_add_many (GST_BIN (pipeline), fakesrc, queue, fakesink, NULL);
  gst_element_link_many (fakesrc, queue, fakesink, NULL);

  gst_element_set_state (pipeline, GST_STATE_PLAYING);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL, -1),
      GST_STATE_CHANGE_SUCCESS);
  fail_unless_equals_int (state, GST_STATE_PLAYING);

  q = gst_query_new_latency ();
  fail_unless (gst_element_query (pipeline, q));
  gst_query_parse_latency (q, &live, &min, &max);
  fail_unless (live == TRUE);
  fail_unless (min == 20 * GST_MSECOND);
  fail_unless (max >= min);
  gst_query_unref (q);

642
643
644
645
646
647
648
649
  /* Wait for time to reach 50 msecs */
  for (;;) {
    gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (clock), NULL);
    if (gst_test_clock_get_next_entry_time (GST_TEST_CLOCK (clock)) >
        50 * GST_MSECOND)
      break;
    gst_test_clock_crank (GST_TEST_CLOCK (clock));
  }
650

651
  /* We waited 50ms, but the position should be 50ms - 20ms latency == 30ms */
652
653
  fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
          &position));
654
  fail_unless_equals_uint64 (position, 30 * GST_MSECOND);
655
656
657
658
659
660
661

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PAUSED),
      GST_STATE_CHANGE_NO_PREROLL);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL, -1),
      GST_STATE_CHANGE_NO_PREROLL);
  fail_unless_equals_int (state, GST_STATE_PAUSED);

662
663
664
  /* And now after pausing the start time should be exactly 50ms */
  fail_unless_equals_uint64 (gst_element_get_start_time (fakesink),
      50 * GST_MSECOND);
665
666
  fail_unless (gst_element_query_position (fakesink, GST_FORMAT_TIME,
          &position));
667
668
  /* but position should still be exactly 50ms - 20ms latency == 30ms */
  fail_unless_equals_uint64 (position, 30 * GST_MSECOND);
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_READY),
      GST_STATE_CHANGE_SUCCESS);

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PAUSED),
      GST_STATE_CHANGE_NO_PREROLL);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL, -1),
      GST_STATE_CHANGE_NO_PREROLL);
  fail_unless_equals_int (state, GST_STATE_PAUSED);

  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  msg = gst_bus_pop_filtered (bus, GST_MESSAGE_WARNING);
  fail_unless (msg == NULL);
  gst_object_unref (bus);

  gst_element_set_state (pipeline, GST_STATE_NULL);

  gst_object_unref (pipeline);
687
  gst_object_unref (clock);
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
}

GST_END_TEST;


GST_START_TEST (test_pipeline_processing_deadline_no_queue)
{
  GstElement *pipeline, *fakesrc, *fakesink;
  GstState state;
  GstBus *bus;
  GstMessage *msg;
  GError *gerror = NULL;

  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
  fakesink = gst_element_factory_make ("fakesink", "fakesink");

  /* no more than 100 buffers per second */
  g_object_set (fakesrc, "do-timestamp", TRUE, "format", GST_FORMAT_TIME,
      "sizetype", 2, "sizemax", 4096, "datarate", 4096 * 100, "is-live", TRUE,
      NULL);

  g_object_set (fakesink, "sync", TRUE, "processing-deadline", 60 * GST_MSECOND,
      NULL);

  fail_unless (pipeline && fakesrc && fakesink);

  gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
  gst_element_link (fakesrc, fakesink);

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL, -1),
      GST_STATE_CHANGE_SUCCESS);

  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
      GST_MESSAGE_WARNING);
  fail_unless (msg != NULL);
  gst_message_parse_warning (msg, &gerror, NULL);
  fail_unless (g_error_matches (gerror, GST_CORE_ERROR, GST_CORE_ERROR_CLOCK));
  gst_message_unref (msg);
  gst_object_unref (bus);
731
  g_clear_error (&gerror);
732
733
734
735
736
737
738
739
740

  gst_element_set_state (pipeline, GST_STATE_NULL);

  gst_object_unref (pipeline);
}

GST_END_TEST;


741
static Suite *
742
743
744
745
746
gst_pipeline_suite (void)
{
  Suite *s = suite_create ("GstPipeline");
  TCase *tc_chain = tcase_create ("pipeline tests");

747
748
  tcase_set_timeout (tc_chain, 0);

749
750
751
752
  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_async_state_change_empty);
  tcase_add_test (tc_chain, test_async_state_change_fake_ready);
  tcase_add_test (tc_chain, test_async_state_change_fake);
753
  tcase_add_test (tc_chain, test_get_bus);
754
  tcase_add_test (tc_chain, test_bus);
755
  tcase_add_test (tc_chain, test_base_time);
756
  tcase_add_test (tc_chain, test_concurrent_create);
757
  tcase_add_test (tc_chain, test_pipeline_in_pipeline);
758
  tcase_add_test (tc_chain, test_pipeline_reset_start_time);
759
760
  tcase_add_test (tc_chain, test_pipeline_processing_deadline);
  tcase_add_test (tc_chain, test_pipeline_processing_deadline_no_queue);
761
762
763
764

  return s;
}

765
GST_CHECK_MAIN (gst_pipeline);