1 /* GStreamer
2  *
3  * unit test for videmixer
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6  * Copyright (C) <2013> Thibault Saunier <thibault.saunier@collabora.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 
28 #ifdef HAVE_VALGRIND
29 # include <valgrind/valgrind.h>
30 #endif
31 
32 #include <gst/check/gstcheck.h>
33 #include <gst/check/gstconsistencychecker.h>
34 #include <gst/base/gstbasesrc.h>
35 
36 #define VIDEO_CAPS_STRING               \
37     "video/x-raw, "                 \
38     "width = (int) 320, "               \
39     "height = (int) 240, "              \
40     "framerate = (fraction) 25/1 , "    \
41     "format = (string) I420"
42 
43 static GMainLoop *main_loop;
44 
45 /* make sure downstream gets a CAPS event before buffers are sent */
GST_START_TEST(test_caps)46 GST_START_TEST (test_caps)
47 {
48   GstElement *pipeline, *src, *videomixer, *sink;
49   GstStateChangeReturn state_res;
50   GstCaps *caps;
51   GstPad *pad;
52 
53   /* build pipeline */
54   pipeline = gst_pipeline_new ("pipeline");
55 
56   src = gst_element_factory_make ("videotestsrc", "src1");
57   videomixer = gst_element_factory_make ("videomixer", "videomixer");
58   sink = gst_element_factory_make ("fakesink", "sink");
59   gst_bin_add_many (GST_BIN (pipeline), src, videomixer, sink, NULL);
60 
61   fail_unless (gst_element_link_many (src, videomixer, sink, NULL));
62 
63   /* prepare playing */
64   state_res = gst_element_set_state (pipeline, GST_STATE_PAUSED);
65   fail_unless_equals_int (state_res, GST_STATE_CHANGE_ASYNC);
66 
67   /* wait for preroll */
68   state_res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
69   fail_unless_equals_int (state_res, GST_STATE_CHANGE_SUCCESS);
70 
71   /* check caps on fakesink */
72   pad = gst_element_get_static_pad (sink, "sink");
73   caps = gst_pad_get_current_caps (pad);
74   fail_unless (caps != NULL);
75   gst_caps_unref (caps);
76   gst_object_unref (pad);
77 
78   gst_element_set_state (pipeline, GST_STATE_NULL);
79   gst_object_unref (pipeline);
80 }
81 
82 GST_END_TEST;
83 
84 static void
message_received(GstBus * bus,GstMessage * message,GstPipeline * bin)85 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
86 {
87   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
88       GST_MESSAGE_SRC (message), message);
89 
90   switch (message->type) {
91     case GST_MESSAGE_EOS:
92       g_main_loop_quit (main_loop);
93       break;
94     case GST_MESSAGE_WARNING:{
95       GError *gerror;
96       gchar *debug;
97 
98       gst_message_parse_warning (message, &gerror, &debug);
99       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
100       g_error_free (gerror);
101       g_free (debug);
102       break;
103     }
104     case GST_MESSAGE_ERROR:{
105       GError *gerror;
106       gchar *debug;
107 
108       gst_message_parse_error (message, &gerror, &debug);
109       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
110       g_error_free (gerror);
111       g_free (debug);
112       g_main_loop_quit (main_loop);
113       break;
114     }
115     default:
116       break;
117   }
118 }
119 
120 
121 static GstFormat format = GST_FORMAT_UNDEFINED;
122 static gint64 position = -1;
123 
124 static void
test_event_message_received(GstBus * bus,GstMessage * message,GstPipeline * bin)125 test_event_message_received (GstBus * bus, GstMessage * message,
126     GstPipeline * bin)
127 {
128   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
129       GST_MESSAGE_SRC (message), message);
130 
131   switch (message->type) {
132     case GST_MESSAGE_SEGMENT_DONE:
133       gst_message_parse_segment_done (message, &format, &position);
134       GST_INFO ("received segment_done : %" G_GINT64_FORMAT, position);
135       g_main_loop_quit (main_loop);
136       break;
137     default:
138       g_assert_not_reached ();
139       break;
140   }
141 }
142 
143 
GST_START_TEST(test_event)144 GST_START_TEST (test_event)
145 {
146   GstElement *bin, *src1, *src2, *videomixer, *sink;
147   GstBus *bus;
148   GstEvent *seek_event;
149   GstStateChangeReturn state_res;
150   gboolean res;
151   GstPad *srcpad, *sinkpad;
152   GstStreamConsistency *chk_1, *chk_2, *chk_3;
153 
154   GST_INFO ("preparing test");
155 
156   /* build pipeline */
157   bin = gst_pipeline_new ("pipeline");
158   bus = gst_element_get_bus (bin);
159   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
160 
161   src1 = gst_element_factory_make ("videotestsrc", "src1");
162   src2 = gst_element_factory_make ("videotestsrc", "src2");
163   videomixer = gst_element_factory_make ("videomixer", "videomixer");
164   sink = gst_element_factory_make ("fakesink", "sink");
165   gst_bin_add_many (GST_BIN (bin), src1, src2, videomixer, sink, NULL);
166 
167   res = gst_element_link (src1, videomixer);
168   fail_unless (res == TRUE, NULL);
169   res = gst_element_link (src2, videomixer);
170   fail_unless (res == TRUE, NULL);
171   res = gst_element_link (videomixer, sink);
172   fail_unless (res == TRUE, NULL);
173 
174   srcpad = gst_element_get_static_pad (videomixer, "src");
175   chk_3 = gst_consistency_checker_new (srcpad);
176   gst_object_unref (srcpad);
177 
178   /* create consistency checkers for the pads */
179   srcpad = gst_element_get_static_pad (src1, "src");
180   chk_1 = gst_consistency_checker_new (srcpad);
181   sinkpad = gst_pad_get_peer (srcpad);
182   gst_consistency_checker_add_pad (chk_3, sinkpad);
183   gst_object_unref (sinkpad);
184   gst_object_unref (srcpad);
185 
186   srcpad = gst_element_get_static_pad (src2, "src");
187   chk_2 = gst_consistency_checker_new (srcpad);
188   sinkpad = gst_pad_get_peer (srcpad);
189   gst_consistency_checker_add_pad (chk_3, sinkpad);
190   gst_object_unref (sinkpad);
191   gst_object_unref (srcpad);
192 
193   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
194       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
195       GST_SEEK_TYPE_SET, (GstClockTime) 0,
196       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
197 
198   format = GST_FORMAT_UNDEFINED;
199   position = -1;
200 
201   main_loop = g_main_loop_new (NULL, FALSE);
202   g_signal_connect (bus, "message::segment-done",
203       (GCallback) test_event_message_received, bin);
204   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
205   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
206   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
207 
208   GST_INFO ("starting test");
209 
210   /* prepare playing */
211   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
212   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
213 
214   /* wait for completion */
215   state_res = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
216   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
217 
218   res = gst_element_send_event (bin, seek_event);
219   fail_unless (res == TRUE, NULL);
220 
221   /* run pipeline */
222   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
223   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
224 
225   GST_INFO ("running main loop");
226   g_main_loop_run (main_loop);
227 
228   state_res = gst_element_set_state (bin, GST_STATE_NULL);
229   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
230 
231   ck_assert_int_eq (position, 2 * GST_SECOND);
232 
233   /* cleanup */
234   g_main_loop_unref (main_loop);
235   gst_consistency_checker_free (chk_1);
236   gst_consistency_checker_free (chk_2);
237   gst_consistency_checker_free (chk_3);
238   gst_bus_remove_signal_watch (bus);
239   gst_object_unref (bus);
240   gst_object_unref (bin);
241 }
242 
243 GST_END_TEST;
244 
245 static guint play_count = 0;
246 static GstEvent *play_seek_event = NULL;
247 
248 static void
test_play_twice_message_received(GstBus * bus,GstMessage * message,GstPipeline * bin)249 test_play_twice_message_received (GstBus * bus, GstMessage * message,
250     GstPipeline * bin)
251 {
252   gboolean res;
253   GstStateChangeReturn state_res;
254 
255   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
256       GST_MESSAGE_SRC (message), message);
257 
258   switch (message->type) {
259     case GST_MESSAGE_SEGMENT_DONE:
260       play_count++;
261       if (play_count == 1) {
262         state_res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_READY);
263         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
264 
265         /* prepare playing again */
266         state_res = gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PAUSED);
267         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
268 
269         /* wait for completion */
270         state_res =
271             gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
272             GST_CLOCK_TIME_NONE);
273         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
274 
275         res = gst_element_send_event (GST_ELEMENT (bin),
276             gst_event_ref (play_seek_event));
277         fail_unless (res == TRUE, NULL);
278 
279         state_res =
280             gst_element_set_state (GST_ELEMENT (bin), GST_STATE_PLAYING);
281         ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
282       } else {
283         g_main_loop_quit (main_loop);
284       }
285       break;
286     default:
287       g_assert_not_reached ();
288       break;
289   }
290 }
291 
292 
GST_START_TEST(test_play_twice)293 GST_START_TEST (test_play_twice)
294 {
295   GstElement *bin, *src1, *src2, *videomixer, *sink;
296   GstBus *bus;
297   gboolean res;
298   GstStateChangeReturn state_res;
299   GstPad *srcpad;
300   GstStreamConsistency *consist;
301 
302   GST_INFO ("preparing test");
303 
304   /* build pipeline */
305   bin = gst_pipeline_new ("pipeline");
306   bus = gst_element_get_bus (bin);
307   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
308 
309   src1 = gst_element_factory_make ("videotestsrc", "src1");
310   src2 = gst_element_factory_make ("videotestsrc", "src2");
311   videomixer = gst_element_factory_make ("videomixer", "videomixer");
312   sink = gst_element_factory_make ("fakesink", "sink");
313   gst_bin_add_many (GST_BIN (bin), src1, src2, videomixer, sink, NULL);
314 
315   res = gst_element_link (src1, videomixer);
316   fail_unless (res == TRUE, NULL);
317   res = gst_element_link (src2, videomixer);
318   fail_unless (res == TRUE, NULL);
319   res = gst_element_link (videomixer, sink);
320   fail_unless (res == TRUE, NULL);
321 
322   srcpad = gst_element_get_static_pad (videomixer, "src");
323   consist = gst_consistency_checker_new (srcpad);
324   gst_object_unref (srcpad);
325 
326   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
327       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
328       GST_SEEK_TYPE_SET, (GstClockTime) 0,
329       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
330 
331   play_count = 0;
332 
333   main_loop = g_main_loop_new (NULL, FALSE);
334   g_signal_connect (bus, "message::segment-done",
335       (GCallback) test_play_twice_message_received, bin);
336   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
337   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
338   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
339 
340   GST_INFO ("starting test");
341 
342   /* prepare playing */
343   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
344   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
345 
346   /* wait for completion */
347   state_res =
348       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
349       GST_CLOCK_TIME_NONE);
350   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
351 
352   res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
353   fail_unless (res == TRUE, NULL);
354 
355   GST_INFO ("seeked");
356 
357   /* run pipeline */
358   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
359   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
360 
361   g_main_loop_run (main_loop);
362 
363   state_res = gst_element_set_state (bin, GST_STATE_NULL);
364   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
365 
366   ck_assert_int_eq (play_count, 2);
367 
368   /* cleanup */
369   g_main_loop_unref (main_loop);
370   gst_consistency_checker_free (consist);
371   gst_event_unref (play_seek_event);
372   gst_bus_remove_signal_watch (bus);
373   gst_object_unref (bus);
374   gst_object_unref (bin);
375 }
376 
377 GST_END_TEST;
378 
GST_START_TEST(test_play_twice_then_add_and_play_again)379 GST_START_TEST (test_play_twice_then_add_and_play_again)
380 {
381   GstElement *bin, *src1, *src2, *src3, *videomixer, *sink;
382   GstBus *bus;
383   gboolean res;
384   GstStateChangeReturn state_res;
385   gint i;
386   GstPad *srcpad;
387   GstStreamConsistency *consist;
388 
389   GST_INFO ("preparing test");
390 
391   /* build pipeline */
392   bin = gst_pipeline_new ("pipeline");
393   bus = gst_element_get_bus (bin);
394   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
395 
396   src1 = gst_element_factory_make ("videotestsrc", "src1");
397   src2 = gst_element_factory_make ("videotestsrc", "src2");
398   videomixer = gst_element_factory_make ("videomixer", "videomixer");
399   sink = gst_element_factory_make ("fakesink", "sink");
400   gst_bin_add_many (GST_BIN (bin), src1, src2, videomixer, sink, NULL);
401 
402   srcpad = gst_element_get_static_pad (videomixer, "src");
403   consist = gst_consistency_checker_new (srcpad);
404   gst_object_unref (srcpad);
405 
406   res = gst_element_link (src1, videomixer);
407   fail_unless (res == TRUE, NULL);
408   res = gst_element_link (src2, videomixer);
409   fail_unless (res == TRUE, NULL);
410   res = gst_element_link (videomixer, sink);
411   fail_unless (res == TRUE, NULL);
412 
413   play_seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
414       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
415       GST_SEEK_TYPE_SET, (GstClockTime) 0,
416       GST_SEEK_TYPE_SET, (GstClockTime) 2 * GST_SECOND);
417 
418   main_loop = g_main_loop_new (NULL, FALSE);
419   g_signal_connect (bus, "message::segment-done",
420       (GCallback) test_play_twice_message_received, bin);
421   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
422   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
423   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
424 
425   /* run it twice */
426   for (i = 0; i < 2; i++) {
427     play_count = 0;
428 
429     GST_INFO ("starting test-loop %d", i);
430 
431     /* prepare playing */
432     state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
433     ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
434 
435     /* wait for completion */
436     state_res =
437         gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
438         GST_CLOCK_TIME_NONE);
439     ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
440 
441     res = gst_element_send_event (bin, gst_event_ref (play_seek_event));
442     fail_unless (res == TRUE, NULL);
443 
444     GST_INFO ("seeked");
445 
446     /* run pipeline */
447     state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
448     ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
449 
450     g_main_loop_run (main_loop);
451 
452     state_res = gst_element_set_state (bin, GST_STATE_READY);
453     ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
454 
455     ck_assert_int_eq (play_count, 2);
456 
457     /* plug another source */
458     if (i == 0) {
459       src3 = gst_element_factory_make ("videotestsrc", "src3");
460       gst_bin_add (GST_BIN (bin), src3);
461 
462       res = gst_element_link (src3, videomixer);
463       fail_unless (res == TRUE, NULL);
464     }
465 
466     gst_consistency_checker_reset (consist);
467   }
468 
469   state_res = gst_element_set_state (bin, GST_STATE_NULL);
470   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
471 
472   /* cleanup */
473   g_main_loop_unref (main_loop);
474   gst_event_unref (play_seek_event);
475   gst_consistency_checker_free (consist);
476   gst_bus_remove_signal_watch (bus);
477   gst_object_unref (bus);
478   gst_object_unref (bin);
479 }
480 
481 GST_END_TEST;
482 
483 /* check if adding pads work as expected */
GST_START_TEST(test_add_pad)484 GST_START_TEST (test_add_pad)
485 {
486   GstElement *bin, *src1, *src2, *videomixer, *sink;
487   GstBus *bus;
488   GstPad *srcpad;
489   gboolean res;
490   GstStateChangeReturn state_res;
491 
492   GST_INFO ("preparing test");
493 
494   /* build pipeline */
495   bin = gst_pipeline_new ("pipeline");
496   bus = gst_element_get_bus (bin);
497   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
498 
499   src1 = gst_element_factory_make ("videotestsrc", "src1");
500   g_object_set (src1, "num-buffers", 4, NULL);
501   src2 = gst_element_factory_make ("videotestsrc", "src2");
502   /* one buffer less, we connect with 1 buffer of delay */
503   g_object_set (src2, "num-buffers", 3, NULL);
504   videomixer = gst_element_factory_make ("videomixer", "videomixer");
505   sink = gst_element_factory_make ("fakesink", "sink");
506   gst_bin_add_many (GST_BIN (bin), src1, videomixer, sink, NULL);
507 
508   res = gst_element_link (src1, videomixer);
509   fail_unless (res == TRUE, NULL);
510   res = gst_element_link (videomixer, sink);
511   fail_unless (res == TRUE, NULL);
512 
513   srcpad = gst_element_get_static_pad (videomixer, "src");
514   gst_object_unref (srcpad);
515 
516   main_loop = g_main_loop_new (NULL, FALSE);
517   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
518       bin);
519   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
520   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
521   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
522 
523   GST_INFO ("starting test");
524 
525   /* prepare playing */
526   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
527   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
528 
529   /* wait for completion */
530   state_res =
531       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
532       GST_CLOCK_TIME_NONE);
533   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
534 
535   /* add other element */
536   gst_bin_add_many (GST_BIN (bin), src2, NULL);
537 
538   /* now link the second element */
539   res = gst_element_link (src2, videomixer);
540   fail_unless (res == TRUE, NULL);
541 
542   /* set to PAUSED as well */
543   state_res = gst_element_set_state (src2, GST_STATE_PAUSED);
544   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
545 
546   /* now play all */
547   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
548   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
549 
550   g_main_loop_run (main_loop);
551 
552   state_res = gst_element_set_state (bin, GST_STATE_NULL);
553   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
554 
555   /* cleanup */
556   g_main_loop_unref (main_loop);
557   gst_bus_remove_signal_watch (bus);
558   gst_object_unref (bus);
559   gst_object_unref (bin);
560 }
561 
562 GST_END_TEST;
563 
564 /* check if removing pads work as expected */
GST_START_TEST(test_remove_pad)565 GST_START_TEST (test_remove_pad)
566 {
567   GstElement *bin, *src, *videomixer, *sink;
568   GstBus *bus;
569   GstPad *pad, *srcpad;
570   gboolean res;
571   GstStateChangeReturn state_res;
572 
573   GST_INFO ("preparing test");
574 
575   /* build pipeline */
576   bin = gst_pipeline_new ("pipeline");
577   bus = gst_element_get_bus (bin);
578   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
579 
580   src = gst_element_factory_make ("videotestsrc", "src");
581   g_object_set (src, "num-buffers", 4, NULL);
582   videomixer = gst_element_factory_make ("videomixer", "videomixer");
583   sink = gst_element_factory_make ("fakesink", "sink");
584   gst_bin_add_many (GST_BIN (bin), src, videomixer, sink, NULL);
585 
586   res = gst_element_link (src, videomixer);
587   fail_unless (res == TRUE, NULL);
588   res = gst_element_link (videomixer, sink);
589   fail_unless (res == TRUE, NULL);
590 
591   /* create an unconnected sinkpad in videomixer */
592   pad = gst_element_get_request_pad (videomixer, "sink_%u");
593   fail_if (pad == NULL, NULL);
594 
595   srcpad = gst_element_get_static_pad (videomixer, "src");
596   gst_object_unref (srcpad);
597 
598   main_loop = g_main_loop_new (NULL, FALSE);
599   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
600       bin);
601   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
602   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
603   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
604 
605   GST_INFO ("starting test");
606 
607   /* prepare playing, this will not preroll as videomixer is waiting
608    * on the unconnected sinkpad. */
609   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
610   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
611 
612   /* wait for completion for one second, will return ASYNC */
613   state_res = gst_element_get_state (GST_ELEMENT (bin), NULL, NULL, GST_SECOND);
614   ck_assert_int_eq (state_res, GST_STATE_CHANGE_ASYNC);
615 
616   /* get rid of the pad now, videomixer should stop waiting on it and
617    * continue the preroll */
618   gst_element_release_request_pad (videomixer, pad);
619   gst_object_unref (pad);
620 
621   /* wait for completion, should work now */
622   state_res =
623       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
624       GST_CLOCK_TIME_NONE);
625   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
626 
627   /* now play all */
628   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
629   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
630 
631   g_main_loop_run (main_loop);
632 
633   state_res = gst_element_set_state (bin, GST_STATE_NULL);
634   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
635 
636   /* cleanup */
637   g_main_loop_unref (main_loop);
638   gst_bus_remove_signal_watch (bus);
639   gst_object_unref (G_OBJECT (bus));
640   gst_object_unref (G_OBJECT (bin));
641 }
642 
643 GST_END_TEST;
644 
645 
646 static GstBuffer *handoff_buffer = NULL;
647 static void
handoff_buffer_cb(GstElement * fakesink,GstBuffer * buffer,GstPad * pad,gpointer user_data)648 handoff_buffer_cb (GstElement * fakesink, GstBuffer * buffer, GstPad * pad,
649     gpointer user_data)
650 {
651   GST_DEBUG ("got buffer %p", buffer);
652   gst_buffer_replace (&handoff_buffer, buffer);
653 }
654 
655 /* check if clipping works as expected */
GST_START_TEST(test_clip)656 GST_START_TEST (test_clip)
657 {
658   GstSegment segment;
659   GstElement *bin, *videomixer, *sink;
660   GstBus *bus;
661   GstPad *sinkpad;
662   gboolean res;
663   GstStateChangeReturn state_res;
664   GstFlowReturn ret;
665   GstEvent *event;
666   GstBuffer *buffer;
667   GstCaps *caps;
668 
669   GST_INFO ("preparing test");
670 
671   /* build pipeline */
672   bin = gst_pipeline_new ("pipeline");
673   bus = gst_element_get_bus (bin);
674   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
675 
676   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
677   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
678   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
679 
680   /* just an videomixer and a fakesink */
681   videomixer = gst_element_factory_make ("videomixer", "videomixer");
682   sink = gst_element_factory_make ("fakesink", "sink");
683   g_object_set (sink, "signal-handoffs", TRUE, NULL);
684   g_signal_connect (sink, "handoff", (GCallback) handoff_buffer_cb, NULL);
685   gst_bin_add_many (GST_BIN (bin), videomixer, sink, NULL);
686 
687   res = gst_element_link (videomixer, sink);
688   fail_unless (res == TRUE, NULL);
689 
690   /* set to playing */
691   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
692   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
693 
694   /* create an unconnected sinkpad in videomixer, should also automatically activate
695    * the pad */
696   sinkpad = gst_element_get_request_pad (videomixer, "sink_%u");
697   fail_if (sinkpad == NULL, NULL);
698 
699   gst_pad_send_event (sinkpad, gst_event_new_stream_start ("test"));
700 
701   caps = gst_caps_from_string (VIDEO_CAPS_STRING);
702 
703   gst_pad_set_caps (sinkpad, caps);
704   gst_caps_unref (caps);
705 
706   /* send segment to videomixer */
707   gst_segment_init (&segment, GST_FORMAT_TIME);
708   segment.start = GST_SECOND;
709   segment.stop = 2 * GST_SECOND;
710   segment.time = 0;
711   event = gst_event_new_segment (&segment);
712   gst_pad_send_event (sinkpad, event);
713 
714   /* should be clipped and ok */
715   buffer = gst_buffer_new_and_alloc (115200);
716   GST_BUFFER_TIMESTAMP (buffer) = 0;
717   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
718   GST_DEBUG ("pushing buffer %p", buffer);
719   ret = gst_pad_chain (sinkpad, buffer);
720   ck_assert_int_eq (ret, GST_FLOW_OK);
721   fail_unless (handoff_buffer == NULL);
722 
723   /* should be partially clipped */
724   buffer = gst_buffer_new_and_alloc (115200);
725   GST_BUFFER_TIMESTAMP (buffer) = 900 * GST_MSECOND;
726   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
727   GST_DEBUG ("pushing buffer %p", buffer);
728   ret = gst_pad_chain (sinkpad, buffer);
729   ck_assert_int_eq (ret, GST_FLOW_OK);
730   fail_unless (handoff_buffer != NULL);
731   gst_buffer_replace (&handoff_buffer, NULL);
732 
733   /* should not be clipped */
734   buffer = gst_buffer_new_and_alloc (115200);
735   GST_BUFFER_TIMESTAMP (buffer) = 1 * GST_SECOND;
736   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
737   GST_DEBUG ("pushing buffer %p", buffer);
738   ret = gst_pad_chain (sinkpad, buffer);
739   ck_assert_int_eq (ret, GST_FLOW_OK);
740   fail_unless (handoff_buffer != NULL);
741   gst_buffer_replace (&handoff_buffer, NULL);
742 
743   /* should be clipped and ok */
744   buffer = gst_buffer_new_and_alloc (115200);
745   GST_BUFFER_TIMESTAMP (buffer) = 2 * GST_SECOND;
746   GST_BUFFER_DURATION (buffer) = 250 * GST_MSECOND;
747   GST_DEBUG ("pushing buffer %p", buffer);
748   ret = gst_pad_chain (sinkpad, buffer);
749   ck_assert_int_eq (ret, GST_FLOW_OK);
750   fail_unless (handoff_buffer == NULL);
751 
752   gst_object_unref (sinkpad);
753   gst_element_set_state (bin, GST_STATE_NULL);
754   gst_bus_remove_signal_watch (bus);
755   gst_object_unref (bus);
756   gst_object_unref (bin);
757 }
758 
759 GST_END_TEST;
760 
GST_START_TEST(test_duration_is_max)761 GST_START_TEST (test_duration_is_max)
762 {
763   GstElement *bin, *src[3], *videomixer, *sink;
764   GstStateChangeReturn state_res;
765   GstFormat format = GST_FORMAT_TIME;
766   gboolean res;
767   gint64 duration;
768 
769   GST_INFO ("preparing test");
770 
771   /* build pipeline */
772   bin = gst_pipeline_new ("pipeline");
773 
774   /* 3 sources, an videomixer and a fakesink */
775   src[0] = gst_element_factory_make ("videotestsrc", NULL);
776   src[1] = gst_element_factory_make ("videotestsrc", NULL);
777   src[2] = gst_element_factory_make ("videotestsrc", NULL);
778   videomixer = gst_element_factory_make ("videomixer", "videomixer");
779   sink = gst_element_factory_make ("fakesink", "sink");
780   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], videomixer, sink,
781       NULL);
782 
783   gst_element_link (src[0], videomixer);
784   gst_element_link (src[1], videomixer);
785   gst_element_link (src[2], videomixer);
786   gst_element_link (videomixer, sink);
787 
788   /* irks, duration is reset on basesrc */
789   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
790   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
791 
792   /* set durations on src */
793   GST_BASE_SRC (src[0])->segment.duration = 1000;
794   GST_BASE_SRC (src[1])->segment.duration = 3000;
795   GST_BASE_SRC (src[2])->segment.duration = 2000;
796 
797   /* set to playing */
798   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
799   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
800 
801   /* wait for completion */
802   state_res =
803       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
804       GST_CLOCK_TIME_NONE);
805   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
806 
807   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
808   fail_unless (res, NULL);
809 
810   ck_assert_int_eq (duration, 3000);
811 
812   gst_element_set_state (bin, GST_STATE_NULL);
813   gst_object_unref (bin);
814 }
815 
816 GST_END_TEST;
817 
GST_START_TEST(test_duration_unknown_overrides)818 GST_START_TEST (test_duration_unknown_overrides)
819 {
820   GstElement *bin, *src[3], *videomixer, *sink;
821   GstStateChangeReturn state_res;
822   GstFormat format = GST_FORMAT_TIME;
823   gboolean res;
824   gint64 duration;
825 
826   GST_INFO ("preparing test");
827 
828   /* build pipeline */
829   bin = gst_pipeline_new ("pipeline");
830 
831   /* 3 sources, an videomixer and a fakesink */
832   src[0] = gst_element_factory_make ("videotestsrc", NULL);
833   src[1] = gst_element_factory_make ("videotestsrc", NULL);
834   src[2] = gst_element_factory_make ("videotestsrc", NULL);
835   videomixer = gst_element_factory_make ("videomixer", "videomixer");
836   sink = gst_element_factory_make ("fakesink", "sink");
837   gst_bin_add_many (GST_BIN (bin), src[0], src[1], src[2], videomixer, sink,
838       NULL);
839 
840   gst_element_link (src[0], videomixer);
841   gst_element_link (src[1], videomixer);
842   gst_element_link (src[2], videomixer);
843   gst_element_link (videomixer, sink);
844 
845   /* irks, duration is reset on basesrc */
846   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
847   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
848 
849   /* set durations on src */
850   GST_BASE_SRC (src[0])->segment.duration = GST_CLOCK_TIME_NONE;
851   GST_BASE_SRC (src[1])->segment.duration = 3000;
852   GST_BASE_SRC (src[2])->segment.duration = 2000;
853 
854   /* set to playing */
855   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
856   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
857 
858   /* wait for completion */
859   state_res =
860       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
861       GST_CLOCK_TIME_NONE);
862   fail_unless (state_res != GST_STATE_CHANGE_FAILURE, NULL);
863 
864   res = gst_element_query_duration (GST_ELEMENT (bin), format, &duration);
865   fail_unless (res, NULL);
866 
867   ck_assert_int_eq (duration, GST_CLOCK_TIME_NONE);
868 
869   gst_element_set_state (bin, GST_STATE_NULL);
870   gst_object_unref (bin);
871 }
872 
873 GST_END_TEST;
874 
875 
876 static gboolean looped = FALSE;
877 
878 static void
loop_segment_done(GstBus * bus,GstMessage * message,GstElement * bin)879 loop_segment_done (GstBus * bus, GstMessage * message, GstElement * bin)
880 {
881   GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
882       GST_MESSAGE_SRC (message), message);
883 
884   if (looped) {
885     g_main_loop_quit (main_loop);
886   } else {
887     GstEvent *seek_event;
888     gboolean res;
889 
890     seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
891         GST_SEEK_FLAG_SEGMENT,
892         GST_SEEK_TYPE_SET, (GstClockTime) 0,
893         GST_SEEK_TYPE_SET, (GstClockTime) 1 * GST_SECOND);
894 
895     res = gst_element_send_event (bin, seek_event);
896     fail_unless (res == TRUE, NULL);
897     looped = TRUE;
898   }
899 }
900 
GST_START_TEST(test_loop)901 GST_START_TEST (test_loop)
902 {
903   GstElement *bin, *src1, *src2, *videomixer, *sink;
904   GstBus *bus;
905   GstEvent *seek_event;
906   GstStateChangeReturn state_res;
907   gboolean res;
908 
909   GST_INFO ("preparing test");
910 
911   /* build pipeline */
912   bin = gst_pipeline_new ("pipeline");
913   bus = gst_element_get_bus (bin);
914   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
915 
916   src1 = gst_element_factory_make ("videotestsrc", "src1");
917   src2 = gst_element_factory_make ("videotestsrc", "src2");
918   videomixer = gst_element_factory_make ("videomixer", "videomixer");
919   sink = gst_element_factory_make ("fakesink", "sink");
920   gst_bin_add_many (GST_BIN (bin), src1, src2, videomixer, sink, NULL);
921 
922   res = gst_element_link (src1, videomixer);
923   fail_unless (res == TRUE, NULL);
924   res = gst_element_link (src2, videomixer);
925   fail_unless (res == TRUE, NULL);
926   res = gst_element_link (videomixer, sink);
927   fail_unless (res == TRUE, NULL);
928 
929   seek_event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
930       GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH,
931       GST_SEEK_TYPE_SET, (GstClockTime) 0, GST_SEEK_TYPE_SET,
932       (GstClockTime) 2 * GST_SECOND);
933 
934   main_loop = g_main_loop_new (NULL, FALSE);
935   g_signal_connect (bus, "message::segment-done",
936       (GCallback) loop_segment_done, bin);
937   g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
938   g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
939   g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
940 
941   GST_INFO ("starting test");
942 
943   /* prepare playing */
944   state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
945   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
946 
947   /* wait for completion */
948   state_res =
949       gst_element_get_state (GST_ELEMENT (bin), NULL, NULL,
950       GST_CLOCK_TIME_NONE);
951   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
952 
953   res = gst_element_send_event (bin, seek_event);
954   fail_unless (res == TRUE, NULL);
955 
956   /* run pipeline */
957   state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
958   ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
959 
960   GST_INFO ("running main loop");
961   g_main_loop_run (main_loop);
962 
963   state_res = gst_element_set_state (bin, GST_STATE_NULL);
964 
965   /* cleanup */
966   g_main_loop_unref (main_loop);
967   gst_bus_remove_signal_watch (bus);
968   gst_object_unref (bus);
969   gst_object_unref (bin);
970 }
971 
972 GST_END_TEST;
973 
974 #if 0
975 GST_START_TEST (test_flush_start_flush_stop)
976 {
977   GstPadTemplate *sink_template;
978   GstPad *tmppad, *sinkpad1, *sinkpad2, *videomixer_src;
979   GstElement *pipeline, *src1, *src2, *videomixer, *sink;
980 
981   GST_INFO ("preparing test");
982 
983   /* build pipeline */
984   pipeline = gst_pipeline_new ("pipeline");
985   src1 = gst_element_factory_make ("videotestsrc", "src1");
986   src2 = gst_element_factory_make ("videotestsrc", "src2");
987   videomixer = gst_element_factory_make ("videomixer", "videomixer");
988   sink = gst_element_factory_make ("fakesink", "sink");
989   gst_bin_add_many (GST_BIN (pipeline), src1, src2, videomixer, sink, NULL);
990 
991   sink_template =
992       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (videomixer),
993       "sink_%u");
994   fail_unless (GST_IS_PAD_TEMPLATE (sink_template));
995   sinkpad1 = gst_element_request_pad (videomixer, sink_template, NULL, NULL);
996   tmppad = gst_element_get_static_pad (src1, "src");
997   gst_pad_link (tmppad, sinkpad1);
998   gst_object_unref (tmppad);
999 
1000   sinkpad2 = gst_element_request_pad (videomixer, sink_template, NULL, NULL);
1001   tmppad = gst_element_get_static_pad (src2, "src");
1002   gst_pad_link (tmppad, sinkpad2);
1003   gst_object_unref (tmppad);
1004 
1005   gst_element_link (videomixer, sink);
1006 
1007   gst_element_set_state (pipeline, GST_STATE_PLAYING);
1008   fail_unless (gst_element_get_state (pipeline, NULL, NULL,
1009           GST_CLOCK_TIME_NONE) == GST_STATE_CHANGE_SUCCESS);
1010 
1011   videomixer_src = gst_element_get_static_pad (videomixer, "src");
1012   fail_if (GST_PAD_IS_FLUSHING (videomixer_src));
1013   gst_pad_send_event (sinkpad1, gst_event_new_flush_start ());
1014   fail_unless (GST_PAD_IS_FLUSHING (videomixer_src));
1015   gst_pad_send_event (sinkpad1, gst_event_new_flush_stop (TRUE));
1016   fail_if (GST_PAD_IS_FLUSHING (videomixer_src));
1017   gst_object_unref (videomixer_src);
1018 
1019   /* cleanup */
1020   gst_element_set_state (pipeline, GST_STATE_NULL);
1021   gst_object_unref (sinkpad1);
1022   gst_object_unref (sinkpad2);
1023   gst_object_unref (pipeline);
1024 }
1025 
1026 GST_END_TEST;
1027 #endif
1028 
1029 static Suite *
videomixer_suite(void)1030 videomixer_suite (void)
1031 {
1032   Suite *s = suite_create ("videomixer");
1033   TCase *tc_chain = tcase_create ("general");
1034 
1035   suite_add_tcase (s, tc_chain);
1036   tcase_add_test (tc_chain, test_caps);
1037   tcase_add_test (tc_chain, test_event);
1038   tcase_add_test (tc_chain, test_play_twice);
1039   tcase_add_test (tc_chain, test_play_twice_then_add_and_play_again);
1040   tcase_add_test (tc_chain, test_add_pad);
1041   tcase_add_test (tc_chain, test_remove_pad);
1042   tcase_add_test (tc_chain, test_clip);
1043   tcase_add_test (tc_chain, test_duration_is_max);
1044   tcase_add_test (tc_chain, test_duration_unknown_overrides);
1045   tcase_add_test (tc_chain, test_loop);
1046   /* This test is racy and occasionally fails in interesting ways
1047    * just like the corresponding adder test does/did, see
1048    * https://bugzilla.gnome.org/show_bug.cgi?id=708891
1049    * It's unlikely that it will ever be fixed for videomixer/collectpads,
1050    * as it works fine with compositor */
1051 #if 0
1052   tcase_add_test (tc_chain, test_flush_start_flush_stop);
1053 #endif
1054 
1055   /* Use a longer timeout */
1056 #ifdef HAVE_VALGRIND
1057   if (RUNNING_ON_VALGRIND) {
1058     tcase_set_timeout (tc_chain, 5 * 60);
1059   } else
1060 #endif
1061   {
1062     /* this is shorter than the default 60 seconds?! (tpm) */
1063     /* tcase_set_timeout (tc_chain, 6); */
1064   }
1065 
1066   return s;
1067 }
1068 
1069 GST_CHECK_MAIN (videomixer);
1070