static void
do_two_buffers_test_apply (gboolean end_contiguous)
{
  GstBuffer *buffer_in, *buffer_out;
  GList *node;

  g_object_set (element, "ntp-offset", NTP_OFFSET, "cseq", 0x12345678,
      "set-e-bit", TRUE, NULL);

  ASSERT_SET_STATE (element, GST_STATE_PLAYING, GST_STATE_CHANGE_SUCCESS);

  buffer_in = create_rtp_buffer (TIMESTAMP, FALSE);
  buffer_out = create_extension_buffer (buffer_in, FALSE, end_contiguous,
      FALSE, NTP_OFFSET, CSEQ);

  /* push initial events */
  gst_check_setup_events (mysrcpad, element, NULL, GST_FORMAT_TIME);

  /* Push buffer */
  fail_unless_equals_int (gst_pad_push (mysrcpad, buffer_in), GST_FLOW_OK);

  /* The buffer hasn't been pushed it as the element is waiting for the next
   * buffer. */
  fail_unless_equals_int (g_list_length (buffers), 0);

  /* push an ntp-offset event to trigger a discontinuty */
  fail_unless (gst_pad_push_event (mysrcpad,
          create_ntp_offset_event (NTP_OFFSET, end_contiguous)));

  /* A second buffer is pushed */
  buffer_in = create_rtp_buffer (TIMESTAMP + 1, FALSE);

  fail_unless_equals_int (gst_pad_push (mysrcpad, buffer_in), GST_FLOW_OK);

  /* The first buffer has now been pushed out */
  fail_unless_equals_int (g_list_length (buffers), 1);

  node = g_list_last (buffers);
  check_buffer_equal ((GstBuffer *) node->data, buffer_out);
  gst_buffer_unref (buffer_out);

  /* Push EOS */
  fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));

  /* The second buffer has been pushed out */
  fail_unless_equals_int (g_list_length (buffers), 2);

  /* Last buffer always has the 'E' flag */
  buffer_out = create_extension_buffer (buffer_in, FALSE, TRUE, end_contiguous,
      NTP_OFFSET, CSEQ);
  node = g_list_last (buffers);
  check_buffer_equal ((GstBuffer *) node->data, buffer_out);
  gst_buffer_unref (buffer_out);

  ASSERT_SET_STATE (element, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
}
static void
do_one_buffer_test_apply (gboolean clean_point)
{
  GstBuffer *buffer_in, *buffer_out;

  g_object_set (element, "ntp-offset", NTP_OFFSET, "cseq", 0x12345678,
      "set-e-bit", FALSE, NULL);

  ASSERT_SET_STATE (element, GST_STATE_PLAYING, GST_STATE_CHANGE_SUCCESS);

  buffer_in = create_rtp_buffer (TIMESTAMP, clean_point);
  buffer_out = create_extension_buffer (buffer_in, clean_point, FALSE, FALSE,
      NTP_OFFSET, CSEQ);

  /* push initial events */
  gst_check_setup_events (mysrcpad, element, NULL, GST_FORMAT_TIME);

  /* Push buffer */
  fail_unless_equals_int (gst_pad_push (mysrcpad, buffer_in), GST_FLOW_OK);

  check_buffer_equal ((GstBuffer *) buffers->data, buffer_out);
  gst_buffer_unref (buffer_out);

  ASSERT_SET_STATE (element, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
}
static GstBuffer *
create_rtp_buffer_with_timestamp (guint32 ssrc, guint8 payload_type,
    guint16 seqnum, guint32 timestamp)
{
  GstRTPBuffer rtpbuf = GST_RTP_BUFFER_INIT;
  GstBuffer *buf = create_rtp_buffer (ssrc, payload_type, seqnum);
  gst_rtp_buffer_map (buf, GST_MAP_WRITE, &rtpbuf);
  gst_rtp_buffer_set_timestamp (&rtpbuf, timestamp);
  gst_rtp_buffer_unmap (&rtpbuf);
  return buf;
}
static void
do_ntp_time (GstClockTime buffer_time, gint segment_start, gint segment_base)
{
  GstSegment segment;
  GstBuffer *buffer;
  GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
  guint8 *data;
  guint64 expected_ntp_time;
  guint64 timestamp;

  /* create a segment that controls the behavior
   * by changing segment.start and segment.base we affect the stream time and
   * running time respectively */
  gst_segment_init (&segment, GST_FORMAT_TIME);
  segment.start = segment_start;
  segment.base = segment_base;
  gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment));

  expected_ntp_time = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME,
      buffer_time);
    expected_ntp_time += NTP_OFFSET;
  expected_ntp_time = gst_util_uint64_scale (expected_ntp_time,
      (G_GINT64_CONSTANT (1) << 32), GST_SECOND);

  buffer = create_rtp_buffer (buffer_time, FALSE);
  fail_unless_equals_int (gst_pad_push (mysrcpad, buffer), GST_FLOW_OK);
  fail_unless_equals_int (g_list_length (buffers), 1);

  buffer = g_list_last (buffers)->data;

  /* get the extension header */
  fail_unless (gst_rtp_buffer_map (buffer, GST_MAP_READWRITE, &rtpbuffer));
  fail_unless (gst_rtp_buffer_get_extension_data (&rtpbuffer, NULL,
          (gpointer) & data, NULL));

  /* ...and read the NTP timestamp and verify that it's the expected one */
  timestamp = GST_READ_UINT64_BE (data);
  fail_unless_equals_uint64 (timestamp, expected_ntp_time);

  gst_rtp_buffer_unmap (&rtpbuffer);
  gst_check_drop_buffers ();
}