fakesink.c 27.9 KB
Newer Older
1 2 3 4 5
/* GStreamer
 *
 * unit test for fakesink
 *
 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6
 *               <2007> Wim Taymans <wim@fluendo.com>
7
 *               <2009> Tim-Philipp Müller <tim centricular net>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 */

#include <unistd.h>

27
#include <gst/base/gstpushsrc.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#include <gst/check/gstcheck.h>

typedef struct
{
  GstPad *pad;
  GstBuffer *buffer;
  GThread *thread;
  GstFlowReturn ret;
} ChainData;

static gpointer
chain_async_buffer (gpointer data)
{
  ChainData *chain_data = (ChainData *) data;

  chain_data->ret = gst_pad_chain (chain_data->pad, chain_data->buffer);

  return chain_data;
}

static ChainData *
chain_async (GstPad * pad, GstBuffer * buffer)
{
  GThread *thread;
  ChainData *chain_data;
  GError *error = NULL;

  chain_data = g_new (ChainData, 1);
  chain_data->pad = pad;
  chain_data->buffer = buffer;
  chain_data->ret = GST_FLOW_ERROR;

  thread = g_thread_create (chain_async_buffer, chain_data, TRUE, &error);
  if (error != NULL) {
    g_warning ("could not create thread reason: %s", error->message);
    g_free (chain_data);
    return NULL;
  }
  chain_data->thread = thread;

  return chain_data;
}

static GstFlowReturn
chain_async_return (ChainData * data)
{
  GstFlowReturn ret;

  g_thread_join (data->thread);
  ret = data->ret;
  g_free (data);

  return ret;
}

GST_START_TEST (test_clipping)
{
  GstElement *sink;
  GstPad *sinkpad;
  GstStateChangeReturn ret;

  /* create sink */
  sink = gst_element_factory_make ("fakesink", "sink");
  fail_if (sink == NULL);

Wim Taymans's avatar
Wim Taymans committed
93
  sinkpad = gst_element_get_static_pad (sink, "sink");
94 95 96 97 98 99 100 101
  fail_if (sinkpad == NULL);

  /* make element ready to accept data */
  ret = gst_element_set_state (sink, GST_STATE_PAUSED);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* send segment */
  {
Wim Taymans's avatar
Wim Taymans committed
102 103
    GstEvent *event;
    GstSegment segment;
104 105 106
    gboolean eret;

    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
107 108 109 110 111 112
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 1 * GST_SECOND;
    segment.stop = 5 * GST_SECOND;
    segment.time = 1 * GST_SECOND;

    event = gst_event_new_segment (&segment);
113

Wim Taymans's avatar
Wim Taymans committed
114
    eret = gst_pad_send_event (sinkpad, event);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 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
    fail_if (eret == FALSE);
  }

  /* new segment should not have finished preroll */
  ret = gst_element_get_state (sink, NULL, NULL, 0);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* send buffer that should be dropped */
  {
    GstBuffer *buffer;
    GstFlowReturn fret;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 0;
    GST_BUFFER_DURATION (buffer) = 1 * GST_MSECOND;

    GST_DEBUG ("sending buffer to be dropped");
    fret = gst_pad_chain (sinkpad, buffer);
    fail_if (fret != GST_FLOW_OK);
  }
  /* dropped buffer should not have finished preroll */
  ret = gst_element_get_state (sink, NULL, NULL, 0);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* send buffer that should be dropped */
  {
    GstBuffer *buffer;
    GstFlowReturn fret;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 5 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_MSECOND;

    GST_DEBUG ("sending buffer to be dropped");
    fret = gst_pad_chain (sinkpad, buffer);
    fail_if (fret != GST_FLOW_OK);
  }
  /* dropped buffer should not have finished preroll */
  ret = gst_element_get_state (sink, NULL, NULL, 0);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* send buffer that should block and finish preroll */
  {
    GstBuffer *buffer;
    GstFlowReturn fret;
    ChainData *data;
    GstState current, pending;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_MSECOND;

    GST_DEBUG ("sending buffer to finish preroll");
    data = chain_async (sinkpad, buffer);
    fail_if (data == NULL);

    /* state should now eventually change to PAUSED */
    ret = gst_element_get_state (sink, &current, &pending, GST_CLOCK_TIME_NONE);
    fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
    fail_unless (current == GST_STATE_PAUSED);
    fail_unless (pending == GST_STATE_VOID_PENDING);

177
    /* playing should render the buffer */
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    ret = gst_element_set_state (sink, GST_STATE_PLAYING);
    fail_unless (ret == GST_STATE_CHANGE_SUCCESS);

    /* and we should get a success return value */
    fret = chain_async_return (data);
    fail_if (fret != GST_FLOW_OK);
  }

  /* send some buffer that will be dropped or clipped, this can 
   * only be observed in the debug log. */
  {
    GstBuffer *buffer;
    GstFlowReturn fret;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 6 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_MSECOND;

    /* should be dropped */
    GST_DEBUG ("sending buffer to drop");
    fret = gst_pad_chain (sinkpad, buffer);
    fail_if (fret != GST_FLOW_OK);

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 0 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 2 * GST_SECOND;

    /* should be clipped */
    GST_DEBUG ("sending buffer to clip");
    fret = gst_pad_chain (sinkpad, buffer);
    fail_if (fret != GST_FLOW_OK);

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 4 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 2 * GST_SECOND;

    /* should be clipped */
    GST_DEBUG ("sending buffer to clip");
    fret = gst_pad_chain (sinkpad, buffer);
    fail_if (fret != GST_FLOW_OK);
  }
219 220 221 222 223

  gst_element_set_state (sink, GST_STATE_NULL);
  gst_element_get_state (sink, NULL, NULL, GST_CLOCK_TIME_NONE);
  gst_object_unref (sinkpad);
  gst_object_unref (sink);
224 225 226 227
}

