1 /* GStreamer simple seek unit test
2 * Copyright (C) 2012 Collabora Ltd.
3 * Author: Tim-Philipp Müller <tim.muller@collabora.co.uk>
4 * Copyright (C) Julien Isorce <jisorce@oblong.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.
27 #include <gst/base/gstbaseparse.h>
28 #include <gst/base/gstbasesrc.h>
30 #include <gst/check/gstcheck.h>
31 #include <gst/check/gstconsistencychecker.h>
33 /* ========================================================================
34 * Dummy source, like a stripped down audio test source
35 * ======================================================================== */
37 #define SAMPLERATE 44100
38 #define CHUNKS_PER_SEC 10
43 GstClockTime next_time;
48 GstBaseSrcClass parent_class;
51 static GstStaticPadTemplate timed_test_src_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("timed/audio"));
57 static GType timed_test_src_get_type (void);
59 G_DEFINE_TYPE (TimedTestSrc, timed_test_src, GST_TYPE_BASE_SRC);
61 static gboolean timed_test_src_is_seekable (GstBaseSrc * basesrc);
62 static gboolean timed_test_src_do_seek (GstBaseSrc * basesrc,
63 GstSegment * segment);
64 static gboolean timed_test_src_start (GstBaseSrc * basesrc);
65 static gboolean timed_test_src_stop (GstBaseSrc * basesrc);
66 static GstFlowReturn timed_test_src_create (GstBaseSrc * basesrc,
67 guint64 offset, guint length, GstBuffer ** buffer);
70 timed_test_src_class_init (TimedTestSrcClass * klass)
72 GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
74 gstbasesrc_class->is_seekable = timed_test_src_is_seekable;
75 gstbasesrc_class->do_seek = timed_test_src_do_seek;
76 gstbasesrc_class->start = timed_test_src_start;
77 gstbasesrc_class->stop = timed_test_src_stop;
78 gstbasesrc_class->create = timed_test_src_create;
80 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
81 &timed_test_src_src_template);
85 timed_test_src_init (TimedTestSrc * src)
87 gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
88 gst_base_src_set_live (GST_BASE_SRC (src), FALSE);
92 timed_test_src_start (GstBaseSrc * basesrc)
94 TimedTestSrc *src = (TimedTestSrc *) basesrc;
101 timed_test_src_stop (GstBaseSrc * basesrc)
107 timed_test_src_do_seek (GstBaseSrc * basesrc, GstSegment * segment)
109 TimedTestSrc *src = (TimedTestSrc *) basesrc;
111 src->next_time = segment->position;
116 timed_test_src_is_seekable (GstBaseSrc * basesrc)
122 timed_test_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
125 TimedTestSrc *src = (TimedTestSrc *) basesrc;
127 *buf = gst_buffer_new_and_alloc (SAMPLERATE / CHUNKS_PER_SEC);
128 GST_BUFFER_TIMESTAMP (*buf) = src->next_time;
129 GST_BUFFER_DURATION (*buf) = GST_SECOND / CHUNKS_PER_SEC;
130 src->next_time += GST_BUFFER_DURATION (*buf);
134 /* ========================================================================
136 * ======================================================================== */
146 GstBaseParseClass parent_class;
149 static GType dummy_parser_get_type (void);
151 G_DEFINE_TYPE (DummyParser, dummy_parser, GST_TYPE_BASE_PARSE);
154 dummy_parser_start (GstBaseParse * parse)
160 dummy_parser_stop (GstBaseParse * parse)
166 dummy_parser_handle_frame (GstBaseParse * parse,
167 GstBaseParseFrame * frame, gint * skipsize)
169 if (((DummyParser *) parse)->caps_set == FALSE) {
172 caps = gst_caps_new_empty_simple ("ANY");
173 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
174 gst_caps_unref (caps);
175 ((DummyParser *) parse)->caps_set = TRUE;
178 GST_BUFFER_DURATION (frame->buffer) = GST_SECOND / 10;
180 return gst_base_parse_finish_frame (parse, frame,
181 gst_buffer_get_size (frame->buffer));
185 dummy_parser_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
187 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
192 dummy_parser_src_event (GstBaseParse * parse, GstEvent * event)
194 switch (GST_EVENT_TYPE (event)) {
196 GST_INFO ("src event %s", GST_EVENT_TYPE_NAME (event));
199 return GST_BASE_PARSE_CLASS (dummy_parser_parent_class)->src_event (parse,
204 dummy_parser_class_init (DummyParserClass * klass)
206 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
207 GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass);
209 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
210 GST_PAD_SINK, GST_PAD_ALWAYS,
211 GST_STATIC_CAPS_ANY);
213 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
214 GST_PAD_SRC, GST_PAD_ALWAYS,
215 GST_STATIC_CAPS_ANY);
217 gst_element_class_add_static_pad_template (element_class, &sink_templ);
218 gst_element_class_add_static_pad_template (element_class, &src_templ);
220 gst_element_class_set_metadata (element_class,
221 "DummyParser", "Parser/Video", "empty", "empty");
223 baseparse_class->start = GST_DEBUG_FUNCPTR (dummy_parser_start);
224 baseparse_class->stop = GST_DEBUG_FUNCPTR (dummy_parser_stop);
225 baseparse_class->handle_frame = GST_DEBUG_FUNCPTR (dummy_parser_handle_frame);
226 baseparse_class->set_sink_caps =
227 GST_DEBUG_FUNCPTR (dummy_parser_set_sink_caps);
228 baseparse_class->src_event = GST_DEBUG_FUNCPTR (dummy_parser_src_event);
232 dummy_parser_init (DummyParser * parser)
234 parser->caps_set = FALSE;
237 /* ======================================================================== */
239 GST_START_TEST (test_seek)
242 GstElement *bin, *src1, *sink;
247 GST_INFO ("preparing test");
250 bin = gst_pipeline_new ("pipeline");
251 bus = gst_element_get_bus (bin);
252 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
254 src1 = g_object_new (timed_test_src_get_type (), "name", "testsrc", NULL);
256 sink = gst_element_factory_make ("fakesink", "sink");
257 gst_bin_add_many (GST_BIN (bin), src1, sink, NULL);
259 res = gst_element_link (src1, sink);
260 fail_unless (res == TRUE, NULL);
262 srcpad = gst_element_get_static_pad (src1, "src");
263 gst_object_unref (srcpad);
265 GST_INFO ("starting test");
267 /* prepare playing */
268 res = gst_element_set_state (bin, GST_STATE_PAUSED);
269 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
271 /* wait for completion */
273 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
274 GST_CLOCK_TIME_NONE);
275 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
277 res = gst_element_send_event (bin,
278 gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
279 GST_SEEK_TYPE_SET, (GstClockTime) 0,
280 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND));
281 fail_unless (res == TRUE, NULL);
286 res = gst_element_set_state (bin, GST_STATE_PLAYING);
287 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
289 msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
290 GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
291 fail_unless_equals_string (GST_MESSAGE_TYPE_NAME (msg), "eos");
292 gst_message_unref (msg);
294 res = gst_element_set_state (bin, GST_STATE_NULL);
295 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
298 gst_bus_remove_signal_watch (bus);
299 gst_object_unref (bus);
300 gst_object_unref (bin);
306 /* This test checks that the pipeline does not wait for nothing after
307 * sending the non-flush seek event. */
308 GST_START_TEST (test_loopback_1)
311 GstElement *bin, *source, *parser, *sink;
318 ("construct the test pipeline fakesrc ! testparse ! fakesink sync=1");
320 bin = gst_pipeline_new ("pipeline");
321 bus = gst_element_get_bus (bin);
322 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
324 source = gst_element_factory_make ("fakesrc", "source");
325 parser = g_object_new (dummy_parser_get_type (), "name", "testparse", NULL);
326 sink = gst_element_factory_make ("fakesink", "sink");
328 gst_bin_add_many (GST_BIN (bin), source, parser, sink, NULL);
330 res = gst_element_link (source, parser);
331 fail_unless (res == TRUE, NULL);
332 res = gst_element_link (parser, sink);
333 fail_unless (res == TRUE, NULL);
335 GST_INFO ("configure elements");
337 g_object_set (G_OBJECT (source),
338 "format", GST_FORMAT_BYTES,
339 "can-activate-pull", TRUE,
340 "can-activate-push", FALSE,
342 "sizemax", 65536, "sizetype", 2 /* FAKE_SRC_SIZETYPE_FIXED */ ,
343 "num-buffers", 35, NULL);
345 /* Sync true is required for this test. */
346 g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
348 GST_INFO ("set paused state");
350 res = gst_element_set_state (bin, GST_STATE_PAUSED);
351 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
353 GST_INFO ("wait for completion");
356 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
357 GST_CLOCK_TIME_NONE);
358 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
360 GST_INFO ("paused state reached");
362 fail_unless (GST_BASE_SRC (source)->random_access);
364 sinkpad = gst_element_get_static_pad (parser, "sink");
365 fail_unless (GST_PAD_MODE (sinkpad) == GST_PAD_MODE_PULL);
366 gst_object_unref (sinkpad);
368 GST_INFO ("flush seek");
371 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_SEGMENT;
373 res = gst_element_seek (bin, 1.0, GST_FORMAT_TIME, seek_flags,
374 GST_SEEK_TYPE_SET, (GstClockTime) 0,
375 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
376 fail_unless (res == TRUE, NULL);
378 GST_INFO ("set playing state");
380 res = gst_element_set_state (bin, GST_STATE_PLAYING);
381 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
383 msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
384 GST_MESSAGE_SEGMENT_DONE | GST_MESSAGE_ERROR);
385 fail_unless_equals_string (GST_MESSAGE_TYPE_NAME (msg), "segment-done");
386 gst_message_unref (msg);
388 GST_INFO ("warm-up sequence done");
390 seek_flags &= ~GST_SEEK_FLAG_FLUSH;
391 fail_if (seek_flags & GST_SEEK_FLAG_FLUSH, NULL);
393 GST_INFO ("non-flush seek");
396 gst_element_seek (bin, 1.0, GST_FORMAT_TIME, seek_flags,
397 GST_SEEK_TYPE_SET, (GstClockTime) 0, GST_SEEK_TYPE_SET,
398 (GstClockTime) 3 * GST_SECOND);
399 fail_unless (res == TRUE, NULL);
401 GST_INFO ("wait for segment done message");
403 msg = gst_bus_timed_pop_filtered (bus, (GstClockTime) 2 * GST_SECOND,
404 GST_MESSAGE_SEGMENT_DONE | GST_MESSAGE_ERROR);
405 /* Make sure the pipeline is not waiting for nothing because the base time is
407 fail_unless (msg, "no message within the timed window");
408 fail_unless_equals_string (GST_MESSAGE_TYPE_NAME (msg), "segment-done");
409 gst_message_unref (msg);
411 res = gst_element_set_state (bin, GST_STATE_NULL);
412 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
415 gst_bus_remove_signal_watch (bus);
416 gst_object_unref (bus);
417 gst_object_unref (bin);
422 /* This test checks that the pipeline does not play the media instantly
423 * after sending the non-flush seek event. */
424 GST_START_TEST (test_loopback_2)
427 GstElement *bin, *source, *parser, *sink;
432 gint64 position = GST_CLOCK_TIME_NONE;
433 gint64 playback_duration = GST_CLOCK_TIME_NONE;
434 gint64 start_absolute_time = GST_CLOCK_TIME_NONE;
437 ("construct the test pipeline fakesrc ! testparse ! fakesink sync=1");
439 bin = gst_pipeline_new ("pipeline");
440 bus = gst_element_get_bus (bin);
441 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
443 source = gst_element_factory_make ("fakesrc", "source");
444 parser = g_object_new (dummy_parser_get_type (), "name", "testparse", NULL);
445 sink = gst_element_factory_make ("fakesink", "sink");
447 gst_bin_add_many (GST_BIN (bin), source, parser, sink, NULL);
449 res = gst_element_link (source, parser);
450 fail_unless (res == TRUE, NULL);
451 res = gst_element_link (parser, sink);
452 fail_unless (res == TRUE, NULL);
454 GST_INFO ("configure elements");
456 g_object_set (G_OBJECT (source),
457 "format", GST_FORMAT_BYTES,
458 "can-activate-pull", TRUE,
459 "can-activate-push", FALSE,
461 "sizemax", 65536, "sizetype", 2 /* FAKE_SRC_SIZETYPE_FIXED */ ,
464 /* Sync true is required for this test. */
465 g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
467 GST_INFO ("set paused state");
469 res = gst_element_set_state (bin, GST_STATE_PAUSED);
470 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
472 GST_INFO ("wait for completion");
475 gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
476 GST_CLOCK_TIME_NONE);
477 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
479 GST_INFO ("paused state reached");
481 fail_unless (GST_BASE_SRC (source)->random_access);
483 sinkpad = gst_element_get_static_pad (parser, "sink");
484 fail_unless (GST_PAD_MODE (sinkpad) == GST_PAD_MODE_PULL);
485 gst_object_unref (sinkpad);
487 GST_INFO ("flush seek");
490 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_SEGMENT;
492 res = gst_element_seek (bin, 1.0, GST_FORMAT_TIME, seek_flags,
493 GST_SEEK_TYPE_SET, (GstClockTime) 0,
494 GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
495 fail_unless (res == TRUE, NULL);
497 GST_INFO ("set playing state");
499 res = gst_element_set_state (bin, GST_STATE_PLAYING);
500 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
502 msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
503 GST_MESSAGE_SEGMENT_DONE | GST_MESSAGE_ERROR);
504 fail_unless_equals_string (GST_MESSAGE_TYPE_NAME (msg), "segment-done");
505 gst_message_unref (msg);
507 GST_INFO ("warm-up sequence done");
509 seek_flags &= ~GST_SEEK_FLAG_FLUSH;
510 fail_if (seek_flags & GST_SEEK_FLAG_FLUSH, NULL);
512 GST_INFO ("non-flush seek");
514 start_absolute_time = gst_clock_get_time (GST_ELEMENT_CLOCK (bin));
517 gst_element_seek (bin, 1.0, GST_FORMAT_TIME, seek_flags,
518 GST_SEEK_TYPE_SET, (GstClockTime) 0, GST_SEEK_TYPE_SET,
519 (GstClockTime) 2 * GST_SECOND);
520 fail_unless (res == TRUE, NULL);
522 GST_INFO ("wait for segment done message");
524 msg = gst_bus_timed_pop_filtered (bus, (GstClockTime) 2 * GST_SECOND,
525 GST_MESSAGE_SEGMENT_DONE | GST_MESSAGE_ERROR);
526 fail_unless (msg, "no message within the timed window");
527 fail_unless_equals_string (GST_MESSAGE_TYPE_NAME (msg), "segment-done");
529 gst_message_parse_segment_done (msg, NULL, &position);
530 gst_message_unref (msg);
532 GST_INFO ("final position: %" G_GINT64_FORMAT, position);
534 fail_unless (position == 2 * GST_SECOND);
537 GST_CLOCK_DIFF (start_absolute_time,
538 gst_clock_get_time (GST_ELEMENT_CLOCK (bin)));
540 GST_INFO ("test duration: %" G_GINT64_FORMAT, playback_duration);
542 fail_unless (playback_duration > GST_SECOND,
543 "playback duration should be near 2 seconds");
545 res = gst_element_set_state (bin, GST_STATE_NULL);
546 fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
549 gst_bus_remove_signal_watch (bus);
550 gst_object_unref (bus);
551 gst_object_unref (bin);
557 pipelines_seek_suite (void)
559 Suite *s = suite_create ("pipelines-seek");
560 TCase *tc_chain = tcase_create ("general");
562 suite_add_tcase (s, tc_chain);
563 tcase_add_test (tc_chain, test_seek);
564 tcase_add_test (tc_chain, test_loopback_1);
565 tcase_add_test (tc_chain, test_loopback_2);
570 GST_CHECK_MAIN (pipelines_seek);