videorate: Add unit test for closing a segment and opening a separate one
authorOlivier Crête <olivier.crete@collabora.com>
Wed, 16 Jun 2021 19:07:13 +0000 (15:07 -0400)
committerOlivier Crête <olivier.crete@ocrete.ca>
Wed, 15 Sep 2021 15:35:43 +0000 (15:35 +0000)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/767>

tests/check/elements/videorate.c

index d378215..4404289 100644 (file)
@@ -1687,6 +1687,147 @@ GST_START_TEST (test_segment_update)
 
 GST_END_TEST;
 
+/* test segment closing */
+GST_START_TEST (test_segment_closing)
+{
+  GstElement *videorate;
+  GstBuffer *buf;
+  GstCaps *caps;
+  GstSegment segment;
+  GList *l;
+  GstClockTime next_ts = 0;
+
+  videorate = setup_videorate_full (&srctemplate, &downstreamsinktemplate);
+  fail_unless (gst_element_set_state (videorate,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
+      "could not set to playing");
+
+  caps = gst_caps_from_string (VIDEO_CAPS_STRING);
+  gst_check_setup_events (mysrcpad, videorate, caps, GST_FORMAT_TIME);
+  gst_caps_unref (caps);
+
+  /* Send first closed segment [0, 5] */
+  gst_segment_init (&segment, GST_FORMAT_TIME);
+  segment.start = 0;
+  segment.stop = 5 * GST_SECOND;
+  fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
+
+  /* first buffer */
+  buf = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (buf) = 0;
+  gst_buffer_memset (buf, 0, 1, 4);
+  ASSERT_BUFFER_REFCOUNT (buf, "first", 1);
+  gst_buffer_ref (buf);
+
+  GST_DEBUG ("pushing first buffer");
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  /* ... and a copy is now stuck inside videorate */
+  ASSERT_BUFFER_REFCOUNT (buf, "first", 1);
+  gst_buffer_unref (buf);
+  fail_unless_equals_int (g_list_length (buffers), 0);
+  assert_videorate_stats (videorate, "first", 1, 0, 0, 0);
+
+  /* second buffer */
+  buf = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (buf) = 2 * GST_SECOND;
+  gst_buffer_memset (buf, 0, 2, 4);
+  ASSERT_BUFFER_REFCOUNT (buf, "second", 1);
+  gst_buffer_ref (buf);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  ASSERT_BUFFER_REFCOUNT (buf, "second", 1);
+  gst_buffer_unref (buf);
+  fail_unless_equals_int (g_list_length (buffers), 26);
+  assert_videorate_stats (videorate, "second", 2, 26, 0, 25);
+
+  for (l = buffers; l; l = l->next) {
+    buf = l->data;
+    fail_unless_equals_uint64 (GST_BUFFER_PTS (buf), next_ts);
+    fail_unless_equals_int (buffer_get_byte (buf, 0), 1);
+
+    next_ts += GST_SECOND / 25;
+  }
+  gst_check_drop_buffers ();
+  fail_unless_equals_int (g_list_length (buffers), 0);
+
+  /* Push a buffer just outside the segment */
+  buf = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (buf) = 5 * GST_SECOND + 1;
+  gst_buffer_memset (buf, 0, 7, 4);
+  ASSERT_BUFFER_REFCOUNT (buf, "drop", 1);
+  gst_buffer_ref (buf);
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  ASSERT_BUFFER_REFCOUNT (buf, "drop", 1);
+  gst_buffer_unref (buf);
+
+  fail_unless_equals_int (g_list_length (buffers), 0);
+
+
+  /* Send a new segment [10, 20] that doesn't intersect with the first */
+  gst_segment_init (&segment, GST_FORMAT_TIME);
+  segment.base = 10 * GST_SECOND;
+  segment.start = 10 * GST_SECOND;
+  segment.stop = 20 * GST_SECOND;
+  fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
+
+  /* Except the rest of the initial segment to be filled */
+  fail_unless_equals_int (g_list_length (buffers), 26);
+  for (l = buffers; l; l = l->next) {
+    buf = l->data;
+    fail_unless_equals_uint64 (GST_BUFFER_PTS (buf), next_ts);
+    fail_unless_equals_int (buffer_get_byte (buf, 0), 2);
+
+    next_ts += GST_SECOND / 25;
+  }
+  gst_check_drop_buffers ();
+  fail_unless_equals_uint64 (next_ts, (25 + 25 + 2) * (GST_SECOND / 25));
+
+  /* third buffer */
+  buf = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (buf) = 12 * GST_SECOND;
+  gst_buffer_memset (buf, 0, 8, 4);
+  ASSERT_BUFFER_REFCOUNT (buf, "third", 1);
+  gst_buffer_ref (buf);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  ASSERT_BUFFER_REFCOUNT (buf, "third", 1);
+  gst_buffer_unref (buf);
+  fail_unless_equals_int (g_list_length (buffers), 0);
+
+  /* fourth buffer */
+  buf = gst_buffer_new_and_alloc (4);
+  GST_BUFFER_TIMESTAMP (buf) = 14 * GST_SECOND;
+  gst_buffer_memset (buf, 0, 9, 4);
+  ASSERT_BUFFER_REFCOUNT (buf, "fourth", 1);
+  gst_buffer_ref (buf);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
+  ASSERT_BUFFER_REFCOUNT (buf, "fourth", 1);
+  gst_buffer_unref (buf);
+  fail_unless_equals_int (g_list_length (buffers), 76);
+
+  assert_videorate_stats (videorate, "fourth", 4, 128, 0, 125);
+
+  /* Expect the ts to stat from 10ms now */
+  next_ts = 10 * GST_SECOND;
+  for (l = buffers; l; l = l->next) {
+    buf = l->data;
+    fail_unless_equals_uint64 (GST_BUFFER_PTS (buf), next_ts);
+    fail_unless_equals_int (buffer_get_byte (buf, 0), 8);
+
+    next_ts += GST_SECOND / 25;
+  }
+
+  /* cleanup */
+  cleanup_videorate (videorate);
+}
+
+GST_END_TEST;
+
 static Suite *
 videorate_suite (void)
 {
@@ -1714,6 +1855,7 @@ videorate_suite (void)
       G_N_ELEMENTS (position_tests));
   tcase_add_test (tc_chain, test_nopts_in_middle);
   tcase_add_test (tc_chain, test_segment_update);
+  tcase_add_test (tc_chain, test_segment_closing);
 
   return s;
 }