GST_END_TEST;

228 229 230 231 232 233 234 235 236
static gint num_preroll = 0;

static void
preroll_count (GstElement * sink)
{
  num_preroll++;
  GST_DEBUG ("got preroll handoff %d", num_preroll);
}

237 238 239 240 241 242 243 244 245 246 247 248 249
GST_START_TEST (test_preroll_sync)
{
  GstElement *pipeline, *sink;
  GstPad *sinkpad;
  GstStateChangeReturn ret;

  /* create sink */
  pipeline = gst_pipeline_new ("pipeline");
  fail_if (pipeline == NULL);

  sink = gst_element_factory_make ("fakesink", "sink");
  fail_if (sink == NULL);
  g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
250 251 252 253
  g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, NULL);
  g_signal_connect (sink, "preroll-handoff", G_CALLBACK (preroll_count), NULL);

  fail_unless (num_preroll == 0);
254 255 256

  gst_bin_add (GST_BIN (pipeline), sink);

Wim Taymans's avatar
Wim Taymans committed
257
  sinkpad = gst_element_get_static_pad (sink, "sink");
258 259 260 261 262 263 264 265
  fail_if (sinkpad == NULL);

  /* make pipeline and element ready to accept data */
  ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* send segment */
  {
Wim Taymans's avatar
Wim Taymans committed
266 267
    GstEvent *event;
    GstSegment segment;
268 269 270
    gboolean eret;

    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
271 272 273 274
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 0 * GST_SECOND;
    segment.stop = 102 * GST_SECOND;
    segment.time = 0 * GST_SECOND;
275

Wim Taymans's avatar
Wim Taymans committed
276 277
    event = gst_event_new_segment (&segment);
    eret = gst_pad_send_event (sinkpad, event);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    fail_if (eret == FALSE);
  }

  /* send buffer that should block and finish preroll */
  {
    GstBuffer *buffer;
    GstFlowReturn fret;
    ChainData *data;
    GstState current, pending;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

    GST_DEBUG ("sending buffer to finish preroll");
    data = chain_async (sinkpad, buffer);
    fail_if (data == NULL);

    /* state should now eventually change to PAUSED */
    ret =
        gst_element_get_state (pipeline, &current, &pending,
        GST_CLOCK_TIME_NONE);
    fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
    fail_unless (current == GST_STATE_PAUSED);
    fail_unless (pending == GST_STATE_VOID_PENDING);

304 305
    fail_unless (num_preroll == 1);

306 307 308 309 310 311 312
    /* playing should render the buffer */
    ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
    fail_unless (ret == GST_STATE_CHANGE_SUCCESS);

    /* and we should get a success return value */
    fret = chain_async_return (data);
    fail_if (fret != GST_FLOW_OK);
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

    /* now we are playing no new preroll was done */
    fail_unless (num_preroll == 1);

    buffer = gst_buffer_new ();
    /* far in the future to make sure we block */
    GST_BUFFER_TIMESTAMP (buffer) = 100 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 100 * GST_SECOND;
    data = chain_async (sinkpad, buffer);
    fail_if (data == NULL);

    g_usleep (1000000);

    /* pause again. Since the buffer has a humongous timestamp we likely
     * interrupt the clock_wait and we should preroll on this buffer again */
    ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
    fail_unless (ret == GST_STATE_CHANGE_ASYNC);

    ret =
        gst_element_get_state (pipeline, &current, &pending,
        GST_CLOCK_TIME_NONE);
    fail_unless (ret == GST_STATE_CHANGE_SUCCESS);
    fail_unless (current == GST_STATE_PAUSED);
    fail_unless (pending == GST_STATE_VOID_PENDING);

    fail_unless (num_preroll == 2);

    /* shutdown */
    ret = gst_element_set_state (pipeline, GST_STATE_READY);
    fail_unless (ret == GST_STATE_CHANGE_SUCCESS);

    /* should be wrong state now */
    fret = chain_async_return (data);
346
    fail_if (fret != GST_FLOW_FLUSHING);
347 348 349 350 351 352 353 354 355
  }
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  gst_object_unref (sinkpad);
  gst_object_unref (pipeline);
}

