From ca1e66a64815b7d5de854f646627082b8b65c3c7 Mon Sep 17 00:00:00 2001 From: Havard Graff Date: Tue, 15 Jul 2014 11:04:47 +0200 Subject: [PATCH] gstrtpjitterbuffer: fix gap-time calculation and remove "late" The amount of time that is completely expired and not worth waiting for, is the duration of the packets in the gap (gap * duration) - the latency (size) of the jitterbuffer (priv->latency_ns). This is the duration that we make a "multi-lost" packet for. The "late" concept made some sense in 0.10 as it reflected that a buffer coming in had not been waited for at all, but had a timestamp that was outside the jitterbuffer to wait for. With the rewrite of the waiting (timeout) mechanism in 1.0, this no longer makes any sense, and the variable no longer reflects anything meaningful (num > 0 is useless, the duration is what matters) Fixed up the tests that had been slightly modified in 1.0 to allow faulty behavior to sneak in, and port some of them to use GstHarness. --- gst/rtpmanager/gstrtpjitterbuffer.c | 34 +- tests/check/elements/rtpjitterbuffer.c | 500 +++++++++++++------------ 2 files changed, 279 insertions(+), 255 deletions(-) diff --git a/gst/rtpmanager/gstrtpjitterbuffer.c b/gst/rtpmanager/gstrtpjitterbuffer.c index 485f2e6fe9..037ca883df 100644 --- a/gst/rtpmanager/gstrtpjitterbuffer.c +++ b/gst/rtpmanager/gstrtpjitterbuffer.c @@ -1961,29 +1961,35 @@ calculate_expected (GstRtpJitterBuffer * jitterbuffer, guint32 expected, GstClockTime gap_time; guint lost_packets; - gap_time = total_duration - priv->latency_ns; - if (duration > 0) { + GstClockTime gap_dur = gap * duration; + if (gap_dur > priv->latency_ns) + gap_time = gap_dur - priv->latency_ns; + else + gap_time = 0; lost_packets = gap_time / duration; - gap_time = lost_packets * duration; } else { + gap_time = total_duration - priv->latency_ns; lost_packets = gap; } /* too many lost packets, some of the missing packets are already * too late and we can generate lost packet events for them. */ - GST_DEBUG_OBJECT (jitterbuffer, "too many lost packets %" GST_TIME_FORMAT - " > %" GST_TIME_FORMAT ", consider %u lost", - GST_TIME_ARGS (total_duration), GST_TIME_ARGS (priv->latency_ns), - lost_packets); + GST_DEBUG_OBJECT (jitterbuffer, + "lost packets (%d, #%d->#%d) duration too large %" GST_TIME_FORMAT + " > %" GST_TIME_FORMAT ", consider %u lost (%" GST_TIME_FORMAT ")", + gap, expected, seqnum, GST_TIME_ARGS (total_duration), + GST_TIME_ARGS (priv->latency_ns), lost_packets, + GST_TIME_ARGS(gap_time)); /* this timer will fire immediately and the lost event will be pushed from * the timer thread */ - add_timer (jitterbuffer, TIMER_TYPE_LOST, expected, lost_packets, - priv->last_in_dts + duration, 0, gap_time); - - expected += lost_packets; - priv->last_in_dts += gap_time; + if (lost_packets > 0) { + add_timer (jitterbuffer, TIMER_TYPE_LOST, expected, lost_packets, + priv->last_in_dts + duration, 0, gap_time); + expected += lost_packets; + priv->last_in_dts += gap_time; + } } expected_dts = priv->last_in_dts + duration; @@ -2759,7 +2765,7 @@ do_lost_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer, GstRtpJitterBufferPrivate *priv = jitterbuffer->priv; GstClockTime duration, timestamp; guint seqnum, lost_packets, num_rtx_retry, next_in_seqnum; - gboolean late, head; + gboolean head; GstEvent *event; RTPJitterBufferItem *item; @@ -2769,7 +2775,6 @@ do_lost_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer, if (duration == GST_CLOCK_TIME_NONE && priv->packet_spacing > 0) duration = priv->packet_spacing; lost_packets = MAX (timer->num, 1); - late = timer->num > 0; num_rtx_retry = timer->num_rtx_retry; /* we had a gap and thus we lost some packets. Create an event for this. */ @@ -2794,7 +2799,6 @@ do_lost_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer, "seqnum", G_TYPE_UINT, (guint) seqnum, "timestamp", G_TYPE_UINT64, timestamp, "duration", G_TYPE_UINT64, duration, - "late", G_TYPE_BOOLEAN, late, "retry", G_TYPE_UINT, num_rtx_retry, NULL)); item = alloc_item (event, ITEM_TYPE_LOST, -1, -1, seqnum, lost_packets, -1); diff --git a/tests/check/elements/rtpjitterbuffer.c b/tests/check/elements/rtpjitterbuffer.c index 31999ef62f..d1161dac07 100644 --- a/tests/check/elements/rtpjitterbuffer.c +++ b/tests/check/elements/rtpjitterbuffer.c @@ -24,6 +24,7 @@ #include #include +#include #include @@ -374,10 +375,13 @@ GST_START_TEST (test_clear_pt_map) } GST_END_TEST; -static const guint payload_size = 160; -static const guint clock_rate = 8000; -static const guint pcmu_payload_type = 0; -static const guint test_ssrc = 0x01BADBAD; + +static const guint PCMU_BUF_SIZE = 160; +static const guint PCMU_BUF_CLOCK_RATE = 8000; +static const guint PCMU_BUF_PT = 0; +static const guint PCMU_BUF_SSRC = 0x01BADBAD; +static const guint PCMU_BUF_MS = 20; +static const GstClockTime PCMU_BUF_DURATION = 20 * GST_MSECOND; typedef struct { @@ -396,10 +400,10 @@ generate_caps (void) { return gst_caps_new_simple ("application/x-rtp", "media", G_TYPE_STRING, "audio", - "clock-rate", G_TYPE_INT, clock_rate, + "clock-rate", G_TYPE_INT, PCMU_BUF_CLOCK_RATE, "encoding-name", G_TYPE_STRING, "PCMU", - "payload", G_TYPE_INT, pcmu_payload_type, - "ssrc", G_TYPE_UINT, test_ssrc, NULL); + "payload", G_TYPE_INT, PCMU_BUF_PT, + "ssrc", G_TYPE_UINT, PCMU_BUF_SSRC, NULL); } static GstBuffer * @@ -411,19 +415,19 @@ generate_test_buffer (GstClockTime gst_ts, guint i; GstRTPBuffer rtp = GST_RTP_BUFFER_INIT; - buf = gst_rtp_buffer_new_allocate (payload_size, 0, 0); + buf = gst_rtp_buffer_new_allocate (PCMU_BUF_SIZE, 0, 0); GST_BUFFER_DTS (buf) = gst_ts; GST_BUFFER_PTS (buf) = gst_ts; gst_rtp_buffer_map (buf, GST_MAP_READWRITE, &rtp); - gst_rtp_buffer_set_payload_type (&rtp, pcmu_payload_type); + gst_rtp_buffer_set_payload_type (&rtp, PCMU_BUF_PT); gst_rtp_buffer_set_marker (&rtp, marker_bit); gst_rtp_buffer_set_seq (&rtp, seq_num); gst_rtp_buffer_set_timestamp (&rtp, rtp_ts); - gst_rtp_buffer_set_ssrc (&rtp, test_ssrc); + gst_rtp_buffer_set_ssrc (&rtp, PCMU_BUF_SSRC); payload = gst_rtp_buffer_get_payload (&rtp); - for (i = 0; i < payload_size; i++) + for (i = 0; i < PCMU_BUF_SIZE; i++) payload[i] = 0xff; gst_rtp_buffer_unmap (&rtp); @@ -578,15 +582,13 @@ destroy_testharness (TestData * data) static void verify_lost_event (GstEvent * event, guint32 expected_seqnum, - GstClockTime expected_timestamp, GstClockTime expected_duration, - gboolean expected_late) + GstClockTime expected_timestamp, GstClockTime expected_duration) { const GstStructure *s = gst_event_get_structure (event); const GValue *value; guint32 seqnum; GstClockTime timestamp; GstClockTime duration; - gboolean late; g_assert (gst_structure_get_uint (s, "seqnum", &seqnum)); @@ -598,12 +600,9 @@ verify_lost_event (GstEvent * event, guint32 expected_seqnum, g_assert (value && G_VALUE_HOLDS_UINT64 (value)); duration = g_value_get_uint64 (value); - g_assert (gst_structure_get_boolean (s, "late", &late)); - - g_assert_cmpint (seqnum, ==, expected_seqnum); - g_assert_cmpint (timestamp, ==, expected_timestamp); - g_assert_cmpint (duration, ==, expected_duration); - g_assert (late == expected_late); + fail_unless_equals_int (seqnum, expected_seqnum); + fail_unless_equals_int (timestamp, expected_timestamp); + fail_unless_equals_int (duration, expected_duration); gst_event_unref (event); } @@ -643,86 +642,78 @@ verify_rtx_event (GstEvent * event, guint32 expected_seqnum, GST_START_TEST (test_only_one_lost_event_on_large_gaps) { - TestData data; + GstHarness * h = gst_harness_new ("rtpjitterbuffer"); + GstTestClock * testclock; GstClockID id, test_id; - GstBuffer *in_buf, *out_buf; + GstBuffer *out_buf; GstEvent *out_event; gint jb_latency_ms = 200; - guint buffer_size_ms = (payload_size * 1000) / clock_rate; + gint num_lost_events = jb_latency_ms / PCMU_BUF_MS; GstRTPBuffer rtp = GST_RTP_BUFFER_INIT; - setup_testharness (&data); + gst_harness_set_src_caps (h, generate_caps ()); + gst_harness_use_testclock (h); + testclock = gst_harness_get_testclock (h); + g_object_set (h->element, + "do-lost", TRUE, + "latency", jb_latency_ms, + NULL); - g_object_set (data.jitter_buffer, "latency", jb_latency_ms, NULL); /* push the first buffer in */ - in_buf = generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0); - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), 0); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); + gst_harness_push (h, generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0)); /* wait for the first buffer to be synced to timestamp + latency */ - gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (data.clock), &id); + gst_test_clock_wait_for_next_pending_id (testclock, &id); /* increase the time to timestamp + latency and release the wait */ - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), - jb_latency_ms * GST_MSECOND); - test_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (data.clock)); - g_assert (test_id == id); + gst_test_clock_set_time (testclock, jb_latency_ms * GST_MSECOND); + test_id = gst_test_clock_process_next_clock_id (testclock); + fail_unless (id == test_id); gst_clock_id_unref (test_id); gst_clock_id_unref (id); /* check for the buffer coming out that was pushed in */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert_cmpint (GST_BUFFER_DTS (out_buf), ==, 0); g_assert_cmpint (GST_BUFFER_PTS (out_buf), ==, 0); gst_buffer_unref (out_buf); - /* move time ahead 10 seconds */ - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), 10 * GST_SECOND); + /* move time ahead to just before 10 seconds */ + gst_test_clock_set_time (testclock, 10 * GST_SECOND - 1); - /* wait a bit */ - g_usleep (G_USEC_PER_SEC / 10); - - /* check that no buffers have been pushed out and no pending waits */ - g_assert_cmpint (g_async_queue_length (data.buf_queue), ==, 0); - g_assert (gst_test_clock_peek_next_pending_id (GST_TEST_CLOCK (data.clock), - &id) == FALSE); + /* check that we have no pending waits */ + g_assert (gst_test_clock_peek_next_pending_id (testclock, &id) == FALSE); /* a buffer now arrives perfectly on time */ - in_buf = generate_test_buffer (10 * GST_SECOND, FALSE, 500, 500 * 160); - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), 10 * GST_SECOND); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); + gst_harness_push (h, generate_test_buffer (10 * GST_SECOND, FALSE, 500, 500 * PCMU_BUF_SIZE)); - /* release the wait */ - gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (data.clock), &id); - gst_test_clock_advance_time (GST_TEST_CLOCK (data.clock), GST_MSECOND * 20); - test_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (data.clock)); - g_assert (id == test_id); + /* release the wait (can't use crank, since that sets the time again) */ + gst_test_clock_wait_for_next_pending_id (testclock, &id); + test_id = gst_test_clock_process_next_clock_id (testclock); + fail_unless (id == test_id); gst_clock_id_unref (test_id); gst_clock_id_unref (id); + /* drop GstEventStreamStart & GstEventCaps & GstEventSegment */ + for (int i = 0; i < 3; i++) + gst_event_unref (gst_harness_pull_event (h)); + /* we should now receive a packet-lost-event for buffers 1 through 489 */ - out_event = g_async_queue_pop (data.sink_event_queue); + out_event = gst_harness_pull_event (h); g_assert (out_event != NULL); - g_assert_cmpint (data.lost_event_count, ==, 1); - verify_lost_event (out_event, 1, 1 * GST_MSECOND * 20, GST_MSECOND * 20 * 490, - TRUE); + verify_lost_event (out_event, 1, 1 * PCMU_BUF_DURATION, PCMU_BUF_DURATION * 489); - /* churn through sync_times until the new buffer gets pushed out */ - while (g_async_queue_length (data.buf_queue) < 1) { - if (gst_test_clock_peek_next_pending_id (GST_TEST_CLOCK (data.clock), &id)) { - GstClockTime t = gst_clock_id_get_time (id); - if (t > gst_clock_get_time (data.clock)) { - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), t); - } - test_id = - gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (data.clock)); - gst_clock_id_unref (test_id); - gst_clock_id_unref (id); - } + /* we get as many lost events as the the number of buffers the jitterbuffer is able to wait for */ + for (int i = 0; i < num_lost_events; i++) { + gst_harness_crank_single_clock_wait (h); + out_event = gst_harness_pull_event (h); + g_assert (out_event != NULL); + verify_lost_event (out_event, 490 + i, (490 + i) * PCMU_BUF_DURATION, PCMU_BUF_DURATION); } - out_buf = g_async_queue_pop (data.buf_queue); + /* and then the buffer is released */ + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert (GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT)); gst_rtp_buffer_map (out_buf, GST_MAP_READ, &rtp); @@ -732,95 +723,79 @@ GST_START_TEST (test_only_one_lost_event_on_large_gaps) g_assert_cmpint (GST_BUFFER_PTS (out_buf), ==, (10 * GST_SECOND)); gst_buffer_unref (out_buf); - /* we get as many lost events as the the number of buffers the jitterbuffer - * is able to wait for (+ the one we already got) */ - g_assert_cmpint (data.lost_event_count, ==, jb_latency_ms / buffer_size_ms); - - destroy_testharness (&data); + gst_object_unref (testclock); + gst_harness_teardown (h); } - GST_END_TEST; GST_START_TEST (test_two_lost_one_arrives_in_time) { - TestData data; - GstClockID id, test_id; - GstBuffer *in_buf, *out_buf; + GstHarness * h = gst_harness_new ("rtpjitterbuffer"); + GstTestClock * testclock; + GstClockID id; + GstBuffer *out_buf; GstEvent *out_event; - gint jb_latency_ms = 100; - GstClockTime buffer_time, now; - gint b; + gint jb_latency_ms = 100; /* FIXME: setting this to 10 produces a strange result (30ms lost event), find out why! */ GstRTPBuffer rtp = GST_RTP_BUFFER_INIT; + GstClockTime buffer_time; + gint b; - setup_testharness (&data); - - g_object_set (data.jitter_buffer, "latency", jb_latency_ms, NULL); + gst_harness_set_src_caps (h, generate_caps ()); + gst_harness_use_testclock (h); + testclock = gst_harness_get_testclock (h); + g_object_set (h->element, + "do-lost", TRUE, + "latency", jb_latency_ms, + NULL); - /* push the first buffer in */ - in_buf = generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0); - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), 0); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (data.clock), &id); - now = jb_latency_ms * GST_MSECOND; - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), now); - test_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (data.clock)); - g_assert (test_id == id); - gst_clock_id_unref (test_id); - gst_clock_id_unref (id); - out_buf = g_async_queue_pop (data.buf_queue); - g_assert (out_buf != NULL); + /* push the first buffer through */ + gst_harness_push (h, generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0)); + gst_harness_crank_single_clock_wait (h); + gst_buffer_unref (gst_harness_pull (h)); /* push some buffers arriving in perfect time! */ for (b = 1; b < 3; b++) { - buffer_time = b * GST_MSECOND * 20; - in_buf = generate_test_buffer (buffer_time, TRUE, b, b * 160); - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), now + buffer_time); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - gst_buffer_unref (out_buf); + buffer_time = b * PCMU_BUF_DURATION; + gst_harness_push (h, generate_test_buffer (buffer_time, TRUE, b, b * PCMU_BUF_SIZE)); /* check for the buffer coming out that was pushed in */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert_cmpint (GST_BUFFER_DTS (out_buf), ==, buffer_time); g_assert_cmpint (GST_BUFFER_PTS (out_buf), ==, buffer_time); + gst_buffer_unref (out_buf); } - gst_buffer_unref (out_buf); /* hop over 2 packets and make another one (gap of 2) */ b = 5; - buffer_time = b * GST_MSECOND * 20; - in_buf = generate_test_buffer (buffer_time, TRUE, b, b * 160); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); + buffer_time = b * PCMU_BUF_DURATION; + gst_harness_push (h, generate_test_buffer (buffer_time, TRUE, b, b * PCMU_BUF_SIZE)); /* verify that the jitterbuffer now wait for the latest moment it can push */ - /* the first lost buffer (buffer 3) out on (buffer-timestamp (60) + latency (10) = 70) */ - gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (data.clock), &id); - g_assert_cmpint (gst_clock_id_get_time (id), ==, - (3 * GST_MSECOND * 20) + (jb_latency_ms * GST_MSECOND)); + /* the first lost buffer (buffer 3) out on (buffer-timestamp (60) + latency (100) = 160) */ + gst_test_clock_wait_for_next_pending_id (testclock, &id); + g_assert_cmpint (gst_clock_id_get_time (id), ==, (3 * PCMU_BUF_DURATION) + (jb_latency_ms * GST_MSECOND)); + gst_clock_id_unref (id); /* let the time expire... */ - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), - gst_clock_id_get_time (id)); - test_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (data.clock)); - g_assert (test_id == id); - gst_clock_id_unref (test_id); - gst_clock_id_unref (id); + gst_harness_crank_single_clock_wait (h); + + /* drop GstEventStreamStart & GstEventCaps & GstEventSegment */ + for (int i = 0; i < 3; i++) + gst_event_unref (gst_harness_pull_event (h)); /* we should now receive a packet-lost-event for buffer 3 */ - out_event = g_async_queue_pop (data.sink_event_queue); + out_event = gst_harness_pull_event (h); g_assert (out_event != NULL); - g_assert_cmpint (data.lost_event_count, ==, 1); - verify_lost_event (out_event, 3, 3 * GST_MSECOND * 20, GST_MSECOND * 20, - FALSE); + verify_lost_event (out_event, 3, 3 * PCMU_BUF_DURATION, PCMU_BUF_DURATION); /* buffer 4 now arrives just in time (time is 70, buffer 4 expires at 90) */ b = 4; - buffer_time = b * GST_MSECOND * 20; - in_buf = generate_test_buffer (buffer_time, TRUE, b, b * 160); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); + buffer_time = b * PCMU_BUF_DURATION; + gst_harness_push (h, generate_test_buffer (buffer_time, TRUE, b, b * PCMU_BUF_SIZE)); /* verify that buffer 4 made it through! */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert (GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT)); gst_rtp_buffer_map (out_buf, GST_MAP_READ, &rtp); @@ -829,7 +804,7 @@ GST_START_TEST (test_two_lost_one_arrives_in_time) gst_buffer_unref (out_buf); /* and see that buffer 5 now arrives in a normal fashion */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert (!GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT)); gst_rtp_buffer_map (out_buf, GST_MAP_READ, &rtp); @@ -837,73 +812,75 @@ GST_START_TEST (test_two_lost_one_arrives_in_time) gst_rtp_buffer_unmap (&rtp); gst_buffer_unref (out_buf); - /* should still have only seen 1 packet lost event */ - g_assert_cmpint (data.lost_event_count, ==, 1); - - destroy_testharness (&data); + gst_object_unref (testclock); + gst_harness_teardown (h); } - GST_END_TEST; GST_START_TEST (test_late_packets_still_makes_lost_events) { - TestData data; + GstHarness * h = gst_harness_new ("rtpjitterbuffer"); + GstTestClock * testclock; GstClockID id, test_id; - GstBuffer *in_buf, *out_buf; + GstBuffer *out_buf; GstEvent *out_event; - gint jb_latency_ms = 10; + gint jb_latency_ms = 100; + GstRTPBuffer rtp = GST_RTP_BUFFER_INIT; GstClockTime buffer_time; gint b; - GstRTPBuffer rtp = GST_RTP_BUFFER_INIT; - - setup_testharness (&data); - - g_object_set (data.jitter_buffer, "latency", jb_latency_ms, NULL); - - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), 10 * GST_SECOND); - - /* push the first buffer in */ - in_buf = generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (data.clock), &id); - test_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (data.clock)); + gst_harness_set_src_caps (h, generate_caps ()); + gst_harness_use_testclock (h); + testclock = gst_harness_get_testclock (h); + g_object_set (h->element, + "do-lost", TRUE, + "latency", jb_latency_ms, + NULL); + + /* advance the clock with 10 seconds */ + gst_test_clock_set_time (testclock, 10 * GST_SECOND); + + /* push the first buffer through */ + gst_harness_push (h, generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0)); + gst_test_clock_wait_for_next_pending_id (testclock, &id); + test_id = gst_test_clock_process_next_clock_id (testclock); g_assert (test_id == id); gst_clock_id_unref (id); gst_clock_id_unref (test_id); - out_buf = g_async_queue_pop (data.buf_queue); - g_assert (out_buf != NULL); + gst_buffer_unref (gst_harness_pull (h)); - /* push some buffers in! */ + /* push some buffers arriving in perfect time! */ for (b = 1; b < 3; b++) { - buffer_time = b * GST_MSECOND * 20; - in_buf = generate_test_buffer (buffer_time, TRUE, b, b * 160); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - gst_buffer_unref (out_buf); + buffer_time = b * PCMU_BUF_DURATION; + gst_harness_push (h, generate_test_buffer (buffer_time, TRUE, b, b * PCMU_BUF_SIZE)); /* check for the buffer coming out that was pushed in */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert_cmpint (GST_BUFFER_DTS (out_buf), ==, buffer_time); g_assert_cmpint (GST_BUFFER_PTS (out_buf), ==, buffer_time); + gst_buffer_unref (out_buf); } - gst_buffer_unref (out_buf); /* hop over 2 packets and make another one (gap of 2) */ b = 5; - buffer_time = b * GST_MSECOND * 20; - in_buf = generate_test_buffer (buffer_time, TRUE, b, b * 160); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); + buffer_time = b * PCMU_BUF_DURATION; + gst_harness_push (h, generate_test_buffer (buffer_time, TRUE, b, b * PCMU_BUF_SIZE)); - /* we should now receive a packet-lost-event for buffer 3 and 4 */ - out_event = g_async_queue_pop (data.sink_event_queue); + /* drop GstEventStreamStart & GstEventCaps & GstEventSegment */ + for (int i = 0; i < 3; i++) + gst_event_unref (gst_harness_pull_event (h)); + + /* we should now receive packet-lost-events for buffer 3 and 4*/ + out_event = gst_harness_pull_event (h); g_assert (out_event != NULL); - g_assert_cmpint (data.lost_event_count, ==, 1); - verify_lost_event (out_event, 3, 3 * GST_MSECOND * 20, GST_MSECOND * 20 * 2, - TRUE); + verify_lost_event (out_event, 3, 3 * PCMU_BUF_DURATION, PCMU_BUF_DURATION); + out_event = gst_harness_pull_event (h); + g_assert (out_event != NULL); + verify_lost_event (out_event, 4, 4 * PCMU_BUF_DURATION, PCMU_BUF_DURATION); /* verify that buffer 5 made it through! */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert (GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT)); gst_rtp_buffer_map (out_buf, GST_MAP_READ, &rtp); @@ -911,74 +888,72 @@ GST_START_TEST (test_late_packets_still_makes_lost_events) gst_rtp_buffer_unmap (&rtp); gst_buffer_unref (out_buf); - /* should still have only seen 1 packet lost event */ - g_assert_cmpint (data.lost_event_count, ==, 1); - - destroy_testharness (&data); + gst_object_unref (testclock); + gst_harness_teardown (h); } - GST_END_TEST; GST_START_TEST (test_all_packets_are_timestamped_zero) { - TestData data; + GstHarness * h = gst_harness_new ("rtpjitterbuffer"); + GstTestClock * testclock; GstClockID id, test_id; - GstBuffer *in_buf, *out_buf; + GstBuffer *out_buf; GstEvent *out_event; - gint jb_latency_ms = 10; - gint b; + gint jb_latency_ms = 100; GstRTPBuffer rtp = GST_RTP_BUFFER_INIT; + gint b; - setup_testharness (&data); - - g_object_set (data.jitter_buffer, "latency", jb_latency_ms, NULL); - - gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), 10 * GST_SECOND); - - /* push the first buffer in */ - in_buf = generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - - gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (data.clock), &id); - test_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (data.clock)); + gst_harness_set_src_caps (h, generate_caps ()); + gst_harness_use_testclock (h); + testclock = gst_harness_get_testclock (h); + g_object_set (h->element, + "do-lost", TRUE, + "latency", jb_latency_ms, + NULL); + + /* advance the clock with 10 seconds */ + gst_test_clock_set_time (testclock, 10 * GST_SECOND); + + /* push the first buffer through */ + gst_harness_push (h, generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0)); + gst_test_clock_wait_for_next_pending_id (testclock, &id); + test_id = gst_test_clock_process_next_clock_id (testclock); g_assert (test_id == id); - gst_clock_id_unref (test_id); gst_clock_id_unref (id); - out_buf = g_async_queue_pop (data.buf_queue); - g_assert (out_buf != NULL); + gst_clock_id_unref (test_id); + gst_buffer_unref (gst_harness_pull (h)); - /* push some buffers in! */ + /* push some buffers in, all timestamped 0 */ for (b = 1; b < 3; b++) { - in_buf = generate_test_buffer (0, TRUE, b, 0); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - gst_buffer_unref (out_buf); + gst_harness_push (h, generate_test_buffer (0 * GST_MSECOND, TRUE, b, 0)); /* check for the buffer coming out that was pushed in */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert_cmpint (GST_BUFFER_DTS (out_buf), ==, 0); g_assert_cmpint (GST_BUFFER_PTS (out_buf), ==, 0); + gst_buffer_unref (out_buf); } - gst_buffer_unref (out_buf); /* hop over 2 packets and make another one (gap of 2) */ b = 5; - in_buf = generate_test_buffer (0, TRUE, b, 0); - g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); + gst_harness_push (h, generate_test_buffer (0 * GST_MSECOND, TRUE, b, 0)); - /* we should now receive a packet-lost-event for buffer 3 and 4 */ - out_event = g_async_queue_pop (data.sink_event_queue); - g_assert (out_event != NULL); - verify_lost_event (out_event, 3, 0, 0, FALSE); + /* drop GstEventStreamStart & GstEventCaps & GstEventSegment */ + for (int i = 0; i < 3; i++) + gst_event_unref (gst_harness_pull_event (h)); - out_event = g_async_queue_pop (data.sink_event_queue); + /* we should now receive packet-lost-events for buffer 3 and 4 */ + out_event = gst_harness_pull_event (h); g_assert (out_event != NULL); - verify_lost_event (out_event, 4, 0, 0, FALSE); - - g_assert_cmpint (data.lost_event_count, ==, 2); + verify_lost_event (out_event, 3, 0, 0); + out_event = gst_harness_pull_event (h); + g_assert (out_event != NULL); + verify_lost_event (out_event, 4, 0, 0); /* verify that buffer 5 made it through! */ - out_buf = g_async_queue_pop (data.buf_queue); + out_buf = gst_harness_pull (h); g_assert (out_buf != NULL); g_assert (GST_BUFFER_FLAG_IS_SET (out_buf, GST_BUFFER_FLAG_DISCONT)); gst_rtp_buffer_map (out_buf, GST_MAP_READ, &rtp); @@ -986,12 +961,9 @@ GST_START_TEST (test_all_packets_are_timestamped_zero) gst_rtp_buffer_unmap (&rtp); gst_buffer_unref (out_buf); - /* should still have only seen 2 packet lost events */ - g_assert_cmpint (data.lost_event_count, ==, 2); - - destroy_testharness (&data); + gst_object_unref (testclock); + gst_harness_teardown (h); } - GST_END_TEST; GST_START_TEST (test_rtx_expected_next) @@ -1020,7 +992,7 @@ GST_START_TEST (test_rtx_expected_next) /* put second buffer, the jitterbuffer should now know that the packet spacing * is 20ms and should ask for retransmission of seqnum 2 in 20ms */ - in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 1, 160); + in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 1, PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); gst_test_clock_wait_for_next_pending_id (GST_TEST_CLOCK (data.clock), &id); @@ -1079,7 +1051,7 @@ GST_START_TEST (test_rtx_expected_next) /* we should now receive a packet-lost-event for buffer 2 */ out_event = g_async_queue_pop (data.sink_event_queue); g_assert (out_event != NULL); - verify_lost_event (out_event, 2, 40 * GST_MSECOND, 20 * GST_MSECOND, FALSE); + verify_lost_event (out_event, 2, 40 * GST_MSECOND, 20 * GST_MSECOND); destroy_testharness (&data); } @@ -1113,12 +1085,12 @@ GST_START_TEST (test_rtx_two_missing) /* put second buffer, the jitterbuffer should now know that the packet spacing * is 20ms and should ask for retransmission of seqnum 2 at 60ms */ - in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 1, 160); + in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 1, PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); /* push buffer 4, 2 and 3 are missing now, we should get retransmission events * for 3 at 100ms*/ - in_buf = generate_test_buffer (80 * GST_MSECOND, TRUE, 4, 4 * 160); + in_buf = generate_test_buffer (80 * GST_MSECOND, TRUE, 4, 4 * PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); /* wait for first retransmission request */ @@ -1155,12 +1127,12 @@ GST_START_TEST (test_rtx_two_missing) verify_rtx_event (out_event, 3, 60 * GST_MSECOND, 40, 20 * GST_MSECOND); /* make buffer 3 */ - in_buf = generate_test_buffer (60 * GST_MSECOND, TRUE, 3, 3 * 160); + in_buf = generate_test_buffer (60 * GST_MSECOND, TRUE, 3, 3 * PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); /* make more buffers */ for (i = 5; i < 15; i++) { - in_buf = generate_test_buffer (i * 20 * GST_MSECOND, TRUE, i, i * 160); + in_buf = generate_test_buffer (i * 20 * GST_MSECOND, TRUE, i, i * PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); } @@ -1205,7 +1177,7 @@ GST_START_TEST (test_rtx_two_missing) /* we should now receive a packet-lost-event for buffer 2 */ out_event = g_async_queue_pop (data.sink_event_queue); g_assert (out_event != NULL); - verify_lost_event (out_event, 2, 40 * GST_MSECOND, 20 * GST_MSECOND, FALSE); + verify_lost_event (out_event, 2, 40 * GST_MSECOND, 20 * GST_MSECOND); /* verify that buffers made it through! */ for (i = 3; i < 15; i++) { @@ -1261,7 +1233,7 @@ GST_START_TEST (test_rtx_packet_delay) /* put second buffer, the jitterbuffer should now know that the packet spacing * is 20ms and should ask for retransmission of seqnum 2 at 60ms */ - in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 1, 160); + in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 1, PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); /* push buffer 8, 2 -> 7 are missing now. note that the rtp time is the same @@ -1269,7 +1241,7 @@ GST_START_TEST (test_rtx_packet_delay) * the estimate for 2 could be refined now to 20ms. also packet 2, 3 and 4 are * exceeding the max allowed reorder distance and should request a * retransmission right away */ - in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 8, 8 * 160); + in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 8, 8 * PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); /* we should now receive retransmission requests for 2 -> 5 */ @@ -1286,7 +1258,7 @@ GST_START_TEST (test_rtx_packet_delay) g_assert_cmpint (data.rtx_event_count, ==, 3); /* push 9, this should immediately request retransmission of 5 */ - in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 9, 9 * 160); + in_buf = generate_test_buffer (20 * GST_MSECOND, TRUE, 9, 9 * PCMU_BUF_SIZE); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); /* we should now receive retransmission requests for 5 */ @@ -1358,7 +1330,7 @@ GST_START_TEST (test_rtx_packet_delay) GST_DEBUG ("popping lost event %d", i); out_event = g_async_queue_pop (data.sink_event_queue); g_assert (out_event != NULL); - verify_lost_event (out_event, i, 20 * GST_MSECOND, 0, FALSE); + verify_lost_event (out_event, i, 20 * GST_MSECOND, 0); } /* verify that buffer 8 made it through! */ @@ -1385,7 +1357,7 @@ GST_START_TEST (test_rtx_packet_delay) GST_DEBUG ("popping lost event 10"); out_event = g_async_queue_pop (data.sink_event_queue); g_assert (out_event != NULL); - verify_lost_event (out_event, 10, 40 * GST_MSECOND, 20 * GST_MSECOND, FALSE); + verify_lost_event (out_event, 10, 40 * GST_MSECOND, 20 * GST_MSECOND); /* should have seen 6 packet lost events */ g_assert_cmpint (data.lost_event_count, ==, 7); @@ -1422,7 +1394,7 @@ GST_START_TEST (test_gap_exceeds_latency) g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); timestamp_ms += 20; - rtp_ts += 160; + rtp_ts += PCMU_BUF_SIZE; gst_test_clock_set_time (GST_TEST_CLOCK (data.clock), timestamp_ms * GST_MSECOND); @@ -1462,8 +1434,8 @@ GST_START_TEST (test_gap_exceeds_latency) g_assert_cmpint (seqnum, ==, 2); gst_event_unref (out_event); - /* Now data comes in again, a "bulk" lost packet is created for 3 -> 6 */ - rtp_ts += (160 * 15); + /* Now data comes in again, a "bulk" lost packet is created for 3 -> 5 */ + rtp_ts += (PCMU_BUF_SIZE * 15); in_buf = generate_test_buffer (timestamp_ms * GST_MSECOND, TRUE, 16, rtp_ts); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); @@ -1472,38 +1444,38 @@ GST_START_TEST (test_gap_exceeds_latency) in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 8, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - last_ts += 20; - last_rtp += 160; + last_ts += PCMU_BUF_MS; + last_rtp += PCMU_BUF_SIZE; in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 9, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - last_ts += 20; - last_rtp += 160; + last_ts += PCMU_BUF_MS; + last_rtp += PCMU_BUF_SIZE; in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 10, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - last_ts += 20; - last_rtp += 160; + last_ts += PCMU_BUF_MS; + last_rtp += PCMU_BUF_SIZE; in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 11, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - last_ts += 20; - last_rtp += 160; + last_ts += PCMU_BUF_MS; + last_rtp += PCMU_BUF_SIZE; in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 12, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - last_ts += 20; - last_rtp += 160; + last_ts += PCMU_BUF_MS; + last_rtp += PCMU_BUF_SIZE; in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 13, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - last_ts += 20; - last_rtp += 160; + last_ts += PCMU_BUF_MS; + last_rtp += PCMU_BUF_SIZE; in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 14, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); - last_ts += 20; - last_rtp += 160; + last_ts += PCMU_BUF_MS; + last_rtp += PCMU_BUF_SIZE; in_buf = generate_test_buffer (last_ts * GST_MSECOND, TRUE, 15, last_rtp); g_assert_cmpint (gst_pad_push (data.test_src_pad, in_buf), ==, GST_FLOW_OK); @@ -1526,6 +1498,12 @@ GST_START_TEST (test_gap_exceeds_latency) g_assert_cmpint (seqnum, ==, 3); gst_event_unref (out_event); + out_event = g_async_queue_pop (data.sink_event_queue); + s = gst_event_get_structure (out_event); + g_assert (gst_structure_get_uint (s, "seqnum", &seqnum)); + g_assert_cmpint (seqnum, ==, 6); + gst_event_unref (out_event); + out_event = g_async_queue_pop (data.sink_event_queue); s = gst_event_get_structure (out_event); g_assert (gst_structure_get_uint (s, "seqnum", &seqnum)); @@ -1565,9 +1543,50 @@ GST_START_TEST (test_gap_exceeds_latency) destroy_testharness (&data); } - GST_END_TEST; +GST_START_TEST (test_dts_gap_larger_than_latency) +{ + GstHarness * h = gst_harness_new ("rtpjitterbuffer"); + GstTestClock * testclock; + GstEvent *out_event; + gint jb_latency_ms = 100; + GstClockTime dts_after_gap = (jb_latency_ms + 1) * GST_MSECOND; + + gst_harness_set_src_caps (h, generate_caps ()); + gst_harness_use_testclock (h); + testclock = gst_harness_get_testclock (h); + g_object_set (h->element, + "do-lost", TRUE, + "latency", jb_latency_ms, + NULL); + + /* push first buffer through */ + gst_harness_push (h, generate_test_buffer (0 * GST_MSECOND, TRUE, 0, 0)); + gst_harness_crank_single_clock_wait (h); + gst_buffer_unref (gst_harness_pull (h)); + + /* Push packet with DTS larger than latency */ + gst_harness_push (h, generate_test_buffer (dts_after_gap, + TRUE, 5, 5 * PCMU_BUF_SIZE)); + + /* drop GstEventStreamStart & GstEventCaps & GstEventSegment */ + for (int i = 0; i < 3; i++) + gst_event_unref (gst_harness_pull_event (h)); + + /* Time out and verify lost events */ + for (gint i = 1; i < 5; i++) { + GstClockTime dur = dts_after_gap / 5; + gst_harness_crank_single_clock_wait (h); + out_event = gst_harness_pull_event (h); + fail_unless (out_event != NULL); + verify_lost_event (out_event, i, i * dur, dur); + } + + gst_object_unref (testclock); + gst_harness_teardown (h); +} +GST_END_TEST; static Suite * rtpjitterbuffer_suite (void) @@ -1589,6 +1608,7 @@ rtpjitterbuffer_suite (void) tcase_add_test (tc_chain, test_rtx_two_missing); tcase_add_test (tc_chain, test_rtx_packet_delay); tcase_add_test (tc_chain, test_gap_exceeds_latency); + tcase_add_test (tc_chain, test_dts_gap_larger_than_latency); return s; }