rtpjitterbuffer.c 116 KB
Newer Older
1 2
/* GStreamer
 *
3
 * Copyright (C) 2009 Nokia Corporation and its subsidiary(-ies)
4
 *               contact: <stefan.kost@nokia.com>
5 6 7
 * Copyright (C) 2012 Cisco Systems, Inc
 *               Authors: Kelley Rogers <kelro@cisco.com>
 *               Havard Graff <hgraff@cisco.com>
8
 * Copyright (C) 2013-2016 Pexip AS
9 10
 *               Stian Selnes <stian@pexip>
 *               Havard Graff <havard@pexip>
11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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
24 25
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
26 27 28
 */

#include <gst/check/gstcheck.h>
29
#include <gst/check/gsttestclock.h>
30
#include <gst/check/gstharness.h>
31

32 33
#include <gst/rtp/gstrtpbuffer.h>

34 35 36
#include "gst/rtpmanager/gstrtpjitterbuffer.h"
#include "gst/rtpmanager/rtptimerqueue.h"

37 38 39 40 41 42
/* For ease of programming we use globals to keep refs for our floating
 * src and sink pads we create; otherwise we always have to do get_pad,
 * get_peer, and then remove references in every test function */
static GstPad *mysrcpad, *mysinkpad;
/* we also have a list of src buffers */
static GList *inbuffers = NULL;
43
static gint num_dropped = 0;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

#define RTP_CAPS_STRING    \
    "application/x-rtp, "               \
    "media = (string)audio, "           \
    "payload = (int) 0, "               \
    "clock-rate = (int) 8000, "         \
    "encoding-name = (string)PCMU"

#define RTP_FRAME_SIZE 20

static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp")
    );
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "clock-rate = (int) [ 1, 2147483647 ]")
    );

66
static void
67
buffer_dropped (G_GNUC_UNUSED gpointer data, GstMiniObject * obj)
68
{
Wim Taymans's avatar
Wim Taymans committed
69 70
  GST_DEBUG ("dropping buffer %p", obj);
  num_dropped++;
71 72
}

73 74 75 76 77 78 79
static GstElement *
setup_jitterbuffer (gint num_buffers)
{
  GstElement *jitterbuffer;
  GstClock *clock;
  GstBuffer *buffer;
  GstCaps *caps;
80
  /* a 20 sample audio block (2,5 ms) generated with
81 82
   * gst-launch audiotestsrc wave=silence blocksize=40 num-buffers=3 !
   *    "audio/x-raw,channels=1,rate=8000" ! mulawenc ! rtppcmupay !
83 84
   *     fakesink dump=1
   */
85 86
  guint8 in[] = {
    /* first 4 bytes are rtp-header, next 4 bytes are timestamp */
87 88 89 90 91 92
    0x80, 0x80, 0x1c, 0x24, 0x46, 0xcd, 0xb7, 0x11, 0x3c, 0x3a, 0x7c, 0x5b,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  };
  GstClockTime ts = G_GUINT64_CONSTANT (0);
  GstClockTime tso = gst_util_uint64_scale (RTP_FRAME_SIZE, GST_SECOND, 8000);
93
  /*guint latency = GST_TIME_AS_MSECONDS (num_buffers * tso); */
94 95 96
  gint i;

  GST_DEBUG ("setup_jitterbuffer");
Wim Taymans's avatar
Wim Taymans committed
97
  jitterbuffer = gst_check_setup_element ("rtpjitterbuffer");
98 99 100 101 102 103 104 105 106 107
  /* we need a clock here */
  clock = gst_system_clock_obtain ();
  gst_element_set_clock (jitterbuffer, clock);
  gst_object_unref (clock);
  /* setup latency */
  /* latency would be 7 for 3 buffers here, default is 200
     g_object_set (G_OBJECT (jitterbuffer), "latency", latency, NULL);
     GST_INFO_OBJECT (jitterbuffer, "set latency to %u ms", latency);
   */

108 109
  mysrcpad = gst_check_setup_src_pad (jitterbuffer, &srctemplate);
  mysinkpad = gst_check_setup_sink_pad (jitterbuffer, &sinktemplate);
110 111 112 113 114
  gst_pad_set_active (mysrcpad, TRUE);
  gst_pad_set_active (mysinkpad, TRUE);

  /* create n buffers */
  caps = gst_caps_from_string (RTP_CAPS_STRING);
115
  gst_check_setup_events (mysrcpad, jitterbuffer, caps, GST_FORMAT_TIME);
Wim Taymans's avatar
Wim Taymans committed
116 117
  gst_caps_unref (caps);

118 119
  for (i = 0; i < num_buffers; i++) {
    buffer = gst_buffer_new_and_alloc (sizeof (in));
Wim Taymans's avatar
Wim Taymans committed
120
    gst_buffer_fill (buffer, 0, in, sizeof (in));
Wim Taymans's avatar
Wim Taymans committed
121 122
    GST_BUFFER_DTS (buffer) = ts;
    GST_BUFFER_PTS (buffer) = ts;
123
    GST_BUFFER_DURATION (buffer) = tso;
Wim Taymans's avatar
Wim Taymans committed
124 125
    gst_mini_object_weak_ref (GST_MINI_OBJECT (buffer), buffer_dropped, NULL);
    GST_DEBUG ("created buffer: %p", buffer);
126 127 128 129 130 131 132 133 134 135 136 137

    if (!i)
      GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);

    inbuffers = g_list_append (inbuffers, buffer);

    /* hackish way to update the rtp header */
    in[1] = 0x00;
    in[3]++;                    /* seqnumber */
    in[7] += RTP_FRAME_SIZE;    /* inc. timestamp with framesize */
    ts += tso;
  }
138
  num_dropped = 0;
139 140 141 142

  return jitterbuffer;
}

