1 /* GStreamer unit test for splitmuxsrc/sink elements
2  *
3  * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
4  * Copyright (C) 2015 Jan Schmidt <jan@centricular.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 <glib/gstdio.h>
27 
28 #include <gst/check/gstcheck.h>
29 #include <gst/app/app.h>
30 #include <stdlib.h>
31 
32 gchar *tmpdir = NULL;
33 GstClockTime first_ts;
34 GstClockTime last_ts;
35 gdouble current_rate;
36 
37 static void
tempdir_setup(void)38 tempdir_setup (void)
39 {
40   const gchar *systmp = g_get_tmp_dir ();
41   tmpdir = g_build_filename (systmp, "splitmux-test-XXXXXX", NULL);
42   /* Rewrites tmpdir template input: */
43   tmpdir = g_mkdtemp (tmpdir);
44 }
45 
46 static void
tempdir_cleanup(void)47 tempdir_cleanup (void)
48 {
49   GDir *d;
50   const gchar *f;
51 
52   fail_if (tmpdir == NULL);
53 
54   d = g_dir_open (tmpdir, 0, NULL);
55   fail_if (d == NULL);
56 
57   while ((f = g_dir_read_name (d)) != NULL) {
58     gchar *fname = g_build_filename (tmpdir, f, NULL);
59     fail_if (g_remove (fname) != 0, "Failed to remove tmp file %s", fname);
60     g_free (fname);
61   }
62   g_dir_close (d);
63 
64   fail_if (g_remove (tmpdir) != 0, "Failed to delete tmpdir %s", tmpdir);
65 
66   g_free (tmpdir);
67   tmpdir = NULL;
68 }
69 
70 static guint
count_files(const gchar * target)71 count_files (const gchar * target)
72 {
73   GDir *d;
74   const gchar *f;
75   guint ret = 0;
76 
77   d = g_dir_open (target, 0, NULL);
78   fail_if (d == NULL);
79 
80   while ((f = g_dir_read_name (d)) != NULL)
81     ret++;
82   g_dir_close (d);
83 
84   return ret;
85 }
86 
87 static void
dump_error(GstMessage * msg)88 dump_error (GstMessage * msg)
89 {
90   GError *err = NULL;
91   gchar *dbg_info;
92 
93   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
94 
95   gst_message_parse_error (msg, &err, &dbg_info);
96 
97   g_printerr ("ERROR from element %s: %s\n",
98       GST_OBJECT_NAME (msg->src), err->message);
99   g_printerr ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
100   g_error_free (err);
101   g_free (dbg_info);
102 }
103 
104 static GstMessage *
run_pipeline(GstElement * pipeline)105 run_pipeline (GstElement * pipeline)
106 {
107   GstBus *bus = gst_element_get_bus (GST_ELEMENT (pipeline));
108   GstMessage *msg;
109 
110   gst_element_set_state (pipeline, GST_STATE_PLAYING);
111   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
112   gst_element_set_state (pipeline, GST_STATE_NULL);
113 
114   gst_object_unref (bus);
115 
116   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
117     dump_error (msg);
118 
119   return msg;
120 }
121 
122 static void
seek_pipeline(GstElement * pipeline,gdouble rate,GstClockTime start,GstClockTime end)123 seek_pipeline (GstElement * pipeline, gdouble rate, GstClockTime start,
124     GstClockTime end)
125 {
126   /* Pause the pipeline, seek to the desired range / rate, wait for PAUSED again, then
127    * clear the tracking vars for start_ts / end_ts */
128   gst_element_set_state (pipeline, GST_STATE_PAUSED);
129   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
130 
131   /* specific end time not implemented: */
132   fail_unless (end == GST_CLOCK_TIME_NONE);
133 
134   gst_element_seek (pipeline, rate, GST_FORMAT_TIME,
135       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, start,
136       GST_SEEK_TYPE_END, 0);
137 
138   /* Wait for the pipeline to preroll again */
139   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
140 
141   GST_LOG ("Seeked pipeline. Rate %f time range %" GST_TIME_FORMAT " to %"
142       GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
143 
144   /* Clear tracking variables now that the seek is complete */
145   first_ts = last_ts = GST_CLOCK_TIME_NONE;
146   current_rate = rate;
147 };
148 
149 static void
receive_handoff(GstElement * object G_GNUC_UNUSED,GstBuffer * buf,GstPad * arg1 G_GNUC_UNUSED,gpointer user_data G_GNUC_UNUSED)150 receive_handoff (GstElement * object G_GNUC_UNUSED, GstBuffer * buf,
151     GstPad * arg1 G_GNUC_UNUSED, gpointer user_data G_GNUC_UNUSED)
152 {
153   GstClockTime start = GST_BUFFER_TIMESTAMP (buf);
154   GstClockTime end = start;
155 
156   if (GST_BUFFER_DURATION_IS_VALID (buf))
157     end += GST_BUFFER_DURATION (buf);
158 
159   GST_LOG ("Got buffer %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
160       GST_TIME_ARGS (start), GST_TIME_ARGS (end));
161 
162   /* Check time is moving in the right direction */
163   if (current_rate > 0) {
164     if (GST_CLOCK_TIME_IS_VALID (first_ts))
165       fail_unless (start >= first_ts,
166           "Timestamps went backward during forward play, %" GST_TIME_FORMAT
167           " < %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
168           GST_TIME_ARGS (first_ts));
169     if (GST_CLOCK_TIME_IS_VALID (last_ts))
170       fail_unless (end >= last_ts,
171           "Timestamps went backward during forward play, %" GST_TIME_FORMAT
172           " < %" GST_TIME_FORMAT, GST_TIME_ARGS (end), GST_TIME_ARGS (last_ts));
173   } else {
174     fail_unless (start <= first_ts,
175         "Timestamps went forward during reverse play, %" GST_TIME_FORMAT " > %"
176         GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (first_ts));
177     fail_unless (end <= last_ts,
178         "Timestamps went forward during reverse play, %" GST_TIME_FORMAT " > %"
179         GST_TIME_FORMAT, GST_TIME_ARGS (end), GST_TIME_ARGS (last_ts));
180   }
181 
182   /* update the range of timestamps we've encountered */
183   if (!GST_CLOCK_TIME_IS_VALID (first_ts) || start < first_ts)
184     first_ts = start;
185   if (!GST_CLOCK_TIME_IS_VALID (last_ts) || end > last_ts)
186     last_ts = end;
187 }
188 
189 static void
test_playback(const gchar * in_pattern,GstClockTime exp_first_time,GstClockTime exp_last_time)190 test_playback (const gchar * in_pattern, GstClockTime exp_first_time,
191     GstClockTime exp_last_time)
192 {
193   GstMessage *msg;
194   GstElement *pipeline;
195   GstElement *fakesink;
196   GstElement *fakesink2;
197   gchar *uri;
198 
199   pipeline = gst_element_factory_make ("playbin", NULL);
200   fail_if (pipeline == NULL);
201 
202   fakesink = gst_element_factory_make ("fakesink", NULL);
203   fail_if (fakesink == NULL);
204   g_object_set (G_OBJECT (pipeline), "video-sink", fakesink, NULL);
205   fakesink2 = gst_element_factory_make ("fakesink", NULL);
206   fail_if (fakesink2 == NULL);
207   g_object_set (G_OBJECT (pipeline), "audio-sink", fakesink2, NULL);
208 
209   uri = g_strdup_printf ("splitmux://%s", in_pattern);
210 
211   g_object_set (G_OBJECT (pipeline), "uri", uri, NULL);
212   g_free (uri);
213 
214   g_signal_connect (fakesink, "handoff", (GCallback) receive_handoff, NULL);
215   g_object_set (G_OBJECT (fakesink), "signal-handoffs", TRUE, NULL);
216 
217   /* test forwards */
218   seek_pipeline (pipeline, 1.0, 0, -1);
219   fail_unless (first_ts == GST_CLOCK_TIME_NONE);
220   msg = run_pipeline (pipeline);
221   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
222   gst_message_unref (msg);
223 
224   /* Check we saw the entire range of values */
225   fail_unless (first_ts == exp_first_time,
226       "Expected start of playback range 0, got %" GST_TIME_FORMAT,
227       GST_TIME_ARGS (first_ts));
228   fail_unless (last_ts == exp_last_time,
229       "Expected end of playback range 3s, got %" GST_TIME_FORMAT,
230       GST_TIME_ARGS (last_ts));
231 
232   /* Test backwards */
233   seek_pipeline (pipeline, -1.0, 0, -1);
234   msg = run_pipeline (pipeline);
235   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
236   gst_message_unref (msg);
237   /* Check we saw the entire range of values */
238   fail_unless (first_ts == exp_first_time,
239       "Expected start of playback range %" GST_TIME_FORMAT
240       ", got %" GST_TIME_FORMAT, GST_TIME_ARGS (exp_first_time),
241       GST_TIME_ARGS (first_ts));
242   fail_unless (last_ts == exp_last_time,
243       "Expected end of playback range %" GST_TIME_FORMAT
244       ", got %" GST_TIME_FORMAT, GST_TIME_ARGS (exp_last_time),
245       GST_TIME_ARGS (last_ts));
246 
247   gst_object_unref (pipeline);
248 }
249 
GST_START_TEST(test_splitmuxsrc)250 GST_START_TEST (test_splitmuxsrc)
251 {
252   gchar *in_pattern =
253       g_build_filename (GST_TEST_FILES_PATH, "splitvideo*.ogg", NULL);
254   test_playback (in_pattern, 0, 3 * GST_SECOND);
255   g_free (in_pattern);
256 }
257 
258 GST_END_TEST;
259 
260 static gchar **
src_format_location_cb(GstElement * splitmuxsrc,gpointer user_data)261 src_format_location_cb (GstElement * splitmuxsrc, gpointer user_data)
262 {
263   gchar **result = g_malloc0_n (4, sizeof (gchar *));
264   result[0] = g_build_filename (GST_TEST_FILES_PATH, "splitvideo00.ogg", NULL);
265   result[1] = g_build_filename (GST_TEST_FILES_PATH, "splitvideo01.ogg", NULL);
266   result[2] = g_build_filename (GST_TEST_FILES_PATH, "splitvideo02.ogg", NULL);
267   return result;
268 }
269 
GST_START_TEST(test_splitmuxsrc_format_location)270 GST_START_TEST (test_splitmuxsrc_format_location)
271 {
272   GstMessage *msg;
273   GstElement *pipeline;
274   GstElement *src;
275   GError *error = NULL;
276 
277   pipeline = gst_parse_launch ("splitmuxsrc name=splitsrc ! decodebin "
278       "! fakesink", &error);
279   g_assert_no_error (error);
280   fail_if (pipeline == NULL);
281 
282   src = gst_bin_get_by_name (GST_BIN (pipeline), "splitsrc");
283   g_signal_connect (src, "format-location",
284       (GCallback) src_format_location_cb, NULL);
285   g_object_unref (src);
286 
287   msg = run_pipeline (pipeline);
288 
289   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
290     dump_error (msg);
291   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
292   gst_message_unref (msg);
293   gst_object_unref (pipeline);
294 }
295 
296 GST_END_TEST;
297 
298 static gchar *
check_format_location(GstElement * object,guint fragment_id,GstSample * first_sample)299 check_format_location (GstElement * object,
300     guint fragment_id, GstSample * first_sample)
301 {
302   GstBuffer *buf = gst_sample_get_buffer (first_sample);
303 
304   /* Must have a buffer */
305   fail_if (buf == NULL);
306   GST_LOG ("New file - first buffer %" GST_TIME_FORMAT,
307       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
308 
309   return NULL;
310 }
311 
GST_START_TEST(test_splitmuxsink)312 GST_START_TEST (test_splitmuxsink)
313 {
314   GstMessage *msg;
315   GstElement *pipeline;
316   GstElement *sink;
317   GstPad *splitmux_sink_pad;
318   GstPad *enc_src_pad;
319   gchar *dest_pattern;
320   guint count;
321   gchar *in_pattern;
322 
323   /* This pipeline has a small time cutoff - it should start a new file
324    * every GOP, ie 1 second */
325   pipeline =
326       gst_parse_launch
327       ("videotestsrc num-buffers=15 ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
328       " queue ! theoraenc keyframe-force=5 ! splitmuxsink name=splitsink "
329       " max-size-time=1000000 max-size-bytes=1000000 muxer=oggmux", NULL);
330   fail_if (pipeline == NULL);
331   sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
332   fail_if (sink == NULL);
333   g_signal_connect (sink, "format-location-full",
334       (GCallback) check_format_location, NULL);
335   dest_pattern = g_build_filename (tmpdir, "out%05d.ogg", NULL);
336   g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
337   g_free (dest_pattern);
338   g_object_unref (sink);
339 
340   msg = run_pipeline (pipeline);
341 
342   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
343     dump_error (msg);
344   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
345   gst_message_unref (msg);
346 
347   /* unlink manually and relase request pad to ensure that we *can* do that
348    * - https://bugzilla.gnome.org/show_bug.cgi?id=753622 */
349   sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
350   fail_if (sink == NULL);
351   splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
352   fail_if (splitmux_sink_pad == NULL);
353   enc_src_pad = gst_pad_get_peer (splitmux_sink_pad);
354   fail_if (enc_src_pad == NULL);
355   fail_unless (gst_pad_unlink (enc_src_pad, splitmux_sink_pad));
356   gst_object_unref (enc_src_pad);
357   gst_element_release_request_pad (sink, splitmux_sink_pad);
358   gst_object_unref (splitmux_sink_pad);
359   /* at this point the pad must be releaased - try to find it again to verify */
360   splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
361   fail_if (splitmux_sink_pad != NULL);
362   g_object_unref (sink);
363 
364   gst_object_unref (pipeline);
365 
366   count = count_files (tmpdir);
367   fail_unless (count == 3, "Expected 3 output files, got %d", count);
368 
369   in_pattern = g_build_filename (tmpdir, "out*.ogg", NULL);
370   test_playback (in_pattern, 0, 3 * GST_SECOND);
371   g_free (in_pattern);
372 }
373 
374 GST_END_TEST;
375 
GST_START_TEST(test_splitmuxsink_async)376 GST_START_TEST (test_splitmuxsink_async)
377 {
378   GstMessage *msg;
379   GstElement *pipeline;
380   GstElement *sink;
381   GstPad *splitmux_sink_pad;
382   GstPad *enc_src_pad;
383   gchar *dest_pattern;
384   guint count;
385   gchar *in_pattern;
386 
387   pipeline =
388       gst_parse_launch
389       ("videotestsrc num-buffers=15 ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
390       " queue ! theoraenc keyframe-force=5 ! splitmuxsink name=splitsink "
391       " max-size-time=1000000000 async-finalize=true "
392       " muxer-factory=matroskamux audiotestsrc num-buffers=15 samplesperbuffer=9600 ! "
393       " audio/x-raw,rate=48000 ! splitsink.audio_%u", NULL);
394   fail_if (pipeline == NULL);
395   sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
396   fail_if (sink == NULL);
397   g_signal_connect (sink, "format-location-full",
398       (GCallback) check_format_location, NULL);
399   dest_pattern = g_build_filename (tmpdir, "matroska%05d.mkv", NULL);
400   g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
401   g_free (dest_pattern);
402   g_object_unref (sink);
403 
404   msg = run_pipeline (pipeline);
405 
406   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
407     dump_error (msg);
408   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
409   gst_message_unref (msg);
410 
411   /* unlink manually and relase request pad to ensure that we *can* do that
412    * - https://bugzilla.gnome.org/show_bug.cgi?id=753622 */
413   sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
414   fail_if (sink == NULL);
415   splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
416   fail_if (splitmux_sink_pad == NULL);
417   enc_src_pad = gst_pad_get_peer (splitmux_sink_pad);
418   fail_if (enc_src_pad == NULL);
419   fail_unless (gst_pad_unlink (enc_src_pad, splitmux_sink_pad));
420   gst_object_unref (enc_src_pad);
421   gst_element_release_request_pad (sink, splitmux_sink_pad);
422   gst_object_unref (splitmux_sink_pad);
423   /* at this point the pad must be releaased - try to find it again to verify */
424   splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
425   fail_if (splitmux_sink_pad != NULL);
426   g_object_unref (sink);
427 
428   gst_object_unref (pipeline);
429 
430   count = count_files (tmpdir);
431   fail_unless (count == 3, "Expected 3 output files, got %d", count);
432 
433   in_pattern = g_build_filename (tmpdir, "matroska*.mkv", NULL);
434   test_playback (in_pattern, 0, 3 * GST_SECOND);
435   g_free (in_pattern);
436 }
437 
438 GST_END_TEST;
439 
440 static GstPadProbeReturn
intercept_stream_start(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)441 intercept_stream_start (GstPad * pad, GstPadProbeInfo * info,
442     gpointer user_data)
443 {
444   GstEvent *event = gst_pad_probe_info_get_event (info);
445 
446   if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
447     GstStreamFlags flags;
448     event = gst_event_make_writable (event);
449     gst_event_parse_stream_flags (event, &flags);
450     gst_event_set_stream_flags (event, flags | GST_STREAM_FLAG_SPARSE);
451     GST_PAD_PROBE_INFO_DATA (info) = event;
452   }
453 
454   return GST_PAD_PROBE_OK;
455 }
456 
457 static GstFlowReturn
new_sample_verify_continuous_timestamps(GstAppSink * appsink,gpointer user_data)458 new_sample_verify_continuous_timestamps (GstAppSink * appsink,
459     gpointer user_data)
460 {
461   GstSample *sample;
462   GstBuffer *buffer;
463   GstClockTime *prev_ts = user_data;
464   GstClockTime new_ts;
465 
466   sample = gst_app_sink_pull_sample (appsink);
467   buffer = gst_sample_get_buffer (sample);
468 
469   new_ts = GST_BUFFER_PTS (buffer);
470   if (GST_CLOCK_TIME_IS_VALID (*prev_ts)) {
471     fail_unless (*prev_ts < new_ts,
472         "%s: prev_ts (%" GST_TIME_FORMAT ") >= new_ts (%" GST_TIME_FORMAT ")",
473         GST_OBJECT_NAME (appsink), GST_TIME_ARGS (*prev_ts),
474         GST_TIME_ARGS (new_ts));
475   }
476 
477   *prev_ts = new_ts;
478   gst_sample_unref (sample);
479   return GST_FLOW_OK;
480 }
481 
482 static GstFlowReturn
new_sample_verify_1sec_offset(GstAppSink * appsink,gpointer user_data)483 new_sample_verify_1sec_offset (GstAppSink * appsink, gpointer user_data)
484 {
485   GstSample *sample;
486   GstBuffer *buffer;
487   GstClockTime *prev_ts = user_data;
488   GstClockTime new_ts;
489 
490   sample = gst_app_sink_pull_sample (appsink);
491   buffer = gst_sample_get_buffer (sample);
492 
493   new_ts = GST_BUFFER_PTS (buffer);
494   if (GST_CLOCK_TIME_IS_VALID (*prev_ts)) {
495     fail_unless (new_ts > (*prev_ts + 900 * GST_MSECOND),
496         "%s: prev_ts (%" GST_TIME_FORMAT ") + 0.9s >= new_ts (%"
497         GST_TIME_FORMAT ")", GST_OBJECT_NAME (appsink),
498         GST_TIME_ARGS (*prev_ts), GST_TIME_ARGS (new_ts));
499   }
500 
501   *prev_ts = new_ts;
502   gst_sample_unref (sample);
503   return GST_FLOW_OK;
504 }
505 
506 /* https://bugzilla.gnome.org/show_bug.cgi?id=761086 */
GST_START_TEST(test_splitmuxsrc_sparse_streams)507 GST_START_TEST (test_splitmuxsrc_sparse_streams)
508 {
509   GstElement *pipeline;
510   GstElement *element;
511   gchar *dest_pattern;
512   GstElement *appsrc;
513   GstPad *appsrc_src;
514   GstBus *bus;
515   GstMessage *msg;
516   gint i;
517 
518   /* generate files */
519 
520   /* in this test, we have 5sec of data with files split at 1sec intervals */
521   pipeline =
522       gst_parse_launch
523       ("videotestsrc num-buffers=75 !"
524       "  video/x-raw,width=80,height=64,framerate=15/1 !"
525       "  theoraenc keyframe-force=5 ! splitmuxsink name=splitsink"
526       "    max-size-time=1000000000 muxer=matroskamux"
527       " audiotestsrc num-buffers=100 samplesperbuffer=1024 !"
528       "  audio/x-raw,rate=20000 ! vorbisenc ! splitsink.audio_%u"
529       " appsrc name=appsrc format=time caps=text/x-raw,format=utf8 !"
530       "  splitsink.subtitle_%u", NULL);
531   fail_if (pipeline == NULL);
532 
533   element = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
534   fail_if (element == NULL);
535   dest_pattern = g_build_filename (tmpdir, "out%05d.ogg", NULL);
536   g_object_set (G_OBJECT (element), "location", dest_pattern, NULL);
537   g_clear_pointer (&dest_pattern, g_free);
538   g_clear_object (&element);
539 
540   appsrc = gst_bin_get_by_name (GST_BIN (pipeline), "appsrc");
541   fail_if (appsrc == NULL);
542 
543   /* add the SPARSE flag on the stream-start event of the subtitle stream */
544   appsrc_src = gst_element_get_static_pad (appsrc, "src");
545   fail_if (appsrc_src == NULL);
546   gst_pad_add_probe (appsrc_src, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
547       intercept_stream_start, NULL, NULL);
548   g_clear_object (&appsrc_src);
549 
550   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
551 
552   gst_element_set_state (pipeline, GST_STATE_PLAYING);
553 
554   /* push subtitles, one per second, starting from t=100ms */
555   for (i = 0; i < 5; i++) {
556     GstBuffer *buffer = gst_buffer_new_allocate (NULL, 5, NULL);
557     GstMapInfo info;
558 
559     gst_buffer_map (buffer, &info, GST_MAP_WRITE);
560     strcpy ((char *) info.data, "test");
561     gst_buffer_unmap (buffer, &info);
562 
563     GST_BUFFER_PTS (buffer) = i * GST_SECOND + 100 * GST_MSECOND;
564     GST_BUFFER_DTS (buffer) = GST_BUFFER_PTS (buffer);
565 
566     fail_if (gst_app_src_push_buffer (GST_APP_SRC (appsrc), buffer)
567         != GST_FLOW_OK);
568   }
569   fail_if (gst_app_src_end_of_stream (GST_APP_SRC (appsrc)) != GST_FLOW_OK);
570 
571   msg = gst_bus_timed_pop_filtered (bus, 5 * GST_SECOND, GST_MESSAGE_EOS);
572   g_clear_pointer (&msg, gst_message_unref);
573 
574   gst_element_set_state (pipeline, GST_STATE_NULL);
575 
576   g_clear_object (&appsrc);
577   g_clear_object (&bus);
578   g_clear_object (&pipeline);
579 
580   /* read and verify */
581 
582   pipeline =
583       gst_parse_launch
584       ("splitmuxsrc name=splitsrc"
585       " splitsrc. ! theoradec ! appsink name=vsink sync=false emit-signals=true"
586       " splitsrc. ! vorbisdec ! appsink name=asink sync=false emit-signals=true"
587       " splitsrc. ! text/x-raw ! appsink name=tsink sync=false emit-signals=true",
588       NULL);
589   fail_if (pipeline == NULL);
590 
591   element = gst_bin_get_by_name (GST_BIN (pipeline), "splitsrc");
592   fail_if (element == NULL);
593   dest_pattern = g_build_filename (tmpdir, "out*.ogg", NULL);
594   g_object_set (G_OBJECT (element), "location", dest_pattern, NULL);
595   g_clear_pointer (&dest_pattern, g_free);
596   g_clear_object (&element);
597 
598   {
599     GstClockTime vsink_prev_ts = GST_CLOCK_TIME_NONE;
600     GstClockTime asink_prev_ts = GST_CLOCK_TIME_NONE;
601     GstClockTime tsink_prev_ts = GST_CLOCK_TIME_NONE;
602 
603     /* verify that timestamps are continuously increasing for audio + video.
604      * if we hit bug 761086, timestamps will jump about -900ms after switching
605      * to a new part, because this is the difference between the last subtitle
606      * pts and the last audio/video pts */
607     element = gst_bin_get_by_name (GST_BIN (pipeline), "vsink");
608     g_signal_connect (element, "new-sample",
609         (GCallback) new_sample_verify_continuous_timestamps, &vsink_prev_ts);
610     g_clear_object (&element);
611 
612     element = gst_bin_get_by_name (GST_BIN (pipeline), "asink");
613     g_signal_connect (element, "new-sample",
614         (GCallback) new_sample_verify_continuous_timestamps, &asink_prev_ts);
615     g_clear_object (&element);
616 
617     /* also verify that subtitle timestamps are increasing by about 1s.
618      * if we hit bug 761086, timestamps will increase by exactly 100ms instead,
619      * because this is the relative difference between a part's start time
620      * (remember a new part starts every 1sec) and the subtitle's pts in that
621      * part, which will be added to the max_ts of the previous part, which
622      * equals the last subtitle's pts (and should not!) */
623     element = gst_bin_get_by_name (GST_BIN (pipeline), "tsink");
624     g_signal_connect (element, "new-sample",
625         (GCallback) new_sample_verify_1sec_offset, &tsink_prev_ts);
626     g_clear_object (&element);
627 
628     msg = run_pipeline (pipeline);
629   }
630 
631   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
632     dump_error (msg);
633   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
634 
635   g_clear_pointer (&msg, gst_message_unref);
636   g_clear_object (&pipeline);
637 }
638 
639 GST_END_TEST;
640 
641 struct CapsChangeData
642 {
643   guint count;
644   GstElement *cf;
645 };
646 
647 static GstPadProbeReturn
switch_caps(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)648 switch_caps (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
649 {
650   struct CapsChangeData *data = (struct CapsChangeData *) (user_data);
651 
652   if (data->count == 4) {
653     GST_INFO ("Saw 5 buffers to the encoder. Switching caps");
654     gst_util_set_object_arg (G_OBJECT (data->cf), "caps",
655         "video/x-raw,width=160,height=128,framerate=10/1");
656   }
657   data->count++;
658   return GST_PAD_PROBE_OK;
659 }
660 
GST_START_TEST(test_splitmuxsrc_caps_change)661 GST_START_TEST (test_splitmuxsrc_caps_change)
662 {
663   GstMessage *msg;
664   GstElement *pipeline;
665   GstElement *sink;
666   GstElement *cf;
667   GstPad *sinkpad;
668   gchar *dest_pattern;
669   guint count;
670   gchar *in_pattern;
671   struct CapsChangeData data;
672 
673   /* This test creates a new file only by changing the caps, which
674    * qtmux will reject (for now - if qtmux starts supporting caps
675    * changes, this test will break and need fixing/disabling */
676   pipeline =
677       gst_parse_launch
678       ("videotestsrc num-buffers=10 !"
679       "  capsfilter name=c caps=video/x-raw,width=80,height=64,framerate=10/1 !"
680       "  jpegenc ! splitmuxsink name=splitsink muxer=qtmux", NULL);
681   fail_if (pipeline == NULL);
682   sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
683   fail_if (sink == NULL);
684   g_signal_connect (sink, "format-location-full",
685       (GCallback) check_format_location, NULL);
686   dest_pattern = g_build_filename (tmpdir, "out%05d.mp4", NULL);
687   g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
688   g_free (dest_pattern);
689   g_object_unref (sink);
690 
691   cf = gst_bin_get_by_name (GST_BIN (pipeline), "c");
692   sinkpad = gst_element_get_static_pad (cf, "sink");
693 
694   data.cf = cf;
695   data.count = 0;
696 
697   gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_BUFFER,
698       switch_caps, &data, NULL);
699 
700   gst_object_unref (sinkpad);
701   gst_object_unref (cf);
702 
703   msg = run_pipeline (pipeline);
704 
705   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
706     dump_error (msg);
707   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
708   gst_message_unref (msg);
709 
710   gst_object_unref (pipeline);
711 
712   count = count_files (tmpdir);
713   fail_unless (count == 2, "Expected 2 output files, got %d", count);
714 
715   in_pattern = g_build_filename (tmpdir, "out*.mp4", NULL);
716   test_playback (in_pattern, 0, GST_SECOND);
717   g_free (in_pattern);
718 }
719 
720 GST_END_TEST;
721 
GST_START_TEST(test_splitmuxsrc_robust_mux)722 GST_START_TEST (test_splitmuxsrc_robust_mux)
723 {
724   GstMessage *msg;
725   GstElement *pipeline;
726   GstElement *sink;
727   gchar *dest_pattern;
728   gchar *in_pattern;
729 
730   /* This test creates a new file only by changing the caps, which
731    * qtmux will reject (for now - if qtmux starts supporting caps
732    * changes, this test will break and need fixing/disabling */
733   pipeline =
734       gst_parse_launch
735       ("videotestsrc num-buffers=10 !"
736       "  video/x-raw,width=80,height=64,framerate=10/1 !"
737       "  jpegenc ! splitmuxsink name=splitsink muxer=\"qtmux reserved-bytes-per-sec=200 reserved-moov-update-period=100000000 \" max-size-time=500000000 use-robust-muxing=true",
738       NULL);
739   fail_if (pipeline == NULL);
740   sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
741   fail_if (sink == NULL);
742   g_signal_connect (sink, "format-location-full",
743       (GCallback) check_format_location, NULL);
744   dest_pattern = g_build_filename (tmpdir, "out%05d.mp4", NULL);
745   g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
746   g_free (dest_pattern);
747   g_object_unref (sink);
748 
749   msg = run_pipeline (pipeline);
750 
751   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
752     dump_error (msg);
753   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
754   gst_message_unref (msg);
755 
756   gst_object_unref (pipeline);
757 
758   /* Unlike other tests, we don't check an explicit file size, because the overflow detection
759    * can be racy (depends on exactly when buffers get handed to the muxer and when it updates the
760    * reserved duration property. All we care about is that the muxing didn't fail because space ran out */
761 
762   in_pattern = g_build_filename (tmpdir, "out*.mp4", NULL);
763   test_playback (in_pattern, 0, GST_SECOND);
764   g_free (in_pattern);
765 }
766 
767 GST_END_TEST;
768 
769 /* For verifying bug https://bugzilla.gnome.org/show_bug.cgi?id=762893 */
GST_START_TEST(test_splitmuxsink_reuse_simple)770 GST_START_TEST (test_splitmuxsink_reuse_simple)
771 {
772   GstElement *sink;
773   GstPad *pad;
774 
775   sink = gst_element_factory_make ("splitmuxsink", NULL);
776   pad = gst_element_get_request_pad (sink, "video");
777   fail_unless (pad != NULL);
778   g_object_set (sink, "location", "/dev/null", NULL);
779 
780   fail_unless (gst_element_set_state (sink,
781           GST_STATE_PLAYING) == GST_STATE_CHANGE_ASYNC);
782   fail_unless (gst_element_set_state (sink,
783           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
784   fail_unless (gst_element_set_state (sink,
785           GST_STATE_PLAYING) == GST_STATE_CHANGE_ASYNC);
786   fail_unless (gst_element_set_state (sink,
787           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
788 
789   gst_element_release_request_pad (sink, pad);
790   gst_object_unref (pad);
791   gst_object_unref (sink);
792 }
793 
794 GST_END_TEST;
795 
796 static Suite *
splitmux_suite(void)797 splitmux_suite (void)
798 {
799   Suite *s = suite_create ("splitmux");
800   TCase *tc_chain = tcase_create ("general");
801   TCase *tc_chain_basic = tcase_create ("basic");
802   TCase *tc_chain_complex = tcase_create ("complex");
803   TCase *tc_chain_mp4_jpeg = tcase_create ("caps_change");
804   gboolean have_theora, have_ogg, have_vorbis, have_matroska, have_qtmux,
805       have_jpeg;
806 
807   /* we assume that if encoder/muxer are there, decoder/demuxer will be a well */
808   have_theora = gst_registry_check_feature_version (gst_registry_get (),
809       "theoraenc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
810   have_ogg = gst_registry_check_feature_version (gst_registry_get (),
811       "oggmux", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
812   have_vorbis = gst_registry_check_feature_version (gst_registry_get (),
813       "vorbisenc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
814   have_matroska = gst_registry_check_feature_version (gst_registry_get (),
815       "matroskamux", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
816   have_qtmux = gst_registry_check_feature_version (gst_registry_get (),
817       "qtmux", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
818   have_jpeg = gst_registry_check_feature_version (gst_registry_get (),
819       "jpegenc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
820 
821   suite_add_tcase (s, tc_chain);
822   suite_add_tcase (s, tc_chain_basic);
823   suite_add_tcase (s, tc_chain_complex);
824   suite_add_tcase (s, tc_chain_mp4_jpeg);
825 
826   tcase_add_test (tc_chain_basic, test_splitmuxsink_reuse_simple);
827 
828   if (have_theora && have_ogg) {
829     tcase_add_checked_fixture (tc_chain, tempdir_setup, tempdir_cleanup);
830 
831     tcase_add_test (tc_chain, test_splitmuxsrc);
832     tcase_add_test (tc_chain, test_splitmuxsrc_format_location);
833     tcase_add_test (tc_chain, test_splitmuxsink);
834 
835     if (have_matroska && have_vorbis) {
836       tcase_add_checked_fixture (tc_chain_complex, tempdir_setup,
837           tempdir_cleanup);
838 
839       tcase_add_test (tc_chain_complex, test_splitmuxsrc_sparse_streams);
840       tcase_add_test (tc_chain, test_splitmuxsink_async);
841     } else {
842       GST_INFO ("Skipping tests, missing plugins: matroska and/or vorbis");
843     }
844   } else {
845     GST_INFO ("Skipping tests, missing plugins: theora and/or ogg");
846   }
847 
848 
849   if (have_qtmux && have_jpeg) {
850     tcase_add_checked_fixture (tc_chain_mp4_jpeg, tempdir_setup,
851         tempdir_cleanup);
852     tcase_add_test (tc_chain_mp4_jpeg, test_splitmuxsrc_caps_change);
853     tcase_add_test (tc_chain_mp4_jpeg, test_splitmuxsrc_robust_mux);
854   } else {
855     GST_INFO ("Skipping tests, missing plugins: jpegenc or mp4mux");
856   }
857   return s;
858 }
859 
860 GST_CHECK_MAIN (splitmux);
861