3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 * Author: Thiago Santos <ts.santos@sisa.samsung.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
26 #include <gst/check/gstcheck.h>
27 #include <gst/base/gstbaseparse.h>
29 static GstPad *mysrcpad, *mysinkpad;
30 static GstElement *parsetest;
32 static GMainLoop *loop = NULL;
33 static gboolean have_eos = FALSE;
34 static gboolean have_data = FALSE;
35 static gint buffer_count = 0;
36 static gboolean caps_set = FALSE;
37 static gchar *raw_buffer = NULL;
38 static gint raw_buffer_size = 0;
39 static gint buffer_pull_count = 0;
40 static gint current_offset = 0;
42 #define TEST_VIDEO_WIDTH 640
43 #define TEST_VIDEO_HEIGHT 480
44 #define TEST_VIDEO_FPS_N 30
45 #define TEST_VIDEO_FPS_D 1
47 #define GST_PARSER_TESTER_TYPE gst_parser_tester_get_type()
48 static GType gst_parser_tester_get_type (void);
50 typedef struct _GstParserTester GstParserTester;
51 typedef struct _GstParserTesterClass GstParserTesterClass;
53 struct _GstParserTester
58 struct _GstParserTesterClass
60 GstBaseParseClass parent_class;
63 G_DEFINE_TYPE (GstParserTester, gst_parser_tester, GST_TYPE_BASE_PARSE);
66 gst_parser_tester_start (GstBaseParse * parse)
72 gst_parser_tester_stop (GstBaseParse * parse)
78 gst_parser_tester_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
80 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
85 gst_parser_tester_handle_frame (GstBaseParse * parse,
86 GstBaseParseFrame * frame, gint * skipsize)
88 GstFlowReturn ret = GST_FLOW_OK;
90 if (caps_set == FALSE) {
94 gst_caps_new_simple ("video/x-test-custom", "width", G_TYPE_INT,
95 TEST_VIDEO_WIDTH, "height", G_TYPE_INT, TEST_VIDEO_HEIGHT, "framerate",
96 GST_TYPE_FRACTION, TEST_VIDEO_FPS_N, TEST_VIDEO_FPS_D, NULL);
97 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
98 gst_caps_unref (caps);
102 while (frame->buffer && gst_buffer_get_size (frame->buffer) >= 8) {
103 GST_BUFFER_DURATION (frame->buffer) =
104 gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
106 ret = gst_base_parse_finish_frame (parse, frame, 8);
112 gst_parser_tester_class_init (GstParserTesterClass * klass)
114 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
115 GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass);
117 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
118 GST_PAD_SINK, GST_PAD_ALWAYS,
119 GST_STATIC_CAPS ("video/x-test-custom"));
121 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
122 GST_PAD_SRC, GST_PAD_ALWAYS,
123 GST_STATIC_CAPS ("video/x-test-custom"));
125 gst_element_class_add_static_pad_template (element_class, &sink_templ);
126 gst_element_class_add_static_pad_template (element_class, &src_templ);
128 gst_element_class_set_metadata (element_class,
129 "ParserTester", "Parser/Video", "yep", "me");
131 baseparse_class->start = gst_parser_tester_start;
132 baseparse_class->stop = gst_parser_tester_stop;
133 baseparse_class->handle_frame = gst_parser_tester_handle_frame;
134 baseparse_class->set_sink_caps = gst_parser_tester_set_sink_caps;
138 gst_parser_tester_init (GstParserTester * tester)
143 setup_parsertester (void)
145 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
148 GST_STATIC_CAPS ("video/x-test-custom")
150 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
153 GST_STATIC_CAPS ("video/x-test-custom")
156 parsetest = g_object_new (GST_PARSER_TESTER_TYPE, NULL);
157 mysrcpad = gst_check_setup_src_pad (parsetest, &srctemplate);
158 mysinkpad = gst_check_setup_sink_pad (parsetest, &sinktemplate);
159 bus = gst_bus_new ();
160 gst_element_set_bus (parsetest, bus);
164 cleanup_parsertest (void)
166 /* release the bus first to get rid of all refcounts */
167 gst_element_set_bus (parsetest, NULL);
168 gst_object_unref (bus);
170 gst_pad_set_active (mysrcpad, FALSE);
171 gst_pad_set_active (mysinkpad, FALSE);
172 gst_check_teardown_src_pad (parsetest);
173 gst_check_teardown_sink_pad (parsetest);
174 gst_check_teardown_element (parsetest);
178 create_test_buffer (guint64 num)
181 guint64 *data = g_malloc (sizeof (guint64));
185 buffer = gst_buffer_new_wrapped (data, sizeof (guint64));
187 GST_BUFFER_PTS (buffer) =
188 gst_util_uint64_scale_round (num, GST_SECOND * TEST_VIDEO_FPS_D,
190 GST_BUFFER_DURATION (buffer) =
191 gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
198 send_startup_events (void)
202 fail_unless (gst_pad_push_event (mysrcpad,
203 gst_event_new_stream_start ("randomvalue")));
207 gst_caps_new_simple ("video/x-test-custom", "width", G_TYPE_INT,
208 TEST_VIDEO_WIDTH, "height", G_TYPE_INT, TEST_VIDEO_HEIGHT, "framerate",
209 GST_TYPE_FRACTION, TEST_VIDEO_FPS_N, TEST_VIDEO_FPS_D, NULL);
210 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_caps (caps)));
211 gst_caps_unref (caps);
215 check_no_error_received (void)
219 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR);
220 fail_unless (msg == NULL);
222 gst_message_unref (msg);
226 run_parser_playback_test (GList * input, gint expected_output, gdouble rate)
233 gst_pad_set_active (mysrcpad, TRUE);
234 gst_element_set_state (parsetest, GST_STATE_PLAYING);
235 gst_pad_set_active (mysinkpad, TRUE);
237 send_startup_events ();
239 /* push a new segment */
240 gst_segment_init (&segment, GST_FORMAT_TIME);
242 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
244 /* push buffers, the data is actually a number so we can track them */
245 for (iter = input; iter; iter = g_list_next (iter)) {
248 fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
252 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
254 /* check that all buffers were received by our source pad */
255 fail_unless (g_list_length (buffers) == expected_output);
257 for (iter = buffers; iter; iter = g_list_next (iter)) {
263 gst_buffer_map (buffer, &map, GST_MAP_READ);
265 num = *(guint64 *) map.data;
266 fail_unless (i == num);
267 fail_unless (GST_BUFFER_PTS (buffer) == gst_util_uint64_scale_round (i,
268 GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
269 fail_unless (GST_BUFFER_DURATION (buffer) ==
270 gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
273 gst_buffer_unmap (buffer, &map);
277 g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
280 check_no_error_received ();
282 cleanup_parsertest ();
286 GST_START_TEST (parser_playback)
292 setup_parsertester ();
294 /* push buffers, the data is actually a number so we can track them */
295 for (i = 0; i < 3; i++) {
296 buffer = create_test_buffer (i);
297 input = g_list_append (input, buffer);
300 run_parser_playback_test (input, 3, 1.0);
306 /* Check https://bugzilla.gnome.org/show_bug.cgi?id=721941 */
307 GST_START_TEST (parser_reverse_playback_on_passthrough)
313 setup_parsertester ();
315 gst_base_parse_set_passthrough (GST_BASE_PARSE (parsetest), TRUE);
317 /* push buffers, the data is actually a number so we can track them */
318 for (i = 0; i < 6; i++) {
319 buffer = create_test_buffer (i);
321 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
322 input = g_list_append (input, buffer);
324 GST_BUFFER_FLAG_SET (g_list_nth (input, 3)->data, GST_BUFFER_FLAG_DISCONT);
326 run_parser_playback_test (input, 6, -1.0);
331 GST_START_TEST (parser_empty_stream)
333 setup_parsertester ();
335 run_parser_playback_test (NULL, 0, 1.0);
341 _sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
346 gst_buffer_map (buffer, &map, GST_MAP_READ);
348 num = *(guint64 *) map.data;
350 fail_unless (buffer_count == num);
351 fail_unless (GST_BUFFER_PTS (buffer) ==
352 gst_util_uint64_scale_round (buffer_count, GST_SECOND * TEST_VIDEO_FPS_D,
354 fail_unless (GST_BUFFER_DURATION (buffer) ==
355 gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
357 gst_buffer_unmap (buffer, &map);
358 gst_buffer_unref (buffer);
366 _sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
368 GST_INFO_OBJECT (pad, "got %s event %p: %" GST_PTR_FORMAT,
369 GST_EVENT_TYPE_NAME (event), event, event);
371 switch (GST_EVENT_TYPE (event)) {
374 while (!g_main_loop_is_running (loop)) {
380 g_main_loop_quit (loop);
385 gst_event_unref (event);
391 _src_getrange (GstPad * pad, GstObject * parent, guint64 offset, guint length,
394 gboolean ret = FALSE;
395 if (offset >= 80 && have_eos == FALSE) {
396 ret = gst_element_seek (parsetest, -1.0, GST_FORMAT_TIME,
397 GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH,
398 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, gst_util_uint64_scale_round (5,
399 GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
400 fail_unless (ret == TRUE);
404 *buffer = create_test_buffer (offset / 8);
410 _src_query (GstPad * pad, GstObject * parent, GstQuery * query)
412 gboolean res = FALSE;
414 switch (GST_QUERY_TYPE (query)) {
415 case GST_QUERY_SCHEDULING:{
416 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
417 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
422 GST_DEBUG_OBJECT (pad, "unhandled %s query", GST_QUERY_TYPE_NAME (query));
429 GST_START_TEST (parser_reverse_playback)
433 loop = g_main_loop_new (NULL, FALSE);
435 setup_parsertester ();
436 gst_pad_set_getrange_function (mysrcpad, _src_getrange);
437 gst_pad_set_query_function (mysrcpad, _src_query);
438 gst_pad_set_chain_function (mysinkpad, _sink_chain);
439 gst_pad_set_event_function (mysinkpad, _sink_event);
441 gst_pad_set_active (mysrcpad, TRUE);
442 gst_element_set_state (parsetest, GST_STATE_PLAYING);
443 gst_pad_set_active (mysinkpad, TRUE);
445 g_main_loop_run (loop);
446 fail_unless (have_eos == TRUE);
447 fail_unless (have_data == TRUE);
449 gst_element_set_state (parsetest, GST_STATE_NULL);
451 check_no_error_received ();
452 cleanup_parsertest ();
454 g_main_loop_unref (loop);
461 _sink_chain_pull_short_read (GstPad * pad, GstObject * parent,
465 gint buffer_size = 0;
466 gint compare_result = 0;
468 gst_buffer_map (buffer, &map, GST_MAP_READ);
469 buffer_size = map.size;
471 fail_unless (current_offset + buffer_size <= raw_buffer_size);
472 compare_result = memcmp (map.data, &raw_buffer[current_offset], buffer_size);
473 fail_unless_equals_int (compare_result, 0);
475 gst_buffer_unmap (buffer, &map);
476 gst_buffer_unref (buffer);
485 _src_getrange_pull_short_read (GstPad * pad, GstObject * parent,
486 guint64 offset, guint length, GstBuffer ** buffer)
488 GstBuffer *buf = NULL;
490 guint buffer_size = 0;
492 if (offset == raw_buffer_size) {
493 /* pulled all buffer */
497 if (offset + length <= raw_buffer_size) {
498 buffer_size = length;
500 /* buffer size is less than request size */
501 buffer_size = raw_buffer_size - offset;
504 data = g_malloc (buffer_size);
505 memcpy (data, &raw_buffer[offset], buffer_size);
506 buf = gst_buffer_new_wrapped (data, buffer_size);
508 GST_BUFFER_PTS (buffer) =
509 gst_util_uint64_scale_round (buffer_pull_count,
510 GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N);
511 GST_BUFFER_DURATION (buf) =
512 gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
516 current_offset = offset;
522 GST_START_TEST (parser_pull_short_read)
525 raw_buffer_size = (64 * 1024) + 512;
526 raw_buffer = g_malloc (raw_buffer_size);
528 for (i = 0; i < raw_buffer_size; i++) {
530 raw_buffer[i] = 'a' + j;
536 loop = g_main_loop_new (NULL, FALSE);
538 setup_parsertester ();
539 gst_pad_set_getrange_function (mysrcpad, _src_getrange_pull_short_read);
540 gst_pad_set_query_function (mysrcpad, _src_query);
541 gst_pad_set_chain_function (mysinkpad, _sink_chain_pull_short_read);
542 gst_pad_set_event_function (mysinkpad, _sink_event);
543 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (parsetest), 1024);
545 gst_pad_set_active (mysrcpad, TRUE);
546 gst_element_set_state (parsetest, GST_STATE_PLAYING);
547 gst_pad_set_active (mysinkpad, TRUE);
549 g_main_loop_run (loop);
550 fail_unless_equals_int (have_eos, TRUE);
551 fail_unless_equals_int (have_data, TRUE);
552 fail_unless_equals_int (buffer_pull_count, buffer_count);
554 gst_element_set_state (parsetest, GST_STATE_NULL);
556 check_no_error_received ();
557 cleanup_parsertest ();
559 g_main_loop_unref (loop);
566 baseparse_setup (void)
568 /* init/reset global state */
569 mysrcpad = mysinkpad = NULL;
573 have_eos = have_data = caps_set = FALSE;
578 baseparse_teardown (void)
583 gst_baseparse_suite (void)
585 Suite *s = suite_create ("GstBaseParse");
586 TCase *tc = tcase_create ("general");
588 suite_add_tcase (s, tc);
589 tcase_add_checked_fixture (tc, baseparse_setup, baseparse_teardown);
590 tcase_add_test (tc, parser_playback);
591 tcase_add_test (tc, parser_empty_stream);
592 tcase_add_test (tc, parser_reverse_playback_on_passthrough);
593 tcase_add_test (tc, parser_reverse_playback);
594 tcase_add_test (tc, parser_pull_short_read);
599 GST_CHECK_MAIN (gst_baseparse);