GST_END_TEST;

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/* after EOS, we refuse everything */
GST_START_TEST (test_eos)
{
  GstElement *pipeline, *sink;
  GstPad *sinkpad;
  GstStateChangeReturn ret;
  GstMessage *message;
  GstBus *bus;

  /* create sink */
  pipeline = gst_pipeline_new ("pipeline");
  fail_if (pipeline == NULL);

  bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (pipeline));
  fail_if (bus == NULL);

  sink = gst_element_factory_make ("fakesink", "sink");
  fail_if (sink == NULL);
  g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);

  gst_bin_add (GST_BIN (pipeline), sink);

Wim Taymans's avatar
Wim Taymans committed
378
  sinkpad = gst_element_get_static_pad (sink, "sink");
379 380 381 382 383 384 385 386 387 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 413
  fail_if (sinkpad == NULL);

  /* make pipeline and element ready to accept data */
  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* send EOS, this should work fine */
  {
    GstEvent *eos;
    gboolean eret;

    GST_DEBUG ("sending EOS");
    eos = gst_event_new_eos ();

    eret = gst_pad_send_event (sinkpad, eos);
    fail_if (eret == FALSE);
  }

  /* wait for preroll */
  gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);

  /* EOS should be on the bus at some point */
  while (TRUE) {
    GstMessageType type;

    /* blocking wait for messages */
    message = gst_bus_timed_pop (bus, GST_CLOCK_TIME_NONE);
    type = GST_MESSAGE_TYPE (message);
    gst_message_unref (message);

    GST_DEBUG ("got message %s", gst_message_type_get_name (type));

    if (type == GST_MESSAGE_EOS)
      break;
  }
414
  gst_object_unref (bus);
