plugins/elements/gsttee.*: Protect pad_alloc with a new lock so that we can be sure...
[platform/upstream/gstreamer.git] / tests / check / elements / tee.c
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., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #include <gst/check/gstcheck.h>
31
32 static void
33 handoff (GstElement * fakesink, GstBuffer * buf, GstPad * pad, guint * count)
34 {
35   *count = *count + 1;
36 }
37
38 /* construct fakesrc num-buffers=3 ! tee name=t ! queue ! fakesink t. ! queue !
39  * fakesink. Each fakesink should exactly receive 3 buffers.
40  */
41 GST_START_TEST (test_num_buffers)
42 {
43 #define NUM_SUBSTREAMS 15
44 #define NUM_BUFFERS 3
45   GstElement *pipeline, *src, *tee;
46   GstElement *queues[NUM_SUBSTREAMS];
47   GstElement *sinks[NUM_SUBSTREAMS];
48   GstPad *req_pads[NUM_SUBSTREAMS];
49   guint counts[NUM_SUBSTREAMS];
50   GstBus *bus;
51   GstMessage *msg;
52   gint i;
53
54   pipeline = gst_pipeline_new ("pipeline");
55   src = gst_check_setup_element ("fakesrc");
56   g_object_set (src, "num-buffers", NUM_BUFFERS, NULL);
57   tee = gst_check_setup_element ("tee");
58   fail_unless (gst_bin_add (GST_BIN (pipeline), src));
59   fail_unless (gst_bin_add (GST_BIN (pipeline), tee));
60   fail_unless (gst_element_link (src, tee));
61
62   for (i = 0; i < NUM_SUBSTREAMS; ++i) {
63     GstPad *qpad;
64     gchar name[32];
65
66     counts[i] = 0;
67
68     queues[i] = gst_check_setup_element ("queue");
69     g_snprintf (name, 32, "queue%d", i);
70     gst_object_set_name (GST_OBJECT (queues[i]), name);
71     fail_unless (gst_bin_add (GST_BIN (pipeline), queues[i]));
72
73     sinks[i] = gst_check_setup_element ("fakesink");
74     g_snprintf (name, 32, "sink%d", i);
75     gst_object_set_name (GST_OBJECT (sinks[i]), name);
76     fail_unless (gst_bin_add (GST_BIN (pipeline), sinks[i]));
77     fail_unless (gst_element_link (queues[i], sinks[i]));
78     g_object_set (sinks[i], "signal-handoffs", TRUE, NULL);
79     g_signal_connect (sinks[i], "handoff", (GCallback) handoff, &counts[i]);
80
81     req_pads[i] = gst_element_get_request_pad (tee, "src%d");
82     fail_unless (req_pads[i] != NULL);
83
84     qpad = gst_element_get_static_pad (queues[i], "sink");
85     fail_unless_equals_int (gst_pad_link (req_pads[i], qpad), GST_PAD_LINK_OK);
86     gst_object_unref (qpad);
87   }
88
89   bus = gst_element_get_bus (pipeline);
90   fail_if (bus == NULL);
91   gst_element_set_state (pipeline, GST_STATE_PLAYING);
92
93   msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
94   fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
95   gst_message_unref (msg);
96
97   for (i = 0; i < NUM_SUBSTREAMS; ++i) {
98     fail_unless_equals_int (counts[i], NUM_BUFFERS);
99   }
100
101   gst_element_set_state (pipeline, GST_STATE_NULL);
102   gst_object_unref (bus);
103
104   for (i = 0; i < NUM_SUBSTREAMS; ++i) {
105     gst_element_release_request_pad (tee, req_pads[i]);
106     gst_object_unref (req_pads[i]);
107   }
108   gst_object_unref (pipeline);
109 }
110
111 GST_END_TEST;
112
113 /* we use fakesrc ! tee ! fakesink and then randomly request/release and link
114  * some pads from tee. This should happily run without any errors. */
115 GST_START_TEST (test_stress)
116 {
117   GstElement *pipeline;
118   GstElement *tee;
119   gchar *desc;
120   GstBus *bus;
121   GstMessage *msg;
122   gint i;
123
124   /* Pump 1000 buffers (10 bytes each) per second through tee for 5 secs */
125   desc = "fakesrc datarate=10000 sizemin=10 sizemax=10 num-buffers=5000 ! "
126       "video/x-raw-rgb,framerate=25/1 ! tee name=t ! "
127       "queue max-size-buffers=2 ! fakesink sync=true";
128
129   pipeline = gst_parse_launch (desc, NULL);
130   fail_if (pipeline == NULL);
131
132   tee = gst_bin_get_by_name (GST_BIN (pipeline), "t");
133   fail_if (tee == NULL);
134
135   /* bring the pipeline to PLAYING, then start switching */
136   bus = gst_element_get_bus (pipeline);
137   fail_if (bus == NULL);
138   gst_element_set_state (pipeline, GST_STATE_PLAYING);
139   /* Wait for the pipeline to hit playing so that parse_launch can do the
140    * initial link, otherwise we perform linking from multiple threads and cause
141    * trouble */
142   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
143
144   for (i = 0; i < 50000; i++) {
145     GstPad *pad;
146
147     pad = gst_element_get_request_pad (tee, "src%d");
148     gst_element_release_request_pad (tee, pad);
149     gst_object_unref (pad);
150
151     if ((msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, 0)))
152       break;
153   }
154
155   /* now wait for completion or error */
156   if (msg == NULL)
157     msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
158   fail_if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_EOS);
159   gst_message_unref (msg);
160
161   gst_element_set_state (pipeline, GST_STATE_NULL);
162   gst_object_unref (tee);
163   gst_object_unref (bus);
164   gst_object_unref (pipeline);
165 }
166
167 GST_END_TEST;
168
169 static GstFlowReturn
170 final_sinkpad_bufferalloc (GstPad * pad, guint64 offset, guint size,
171     GstCaps * caps, GstBuffer ** buf);
172
173 typedef struct
174 {
175   GstElement *tee;
176   GstCaps *caps;
177   GstPad *start_srcpad;
178   GstPad *tee_sinkpad;
179   GstPad *tee_srcpad;
180   GstPad *final_sinkpad;
181   GThread *app_thread;
182   gint countdown;
183   gboolean app_thread_prepped;
184   gboolean bufferalloc_blocked;
185 } BufferAllocHarness;
186
187 void
188 buffer_alloc_harness_setup (BufferAllocHarness * h, gint countdown)
189 {
190   h->tee = gst_check_setup_element ("tee");
191   fail_if (h->tee == NULL);
192
193   h->countdown = countdown;
194
195   fail_unless_equals_int (gst_element_set_state (h->tee, GST_STATE_PLAYING),
196       TRUE);
197
198   h->caps = gst_caps_new_simple ("video/x-raw-yuv", NULL);
199
200   h->start_srcpad = gst_pad_new ("src", GST_PAD_SRC);
201   fail_if (h->start_srcpad == NULL);
202   fail_unless (gst_pad_set_caps (h->start_srcpad, h->caps) == TRUE);
203   fail_unless (gst_pad_set_active (h->start_srcpad, TRUE) == TRUE);
204
205   h->tee_sinkpad = gst_element_get_static_pad (h->tee, "sink");
206   fail_if (h->tee_sinkpad == NULL);
207
208   h->tee_srcpad = gst_element_get_request_pad (h->tee, "src%d");
209   fail_if (h->tee_srcpad == NULL);
210
211   h->final_sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
212   fail_if (h->final_sinkpad == NULL);
213   gst_pad_set_bufferalloc_function (h->final_sinkpad,
214       final_sinkpad_bufferalloc);
215   fail_unless (gst_pad_set_caps (h->final_sinkpad, h->caps) == TRUE);
216   fail_unless (gst_pad_set_active (h->final_sinkpad, TRUE) == TRUE);
217   g_object_set_qdata (G_OBJECT (h->final_sinkpad),
218       g_quark_from_static_string ("buffer-alloc-harness"), h);
219
220   fail_unless_equals_int (gst_pad_link (h->start_srcpad, h->tee_sinkpad),
221       GST_PAD_LINK_OK);
222   fail_unless_equals_int (gst_pad_link (h->tee_srcpad, h->final_sinkpad),
223       GST_PAD_LINK_OK);
224 }
225
226 void
227 buffer_alloc_harness_teardown (BufferAllocHarness * h)
228 {
229   g_thread_join (h->app_thread);
230
231   gst_pad_set_active (h->final_sinkpad, FALSE);
232   gst_object_unref (h->final_sinkpad);
233   gst_object_unref (h->tee_srcpad);
234   gst_object_unref (h->tee_sinkpad);
235   gst_pad_set_active (h->start_srcpad, FALSE);
236   gst_object_unref (h->start_srcpad);
237   gst_caps_unref (h->caps);
238   gst_check_teardown_element (h->tee);
239 }
240
241 static gpointer
242 app_thread_func (gpointer data)
243 {
244   BufferAllocHarness *h = data;
245
246   /* Signal that we are about to call release_request_pad(). */
247   g_mutex_lock (check_mutex);
248   h->app_thread_prepped = TRUE;
249   g_cond_signal (check_cond);
250   g_mutex_unlock (check_mutex);
251
252   /* Simulate that the app releases the pad while the streaming thread is in
253    * buffer_alloc below. */
254   gst_element_release_request_pad (h->tee, h->tee_srcpad);
255
256   /* Signal the bufferalloc function below if it's still waiting. */
257   g_mutex_lock (check_mutex);
258   h->bufferalloc_blocked = FALSE;
259   g_cond_signal (check_cond);
260   g_mutex_unlock (check_mutex);
261
262   return NULL;
263 }
264
265 static GstFlowReturn
266 final_sinkpad_bufferalloc (GstPad * pad, guint64 offset, guint size,
267     GstCaps * caps, GstBuffer ** buf)
268 {
269   BufferAllocHarness *h;
270   GTimeVal deadline;
271
272   h = g_object_get_qdata (G_OBJECT (pad),
273       g_quark_from_static_string ("buffer-alloc-harness"));
274   g_assert (h != NULL);
275
276   if (--(h->countdown) == 0) {
277     /* Time to make the app release the pad. */
278     h->app_thread_prepped = FALSE;
279     h->bufferalloc_blocked = TRUE;
280
281     h->app_thread = g_thread_create (app_thread_func, h, TRUE, NULL);
282     fail_if (h->app_thread == NULL);
283
284     /* Wait for the app thread to get ready to call release_request_pad(). */
285     g_mutex_lock (check_mutex);
286     while (!h->app_thread_prepped)
287       g_cond_wait (check_cond, check_mutex);
288     g_mutex_unlock (check_mutex);
289
290     /* Now wait for it to do that within a second, to avoid deadlocking
291      * in the event of future changes to the locking semantics. */
292     g_mutex_lock (check_mutex);
293     g_get_current_time (&deadline);
294     deadline.tv_sec += 1;
295     while (h->bufferalloc_blocked) {
296       if (!g_cond_timed_wait (check_cond, check_mutex, &deadline))
297         break;
298     }
299     g_mutex_unlock (check_mutex);
300   }
301
302   *buf = gst_buffer_new_and_alloc (size);
303   gst_buffer_set_caps (*buf, caps);
304
305   return GST_FLOW_OK;
306 }
307
308 /* Simulate an app releasing the pad while the first alloc_buffer() is in
309  * progress. */
310 GST_START_TEST (test_release_while_buffer_alloc)
311 {
312   BufferAllocHarness h;
313   GstBuffer *buf;
314
315   buffer_alloc_harness_setup (&h, 1);
316
317   fail_unless_equals_int (gst_pad_alloc_buffer (h.start_srcpad, 0, 1, h.caps,
318           &buf), GST_FLOW_OK);
319   gst_buffer_unref (buf);
320
321   buffer_alloc_harness_teardown (&h);
322 }
323
324 GST_END_TEST;
325
326 /* Simulate an app releasing the pad while the second alloc_buffer() is in
327  * progress. */
328 GST_START_TEST (test_release_while_second_buffer_alloc)
329 {
330   BufferAllocHarness h;
331   GstBuffer *buf;
332
333   buffer_alloc_harness_setup (&h, 2);
334
335   fail_unless_equals_int (gst_pad_alloc_buffer (h.start_srcpad, 0, 1, h.caps,
336           &buf), GST_FLOW_OK);
337   gst_buffer_unref (buf);
338
339   fail_unless_equals_int (gst_pad_alloc_buffer (h.start_srcpad, 0, 1, h.caps,
340           &buf), GST_FLOW_OK);
341   gst_buffer_unref (buf);
342
343   buffer_alloc_harness_teardown (&h);
344 }
345
346 GST_END_TEST;
347
348 static Suite *
349 tee_suite (void)
350 {
351   Suite *s = suite_create ("tee");
352   TCase *tc_chain = tcase_create ("general");
353
354   suite_add_tcase (s, tc_chain);
355   tcase_add_test (tc_chain, test_num_buffers);
356   tcase_add_test (tc_chain, test_stress);
357   tcase_add_test (tc_chain, test_release_while_buffer_alloc);
358   tcase_add_test (tc_chain, test_release_while_second_buffer_alloc);
359
360   return s;
361 }
362
363 GST_CHECK_MAIN (tee);