• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit test for tee
4  *
5  * Copyright (C) <2007> Wim Taymans <wim dot taymans at gmail dot com>
6  * Copyright (C) <2008> Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
7  * Copyright (C) <2008> Christian Berentsen <christian.berentsen@tandberg.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <gst/check/gstcheck.h>
29 
30 static void
handoff(GstElement * fakesink,GstBuffer * buf,GstPad * pad,guint * count)31 handoff (GstElement * fakesink, GstBuffer * buf, GstPad * pad, guint * count)
32 {
33   *count = *count + 1;
34 }
35 
36 /* construct fakesrc num-buffers=3 ! tee name=t ! queue ! fakesink t. ! queue !
37  * fakesink. Each fakesink should exactly receive 3 buffers.
38  */
GST_START_TEST(test_num_buffers)39 GST_START_TEST (test_num_buffers)
40 {
41 #define NUM_SUBSTREAMS 15
42 #define NUM_BUFFERS 3
43   GstElement *pipeline, *src, *tee;
44   GstElement *queues[NUM_SUBSTREAMS];
45   GstElement *sinks[NUM_SUBSTREAMS];
46   GstPad *req_pads[NUM_SUBSTREAMS];
47   guint counts[NUM_SUBSTREAMS];
48   GstBus *bus;
49   GstMessage *msg;
50   gint i;
51 
52   pipeline = gst_pipeline_new ("pipeline");
53   src = gst_check_setup_element ("fakesrc");
54   g_object_set (src, "num-buffers", NUM_BUFFERS, NULL);
55   tee = gst_check_setup_element ("tee");
56   fail_unless (gst_bin_add (GST_BIN (pipeline), src));
57   fail_unless (gst_bin_add (GST_BIN (pipeline), tee));
58   fail_unless (gst_element_link (src, tee));
59 
60   for (i = 0; i < NUM_SUBSTREAMS; ++i) {
61     GstPad *qpad;
62     gchar name[32];
63 
64     counts[i] = 0;
65 
66     queues[i] = gst_check_setup_element ("queue");
67     g_snprintf (name, 32, "queue%d", i);
68     gst_object_set_name (GST_OBJECT (queues[i]), name);
69     fail_unless (gst_bin_add (GST_BIN (pipeline), queues[i]));
70 
71     sinks[i] = gst_check_setup_element ("fakesink");
72     g_snprintf (name, 32, "sink%d", i);
73     gst_object_set_name (GST_OBJECT (sinks[i]), name);
74     fail_unless (gst_bin_add (GST_BIN (pipeline), sinks[i]));
75     fail_unless (gst_element_link (queues[i], sinks[i]));
76     g_object_set (sinks[i], "signal-handoffs", TRUE, NULL);
77     g_signal_connect (sinks[i], "handoff", (GCallback) handoff, &counts[i]);
78 
79     req_pads[i] = gst_element_request_pad_simple (tee, "src_%u");
80     fail_unless (req_pads[i] != NULL);
81 
82     qpad = gst_element_get_static_pad (queues[i], "sink");
83     fail_unless_equals_int (gst_pad_link (req_pads[i], qpad), GST_PAD_LINK_OK);
84     gst_object_unref (qpad);
85   }
86 
87   bus = gst_element_get_bus (pipeline);
88   fail_if (bus == NULL);
89   gst_element_set_state (pipeline, GST_STATE_PLAYING);
90 
91   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
92   fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
93   gst_message_unref (msg);
94 
95   for (i = 0; i < NUM_SUBSTREAMS; ++i) {
96     fail_unless_equals_int (counts[i], NUM_BUFFERS);
97   }
98 
99   gst_element_set_state (pipeline, GST_STATE_NULL);
100   gst_object_unref (bus);
101 
102   for (i = 0; i < NUM_SUBSTREAMS; ++i) {
103     gst_element_release_request_pad (tee, req_pads[i]);
104     gst_object_unref (req_pads[i]);
105   }
106   gst_object_unref (pipeline);
107 }
108 
109 GST_END_TEST;
110 
111 /* we use fakesrc ! tee ! fakesink and then randomly request/release and link
112  * some pads from tee. This should happily run without any errors. */
GST_START_TEST(test_stress)113 GST_START_TEST (test_stress)
114 {
115   GstElement *pipeline;
116   GstElement *tee;
117   const gchar *desc;
118   GstBus *bus;
119   GstMessage *msg;
120   gint i;
121 
122   /* Pump 1000 buffers (10 bytes each) per second through tee for 5 secs */
123   desc = "fakesrc datarate=10000 sizemin=10 sizemax=10 num-buffers=5000 ! "
124       "video/x-raw,framerate=25/1 ! tee name=t ! "
125       "queue max-size-buffers=2 ! fakesink sync=true";
126 
127   pipeline = gst_parse_launch (desc, NULL);
128   fail_if (pipeline == NULL);
129 
130   tee = gst_bin_get_by_name (GST_BIN (pipeline), "t");
131   fail_if (tee == NULL);
132 
133   /* bring the pipeline to PLAYING, then start switching */
134   bus = gst_element_get_bus (pipeline);
135   fail_if (bus == NULL);
136   gst_element_set_state (pipeline, GST_STATE_PLAYING);
137   /* Wait for the pipeline to hit playing so that parse_launch can do the
138    * initial link, otherwise we perform linking from multiple threads and cause
139    * trouble */
140   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
141 
142   for (i = 0; i < 50000; i++) {
143     GstPad *pad;
144 
145     pad = gst_element_request_pad_simple (tee, "src_%u");
146     gst_element_release_request_pad (tee, pad);
147     gst_object_unref (pad);
148 
149     if ((msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, 0)))
150       break;
151   }
152 
153   /* now wait for completion or error */
154   if (msg == NULL)
155     msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
156   fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
157   gst_message_unref (msg);
158 
159   gst_element_set_state (pipeline, GST_STATE_NULL);
160   gst_object_unref (tee);
161   gst_object_unref (bus);
162   gst_object_unref (pipeline);
163 }
164 
165 GST_END_TEST;
166 
167 typedef struct
168 {
169   GstElement *tee;
170   GstCaps *caps;
171   GstPad *start_srcpad;
172   GstPad *tee_sinkpad;
173   GstPad *tee_srcpad;
174   GstPad *final_sinkpad;
175   GThread *app_thread;
176   gint countdown;
177   gboolean app_thread_prepped;
178   gboolean bufferalloc_blocked;
179 } BufferAllocHarness;
180 
181 static void
buffer_alloc_harness_setup(BufferAllocHarness * h,gint countdown)182 buffer_alloc_harness_setup (BufferAllocHarness * h, gint countdown)
183 {
184   h->app_thread = NULL;
185 
186   h->tee = gst_check_setup_element ("tee");
187   fail_if (h->tee == NULL);
188 
189   h->countdown = countdown;
190 
191   fail_unless_equals_int (gst_element_set_state (h->tee, GST_STATE_PLAYING),
192       TRUE);
193 
194   h->caps = gst_caps_new_empty_simple ("video/x-raw");
195 
196   h->start_srcpad = gst_pad_new ("src", GST_PAD_SRC);
197   fail_if (h->start_srcpad == NULL);
198   fail_unless (gst_pad_set_active (h->start_srcpad, TRUE) == TRUE);
199   fail_unless (gst_pad_set_caps (h->start_srcpad, h->caps) == TRUE);
200 
201   h->tee_sinkpad = gst_element_get_static_pad (h->tee, "sink");
202   fail_if (h->tee_sinkpad == NULL);
203 
204   h->tee_srcpad = gst_element_request_pad_simple (h->tee, "src_%u");
205   fail_if (h->tee_srcpad == NULL);
206 
207   h->final_sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
208   fail_if (h->final_sinkpad == NULL);
209   fail_unless (gst_pad_set_active (h->final_sinkpad, TRUE) == TRUE);
210   fail_unless (gst_pad_set_caps (h->final_sinkpad, h->caps) == TRUE);
211   g_object_set_qdata (G_OBJECT (h->final_sinkpad),
212       g_quark_from_static_string ("buffer-alloc-harness"), h);
213 
214   fail_unless_equals_int (gst_pad_link (h->start_srcpad, h->tee_sinkpad),
215       GST_PAD_LINK_OK);
216   fail_unless_equals_int (gst_pad_link (h->tee_srcpad, h->final_sinkpad),
217       GST_PAD_LINK_OK);
218 }
219 
220 static void
buffer_alloc_harness_teardown(BufferAllocHarness * h)221 buffer_alloc_harness_teardown (BufferAllocHarness * h)
222 {
223   if (h->app_thread)
224     g_thread_join (h->app_thread);
225 
226   gst_pad_set_active (h->final_sinkpad, FALSE);
227   gst_object_unref (h->final_sinkpad);
228   gst_object_unref (h->tee_srcpad);
229   gst_object_unref (h->tee_sinkpad);
230   gst_pad_set_active (h->start_srcpad, FALSE);
231   gst_object_unref (h->start_srcpad);
232   gst_caps_unref (h->caps);
233   gst_check_teardown_element (h->tee);
234 }
235 
236 #if 0
237 static gpointer
238 app_thread_func (gpointer data)
239 {
240   BufferAllocHarness *h = data;
241 
242   /* Signal that we are about to call release_request_pad(). */
243   g_mutex_lock (&check_mutex);
244   h->app_thread_prepped = TRUE;
245   g_cond_signal (&check_cond);
246   g_mutex_unlock (&check_mutex);
247 
248   /* Simulate that the app releases the pad while the streaming thread is in
249    * buffer_alloc below. */
250   gst_element_release_request_pad (h->tee, h->tee_srcpad);
251 
252   /* Signal the bufferalloc function below if it's still waiting. */
253   g_mutex_lock (&check_mutex);
254   h->bufferalloc_blocked = FALSE;
255   g_cond_signal (&check_cond);
256   g_mutex_unlock (&check_mutex);
257 
258   return NULL;
259 }
260 #endif
261 
262 #if 0
263 static GstFlowReturn
264 final_sinkpad_bufferalloc (GstPad * pad, guint64 offset, guint size,
265     GstCaps * caps, GstBuffer ** buf)
266 {
267   BufferAllocHarness *h;
268   gint64 deadline;
269 
270   h = g_object_get_qdata (G_OBJECT (pad),
271       g_quark_from_static_string ("buffer-alloc-harness"));
272   g_assert (h != NULL);
273 
274   if (--(h->countdown) == 0) {
275     /* Time to make the app release the pad. */
276     h->app_thread_prepped = FALSE;
277     h->bufferalloc_blocked = TRUE;
278 
279     h->app_thread = g_thread_try_new ("gst-check", app_thread_func, h, NULL);
280     fail_if (h->app_thread == NULL);
281 
282     /* Wait for the app thread to get ready to call release_request_pad(). */
283     g_mutex_lock (&check_mutex);
284     while (!h->app_thread_prepped)
285       g_cond_wait (&check_cond, &check_mutex);
286     g_mutex_unlock (&check_mutex);
287 
288     /* Now wait for it to do that within a second, to avoid deadlocking
289      * in the event of future changes to the locking semantics. */
290     g_mutex_lock (&check_mutex);
291     deadline = g_get_monotonic_time ();
292     deadline += G_USEC_PER_SEC;
293     while (h->bufferalloc_blocked) {
294       if (!g_cond_wait_until (&check_cond, &check_mutex, deadline))
295         break;
296     }
297     g_mutex_unlock (&check_mutex);
298   }
299 
300   *buf = gst_buffer_new_and_alloc (size);
301   gst_buffer_set_caps (*buf, caps);
302 
303   return GST_FLOW_OK;
304 }
305 #endif
306 
307 /* Simulate an app releasing the pad while the first alloc_buffer() is in
308  * progress. */
GST_START_TEST(test_release_while_buffer_alloc)309 GST_START_TEST (test_release_while_buffer_alloc)
310 {
311   BufferAllocHarness h;
312 
313   buffer_alloc_harness_setup (&h, 1);
314 
315   buffer_alloc_harness_teardown (&h);
316 }
317 
318 GST_END_TEST;
319 
320 /* Simulate an app releasing the pad while the second alloc_buffer() is in
321  * progress. */
GST_START_TEST(test_release_while_second_buffer_alloc)322 GST_START_TEST (test_release_while_second_buffer_alloc)
323 {
324   BufferAllocHarness h;
325 
326   buffer_alloc_harness_setup (&h, 2);
327 
328   buffer_alloc_harness_teardown (&h);
329 }
330 
331 GST_END_TEST;
332 
333 /* Check the internal pads of tee */
GST_START_TEST(test_internal_links)334 GST_START_TEST (test_internal_links)
335 {
336   GstElement *tee;
337   GstPad *sinkpad, *srcpad1, *srcpad2;
338   GstIterator *it;
339   GstIteratorResult res;
340   GValue val1 = { 0, }
341   , val2 = {
342   0,};
343 
344   tee = gst_check_setup_element ("tee");
345 
346   sinkpad = gst_element_get_static_pad (tee, "sink");
347   fail_unless (sinkpad != NULL);
348   it = gst_pad_iterate_internal_links (sinkpad);
349   fail_unless (it != NULL);
350 
351   /* iterator should not return anything */
352   res = gst_iterator_next (it, &val1);
353   fail_unless (res == GST_ITERATOR_DONE);
354   fail_unless (g_value_get_object (&val1) == NULL);
355 
356   srcpad1 = gst_element_request_pad_simple (tee, "src_%u");
357   fail_unless (srcpad1 != NULL);
358 
359   /* iterator should resync */
360   res = gst_iterator_next (it, &val1);
361   fail_unless (res == GST_ITERATOR_RESYNC);
362   fail_unless (g_value_get_object (&val1) == NULL);
363   gst_iterator_resync (it);
364 
365   /* we should get something now */
366   res = gst_iterator_next (it, &val1);
367   fail_unless (res == GST_ITERATOR_OK);
368   fail_unless (GST_PAD_CAST (g_value_get_object (&val1)) == srcpad1);
369 
370   g_value_reset (&val1);
371 
372   res = gst_iterator_next (it, &val1);
373   fail_unless (res == GST_ITERATOR_DONE);
374   fail_unless (g_value_get_object (&val1) == NULL);
375 
376   srcpad2 = gst_element_request_pad_simple (tee, "src_%u");
377   fail_unless (srcpad2 != NULL);
378 
379   /* iterator should resync */
380   res = gst_iterator_next (it, &val1);
381   fail_unless (res == GST_ITERATOR_RESYNC);
382   fail_unless (g_value_get_object (&val1) == NULL);
383   gst_iterator_resync (it);
384 
385   /* we should get one of the 2 pads now */
386   res = gst_iterator_next (it, &val1);
387   fail_unless (res == GST_ITERATOR_OK);
388   fail_unless (GST_PAD_CAST (g_value_get_object (&val1)) == srcpad1
389       || GST_PAD_CAST (g_value_get_object (&val1)) == srcpad2);
390 
391   /* and the other */
392   res = gst_iterator_next (it, &val2);
393   fail_unless (res == GST_ITERATOR_OK);
394   fail_unless (GST_PAD_CAST (g_value_get_object (&val2)) == srcpad1
395       || GST_PAD_CAST (g_value_get_object (&val2)) == srcpad2);
396   fail_unless (g_value_get_object (&val1) != g_value_get_object (&val2));
397   g_value_reset (&val1);
398   g_value_reset (&val2);
399 
400   res = gst_iterator_next (it, &val1);
401   fail_unless (res == GST_ITERATOR_DONE);
402   fail_unless (g_value_get_object (&val1) == NULL);
403 
404   gst_iterator_free (it);
405 
406   /* get an iterator for the other direction */
407   it = gst_pad_iterate_internal_links (srcpad1);
408   fail_unless (it != NULL);
409 
410   res = gst_iterator_next (it, &val1);
411   fail_unless (res == GST_ITERATOR_OK);
412   fail_unless (GST_PAD_CAST (g_value_get_object (&val1)) == sinkpad);
413   g_value_reset (&val1);
414 
415   res = gst_iterator_next (it, &val1);
416   fail_unless (res == GST_ITERATOR_DONE);
417   gst_iterator_free (it);
418 
419   it = gst_pad_iterate_internal_links (srcpad2);
420   fail_unless (it != NULL);
421 
422   res = gst_iterator_next (it, &val1);
423   fail_unless (res == GST_ITERATOR_OK);
424   fail_unless (GST_PAD_CAST (g_value_get_object (&val1)) == sinkpad);
425   g_value_reset (&val1);
426 
427   res = gst_iterator_next (it, &val1);
428   fail_unless (res == GST_ITERATOR_DONE);
429 
430   g_value_unset (&val1);
431   g_value_unset (&val2);
432   gst_iterator_free (it);
433   gst_object_unref (srcpad1);
434   gst_object_unref (srcpad2);
435   gst_object_unref (sinkpad);
436   gst_object_unref (tee);
437 }
438 
439 GST_END_TEST;
440 
441 static GstFlowReturn
_fake_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)442 _fake_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
443 {
444   gst_buffer_unref (buffer);
445   return GST_FLOW_OK;
446 }
447 
448 static GstFlowReturn
_fake_chain_error(GstPad * pad,GstObject * parent,GstBuffer * buffer)449 _fake_chain_error (GstPad * pad, GstObject * parent, GstBuffer * buffer)
450 {
451   gst_buffer_unref (buffer);
452   return GST_FLOW_ERROR;
453 }
454 
GST_START_TEST(test_flow_aggregation)455 GST_START_TEST (test_flow_aggregation)
456 {
457   GstPad *mysrc, *mysink1, *mysink2;
458   GstPad *teesink, *teesrc1, *teesrc2;
459   GstElement *tee;
460   GstBuffer *buffer;
461   GstSegment segment;
462   GstCaps *caps;
463 
464   caps = gst_caps_new_empty_simple ("test/test");
465 
466   tee = gst_element_factory_make ("tee", NULL);
467   fail_unless (tee != NULL);
468   teesink = gst_element_get_static_pad (tee, "sink");
469   fail_unless (teesink != NULL);
470   teesrc1 = gst_element_request_pad_simple (tee, "src_%u");
471   fail_unless (teesrc1 != NULL);
472   teesrc2 = gst_element_request_pad_simple (tee, "src_%u");
473   fail_unless (teesrc2 != NULL);
474 
475   GST_DEBUG ("Creating mysink1");
476   mysink1 = gst_pad_new ("mysink1", GST_PAD_SINK);
477   gst_pad_set_chain_function (mysink1, _fake_chain);
478   gst_pad_set_active (mysink1, TRUE);
479 
480   GST_DEBUG ("Creating mysink2");
481   mysink2 = gst_pad_new ("mysink2", GST_PAD_SINK);
482   gst_pad_set_chain_function (mysink2, _fake_chain);
483   gst_pad_set_active (mysink2, TRUE);
484 
485   GST_DEBUG ("Creating mysrc");
486   mysrc = gst_pad_new ("mysrc", GST_PAD_SRC);
487   gst_pad_set_active (mysrc, TRUE);
488 
489   gst_segment_init (&segment, GST_FORMAT_BYTES);
490   gst_pad_push_event (mysrc, gst_event_new_stream_start ("test"));
491   gst_pad_set_caps (mysrc, caps);
492   gst_pad_push_event (mysrc, gst_event_new_segment (&segment));
493 
494   fail_unless (gst_pad_link (mysrc, teesink) == GST_PAD_LINK_OK);
495   fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
496   fail_unless (gst_pad_link (teesrc2, mysink2) == GST_PAD_LINK_OK);
497 
498   fail_unless (gst_element_set_state (tee,
499           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
500 
501   buffer = gst_buffer_new ();
502 #if 0
503   gst_buffer_set_caps (buffer, caps);
504 #endif
505 
506   GST_DEBUG ("Try to push a buffer");
507   /* First check if everything works in normal state */
508   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
509 
510   /* One pad being in wrong state must result in wrong state */
511   GST_DEBUG ("Trying to push with mysink2 disabled");
512   gst_pad_set_active (mysink2, FALSE);
513   fail_unless (gst_pad_push (mysrc,
514           gst_buffer_ref (buffer)) == GST_FLOW_FLUSHING);
515 
516   GST_DEBUG ("Trying to push with mysink2 disabled");
517   gst_pad_set_active (mysink1, FALSE);
518   gst_pad_set_active (mysink2, TRUE);
519   fail_unless (gst_pad_push (mysrc,
520           gst_buffer_ref (buffer)) == GST_FLOW_FLUSHING);
521 
522   GST_DEBUG ("Trying to push with mysink2 and mysink1 disabled");
523   gst_pad_set_active (mysink2, FALSE);
524   fail_unless (gst_pad_push (mysrc,
525           gst_buffer_ref (buffer)) == GST_FLOW_FLUSHING);
526 
527   /* Test if everything still works in normal state */
528   GST_DEBUG ("Reactivate both pads and try pushing");
529   gst_pad_set_active (mysink1, TRUE);
530   gst_pad_set_active (mysink2, TRUE);
531   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
532 
533   /* One unlinked pad must return OK, two unlinked pads must return NOT_LINKED */
534   GST_DEBUG ("Pushing with mysink1 unlinked");
535   fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
536   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
537 
538   GST_DEBUG ("Pushing with mysink2 unlinked");
539   fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
540   fail_unless (gst_pad_unlink (teesrc2, mysink2) == TRUE);
541   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
542 
543   GST_DEBUG ("Pushing with mysink1 AND mysink2 unlinked");
544   fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
545   fail_unless (gst_pad_push (mysrc,
546           gst_buffer_ref (buffer)) == GST_FLOW_NOT_LINKED);
547 
548   /* Test if everything still works in normal state */
549   GST_DEBUG ("Relink both pads and try pushing");
550   fail_unless (gst_pad_link (teesrc1, mysink1) == GST_PAD_LINK_OK);
551   fail_unless (gst_pad_link (teesrc2, mysink2) == GST_PAD_LINK_OK);
552   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
553 
554   /* One pad returning ERROR should result in ERROR */
555   GST_DEBUG ("Pushing with mysink1 returning GST_FLOW_ERROR");
556   gst_pad_set_chain_function (mysink1, _fake_chain_error);
557   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
558 
559   GST_DEBUG ("Pushing with mysink2 returning GST_FLOW_ERROR");
560   gst_pad_set_chain_function (mysink1, _fake_chain);
561   gst_pad_set_chain_function (mysink2, _fake_chain_error);
562   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
563 
564   GST_DEBUG ("Pushing with mysink1 AND mysink2 returning GST_FLOW_ERROR");
565   gst_pad_set_chain_function (mysink1, _fake_chain_error);
566   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_ERROR);
567 
568   /* And now everything still needs to work */
569   GST_DEBUG ("Try pushing with everything ok");
570   gst_pad_set_chain_function (mysink1, _fake_chain);
571   gst_pad_set_chain_function (mysink2, _fake_chain);
572   fail_unless (gst_pad_push (mysrc, gst_buffer_ref (buffer)) == GST_FLOW_OK);
573 
574   fail_unless (gst_element_set_state (tee,
575           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
576 
577   fail_unless (gst_pad_unlink (mysrc, teesink) == TRUE);
578   fail_unless (gst_pad_unlink (teesrc1, mysink1) == TRUE);
579   fail_unless (gst_pad_unlink (teesrc2, mysink2) == TRUE);
580 
581 
582   gst_object_unref (teesink);
583   gst_object_unref (teesrc1);
584   gst_object_unref (teesrc2);
585   gst_element_release_request_pad (tee, teesrc1);
586   gst_element_release_request_pad (tee, teesrc2);
587   gst_object_unref (tee);
588 
589   gst_object_unref (mysink1);
590   gst_object_unref (mysink2);
591   gst_object_unref (mysrc);
592   gst_caps_unref (caps);
593   gst_buffer_unref (buffer);
594 }
595 
596 GST_END_TEST;
597 
GST_START_TEST(test_request_pads)598 GST_START_TEST (test_request_pads)
599 {
600   GstElement *tee;
601   GstPad *srcpad1, *srcpad2, *srcpad3, *srcpad4;
602 
603   tee = gst_check_setup_element ("tee");
604 
605   srcpad1 = gst_element_request_pad_simple (tee, "src_%u");
606   fail_unless (srcpad1 != NULL);
607   fail_unless_equals_string (GST_OBJECT_NAME (srcpad1), "src_0");
608   srcpad2 = gst_element_request_pad_simple (tee, "src_100");
609   fail_unless (srcpad2 != NULL);
610   fail_unless_equals_string (GST_OBJECT_NAME (srcpad2), "src_100");
611   srcpad3 = gst_element_request_pad_simple (tee, "src_10");
612   fail_unless (srcpad3 != NULL);
613   fail_unless_equals_string (GST_OBJECT_NAME (srcpad3), "src_10");
614   srcpad4 = gst_element_request_pad_simple (tee, "src_%u");
615   fail_unless (srcpad4 != NULL);
616 
617   gst_object_unref (srcpad1);
618   gst_object_unref (srcpad2);
619   gst_object_unref (srcpad3);
620   gst_object_unref (srcpad4);
621   gst_object_unref (tee);
622 }
623 
624 GST_END_TEST;
625 
GST_START_TEST(test_allow_not_linked)626 GST_START_TEST (test_allow_not_linked)
627 {
628   GstElement *tee;
629   GstPad *src1, *src2;
630   GstBuffer *buffer;
631   GstPad *srcpad;
632   GstCaps *caps;
633   GstSegment segment;
634 
635   static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
636       GST_PAD_SRC,
637       GST_PAD_ALWAYS,
638       GST_STATIC_CAPS_ANY);
639 
640   caps = gst_caps_new_empty_simple ("test/test");
641 
642   tee = gst_check_setup_element ("tee");
643   fail_unless (tee);
644   g_object_set (tee, "allow-not-linked", TRUE, NULL);
645 
646   srcpad = gst_check_setup_src_pad (tee, &srctemplate);
647   gst_pad_set_active (srcpad, TRUE);
648 
649   gst_pad_push_event (srcpad, gst_event_new_stream_start ("test"));
650   gst_segment_init (&segment, GST_FORMAT_BYTES);
651   gst_pad_push_event (srcpad, gst_event_new_stream_start ("test"));
652   gst_pad_set_caps (srcpad, caps);
653   gst_caps_unref (caps);
654   gst_pad_push_event (srcpad, gst_event_new_segment (&segment));
655 
656   fail_unless (gst_element_set_state (tee,
657           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
658 
659   buffer = gst_buffer_new ();
660   fail_unless (buffer);
661 
662   fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
663 
664   src1 = gst_element_request_pad_simple (tee, "src_%u");
665 
666   fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
667 
668   src2 = gst_element_request_pad_simple (tee, "src_%u");
669 
670   fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK);
671 
672   g_object_set (tee, "allow-not-linked", FALSE, NULL);
673 
674   fail_unless (gst_pad_push (srcpad,
675           gst_buffer_ref (buffer)) == GST_FLOW_NOT_LINKED);
676 
677   gst_element_release_request_pad (tee, src1);
678 
679   fail_unless (gst_pad_push (srcpad,
680           gst_buffer_ref (buffer)) == GST_FLOW_NOT_LINKED);
681 
682   gst_element_release_request_pad (tee, src2);
683   gst_object_unref (src1);
684   gst_object_unref (src2);
685 
686   fail_unless (gst_pad_push (srcpad,
687           gst_buffer_ref (buffer)) == GST_FLOW_NOT_LINKED);
688 
689   gst_pad_set_active (srcpad, FALSE);
690   gst_check_teardown_src_pad (tee);
691   gst_check_teardown_element (tee);
692 
693   fail_if (buffer->mini_object.refcount != 1);
694   gst_buffer_unref (buffer);
695 }
696 
697 GST_END_TEST;
698 
699 static gboolean
allocation_query_empty(GstPad * pad,GstObject * parent,GstQuery * query)700 allocation_query_empty (GstPad * pad, GstObject * parent, GstQuery * query)
701 {
702   if (GST_QUERY_TYPE (query) != GST_QUERY_ALLOCATION)
703     return gst_pad_query_default (pad, parent, query);
704 
705   return TRUE;
706 }
707 
708 static gboolean
allocation_query1(GstPad * pad,GstObject * parent,GstQuery * query)709 allocation_query1 (GstPad * pad, GstObject * parent, GstQuery * query)
710 {
711   GstAllocationParams param = { 0, 15, 1, 1 };
712 
713   if (GST_QUERY_TYPE (query) != GST_QUERY_ALLOCATION)
714     return gst_pad_query_default (pad, parent, query);
715 
716   gst_query_add_allocation_pool (query, NULL, 128, 2, 10);
717   gst_query_add_allocation_param (query, NULL, &param);
718   gst_query_add_allocation_meta (query, GST_PARENT_BUFFER_META_API_TYPE, NULL);
719   gst_query_add_allocation_meta (query, GST_REFERENCE_TIMESTAMP_META_API_TYPE,
720       NULL);
721   gst_query_add_allocation_meta (query, GST_PROTECTION_META_API_TYPE, NULL);
722 
723   return TRUE;
724 }
725 
726 static gboolean
allocation_query2(GstPad * pad,GstObject * parent,GstQuery * query)727 allocation_query2 (GstPad * pad, GstObject * parent, GstQuery * query)
728 {
729   GstAllocationParams param = { 0, 7, 2, 1 };
730 
731   if (GST_QUERY_TYPE (query) != GST_QUERY_ALLOCATION)
732     return gst_pad_query_default (pad, parent, query);
733 
734   gst_query_add_allocation_pool (query, NULL, 129, 1, 15);
735   gst_query_add_allocation_param (query, NULL, &param);
736   gst_query_add_allocation_meta (query, GST_PARENT_BUFFER_META_API_TYPE, NULL);
737   gst_query_add_allocation_meta (query, GST_REFERENCE_TIMESTAMP_META_API_TYPE,
738       NULL);
739   gst_query_add_allocation_meta (query, GST_PROTECTION_META_API_TYPE, NULL);
740 
741   return TRUE;
742 }
743 
744 static gboolean
allocation_query3(GstPad * pad,GstObject * parent,GstQuery * query)745 allocation_query3 (GstPad * pad, GstObject * parent, GstQuery * query)
746 {
747   GstStructure *s;
748   GstAllocationParams param = { 0, 7, 1, 2 };
749 
750   if (GST_QUERY_TYPE (query) != GST_QUERY_ALLOCATION)
751     return gst_pad_query_default (pad, parent, query);
752 
753   gst_query_add_allocation_pool (query, NULL, 130, 1, 20);
754   gst_query_add_allocation_param (query, NULL, &param);
755   gst_query_add_allocation_meta (query, GST_PARENT_BUFFER_META_API_TYPE, NULL);
756   s = gst_structure_new_empty ("test/test");
757   gst_query_add_allocation_meta (query, GST_PROTECTION_META_API_TYPE, s);
758   gst_structure_free (s);
759 
760   return TRUE;
761 }
762 
763 static gboolean
allocation_query_fail(GstPad * pad,GstObject * parent,GstQuery * query)764 allocation_query_fail (GstPad * pad, GstObject * parent, GstQuery * query)
765 {
766   if (GST_QUERY_TYPE (query) != GST_QUERY_ALLOCATION)
767     return gst_pad_query_default (pad, parent, query);
768 
769   return FALSE;
770 }
771 
772 static void
add_sink_pad_and_setup_query_func(GstElement * tee,GstPadQueryFunction query_func)773 add_sink_pad_and_setup_query_func (GstElement * tee,
774     GstPadQueryFunction query_func)
775 {
776   GstPad *sink;
777   static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
778       GST_PAD_SINK,
779       GST_PAD_ALWAYS,
780       GST_STATIC_CAPS_ANY);
781 
782   sink = gst_check_setup_sink_pad_by_name (tee, &sinktemplate, "src_%u");
783   fail_unless (sink != NULL);
784   gst_pad_set_query_function (sink, query_func);
785 }
786 
GST_START_TEST(test_allocation_query_aggregation)787 GST_START_TEST (test_allocation_query_aggregation)
788 {
789   GstElement *tee;
790   GstPad *sinkpad;
791   GstCaps *caps;
792   GstQuery *query;
793   guint size, min, max;
794   GstAllocationParams param;
795 
796   tee = gst_check_setup_element ("tee");
797   fail_unless (tee);
798 
799   sinkpad = gst_element_get_static_pad (tee, "sink");
800   add_sink_pad_and_setup_query_func (tee, allocation_query1);
801   add_sink_pad_and_setup_query_func (tee, allocation_query2);
802   add_sink_pad_and_setup_query_func (tee, allocation_query3);
803 
804   caps = gst_caps_new_empty_simple ("test/test");
805   query = gst_query_new_allocation (caps, TRUE);
806   fail_unless (gst_pad_query (sinkpad, query));
807 
808   ck_assert_int_eq (gst_query_get_n_allocation_pools (query), 1);
809   gst_query_parse_nth_allocation_pool (query, 0, NULL, &size, &min, &max);
810   fail_unless (size == 130);
811   /* The tee will allocate one more buffer when multiplexing */
812   fail_unless (min == 2 + 1);
813   fail_unless (max == 0);
814 
815   fail_unless (gst_query_get_n_allocation_params (query), 1);
816   gst_query_parse_nth_allocation_param (query, 0, NULL, &param);
817   fail_unless (param.align == 15);
818   fail_unless (param.prefix == 2);
819   fail_unless (param.padding == 2);
820 
821   fail_unless (gst_query_get_n_allocation_metas (query), 1);
822   fail_unless (gst_query_parse_nth_allocation_meta (query, 0, NULL) ==
823       GST_PARENT_BUFFER_META_API_TYPE);
824 
825   gst_caps_unref (caps);
826   gst_query_unref (query);
827   gst_check_teardown_pad_by_name (tee, "src_0");
828   gst_check_teardown_pad_by_name (tee, "src_1");
829   gst_check_teardown_pad_by_name (tee, "src_2");
830   gst_object_unref (sinkpad);
831   gst_check_teardown_element (tee);
832 }
833 
834 GST_END_TEST;
835 
836 
GST_START_TEST(test_allocation_query_allow_not_linked)837 GST_START_TEST (test_allocation_query_allow_not_linked)
838 {
839   GstElement *tee;
840   GstPad *sinkpad, *srcpad;
841   GstCaps *caps;
842   GstQuery *query;
843 
844   tee = gst_check_setup_element ("tee");
845   fail_unless (tee);
846 
847   sinkpad = gst_element_get_static_pad (tee, "sink");
848   add_sink_pad_and_setup_query_func (tee, allocation_query1);
849   add_sink_pad_and_setup_query_func (tee, allocation_query2);
850   add_sink_pad_and_setup_query_func (tee, allocation_query3);
851   /* This unlinked pad is what will make a difference between having
852    * allow-not-linked set or not */
853   srcpad = gst_element_request_pad_simple (tee, "src_%u");
854   caps = gst_caps_new_empty_simple ("test/test");
855 
856   /* Without allow-not-linked the query should fail */
857   query = gst_query_new_allocation (caps, TRUE);
858   fail_if (gst_pad_query (sinkpad, query));
859 
860   /* While with allow-not-linked it should succeed (ignoring that pad) */
861   g_object_set (tee, "allow-not-linked", TRUE, NULL);
862   gst_query_unref (query);
863   query = gst_query_new_allocation (caps, TRUE);
864   fail_unless (gst_pad_query (sinkpad, query));
865 
866   gst_caps_unref (caps);
867   gst_query_unref (query);
868   gst_check_teardown_pad_by_name (tee, "src_0");
869   gst_check_teardown_pad_by_name (tee, "src_1");
870   gst_check_teardown_pad_by_name (tee, "src_2");
871   gst_element_release_request_pad (tee, srcpad);
872   gst_object_unref (srcpad);
873   gst_object_unref (sinkpad);
874   gst_check_teardown_element (tee);
875 }
876 
877 GST_END_TEST;
878 
879 
GST_START_TEST(test_allocation_query_failure)880 GST_START_TEST (test_allocation_query_failure)
881 {
882   GstElement *tee;
883   GstPad *sinkpad;
884   GstCaps *caps;
885   GstQuery *query;
886 
887   tee = gst_check_setup_element ("tee");
888   fail_unless (tee);
889   g_object_set (tee, "allow-not-linked", TRUE, NULL);
890 
891   sinkpad = gst_element_get_static_pad (tee, "sink");
892   add_sink_pad_and_setup_query_func (tee, allocation_query1);
893   add_sink_pad_and_setup_query_func (tee, allocation_query2);
894   add_sink_pad_and_setup_query_func (tee, allocation_query_fail);
895 
896   caps = gst_caps_new_empty_simple ("test/test");
897   query = gst_query_new_allocation (caps, TRUE);
898   fail_if (gst_pad_query (sinkpad, query));
899 
900   gst_caps_unref (caps);
901   gst_query_unref (query);
902   gst_check_teardown_pad_by_name (tee, "src_0");
903   gst_check_teardown_pad_by_name (tee, "src_1");
904   gst_check_teardown_pad_by_name (tee, "src_2");
905   gst_object_unref (sinkpad);
906   gst_check_teardown_element (tee);
907 }
908 
909 GST_END_TEST;
910 
911 
GST_START_TEST(test_allocation_query_empty)912 GST_START_TEST (test_allocation_query_empty)
913 {
914   GstElement *tee;
915   GstPad *sinkpad;
916   GstCaps *caps;
917   GstQuery *query;
918 
919   tee = gst_check_setup_element ("tee");
920   fail_unless (tee);
921 
922   sinkpad = gst_element_get_static_pad (tee, "sink");
923   add_sink_pad_and_setup_query_func (tee, allocation_query_empty);
924 
925   caps = gst_caps_new_empty_simple ("test/test");
926 
927   query = gst_query_new_allocation (caps, TRUE);
928   fail_unless (gst_pad_query (sinkpad, query));
929 
930   ck_assert_int_eq (gst_query_get_n_allocation_pools (query), 0);
931   ck_assert_int_eq (gst_query_get_n_allocation_params (query), 0);
932 
933   gst_caps_unref (caps);
934   gst_query_unref (query);
935   gst_check_teardown_pad_by_name (tee, "src_0");
936   gst_object_unref (sinkpad);
937   gst_check_teardown_element (tee);
938 }
939 
940 GST_END_TEST;
941 
942 
943 static Suite *
tee_suite(void)944 tee_suite (void)
945 {
946   Suite *s = suite_create ("tee");
947   TCase *tc_chain = tcase_create ("general");
948 
949   /* Set the timeout to a much larger time - 3 minutes */
950   tcase_set_timeout (tc_chain, 180);
951 
952   suite_add_tcase (s, tc_chain);
953   tcase_add_test (tc_chain, test_num_buffers);
954   tcase_add_test (tc_chain, test_stress);
955   tcase_add_test (tc_chain, test_release_while_buffer_alloc);
956   tcase_add_test (tc_chain, test_release_while_second_buffer_alloc);
957   tcase_add_test (tc_chain, test_internal_links);
958   tcase_add_test (tc_chain, test_flow_aggregation);
959   tcase_add_test (tc_chain, test_request_pads);
960   tcase_add_test (tc_chain, test_allow_not_linked);
961   tcase_add_test (tc_chain, test_allocation_query_aggregation);
962   tcase_add_test (tc_chain, test_allocation_query_allow_not_linked);
963   tcase_add_test (tc_chain, test_allocation_query_failure);
964   tcase_add_test (tc_chain, test_allocation_query_empty);
965 
966   return s;
967 }
968 
969 GST_CHECK_MAIN (tee);
970