415 416 417 418 419 420 421 422 423 424 425 426 427

  /* send another EOS, this should fail */
  {
    GstEvent *eos;
    gboolean eret;

    GST_DEBUG ("sending second EOS");
    eos = gst_event_new_eos ();

    eret = gst_pad_send_event (sinkpad, eos);
    fail_if (eret == TRUE);
  }

428
  /* send segment, this should fail */
429
  {
Wim Taymans's avatar
Wim Taymans committed
430 431
    GstEvent *event;
    GstSegment segment;
432 433 434
    gboolean eret;

    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
435 436 437 438 439
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 0 * GST_SECOND;
    segment.stop = 2 * GST_SECOND;
    segment.time = 0 * GST_SECOND;
    event = gst_event_new_segment (&segment);
440

Wim Taymans's avatar
Wim Taymans committed
441
    eret = gst_pad_send_event (sinkpad, event);
442
    fail_if (eret == TRUE);
443 444 445 446 447 448 449 450 451 452 453 454 455
  }

  /* send buffer that should fail after EOS */
  {
    GstBuffer *buffer;
    GstFlowReturn fret;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

    GST_DEBUG ("sending buffer");

456
    /* buffer after EOS is not EOS */
457
    fret = gst_pad_chain (sinkpad, buffer);
458
    fail_unless (fret == GST_FLOW_EOS);
459 460 461 462 463 464 465 466 467 468 469 470 471
  }

  /* flush, EOS state is flushed again. */
  {
    GstEvent *event;
    gboolean eret;

    GST_DEBUG ("sending FLUSH_START");
    event = gst_event_new_flush_start ();
    eret = gst_pad_send_event (sinkpad, event);
    fail_unless (eret == TRUE);

    GST_DEBUG ("sending FLUSH_STOP");
472
    event = gst_event_new_flush_stop (TRUE);
473 474 475 476 477 478
    eret = gst_pad_send_event (sinkpad, event);
    fail_unless (eret == TRUE);
  }

  /* send segment, this should now work again */
  {
Wim Taymans's avatar
Wim Taymans committed
479 480
    GstEvent *event;
    GstSegment segment;
481 482 483
    gboolean eret;

    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
484 485 486 487 488
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 0 * GST_SECOND;
    segment.stop = 2 * GST_SECOND;
    segment.time = 0 * GST_SECOND;
    event = gst_event_new_segment (&segment);
489

Wim Taymans's avatar
Wim Taymans committed
490
    eret = gst_pad_send_event (sinkpad, event);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    fail_unless (eret == TRUE);
  }

  /* send buffer that should work and block */
  {
    GstBuffer *buffer;
    GstFlowReturn fret;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

    GST_DEBUG ("sending buffer");

    fret = gst_pad_chain (sinkpad, buffer);
    fail_unless (fret == GST_FLOW_OK);
  }

  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  gst_object_unref (sinkpad);
  gst_object_unref (pipeline);
}

