Use g_memdup2() where available and add fallback for older GLib versions
[platform/upstream/gstreamer.git] / tests / check / pipelines / seek.c
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <gst/gst.h>
27 #include <gst/base/gstbaseparse.h>
28 #include <gst/base/gstbasesrc.h>
29
30 #include <gst/check/gstcheck.h>
31 #include <gst/check/gstconsistencychecker.h>
32
33 /* ========================================================================
34  *  Dummy source, like a stripped down audio test source
35  * ======================================================================== */
36
37 #define SAMPLERATE 44100
38 #define CHUNKS_PER_SEC 10
39
40 typedef struct
41 {
42   GstBaseSrc parent;
43   GstClockTime next_time;
44 } TimedTestSrc;
45
46 typedef struct
47 {
48   GstBaseSrcClass parent_class;
49 } TimedTestSrcClass;
50
51 static GstStaticPadTemplate timed_test_src_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("timed/audio"));
56
57 static GType timed_test_src_get_type (void);
58
59 G_DEFINE_TYPE (TimedTestSrc, timed_test_src, GST_TYPE_BASE_SRC);
60
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);
68
69 static void
70 timed_test_src_class_init (TimedTestSrcClass * klass)
71 {
72   GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
73
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;
79
80   gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
81       &timed_test_src_src_template);
82 }
83
84 static void
85 timed_test_src_init (TimedTestSrc * src)
86 {
87   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
88   gst_base_src_set_live (GST_BASE_SRC (src), FALSE);
89 }
90
91 static gboolean
92 timed_test_src_start (GstBaseSrc * basesrc)
93 {
94   TimedTestSrc *src = (TimedTestSrc *) basesrc;
95
96   src->next_time = 0;
97   return TRUE;
98 }
99
100 static gboolean
101 timed_test_src_stop (GstBaseSrc * basesrc)
102 {
103   return TRUE;
104 }
105
106 static gboolean
107 timed_test_src_do_seek (GstBaseSrc * basesrc, GstSegment * segment)
108 {
109   TimedTestSrc *src = (TimedTestSrc *) basesrc;
110
111   src->next_time = segment->position;
112   return TRUE;
113 }
114
115 static gboolean
116 timed_test_src_is_seekable (GstBaseSrc * basesrc)
117 {
118   return TRUE;
119 }
120
121 static GstFlowReturn
122 timed_test_src_create (GstBaseSrc * basesrc, guint64 offset, guint length,
123     GstBuffer ** buf)
124 {
125   TimedTestSrc *src = (TimedTestSrc *) basesrc;
126
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);
131   return GST_FLOW_OK;
132 }
133
134 /* ========================================================================
135  *  Dummy parser
136  * ======================================================================== */
137
138 typedef struct
139 {
140   GstBaseParse parent;
141   gboolean caps_set;
142 } DummyParser;
143
144 typedef struct
145 {
146   GstBaseParseClass parent_class;
147 } DummyParserClass;
148
149 static GType dummy_parser_get_type (void);
150
151 G_DEFINE_TYPE (DummyParser, dummy_parser, GST_TYPE_BASE_PARSE);
152
153 static gboolean
154 dummy_parser_start (GstBaseParse * parse)
155 {
156   return TRUE;
157 }
158
159 static gboolean
160 dummy_parser_stop (GstBaseParse * parse)
161 {
162   return TRUE;
163 }
164
165 static GstFlowReturn
166 dummy_parser_handle_frame (GstBaseParse * parse,
167     GstBaseParseFrame * frame, gint * skipsize)
168 {
169   if (((DummyParser *) parse)->caps_set == FALSE) {
170     GstCaps *caps;
171     /* push caps */
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;
176   }
177
178   GST_BUFFER_DURATION (frame->buffer) = GST_SECOND / 10;
179
180   return gst_base_parse_finish_frame (parse, frame,
181       gst_buffer_get_size (frame->buffer));
182 }
183
184 static gboolean
185 dummy_parser_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
186 {
187   gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
188   return TRUE;
189 }
190
191 static gboolean
192 dummy_parser_src_event (GstBaseParse * parse, GstEvent * event)
193 {
194   switch (GST_EVENT_TYPE (event)) {
195     default:
196       GST_INFO ("src event %s", GST_EVENT_TYPE_NAME (event));
197       break;
198   }
199   return GST_BASE_PARSE_CLASS (dummy_parser_parent_class)->src_event (parse,
200       event);
201 }
202
203 static void
204 dummy_parser_class_init (DummyParserClass * klass)
205 {
206   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
207   GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass);
208
209   static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
210       GST_PAD_SINK, GST_PAD_ALWAYS,
211       GST_STATIC_CAPS_ANY);
212
213   static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
214       GST_PAD_SRC, GST_PAD_ALWAYS,
215       GST_STATIC_CAPS_ANY);
216
217   gst_element_class_add_static_pad_template (element_class, &sink_templ);
218   gst_element_class_add_static_pad_template (element_class, &src_templ);
219
220   gst_element_class_set_metadata (element_class,
221       "DummyParser", "Parser/Video", "empty", "empty");
222
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);
229 }
230
231 static void
232 dummy_parser_init (DummyParser * parser)
233 {
234   parser->caps_set = FALSE;
235 }
236
237 /* ======================================================================== */
238
239 GST_START_TEST (test_seek)
240 {
241   GstMessage *msg;
242   GstElement *bin, *src1, *sink;
243   gboolean res;
244   GstPad *srcpad;
245   GstBus *bus;
246
247   GST_INFO ("preparing test");
248
249   /* build pipeline */
250   bin = gst_pipeline_new ("pipeline");
251   bus = gst_element_get_bus (bin);
252   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
253
254   src1 = g_object_new (timed_test_src_get_type (), "name", "testsrc", NULL);
255
256   sink = gst_element_factory_make ("fakesink", "sink");
257   gst_bin_add_many (GST_BIN (bin), src1, sink, NULL);
258
259   res = gst_element_link (src1, sink);
260   fail_unless (res == TRUE, NULL);
261
262   srcpad = gst_element_get_static_pad (src1, "src");
263   gst_object_unref (srcpad);
264
265   GST_INFO ("starting test");
266
267   /* prepare playing */
268   res = gst_element_set_state (bin, GST_STATE_PAUSED);
269   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
270
271   /* wait for completion */
272   res =
273       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
274       GST_CLOCK_TIME_NONE);
275   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
276
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);
282
283   GST_INFO ("seeked");
284
285   /* run pipeline */
286   res = gst_element_set_state (bin, GST_STATE_PLAYING);
287   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
288
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);
293
294   res = gst_element_set_state (bin, GST_STATE_NULL);
295   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
296
297   /* cleanup */
298   gst_bus_remove_signal_watch (bus);
299   gst_object_unref (bus);
300   gst_object_unref (bin);
301 }
302
303 GST_END_TEST;
304
305
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)
309 {
310   GstMessage *msg;
311   GstElement *bin, *source, *parser, *sink;
312   gboolean res;
313   GstPad *sinkpad;
314   GstBus *bus;
315   guint seek_flags;
316
317   GST_INFO
318       ("construct the test pipeline fakesrc ! testparse ! fakesink sync=1");
319
320   bin = gst_pipeline_new ("pipeline");
321   bus = gst_element_get_bus (bin);
322   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
323
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");
327
328   gst_bin_add_many (GST_BIN (bin), source, parser, sink, NULL);
329
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);
334
335   GST_INFO ("configure elements");
336
337   g_object_set (G_OBJECT (source),
338       "format", GST_FORMAT_BYTES,
339       "can-activate-pull", TRUE,
340       "can-activate-push", FALSE,
341       "is-live", FALSE,
342       "sizemax", 65536, "sizetype", 2 /* FAKE_SRC_SIZETYPE_FIXED */ ,
343       "num-buffers", 35, NULL);
344
345   /* Sync true is required for this test. */
346   g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
347
348   GST_INFO ("set paused state");
349
350   res = gst_element_set_state (bin, GST_STATE_PAUSED);
351   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
352
353   GST_INFO ("wait for completion");
354
355   res =
356       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
357       GST_CLOCK_TIME_NONE);
358   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
359
360   GST_INFO ("paused state reached");
361
362   fail_unless (GST_BASE_SRC (source)->random_access);
363
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);
367
368   GST_INFO ("flush seek");
369
370   seek_flags =
371       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_SEGMENT;
372
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);
377
378   GST_INFO ("set playing state");
379
380   res = gst_element_set_state (bin, GST_STATE_PLAYING);
381   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
382
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);
387
388   GST_INFO ("warm-up sequence done");
389
390   seek_flags &= ~GST_SEEK_FLAG_FLUSH;
391   fail_if (seek_flags & GST_SEEK_FLAG_FLUSH, NULL);
392
393   GST_INFO ("non-flush seek");
394
395   res =
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);
400
401   GST_INFO ("wait for segment done message");
402
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
406    *  screwed up. */
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);
410
411   res = gst_element_set_state (bin, GST_STATE_NULL);
412   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
413
414   /* cleanup */
415   gst_bus_remove_signal_watch (bus);
416   gst_object_unref (bus);
417   gst_object_unref (bin);
418 }
419
420 GST_END_TEST;
421
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)
425 {
426   GstMessage *msg;
427   GstElement *bin, *source, *parser, *sink;
428   gboolean res;
429   GstPad *sinkpad;
430   GstBus *bus;
431   guint seek_flags;
432   gint64 position = GST_CLOCK_TIME_NONE;
433   gint64 playback_duration = GST_CLOCK_TIME_NONE;
434   gint64 start_absolute_time = GST_CLOCK_TIME_NONE;
435
436   GST_INFO
437       ("construct the test pipeline fakesrc ! testparse ! fakesink sync=1");
438
439   bin = gst_pipeline_new ("pipeline");
440   bus = gst_element_get_bus (bin);
441   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
442
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");
446
447   gst_bin_add_many (GST_BIN (bin), source, parser, sink, NULL);
448
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);
453
454   GST_INFO ("configure elements");
455
456   g_object_set (G_OBJECT (source),
457       "format", GST_FORMAT_BYTES,
458       "can-activate-pull", TRUE,
459       "can-activate-push", FALSE,
460       "is-live", FALSE,
461       "sizemax", 65536, "sizetype", 2 /* FAKE_SRC_SIZETYPE_FIXED */ ,
462       NULL);
463
464   /* Sync true is required for this test. */
465   g_object_set (G_OBJECT (sink), "sync", TRUE, NULL);
466
467   GST_INFO ("set paused state");
468
469   res = gst_element_set_state (bin, GST_STATE_PAUSED);
470   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
471
472   GST_INFO ("wait for completion");
473
474   res =
475       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
476       GST_CLOCK_TIME_NONE);
477   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
478
479   GST_INFO ("paused state reached");
480
481   fail_unless (GST_BASE_SRC (source)->random_access);
482
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);
486
487   GST_INFO ("flush seek");
488
489   seek_flags =
490       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_SEGMENT;
491
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);
496
497   GST_INFO ("set playing state");
498
499   res = gst_element_set_state (bin, GST_STATE_PLAYING);
500   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
501
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);
506
507   GST_INFO ("warm-up sequence done");
508
509   seek_flags &= ~GST_SEEK_FLAG_FLUSH;
510   fail_if (seek_flags & GST_SEEK_FLAG_FLUSH, NULL);
511
512   GST_INFO ("non-flush seek");
513
514   start_absolute_time = gst_clock_get_time (GST_ELEMENT_CLOCK (bin));
515
516   res =
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);
521
522   GST_INFO ("wait for segment done message");
523
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");
528
529   gst_message_parse_segment_done (msg, NULL, &position);
530   gst_message_unref (msg);
531
532   GST_INFO ("final position: %" G_GINT64_FORMAT, position);
533
534   fail_unless (position == 2 * GST_SECOND);
535
536   playback_duration =
537       GST_CLOCK_DIFF (start_absolute_time,
538       gst_clock_get_time (GST_ELEMENT_CLOCK (bin)));
539
540   GST_INFO ("test duration: %" G_GINT64_FORMAT, playback_duration);
541
542   fail_unless (playback_duration > GST_SECOND,
543       "playback duration should be near 2 seconds");
544
545   res = gst_element_set_state (bin, GST_STATE_NULL);
546   fail_unless (res != GST_STATE_CHANGE_FAILURE, NULL);
547
548   /* cleanup */
549   gst_bus_remove_signal_watch (bus);
550   gst_object_unref (bus);
551   gst_object_unref (bin);
552 }
553
554 GST_END_TEST;
555
556 static Suite *
557 pipelines_seek_suite (void)
558 {
559   Suite *s = suite_create ("pipelines-seek");
560   TCase *tc_chain = tcase_create ("general");
561
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);
566
567   return s;
568 }
569
570 GST_CHECK_MAIN (pipelines_seek);