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