GST_END_TEST;

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
/* test EOS triggered by the element */
GST_START_TEST (test_eos2)
{
  GstElement *pipeline, *sink;
  GstPad *sinkpad;
  GstStateChangeReturn ret;

  /* create sink */
  pipeline = gst_pipeline_new ("pipeline");
  fail_if (pipeline == NULL);

  sink = gst_element_factory_make ("fakesink", "sink");
  fail_if (sink == NULL);
  g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
  g_object_set (G_OBJECT (sink), "num-buffers", 1, NULL);

  gst_bin_add (GST_BIN (pipeline), sink);

Wim Taymans's avatar
Wim Taymans committed
535
  sinkpad = gst_element_get_static_pad (sink, "sink");
536 537 538 539 540 541 542 543
  fail_if (sinkpad == NULL);

  /* make pipeline and element ready to accept data */
  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* send segment, this should work */
  {
Wim Taymans's avatar
Wim Taymans committed
544 545
    GstEvent *event;
    GstSegment segment;
546 547 548
    gboolean eret;

    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
549 550 551 552 553
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 0 * GST_SECOND;
    segment.stop = 2 * GST_SECOND;
    segment.time = 0 * GST_SECOND;
    event = gst_event_new_segment (&segment);
554

Wim Taymans's avatar
Wim Taymans committed
555
    eret = gst_pad_send_event (sinkpad, event);
556 557 558
    fail_if (eret == FALSE);
  }

559
  /* send buffer that should return EOS */
560 561 562 563 564 565 566 567 568 569
  {
    GstBuffer *buffer;
    GstFlowReturn fret;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

    GST_DEBUG ("sending buffer");

570
    /* this buffer will generate EOS */
571
    fret = gst_pad_chain (sinkpad, buffer);
572
    fail_unless (fret == GST_FLOW_EOS);
573 574
  }

575
  /* send buffer that should return EOS */
576 577 578 579 580 581 582 583 584 585 586
  {
    GstBuffer *buffer;
    GstFlowReturn fret;

    buffer = gst_buffer_new ();
    GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
    GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

    GST_DEBUG ("sending buffer");

    fret = gst_pad_chain (sinkpad, buffer);
587
    fail_unless (fret == GST_FLOW_EOS);
588 589 590 591 592 593 594 595 596 597
  }

  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  gst_object_unref (sinkpad);
  gst_object_unref (pipeline);
}

GST_END_TEST;

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
/* test position reporting before, during and after flush 
 * in PAUSED and PLAYING */
