GST_START_TEST (test_audiointerleave_2ch_smallbuf_pexip)
{
  GstElement *audiointerleave;
  GstHarness *hsrc;
  GstHarness *h;
  GstHarness *h2;
  GstBuffer *buffer;
  GstQuery *q;
  gint i;
  GstEvent *ev;
  GstCaps *ecaps, *caps;

  audiointerleave = gst_element_factory_make ("audiointerleave", NULL);

  g_object_set (audiointerleave, "latency", GST_SECOND / 2,
      "output-buffer-duration", GST_SECOND / 4, NULL);

  h = gst_harness_new_with_element (audiointerleave, "sink_0", "src");
  gst_harness_use_testclock (h);

  h2 = gst_harness_new_with_element (audiointerleave, "sink_1", NULL);
  gst_harness_set_src_caps_str (h2, "audio/x-raw, "
      "format=" GST_AUDIO_NE (F32) ", channels=(int)1,"
      " layout=interleaved, rate=48000, channel-mask=(bitmask)8");

  hsrc = gst_harness_new ("fakesrc");
  gst_harness_use_testclock (hsrc);
  g_object_set (hsrc->element,
      "is-live", TRUE,
      "sync", TRUE,
      "signal-handoffs", TRUE,
      "format", GST_FORMAT_TIME,
      "sizetype", 2,
      "sizemax", (int) 480 * sizeof (gfloat),
      "datarate", (int) 48000 * sizeof (gfloat), NULL);
  g_signal_connect (hsrc->element, "handoff",
      G_CALLBACK (src_handoff_float32_audiointerleaved), GINT_TO_POINTER (2));
  gst_harness_play (hsrc);

  gst_harness_crank_single_clock_wait (hsrc);
  gst_harness_push_event (h, gst_harness_pull_event (hsrc)); // stream-start
  gst_harness_push_event (h, gst_harness_pull_event (hsrc)); // caps
  gst_harness_push_event (h, gst_harness_pull_event (hsrc)); // segment
  gst_harness_push (h, gst_harness_pull (hsrc)); // buffer

  for (i = 0; i < 24; i++) {
    gst_harness_crank_single_clock_wait (hsrc);
    gst_harness_push_event (h, gst_harness_pull_event (hsrc)); // caps
    gst_harness_push (h, gst_harness_pull (hsrc)); // buffer
  }

  gst_harness_crank_single_clock_wait (h);

  gst_event_unref (gst_harness_pull_event (h)); // stream-start
  ev = gst_harness_pull_event (h); // caps
  fail_unless_equals_int (GST_EVENT_CAPS, GST_EVENT_TYPE (ev));

  caps = gst_caps_new_simple ("audio/x-raw",
      "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
      "channels", G_TYPE_INT, 2,
      "layout", G_TYPE_STRING, "interleaved",
      "rate", G_TYPE_INT, 48000,
      "channel-mask", GST_TYPE_BITMASK, 0x9, NULL);

  gst_event_parse_caps (ev, &ecaps);
  gst_check_caps_equal (ecaps, caps);
  gst_caps_unref (caps);
  gst_event_unref (ev);

  for (i = 0; i < 24; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 750 * GST_MSECOND);

  /*  Check that the queue is really empty */
  q = gst_query_new_drain ();
  gst_pad_peer_query (h->srcpad, q);
  gst_query_unref (q);

  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);
  fail_unless_equals_int (gst_harness_buffers_received (h), 1);

  for (i = 0; i < 50; i++) {
    gst_harness_crank_single_clock_wait (hsrc);
    gst_harness_push_event (h, gst_harness_pull_event (hsrc)); // caps
    gst_harness_push (h, gst_harness_pull (hsrc)); // buffer
  }
  for (i = 0; i < 25; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 1000 * GST_MSECOND);
  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);
  fail_unless_equals_int (gst_harness_buffers_received (h), 2);

  for (i = 0; i < 25; i++) {
    gst_harness_crank_single_clock_wait (hsrc);
    gst_harness_push_event (h, gst_harness_pull_event (hsrc)); // caps
    gst_harness_push (h, gst_harness_pull (hsrc)); // buffer
  }
  for (i = 0; i < 25; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 1250 * GST_MSECOND);
  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);
  fail_unless_equals_int (gst_harness_buffers_received (h), 3);

  gst_harness_push_event (h, gst_event_new_eos ());

  for (i = 0; i < 25; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 1500 * GST_MSECOND);
  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);

  fail_unless_equals_int (gst_harness_buffers_received (h), 4);

  gst_harness_teardown (h2);
  gst_harness_teardown (h);
  gst_harness_teardown (hsrc);
  gst_object_unref (audiointerleave);

}
GST_END_TEST;