Wim Taymans's avatar
Wim Taymans committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static GstStateChangeReturn
start_jitterbuffer (GstElement * jitterbuffer)
{
  GstStateChangeReturn ret;
  GstClockTime now;
  GstClock *clock;

  clock = gst_element_get_clock (jitterbuffer);
  now = gst_clock_get_time (clock);
  gst_object_unref (clock);

  gst_element_set_base_time (jitterbuffer, now);
  ret = gst_element_set_state (jitterbuffer, GST_STATE_PLAYING);

  return ret;
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173
static void
cleanup_jitterbuffer (GstElement * jitterbuffer)
{
  GST_DEBUG ("cleanup_jitterbuffer");

  g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
  g_list_free (buffers);
  buffers = NULL;

  g_list_free (inbuffers);
  inbuffers = NULL;

  gst_pad_set_active (mysrcpad, FALSE);
  gst_pad_set_active (mysinkpad, FALSE);
174
  gst_element_set_state (jitterbuffer, GST_STATE_NULL);
175 176 177 178 179 180
  gst_check_teardown_src_pad (jitterbuffer);
  gst_check_teardown_sink_pad (jitterbuffer);
  gst_check_teardown_element (jitterbuffer);
}

static void
181
check_jitterbuffer_results (gint num_buffers)
182 183 184 185 186
{
  GstBuffer *buffer;
  GList *node;
  GstClockTime ts = G_GUINT64_CONSTANT (0);
  GstClockTime tso = gst_util_uint64_scale (RTP_FRAME_SIZE, GST_SECOND, 8000);
Wim Taymans's avatar
Wim Taymans committed
187
  GstMapInfo map;
188 189 190
  guint16 prev_sn = 0, cur_sn;
  guint32 prev_ts = 0, cur_ts;

Wim Taymans's avatar
Wim Taymans committed
191 192
  /* sleep for twice the latency */
  g_usleep (400 * 1000);
193 194 195 196 197 198

  GST_INFO ("of %d buffer %d/%d received/dropped", num_buffers,
      g_list_length (buffers), num_dropped);
  /* if this fails, not all buffers have been processed */
  fail_unless_equals_int ((g_list_length (buffers) + num_dropped), num_buffers);

199 200 201 202
  /* check the buffer list */
  fail_unless_equals_int (g_list_length (buffers), num_buffers);
  for (node = buffers; node; node = g_list_next (node)) {
    fail_if ((buffer = (GstBuffer *) node->data) == NULL);
Wim Taymans's avatar
Wim Taymans committed
203
    fail_if (GST_BUFFER_PTS (buffer) != ts);
Wim Taymans's avatar
Wim Taymans committed
204 205 206 207 208
    gst_buffer_map (buffer, &map, GST_MAP_READ);
    cur_sn = ((guint16) map.data[2] << 8) | map.data[3];
    cur_ts = ((guint32) map.data[4] << 24) | ((guint32) map.data[5] << 16) |
        ((guint32) map.data[6] << 8) | map.data[7];
    gst_buffer_unmap (buffer, &map);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

    if (node != buffers) {
      fail_unless (cur_sn > prev_sn);
      fail_unless (cur_ts > prev_ts);

      prev_sn = cur_sn;
      prev_ts = cur_ts;
    }
    ts += tso;
  }
}

GST_START_TEST (test_push_forward_seq)
{
  GstElement *jitterbuffer;
  const guint num_buffers = 3;
  GstBuffer *buffer;
  GList *node;

  jitterbuffer = setup_jitterbuffer (num_buffers);
Wim Taymans's avatar
Wim Taymans committed
229 230
  fail_unless (start_jitterbuffer (jitterbuffer)
      == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
231 232 233 234 235 236 237 238

  /* push buffers: 0,1,2, */
  for (node = inbuffers; node; node = g_list_next (node)) {
    buffer = (GstBuffer *) node->data;
    fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  }

  /* check the buffer list */
239
  check_jitterbuffer_results (num_buffers);
240 241 242 243 244 245 246 247 248 249

  /* cleanup */
  cleanup_jitterbuffer (jitterbuffer);
}

GST_END_TEST;

GST_START_TEST (test_push_backward_seq)
{
  GstElement *jitterbuffer;
250
  const guint num_buffers = 4;
251 252 253 254
  GstBuffer *buffer;
  GList *node;

  jitterbuffer = setup_jitterbuffer (num_buffers);
Wim Taymans's avatar
Wim Taymans committed
255 256
  fail_unless (start_jitterbuffer (jitterbuffer)
      == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
257

258 259 260 261 262
  /* push buffers: 0,3,2,1 */
  buffer = (GstBuffer *) inbuffers->data;
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  for (node = g_list_last (inbuffers); node != inbuffers;
      node = g_list_previous (node)) {
263 264 265 266 267
    buffer = (GstBuffer *) node->data;
    fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  }

  /* check the buffer list */
268
  check_jitterbuffer_results (num_buffers);
269 270 271 272 273 274 275 276 277 278

  /* cleanup */
  cleanup_jitterbuffer (jitterbuffer);
}

GST_END_TEST;

GST_START_TEST (test_push_unordered)
{
  GstElement *jitterbuffer;
279
  const guint num_buffers = 4;
280 281 282
  GstBuffer *buffer;

  jitterbuffer = setup_jitterbuffer (num_buffers);
Wim Taymans's avatar
Wim Taymans committed
283 284
  fail_unless (start_jitterbuffer (jitterbuffer)
      == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
285

286
  /* push buffers; 0,2,1,3 */
287 288 289 290 291 292
  buffer = (GstBuffer *) inbuffers->data;
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  buffer = g_list_nth_data (inbuffers, 2);
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  buffer = g_list_nth_data (inbuffers, 1);
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
293 294
  buffer = g_list_nth_data (inbuffers, 3);
  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
295 296

  /* check the buffer list */
297
  check_jitterbuffer_results (num_buffers);
298 299 300 301 302 303 304

  /* cleanup */
  cleanup_jitterbuffer (jitterbuffer);
}

GST_END_TEST;

305 306 307
gboolean is_eos;

static gboolean
308 309
eos_event_function (G_GNUC_UNUSED GstPad * pad,
    G_GNUC_UNUSED GstObject * parent, GstEvent * event)
310 311 312 313 314 315 316
{
  if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
    g_mutex_lock (&check_mutex);
    is_eos = TRUE;
    g_cond_signal (&check_cond);
    g_mutex_unlock (&check_mutex);
  }
317
  gst_event_unref (event);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  return TRUE;
}

GST_START_TEST (test_push_eos)
{
  GstElement *jitterbuffer;
  const guint num_buffers = 5;
  GList *node;
  GstStructure *stats;
  guint64 pushed, lost, late, duplicates;
  int n = 0;

  is_eos = FALSE;

  jitterbuffer = setup_jitterbuffer (num_buffers);
  gst_pad_set_event_function (mysinkpad, eos_event_function);

  g_object_set (jitterbuffer, "latency", 1, NULL);

  fail_unless (start_jitterbuffer (jitterbuffer)
      == GST_STATE_CHANGE_SUCCESS, "could not set to playing");

  /* push buffers: 0,1,2, */
  for (node = inbuffers; node; node = g_list_next (node)) {
342 343 344 345 346 347
    GstBuffer *buffer;

    /* steal buffer from list */
    buffer = node->data;
    node->data = NULL;

348 349 350
    n++;
    /* Skip 1 */
    if (n == 2) {
351
      gst_buffer_unref (buffer);
352 353 354 355 356 357 358 359 360 361 362 363
      continue;
    }
    fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  }

  gst_pad_push_event (mysrcpad, gst_event_new_eos ());

  g_mutex_lock (&check_mutex);
  while (!is_eos)
    g_cond_wait (&check_cond, &check_mutex);
  g_mutex_unlock (&check_mutex);

364 365
  fail_unless_equals_int (g_list_length (buffers), num_buffers - 1);

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  /* Verify statistics */
  g_object_get (jitterbuffer, "stats", &stats, NULL);
  gst_structure_get (stats, "num-pushed", G_TYPE_UINT64, &pushed,
      "num-lost", G_TYPE_UINT64, &lost,
      "num-late", G_TYPE_UINT64, &late,
      "num-duplicates", G_TYPE_UINT64, &duplicates, NULL);
  fail_unless_equals_int (pushed, g_list_length (inbuffers) - 1);
  fail_unless_equals_int (lost, 1);
  fail_unless_equals_int (late, 0);
  fail_unless_equals_int (duplicates, 0);
  gst_structure_free (stats);

  /* cleanup */
  cleanup_jitterbuffer (jitterbuffer);
}

GST_END_TEST;

384 385 386 387 388 389 390 391 392
GST_START_TEST (test_basetime)
{
  GstElement *jitterbuffer;
  const guint num_buffers = 3;
  GstBuffer *buffer;
  GList *node;
  GstClockTime tso = gst_util_uint64_scale (RTP_FRAME_SIZE, GST_SECOND, 8000);

  jitterbuffer = setup_jitterbuffer (num_buffers);
Wim Taymans's avatar
Wim Taymans committed
393 394
  fail_unless (start_jitterbuffer (jitterbuffer)
      == GST_STATE_CHANGE_SUCCESS, "could not set to playing");
395 396 397 398 399 400 401

  /* push buffers: 2,1,0 */
  for (node = g_list_last (inbuffers); node; node = g_list_previous (node)) {
    buffer = (GstBuffer *) node->data;
    fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  }

Wim Taymans's avatar
Wim Taymans committed
402 403
  /* sleep for twice the latency */
  g_usleep (400 * 1000);
404 405 406 407 408

  /* if this fails, not all buffers have been processed */
  fail_unless_equals_int ((g_list_length (buffers) + num_dropped), num_buffers);

  buffer = (GstBuffer *) buffers->data;
Wim Taymans's avatar
Wim Taymans committed
409 410
  fail_unless (GST_BUFFER_DTS (buffer) != (num_buffers * tso));
  fail_unless (GST_BUFFER_PTS (buffer) != (num_buffers * tso));
411 412 413 414 415 416 417

  /* cleanup */
  cleanup_jitterbuffer (jitterbuffer);
}

GST_END_TEST;

418
static GstCaps *
419
request_pt_map (G_GNUC_UNUSED GstElement * jitterbuffer, guint pt)
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
{
  fail_unless (pt == 0);

  return gst_caps_from_string (RTP_CAPS_STRING);
}

GST_START_TEST (test_clear_pt_map)
{
  GstElement *jitterbuffer;
  const guint num_buffers = 10;
  gint i;
  GstBuffer *buffer;
  GList *node;

  jitterbuffer = setup_jitterbuffer (num_buffers);
  fail_unless (start_jitterbuffer (jitterbuffer)
      == GST_STATE_CHANGE_SUCCESS, "could not set to playing");

  g_signal_connect (jitterbuffer, "request-pt-map", (GCallback)
      request_pt_map, NULL);

  /* push buffers: 0,1,2, */
  for (node = inbuffers, i = 0; node && i < 3; node = g_list_next (node), i++) {
    buffer = (GstBuffer *) node->data;
    fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  }

  g_usleep (400 * 1000);

  g_signal_emit_by_name (jitterbuffer, "clear-pt-map", NULL);

  for (; node && i < 10; node = g_list_next (node), i++) {
    buffer = (GstBuffer *) node->data;
    fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
  }

  /* check the buffer list */
457
  check_jitterbuffer_results (num_buffers);
458 459 460 461 462 463

  /* cleanup */
  cleanup_jitterbuffer (jitterbuffer);
}

GST_END_TEST;
464

465
#define TEST_BUF_CLOCK_RATE 8000
466
#define AS_TEST_BUF_RTP_TIME(gst_time) gst_util_uint64_scale_int (TEST_BUF_CLOCK_RATE, gst_time, GST_SECOND)
467 468 469 470 471
#define TEST_BUF_PT 0
#define TEST_BUF_SSRC 0x01BADBAD
#define TEST_BUF_MS  20
#define TEST_BUF_DURATION (TEST_BUF_MS * GST_MSECOND)
#define TEST_BUF_SIZE (64000 * TEST_BUF_MS / 1000)
472
#define TEST_RTP_TS_DURATION AS_TEST_BUF_RTP_TIME (TEST_BUF_DURATION)
473 474 475 476 477 478

static GstCaps *
generate_caps (void)
{
  return gst_caps_new_simple ("application/x-rtp",
      "media", G_TYPE_STRING, "audio",
479 480 481 482
      "clock-rate", G_TYPE_INT, TEST_BUF_CLOCK_RATE,
      "encoding-name", G_TYPE_STRING, "TEST",
      "payload", G_TYPE_INT, TEST_BUF_PT,
      "ssrc", G_TYPE_UINT, TEST_BUF_SSRC, NULL);
483 484 485
}

static GstBuffer *
486
generate_test_buffer_full (GstClockTime dts, guint seq_num, guint32 rtp_ts)
487 488 489 490
{
  GstBuffer *buf;
  guint8 *payload;
  guint i;
491
  GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
492

493 494
  buf = gst_rtp_buffer_new_allocate (TEST_BUF_SIZE, 0, 0);
  GST_BUFFER_DTS (buf) = dts;
495 496

  gst_rtp_buffer_map (buf, GST_MAP_READWRITE, &rtp);
497
  gst_rtp_buffer_set_payload_type (&rtp, TEST_BUF_PT);
498 499
  gst_rtp_buffer_set_seq (&rtp, seq_num);
  gst_rtp_buffer_set_timestamp (&rtp, rtp_ts);
500
  gst_rtp_buffer_set_ssrc (&rtp, TEST_BUF_SSRC);
501 502

  payload = gst_rtp_buffer_get_payload (&rtp);
503
  for (i = 0; i < TEST_BUF_SIZE; i++)
504 505
    payload[i] = 0xff;

506 507
  gst_rtp_buffer_unmap (&rtp);

508 509 510
  return buf;
}

511 512 513
static GstBuffer *
generate_test_buffer (guint seq_num)
{
514 515
  return generate_test_buffer_full (seq_num * TEST_BUF_DURATION,
      seq_num, seq_num * TEST_RTP_TS_DURATION);
516 517
}

518 519 520
static GstBuffer *
generate_test_buffer_rtx (GstClockTime dts, guint seq_num)
{
521 522
  GstBuffer *buffer = generate_test_buffer_full (dts, seq_num,
      seq_num * TEST_RTP_TS_DURATION);
523 524 525 526
  GST_BUFFER_FLAG_SET (buffer, GST_RTP_BUFFER_FLAG_RETRANSMISSION);
  return buffer;
}

527 528 529 530 531 532 533 534
static void
push_test_buffer (GstHarness * h, guint seq_num)
{
  gst_harness_set_time (h, seq_num * TEST_BUF_DURATION);
  fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h,
          generate_test_buffer (seq_num)));
}

535 536 537 538 539 540 541 542 543 544 545
static gint
get_rtp_seq_num (GstBuffer * buf)
{
  GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
  gint seq;
  gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
  seq = gst_rtp_buffer_get_seq (&rtp);
  gst_rtp_buffer_unmap (&rtp);
  return seq;
}

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 611 612
static void
verify_lost_event (GstHarness * h, guint exp_seq, GstClockTime exp_ts,
    GstClockTime exp_dur)
{
  GstEvent *event;
  const GstStructure *s;
  const GValue *value;
  guint seq;
  GstClockTime ts;
  GstClockTime dur;

  event = gst_harness_pull_event (h);
  fail_unless (event != NULL);

  s = gst_event_get_structure (event);
  fail_unless (s != NULL);
  fail_unless (gst_structure_get_uint (s, "seqnum", &seq));

  value = gst_structure_get_value (s, "timestamp");
  fail_unless (value && G_VALUE_HOLDS_UINT64 (value));

  ts = g_value_get_uint64 (value);
  value = gst_structure_get_value (s, "duration");
  fail_unless (value && G_VALUE_HOLDS_UINT64 (value));

  dur = g_value_get_uint64 (value);
  fail_unless_equals_int ((guint16) exp_seq, seq);
  fail_unless_equals_uint64 (exp_ts, ts);
  fail_unless_equals_uint64 (exp_dur, dur);

  gst_event_unref (event);
}

static void
verify_rtx_event (GstHarness * h, guint exp_seq, GstClockTime exp_ts,
    gint exp_delay, GstClockTime exp_spacing)
{
  GstEvent *event;
  const GstStructure *s;
  const GValue *value;
  guint seq;
  GstClockTime ts;
  guint delay;
  GstClockTime spacing;

  event = gst_harness_pull_upstream_event (h);
  fail_unless (event != NULL);

  s = gst_event_get_structure (event);
  fail_unless (s != NULL);
  fail_unless (gst_structure_get_uint (s, "seqnum", &seq));

  value = gst_structure_get_value (s, "running-time");
  fail_unless (value && G_VALUE_HOLDS_UINT64 (value));

  ts = g_value_get_uint64 (value);
  fail_unless (gst_structure_get_uint (s, "delay", &delay));
  value = gst_structure_get_value (s, "packet-spacing");
  fail_unless (value && G_VALUE_HOLDS_UINT64 (value));
  spacing = g_value_get_uint64 (value);
  fail_unless_equals_int ((guint16) exp_seq, seq);
  fail_unless_equals_uint64 (exp_ts, ts);
  fail_unless_equals_int (exp_delay, delay);
  fail_unless_equals_uint64 (exp_spacing, spacing);

  gst_event_unref (event);
}
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
static gboolean
verify_jb_stats (GstElement * jb, GstStructure * expected)
{
  gboolean ret;
  GstStructure *actual;
  g_object_get (jb, "stats", &actual, NULL);

  ret = gst_structure_is_subset (actual, expected);

  if (!ret) {
    gchar *e_str = gst_structure_to_string (expected);
    gchar *a_str = gst_structure_to_string (actual);
    fail_unless (ret, "%s is not a subset of %s", e_str, a_str);
    g_free (e_str);
    g_free (a_str);
  }

  gst_structure_free (expected);
  gst_structure_free (actual);

  return ret;
}

637 638 639 640 641
static guint
construct_deterministic_initial_state (GstHarness * h, gint latency_ms)
{
  guint next_seqnum = latency_ms / TEST_BUF_MS + 1;
  guint seqnum;
642
  gint i;
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

  g_assert (latency_ms % TEST_BUF_MS == 0);

  gst_harness_set_src_caps (h, generate_caps ());
  g_object_set (h->element, "latency", latency_ms, NULL);

  /* When the first packet arrives in the jitterbuffer, it will create a
   * timeout for this packet equal to the latency of the jitterbuffer.
   * This is known as DEADLINE internally, and is meant to allow the stream
   * to buffer a bit before starting to push it out, to get some ideas about
   * the nature of the stream. (packetspacing, jitter etc.)
   *
   * When writing tests using the test-clock, it it hence important to know
   * that by simply advancing the clock to this timeout, you are basically
   * describing a stream that had one initial packet, and then nothing at all
   * for the duration of the latency (100ms in this test), which is not a very
   * usual scenario.
   *
   * Instead, a pattern used throughout this test-suite, is to keep the buffers
   * arriving at their optimal time, until the DEADLINE is reached, and that
   * then becomes the "starting-point" for the test, because at this time
   * there should now be no waiting timers (unless using rtx) and we have
   * a "clean" state to craft the test from.
   */

  /* Packet 0 arrives at time 0ms, Packet 5 arrives at time 100ms */
  for (seqnum = 0; seqnum < next_seqnum; seqnum++) {
    push_test_buffer (h, seqnum);
    gst_harness_wait_for_clock_id_waits (h, 1, 60);
  }

  /* We release the DEADLINE timer for packet 0, verify the time is indeed
   * @latency_ms (100ms) and pull out all the buffers that have been released,
   * and verify their PTS and sequence numbers.
   */
  gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_int64 (latency_ms * GST_MSECOND,
      gst_clock_get_time (GST_ELEMENT_CLOCK (h->element)));
  for (seqnum = 0; seqnum < next_seqnum; seqnum++) {
    GstBuffer *buf = gst_harness_pull (h);
    fail_unless_equals_uint64 (seqnum * TEST_BUF_DURATION,
        GST_BUFFER_PTS (buf));
    fail_unless_equals_int (seqnum, get_rtp_seq_num (buf));
    gst_buffer_unref (buf);
  }

  /* drop GstEventStreamStart & GstEventCaps & GstEventSegment */
690
  for (i = 0; i < 3; i++)
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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    gst_event_unref (gst_harness_pull_event (h));

  /* drop reconfigure event */
  gst_event_unref (gst_harness_pull_upstream_event (h));

  /* Verify that at this point our queues are empty */
  fail_unless_equals_int (0, gst_harness_buffers_in_queue (h));
  fail_unless_equals_int (0, gst_harness_events_in_queue (h));

  return next_seqnum;
}

GST_START_TEST (test_lost_event)
{
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
  GstBuffer *buf;
  gint latency_ms = 100;
  guint next_seqnum;
  guint missing_seqnum;

  g_object_set (h->element, "do-lost", TRUE, NULL);
  next_seqnum = construct_deterministic_initial_state (h, latency_ms);

  /* We will now create a gap in the stream, by skipping one sequence-number,
   * and push the following packet.
   */
  missing_seqnum = next_seqnum;
  next_seqnum += 1;
  push_test_buffer (h, next_seqnum);

  /* This packet (@next_seqnum) will now be held back, awaiting the missing one,
   * verify that this is the case:
   */
  fail_unless_equals_int (0, gst_harness_buffers_in_queue (h));
  fail_unless_equals_int (0, gst_harness_events_in_queue (h));

  /* The lost-timeout for the missing packet will now be its pts + latency, so
   * now we will simply crank the clock to advance to this point in time, and
   * check that we get a lost-event, as well as the last packet we pushed in.
   */
  gst_harness_crank_single_clock_wait (h);
  verify_lost_event (h, missing_seqnum,
      missing_seqnum * TEST_BUF_DURATION, TEST_BUF_DURATION);

  buf = gst_harness_pull (h);
  fail_unless_equals_uint64 (next_seqnum * TEST_BUF_DURATION,
      GST_BUFFER_PTS (buf));
  fail_unless_equals_int (next_seqnum, get_rtp_seq_num (buf));
  gst_buffer_unref (buf);

  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
              "num-pushed", G_TYPE_UINT64, (guint64) next_seqnum,
              "num-lost", G_TYPE_UINT64, (guint64) 1, NULL)));

  gst_harness_teardown (h);
}