GST_START_TEST (test_position)
{
  GstElement *pipeline, *sink;
  GstPad *sinkpad;
  GstStateChangeReturn ret;
  gboolean qret;
  gint64 qcur;
  GstBuffer *buffer;
  GstFlowReturn fret;
  ChainData *data;
  GstEvent *event;
  gboolean eret;
  gint i;

  /* create sink */
  pipeline = gst_pipeline_new ("pipeline");
  fail_if (pipeline == NULL);

  sink = gst_element_factory_make ("fakesink", "sink");
  fail_if (sink == NULL);
  g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
  g_object_set (G_OBJECT (sink), "num-buffers", 2, NULL);

  gst_bin_add (GST_BIN (pipeline), sink);

  sinkpad = gst_element_get_static_pad (sink, "sink");
  fail_if (sinkpad == NULL);

  /* do position query, this should fail, we have nothing received yet */
629
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
630 631 632 633 634 635
  fail_unless (qret == FALSE);

  ret = gst_element_set_state (pipeline, GST_STATE_READY);
  fail_unless (ret == GST_STATE_CHANGE_SUCCESS);

  /* do position query, this should fail, we have nothing received yet */
636
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
637 638 639 640 641 642 643
  fail_unless (qret == FALSE);

  /* make pipeline and element ready to accept data */
  ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* do position query, this should fail, we have nothing received yet */
644
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
645 646 647 648
  fail_unless (qret == FALSE);

  /* send segment, this should work */
  {
Wim Taymans's avatar
Wim Taymans committed
649 650
    GstSegment segment;

651
    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
652 653 654 655 656
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 1 * GST_SECOND;
    segment.stop = 3 * GST_SECOND;
    segment.time = 1 * GST_SECOND;
    event = gst_event_new_segment (&segment);
657 658 659 660 661 662 663

    eret = gst_pad_send_event (sinkpad, event);
    fail_if (eret == FALSE);
  }

  /* FIXME, do position query, this should succeed with the time value from the
   * segment. */
664
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  fail_unless (qret == TRUE);
  fail_unless (qcur == 1 * GST_SECOND);

  /* send buffer that we will flush out */
  buffer = gst_buffer_new ();
  GST_BUFFER_TIMESTAMP (buffer) = 2 * GST_SECOND;
  GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

  GST_DEBUG ("sending buffer");

  /* this buffer causes the sink to preroll */
  data = chain_async (sinkpad, buffer);
  fail_if (data == NULL);

  /* wait for preroll */
  ret = gst_element_get_state (pipeline, NULL, NULL, -1);

  /* do position query, this should succeed with the time value from the
   * segment. */
684
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
685 686 687 688 689 690 691 692 693 694 695 696 697 698
  fail_unless (qret == TRUE);
  fail_unless (qcur == 1 * GST_SECOND);

  /* start flushing, no timing is affected yet */
  {
    GST_DEBUG ("sending flush_start");
    event = gst_event_new_flush_start ();

    eret = gst_pad_send_event (sinkpad, event);
    fail_if (eret == FALSE);
  }

  /* preroll buffer is flushed out */
  fret = chain_async_return (data);
699
  fail_unless (fret == GST_FLOW_FLUSHING);
700 701 702

  /* do position query, this should succeed with the time value from the
   * segment before the flush. */
703
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
704 705 706 707 708 709
  fail_unless (qret == TRUE);
  fail_unless (qcur == 1 * GST_SECOND);

  /* stop flushing, timing is affected now */
  {
    GST_DEBUG ("sending flush_stop");
710
    event = gst_event_new_flush_stop (TRUE);
711 712 713 714 715 716

    eret = gst_pad_send_event (sinkpad, event);
    fail_if (eret == FALSE);
  }

  /* do position query, this should fail, the segment is flushed */
717
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
718 719 720 721
  fail_unless (qret == FALSE);

  /* send segment, this should work */
  {
Wim Taymans's avatar
Wim Taymans committed
722 723
    GstSegment segment;

724
    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
725 726 727 728 729
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 2 * GST_SECOND;
    segment.stop = 4 * GST_SECOND;
    segment.time = 1 * GST_SECOND;
    event = gst_event_new_segment (&segment);
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

    eret = gst_pad_send_event (sinkpad, event);
    fail_if (eret == FALSE);
  }

  /* send buffer that should return OK */
  buffer = gst_buffer_new ();
  GST_BUFFER_TIMESTAMP (buffer) = 3 * GST_SECOND;
  GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

  GST_DEBUG ("sending buffer");

  /* this buffer causes the sink to preroll */
  data = chain_async (sinkpad, buffer);
  fail_if (data == NULL);

  /* wait for preroll */
  ret = gst_element_get_state (pipeline, NULL, NULL, -1);

  /* do position query, this should succeed with the time value from the
   * segment. */
751
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
752 753 754 755 756 757 758 759
  fail_unless (qret == TRUE);
  fail_unless (qcur == 1 * GST_SECOND);

  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  fail_unless (ret == GST_STATE_CHANGE_SUCCESS);

  /* position now is increasing but never exceeds the boundaries of the segment */
  for (i = 0; i < 5; i++) {
760
    qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
761 762 763 764 765 766 767 768 769 770 771 772
    GST_DEBUG ("position %" GST_TIME_FORMAT, GST_TIME_ARGS (qcur));
    fail_unless (qret == TRUE);
    fail_unless (qcur >= 1 * GST_SECOND && qcur <= 3 * GST_SECOND);
    g_usleep (1000 * 250);
  }

  /* preroll buffer is rendered, we expect one more buffer after this one */
  fret = chain_async_return (data);
  fail_unless (fret == GST_FLOW_OK);

  /* after rendering the position must be bigger then the stream_time of the
   * buffer */
773
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
774 775 776 777 778 779 780 781 782 783 784 785 786
  fail_unless (qret == TRUE);
  fail_unless (qcur >= 2 * GST_SECOND && qcur <= 3 * GST_SECOND);

  /* start flushing in PLAYING */
  {
    GST_DEBUG ("sending flush_start");
    event = gst_event_new_flush_start ();

    eret = gst_pad_send_event (sinkpad, event);
    fail_if (eret == FALSE);
  }

  /* this should now just report the stream time of the last buffer */
787
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
788 789 790 791 792
  fail_unless (qret == TRUE);
  fail_unless (qcur == 2 * GST_SECOND);

  {
    GST_DEBUG ("sending flush_stop");
793
    event = gst_event_new_flush_stop (TRUE);
794 795 796 797 798 799

    eret = gst_pad_send_event (sinkpad, event);
    fail_if (eret == FALSE);
  }

  /* do position query, this should fail, the segment is flushed */
800
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
801 802 803 804
  fail_unless (qret == FALSE);

  /* send segment, this should work */
  {
Wim Taymans's avatar
Wim Taymans committed
805 806
    GstSegment segment;

807
    GST_DEBUG ("sending segment");
Wim Taymans's avatar
Wim Taymans committed
808 809 810 811 812
    gst_segment_init (&segment, GST_FORMAT_TIME);
    segment.start = 2 * GST_SECOND;
    segment.stop = 4 * GST_SECOND;
    segment.time = 1 * GST_SECOND;
    event = gst_event_new_segment (&segment);
813 814 815 816 817

    eret = gst_pad_send_event (sinkpad, event);
    fail_if (eret == FALSE);
  }

818
  /* send buffer that should return EOS */
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
  buffer = gst_buffer_new ();
  GST_BUFFER_TIMESTAMP (buffer) = 3 * GST_SECOND;
  GST_BUFFER_DURATION (buffer) = 1 * GST_SECOND;

  GST_DEBUG ("sending buffer");

  /* this buffer causes the sink to preroll */
  data = chain_async (sinkpad, buffer);
  fail_if (data == NULL);

  /* wait for preroll */
  ret = gst_element_get_state (pipeline, NULL, NULL, -1);

  /* preroll buffer is rendered, we expect no more buffer after this one */
  fret = chain_async_return (data);
834
  fail_unless (fret == GST_FLOW_EOS);
835 836 837 838

  /* do position query, this should succeed with the stream time of the buffer
   * against the clock. Since the buffer is synced against the clock, the time
   * should be at least the stream time of the buffer. */
839
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
840 841 842 843 844 845 846
  fail_unless (qret == TRUE);
  fail_unless (qcur >= 2 * GST_SECOND && qcur <= 3 * GST_SECOND);

  /* wait 2 more seconds, enough to test if the position was clipped correctly
   * against the segment */
  g_usleep (2 * G_USEC_PER_SEC);

847
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
848 849 850 851 852 853 854 855 856 857 858
  fail_unless (qret == TRUE);
  fail_unless (qcur == 3 * GST_SECOND);

  GST_DEBUG ("going to PAUSED");

  ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
  fail_unless (ret == GST_STATE_CHANGE_ASYNC);

  /* we report the time of the last start of the buffer. This is slightly
   * incorrect, we should report the exact time when we paused but there is no
   * record of that anywhere */
859
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
860
  fail_unless (qret == TRUE);
861
  fail_unless (qcur >= 4 * GST_SECOND);
862 863 864 865 866

  ret = gst_element_set_state (pipeline, GST_STATE_READY);
  fail_unless (ret == GST_STATE_CHANGE_SUCCESS);

  /* fails again because we are in the wrong state */
867
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
868 869 870 871
  fail_unless (qret == FALSE);

  gst_element_set_state (pipeline, GST_STATE_NULL);

872
  qret = gst_element_query_position (sink, GST_FORMAT_TIME, &qcur);
873 874 875 876 877 878 879 880
  fail_unless (qret == FALSE);

  gst_object_unref (sinkpad);
  gst_object_unref (pipeline);
}

