tests: remove the hacks to workaround the pad-leak
[platform/upstream/gstreamer.git] / tests / check / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * gstpad.c: Unit test for GstPad
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 GST_START_TEST (test_link)
25 {
26   GstPad *src, *sink;
27   GstPadTemplate *srct;
28
29   GstPadLinkReturn ret;
30   gchar *name;
31
32   src = gst_pad_new ("source", GST_PAD_SRC);
33   fail_if (src == NULL);
34   ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
35
36   name = gst_pad_get_name (src);
37   fail_unless (strcmp (name, "source") == 0);
38   ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
39   g_free (name);
40
41   sink = gst_pad_new ("sink", GST_PAD_SINK);
42   fail_if (sink == NULL);
43
44   /* linking without templates or caps should fail */
45   ret = gst_pad_link (src, sink);
46   ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
47   ASSERT_OBJECT_REFCOUNT (sink, "sink pad", 1);
48   fail_unless (ret == GST_PAD_LINK_NOFORMAT);
49
50   ASSERT_CRITICAL (gst_pad_get_pad_template (NULL));
51
52   srct = gst_pad_get_pad_template (src);
53   fail_unless (srct == NULL);
54   ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
55
56   /* clean up */
57   ASSERT_OBJECT_REFCOUNT (src, "source pad", 1);
58   gst_object_unref (src);
59   gst_object_unref (sink);
60 }
61
62 GST_END_TEST;
63
64 /* threaded link/unlink */
65 /* use globals */
66 static GstPad *src, *sink;
67
68 static void
69 thread_link_unlink (gpointer data)
70 {
71   THREAD_START ();
72
73   while (THREAD_TEST_RUNNING ()) {
74     gst_pad_link (src, sink);
75     gst_pad_unlink (src, sink);
76     THREAD_SWITCH ();
77   }
78 }
79
80 GST_START_TEST (test_link_unlink_threaded)
81 {
82   GstCaps *caps;
83   int i;
84
85   src = gst_pad_new ("source", GST_PAD_SRC);
86   fail_if (src == NULL);
87   sink = gst_pad_new ("sink", GST_PAD_SINK);
88   fail_if (sink == NULL);
89
90   caps = gst_caps_from_string ("foo/bar");
91   gst_pad_set_caps (src, caps);
92   gst_pad_set_caps (sink, caps);
93   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
94
95   MAIN_START_THREADS (5, thread_link_unlink, NULL);
96   for (i = 0; i < 1000; ++i) {
97     gst_pad_is_linked (src);
98     gst_pad_is_linked (sink);
99     THREAD_SWITCH ();
100   }
101   MAIN_STOP_THREADS ();
102
103   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
104   gst_caps_unref (caps);
105
106   ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
107   gst_object_unref (src);
108   gst_object_unref (sink);
109 }
110
111 GST_END_TEST;
112
113 GST_START_TEST (test_refcount)
114 {
115   GstPad *src, *sink;
116   GstCaps *caps;
117   GstPadLinkReturn plr;
118
119   sink = gst_pad_new ("sink", GST_PAD_SINK);
120   fail_if (sink == NULL);
121
122   src = gst_pad_new ("src", GST_PAD_SRC);
123   fail_if (src == NULL);
124
125   caps = gst_caps_from_string ("foo/bar");
126   /* one for me */
127   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
128
129   gst_pad_set_caps (src, caps);
130   gst_pad_set_caps (sink, caps);
131   /* one for me and one for each set_caps */
132   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
133
134   plr = gst_pad_link (src, sink);
135   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
136   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
137
138   gst_pad_unlink (src, sink);
139   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
140
141   /* cleanup */
142   gst_object_unref (src);
143   gst_object_unref (sink);
144   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
145
146   gst_caps_unref (caps);
147 }
148
149 GST_END_TEST;
150
151 GST_START_TEST (test_get_allowed_caps)
152 {
153   GstPad *src, *sink;
154   GstCaps *caps, *gotcaps;
155   GstBuffer *buffer;
156   GstPadLinkReturn plr;
157
158   ASSERT_CRITICAL (gst_pad_get_allowed_caps (NULL));
159
160   buffer = gst_buffer_new ();
161   ASSERT_CRITICAL (gst_pad_get_allowed_caps ((GstPad *) buffer));
162   gst_buffer_unref (buffer);
163
164   src = gst_pad_new ("src", GST_PAD_SRC);
165   fail_if (src == NULL);
166   caps = gst_pad_get_allowed_caps (src);
167   fail_unless (caps == NULL);
168
169   caps = gst_caps_from_string ("foo/bar");
170
171   sink = gst_pad_new ("sink", GST_PAD_SINK);
172   gst_pad_set_caps (src, caps);
173   gst_pad_set_caps (sink, caps);
174   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
175
176   plr = gst_pad_link (src, sink);
177   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
178
179   gotcaps = gst_pad_get_allowed_caps (src);
180   fail_if (gotcaps == NULL);
181   fail_unless (gst_caps_is_equal (gotcaps, caps));
182
183   ASSERT_CAPS_REFCOUNT (gotcaps, "gotcaps", 1);
184   gst_caps_unref (gotcaps);
185
186   gst_pad_unlink (src, sink);
187
188   /* cleanup */
189   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
190   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
191   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
192
193   gst_object_unref (src);
194   gst_object_unref (sink);
195
196   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
197   gst_caps_unref (caps);
198 }
199
200 GST_END_TEST;
201
202 static gboolean
203 name_is_valid (const gchar * name, GstPadPresence presence)
204 {
205   GstPadTemplate *new;
206   GstCaps *any = GST_CAPS_ANY;
207
208   new = gst_pad_template_new (name, GST_PAD_SRC, presence, any);
209   if (new) {
210     gst_object_unref (GST_OBJECT (new));
211     return TRUE;
212   }
213   return FALSE;
214 }
215
216 GST_START_TEST (test_name_is_valid)
217 {
218   gboolean result = FALSE;
219
220   fail_unless (name_is_valid ("src", GST_PAD_ALWAYS));
221   ASSERT_WARNING (name_is_valid ("src%", GST_PAD_ALWAYS));
222   ASSERT_WARNING (result = name_is_valid ("src%d", GST_PAD_ALWAYS));
223   fail_if (result);
224
225   fail_unless (name_is_valid ("src", GST_PAD_REQUEST));
226   ASSERT_WARNING (name_is_valid ("src%s%s", GST_PAD_REQUEST));
227   ASSERT_WARNING (name_is_valid ("src%c", GST_PAD_REQUEST));
228   ASSERT_WARNING (name_is_valid ("src%", GST_PAD_REQUEST));
229   ASSERT_WARNING (name_is_valid ("src%dsrc", GST_PAD_REQUEST));
230
231   fail_unless (name_is_valid ("src", GST_PAD_SOMETIMES));
232   fail_unless (name_is_valid ("src%c", GST_PAD_SOMETIMES));
233 }
234
235 GST_END_TEST;
236
237 static gboolean
238 _probe_handler (GstPad * pad, GstBuffer * buffer, gpointer userdata)
239 {
240   gint ret = GPOINTER_TO_INT (userdata);
241
242   if (ret == 1)
243     return TRUE;
244   return FALSE;
245 }
246
247 GST_START_TEST (test_push_unlinked)
248 {
249   GstPad *src;
250   GstCaps *caps;
251   GstBuffer *buffer;
252   gulong id;
253
254   src = gst_pad_new ("src", GST_PAD_SRC);
255   fail_if (src == NULL);
256   caps = gst_pad_get_allowed_caps (src);
257   fail_unless (caps == NULL);
258
259   caps = gst_caps_from_string ("foo/bar");
260
261   gst_pad_set_caps (src, caps);
262   ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
263
264   /* pushing on an unlinked pad will drop the buffer */
265   buffer = gst_buffer_new ();
266   gst_buffer_ref (buffer);
267   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
268   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
269   gst_buffer_unref (buffer);
270
271   /* adding a probe that returns FALSE will drop the buffer without trying
272    * to chain */
273   id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
274       GINT_TO_POINTER (0));
275   buffer = gst_buffer_new ();
276   gst_buffer_ref (buffer);
277   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
278   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
279   gst_buffer_unref (buffer);
280   gst_pad_remove_buffer_probe (src, id);
281
282   /* adding a probe that returns TRUE will still chain the buffer,
283    * and hence drop because pad is unlinked */
284   id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
285       GINT_TO_POINTER (1));
286   buffer = gst_buffer_new ();
287   gst_buffer_ref (buffer);
288   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
289   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
290   gst_buffer_unref (buffer);
291   gst_pad_remove_buffer_probe (src, id);
292
293
294   /* cleanup */
295   ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
296   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
297
298   gst_object_unref (src);
299
300   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
301   gst_caps_unref (caps);
302 }
303
304 GST_END_TEST;
305
306 GST_START_TEST (test_push_linked)
307 {
308   GstPad *src, *sink;
309   GstPadLinkReturn plr;
310   GstCaps *caps;
311   GstBuffer *buffer;
312   gulong id;
313
314   /* setup */
315   sink = gst_pad_new ("sink", GST_PAD_SINK);
316   fail_if (sink == NULL);
317   gst_pad_set_chain_function (sink, gst_check_chain_func);
318
319   src = gst_pad_new ("src", GST_PAD_SRC);
320   fail_if (src == NULL);
321
322   caps = gst_caps_from_string ("foo/bar");
323   /* one for me */
324   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
325
326   gst_pad_set_caps (src, caps);
327   gst_pad_set_caps (sink, caps);
328   /* one for me and one for each set_caps */
329   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
330
331   plr = gst_pad_link (src, sink);
332   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
333   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
334
335   buffer = gst_buffer_new ();
336 #if 0
337   /* FIXME, new pad should be flushing */
338   gst_buffer_ref (buffer);
339   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_WRONG_STATE);
340   gst_buffer_ref (buffer);
341   fail_unless (gst_pad_chain (sink, buffer) == GST_FLOW_WRONG_STATE);
342 #endif
343
344   /* activate pads */
345   gst_pad_set_active (src, TRUE);
346   gst_pad_set_active (sink, TRUE);
347
348   /* test */
349   /* pushing on a linked pad will drop the ref to the buffer */
350   gst_buffer_ref (buffer);
351   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
352   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
353   gst_buffer_unref (buffer);
354   fail_unless_equals_int (g_list_length (buffers), 1);
355   buffer = GST_BUFFER (buffers->data);
356   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
357   gst_buffer_unref (buffer);
358   g_list_free (buffers);
359   buffers = NULL;
360
361   /* adding a probe that returns FALSE will drop the buffer without trying
362    * to chain */
363   id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
364       GINT_TO_POINTER (0));
365   buffer = gst_buffer_new ();
366   gst_buffer_ref (buffer);
367   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
368   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
369   gst_buffer_unref (buffer);
370   gst_pad_remove_buffer_probe (src, id);
371   fail_unless_equals_int (g_list_length (buffers), 0);
372
373   /* adding a probe that returns TRUE will still chain the buffer */
374   id = gst_pad_add_buffer_probe (src, (GCallback) _probe_handler,
375       GINT_TO_POINTER (1));
376   buffer = gst_buffer_new ();
377   gst_buffer_ref (buffer);
378   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
379   gst_pad_remove_buffer_probe (src, id);
380
381   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
382   gst_buffer_unref (buffer);
383   fail_unless_equals_int (g_list_length (buffers), 1);
384   buffer = GST_BUFFER (buffers->data);
385   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
386   gst_buffer_unref (buffer);
387   g_list_free (buffers);
388   buffers = NULL;
389
390   /* teardown */
391   gst_pad_unlink (src, sink);
392   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
393   gst_object_unref (src);
394   gst_object_unref (sink);
395   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
396
397   gst_caps_unref (caps);
398 }
399
400 GST_END_TEST;
401
402 GST_START_TEST (test_flowreturn)
403 {
404   GstFlowReturn ret;
405   GQuark quark;
406
407   /* test some of the macros */
408   ret = GST_FLOW_UNEXPECTED;
409   fail_unless (GST_FLOW_IS_FATAL (ret));
410   fail_if (GST_FLOW_IS_SUCCESS (ret));
411   fail_if (strcmp (gst_flow_get_name (ret), "unexpected"));
412   quark = gst_flow_to_quark (ret);
413   fail_if (strcmp (g_quark_to_string (quark), "unexpected"));
414
415   ret = GST_FLOW_RESEND;
416   fail_if (GST_FLOW_IS_FATAL (ret));
417   fail_unless (GST_FLOW_IS_SUCCESS (ret));
418   fail_if (strcmp (gst_flow_get_name (ret), "resend"));
419   quark = gst_flow_to_quark (ret);
420   fail_if (strcmp (g_quark_to_string (quark), "resend"));
421
422   /* custom returns */
423   ret = GST_FLOW_CUSTOM_SUCCESS;
424   fail_if (GST_FLOW_IS_FATAL (ret));
425   fail_unless (GST_FLOW_IS_SUCCESS (ret));
426   fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
427   quark = gst_flow_to_quark (ret);
428   fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
429
430   ret = GST_FLOW_CUSTOM_ERROR;
431   fail_unless (GST_FLOW_IS_FATAL (ret));
432   fail_if (GST_FLOW_IS_SUCCESS (ret));
433   fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
434   quark = gst_flow_to_quark (ret);
435   fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
436
437   /* custom returns clamping */
438   ret = GST_FLOW_CUSTOM_SUCCESS + 2;
439   fail_if (GST_FLOW_IS_FATAL (ret));
440   fail_unless (GST_FLOW_IS_SUCCESS (ret));
441   fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
442   quark = gst_flow_to_quark (ret);
443   fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
444
445   ret = GST_FLOW_CUSTOM_ERROR - 2;
446   fail_unless (GST_FLOW_IS_FATAL (ret));
447   fail_if (GST_FLOW_IS_SUCCESS (ret));
448   fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
449   quark = gst_flow_to_quark (ret);
450   fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
451
452   /* unknown values */
453   ret = GST_FLOW_CUSTOM_ERROR + 2;
454   fail_unless (GST_FLOW_IS_FATAL (ret));
455   fail_if (GST_FLOW_IS_SUCCESS (ret));
456   fail_if (strcmp (gst_flow_get_name (ret), "unknown"));
457   quark = gst_flow_to_quark (ret);
458   fail_unless (quark == 0);
459 }
460
461 GST_END_TEST;
462
463 GST_START_TEST (test_push_negotiation)
464 {
465   GstPad *src, *sink;
466   GstPadLinkReturn plr;
467   GstCaps *srccaps =
468       gst_caps_from_string ("audio/x-raw-int,width={16,32},depth={16,32}");
469   GstCaps *sinkcaps =
470       gst_caps_from_string ("audio/x-raw-int,width=32,depth={16,32}");
471   GstPadTemplate *src_template;
472   GstPadTemplate *sink_template;
473   GstCaps *caps;
474   GstBuffer *buffer;
475
476   /* setup */
477   src_template = gst_pad_template_new ("src", GST_PAD_SRC,
478       GST_PAD_ALWAYS, srccaps);
479   sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
480       GST_PAD_ALWAYS, sinkcaps);
481
482   sink = gst_pad_new_from_template (sink_template, "sink");
483   fail_if (sink == NULL);
484   gst_pad_set_chain_function (sink, gst_check_chain_func);
485
486   src = gst_pad_new_from_template (src_template, "src");
487   fail_if (src == NULL);
488
489   plr = gst_pad_link (src, sink);
490   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
491
492   buffer = gst_buffer_new ();
493
494   /* activate pads */
495   gst_pad_set_active (src, TRUE);
496   gst_pad_set_active (sink, TRUE);
497
498   caps = gst_caps_from_string ("audio/x-raw-int,width=16,depth=16");
499
500   /* Should fail if src pad caps are incompatible with sink pad caps */
501   gst_pad_set_caps (src, caps);
502   gst_buffer_ref (buffer);
503   gst_buffer_set_caps (buffer, caps);
504   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_NEGOTIATED);
505   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
506   gst_buffer_unref (buffer);
507
508   /* teardown */
509   gst_pad_unlink (src, sink);
510   gst_object_unref (src);
511   gst_object_unref (sink);
512   gst_caps_unref (caps);
513   gst_object_unref (sink_template);
514   gst_object_unref (src_template);
515 }
516
517 GST_END_TEST;
518
519 /* see that an unref also unlinks the pads */
520 GST_START_TEST (test_src_unref_unlink)
521 {
522   GstPad *src, *sink;
523   GstCaps *caps;
524   GstPadLinkReturn plr;
525
526   sink = gst_pad_new ("sink", GST_PAD_SINK);
527   fail_if (sink == NULL);
528
529   src = gst_pad_new ("src", GST_PAD_SRC);
530   fail_if (src == NULL);
531
532   caps = gst_caps_from_string ("foo/bar");
533
534   gst_pad_set_caps (src, caps);
535   gst_pad_set_caps (sink, caps);
536
537   plr = gst_pad_link (src, sink);
538   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
539
540   /* unref the srcpad */
541   gst_object_unref (src);
542
543   /* sink should be unlinked now */
544   fail_if (gst_pad_is_linked (sink));
545
546   /* cleanup */
547   gst_object_unref (sink);
548   gst_caps_unref (caps);
549 }
550
551 GST_END_TEST;
552
553 /* see that an unref also unlinks the pads */
554 GST_START_TEST (test_sink_unref_unlink)
555 {
556   GstPad *src, *sink;
557   GstCaps *caps;
558   GstPadLinkReturn plr;
559
560   sink = gst_pad_new ("sink", GST_PAD_SINK);
561   fail_if (sink == NULL);
562
563   src = gst_pad_new ("src", GST_PAD_SRC);
564   fail_if (src == NULL);
565
566   caps = gst_caps_from_string ("foo/bar");
567
568   gst_pad_set_caps (src, caps);
569   gst_pad_set_caps (sink, caps);
570
571   plr = gst_pad_link (src, sink);
572   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
573
574   /* unref the sinkpad */
575   gst_object_unref (sink);
576
577   /* src should be unlinked now */
578   fail_if (gst_pad_is_linked (src));
579
580   /* cleanup */
581   gst_object_unref (src);
582   gst_caps_unref (caps);
583 }
584
585 GST_END_TEST;
586
587 /* gst_pad_get_caps should return a copy of the caps */
588 GST_START_TEST (test_get_caps_must_be_copy)
589 {
590   GstPad *pad;
591   GstCaps *caps;
592   GstPadTemplate *templ;
593
594   caps = gst_caps_new_any ();
595   templ =
596       gst_pad_template_new ("test_templ", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
597
598   pad = gst_pad_new_from_template (templ, NULL);
599   fail_unless (GST_PAD_CAPS (pad) == NULL, "caps present on pad");
600   /* This is a writable copy ! */
601   caps = gst_pad_get_caps (pad);
602
603   /* we must own the caps */
604   ASSERT_OBJECT_REFCOUNT (caps, "caps", 1);
605
606   /* cleanup */
607   gst_object_unref (templ);
608   gst_caps_unref (caps);
609   gst_object_unref (pad);
610 }
611
612 GST_END_TEST;
613
614 static void
615 unblock_async_cb (GstPad * pad, gboolean blocked, gpointer user_data)
616 {
617   gboolean *bool_user_data = (gboolean *) user_data;
618
619   /* here we should have blocked == 1 unblocked == 0 */
620   fail_unless (bool_user_data[0] == TRUE);
621   fail_unless (bool_user_data[1] == FALSE);
622
623   bool_user_data[1] = TRUE;
624 }
625
626 static void
627 block_async_cb (GstPad * pad, gboolean blocked, gpointer user_data)
628 {
629   gboolean *bool_user_data = (gboolean *) user_data;
630
631   /* here we should have blocked == 0 unblocked == 0 */
632   fail_unless (bool_user_data[0] == FALSE);
633   fail_unless (bool_user_data[1] == FALSE);
634
635   bool_user_data[0] = blocked;
636
637   gst_pad_set_blocked_async (pad, FALSE, unblock_async_cb, user_data);
638 }
639
640 GST_START_TEST (test_block_async)
641 {
642   GstPad *pad;
643   /* we set data[0] = TRUE when the pad is blocked, data[1] = TRUE when it's
644    * unblocked */
645   gboolean data[2] = { FALSE, FALSE };
646
647   pad = gst_pad_new ("src", GST_PAD_SRC);
648   fail_unless (pad != NULL);
649
650   gst_pad_set_active (pad, TRUE);
651   gst_pad_set_blocked_async (pad, TRUE, block_async_cb, &data);
652
653   fail_unless (data[0] == FALSE);
654   fail_unless (data[1] == FALSE);
655   gst_pad_push (pad, gst_buffer_new ());
656
657   gst_object_unref (pad);
658 }
659
660 GST_END_TEST;
661
662 #if 0
663 static void
664 block_async_second (GstPad * pad, gboolean blocked, gpointer user_data)
665 {
666   gst_pad_set_blocked_async (pad, FALSE, unblock_async_cb, NULL);
667 }
668
669 static void
670 block_async_first (GstPad * pad, gboolean blocked, gpointer user_data)
671 {
672   static int n_calls = 0;
673   gboolean *bool_user_data = (gboolean *) user_data;
674
675   if (++n_calls > 1)
676     /* we expect this callback to be called only once */
677     g_warn_if_reached ();
678
679   *bool_user_data = blocked;
680
681   /* replace block_async_first with block_async_second so next time the pad is
682    * blocked the latter should be called */
683   gst_pad_set_blocked_async (pad, TRUE, block_async_second, NULL);
684
685   /* unblock temporarily, in the next push block_async_second should be called
686    */
687   gst_pad_push_event (pad, gst_event_new_flush_start ());
688 }
689
690 GST_START_TEST (test_block_async_replace_callback)
691 {
692   GstPad *pad;
693   gboolean blocked;
694
695   pad = gst_pad_new ("src", GST_PAD_SRC);
696   fail_unless (pad != NULL);
697   gst_pad_set_active (pad, TRUE);
698
699   gst_pad_set_blocked_async (pad, TRUE, block_async_first, &blocked);
700   blocked = FALSE;
701
702   gst_pad_push (pad, gst_buffer_new ());
703   fail_unless (blocked == TRUE);
704   /* block_async_first flushes to unblock */
705   gst_pad_push_event (pad, gst_event_new_flush_stop ());
706
707   /* push again, this time block_async_second should be called */
708   gst_pad_push (pad, gst_buffer_new ());
709   fail_unless (blocked == TRUE);
710
711   gst_object_unref (pad);
712 }
713
714 GST_END_TEST;
715 #endif
716
717 static void
718 block_async_full_destroy (gpointer user_data)
719 {
720   gint *state = (gint *) user_data;
721
722   fail_unless (*state < 2);
723
724   *state = 2;
725 }
726
727 static void
728 block_async_full_cb (GstPad * pad, gboolean blocked, gpointer user_data)
729 {
730   *(gint *) user_data = (gint) blocked;
731
732   gst_pad_push_event (pad, gst_event_new_flush_start ());
733 }
734
735 GST_START_TEST (test_block_async_full_destroy)
736 {
737   GstPad *pad;
738   /* 0 = unblocked, 1 = blocked, 2 = destroyed */
739   gint state = 0;
740
741   pad = gst_pad_new ("src", GST_PAD_SRC);
742   fail_unless (pad != NULL);
743   gst_pad_set_active (pad, TRUE);
744
745   gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
746       &state, block_async_full_destroy);
747   fail_unless (state == 0);
748
749   gst_pad_push (pad, gst_buffer_new ());
750   /* block_async_full_cb sets state to 1 and then flushes to unblock temporarily
751    */
752   fail_unless (state == 1);
753   gst_pad_push_event (pad, gst_event_new_flush_stop ());
754
755   /* call with the same user_data, should not call the destroy_notify function
756    */
757   gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
758       &state, block_async_full_destroy);
759   fail_unless (state == 1);
760
761   /* now change user_data (to NULL in this case) so destroy_notify should be
762    * called */
763   gst_pad_set_blocked_async_full (pad, FALSE, block_async_full_cb,
764       NULL, block_async_full_destroy);
765   fail_unless (state == 2);
766
767   gst_object_unref (pad);
768 }
769
770 GST_END_TEST;
771
772 GST_START_TEST (test_block_async_full_destroy_dispose)
773 {
774   GstPad *pad;
775   /* 0 = unblocked, 1 = blocked, 2 = destroyed */
776   gint state = 0;
777
778   pad = gst_pad_new ("src", GST_PAD_SRC);
779   fail_unless (pad != NULL);
780   gst_pad_set_active (pad, TRUE);
781
782   gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
783       &state, block_async_full_destroy);
784
785   gst_pad_push (pad, gst_buffer_new ());
786   /* block_async_full_cb sets state to 1 and then flushes to unblock temporarily
787    */
788   fail_unless_equals_int (state, 1);
789   gst_pad_push_event (pad, gst_event_new_flush_stop ());
790
791   /* gst_pad_dispose calls the destroy_notify function if necessary */
792   gst_object_unref (pad);
793
794   fail_unless_equals_int (state, 2);
795 }
796
797 GST_END_TEST;
798
799
800 static void
801 unblock_async_no_flush_cb (GstPad * pad, gboolean blocked, gpointer user_data)
802 {
803   gboolean *bool_user_data = (gboolean *) user_data;
804
805   /* here we should have blocked == 1 unblocked == 0 */
806
807   fail_unless (blocked == FALSE);
808
809   fail_unless (bool_user_data[0] == TRUE);
810   fail_unless (bool_user_data[1] == TRUE);
811   fail_unless (bool_user_data[2] == FALSE);
812
813   bool_user_data[2] = TRUE;
814 }
815
816
817 static void
818 unblock_async_not_called (GstPad * pad, gboolean blocked, gpointer user_data)
819 {
820   g_warn_if_reached ();
821 }
822
823 static void
824 block_async_second_no_flush (GstPad * pad, gboolean blocked, gpointer user_data)
825 {
826   gboolean *bool_user_data = (gboolean *) user_data;
827
828   fail_unless (blocked == TRUE);
829
830   fail_unless (bool_user_data[0] == TRUE);
831   fail_unless (bool_user_data[1] == FALSE);
832   fail_unless (bool_user_data[2] == FALSE);
833
834   bool_user_data[1] = TRUE;
835
836   fail_unless (gst_pad_set_blocked_async (pad, FALSE, unblock_async_no_flush_cb,
837           user_data));
838 }
839
840 static void
841 block_async_first_no_flush (GstPad * pad, gboolean blocked, gpointer user_data)
842 {
843   static int n_calls = 0;
844   gboolean *bool_user_data = (gboolean *) user_data;
845
846   fail_unless (blocked == TRUE);
847
848   if (++n_calls > 1)
849     /* we expect this callback to be called only once */
850     g_warn_if_reached ();
851
852   *bool_user_data = blocked;
853
854   fail_unless (bool_user_data[0] == TRUE);
855   fail_unless (bool_user_data[1] == FALSE);
856   fail_unless (bool_user_data[2] == FALSE);
857
858   fail_unless (gst_pad_set_blocked_async (pad, FALSE, unblock_async_not_called,
859           NULL));
860
861   /* replace block_async_first with block_async_second so next time the pad is
862    * blocked the latter should be called */
863   fail_unless (gst_pad_set_blocked_async (pad, TRUE,
864           block_async_second_no_flush, user_data));
865 }
866
867 GST_START_TEST (test_block_async_replace_callback_no_flush)
868 {
869   GstPad *pad;
870   gboolean bool_user_data[3] = { FALSE, FALSE, FALSE };
871
872   pad = gst_pad_new ("src", GST_PAD_SRC);
873   fail_unless (pad != NULL);
874   gst_pad_set_active (pad, TRUE);
875
876   fail_unless (gst_pad_set_blocked_async (pad, TRUE, block_async_first_no_flush,
877           bool_user_data));
878
879   gst_pad_push (pad, gst_buffer_new ());
880   fail_unless (bool_user_data[0] == TRUE);
881   fail_unless (bool_user_data[1] == TRUE);
882   fail_unless (bool_user_data[2] == TRUE);
883
884   gst_object_unref (pad);
885 }
886
887 GST_END_TEST;
888
889
890 static Suite *
891 gst_pad_suite (void)
892 {
893   Suite *s = suite_create ("GstPad");
894   TCase *tc_chain = tcase_create ("general");
895
896   /* turn off timeout */
897   tcase_set_timeout (tc_chain, 60);
898
899   suite_add_tcase (s, tc_chain);
900   tcase_add_test (tc_chain, test_link);
901   tcase_add_test (tc_chain, test_refcount);
902   tcase_add_test (tc_chain, test_get_allowed_caps);
903   tcase_add_test (tc_chain, test_link_unlink_threaded);
904   tcase_add_test (tc_chain, test_name_is_valid);
905   tcase_add_test (tc_chain, test_push_unlinked);
906   tcase_add_test (tc_chain, test_push_linked);
907   tcase_add_test (tc_chain, test_flowreturn);
908   tcase_add_test (tc_chain, test_push_negotiation);
909   tcase_add_test (tc_chain, test_src_unref_unlink);
910   tcase_add_test (tc_chain, test_sink_unref_unlink);
911   tcase_add_test (tc_chain, test_get_caps_must_be_copy);
912   tcase_add_test (tc_chain, test_block_async);
913 #if 0
914   tcase_add_test (tc_chain, test_block_async_replace_callback);
915 #endif
916   tcase_add_test (tc_chain, test_block_async_full_destroy);
917   tcase_add_test (tc_chain, test_block_async_full_destroy_dispose);
918   tcase_add_test (tc_chain, test_block_async_replace_callback_no_flush);
919
920   return s;
921 }
922
923 GST_CHECK_MAIN (gst_pad);