5f6ae2e2aedc4e9b458578d1342f30b1a823d4b4
[platform/upstream/gstreamer.git] / tests / check / libs / aggregator.c
1 /*
2  * aggregator.c - GstAggregator testsuite
3  * Copyright (C) 2006 Alessandro Decina <alessandro.d@gmail.com>
4  * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@oencreed.com>
5  * Copyright (C) 2014 Thibault Saunier <tsaunier@opencreed.com>
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 #include <gst/check/gstcheck.h>
28 #include <gst/base/gstaggregator.h>
29
30 /* dummy aggregator based element */
31
32 #define GST_TYPE_TEST_AGGREGATOR            (gst_test_aggregator_get_type ())
33 #define GST_TEST_AGGREGATOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TEST_AGGREGATOR, GstTestAggregator))
34 #define GST_TEST_AGGREGATOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TEST_AGGREGATOR, GstTestAggregatorClass))
35 #define GST_TEST_AGGREGATOR_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_TEST_AGGREGATOR, GstTestAggregatorClass))
36
37 #define fail_error_message(msg)     \
38   G_STMT_START {        \
39     GError *error;        \
40     gst_message_parse_error(msg, &error, NULL);       \
41     fail_unless(FALSE, "Error Message from %s : %s",      \
42     GST_OBJECT_NAME (GST_MESSAGE_SRC(msg)), error->message); \
43     g_error_free (error);           \
44   } G_STMT_END;
45
46 typedef struct _GstTestAggregator GstTestAggregator;
47 typedef struct _GstTestAggregatorClass GstTestAggregatorClass;
48
49 static GType gst_test_aggregator_get_type (void);
50
51 #define BUFFER_DURATION 100000000       /* 10 frames per second */
52 #define TEST_GAP_PTS 0
53 #define TEST_GAP_DURATION (5 * GST_SECOND)
54
55 struct _GstTestAggregator
56 {
57   GstAggregator parent;
58
59   guint64 timestamp;
60   gboolean gap_expected;
61 };
62
63 struct _GstTestAggregatorClass
64 {
65   GstAggregatorClass parent_class;
66 };
67
68 static GstFlowReturn
69 gst_test_aggregator_aggregate (GstAggregator * aggregator, gboolean timeout)
70 {
71   GstIterator *iter;
72   gboolean all_eos = TRUE;
73   GstTestAggregator *testagg;
74   GstBuffer *buf;
75
76   gboolean done_iterating = FALSE;
77
78   testagg = GST_TEST_AGGREGATOR (aggregator);
79
80   iter = gst_element_iterate_sink_pads (GST_ELEMENT (testagg));
81   while (!done_iterating) {
82     GValue value = { 0, };
83     GstAggregatorPad *pad;
84
85     switch (gst_iterator_next (iter, &value)) {
86       case GST_ITERATOR_OK:
87         pad = g_value_get_object (&value);
88
89         if (gst_aggregator_pad_is_eos (pad) == FALSE)
90           all_eos = FALSE;
91
92         if (testagg->gap_expected == TRUE) {
93           buf = gst_aggregator_pad_get_buffer (pad);
94           fail_unless (buf);
95           fail_unless (GST_BUFFER_PTS (buf) == TEST_GAP_PTS);
96           fail_unless (GST_BUFFER_DURATION (buf) == TEST_GAP_DURATION);
97           fail_unless (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP));
98           fail_unless (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DROPPABLE));
99           gst_buffer_unref (buf);
100           testagg->gap_expected = FALSE;
101         }
102
103         gst_aggregator_pad_drop_buffer (pad);
104
105         g_value_reset (&value);
106         break;
107       case GST_ITERATOR_RESYNC:
108         gst_iterator_resync (iter);
109         break;
110       case GST_ITERATOR_ERROR:
111         GST_WARNING_OBJECT (testagg, "Sinkpads iteration error");
112         done_iterating = TRUE;
113         break;
114       case GST_ITERATOR_DONE:
115         done_iterating = TRUE;
116         break;
117     }
118   }
119   gst_iterator_free (iter);
120
121   if (all_eos == TRUE) {
122     GST_INFO_OBJECT (testagg, "no data available, must be EOS");
123     gst_pad_push_event (aggregator->srcpad, gst_event_new_eos ());
124     return GST_FLOW_EOS;
125   }
126
127   buf = gst_buffer_new ();
128   GST_BUFFER_TIMESTAMP (buf) = testagg->timestamp;
129   GST_BUFFER_DURATION (buf) = BUFFER_DURATION;
130   testagg->timestamp += BUFFER_DURATION;
131
132   gst_aggregator_finish_buffer (aggregator, buf);
133
134   /* We just check finish_frame return FLOW_OK */
135   return GST_FLOW_OK;
136 }
137
138 #define gst_test_aggregator_parent_class parent_class
139 G_DEFINE_TYPE (GstTestAggregator, gst_test_aggregator, GST_TYPE_AGGREGATOR);
140
141 static void
142 gst_test_aggregator_class_init (GstTestAggregatorClass * klass)
143 {
144   GstElementClass *gstelement_class = (GstElementClass *) klass;
145   GstAggregatorClass *base_aggregator_class = (GstAggregatorClass *) klass;
146
147   static GstStaticPadTemplate _src_template =
148       GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
149       GST_STATIC_CAPS_ANY);
150
151   static GstStaticPadTemplate _sink_template =
152       GST_STATIC_PAD_TEMPLATE ("sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
153       GST_STATIC_CAPS_ANY);
154
155   gst_element_class_add_static_pad_template (gstelement_class, &_src_template);
156
157   gst_element_class_add_static_pad_template (gstelement_class, &_sink_template);
158
159   gst_element_class_set_static_metadata (gstelement_class, "Aggregator",
160       "Testing", "Combine N buffers", "Stefan Sauer <ensonic@users.sf.net>");
161
162   base_aggregator_class->aggregate =
163       GST_DEBUG_FUNCPTR (gst_test_aggregator_aggregate);
164 }
165
166 static void
167 gst_test_aggregator_init (GstTestAggregator * self)
168 {
169   GstAggregator *agg = GST_AGGREGATOR (self);
170   gst_segment_init (&agg->segment, GST_FORMAT_TIME);
171   self->timestamp = 0;
172   self->gap_expected = FALSE;
173 }
174
175 static gboolean
176 gst_test_aggregator_plugin_init (GstPlugin * plugin)
177 {
178   return gst_element_register (plugin, "testaggregator", GST_RANK_NONE,
179       GST_TYPE_TEST_AGGREGATOR);
180 }
181
182 static gboolean
183 gst_test_aggregator_plugin_register (void)
184 {
185   return gst_plugin_register_static (GST_VERSION_MAJOR,
186       GST_VERSION_MINOR,
187       "testaggregator",
188       "Combine buffers",
189       gst_test_aggregator_plugin_init,
190       VERSION, GST_LICENSE, PACKAGE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
191 }
192
193 typedef struct
194 {
195   GstEvent *event;
196   GstBuffer *buffer;
197   GstElement *aggregator;
198   GstPad *sinkpad, *srcpad;
199   GstFlowReturn expected_result;
200
201   /*                       ------------------
202    * -----------   --------|--              |
203    * | srcpad | -- | sinkpad |  aggregator  |
204    * -----------   --------|--              |
205    *                       ------------------
206    *  This is for 1 Chain, we can have several
207    */
208 } ChainData;
209
210 typedef struct
211 {
212   GMainLoop *ml;
213   GstPad *srcpad,               /* srcpad of the GstAggregator */
214    *sinkpad;                    /* fake sinkpad to which GstAggregator.srcpad is linked */
215   guint timeout_id;
216   GstElement *aggregator;
217
218   /* -----------------|
219    * |             ----------    -----------
220    * | aggregator  | srcpad | -- | sinkpad |
221    * |             ----------    -----------
222    * -----------------|
223    */
224
225   gint flush_start_events, flush_stop_events;
226 } TestData;
227
228 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
229     GST_PAD_SRC,
230     GST_PAD_ALWAYS,
231     GST_STATIC_CAPS_ANY);
232
233 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
234     GST_PAD_SINK,
235     GST_PAD_ALWAYS,
236     GST_STATIC_CAPS_ANY);
237
238 static void
239 start_flow (ChainData * chain_data)
240 {
241   GstSegment segment;
242   GstCaps *caps;
243
244   gst_pad_push_event (chain_data->srcpad, gst_event_new_stream_start ("test"));
245
246   caps = gst_caps_new_empty_simple ("foo/x-bar");
247   gst_pad_push_event (chain_data->srcpad, gst_event_new_caps (caps));
248   gst_caps_unref (caps);
249
250   gst_segment_init (&segment, GST_FORMAT_TIME);
251   gst_pad_push_event (chain_data->srcpad, gst_event_new_segment (&segment));
252 }
253
254 static gpointer
255 push_buffer (gpointer user_data)
256 {
257   GstFlowReturn flow;
258   ChainData *chain_data = (ChainData *) user_data;
259
260   start_flow (chain_data);
261
262   GST_DEBUG ("Pushing buffer on pad: %s:%s",
263       GST_DEBUG_PAD_NAME (chain_data->sinkpad));
264   flow = gst_pad_push (chain_data->srcpad, chain_data->buffer);
265   fail_unless (flow == chain_data->expected_result,
266       "got flow %s instead of %s on %s:%s", gst_flow_get_name (flow),
267       gst_flow_get_name (chain_data->expected_result),
268       GST_DEBUG_PAD_NAME (chain_data->sinkpad));
269   chain_data->buffer = NULL;
270
271   return NULL;
272 }
273
274 static gpointer
275 push_event (gpointer user_data)
276 {
277   ChainData *chain_data = (ChainData *) user_data;
278   GstTestAggregator *aggregator = (GstTestAggregator *) chain_data->aggregator;
279   GstEventType event_type;
280
281   start_flow (chain_data);
282
283   GST_INFO_OBJECT (chain_data->srcpad, "Pushing event: %"
284       GST_PTR_FORMAT, chain_data->event);
285
286   event_type = GST_EVENT_TYPE (chain_data->event);
287   switch (event_type) {
288     case GST_EVENT_GAP:
289       aggregator->gap_expected = TRUE;
290       break;
291     default:
292       break;
293   }
294
295   fail_unless (gst_pad_push_event (chain_data->srcpad,
296           chain_data->event) == TRUE);
297
298
299   return NULL;
300 }
301
302 static gboolean
303 _aggregate_timeout (GMainLoop * ml)
304 {
305   g_main_loop_quit (ml);
306
307   fail_unless ("No buffer found on aggregator.srcpad -> TIMEOUT" == NULL);
308
309   return FALSE;
310 }
311
312 static gboolean
313 _quit (GMainLoop * ml)
314 {
315   GST_DEBUG ("QUITING ML");
316   g_main_loop_quit (ml);
317
318   return G_SOURCE_REMOVE;
319 }
320
321 static GstPadProbeReturn
322 _aggregated_cb (GstPad * pad, GstPadProbeInfo * info, GMainLoop * ml)
323 {
324   GST_DEBUG ("Should quit ML");
325   g_idle_add ((GSourceFunc) _quit, ml);
326
327   return GST_PAD_PROBE_REMOVE;
328 }
329
330 static GstPadProbeReturn
331 downstream_probe_cb (GstPad * pad, GstPadProbeInfo * info, TestData * test)
332 {
333   GST_DEBUG ("PROBING ");
334   if (info->type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) {
335     if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_EVENT (info)) ==
336         GST_EVENT_FLUSH_START) {
337
338       g_atomic_int_inc (&test->flush_start_events);
339       GST_DEBUG ("==========> FLUSH: %i", test->flush_start_events);
340     } else if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_EVENT (info)) ==
341         GST_EVENT_FLUSH_STOP)
342       g_atomic_int_inc (&test->flush_stop_events);
343   }
344
345   gst_mini_object_unref (info->data);
346
347   return GST_PAD_PROBE_HANDLED;
348 }
349
350 /*
351  * Not thread safe, will create a new ChainData which contains
352  * an activated src pad linked to a requested sink pad of @agg, and
353  * a newly allocated buffer ready to be pushed. Caller needs to
354  * clear with _chain_data_clear after.
355  */
356 static void
357 _chain_data_init (ChainData * data, GstElement * agg)
358 {
359   static gint num_src_pads = 0;
360   gchar *pad_name = g_strdup_printf ("src%d", num_src_pads);
361
362   num_src_pads += 1;
363
364   data->srcpad = gst_pad_new_from_static_template (&srctemplate, pad_name);
365   g_free (pad_name);
366   gst_pad_set_active (data->srcpad, TRUE);
367   data->aggregator = agg;
368   data->buffer = gst_buffer_new ();
369   data->sinkpad = gst_element_get_request_pad (agg, "sink_%u");
370   fail_unless (GST_IS_PAD (data->sinkpad));
371   fail_unless (gst_pad_link (data->srcpad, data->sinkpad) == GST_PAD_LINK_OK);
372 }
373
374 static void
375 _chain_data_clear (ChainData * data)
376 {
377   if (data->buffer)
378     gst_buffer_unref (data->buffer);
379   if (data->srcpad)
380     gst_object_unref (data->srcpad);
381   if (data->sinkpad)
382     gst_object_unref (data->sinkpad);
383 }
384
385 static void
386 _test_data_init (TestData * test, gboolean needs_flushing)
387 {
388   test->aggregator = gst_element_factory_make ("testaggregator", NULL);
389   gst_element_set_state (test->aggregator, GST_STATE_PLAYING);
390   test->ml = g_main_loop_new (NULL, TRUE);
391   test->srcpad = GST_AGGREGATOR (test->aggregator)->srcpad;
392
393   GST_DEBUG ("Srcpad: %p", test->srcpad);
394
395   if (needs_flushing) {
396     static gint num_sink_pads = 0;
397     gchar *pad_name = g_strdup_printf ("sink%d", num_sink_pads);
398
399     num_sink_pads += 1;
400     test->sinkpad = gst_pad_new_from_static_template (&sinktemplate, pad_name);
401     g_free (pad_name);
402     fail_unless (gst_pad_link (test->srcpad, test->sinkpad) == GST_PAD_LINK_OK);
403     gst_pad_add_probe (test->srcpad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
404         GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM |
405         GST_PAD_PROBE_TYPE_EVENT_FLUSH,
406         (GstPadProbeCallback) downstream_probe_cb, test, NULL);
407   } else {
408     gst_pad_add_probe (test->srcpad, GST_PAD_PROBE_TYPE_BUFFER,
409         (GstPadProbeCallback) _aggregated_cb, test->ml, NULL);
410   }
411
412
413   test->timeout_id =
414       g_timeout_add (1000, (GSourceFunc) _aggregate_timeout, test->ml);
415 }
416
417 static void
418 _test_data_clear (TestData * test)
419 {
420   gst_element_set_state (test->aggregator, GST_STATE_NULL);
421   gst_object_unref (test->aggregator);
422
423   if (test->sinkpad)
424     gst_object_unref (test->sinkpad);
425
426   g_main_loop_unref (test->ml);
427 }
428
429 GST_START_TEST (test_aggregate)
430 {
431   GThread *thread1, *thread2;
432
433   ChainData data1 = { 0, };
434   ChainData data2 = { 0, };
435   TestData test = { 0, };
436
437   _test_data_init (&test, FALSE);
438   _chain_data_init (&data1, test.aggregator);
439   _chain_data_init (&data2, test.aggregator);
440
441   thread1 = g_thread_try_new ("gst-check", push_buffer, &data1, NULL);
442   thread2 = g_thread_try_new ("gst-check", push_buffer, &data2, NULL);
443
444   g_main_loop_run (test.ml);
445   g_source_remove (test.timeout_id);
446
447
448   /* these will return immediately as when the data is popped the threads are
449    * unlocked and will terminate */
450   g_thread_join (thread1);
451   g_thread_join (thread2);
452
453   _chain_data_clear (&data1);
454   _chain_data_clear (&data2);
455   _test_data_clear (&test);
456 }
457
458 GST_END_TEST;
459
460 GST_START_TEST (test_aggregate_eos)
461 {
462   GThread *thread1, *thread2;
463
464   ChainData data1 = { 0, };
465   ChainData data2 = { 0, };
466   TestData test = { 0, };
467
468   _test_data_init (&test, FALSE);
469   _chain_data_init (&data1, test.aggregator);
470   _chain_data_init (&data2, test.aggregator);
471
472   data2.event = gst_event_new_eos ();
473
474   thread1 = g_thread_try_new ("gst-check", push_buffer, &data1, NULL);
475   thread2 = g_thread_try_new ("gst-check", push_event, &data2, NULL);
476
477   g_main_loop_run (test.ml);
478   g_source_remove (test.timeout_id);
479
480   /* these will return immediately as when the data is popped the threads are
481    * unlocked and will terminate */
482   g_thread_join (thread1);
483   g_thread_join (thread2);
484
485   _chain_data_clear (&data1);
486   _chain_data_clear (&data2);
487   _test_data_clear (&test);
488 }
489
490 GST_END_TEST;
491
492 GST_START_TEST (test_aggregate_gap)
493 {
494   GThread *thread;
495
496   ChainData data = { 0, };
497   TestData test = { 0, };
498
499   _test_data_init (&test, FALSE);
500   _chain_data_init (&data, test.aggregator);
501
502   data.event = gst_event_new_gap (TEST_GAP_PTS, TEST_GAP_DURATION);
503
504   thread = g_thread_try_new ("gst-check", push_event, &data, NULL);
505
506   g_main_loop_run (test.ml);
507   g_source_remove (test.timeout_id);
508
509   /* these will return immediately as when the data is popped the threads are
510    * unlocked and will terminate */
511   g_thread_join (thread);
512
513   _chain_data_clear (&data);
514   _test_data_clear (&test);
515 }
516
517 GST_END_TEST;
518
519 #define NUM_BUFFERS 3
520 static void
521 handoff (GstElement * fakesink, GstBuffer * buf, GstPad * pad, guint * count)
522 {
523   *count = *count + 1;
524   GST_DEBUG ("HANDOFF: %i", *count);
525 }
526
527 /* Test a linear pipeline using aggregator */
528 GST_START_TEST (test_linear_pipeline)
529 {
530   GstBus *bus;
531   GstMessage *msg;
532   GstElement *pipeline, *src, *agg, *sink;
533
534   gint count = 0;
535
536   pipeline = gst_pipeline_new ("pipeline");
537   src = gst_check_setup_element ("fakesrc");
538   g_object_set (src, "num-buffers", NUM_BUFFERS, "sizetype", 2, "sizemax", 4,
539       NULL);
540   agg = gst_check_setup_element ("testaggregator");
541   sink = gst_check_setup_element ("fakesink");
542   g_object_set (sink, "signal-handoffs", TRUE, NULL);
543   g_signal_connect (sink, "handoff", (GCallback) handoff, &count);
544
545   fail_unless (gst_bin_add (GST_BIN (pipeline), src));
546   fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
547   fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
548   fail_unless (gst_element_link (src, agg));
549   fail_unless (gst_element_link (agg, sink));
550
551   bus = gst_element_get_bus (pipeline);
552   fail_if (bus == NULL);
553   gst_element_set_state (pipeline, GST_STATE_PLAYING);
554
555   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
556   fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
557   gst_message_unref (msg);
558
559   fail_unless_equals_int (count, NUM_BUFFERS);
560
561   gst_element_set_state (pipeline, GST_STATE_NULL);
562   gst_object_unref (bus);
563   gst_object_unref (pipeline);
564 }
565
566 GST_END_TEST;
567
568 GST_START_TEST (test_two_src_pipeline)
569 {
570   GstBus *bus;
571   GstMessage *msg;
572   GstElement *pipeline, *src, *src1, *agg, *sink;
573
574   gint count = 0;
575
576   pipeline = gst_pipeline_new ("pipeline");
577   src = gst_element_factory_make ("fakesrc", NULL);
578   g_object_set (src, "num-buffers", NUM_BUFFERS, "sizetype", 2, "sizemax", 4,
579       NULL);
580
581   src1 = gst_element_factory_make ("fakesrc", NULL);
582   g_object_set (src1, "num-buffers", NUM_BUFFERS + 1, "sizetype", 2, "sizemax",
583       4, NULL);
584
585   agg = gst_check_setup_element ("testaggregator");
586   sink = gst_check_setup_element ("fakesink");
587   g_object_set (sink, "signal-handoffs", TRUE, NULL);
588   g_signal_connect (sink, "handoff", (GCallback) handoff, &count);
589
590   fail_unless (gst_bin_add (GST_BIN (pipeline), src));
591   fail_unless (gst_bin_add (GST_BIN (pipeline), src1));
592   fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
593   fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
594   fail_unless (gst_element_link (src, agg));
595   fail_unless (gst_element_link (src1, agg));
596   fail_unless (gst_element_link (agg, sink));
597
598   bus = gst_element_get_bus (pipeline);
599   fail_if (bus == NULL);
600   gst_element_set_state (pipeline, GST_STATE_PLAYING);
601
602   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
603   fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
604   gst_message_unref (msg);
605
606   fail_unless_equals_int (count, NUM_BUFFERS + 1);
607
608   gst_element_set_state (pipeline, GST_STATE_NULL);
609   gst_object_unref (bus);
610   gst_object_unref (pipeline);
611 }
612
613 GST_END_TEST;
614
615 static GstPadProbeReturn
616 _drop_buffer_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
617 {
618   gint wait;
619
620   if (GST_IS_BUFFER (info->data)) {
621     wait = GPOINTER_TO_INT (user_data);
622     if (wait > 0)
623       g_usleep (wait / 1000);
624     return GST_PAD_PROBE_DROP;
625   }
626
627   return GST_PAD_PROBE_PASS;
628 }
629
630 #define TIMEOUT_NUM_BUFFERS 20
631 static void
632 _test_timeout (gint buffer_wait)
633 {
634   GstBus *bus;
635   GstMessage *msg;
636   GstElement *pipeline, *src, *src1, *agg, *sink;
637   GstPad *src1pad;
638
639   gint count = 0;
640
641   pipeline = gst_pipeline_new ("pipeline");
642   src = gst_element_factory_make ("fakesrc", NULL);
643   g_object_set (src, "num-buffers", TIMEOUT_NUM_BUFFERS, "sizetype", 2,
644       "sizemax", 4, "is-live", TRUE, "datarate", 4000, NULL);
645
646   src1 = gst_element_factory_make ("fakesrc", NULL);
647   g_object_set (src1, "num-buffers", TIMEOUT_NUM_BUFFERS, "sizetype", 2,
648       "sizemax", 4, "is-live", TRUE, "datarate", 4000, NULL);
649
650   agg = gst_check_setup_element ("testaggregator");
651   g_object_set (agg, "latency", GST_USECOND, NULL);
652   sink = gst_check_setup_element ("fakesink");
653   g_object_set (sink, "signal-handoffs", TRUE, NULL);
654   g_signal_connect (sink, "handoff", (GCallback) handoff, &count);
655
656   fail_unless (gst_bin_add (GST_BIN (pipeline), src));
657   fail_unless (gst_bin_add (GST_BIN (pipeline), src1));
658   fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
659   fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
660
661   src1pad = gst_element_get_static_pad (src1, "src");
662   fail_if (src1pad == NULL);
663   gst_pad_add_probe (src1pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
664       (GstPadProbeCallback) _drop_buffer_probe_cb,
665       GINT_TO_POINTER (buffer_wait), NULL);
666
667   fail_unless (gst_element_link (src, agg));
668   fail_unless (gst_element_link (src1, agg));
669   fail_unless (gst_element_link (agg, sink));
670
671   bus = gst_element_get_bus (pipeline);
672   fail_if (bus == NULL);
673   gst_element_set_state (pipeline, GST_STATE_PLAYING);
674
675   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
676   fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
677   gst_message_unref (msg);
678
679   /* cannot rely on the exact number of buffers as the timeout may produce
680    * more buffers with the unsynchronized _aggregate() implementation in
681    * testaggregator */
682   fail_if (count < TIMEOUT_NUM_BUFFERS);
683
684   gst_element_set_state (pipeline, GST_STATE_NULL);
685   gst_object_unref (src1pad);
686   gst_object_unref (bus);
687   gst_object_unref (pipeline);
688 }
689
690 GST_START_TEST (test_timeout_pipeline)
691 {
692   _test_timeout (0);
693 }
694
695 GST_END_TEST;
696
697 GST_START_TEST (test_timeout_pipeline_with_wait)
698 {
699   _test_timeout (1000000 /* 1 ms */ );
700 }
701
702 GST_END_TEST;
703
704 GST_START_TEST (test_flushing_seek)
705 {
706   GstEvent *event;
707   GThread *thread1, *thread2;
708
709   ChainData data1 = { 0, };
710   ChainData data2 = { 0, };
711   TestData test = { 0, };
712
713   _test_data_init (&test, TRUE);
714
715   /* Queue a buffer in agg:sink_1. Then do a flushing seek and check that the
716    * new flushing seek logic is triggered. On the first FLUSH_START call the
717    * buffers queued in collectpads should get flushed. Only one FLUSH_START and
718    * one FLUSH_STOP should be forwarded downstream.
719    */
720   _chain_data_init (&data1, test.aggregator);
721   _chain_data_init (&data2, test.aggregator);
722   GST_BUFFER_TIMESTAMP (data2.buffer) = 0;
723
724   gst_segment_init (&GST_AGGREGATOR (test.aggregator)->segment,
725       GST_FORMAT_TIME);
726
727   /* now do a successful flushing seek */
728   event = gst_event_new_seek (1, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
729       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 10 * GST_SECOND);
730   fail_unless (gst_pad_send_event (test.srcpad, event));
731
732   /* flushing starts once one of the upstream elements sends the first
733    * FLUSH_START */
734   fail_unless_equals_int (test.flush_start_events, 0);
735   fail_unless_equals_int (test.flush_stop_events, 0);
736
737   /* flush ogg:sink_0. This flushs collectpads, calls ::flush() and sends
738    * FLUSH_START downstream */
739   GST_DEBUG ("Flushing: %s:%s", GST_DEBUG_PAD_NAME (data2.sinkpad));
740   fail_unless (gst_pad_push_event (data2.srcpad, gst_event_new_flush_start ()));
741
742   /* expect this buffer to be flushed */
743   data2.expected_result = GST_FLOW_FLUSHING;
744   thread2 = g_thread_try_new ("gst-check", push_buffer, &data2, NULL);
745
746   fail_unless (gst_pad_push_event (data1.srcpad, gst_event_new_flush_start ()));
747   fail_unless_equals_int (test.flush_start_events, 1);
748   fail_unless_equals_int (test.flush_stop_events, 0);
749
750   /* the first FLUSH_STOP is not forwarded downstream */
751   fail_unless (gst_pad_push_event (data1.srcpad,
752           gst_event_new_flush_stop (TRUE)));
753   fail_unless_equals_int (test.flush_start_events, 1);
754   fail_unless_equals_int (test.flush_stop_events, 0);
755
756   /* at this point even the other pad agg:sink_1 should be flushing so thread2
757    * should have stopped */
758   g_thread_join (thread2);
759
760   /* push a buffer on agg:sink_0 to trigger one collect after flushing to verify
761    * that flushing completes once all the pads have been flushed */
762   thread1 = g_thread_try_new ("gst-check", push_buffer, &data1, NULL);
763
764   /* flush agg:sink_1 as well. This completes the flushing seek so a FLUSH_STOP is
765    * sent downstream */
766   gst_pad_push_event (data2.srcpad, gst_event_new_flush_stop (TRUE));
767
768   /* and the last FLUSH_STOP is forwarded downstream */
769   fail_unless_equals_int (test.flush_stop_events, 1);
770
771   /*  Check collected */
772   gst_pad_add_probe (test.srcpad, GST_PAD_PROBE_TYPE_BUFFER,
773       (GstPadProbeCallback) _aggregated_cb, test.ml, NULL);
774
775   data2.event = gst_event_new_eos ();
776   thread2 = g_thread_try_new ("gst-check", push_event, &data2, NULL);
777
778   g_main_loop_run (test.ml);
779   g_source_remove (test.timeout_id);
780
781   fail_unless_equals_int (test.flush_stop_events, 1);
782
783   /* these will return immediately as at this point the threads have been
784    * unlocked and are finished */
785   g_thread_join (thread1);
786   g_thread_join (thread2);
787
788   _chain_data_clear (&data1);
789   _chain_data_clear (&data2);
790   _test_data_clear (&test);
791
792 }
793
794 GST_END_TEST;
795
796 static void
797 infinite_seek (guint num_srcs, guint num_seeks, gboolean is_live)
798 {
799   GstBus *bus;
800   GstMessage *message;
801   GstElement *pipeline, *src, *agg, *sink;
802
803   gint count = 0, i;
804   gboolean seek_res, carry_on = TRUE;
805
806   gst_init (NULL, NULL);
807
808   pipeline = gst_pipeline_new ("pipeline");
809
810   agg = gst_check_setup_element ("testaggregator");
811   sink = gst_check_setup_element ("fakesink");
812
813   if (is_live)
814     g_object_set (agg, "latency", GST_MSECOND, NULL);
815
816   fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
817   fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
818   fail_unless (gst_element_link (agg, sink));
819
820   for (i = 0; i < num_srcs; i++) {
821     src = gst_element_factory_make ("fakesrc", NULL);
822     g_object_set (src, "sizetype", 2, "sizemax", 4,
823         "format", GST_FORMAT_TIME, "datarate", 1000, NULL);
824     if (is_live)
825       g_object_set (src, "is-live", TRUE, NULL);
826     fail_unless (gst_bin_add (GST_BIN (pipeline), src));
827     fail_unless (gst_element_link (src, agg));
828   }
829
830   bus = gst_element_get_bus (pipeline);
831   fail_if (bus == NULL);
832   gst_element_set_state (pipeline, GST_STATE_PLAYING);
833   while (count < num_seeks && carry_on) {
834     message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 10);
835     if (message) {
836       switch (GST_MESSAGE_TYPE (message)) {
837         case GST_MESSAGE_EOS:
838         {
839           /* we should check if we really finished here */
840           GST_WARNING ("Got an EOS");
841           carry_on = FALSE;
842           break;
843         }
844         case GST_MESSAGE_STATE_CHANGED:
845         {
846           GstState new;
847
848           if (GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline)) {
849             gst_message_parse_state_changed (message, NULL, &new, NULL);
850
851             if (new != GST_STATE_PLAYING)
852               break;
853
854             GST_INFO ("Seeking (num: %i)", count);
855             seek_res =
856                 gst_element_seek_simple (sink, GST_FORMAT_TIME,
857                 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, 0);
858             GST_INFO ("seek result is : %d", seek_res);
859             fail_unless (seek_res != 0);
860             count++;
861           }
862
863           break;
864         }
865         case GST_MESSAGE_ERROR:
866           GST_ERROR ("Error on the bus: %" GST_PTR_FORMAT, message);
867           carry_on = FALSE;
868           fail_error_message (message);
869           break;
870         default:
871           break;
872       }
873       gst_message_unref (message);
874     }
875   }
876
877   gst_element_set_state (pipeline, GST_STATE_NULL);
878   gst_object_unref (bus);
879   gst_object_unref (pipeline);
880 }
881
882 GST_START_TEST (test_infinite_seek)
883 {
884   infinite_seek (2, 500, FALSE);
885 }
886
887 GST_END_TEST;
888
889 GST_START_TEST (test_infinite_seek_50_src)
890 {
891   infinite_seek (50, 100, FALSE);
892 }
893
894 GST_END_TEST;
895
896 GST_START_TEST (test_infinite_seek_50_src_live)
897 {
898   infinite_seek (50, 100, TRUE);
899 }
900
901 GST_END_TEST;
902
903 typedef struct
904 {
905   GstElement *agg, *src, *pipeline;
906   GCond *cond;
907   GMutex *lock;
908 } RemoveElementData;
909
910 static GstPadProbeReturn
911 pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, RemoveElementData * data)
912 {
913   GstPad *peer;
914
915   GST_INFO_OBJECT (pad, "Removing pad");
916
917   peer = gst_pad_get_peer (pad);
918   gst_pad_unlink (pad, peer);
919   gst_element_release_request_pad (data->agg, peer);
920   fail_unless (gst_bin_remove (GST_BIN (data->pipeline), data->src));
921   gst_object_unref (peer);
922
923   g_mutex_lock (data->lock);
924   g_cond_broadcast (data->cond);
925   g_mutex_unlock (data->lock);
926
927   return GST_PAD_PROBE_OK;
928 }
929
930 GST_START_TEST (test_add_remove)
931 {
932   /* Used to notify that we removed the pad from  */
933   GCond cond;
934   GMutex lock;
935
936   GstBus *bus;
937   GstState state;
938   GstMessage *message;
939   gboolean carry_on = TRUE;
940   guint num_iterations = 100;
941
942   GstPad *pad;
943   GstElement *pipeline, *src, *src1 = NULL, *agg, *sink;
944
945   gint count = 0;
946
947   gst_init (NULL, NULL);
948   g_mutex_init (&lock);
949   g_cond_init (&cond);
950
951   pipeline = gst_pipeline_new ("pipeline");
952
953   agg = gst_check_setup_element ("testaggregator");
954   sink = gst_check_setup_element ("fakesink");
955
956   fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
957   fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
958   fail_unless (gst_element_link (agg, sink));
959
960   bus = gst_element_get_bus (pipeline);
961   while (count < num_iterations) {
962
963     src = gst_element_factory_make ("fakesrc", NULL);
964     g_object_set (src, "num-buffers", 100000, "sizetype", 2, "sizemax", 4,
965         "format", GST_FORMAT_TIME, "datarate", 1000, NULL);
966     gst_element_set_locked_state (src, TRUE);
967     fail_unless (gst_bin_add (GST_BIN (pipeline), src));
968     fail_unless (gst_element_link (src, agg));
969     gst_element_set_locked_state (src, FALSE);
970     fail_unless (gst_element_sync_state_with_parent (src));
971
972     if (count == 0)
973       gst_element_set_state (pipeline, GST_STATE_PLAYING);
974
975     /* Now make sure the seek happend */
976     carry_on = TRUE;
977     do {
978       message = gst_bus_timed_pop (bus, -1);
979       switch (GST_MESSAGE_TYPE (message)) {
980         case GST_MESSAGE_EOS:
981         {
982           /* we should check if we really finished here */
983           GST_WARNING ("Got an EOS");
984           carry_on = FALSE;
985           break;
986         }
987         case GST_MESSAGE_STATE_CHANGED:
988         {
989           if (GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline)) {
990             gst_message_parse_state_changed (message, NULL, &state, NULL);
991
992             if (state == GST_STATE_PLAYING) {
993               RemoveElementData data;
994
995               carry_on = FALSE;
996               if (count == 0) {
997                 GST_DEBUG ("First run, not removing any element yet");
998
999                 break;
1000               }
1001
1002               data.src = gst_object_ref (src1);
1003               data.agg = agg;
1004               data.lock = &lock;
1005               data.cond = &cond;
1006               data.pipeline = pipeline;
1007               pad = gst_element_get_static_pad (data.src, "src");
1008
1009               g_mutex_lock (&lock);
1010               gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1011                   (GstPadProbeCallback) pad_probe_cb, &data, NULL);
1012               GST_INFO ("Waiting for %" GST_PTR_FORMAT " %s", pad,
1013                   gst_element_state_get_name (GST_STATE (data.src)));
1014               g_cond_wait (&cond, &lock);
1015               g_mutex_unlock (&lock);
1016               gst_object_unref (pad);
1017
1018               /*  We can not set state from the streaming thread so we
1019                *  need to make sure that the source has been removed
1020                *  before setting its state to NULL */
1021               gst_element_set_state (data.src, GST_STATE_NULL);
1022
1023               gst_object_unref (data.src);
1024             }
1025           }
1026
1027           break;
1028         }
1029         case GST_MESSAGE_ERROR:
1030         {
1031           GST_ERROR ("Error on the bus: %" GST_PTR_FORMAT, message);
1032           carry_on = FALSE;
1033           fail_error_message (message);
1034           break;
1035         }
1036         default:
1037           break;
1038       }
1039
1040       gst_message_unref (message);
1041     } while (carry_on);
1042
1043     GST_INFO ("Seeking");
1044     fail_unless (gst_element_seek_simple (pipeline, GST_FORMAT_TIME,
1045             GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, 0));
1046
1047     count++;
1048     src1 = src;
1049   }
1050   gst_element_set_state (pipeline, GST_STATE_NULL);
1051   gst_object_unref (bus);
1052   gst_object_unref (pipeline);
1053   g_mutex_clear (&lock);
1054   g_cond_clear (&cond);
1055 }
1056
1057 GST_END_TEST;
1058
1059 GST_START_TEST (test_change_state_intensive)
1060 {
1061   GstBus *bus;
1062   GstMessage *message;
1063   GstElement *pipeline, *src, *agg, *sink;
1064
1065   gint i, state_i = 0, num_srcs = 3;
1066   gboolean carry_on = TRUE, ready = FALSE;
1067   GstStateChangeReturn state_return;
1068   GstState wanted_state, wanted_states[] = {
1069     GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_READY,
1070     GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_READY,
1071     GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_READY,
1072     GST_STATE_PAUSED, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_READY,
1073     GST_STATE_PAUSED, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_NULL,
1074     GST_STATE_PAUSED, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_NULL,
1075     GST_STATE_PAUSED, GST_STATE_NULL, GST_STATE_PAUSED, GST_STATE_NULL,
1076     GST_STATE_PAUSED, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
1077     GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
1078     GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
1079     GST_STATE_PLAYING, GST_STATE_NULL, GST_STATE_PLAYING, GST_STATE_NULL,
1080   };
1081
1082   gst_init (NULL, NULL);
1083
1084   pipeline = gst_pipeline_new ("pipeline");
1085
1086   agg = gst_check_setup_element ("testaggregator");
1087   sink = gst_check_setup_element ("fakesink");
1088
1089   fail_unless (gst_bin_add (GST_BIN (pipeline), agg));
1090   fail_unless (gst_bin_add (GST_BIN (pipeline), sink));
1091   fail_unless (gst_element_link (agg, sink));
1092
1093   for (i = 0; i < num_srcs; i++) {
1094     src = gst_element_factory_make ("fakesrc", NULL);
1095     g_object_set (src, "sizetype", 2, "sizemax", 4, NULL);
1096     fail_unless (gst_bin_add (GST_BIN (pipeline), src));
1097     fail_unless (gst_element_link (src, agg));
1098   }
1099
1100   bus = gst_element_get_bus (pipeline);
1101   fail_if (bus == NULL);
1102
1103   wanted_state = wanted_states[state_i++];
1104   state_return = gst_element_set_state (pipeline, wanted_state);
1105
1106   while (state_i < G_N_ELEMENTS (wanted_states) && carry_on) {
1107     if (state_return == GST_STATE_CHANGE_SUCCESS && ready) {
1108       wanted_state = wanted_states[state_i++];
1109       fail_unless (gst_element_set_state (pipeline, wanted_state),
1110           GST_STATE_CHANGE_SUCCESS);
1111       GST_INFO ("Wanted state: %s", gst_element_state_get_name (wanted_state));
1112     }
1113
1114     message = gst_bus_poll (bus, GST_MESSAGE_ANY, GST_SECOND / 10);
1115     if (message) {
1116       switch (GST_MESSAGE_TYPE (message)) {
1117         case GST_MESSAGE_EOS:
1118         {
1119           /* we should check if we really finished here */
1120           GST_WARNING ("Got an EOS");
1121           carry_on = FALSE;
1122           break;
1123         }
1124         case GST_MESSAGE_STATE_CHANGED:
1125         {
1126           GstState new;
1127
1128           if (GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline)) {
1129             gst_message_parse_state_changed (message, NULL, &new, NULL);
1130
1131             if (new != wanted_state) {
1132               ready = FALSE;
1133               break;
1134             }
1135
1136             GST_DEBUG ("State %s reached",
1137                 gst_element_state_get_name (wanted_state));
1138             wanted_state = wanted_states[state_i++];
1139             GST_DEBUG ("Wanted state: %s",
1140                 gst_element_state_get_name (wanted_state));
1141             state_return = gst_element_set_state (pipeline, wanted_state);
1142             fail_unless (state_return == GST_STATE_CHANGE_SUCCESS ||
1143                 state_return == GST_STATE_CHANGE_ASYNC);
1144             ready = TRUE;
1145           }
1146
1147           break;
1148         }
1149         case GST_MESSAGE_ERROR:
1150           GST_ERROR ("Error on the bus: %" GST_PTR_FORMAT, message);
1151           carry_on = FALSE;
1152           break;
1153         default:
1154           break;
1155       }
1156       gst_message_unref (message);
1157     }
1158   }
1159
1160   gst_element_set_state (pipeline, GST_STATE_NULL);
1161   gst_object_unref (bus);
1162   gst_object_unref (pipeline);
1163 }
1164
1165 GST_END_TEST;
1166
1167 static Suite *
1168 gst_aggregator_suite (void)
1169 {
1170   Suite *suite;
1171   TCase *general;
1172
1173   gst_test_aggregator_plugin_register ();
1174
1175   suite = suite_create ("GstAggregator");
1176
1177   general = tcase_create ("general");
1178   suite_add_tcase (suite, general);
1179   tcase_add_test (general, test_aggregate);
1180   tcase_add_test (general, test_aggregate_eos);
1181   tcase_add_test (general, test_aggregate_gap);
1182   tcase_add_test (general, test_flushing_seek);
1183   tcase_add_test (general, test_infinite_seek);
1184   tcase_add_test (general, test_infinite_seek_50_src);
1185   tcase_add_test (general, test_infinite_seek_50_src_live);
1186   tcase_add_test (general, test_linear_pipeline);
1187   tcase_add_test (general, test_two_src_pipeline);
1188   tcase_add_test (general, test_timeout_pipeline);
1189   tcase_add_test (general, test_timeout_pipeline_with_wait);
1190   tcase_add_test (general, test_add_remove);
1191   tcase_add_test (general, test_change_state_intensive);
1192
1193   return suite;
1194 }
1195
1196 GST_CHECK_MAIN (gst_aggregator);