GST_END_TEST;

751 752
GST_START_TEST (test_only_one_lost_event_on_large_gaps)
{
753 754
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
  GstTestClock *testclock;
755
  GstBuffer *out_buf;
756 757 758
  guint next_seqnum;
  gint latency_ms = 200;
  gint num_lost_events = latency_ms / TEST_BUF_MS;
759
  gint i;
760

761
  testclock = gst_harness_get_testclock (h);
762 763 764 765 766
  /* Need to set max-misorder-time and max-dropout-time to 0 so the
   * jitterbuffer does not base them on packet rate calculations.
   * If it does, out gap is big enough to be considered a new stream and
   * we wait for a few consecutive packets just to be sure
   */
767
  g_object_set (h->element, "do-lost", TRUE,
768
      "max-misorder-time", 0, "max-dropout-time", 0, NULL);
769
  next_seqnum = construct_deterministic_initial_state (h, latency_ms);
770

771
  /* move time ahead to just before 10 seconds */
772
  gst_harness_set_time (h, 10 * GST_SECOND - 1);
773

774 775
  /* check that we have no pending waits */
  fail_unless_equals_int (0, gst_test_clock_peek_id_count (testclock));
776

Wim Taymans's avatar
Wim Taymans committed
777
  /* a buffer now arrives perfectly on time */
778 779
  fail_unless_equals_int (GST_FLOW_OK,
      gst_harness_push (h, generate_test_buffer (500)));
780

781
  /* release the wait, advancing the clock to 10 sec */
782
  fail_unless (gst_harness_crank_single_clock_wait (h));
783

784 785 786
  /* we should now receive a packet-lost-event for buffers 11 through 489 ... */
  verify_lost_event (h, next_seqnum,
      next_seqnum * TEST_BUF_DURATION, TEST_BUF_DURATION * (490 - next_seqnum));
787

788
  /* ... as well as 490 (since at 10 sec 490 is too late) */
789
  verify_lost_event (h, 490, 490 * TEST_BUF_DURATION, TEST_BUF_DURATION);
790

791 792
  /* we get as many lost events as the the number of *
   * buffers the jitterbuffer is able to wait for */
793
  for (i = 1; i < num_lost_events; i++) {
794
    fail_unless (gst_harness_crank_single_clock_wait (h));
795 796
    verify_lost_event (h, 490 + i, (490 + i) * TEST_BUF_DURATION,
        TEST_BUF_DURATION);
797 798
  }

799 800
  /* and then the buffer is released */
  out_buf = gst_harness_pull (h);
801 802 803
  fail_unless (GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT));
  fail_unless_equals_int (500, get_rtp_seq_num (out_buf));
  fail_unless_equals_uint64 (10 * GST_SECOND, GST_BUFFER_PTS (out_buf));