GST_END_TEST;

881 882 883 884
/* like fakesrc, but also pushes an OOB event after each buffer */
typedef GstPushSrc OOBSource;
typedef GstPushSrcClass OOBSourceClass;

885
GType oob_source_get_type (void);
886
G_DEFINE_TYPE (OOBSource, oob_source, GST_TYPE_PUSH_SRC);
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

static GstFlowReturn
oob_source_create (GstPushSrc * src, GstBuffer ** p_buf)
{
  *p_buf = gst_buffer_new ();

  gst_pad_push_event (GST_BASE_SRC_PAD (src),
      gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, NULL));

  return GST_FLOW_OK;
}

static void
oob_source_class_init (OOBSourceClass * klass)
{
902 903 904
  static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("src",
      GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS_ANY);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
905 906
  GstPushSrcClass *pushsrc_class = GST_PUSH_SRC_CLASS (klass);

907 908 909
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sinktemplate));

910 911 912 913
  pushsrc_class->create = GST_DEBUG_FUNCPTR (oob_source_create);
}

static void
914
oob_source_init (OOBSource * src)
915 916 917 918
{
  /* nothing to do */
}

919
#define NOTIFY_RACE_NUM_PIPELINES 10
920 921 922 923 924 925 926 927 928 929 930 931

typedef struct
{
  GstElement *src;
  GstElement *queue;
  GstElement *sink;
  GstElement *pipe;
} NotifyRacePipeline;

