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