804
  gst_buffer_unref (out_buf);
805

806 807
  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
808
              "num-lost", G_TYPE_UINT64, (guint64) 489, NULL)));
809

810 811
  gst_object_unref (testclock);
  gst_harness_teardown (h);
812
}
813

814 815 816 817
GST_END_TEST;

GST_START_TEST (test_two_lost_one_arrives_in_time)
{
818 819
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
  GstTestClock *testclock;
820
  GstClockID id;
821 822 823 824 825 826
  GstBuffer *buf;
  gint latency_ms = 100;
  guint next_seqnum;
  guint first_missing;
  guint second_missing;
  guint current_arrived;
827

828
  testclock = gst_harness_get_testclock (h);
829 830
  g_object_set (h->element, "do-lost", TRUE, NULL);
  next_seqnum = construct_deterministic_initial_state (h, latency_ms);
831

Wim Taymans's avatar
Wim Taymans committed
832
  /* hop over 2 packets and make another one (gap of 2) */
833 834 835 836
  first_missing = next_seqnum;
  second_missing = next_seqnum + 1;
  current_arrived = next_seqnum + 2;
  push_test_buffer (h, current_arrived);
837

838 839 840
  /* verify that the jitterbuffer now wait for the latest moment it can push the
   * @first_missing packet out.
   */
841
  gst_test_clock_wait_for_next_pending_id (testclock, &id);
842 843
  fail_unless_equals_uint64 (first_missing * TEST_BUF_DURATION +
      latency_ms * GST_MSECOND, gst_clock_id_get_time (id));
844
  gst_clock_id_unref (id);
845

Wim Taymans's avatar
Wim Taymans committed
846
  /* let the time expire... */
847
  fail_unless (gst_harness_crank_single_clock_wait (h));
848

849 850 851
  /* we should now receive a packet-lost-event */
  verify_lost_event (h, first_missing,
      first_missing * TEST_BUF_DURATION, TEST_BUF_DURATION);
852

853
  /* @second_missing now arrives just in time */
854
  fail_unless_equals_int (GST_FLOW_OK,
855
      gst_harness_push (h, generate_test_buffer (second_missing)));
856

857 858 859 860 861
  /* verify that @second_missing made it through! */
  buf = gst_harness_pull (h);
  fail_unless (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT));
  fail_unless_equals_int (second_missing, get_rtp_seq_num (buf));
  gst_buffer_unref (buf);