GST_START_TEST (test_audiointerleave_2ch_smallbuf_pexip_no_forwarding)
{
  GstElement *audiointerleave;
  GstHarness *h;
  GstHarness *h2;
  GstBuffer *buffer;
  GstQuery *q;
  gint i;
  GstEvent *ev;
  GstCaps *ecaps, *caps;

  audiointerleave = gst_element_factory_make ("audiointerleave", NULL);

  g_object_set (audiointerleave, "latency", GST_SECOND / 2,
      "output-buffer-duration", GST_SECOND / 4, NULL);

  h = gst_harness_new_with_element (audiointerleave, "sink_0", "src");
  gst_harness_use_testclock (h);
  gst_harness_set_forwarding (h, FALSE);

  h2 = gst_harness_new_with_element (audiointerleave, "sink_1", NULL);
  gst_harness_set_src_caps_str (h2, "audio/x-raw, "
      "format=" GST_AUDIO_NE (F32) ", channels=(int)1,"
      " layout=interleaved, rate=48000, channel-mask=(bitmask)8");

  gst_harness_add_src (h, "fakesrc", TRUE);
  g_object_set (h->src_harness->element,
      "is-live", TRUE,
      "sync", TRUE,
      "signal-handoffs", TRUE,
      "format", GST_FORMAT_TIME,
      "sizetype", 2,
      "sizemax", (int) 480 * sizeof (gfloat),
      "datarate", (int) 48000 * sizeof (gfloat), NULL);
  g_signal_connect (h->src_harness->element, "handoff",
      G_CALLBACK (src_handoff_float32_audiointerleaved), GINT_TO_POINTER (2));
  gst_harness_play (h->src_harness);

  gst_harness_crank_single_clock_wait (h->src_harness);
  gst_harness_src_push_event (h); // stream-start
  gst_harness_src_push_event (h); // caps
  gst_harness_src_push_event (h); // segment
  gst_harness_src_crank_and_push_many (h, 0, 1); // buffer

  for (i = 0; i < 24; i++) {
    gst_harness_crank_single_clock_wait (h->src_harness);
    gst_harness_src_push_event (h); // caps
    gst_harness_src_crank_and_push_many (h, 0, 1); // buffer
  }

  gst_harness_crank_single_clock_wait (h);

  gst_event_unref (gst_harness_pull_event (h)); // stream-start
  ev = gst_harness_pull_event (h); // caps
  fail_unless_equals_int (GST_EVENT_CAPS, GST_EVENT_TYPE (ev));

  caps = gst_caps_new_simple ("audio/x-raw",
      "format", G_TYPE_STRING, GST_AUDIO_NE (F32),
      "channels", G_TYPE_INT, 2,
      "layout", G_TYPE_STRING, "interleaved",
      "rate", G_TYPE_INT, 48000,
      "channel-mask", GST_TYPE_BITMASK, 0x9, NULL);

  gst_event_parse_caps (ev, &ecaps);
  gst_check_caps_equal (ecaps, caps);
  gst_caps_unref (caps);
  gst_event_unref (ev);

  for (i = 0; i < 24; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 750 * GST_MSECOND);

  /*  Check that the queue is really empty */
  q = gst_query_new_drain ();
  gst_pad_peer_query (h->srcpad, q);
  gst_query_unref (q);

  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);
  fail_unless_equals_int (gst_harness_buffers_received (h), 1);

  for (i = 0; i < 50; i++) {
    gst_harness_crank_single_clock_wait (h->src_harness);
    gst_harness_src_push_event (h); // caps
    gst_harness_src_crank_and_push_many (h, 0, 1); // buffer
  }
  for (i = 0; i < 25; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 1000 * GST_MSECOND);
  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);
  fail_unless_equals_int (gst_harness_buffers_received (h), 2);

  for (i = 0; i < 25; i++) {
    gst_harness_crank_single_clock_wait (h->src_harness);
    gst_harness_src_push_event (h); // caps
    gst_harness_src_crank_and_push_many (h, 0, 1); // buffer
  }
  for (i = 0; i < 25; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 1250 * GST_MSECOND);
  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);
  fail_unless_equals_int (gst_harness_buffers_received (h), 3);

  gst_harness_push_event (h, gst_event_new_eos ());

  for (i = 0; i < 25; i++)
    gst_harness_crank_single_clock_wait (h);
  fail_unless_equals_uint64 (gst_clock_get_time (GST_ELEMENT_CLOCK
          (h->element)), 1500 * GST_MSECOND);
  buffer = gst_harness_pull (h);
  sink_handoff_float32 (NULL, buffer, NULL, GUINT_TO_POINTER (3));
  gst_buffer_unref (buffer);

  fail_unless_equals_int (gst_harness_buffers_received (h), 4);

  gst_harness_teardown (h2);
  gst_harness_teardown (h);
  gst_object_unref (audiointerleave);

}
GST_END_TEST;