static void
test_notify_race_setup_pipeline (NotifyRacePipeline * p)
{
932
  GST_DEBUG ("Creating pipeline");
933 934 935 936 937 938 939 940 941 942 943 944
  p->pipe = gst_pipeline_new ("pipeline");
  p->src = g_object_new (oob_source_get_type (), NULL);

  p->queue = gst_element_factory_make ("queue", NULL);
  g_object_set (p->queue, "max-size-buffers", 2, NULL);

  p->sink = gst_element_factory_make ("fakesink", NULL);
  gst_bin_add (GST_BIN (p->pipe), p->src);
  gst_bin_add (GST_BIN (p->pipe), p->queue);
  gst_bin_add (GST_BIN (p->pipe), p->sink);
  gst_element_link_many (p->src, p->queue, p->sink, NULL);

945
  GST_DEBUG ("Setting pipeline to PLAYING");
946 947
  fail_unless_equals_int (gst_element_set_state (p->pipe, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);
948
  GST_DEBUG ("Getting state");
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
  fail_unless_equals_int (gst_element_get_state (p->pipe, NULL, NULL, -1),
      GST_STATE_CHANGE_SUCCESS);
}

static void
test_notify_race_cleanup_pipeline (NotifyRacePipeline * p)
{
  gst_element_set_state (p->pipe, GST_STATE_NULL);
  gst_object_unref (p->pipe);
  memset (p, 0, sizeof (NotifyRacePipeline));
}

/* we create N pipelines to make sure the notify race isn't per-class, but
 * only per instance */
GST_START_TEST (test_notify_race)
{
  NotifyRacePipeline pipelines[NOTIFY_RACE_NUM_PIPELINES];
  int i;

  for (i = 0; i < G_N_ELEMENTS (pipelines); ++i) {
969
    GST_DEBUG ("Starting up pipeline %d", i);
970 971 972 973 974 975
    test_notify_race_setup_pipeline (&pipelines[i]);
  }

  g_usleep (2 * G_USEC_PER_SEC);

  for (i = 0; i < G_N_ELEMENTS (pipelines); ++i) {
976
    GST_DEBUG ("Cleaning up pipeline %d", i);
977 978 979 980 981 982
    test_notify_race_cleanup_pipeline (&pipelines[i]);
  }
}

GST_END_TEST;

983
static Suite *
984 985 986 987 988
fakesink_suite (void)
{
  Suite *s = suite_create ("fakesink");
  TCase *tc_chain = tcase_create ("general");

989 990
  tcase_set_timeout (tc_chain, 20);

991 992
  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_clipping);
993
  tcase_add_test (tc_chain, test_preroll_sync);
994
  tcase_add_test (tc_chain, test_eos);
995
  tcase_add_test (tc_chain, test_eos2);
996
  tcase_add_test (tc_chain, test_position);
997
  tcase_add_test (tc_chain, test_notify_race);
998 999 1000 1001

  return s;
}

1002
GST_CHECK_MAIN (fakesink);