862

863 864 865 866 867
  /* and see that @current_arrived now also is pushed */
  buf = gst_harness_pull (h);
  fail_unless (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT));
  fail_unless_equals_int (current_arrived, get_rtp_seq_num (buf));
  gst_buffer_unref (buf);
868

869 870
  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
871
              "num-pushed", G_TYPE_UINT64, (guint64) next_seqnum + 2,
872 873
              "num-lost", G_TYPE_UINT64, (guint64) 1, NULL)));

874 875
  gst_object_unref (testclock);
  gst_harness_teardown (h);
876
}
877

878 879 880 881
GST_END_TEST;

GST_START_TEST (test_late_packets_still_makes_lost_events)
{
882
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
883
  GstBuffer *out_buf;
884 885 886 887
  gint latency_ms = 100;
  guint next_seqnum;
  guint seqnum;
  GstClockTime now;
888

889 890
  g_object_set (h->element, "do-lost", TRUE, NULL);
  next_seqnum = construct_deterministic_initial_state (h, latency_ms);
891

892 893 894
  /* jump 10 seconds forward in time */
  now = 10 * GST_SECOND;
  gst_harness_set_time (h, now);
895

896 897 898 899 900
  /* push a packet with a gap of 2, that now is very late */
  seqnum = next_seqnum + 2;
  fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h,
          generate_test_buffer_full (now,
              seqnum, seqnum * TEST_RTP_TS_DURATION)));
