1 /* GStreamer unit test for splitmuxsink 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)151 receive_sample (GstAppSink * appsink, gpointer user_data)
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 if (user_data) {
215 guint *num_frame = (guint *) user_data;
216
217 *num_frame = *num_frame + 1;
218 }
219
220 return GST_FLOW_OK;
221 }
222
223 static void
test_playback(const gchar * in_pattern,GstClockTime exp_first_time,GstClockTime exp_last_time,gboolean test_reverse)224 test_playback (const gchar * in_pattern, GstClockTime exp_first_time,
225 GstClockTime exp_last_time, gboolean test_reverse)
226 {
227 GstMessage *msg;
228 GstElement *pipeline;
229 GstElement *appsink;
230 GstElement *fakesink2;
231 GstAppSinkCallbacks callbacks = { NULL };
232 gchar *uri;
233
234 GST_DEBUG ("Playing back files matching %s", in_pattern);
235
236 pipeline = gst_element_factory_make ("playbin", NULL);
237 fail_if (pipeline == NULL);
238
239 appsink = gst_element_factory_make ("appsink", NULL);
240 fail_if (appsink == NULL);
241 g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
242
243 g_object_set (G_OBJECT (pipeline), "video-sink", appsink, NULL);
244 fakesink2 = gst_element_factory_make ("fakesink", NULL);
245 fail_if (fakesink2 == NULL);
246 g_object_set (G_OBJECT (pipeline), "audio-sink", fakesink2, NULL);
247
248 uri = g_strdup_printf ("splitmux://%s", in_pattern);
249
250 g_object_set (G_OBJECT (pipeline), "uri", uri, NULL);
251 g_free (uri);
252
253 callbacks.new_sample = receive_sample;
254 gst_app_sink_set_callbacks (GST_APP_SINK (appsink), &callbacks, NULL, NULL);
255
256 /* test forwards */
257 seek_pipeline (pipeline, 1.0, 0, -1);
258 fail_unless (first_ts == GST_CLOCK_TIME_NONE);
259 msg = run_pipeline (pipeline);
260 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
261 gst_message_unref (msg);
262
263 /* Check we saw the entire range of values */
264 fail_unless (first_ts == exp_first_time,
265 "Expected start of playback range %" GST_TIME_FORMAT ", got %"
266 GST_TIME_FORMAT, GST_TIME_ARGS (exp_first_time),
267 GST_TIME_ARGS (first_ts));
268 fail_unless (last_ts == exp_last_time,
269 "Expected end of playback range %" GST_TIME_FORMAT ", got %"
270 GST_TIME_FORMAT, GST_TIME_ARGS (exp_last_time), GST_TIME_ARGS (last_ts));
271
272 if (test_reverse) {
273 /* Test backwards */
274 seek_pipeline (pipeline, -1.0, 0, -1);
275 msg = run_pipeline (pipeline);
276 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
277 gst_message_unref (msg);
278 /* Check we saw the entire range of values */
279 fail_unless (first_ts == exp_first_time,
280 "Expected start of playback range %" GST_TIME_FORMAT
281 ", got %" GST_TIME_FORMAT, GST_TIME_ARGS (exp_first_time),
282 GST_TIME_ARGS (first_ts));
283 fail_unless (last_ts == exp_last_time,
284 "Expected end of playback range %" GST_TIME_FORMAT
285 ", got %" GST_TIME_FORMAT, GST_TIME_ARGS (exp_last_time),
286 GST_TIME_ARGS (last_ts));
287 }
288
289 gst_object_unref (pipeline);
290 }
291
292 struct splitmux_location_state
293 {
294 GstElement *splitmuxsink;
295 gboolean got_format_location;
296 gboolean fragment_opened;
297 gchar *current_location;
298 };
299
300 static gchar *
check_format_location(GstElement * object,guint fragment_id,GstSample * first_sample,struct splitmux_location_state * location_state)301 check_format_location (GstElement * object,
302 guint fragment_id, GstSample * first_sample,
303 struct splitmux_location_state *location_state)
304 {
305 GstBuffer *buf = gst_sample_get_buffer (first_sample);
306
307 /* Must have a buffer */
308 fail_if (buf == NULL);
309 GST_LOG ("New file - first buffer %" GST_TIME_FORMAT,
310 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
311
312 if (location_state) {
313 fail_unless (location_state->got_format_location == FALSE,
314 "Got format-location signal twice without an intervening splitmuxsink-fragment-closed");
315 location_state->got_format_location = TRUE;
316 }
317
318 return NULL;
319 }
320
321 static GstBusSyncReply
bus_sync_handler(GstBus * bus,GstMessage * message,struct splitmux_location_state * location_state)322 bus_sync_handler (GstBus * bus, GstMessage * message,
323 struct splitmux_location_state *location_state)
324 {
325 switch (message->type) {
326 case GST_MESSAGE_ELEMENT:
327 {
328 const GstStructure *s = gst_message_get_structure (message);
329 if (message->src == GST_OBJECT_CAST (location_state->splitmuxsink)) {
330 if (gst_structure_has_name (s, "splitmuxsink-fragment-opened")) {
331 const gchar *location = gst_structure_get_string (s, "location");
332 fail_unless (location != NULL);
333 fail_unless (location_state->got_format_location == TRUE,
334 "Failed to get format-location before fragment start");
335 fail_unless (location_state->fragment_opened == FALSE);
336 location_state->fragment_opened = TRUE;
337
338 /* The location must be different to last time */
339 fail_unless (location_state->current_location == NULL
340 || !g_str_equal (location_state->current_location, location));
341 g_free (location_state->current_location);
342 location_state->current_location = g_strdup (location);
343
344 } else if (gst_structure_has_name (s, "splitmuxsink-fragment-closed")) {
345 fail_unless (location_state->got_format_location == TRUE);
346 fail_unless (location_state->fragment_opened == TRUE);
347 location_state->got_format_location = FALSE; /* We need another format-location before the next open */
348 location_state->fragment_opened = FALSE;
349 }
350 }
351 break;
352 }
353 default:
354 break;
355 }
356
357 return GST_BUS_PASS;
358 }
359
GST_START_TEST(test_splitmuxsink)360 GST_START_TEST (test_splitmuxsink)
361 {
362 GstMessage *msg;
363 GstElement *pipeline;
364 GstElement *sink;
365 GstPad *splitmux_sink_pad;
366 GstPad *enc_src_pad;
367 gchar *dest_pattern;
368 guint count;
369 gchar *in_pattern;
370 struct splitmux_location_state location_state = { NULL, FALSE, FALSE, NULL };
371 GstBus *bus;
372
373 /* This pipeline has a small time cutoff - it should start a new file
374 * every GOP, ie 1 second */
375 pipeline =
376 gst_parse_launch
377 ("videotestsrc num-buffers=15 ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
378 " queue ! theoraenc keyframe-force=5 ! splitmuxsink name=splitsink "
379 " max-size-time=1000000 max-size-bytes=1000000 muxer=oggmux", NULL);
380 fail_if (pipeline == NULL);
381 location_state.splitmuxsink = sink =
382 gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
383 fail_if (sink == NULL);
384 g_signal_connect (sink, "format-location-full",
385 (GCallback) check_format_location, &location_state);
386
387 dest_pattern = g_build_filename (tmpdir, "out%05d.ogg", NULL);
388 g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
389 g_free (dest_pattern);
390 g_object_unref (sink);
391
392 bus = gst_element_get_bus (pipeline);
393 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
394 &location_state, NULL);
395 gst_object_unref (bus);
396
397 msg = run_pipeline (pipeline);
398
399 /* Clean up the location state */
400 g_free (location_state.current_location);
401
402 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
403 dump_error (msg);
404 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
405 gst_message_unref (msg);
406
407 /* unlink manually and release request pad to ensure that we *can* do that
408 * - https://bugzilla.gnome.org/show_bug.cgi?id=753622 */
409 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
410 fail_if (sink == NULL);
411 splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
412 fail_if (splitmux_sink_pad == NULL);
413 enc_src_pad = gst_pad_get_peer (splitmux_sink_pad);
414 fail_if (enc_src_pad == NULL);
415 fail_unless (gst_pad_unlink (enc_src_pad, splitmux_sink_pad));
416 gst_object_unref (enc_src_pad);
417 gst_element_release_request_pad (sink, splitmux_sink_pad);
418 gst_object_unref (splitmux_sink_pad);
419 /* at this point the pad must be released - try to find it again to verify */
420 splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
421 fail_if (splitmux_sink_pad != NULL);
422 g_object_unref (sink);
423
424 gst_object_unref (pipeline);
425
426 count = count_files (tmpdir);
427 fail_unless (count == 3, "Expected 3 output files, got %d", count);
428
429 in_pattern = g_build_filename (tmpdir, "out*.ogg", NULL);
430 test_playback (in_pattern, 0, 3 * GST_SECOND, TRUE);
431 g_free (in_pattern);
432 }
433
434 GST_END_TEST;
435
GST_START_TEST(test_splitmuxsink_clean_failure)436 GST_START_TEST (test_splitmuxsink_clean_failure)
437 {
438 GstMessage *msg;
439 GstElement *pipeline;
440 GstElement *sink, *fakesink;
441
442 /* This pipeline has a small time cutoff - it should start a new file
443 * every GOP, ie 1 second */
444 pipeline =
445 gst_parse_launch
446 ("videotestsrc horizontal-speed=2 is-live=true ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
447 " queue ! theoraenc keyframe-force=5 ! splitmuxsink name=splitsink "
448 " max-size-time=1000000 max-size-bytes=1000000 muxer=oggmux", NULL);
449 fail_if (pipeline == NULL);
450 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
451 fail_if (sink == NULL);
452
453 fakesink = gst_element_factory_make ("fakesink", "fakesink-fail");
454 fail_if (fakesink == NULL);
455
456 /* Trigger an error on READY->PAUSED */
457 g_object_set (fakesink, "state-error", 2, NULL);
458 g_object_set (sink, "sink", fakesink, NULL);
459 gst_object_unref (sink);
460
461 msg = run_pipeline (pipeline);
462
463 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
464 gst_message_unref (msg);
465
466 fail_unless (gst_element_set_state (pipeline,
467 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
468 gst_object_unref (pipeline);
469 }
470
471 GST_END_TEST;
472
GST_START_TEST(test_splitmuxsink_multivid)473 GST_START_TEST (test_splitmuxsink_multivid)
474 {
475 GstMessage *msg;
476 GstElement *pipeline;
477 GstElement *sink;
478 gchar *dest_pattern;
479 guint count;
480 gchar *in_pattern;
481
482 /* This pipeline should start a new file every GOP, ie 1 second,
483 * driven by the primary video stream and with 2 auxiliary video streams */
484 pipeline =
485 gst_parse_launch
486 ("splitmuxsink name=splitsink "
487 " max-size-time=1000000 max-size-bytes=1000000 muxer=qtmux "
488 "videotestsrc num-buffers=15 ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
489 " queue ! vp8enc keyframe-max-dist=5 ! splitsink.video "
490 "videotestsrc num-buffers=15 pattern=snow ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
491 " queue ! vp8enc keyframe-max-dist=6 ! splitsink.video_aux_0 "
492 "videotestsrc num-buffers=15 pattern=ball ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
493 " queue ! vp8enc keyframe-max-dist=8 ! splitsink.video_aux_1 ", NULL);
494 fail_if (pipeline == NULL);
495 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
496 fail_if (sink == NULL);
497 g_signal_connect (sink, "format-location-full",
498 (GCallback) check_format_location, NULL);
499 dest_pattern = g_build_filename (tmpdir, "out%05d.m4v", NULL);
500 g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
501 g_free (dest_pattern);
502 g_object_unref (sink);
503
504 msg = run_pipeline (pipeline);
505
506 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
507 dump_error (msg);
508 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
509 gst_message_unref (msg);
510
511 gst_object_unref (pipeline);
512
513 count = count_files (tmpdir);
514 fail_unless (count == 3, "Expected 3 output files, got %d", count);
515
516 in_pattern = g_build_filename (tmpdir, "out*.m4v", NULL);
517 /* FIXME: Reverse playback works poorly with multiple video streams
518 * in qtdemux (at least, maybe other demuxers) at the time this was
519 * written, and causes test failures like buffers being output
520 * multiple times by qtdemux as it loops through GOPs. Disable that
521 * for now */
522 test_playback (in_pattern, 0, 3 * GST_SECOND, FALSE);
523 g_free (in_pattern);
524 }
525
526 GST_END_TEST;
527
GST_START_TEST(test_splitmuxsink_async)528 GST_START_TEST (test_splitmuxsink_async)
529 {
530 GstMessage *msg;
531 GstElement *pipeline;
532 GstElement *sink;
533 GstPad *splitmux_sink_pad;
534 GstPad *enc_src_pad;
535 gchar *dest_pattern;
536 guint count;
537 gchar *in_pattern;
538
539 pipeline =
540 gst_parse_launch
541 ("videotestsrc num-buffers=15 ! video/x-raw,width=80,height=64,framerate=5/1 ! videoconvert !"
542 " queue ! theoraenc keyframe-force=5 ! splitmuxsink name=splitsink "
543 " max-size-time=1000000000 async-finalize=true "
544 " muxer-factory=matroskamux audiotestsrc num-buffers=15 samplesperbuffer=9600 ! "
545 " audio/x-raw,rate=48000 ! splitsink.audio_%u", NULL);
546 fail_if (pipeline == NULL);
547 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
548 fail_if (sink == NULL);
549 g_signal_connect (sink, "format-location-full",
550 (GCallback) check_format_location, NULL);
551 dest_pattern = g_build_filename (tmpdir, "matroska%05d.mkv", NULL);
552 g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
553 g_free (dest_pattern);
554 g_object_unref (sink);
555
556 msg = run_pipeline (pipeline);
557
558 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
559 dump_error (msg);
560 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
561 gst_message_unref (msg);
562
563 /* unlink manually and release request pad to ensure that we *can* do that
564 * - https://bugzilla.gnome.org/show_bug.cgi?id=753622 */
565 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
566 fail_if (sink == NULL);
567 splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
568 fail_if (splitmux_sink_pad == NULL);
569 enc_src_pad = gst_pad_get_peer (splitmux_sink_pad);
570 fail_if (enc_src_pad == NULL);
571 fail_unless (gst_pad_unlink (enc_src_pad, splitmux_sink_pad));
572 gst_object_unref (enc_src_pad);
573 gst_element_release_request_pad (sink, splitmux_sink_pad);
574 gst_object_unref (splitmux_sink_pad);
575 /* at this point the pad must be released - try to find it again to verify */
576 splitmux_sink_pad = gst_element_get_static_pad (sink, "video");
577 fail_if (splitmux_sink_pad != NULL);
578 g_object_unref (sink);
579
580 gst_object_unref (pipeline);
581
582 count = count_files (tmpdir);
583 fail_unless (count == 3, "Expected 3 output files, got %d", count);
584
585 in_pattern = g_build_filename (tmpdir, "matroska*.mkv", NULL);
586 test_playback (in_pattern, 0, 3 * GST_SECOND, TRUE);
587 g_free (in_pattern);
588 }
589
590 GST_END_TEST;
591
592 /* For verifying bug https://bugzilla.gnome.org/show_bug.cgi?id=762893 */
GST_START_TEST(test_splitmuxsink_reuse_simple)593 GST_START_TEST (test_splitmuxsink_reuse_simple)
594 {
595 GstElement *sink;
596 GstPad *pad;
597
598 sink = gst_element_factory_make ("splitmuxsink", NULL);
599 pad = gst_element_request_pad_simple (sink, "video");
600 fail_unless (pad != NULL);
601 g_object_set (sink, "location", "/dev/null", NULL);
602
603 fail_unless (gst_element_set_state (sink,
604 GST_STATE_PLAYING) == GST_STATE_CHANGE_ASYNC);
605 fail_unless (gst_element_set_state (sink,
606 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
607 fail_unless (gst_element_set_state (sink,
608 GST_STATE_PLAYING) == GST_STATE_CHANGE_ASYNC);
609 fail_unless (gst_element_set_state (sink,
610 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
611
612 gst_element_release_request_pad (sink, pad);
613 gst_object_unref (pad);
614 gst_object_unref (sink);
615 }
616
617 GST_END_TEST;
618
GST_START_TEST(test_splitmuxsink_muxer_pad_map)619 GST_START_TEST (test_splitmuxsink_muxer_pad_map)
620 {
621 GstElement *sink, *muxer;
622 GstPad *muxpad;
623 GstPad *pad1 = NULL, *pad2 = NULL;
624 GstStructure *pad_map;
625
626 pad_map = gst_structure_new ("x-pad-map",
627 "video", G_TYPE_STRING, "video_100",
628 "audio_0", G_TYPE_STRING, "audio_101", NULL);
629
630 muxer = gst_element_factory_make ("qtmux", NULL);
631 fail_if (muxer == NULL);
632 sink = gst_element_factory_make ("splitmuxsink", NULL);
633 fail_if (sink == NULL);
634
635 g_object_set (sink, "muxer", muxer, "muxer-pad-map", pad_map, NULL);
636 gst_structure_free (pad_map);
637
638 pad1 = gst_element_request_pad_simple (sink, "video");
639 fail_unless (g_str_equal ("video", GST_PAD_NAME (pad1)));
640 muxpad = gst_element_get_static_pad (muxer, "video_100");
641 fail_unless (muxpad != NULL);
642 gst_object_unref (muxpad);
643
644 pad2 = gst_element_request_pad_simple (sink, "audio_0");
645 fail_unless (g_str_equal ("audio_0", GST_PAD_NAME (pad2)));
646 muxpad = gst_element_get_static_pad (muxer, "audio_101");
647 fail_unless (muxpad != NULL);
648 gst_object_unref (muxpad);
649
650 g_object_set (sink, "location", "/dev/null", NULL);
651
652 fail_unless (gst_element_set_state (sink,
653 GST_STATE_PLAYING) == GST_STATE_CHANGE_ASYNC);
654 fail_unless (gst_element_set_state (sink,
655 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
656
657 gst_element_release_request_pad (sink, pad1);
658 gst_object_unref (pad1);
659 gst_element_release_request_pad (sink, pad2);
660 gst_object_unref (pad2);
661 gst_object_unref (sink);
662 }
663
664 GST_END_TEST;
665
666 static GstPadProbeReturn
count_upstrea_fku(GstPad * pad,GstPadProbeInfo * info,guint * upstream_fku_count)667 count_upstrea_fku (GstPad * pad, GstPadProbeInfo * info,
668 guint * upstream_fku_count)
669 {
670 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
671
672 switch (GST_EVENT_TYPE (event)) {
673 case GST_EVENT_CUSTOM_UPSTREAM:
674 if (gst_video_event_is_force_key_unit (event))
675 *upstream_fku_count += 1;
676 break;
677 default:
678 break;
679 }
680
681 return GST_PAD_PROBE_OK;
682 }
683
684 static void
splitmuxsink_split_by_keyframe(gboolean send_keyframe_request,guint max_size_time_sec,guint encoder_key_interval_sec)685 splitmuxsink_split_by_keyframe (gboolean send_keyframe_request,
686 guint max_size_time_sec, guint encoder_key_interval_sec)
687 {
688 GstMessage *msg;
689 GstElement *pipeline;
690 GstElement *sink;
691 GstElement *enc;
692 GstPad *srcpad;
693 gchar *pipeline_str;
694 gchar *dest_pattern;
695 guint count;
696 guint expected_count;
697 gchar *in_pattern;
698 guint upstream_fku_count = 0;
699 guint expected_fku_count;
700
701 pipeline_str = g_strdup_printf ("splitmuxsink name=splitsink "
702 "max-size-time=%" G_GUINT64_FORMAT
703 " send-keyframe-requests=%s muxer=qtmux "
704 "videotestsrc num-buffers=30 ! video/x-raw,width=80,height=64,framerate=5/1 "
705 "! videoconvert ! queue ! vp8enc name=enc keyframe-max-dist=%d ! splitsink.video ",
706 max_size_time_sec * GST_SECOND, send_keyframe_request ? "true" : "false",
707 encoder_key_interval_sec * 5);
708
709 pipeline = gst_parse_launch (pipeline_str, NULL);
710 g_free (pipeline_str);
711
712 fail_if (pipeline == NULL);
713 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
714 fail_if (sink == NULL);
715 g_signal_connect (sink, "format-location-full",
716 (GCallback) check_format_location, NULL);
717 dest_pattern = g_build_filename (tmpdir, "out%05d.m4v", NULL);
718 g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
719 g_free (dest_pattern);
720 g_object_unref (sink);
721
722 enc = gst_bin_get_by_name (GST_BIN (pipeline), "enc");
723 fail_if (enc == NULL);
724 srcpad = gst_element_get_static_pad (enc, "src");
725 fail_if (srcpad == NULL);
726
727 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
728 (GstPadProbeCallback) count_upstrea_fku, &upstream_fku_count, NULL);
729 gst_object_unref (srcpad);
730 gst_object_unref (enc);
731
732 msg = run_pipeline (pipeline);
733
734 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
735 dump_error (msg);
736 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
737 gst_message_unref (msg);
738
739 gst_object_unref (pipeline);
740
741 count = count_files (tmpdir);
742 expected_count = 6 / max_size_time_sec;
743 fail_unless (count == expected_count,
744 "Expected %d output files, got %d", expected_count, count);
745
746 if (!send_keyframe_request) {
747 expected_fku_count = 0;
748 } else {
749 expected_fku_count = count;
750 }
751
752 GST_INFO ("Upstream force keyunit event count %d", upstream_fku_count);
753
754 fail_unless (upstream_fku_count == expected_fku_count,
755 "Expected upstream force keyunit event count %d, got %d",
756 expected_fku_count, upstream_fku_count);
757
758 in_pattern = g_build_filename (tmpdir, "out*.m4v", NULL);
759 /* FIXME: Reverse playback works poorly with multiple video streams
760 * in qtdemux (at least, maybe other demuxers) at the time this was
761 * written, and causes test failures like buffers being output
762 * multiple times by qtdemux as it loops through GOPs. Disable that
763 * for now */
764 test_playback (in_pattern, 0, 6 * GST_SECOND, FALSE);
765 g_free (in_pattern);
766 }
767
GST_START_TEST(test_splitmuxsink_without_keyframe_request)768 GST_START_TEST (test_splitmuxsink_without_keyframe_request)
769 {
770 /* This encoding option is intended to produce keyframe per 1 seconds
771 * but splitmuxsink will split file per 2 second without keyframe request */
772 splitmuxsink_split_by_keyframe (FALSE, 2, 1);
773 }
774
775 GST_END_TEST;
776
GST_START_TEST(test_splitmuxsink_keyframe_request)777 GST_START_TEST (test_splitmuxsink_keyframe_request)
778 {
779 /* This encoding option is intended to produce keyframe per 2 seconds
780 * and splitmuxsink will request keyframe per 2 seconds as well.
781 * This should produce 2 seconds long files */
782 splitmuxsink_split_by_keyframe (TRUE, 2, 2);
783 }
784
785 GST_END_TEST;
786
GST_START_TEST(test_splitmuxsink_keyframe_request_more)787 GST_START_TEST (test_splitmuxsink_keyframe_request_more)
788 {
789 /* This encoding option is intended to produce keyframe per 2 seconds
790 * but splitmuxsink will request keyframe per 1 second. This should produce
791 * 1 second long files */
792 splitmuxsink_split_by_keyframe (TRUE, 1, 2);
793 }
794
795 GST_END_TEST;
796
GST_START_TEST(test_splitmuxsink_keyframe_request_less)797 GST_START_TEST (test_splitmuxsink_keyframe_request_less)
798 {
799 /* This encoding option is intended to produce keyframe per 1 second
800 * but splitmuxsink will request keyframe per 2 seconds. This should produce
801 * 2 seconds long files */
802 splitmuxsink_split_by_keyframe (TRUE, 2, 1);
803 }
804
805 GST_END_TEST;
806
807 static Suite *
splitmuxsink_suite(void)808 splitmuxsink_suite (void)
809 {
810 Suite *s = suite_create ("splitmuxsink");
811 TCase *tc_chain = tcase_create ("general");
812 TCase *tc_chain_basic = tcase_create ("basic");
813 TCase *tc_chain_complex = tcase_create ("complex");
814 TCase *tc_chain_mp4_jpeg = tcase_create ("caps_change");
815 TCase *tc_chain_keyframe_request = tcase_create ("keyframe_request");
816 gboolean have_theora, have_ogg, have_vorbis, have_matroska, have_qtmux,
817 have_jpeg, have_vp8;
818
819 /* we assume that if encoder/muxer are there, decoder/demuxer will be a well */
820 have_theora = gst_registry_check_feature_version (gst_registry_get (),
821 "theoraenc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
822 have_ogg = gst_registry_check_feature_version (gst_registry_get (),
823 "oggmux", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
824 have_vorbis = gst_registry_check_feature_version (gst_registry_get (),
825 "vorbisenc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
826 have_matroska = gst_registry_check_feature_version (gst_registry_get (),
827 "matroskamux", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
828 have_qtmux = gst_registry_check_feature_version (gst_registry_get (),
829 "qtmux", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
830 have_jpeg = gst_registry_check_feature_version (gst_registry_get (),
831 "jpegenc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
832 have_vp8 = gst_registry_check_feature_version (gst_registry_get (),
833 "vp8enc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
834
835 suite_add_tcase (s, tc_chain);
836 suite_add_tcase (s, tc_chain_basic);
837 suite_add_tcase (s, tc_chain_complex);
838 suite_add_tcase (s, tc_chain_mp4_jpeg);
839 suite_add_tcase (s, tc_chain_keyframe_request);
840
841 tcase_add_test (tc_chain_basic, test_splitmuxsink_reuse_simple);
842
843 if (have_theora && have_ogg) {
844 tcase_add_checked_fixture (tc_chain, tempdir_setup, tempdir_cleanup);
845
846 tcase_add_test (tc_chain, test_splitmuxsink);
847 tcase_add_test (tc_chain, test_splitmuxsink_clean_failure);
848
849 if (have_matroska && have_vorbis) {
850 tcase_add_checked_fixture (tc_chain_complex, tempdir_setup,
851 tempdir_cleanup);
852
853 tcase_add_test (tc_chain, test_splitmuxsink_async);
854 } else {
855 GST_INFO ("Skipping tests, missing plugins: matroska and/or vorbis");
856 }
857 } else {
858 GST_INFO ("Skipping tests, missing plugins: theora and/or ogg");
859 }
860
861
862 if (have_qtmux && have_jpeg) {
863 tcase_add_checked_fixture (tc_chain_mp4_jpeg, tempdir_setup,
864 tempdir_cleanup);
865 tcase_add_test (tc_chain_mp4_jpeg, test_splitmuxsink_muxer_pad_map);
866 } else {
867 GST_INFO ("Skipping tests, missing plugins: jpegenc or mp4mux");
868 }
869
870 if (have_qtmux && have_vp8) {
871 tcase_add_checked_fixture (tc_chain_keyframe_request, tempdir_setup,
872 tempdir_cleanup);
873 tcase_add_test (tc_chain_keyframe_request, test_splitmuxsink_multivid);
874 tcase_add_test (tc_chain_keyframe_request,
875 test_splitmuxsink_without_keyframe_request);
876 tcase_add_test (tc_chain_keyframe_request,
877 test_splitmuxsink_keyframe_request);
878 tcase_add_test (tc_chain_keyframe_request,
879 test_splitmuxsink_keyframe_request_more);
880 tcase_add_test (tc_chain_keyframe_request,
881 test_splitmuxsink_keyframe_request_less);
882 } else {
883 GST_INFO ("Skipping tests, missing plugins: vp8enc or mp4mux");
884 }
885
886 return s;
887 }
888
889 GST_CHECK_MAIN (splitmuxsink);
890