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 #ifdef HAVE_VALGRIND
27 # include <valgrind/valgrind.h>
28 #else
29 #define RUNNING_ON_VALGRIND FALSE
30 #endif
31
32 #include <glib/gstdio.h>
33
34 #include <gst/check/gstcheck.h>
35 #include <gst/app/app.h>
36 #include <gst/video/video.h>
37 #include <stdlib.h>
38
39 gchar *tmpdir = NULL;
40 GstClockTime first_ts;
41 GstClockTime last_ts;
42 gdouble current_rate;
43
44 static void
tempdir_setup(void)45 tempdir_setup (void)
46 {
47 const gchar *systmp = g_get_tmp_dir ();
48 tmpdir = g_build_filename (systmp, "splitmux-timecode-test-XXXXXX", NULL);
49 /* Rewrites tmpdir template input: */
50 tmpdir = g_mkdtemp (tmpdir);
51 }
52
53 static void
tempdir_cleanup(void)54 tempdir_cleanup (void)
55 {
56 GDir *d;
57 const gchar *f;
58
59 fail_if (tmpdir == NULL);
60
61 d = g_dir_open (tmpdir, 0, NULL);
62 fail_if (d == NULL);
63
64 while ((f = g_dir_read_name (d)) != NULL) {
65 gchar *fname = g_build_filename (tmpdir, f, NULL);
66 fail_if (g_remove (fname) != 0, "Failed to remove tmp file %s", fname);
67 g_free (fname);
68 }
69 g_dir_close (d);
70
71 fail_if (g_remove (tmpdir) != 0, "Failed to delete tmpdir %s", tmpdir);
72
73 g_free (tmpdir);
74 tmpdir = NULL;
75 }
76
77 static guint
count_files(const gchar * target)78 count_files (const gchar * target)
79 {
80 GDir *d;
81 const gchar *f;
82 guint ret = 0;
83
84 d = g_dir_open (target, 0, NULL);
85 fail_if (d == NULL);
86
87 while ((f = g_dir_read_name (d)) != NULL)
88 ret++;
89 g_dir_close (d);
90
91 return ret;
92 }
93
94 static void
dump_error(GstMessage * msg)95 dump_error (GstMessage * msg)
96 {
97 GError *err = NULL;
98 gchar *dbg_info;
99
100 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
101
102 gst_message_parse_error (msg, &err, &dbg_info);
103
104 g_printerr ("ERROR from element %s: %s\n",
105 GST_OBJECT_NAME (msg->src), err->message);
106 g_printerr ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
107 g_error_free (err);
108 g_free (dbg_info);
109 }
110
111 static GstMessage *
run_pipeline(GstElement * pipeline)112 run_pipeline (GstElement * pipeline)
113 {
114 GstBus *bus = gst_element_get_bus (GST_ELEMENT (pipeline));
115 GstMessage *msg;
116
117 gst_element_set_state (pipeline, GST_STATE_PLAYING);
118 msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
119 gst_element_set_state (pipeline, GST_STATE_NULL);
120
121 gst_object_unref (bus);
122
123 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
124 dump_error (msg);
125
126 return msg;
127 }
128
129 static void
seek_pipeline(GstElement * pipeline,gdouble rate,GstClockTime start,GstClockTime end)130 seek_pipeline (GstElement * pipeline, gdouble rate, GstClockTime start,
131 GstClockTime end)
132 {
133 /* Pause the pipeline, seek to the desired range / rate, wait for PAUSED again, then
134 * clear the tracking vars for start_ts / end_ts */
135 gst_element_set_state (pipeline, GST_STATE_PAUSED);
136 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
137
138 /* specific end time not implemented: */
139 fail_unless (end == GST_CLOCK_TIME_NONE);
140
141 gst_element_seek (pipeline, rate, GST_FORMAT_TIME,
142 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, start,
143 GST_SEEK_TYPE_END, 0);
144
145 /* Wait for the pipeline to preroll again */
146 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
147
148 GST_LOG ("Seeked pipeline. Rate %f time range %" GST_TIME_FORMAT " to %"
149 GST_TIME_FORMAT, rate, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
150
151 /* Clear tracking variables now that the seek is complete */
152 first_ts = last_ts = GST_CLOCK_TIME_NONE;
153 current_rate = rate;
154 };
155
156 static GstFlowReturn
receive_sample(GstAppSink * appsink,gpointer user_data)157 receive_sample (GstAppSink * appsink, gpointer user_data)
158 {
159 GstSample *sample;
160 GstSegment *seg;
161 GstBuffer *buf;
162 GstClockTime start;
163 GstClockTime end;
164
165 g_signal_emit_by_name (appsink, "pull-sample", &sample);
166 fail_unless (sample != NULL);
167
168 seg = gst_sample_get_segment (sample);
169 fail_unless (seg != NULL);
170
171 buf = gst_sample_get_buffer (sample);
172 fail_unless (buf != NULL);
173
174 GST_LOG ("Got buffer %" GST_PTR_FORMAT, buf);
175
176 start = GST_BUFFER_PTS (buf);
177 end = start;
178
179 if (GST_CLOCK_TIME_IS_VALID (start))
180 start = gst_segment_to_stream_time (seg, GST_FORMAT_TIME, start);
181
182 if (GST_CLOCK_TIME_IS_VALID (end)) {
183 if (GST_BUFFER_DURATION_IS_VALID (buf))
184 end += GST_BUFFER_DURATION (buf);
185
186 end = gst_segment_to_stream_time (seg, GST_FORMAT_TIME, end);
187 }
188
189 GST_DEBUG ("Got buffer stream time %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
190 GST_TIME_ARGS (start), GST_TIME_ARGS (end));
191
192 /* Check time is moving in the right direction */
193 if (current_rate > 0) {
194 if (GST_CLOCK_TIME_IS_VALID (first_ts))
195 fail_unless (start >= first_ts,
196 "Timestamps went backward during forward play, %" GST_TIME_FORMAT
197 " < %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
198 GST_TIME_ARGS (first_ts));
199 if (GST_CLOCK_TIME_IS_VALID (last_ts))
200 fail_unless (end >= last_ts,
201 "Timestamps went backward during forward play, %" GST_TIME_FORMAT
202 " < %" GST_TIME_FORMAT, GST_TIME_ARGS (end), GST_TIME_ARGS (last_ts));
203 } else {
204 fail_unless (start <= first_ts,
205 "Timestamps went forward during reverse play, %" GST_TIME_FORMAT " > %"
206 GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (first_ts));
207 fail_unless (end <= last_ts,
208 "Timestamps went forward during reverse play, %" GST_TIME_FORMAT " > %"
209 GST_TIME_FORMAT, GST_TIME_ARGS (end), GST_TIME_ARGS (last_ts));
210 }
211
212 /* update the range of timestamps we've encountered */
213 if (!GST_CLOCK_TIME_IS_VALID (first_ts) || start < first_ts)
214 first_ts = start;
215 if (!GST_CLOCK_TIME_IS_VALID (last_ts) || end > last_ts)
216 last_ts = end;
217
218 gst_sample_unref (sample);
219
220 if (user_data) {
221 guint *num_frame = (guint *) user_data;
222
223 *num_frame = *num_frame + 1;
224 }
225
226 return GST_FLOW_OK;
227 }
228
229 static void
test_playback(const gchar * in_pattern,GstClockTime exp_first_time,GstClockTime exp_last_time,gboolean test_reverse)230 test_playback (const gchar * in_pattern, GstClockTime exp_first_time,
231 GstClockTime exp_last_time, gboolean test_reverse)
232 {
233 GstMessage *msg;
234 GstElement *pipeline;
235 GstElement *appsink;
236 GstElement *fakesink2;
237 GstAppSinkCallbacks callbacks = { NULL };
238 gchar *uri;
239
240 GST_DEBUG ("Playing back files matching %s", in_pattern);
241
242 pipeline = gst_element_factory_make ("playbin", NULL);
243 fail_if (pipeline == NULL);
244
245 appsink = gst_element_factory_make ("appsink", NULL);
246 fail_if (appsink == NULL);
247 g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
248
249 g_object_set (G_OBJECT (pipeline), "video-sink", appsink, NULL);
250 fakesink2 = gst_element_factory_make ("fakesink", NULL);
251 fail_if (fakesink2 == NULL);
252 g_object_set (G_OBJECT (pipeline), "audio-sink", fakesink2, NULL);
253
254 uri = g_strdup_printf ("splitmux://%s", in_pattern);
255
256 g_object_set (G_OBJECT (pipeline), "uri", uri, NULL);
257 g_free (uri);
258
259 callbacks.new_sample = receive_sample;
260 gst_app_sink_set_callbacks (GST_APP_SINK (appsink), &callbacks, NULL, NULL);
261
262 /* test forwards */
263 seek_pipeline (pipeline, 1.0, 0, -1);
264 fail_unless (first_ts == GST_CLOCK_TIME_NONE);
265 msg = run_pipeline (pipeline);
266 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
267 gst_message_unref (msg);
268
269 /* Check we saw the entire range of values */
270 fail_unless (first_ts == exp_first_time,
271 "Expected start of playback range %" GST_TIME_FORMAT ", got %"
272 GST_TIME_FORMAT, GST_TIME_ARGS (exp_first_time),
273 GST_TIME_ARGS (first_ts));
274 fail_unless (last_ts == exp_last_time,
275 "Expected end of playback range %" GST_TIME_FORMAT ", got %"
276 GST_TIME_FORMAT, GST_TIME_ARGS (exp_last_time), GST_TIME_ARGS (last_ts));
277
278 if (test_reverse) {
279 /* Test backwards */
280 seek_pipeline (pipeline, -1.0, 0, -1);
281 msg = run_pipeline (pipeline);
282 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
283 gst_message_unref (msg);
284 /* Check we saw the entire range of values */
285 fail_unless (first_ts == exp_first_time,
286 "Expected start of playback range %" GST_TIME_FORMAT
287 ", got %" GST_TIME_FORMAT, GST_TIME_ARGS (exp_first_time),
288 GST_TIME_ARGS (first_ts));
289 fail_unless (last_ts == exp_last_time,
290 "Expected end of playback range %" GST_TIME_FORMAT
291 ", got %" GST_TIME_FORMAT, GST_TIME_ARGS (exp_last_time),
292 GST_TIME_ARGS (last_ts));
293 }
294
295 gst_object_unref (pipeline);
296 }
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
312 static GstPadProbeReturn
count_upstrea_fku(GstPad * pad,GstPadProbeInfo * info,guint * upstream_fku_count)313 count_upstrea_fku (GstPad * pad, GstPadProbeInfo * info,
314 guint * upstream_fku_count)
315 {
316 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
317
318 switch (GST_EVENT_TYPE (event)) {
319 case GST_EVENT_CUSTOM_UPSTREAM:
320 if (gst_video_event_is_force_key_unit (event))
321 *upstream_fku_count += 1;
322 break;
323 default:
324 break;
325 }
326
327 return GST_PAD_PROBE_OK;
328 }
329
330 static void
splitmuxsink_split_by_keyframe_timecode(gboolean send_keyframe_request,const gchar * maxsize_timecode_string,guint maxsize_timecode_in_sec,guint encoder_key_interval_sec)331 splitmuxsink_split_by_keyframe_timecode (gboolean send_keyframe_request,
332 const gchar * maxsize_timecode_string, guint maxsize_timecode_in_sec,
333 guint encoder_key_interval_sec)
334 {
335 GstMessage *msg;
336 GstElement *pipeline;
337 GstElement *sink;
338 GstElement *enc;
339 GstPad *srcpad;
340 gchar *pipeline_str;
341 gchar *dest_pattern;
342 guint count;
343 guint expected_count;
344 gchar *in_pattern;
345 guint upstream_fku_count = 0;
346 guint expected_fku_count;
347
348 pipeline_str = g_strdup_printf ("splitmuxsink name=splitsink "
349 "max-size-timecode=%s"
350 " send-keyframe-requests=%s muxer=qtmux "
351 "videotestsrc num-buffers=30 ! video/x-raw,width=80,height=64,framerate=5/1 "
352 "! videoconvert ! timecodestamper ! queue ! vp8enc name=enc keyframe-max-dist=%d ! splitsink.video ",
353 maxsize_timecode_string, send_keyframe_request ? "true" : "false",
354 encoder_key_interval_sec ? encoder_key_interval_sec * 5 : 1);
355
356 pipeline = gst_parse_launch (pipeline_str, NULL);
357 g_free (pipeline_str);
358
359 fail_if (pipeline == NULL);
360 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
361 fail_if (sink == NULL);
362 g_signal_connect (sink, "format-location-full",
363 (GCallback) check_format_location, NULL);
364 dest_pattern = g_build_filename (tmpdir, "out%05d.m4v", NULL);
365 g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
366 g_free (dest_pattern);
367 g_object_unref (sink);
368
369 enc = gst_bin_get_by_name (GST_BIN (pipeline), "enc");
370 fail_if (enc == NULL);
371 srcpad = gst_element_get_static_pad (enc, "src");
372 fail_if (srcpad == NULL);
373
374 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
375 (GstPadProbeCallback) count_upstrea_fku, &upstream_fku_count, NULL);
376 gst_object_unref (srcpad);
377 gst_object_unref (enc);
378
379 msg = run_pipeline (pipeline);
380
381 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
382 dump_error (msg);
383 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
384 gst_message_unref (msg);
385
386 gst_object_unref (pipeline);
387
388 count = count_files (tmpdir);
389 expected_count = (6 / maxsize_timecode_in_sec) +
390 (6 % maxsize_timecode_in_sec ? 1 : 0);
391 fail_unless (count == expected_count,
392 "Expected %d output files, got %d", expected_count, count);
393
394 if (!send_keyframe_request) {
395 expected_fku_count = 0;
396 } else {
397 expected_fku_count = count;
398 }
399
400 GST_INFO ("Upstream force keyunit event count %d", upstream_fku_count);
401
402 fail_unless (upstream_fku_count == expected_fku_count,
403 "Expected upstream force keyunit event count %d, got %d",
404 expected_fku_count, upstream_fku_count);
405
406 in_pattern = g_build_filename (tmpdir, "out*.m4v", NULL);
407 /* FIXME: Reverse playback works poorly with multiple video streams
408 * in qtdemux (at least, maybe other demuxers) at the time this was
409 * written, and causes test failures like buffers being output
410 * multiple times by qtdemux as it loops through GOPs. Disable that
411 * for now */
412 test_playback (in_pattern, 0, 6 * GST_SECOND, FALSE);
413 g_free (in_pattern);
414 }
415
GST_START_TEST(test_splitmuxsink_without_keyframe_request_timecode)416 GST_START_TEST (test_splitmuxsink_without_keyframe_request_timecode)
417 {
418 /* This encoding option is intended to produce keyframe per 1 second
419 * but splitmuxsink will split file per 2 second without keyframe request */
420 splitmuxsink_split_by_keyframe_timecode (FALSE, "00:00:02:00", 2, 1);
421 }
422
423 GST_END_TEST;
424
GST_START_TEST(test_splitmuxsink_keyframe_request_timecode)425 GST_START_TEST (test_splitmuxsink_keyframe_request_timecode)
426 {
427 /* This encoding option is intended to produce keyframe per 1 second
428 * but splitmuxsink will request keyframe per 2 seconds. This should produce
429 * 2 seconds long files */
430 splitmuxsink_split_by_keyframe_timecode (TRUE, "00:00:02:00", 2, 1);
431 }
432
433 GST_END_TEST;
434
GST_START_TEST(test_splitmuxsink_keyframe_request_timecode_trailing_small_segment)435 GST_START_TEST
436 (test_splitmuxsink_keyframe_request_timecode_trailing_small_segment) {
437 /* This encoding option is intended to produce keyframe per 1 second
438 * but splitmuxsink will request keyframe per 4 seconds. This should produce
439 * 4 seconds long files */
440 splitmuxsink_split_by_keyframe_timecode (TRUE, "00:00:04:00", 4, 1);
441 }
442
443 GST_END_TEST;
444
GST_START_TEST(test_splitmuxsink_keyframe_request_timecode_all_intra)445 GST_START_TEST (test_splitmuxsink_keyframe_request_timecode_all_intra)
446 {
447 /* This encoding option is intended to produce keyframe for every frame.
448 * This should produce 1 second long files */
449 splitmuxsink_split_by_keyframe_timecode (TRUE, "00:00:01:00", 1, 0);
450 }
451
452 GST_END_TEST;
453
454 static void
count_frames(const gchar * file_name,guint expected_count)455 count_frames (const gchar * file_name, guint expected_count)
456 {
457 GstMessage *msg;
458 GstElement *pipeline;
459 GstElement *appsink;
460 GstElement *fakesink2;
461 GstAppSinkCallbacks callbacks = { NULL };
462 gchar *uri;
463 guint frame_count = 0;
464
465 GST_DEBUG ("Playing back files matching %s", file_name);
466
467 pipeline = gst_element_factory_make ("playbin", NULL);
468 fail_if (pipeline == NULL);
469
470 appsink = gst_element_factory_make ("appsink", NULL);
471 fail_if (appsink == NULL);
472 g_object_set (G_OBJECT (appsink), "sync", FALSE, NULL);
473
474 g_object_set (G_OBJECT (pipeline), "video-sink", appsink, NULL);
475 fakesink2 = gst_element_factory_make ("fakesink", NULL);
476 fail_if (fakesink2 == NULL);
477 g_object_set (G_OBJECT (pipeline), "audio-sink", fakesink2, NULL);
478
479 uri = g_strdup_printf ("file://%s", file_name);
480
481 g_object_set (G_OBJECT (pipeline), "uri", uri, NULL);
482 g_free (uri);
483
484 callbacks.new_sample = receive_sample;
485 gst_app_sink_set_callbacks (GST_APP_SINK (appsink),
486 &callbacks, &frame_count, NULL);
487
488 seek_pipeline (pipeline, 1.0, 0, -1);
489 fail_unless (first_ts == GST_CLOCK_TIME_NONE);
490 msg = run_pipeline (pipeline);
491 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
492 gst_message_unref (msg);
493
494 fail_unless (frame_count == expected_count,
495 "Frame count %u is not equal to expected %u frame count %u",
496 expected_count, frame_count);
497
498 gst_object_unref (pipeline);
499 }
500
501 typedef struct
502 {
503 const gchar *max_timecode;
504 guint num_frame[3];
505 const gchar *fragment_name[3];
506 GstClockTime expected_fku_time[3];
507 guint upstream_fku_count;
508 } TimeCodeTestData;
509
510 static GstPadProbeReturn
count_upstrea_fku_with_data(GstPad * pad,GstPadProbeInfo * info,TimeCodeTestData * data)511 count_upstrea_fku_with_data (GstPad * pad, GstPadProbeInfo * info,
512 TimeCodeTestData * data)
513 {
514 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
515
516 switch (GST_EVENT_TYPE (event)) {
517 case GST_EVENT_CUSTOM_UPSTREAM:
518 if (gst_video_event_is_force_key_unit (event)) {
519 GstClockTime running_time;
520 GstClockTime expected;
521
522 expected = data->expected_fku_time[data->upstream_fku_count];
523
524 gst_video_event_parse_upstream_force_key_unit (event,
525 &running_time, NULL, NULL);
526
527 GST_INFO ("expected fku time %" GST_TIME_FORMAT
528 ", got %" GST_TIME_FORMAT, GST_TIME_ARGS (expected),
529 GST_TIME_ARGS (running_time));
530
531 /* splitmuxsink will request keyframe with slightly earlier timestamp */
532 fail_unless (expected <= running_time + 5 * GST_USECOND);
533 fail_unless (expected >= running_time);
534
535 data->upstream_fku_count++;
536 }
537 break;
538 default:
539 break;
540 }
541
542 return GST_PAD_PROBE_OK;
543 }
544
545 static void
splitmuxsink_split_by_keyframe_timecode_framerate_29_97(gboolean equal_dur,gboolean all_keyframe)546 splitmuxsink_split_by_keyframe_timecode_framerate_29_97 (gboolean equal_dur,
547 gboolean all_keyframe)
548 {
549 GstMessage *msg;
550 GstElement *pipeline;
551 GstElement *sink;
552 GstElement *enc;
553 GstPad *srcpad;
554 gchar *pipeline_str;
555 gchar *dest_pattern;
556 guint count;
557 guint expected_fku_count;
558 TimeCodeTestData data;
559 gint i;
560
561 if (equal_dur) {
562 data.max_timecode = "00:01:00;02";
563 data.num_frame[0] = data.num_frame[1] = 1800;
564 data.expected_fku_time[0] =
565 gst_util_uint64_scale (1800 * GST_SECOND, 1001, 30000);
566 data.expected_fku_time[1] =
567 gst_util_uint64_scale (2 * 1800 * GST_SECOND, 1001, 30000);
568 data.expected_fku_time[2] =
569 gst_util_uint64_scale (3 * 1800 * GST_SECOND, 1001, 30000);
570 } else {
571 data.max_timecode = "00:01:00;00";
572 data.num_frame[0] = 1800;
573 data.num_frame[1] = 1798;
574 data.expected_fku_time[0] =
575 gst_util_uint64_scale (1800 * GST_SECOND, 1001, 30000);
576 data.expected_fku_time[1] =
577 gst_util_uint64_scale ((1800 + 1798) * GST_SECOND, 1001, 30000);
578 data.expected_fku_time[2] =
579 gst_util_uint64_scale ((1800 + 2 * 1798) * GST_SECOND, 1001, 30000);
580 }
581 data.num_frame[2] = 5000 - (data.num_frame[0] + data.num_frame[1]);
582
583 data.fragment_name[0] = "out0.m4v";
584 data.fragment_name[1] = "out1.m4v";
585 data.fragment_name[2] = "out2.m4v";
586 data.upstream_fku_count = 0;
587
588 pipeline_str = g_strdup_printf ("splitmuxsink name=splitsink "
589 "max-size-timecode=%s "
590 "send-keyframe-requests=%s muxer=qtmux "
591 "videotestsrc num-buffers=5000 ! "
592 "video/x-raw,width=80,height=64,framerate=30000/1001 "
593 "! videoconvert ! timecodestamper drop-frame=true ! queue ! "
594 "vp8enc name=enc keyframe-max-dist=%d ! splitsink.video ",
595 data.max_timecode, all_keyframe ? "false" : "true",
596 all_keyframe ? 1 : 5000);
597
598 pipeline = gst_parse_launch (pipeline_str, NULL);
599 g_free (pipeline_str);
600
601 fail_if (pipeline == NULL);
602 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
603 fail_if (sink == NULL);
604 g_signal_connect (sink, "format-location-full",
605 (GCallback) check_format_location, NULL);
606 dest_pattern = g_build_filename (tmpdir, "out%d.m4v", NULL);
607 g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
608 g_free (dest_pattern);
609 g_object_unref (sink);
610
611 enc = gst_bin_get_by_name (GST_BIN (pipeline), "enc");
612 fail_if (enc == NULL);
613 srcpad = gst_element_get_static_pad (enc, "src");
614 fail_if (srcpad == NULL);
615
616 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
617 (GstPadProbeCallback) count_upstrea_fku_with_data, &data, NULL);
618 gst_object_unref (srcpad);
619 gst_object_unref (enc);
620
621 msg = run_pipeline (pipeline);
622
623 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
624 dump_error (msg);
625 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
626 gst_message_unref (msg);
627
628 gst_object_unref (pipeline);
629
630 count = count_files (tmpdir);
631 fail_unless (count == 3, "Expected 3 output files, got %d", count);
632
633 if (all_keyframe) {
634 expected_fku_count = 0;
635 } else {
636 expected_fku_count = count;
637 }
638
639 GST_INFO ("Upstream force keyunit event count %d", data.upstream_fku_count);
640
641 fail_unless (data.upstream_fku_count == expected_fku_count,
642 "Expected upstream force keyunit event count %d, got %d",
643 expected_fku_count, data.upstream_fku_count);
644
645 for (i = 0; i < 3; i++) {
646 gchar *file_name = g_build_filename (tmpdir, data.fragment_name[i], NULL);
647 count_frames (file_name, data.num_frame[i]);
648 g_free (file_name);
649 }
650 }
651
GST_START_TEST(test_splitmuxsink_timecode_framerate_29_97_equal_duration)652 GST_START_TEST (test_splitmuxsink_timecode_framerate_29_97_equal_duration)
653 {
654 splitmuxsink_split_by_keyframe_timecode_framerate_29_97 (TRUE, FALSE);
655 }
656
657 GST_END_TEST;
658
GST_START_TEST(test_splitmuxsink_timecode_framerate_29_97_equal_duration_all_intra)659 GST_START_TEST
660 (test_splitmuxsink_timecode_framerate_29_97_equal_duration_all_intra) {
661 splitmuxsink_split_by_keyframe_timecode_framerate_29_97 (TRUE, TRUE);
662 }
663
664 GST_END_TEST;
665
GST_START_TEST(test_splitmuxsink_timecode_framerate_29_97_not_equal_duration)666 GST_START_TEST (test_splitmuxsink_timecode_framerate_29_97_not_equal_duration)
667 {
668 splitmuxsink_split_by_keyframe_timecode_framerate_29_97 (TRUE, FALSE);
669 }
670
671 GST_END_TEST;
672
GST_START_TEST(test_splitmuxsink_timecode_framerate_29_97_not_equal_duration_all_intra)673 GST_START_TEST
674 (test_splitmuxsink_timecode_framerate_29_97_not_equal_duration_all_intra) {
675 splitmuxsink_split_by_keyframe_timecode_framerate_29_97 (TRUE, TRUE);
676 }
677
678 GST_END_TEST;
679
680 static void
splitmuxsink_timecode_framerate_25(gboolean all_keyframe)681 splitmuxsink_timecode_framerate_25 (gboolean all_keyframe)
682 {
683 GstMessage *msg;
684 GstElement *pipeline;
685 GstElement *sink;
686 GstElement *enc;
687 GstPad *srcpad;
688 gchar *pipeline_str;
689 gchar *dest_pattern;
690 guint count;
691 guint expected_fku_count;
692 TimeCodeTestData data;
693 gint i;
694 guint num_total_frames = 4000;
695
696 data.max_timecode = "00:01:00;00";
697 data.num_frame[0] = 1500;
698 data.num_frame[1] = 1500;
699 data.num_frame[2] =
700 num_total_frames - (data.num_frame[0] + data.num_frame[1]);
701 /* in case of framerate 25/1 with maxsize timecode "00:01:00;00",
702 * all fragments will have equal size */
703 data.expected_fku_time[0] = GST_SECOND * 60;
704 data.expected_fku_time[1] = GST_SECOND * 120;
705 data.expected_fku_time[2] = GST_SECOND * 180;
706
707 data.fragment_name[0] = "out0.m4v";
708 data.fragment_name[1] = "out1.m4v";
709 data.fragment_name[2] = "out2.m4v";
710 data.upstream_fku_count = 0;
711
712 pipeline_str = g_strdup_printf ("splitmuxsink name=splitsink "
713 "max-size-timecode=%s "
714 "send-keyframe-requests=%s muxer=qtmux "
715 "videotestsrc num-buffers=%d ! "
716 "video/x-raw,width=80,height=64,framerate=25/1 "
717 "! videoconvert ! timecodestamper drop-frame=true ! queue ! "
718 "vp8enc name=enc keyframe-max-dist=%d ! splitsink.video ",
719 data.max_timecode, all_keyframe ? "false" : "true", num_total_frames,
720 all_keyframe ? 1 : num_total_frames);
721
722 pipeline = gst_parse_launch (pipeline_str, NULL);
723 g_free (pipeline_str);
724
725 fail_if (pipeline == NULL);
726 sink = gst_bin_get_by_name (GST_BIN (pipeline), "splitsink");
727 fail_if (sink == NULL);
728 g_signal_connect (sink, "format-location-full",
729 (GCallback) check_format_location, NULL);
730 dest_pattern = g_build_filename (tmpdir, "out%d.m4v", NULL);
731 g_object_set (G_OBJECT (sink), "location", dest_pattern, NULL);
732 g_free (dest_pattern);
733 g_object_unref (sink);
734
735 enc = gst_bin_get_by_name (GST_BIN (pipeline), "enc");
736 fail_if (enc == NULL);
737 srcpad = gst_element_get_static_pad (enc, "src");
738 fail_if (srcpad == NULL);
739
740 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
741 (GstPadProbeCallback) count_upstrea_fku_with_data, &data, NULL);
742 gst_object_unref (srcpad);
743 gst_object_unref (enc);
744
745 msg = run_pipeline (pipeline);
746
747 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR)
748 dump_error (msg);
749 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
750 gst_message_unref (msg);
751
752 gst_object_unref (pipeline);
753
754 count = count_files (tmpdir);
755 fail_unless (count == 3, "Expected 3 output files, got %d", count);
756
757 if (all_keyframe) {
758 expected_fku_count = 0;
759 } else {
760 expected_fku_count = count;
761 }
762
763 GST_INFO ("Upstream force keyunit event count %d", data.upstream_fku_count);
764
765 fail_unless (data.upstream_fku_count == expected_fku_count,
766 "Expected upstream force keyunit event count %d, got %d",
767 expected_fku_count, data.upstream_fku_count);
768
769 for (i = 0; i < 3; i++) {
770 gchar *file_name = g_build_filename (tmpdir, data.fragment_name[i], NULL);
771 count_frames (file_name, data.num_frame[i]);
772 g_free (file_name);
773 }
774 }
775
GST_START_TEST(test_splitmuxsink_timecode_framerate_25)776 GST_START_TEST (test_splitmuxsink_timecode_framerate_25)
777 {
778 splitmuxsink_timecode_framerate_25 (TRUE);
779 }
780
781 GST_END_TEST;
782
GST_START_TEST(test_splitmuxsink_timecode_framerate_25_all_intra)783 GST_START_TEST (test_splitmuxsink_timecode_framerate_25_all_intra)
784 {
785 splitmuxsink_timecode_framerate_25 (FALSE);
786 }
787
788 GST_END_TEST;
789
790 static Suite *
splitmuxsinktimecode_suite(void)791 splitmuxsinktimecode_suite (void)
792 {
793 Suite *s = suite_create ("splitmuxsink-timecode");
794 TCase *tc_chain = tcase_create ("general");
795 gboolean have_qtmux, have_vp8, have_timecodestamper;
796
797 /* we assume that if encoder/muxer are there, decoder/demuxer will be a well */
798 have_qtmux = gst_registry_check_feature_version (gst_registry_get (),
799 "qtmux", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
800 have_vp8 = gst_registry_check_feature_version (gst_registry_get (),
801 "vp8enc", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
802 have_timecodestamper =
803 gst_registry_check_feature_version (gst_registry_get (),
804 "timecodestamper", GST_VERSION_MAJOR, GST_VERSION_MINOR, 0);
805
806 suite_add_tcase (s, tc_chain);
807
808 if (have_qtmux && have_vp8 && have_timecodestamper) {
809 tcase_add_checked_fixture (tc_chain, tempdir_setup, tempdir_cleanup);
810 tcase_add_test (tc_chain,
811 test_splitmuxsink_without_keyframe_request_timecode);
812 tcase_add_test (tc_chain, test_splitmuxsink_keyframe_request_timecode);
813 tcase_add_test (tc_chain,
814 test_splitmuxsink_keyframe_request_timecode_trailing_small_segment);
815 tcase_add_test (tc_chain,
816 test_splitmuxsink_keyframe_request_timecode_all_intra);
817 if (!(RUNNING_ON_VALGRIND)) {
818 tcase_add_test (tc_chain,
819 test_splitmuxsink_timecode_framerate_29_97_equal_duration);
820 tcase_add_test (tc_chain,
821 test_splitmuxsink_timecode_framerate_29_97_equal_duration_all_intra);
822 tcase_add_test (tc_chain,
823 test_splitmuxsink_timecode_framerate_29_97_not_equal_duration);
824 tcase_add_test (tc_chain,
825 test_splitmuxsink_timecode_framerate_29_97_not_equal_duration_all_intra);
826 tcase_add_test (tc_chain, test_splitmuxsink_timecode_framerate_25);
827 tcase_add_test (tc_chain,
828 test_splitmuxsink_timecode_framerate_25_all_intra);
829 }
830 } else {
831 GST_INFO
832 ("Skipping tests, missing plugins: vp8enc, mp4mux, or timecodestamper");
833 }
834
835 return s;
836 }
837
838 GST_CHECK_MAIN (splitmuxsinktimecode);
839