901

902
  /* we should now receive packet-lost-events for the gap
903 904 905 906 907
   * FIXME: The timeout and duration here are a bit crap...
   */
  verify_lost_event (h, next_seqnum, 3400 * GST_MSECOND, 6500 * GST_MSECOND);
  verify_lost_event (h, next_seqnum + 1,
      9900 * GST_MSECOND, 3300 * GST_MSECOND);
908

909
  /* verify that packet @seqnum made it through! */
910
  out_buf = gst_harness_pull (h);
911
  fail_unless (GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT));
912
  fail_unless_equals_int (seqnum, get_rtp_seq_num (out_buf));
913
  gst_buffer_unref (out_buf);
914

915 916
  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
917
              "num-pushed", G_TYPE_UINT64, (guint64) next_seqnum + 1,
918 919
              "num-lost", G_TYPE_UINT64, (guint64) 2, NULL)));

920
  gst_harness_teardown (h);
921
}
922

923 924
GST_END_TEST;

925 926 927 928 929

GST_START_TEST (test_num_late_when_considered_lost_arrives)
{
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
  gboolean do_lost = __i__ != 0;
930 931
  gint latency_ms = 100;
  guint next_seqnum;
932

933 934
  g_object_set (h->element, "do-lost", do_lost, NULL);
  next_seqnum = construct_deterministic_initial_state (h, latency_ms);
935 936

  /* gap of 1 */
937
  push_test_buffer (h, next_seqnum + 1);
938

939
  /* crank to trigger lost-event */
940 941 942
  gst_harness_crank_single_clock_wait (h);

  if (do_lost) {
943 944 945
    /* we should now receive packet-lost-events for the missing packet */
    verify_lost_event (h, next_seqnum,
        next_seqnum * TEST_BUF_DURATION, TEST_BUF_DURATION);
946 947
  }

948
  /* pull out the pushed packet */
949 950 951 952 953
  gst_buffer_unref (gst_harness_pull (h));

  /* we have one lost packet in the stats */
  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
954
              "num-pushed", G_TYPE_UINT64, (guint64) next_seqnum + 1,
955 956 957
              "num-lost", G_TYPE_UINT64, (guint64) 1,
              "num-late", G_TYPE_UINT64, (guint64) 0, NULL)));

958
  /* the missing packet now arrives (too late) */
959
  fail_unless_equals_int (GST_FLOW_OK,
960
      gst_harness_push (h, generate_test_buffer (next_seqnum)));
961 962 963 964

  /* and this increments num-late */
  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
965
              "num-pushed", G_TYPE_UINT64, (guint64) next_seqnum + 1,
966 967 968 969 970 971 972 973
              "num-lost", G_TYPE_UINT64, (guint64) 1,
              "num-late", G_TYPE_UINT64, (guint64) 1, NULL)));

  gst_harness_teardown (h);
}

GST_END_TEST;

974 975 976 977
GST_START_TEST (test_lost_event_uses_pts)
{
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
  GstClockTime now;
978 979 980
  gint latency_ms = 100;
  guint next_seqnum;
  guint lost_seqnum;
981

982 983
  g_object_set (h->element, "do-lost", TRUE, NULL);
  next_seqnum = construct_deterministic_initial_state (h, latency_ms);
984

985
  /* hop over 1 packets and make another one (gap of 1), but due to
986
     network delays, this packets is also grossly late */
987 988
  lost_seqnum = next_seqnum;
  next_seqnum += 1;
989

990 991
  /* advance the clock to the latest time packet @next_seqnum could arrive */
  now = next_seqnum * TEST_BUF_DURATION + latency_ms * GST_MSECOND;
992
  gst_harness_set_time (h, now);
993 994
  gst_harness_push (h, generate_test_buffer_full (now, next_seqnum,
          next_seqnum * TEST_RTP_TS_DURATION));
995 996

  /* we should now have received a packet-lost-event for buffer 3 */
997 998
  verify_lost_event (h, lost_seqnum,
      lost_seqnum * TEST_BUF_DURATION, TEST_BUF_DURATION);
999 1000 1001 1002 1003 1004

  /* and pull out packet 4 */
  gst_buffer_unref (gst_harness_pull (h));

  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
1005
              "num-pushed", G_TYPE_UINT64, (guint64) next_seqnum,
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
              "num-lost", G_TYPE_UINT64, (guint64) 1, NULL)));

  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_lost_event_with_backwards_rtptime)
{
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
1016
  gint latency_ms = 40;
1017

1018 1019
  g_object_set (h->element, "do-lost", TRUE, NULL);
  construct_deterministic_initial_state (h, latency_ms);
1020 1021

  /*
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
   * For video using B-frames, an expected sequence
   * could be like this:
   * (I = I-frame, P = P-frame, B = B-frame)
   *               ___   ___   ___   ___   ___
   *          ... | 3 | | 4 | | 5 | | 6 | | 7 |
   *               –––   –––   –––   –––   –––
   * rtptime:       3(I)  5(P)  5(P)  4(B)  6(P)
   * arrival(dts):  3     5     5     5     6
   *
   * Notice here that packet 6 (the B frame) make
   * the rtptime go backwards.
   *
   * But we get this:
   *               ___   ___   _ _   ___   ___
   *          ... | 3 | | 4 | |   | | 6 | | 7 |
   *               –––   –––   - -   –––   –––
   * rtptime:       3(I)  5(P)        4(B)  6(P)
   * arrival(dts):  3     5           5     6
   *
1041
   */
1042 1043

  /* seqnum 3 */
1044
  push_test_buffer (h, 3);
1045 1046
  gst_buffer_unref (gst_harness_pull (h));

1047
  /* seqnum 4, arriving at time 5 with rtptime 5 */
1048 1049 1050
  gst_harness_push (h,
      generate_test_buffer_full (5 * TEST_BUF_DURATION,
          4, 5 * TEST_RTP_TS_DURATION));
1051 1052 1053 1054
  gst_buffer_unref (gst_harness_pull (h));

  /* seqnum 6, arriving at time 5 with rtptime 4,
     making a gap for missing seqnum 5 */
1055 1056 1057
  gst_harness_push (h,
      generate_test_buffer_full (5 * TEST_BUF_DURATION,
          6, 4 * TEST_RTP_TS_DURATION));
1058 1059

  /* seqnum 7, arriving at time 6 with rtptime 6 */
1060 1061 1062
  gst_harness_push (h,
      generate_test_buffer_full (6 * TEST_BUF_DURATION,
          7, 6 * TEST_RTP_TS_DURATION));
1063 1064 1065 1066

  /* we should now have received a packet-lost-event for seqnum 5,
     with time 5 and 0 duration */
  gst_harness_crank_single_clock_wait (h);
1067
  verify_lost_event (h, 5, 5 * TEST_BUF_DURATION, 0);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

  /* and pull out 6 and 7 */
  gst_buffer_unref (gst_harness_pull (h));
  gst_buffer_unref (gst_harness_pull (h));

  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
              "num-pushed", G_TYPE_UINT64, (guint64) 7,
              "num-lost", G_TYPE_UINT64, (guint64) 1, NULL)));

  gst_harness_teardown (h);
}

GST_END_TEST;

1083 1084
GST_START_TEST (test_all_packets_are_timestamped_zero)
{
1085
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
1086 1087
  GstBuffer *out_buf;
  gint jb_latency_ms = 100;
1088
  gint i, b;
1089

1090
  gst_harness_set_src_caps (h, generate_caps ());
1091
  g_object_set (h->element, "do-lost", TRUE, "latency", jb_latency_ms, NULL);
1092

1093
  /* advance the clock with 10 seconds */
1094
  gst_harness_set_time (h, 10 * GST_SECOND);
1095

1096 1097
  /* push the first buffer through */
  gst_buffer_unref (gst_harness_push_and_pull (h, generate_test_buffer (0)));
1098

1099
  /* push some buffers in, all timestamped 0 */
1100
  for (b = 1; b < 3; b++) {
1101 1102
    fail_unless_equals_int (GST_FLOW_OK,
        gst_harness_push (h,
1103
            generate_test_buffer_full (0 * GST_MSECOND, b, 0)));
1104

Wim Taymans's avatar
Wim Taymans committed
1105
    /* check for the buffer coming out that was pushed in */
1106
    out_buf = gst_harness_pull (h);
1107
    fail_unless_equals_uint64 (0, GST_BUFFER_PTS (out_buf));
1108
    gst_buffer_unref (out_buf);
1109 1110
  }

Wim Taymans's avatar
Wim Taymans committed
1111
  /* hop over 2 packets and make another one (gap of 2) */
1112
  b = 5;
1113
  fail_unless_equals_int (GST_FLOW_OK,
1114
      gst_harness_push (h, generate_test_buffer_full (0 * GST_MSECOND, b, 0)));
1115

1116
  /* drop GstEventStreamStart & GstEventCaps & GstEventSegment */
1117
  for (i = 0; i < 3; i++)
1118
    gst_event_unref (gst_harness_pull_event (h));
1119

1120
  /* we should now receive packet-lost-events for buffer 3 and 4 */
1121 1122
  verify_lost_event (h, 3, 0, 0);
  verify_lost_event (h, 4, 0, 0);
1123

Wim Taymans's avatar
Wim Taymans committed
1124
  /* verify that buffer 5 made it through! */
1125
  out_buf = gst_harness_pull (h);
1126 1127
  fail_unless (GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT));
  fail_unless_equals_int (5, get_rtp_seq_num (out_buf));
1128
  gst_buffer_unref (out_buf);
1129

1130 1131 1132 1133 1134
  fail_unless (verify_jb_stats (h->element,
          gst_structure_new ("application/x-rtp-jitterbuffer-stats",
              "num-pushed", G_TYPE_UINT64, (guint64) 4,
              "num-lost", G_TYPE_UINT64, (guint64) 2, NULL)));

1135
  gst_harness_teardown (h);
1136
}
1137

1138
GST_END_TEST;
1139

1140 1141 1142 1143 1144 1145 1146 1147 1148
GST_START_TEST (test_reorder_of_non_equidistant_packets)
{
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
  GstTestClock *testclock;
  gint latency_ms = 5;
  GstClockID pending_id;
  GstClockTime time;
  gint seq, frame;
  gint num_init_frames = 1;
1149 1150
  const GstClockTime frame_dur = TEST_BUF_DURATION;
  const guint32 frame_rtp_ts_dur = TEST_RTP_TS_DURATION;
1151 1152 1153 1154 1155 1156 1157 1158 1159

  gst_harness_set_src_caps (h, generate_caps ());
  testclock = gst_harness_get_testclock (h);
  g_object_set (h->element, "do-lost", TRUE, "latency", latency_ms, NULL);

  for (frame = 0, seq = 0; frame < num_init_frames; frame++, seq += 2) {
    /* Push a couple of packets with identical timestamp, typical for a video
     * stream where one frame generates multiple packets. */
    gst_harness_set_time (h, frame * frame_dur);
1160
    gst_harness_push (h, generate_test_buffer_full (frame * frame_dur,
1161
            seq, frame * frame_rtp_ts_dur));
1162
    gst_harness_push (h, generate_test_buffer_full (frame * frame_dur,
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
            seq + 1, frame * frame_rtp_ts_dur));

    if (frame == 0)
      /* deadline for buffer 0 expires */
      gst_harness_crank_single_clock_wait (h);

    gst_buffer_unref (gst_harness_pull (h));
    gst_buffer_unref (gst_harness_pull (h));
  }

  /* Finally push the last frame reordered */
  gst_harness_set_time (h, frame * frame_dur);
1175
  gst_harness_push (h, generate_test_buffer_full (frame * frame_dur,
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
          seq + 1, frame * frame_rtp_ts_dur));

  /* Check the scheduled lost timer. The expected arrival of this packet
   * should be assumed to be the same as the last packet received since we
   * don't know wether the missing packet belonged to this or previous
   * frame. */
  gst_test_clock_wait_for_next_pending_id (testclock, &pending_id);
  time = gst_clock_id_get_time (pending_id);
  fail_unless_equals_int64 (time, frame * frame_dur + latency_ms * GST_MSECOND);
  gst_clock_id_unref (pending_id);

  /* And then missing packet arrives just in time */
  gst_harness_set_time (h, time - 1);
1189
  gst_harness_push (h, generate_test_buffer_full (time - 1, seq,
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
          frame * frame_rtp_ts_dur));

  gst_buffer_unref (gst_harness_pull (h));
  gst_buffer_unref (gst_harness_pull (h));

  gst_object_unref (testclock);
  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_loss_equidistant_spacing_with_parameter_packets)
{
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
  gint latency_ms = 5;
  gint seq, frame;
  gint num_init_frames = 10;
1207
  gint i;
1208 1209 1210 1211 1212

  gst_harness_set_src_caps (h, generate_caps ());
  g_object_set (h->element, "do-lost", TRUE, "latency", latency_ms, NULL);

  /* drop stream-start, caps, segment */
1213
  for (i = 0; i < 3; i++)
1214 1215 1216
    gst_event_unref (gst_harness_pull_event (h));

  for (frame = 0, seq = 0; frame < num_init_frames; frame++, seq++) {
1217 1218 1219
    gst_harness_set_time (h, frame * TEST_BUF_DURATION);
    gst_harness_push (h, generate_test_buffer_full (frame * TEST_BUF_DURATION,
            seq, frame * TEST_RTP_TS_DURATION));
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

    if (frame == 0)
      /* deadline for buffer 0 expires */
      gst_harness_crank_single_clock_wait (h);

    gst_buffer_unref (gst_harness_pull (h));
  }

  /* Push three packets with same rtptime, simulating parameter packets +
   * frame. This should not disable equidistant mode as it is common for
   * certain audio codecs. */
1231
  for (i = 0; i < 3; i++) {
1232 1233 1234
    gst_harness_set_time (h, frame * TEST_BUF_DURATION);
    gst_harness_push (h, generate_test_buffer_full (frame * TEST_BUF_DURATION,
            seq++, frame * TEST_RTP_TS_DURATION));
1235 1236 1237 1238 1239
    gst_buffer_unref (gst_harness_pull (h));
  }
  frame++;

  /* Finally push the last packet introducing a gap */
1240 1241 1242
  gst_harness_set_time (h, frame * TEST_BUF_DURATION);
  gst_harness_push (h, generate_test_buffer_full (frame * TEST_BUF_DURATION,
          seq + 1, frame * TEST_RTP_TS_DURATION));
1243 1244 1245

  /* Check that the lost event has been generated assuming equidistant
   * spacing. */
1246 1247
  verify_lost_event (h, seq,
      frame * TEST_BUF_DURATION - TEST_BUF_DURATION / 2, TEST_BUF_DURATION / 2);
1248 1249 1250 1251 1252 1253 1254 1255 1256

  gst_buffer_unref (gst_harness_pull (h));

  gst_harness_teardown (h);
}

GST_END_TEST;


1257 1258 1259
static void
gst_test_clock_set_time_and_process (GstTestClock * testclock,
    GstClockTime time)
1260 1261
{
  GstClockID id, tid;
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
  gst_test_clock_wait_for_next_pending_id (testclock, &id);
  gst_test_clock_set_time (testclock, time);
  tid = gst_test_clock_process_next_clock_id (testclock);
  g_assert (tid == id);
  gst_clock_id_unref (tid);
  gst_clock_id_unref (id);
}

GST_START_TEST (test_rtx_expected_next)
{
  GstHarness *h = gst_harness_new ("rtpjitterbuffer");
1273 1274 1275 1276 1277
  gint latency_ms = 200;
  guint next_seqnum;
  GstClockTime timeout;
  gint rtx_delay_ms;
  const GstClockTime rtx_retry_timeout_ms = 40;
1278

1279 1280 1281
  g_object_set (h->element, "do-lost", TRUE, NULL);
  g_object_set (h->element, "do-retransmission", TRUE, NULL);
  g_object_set (h->element, "rtx-retry-period", 120, NULL);
1282
  next_seqnum = construct_deterministic_initial_state (h, latency_ms);
1283

1284 1285 1286 1287 1288
  /* At this point there is already existing a rtx-timer for @next_seqnum,
   * that will have a timeout of the expected arrival-time for that seqnum,
   * and a delay equal to 2*jitter==0 and 0.5*packet_spacing==10ms */
  timeout = next_seqnum * TEST_BUF_DURATION;
  rtx_delay_ms = 0.5 * TEST_BUF_MS;
1289

1290 1291 1292
  /* We crank the clock to time-out the next scheduled timer */
  gst_harness_crank_single_clock_wait (h);
  verify_rtx_event (h, next_seqnum, timeout, rtx_delay_ms, TEST_BUF_DURATION);
1293

1294 1295 1296 1297 1298
  /* now we wait for the next timeout, all following timeouts 40ms in the
   * future because this is rtx-retry-timeout */
  rtx_delay_ms += rtx_retry_timeout_ms;
  gst_harness_crank_single_clock_wait (h);
  verify_rtx_event (h, next_seqnum, timeout, rtx_delay_ms, TEST_BUF_DURATION);
1299

1300 1301 1302 1303
  /* And a third time... */
  rtx_delay_ms += rtx_retry_timeout_ms;
  gst_harness_crank_single_clock_wait (h);
  verify_rtx_event (h, next_seqnum, timeout, rtx_delay_ms, TEST_BUF_DURATION);
1304

1305 1306 1307
  /* we should now receive a packet-lost-event for packet @next_seqnum */
  gst_harness_crank_single_clock_wait (h);
  verify_lost_event (h, next_seqnum, timeout, TEST_BUF_DURATION);
1308

1309
  gst_harness_teardown (h);
1310 1311 1312 1313
}

GST_END_TEST;

1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377