tools, tests: fix some unused-but-set-variable compiler warnings
[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_active (sink, TRUE);
93   gst_pad_set_caps (sink, caps);
94   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
95
96   MAIN_START_THREADS (5, thread_link_unlink, NULL);
97   for (i = 0; i < 1000; ++i) {
98     gst_pad_is_linked (src);
99     gst_pad_is_linked (sink);
100     THREAD_SWITCH ();
101   }
102   MAIN_STOP_THREADS ();
103
104   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
105   gst_caps_unref (caps);
106
107   ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
108   gst_object_unref (src);
109   gst_object_unref (sink);
110 }
111
112 GST_END_TEST;
113
114 GST_START_TEST (test_refcount)
115 {
116   GstPad *src, *sink;
117   GstCaps *caps;
118   GstPadLinkReturn plr;
119
120   sink = gst_pad_new ("sink", GST_PAD_SINK);
121   fail_if (sink == NULL);
122
123   src = gst_pad_new ("src", GST_PAD_SRC);
124   fail_if (src == NULL);
125
126   caps = gst_caps_from_string ("foo/bar");
127   /* one for me */
128   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
129
130   fail_unless (gst_pad_set_caps (src, caps) == TRUE);
131   /* can't set caps on flushing sinkpad */
132   fail_if (gst_pad_set_caps (sink, caps) == TRUE);
133   /* one for me and one for each set_caps */
134   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
135
136   gst_pad_set_active (sink, TRUE);
137   fail_unless (gst_pad_set_caps (sink, caps) == TRUE);
138   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
139
140   plr = gst_pad_link (src, sink);
141   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
142   /* src caps added to pending caps on sink */
143   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
144
145   gst_pad_unlink (src, sink);
146   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
147
148   /* cleanup */
149   gst_object_unref (src);
150   gst_object_unref (sink);
151   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
152
153   gst_caps_unref (caps);
154 }
155
156 GST_END_TEST;
157
158 GST_START_TEST (test_get_allowed_caps)
159 {
160   GstPad *src, *sink;
161   GstCaps *caps, *gotcaps;
162   GstBuffer *buffer;
163   GstPadLinkReturn plr;
164
165   ASSERT_CRITICAL (gst_pad_get_allowed_caps (NULL));
166
167   buffer = gst_buffer_new ();
168   ASSERT_CRITICAL (gst_pad_get_allowed_caps ((GstPad *) buffer));
169   gst_buffer_unref (buffer);
170
171   src = gst_pad_new ("src", GST_PAD_SRC);
172   fail_if (src == NULL);
173   caps = gst_pad_get_allowed_caps (src);
174   fail_unless (caps == NULL);
175
176   caps = gst_caps_from_string ("foo/bar");
177
178   sink = gst_pad_new ("sink", GST_PAD_SINK);
179   fail_unless (gst_pad_set_caps (src, caps) == TRUE);
180   fail_if (gst_pad_set_caps (sink, caps) == TRUE);
181   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
182
183   gst_pad_set_active (sink, TRUE);
184   fail_unless (gst_pad_set_caps (sink, caps) == TRUE);
185   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
186
187   plr = gst_pad_link (src, sink);
188   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
189
190   gotcaps = gst_pad_get_allowed_caps (src);
191   fail_if (gotcaps == NULL);
192 #if 0
193   /* FIXME, does not work, caps events are different so the sinkpad loses caps
194    * when linking */
195   fail_unless (gst_caps_is_equal (gotcaps, caps));
196 #endif
197
198   ASSERT_CAPS_REFCOUNT (gotcaps, "gotcaps", 1);
199   gst_caps_unref (gotcaps);
200
201   gst_pad_unlink (src, sink);
202
203   /* cleanup */
204   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
205   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
206   ASSERT_OBJECT_REFCOUNT (sink, "sink", 1);
207
208   gst_object_unref (src);
209   gst_object_unref (sink);
210
211   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
212   gst_caps_unref (caps);
213 }
214
215 GST_END_TEST;
216
217 static gboolean
218 name_is_valid (const gchar * name, GstPadPresence presence)
219 {
220   GstPadTemplate *new;
221   GstCaps *any = GST_CAPS_ANY;
222
223   new = gst_pad_template_new (name, GST_PAD_SRC, presence, any);
224   if (new) {
225     gst_object_unref (GST_OBJECT (new));
226     return TRUE;
227   }
228   return FALSE;
229 }
230
231 GST_START_TEST (test_name_is_valid)
232 {
233   gboolean result = FALSE;
234
235   fail_unless (name_is_valid ("src", GST_PAD_ALWAYS));
236   ASSERT_WARNING (name_is_valid ("src%", GST_PAD_ALWAYS));
237   ASSERT_WARNING (result = name_is_valid ("src%d", GST_PAD_ALWAYS));
238   fail_if (result);
239
240   fail_unless (name_is_valid ("src", GST_PAD_REQUEST));
241   ASSERT_WARNING (name_is_valid ("src%s%s", GST_PAD_REQUEST));
242   ASSERT_WARNING (name_is_valid ("src%c", GST_PAD_REQUEST));
243   ASSERT_WARNING (name_is_valid ("src%", GST_PAD_REQUEST));
244   ASSERT_WARNING (name_is_valid ("src%dsrc", GST_PAD_REQUEST));
245
246   fail_unless (name_is_valid ("src", GST_PAD_SOMETIMES));
247   fail_unless (name_is_valid ("src%c", GST_PAD_SOMETIMES));
248 }
249
250 GST_END_TEST;
251
252 static GstProbeReturn
253 _probe_handler (GstPad * pad, GstProbeType type, GstBuffer * buffer,
254     gpointer userdata)
255 {
256   gint ret = GPOINTER_TO_INT (userdata);
257
258   if (ret == 1)
259     return GST_PROBE_OK;
260
261   return GST_PROBE_DROP;
262 }
263
264 GST_START_TEST (test_push_unlinked)
265 {
266   GstPad *src;
267   GstCaps *caps;
268   GstBuffer *buffer;
269   gulong id;
270
271   src = gst_pad_new ("src", GST_PAD_SRC);
272   fail_if (src == NULL);
273   caps = gst_pad_get_allowed_caps (src);
274   fail_unless (caps == NULL);
275
276   caps = gst_caps_from_string ("foo/bar");
277
278   gst_pad_set_caps (src, caps);
279   ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
280
281   /* pushing on an inactive pad will return wrong state */
282   buffer = gst_buffer_new ();
283   gst_buffer_ref (buffer);
284   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_WRONG_STATE);
285   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
286   gst_buffer_unref (buffer);
287
288   gst_pad_set_active (src, TRUE);
289
290   /* pushing on an unlinked pad will drop the buffer */
291   buffer = gst_buffer_new ();
292   gst_buffer_ref (buffer);
293   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
294   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
295   gst_buffer_unref (buffer);
296
297   /* adding a probe that returns FALSE will drop the buffer without trying
298    * to chain */
299   id = gst_pad_add_probe (src, GST_PROBE_TYPE_BUFFER,
300       (GstPadProbeCallback) _probe_handler, GINT_TO_POINTER (0), NULL);
301   buffer = gst_buffer_new ();
302   gst_buffer_ref (buffer);
303   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
304   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
305   gst_buffer_unref (buffer);
306   gst_pad_remove_probe (src, id);
307
308   /* adding a probe that returns TRUE will still chain the buffer,
309    * and hence drop because pad is unlinked */
310   id = gst_pad_add_probe (src, GST_PROBE_TYPE_BUFFER,
311       (GstPadProbeCallback) _probe_handler, GINT_TO_POINTER (1), NULL);
312   buffer = gst_buffer_new ();
313   gst_buffer_ref (buffer);
314   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_LINKED);
315   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
316   gst_buffer_unref (buffer);
317   gst_pad_remove_probe (src, id);
318
319
320   /* cleanup */
321   ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
322   ASSERT_OBJECT_REFCOUNT (src, "src", 1);
323
324   gst_object_unref (src);
325
326   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
327   gst_caps_unref (caps);
328 }
329
330 GST_END_TEST;
331
332 GST_START_TEST (test_push_linked)
333 {
334   GstPad *src, *sink;
335   GstPadLinkReturn plr;
336   GstCaps *caps;
337   GstBuffer *buffer;
338   gulong id;
339
340   /* setup */
341   sink = gst_pad_new ("sink", GST_PAD_SINK);
342   fail_if (sink == NULL);
343   gst_pad_set_chain_function (sink, gst_check_chain_func);
344
345   src = gst_pad_new ("src", GST_PAD_SRC);
346   fail_if (src == NULL);
347
348   caps = gst_caps_from_string ("foo/bar");
349   /* one for me */
350   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
351
352   gst_pad_set_caps (src, caps);
353   gst_pad_set_active (sink, TRUE);
354   gst_pad_set_caps (sink, caps);
355   /* one for me and one for each set_caps */
356   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
357
358   plr = gst_pad_link (src, sink);
359   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
360   ASSERT_CAPS_REFCOUNT (caps, "caps", 3);
361
362   buffer = gst_buffer_new ();
363 #if 0
364   /* FIXME, new pad should be flushing */
365   gst_buffer_ref (buffer);
366   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_WRONG_STATE);
367   gst_buffer_ref (buffer);
368   fail_unless (gst_pad_chain (sink, buffer) == GST_FLOW_WRONG_STATE);
369 #endif
370
371   /* activate pads */
372   gst_pad_set_active (src, TRUE);
373   gst_pad_set_active (sink, TRUE);
374
375   /* test */
376   /* pushing on a linked pad will drop the ref to the buffer */
377   gst_buffer_ref (buffer);
378   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
379   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
380   gst_buffer_unref (buffer);
381   fail_unless_equals_int (g_list_length (buffers), 1);
382   buffer = GST_BUFFER (buffers->data);
383   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
384   gst_buffer_unref (buffer);
385   g_list_free (buffers);
386   buffers = NULL;
387
388   /* adding a probe that returns FALSE will drop the buffer without trying
389    * to chain */
390   id = gst_pad_add_probe (src, GST_PROBE_TYPE_BUFFER,
391       (GstPadProbeCallback) _probe_handler, GINT_TO_POINTER (0), NULL);
392   buffer = gst_buffer_new ();
393   gst_buffer_ref (buffer);
394   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
395   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
396   gst_buffer_unref (buffer);
397   gst_pad_remove_probe (src, id);
398   fail_unless_equals_int (g_list_length (buffers), 0);
399
400   /* adding a probe that returns TRUE will still chain the buffer */
401   id = gst_pad_add_probe (src, GST_PROBE_TYPE_BUFFER,
402       (GstPadProbeCallback) _probe_handler, GINT_TO_POINTER (1), NULL);
403   buffer = gst_buffer_new ();
404   gst_buffer_ref (buffer);
405   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_OK);
406   gst_pad_remove_probe (src, id);
407
408   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 2);
409   gst_buffer_unref (buffer);
410   fail_unless_equals_int (g_list_length (buffers), 1);
411   buffer = GST_BUFFER (buffers->data);
412   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
413   gst_buffer_unref (buffer);
414   g_list_free (buffers);
415   buffers = NULL;
416
417   /* teardown */
418   gst_pad_unlink (src, sink);
419   ASSERT_CAPS_REFCOUNT (caps, "caps", 2);
420   gst_object_unref (src);
421   gst_object_unref (sink);
422   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
423
424   gst_caps_unref (caps);
425 }
426
427 GST_END_TEST;
428
429 static GstBuffer *
430 buffer_from_string (const gchar * str)
431 {
432   guint size;
433   GstBuffer *buf;
434   gpointer data;
435
436   size = strlen (str);
437   buf = gst_buffer_new_and_alloc (size);
438
439   data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
440   memcpy (data, str, size);
441   gst_buffer_unmap (buf, data, size);
442
443   return buf;
444 }
445
446 static gboolean
447 buffer_compare (GstBuffer * buf, const gchar * str, gsize size)
448 {
449   gboolean res;
450   gpointer data;
451
452   data = gst_buffer_map (buf, NULL, NULL, GST_MAP_READ);
453   res = memcmp (data, str, size) == 0;
454   GST_DEBUG ("%s <-> %s: %d", (gchar *) data, str, res);
455   gst_buffer_unmap (buf, data, size);
456
457   return res;
458 }
459
460 GST_START_TEST (test_push_buffer_list_compat)
461 {
462   GstPad *src, *sink;
463   GstPadLinkReturn plr;
464   GstCaps *caps;
465   GstBufferList *list;
466   GstBuffer *buffer;
467
468   /* setup */
469   sink = gst_pad_new ("sink", GST_PAD_SINK);
470   fail_if (sink == NULL);
471   gst_pad_set_chain_function (sink, gst_check_chain_func);
472   /* leave chainlistfunc unset */
473
474   src = gst_pad_new ("src", GST_PAD_SRC);
475   fail_if (src == NULL);
476
477   caps = gst_caps_from_string ("foo/bar");
478
479   gst_pad_set_caps (src, caps);
480   gst_pad_set_active (sink, TRUE);
481   gst_pad_set_caps (sink, caps);
482
483   plr = gst_pad_link (src, sink);
484   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
485
486   list = gst_buffer_list_new ();
487
488   /* activate pads */
489   gst_pad_set_active (src, TRUE);
490   gst_pad_set_active (sink, TRUE);
491
492   /* test */
493   /* adding to a buffer list will drop the ref to the buffer */
494   gst_buffer_list_add (list, buffer_from_string ("ListGroup"));
495   gst_buffer_list_add (list, buffer_from_string ("AnotherListGroup"));
496
497   fail_unless (gst_pad_push_list (src, list) == GST_FLOW_OK);
498   fail_unless_equals_int (g_list_length (buffers), 2);
499   buffer = GST_BUFFER (buffers->data);
500   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
501   fail_unless (buffer_compare (buffer, "ListGroup", 9));
502   gst_buffer_unref (buffer);
503   buffers = g_list_delete_link (buffers, buffers);
504   buffer = GST_BUFFER (buffers->data);
505   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
506   fail_unless (buffer_compare (buffer, "AnotherListGroup", 16));
507   gst_buffer_unref (buffer);
508   buffers = g_list_delete_link (buffers, buffers);
509   fail_unless (buffers == NULL);
510
511   /* teardown */
512   gst_pad_unlink (src, sink);
513   gst_object_unref (src);
514   gst_object_unref (sink);
515   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
516   gst_caps_unref (caps);
517 }
518
519 GST_END_TEST;
520
521 GST_START_TEST (test_flowreturn)
522 {
523   GstFlowReturn ret;
524   GQuark quark;
525
526   /* test some of the macros */
527   ret = GST_FLOW_UNEXPECTED;
528   fail_if (strcmp (gst_flow_get_name (ret), "unexpected"));
529   quark = gst_flow_to_quark (ret);
530   fail_if (strcmp (g_quark_to_string (quark), "unexpected"));
531
532   ret = GST_FLOW_RESEND;
533   fail_if (strcmp (gst_flow_get_name (ret), "resend"));
534   quark = gst_flow_to_quark (ret);
535   fail_if (strcmp (g_quark_to_string (quark), "resend"));
536
537   /* custom returns */
538   ret = GST_FLOW_CUSTOM_SUCCESS;
539   fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
540   quark = gst_flow_to_quark (ret);
541   fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
542
543   ret = GST_FLOW_CUSTOM_ERROR;
544   fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
545   quark = gst_flow_to_quark (ret);
546   fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
547
548   /* custom returns clamping */
549   ret = GST_FLOW_CUSTOM_SUCCESS + 2;
550   fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
551   quark = gst_flow_to_quark (ret);
552   fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
553
554   ret = GST_FLOW_CUSTOM_ERROR - 2;
555   fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
556   quark = gst_flow_to_quark (ret);
557   fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
558
559   /* unknown values */
560   ret = GST_FLOW_CUSTOM_ERROR + 2;
561   fail_if (strcmp (gst_flow_get_name (ret), "unknown"));
562   quark = gst_flow_to_quark (ret);
563   fail_unless (quark == 0);
564 }
565
566 GST_END_TEST;
567
568 GST_START_TEST (test_push_negotiation)
569 {
570   GstPad *src, *sink;
571   GstPadLinkReturn plr;
572   GstCaps *srccaps =
573       gst_caps_from_string ("audio/x-raw-int,width={16,32},depth={16,32}");
574   GstCaps *sinkcaps =
575       gst_caps_from_string ("audio/x-raw-int,width=32,depth={16,32}");
576   GstPadTemplate *src_template;
577   GstPadTemplate *sink_template;
578   GstCaps *caps;
579
580   /* setup */
581   src_template = gst_pad_template_new ("src", GST_PAD_SRC,
582       GST_PAD_ALWAYS, srccaps);
583   sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
584       GST_PAD_ALWAYS, sinkcaps);
585   gst_caps_unref (srccaps);
586   gst_caps_unref (sinkcaps);
587
588   sink = gst_pad_new_from_template (sink_template, "sink");
589   fail_if (sink == NULL);
590   gst_pad_set_chain_function (sink, gst_check_chain_func);
591
592   src = gst_pad_new_from_template (src_template, "src");
593   fail_if (src == NULL);
594
595   plr = gst_pad_link (src, sink);
596   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
597
598   /* activate pads */
599   gst_pad_set_active (src, TRUE);
600   gst_pad_set_active (sink, TRUE);
601
602   caps = gst_caps_from_string ("audio/x-raw-int,width=16,depth=16");
603
604   /* Should fail if src pad caps are incompatible with sink pad caps */
605   gst_pad_set_caps (src, caps);
606   fail_unless (gst_pad_set_caps (sink, caps) == FALSE);
607
608   /* teardown */
609   gst_pad_unlink (src, sink);
610   gst_object_unref (src);
611   gst_object_unref (sink);
612   gst_caps_unref (caps);
613   gst_object_unref (sink_template);
614   gst_object_unref (src_template);
615 }
616
617 GST_END_TEST;
618
619 /* see that an unref also unlinks the pads */
620 GST_START_TEST (test_src_unref_unlink)
621 {
622   GstPad *src, *sink;
623   GstCaps *caps;
624   GstPadLinkReturn plr;
625
626   sink = gst_pad_new ("sink", GST_PAD_SINK);
627   fail_if (sink == NULL);
628
629   src = gst_pad_new ("src", GST_PAD_SRC);
630   fail_if (src == NULL);
631
632   caps = gst_caps_from_string ("foo/bar");
633
634   gst_pad_set_caps (src, caps);
635   gst_pad_set_active (sink, TRUE);
636   gst_pad_set_caps (sink, caps);
637
638   plr = gst_pad_link (src, sink);
639   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
640
641   /* unref the srcpad */
642   gst_object_unref (src);
643
644   /* sink should be unlinked now */
645   fail_if (gst_pad_is_linked (sink));
646
647   /* cleanup */
648   gst_object_unref (sink);
649   gst_caps_unref (caps);
650 }
651
652 GST_END_TEST;
653
654 /* see that an unref also unlinks the pads */
655 GST_START_TEST (test_sink_unref_unlink)
656 {
657   GstPad *src, *sink;
658   GstCaps *caps;
659   GstPadLinkReturn plr;
660
661   sink = gst_pad_new ("sink", GST_PAD_SINK);
662   fail_if (sink == NULL);
663
664   src = gst_pad_new ("src", GST_PAD_SRC);
665   fail_if (src == NULL);
666
667   caps = gst_caps_from_string ("foo/bar");
668
669   gst_pad_set_caps (src, caps);
670   gst_pad_set_active (sink, TRUE);
671   gst_pad_set_caps (sink, caps);
672
673   plr = gst_pad_link (src, sink);
674   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
675
676   /* unref the sinkpad */
677   gst_object_unref (sink);
678
679   /* src should be unlinked now */
680   fail_if (gst_pad_is_linked (src));
681
682   /* cleanup */
683   gst_object_unref (src);
684   gst_caps_unref (caps);
685 }
686
687 GST_END_TEST;
688
689 static gulong id;
690
691 static GstProbeReturn
692 block_async_cb (GstPad * pad, GstProbeType type, gpointer type_data,
693     gpointer user_data)
694 {
695   gboolean *bool_user_data = (gboolean *) user_data;
696
697   fail_unless ((type & GST_PROBE_TYPE_BLOCK) != 0);
698
699   /* here we should have blocked == 0 unblocked == 0 */
700   fail_unless (bool_user_data[0] == FALSE);
701   fail_unless (bool_user_data[1] == FALSE);
702
703   bool_user_data[0] = TRUE;
704
705   gst_pad_remove_probe (pad, id);
706   bool_user_data[1] = TRUE;
707
708   return GST_PROBE_OK;
709 }
710
711 GST_START_TEST (test_block_async)
712 {
713   GstPad *pad;
714   /* we set data[0] = TRUE when the pad is blocked, data[1] = TRUE when it's
715    * unblocked */
716   gboolean data[2] = { FALSE, FALSE };
717
718   pad = gst_pad_new ("src", GST_PAD_SRC);
719   fail_unless (pad != NULL);
720
721   gst_pad_set_active (pad, TRUE);
722   id = gst_pad_add_probe (pad, GST_PROBE_TYPE_BLOCK, block_async_cb, &data,
723       NULL);
724
725   fail_unless (data[0] == FALSE);
726   fail_unless (data[1] == FALSE);
727   gst_pad_push (pad, gst_buffer_new ());
728
729   gst_object_unref (pad);
730 }
731
732 GST_END_TEST;
733
734 #if 0
735 static void
736 block_async_second (GstPad * pad, gboolean blocked, gpointer user_data)
737 {
738   gst_pad_set_blocked (pad, FALSE, unblock_async_cb, NULL, NULL);
739 }
740
741 static void
742 block_async_first (GstPad * pad, gboolean blocked, gpointer user_data)
743 {
744   static int n_calls = 0;
745   gboolean *bool_user_data = (gboolean *) user_data;
746
747   if (++n_calls > 1)
748     /* we expect this callback to be called only once */
749     g_warn_if_reached ();
750
751   *bool_user_data = blocked;
752
753   /* replace block_async_first with block_async_second so next time the pad is
754    * blocked the latter should be called */
755   gst_pad_set_blocked (pad, TRUE, block_async_second, NULL, NULL);
756
757   /* unblock temporarily, in the next push block_async_second should be called
758    */
759   gst_pad_push_event (pad, gst_event_new_flush_start ());
760 }
761
762 GST_START_TEST (test_block_async_replace_callback)
763 {
764   GstPad *pad;
765   gboolean blocked;
766
767   pad = gst_pad_new ("src", GST_PAD_SRC);
768   fail_unless (pad != NULL);
769   gst_pad_set_active (pad, TRUE);
770
771   gst_pad_set_blocked (pad, TRUE, block_async_first, &blocked, NULL);
772   blocked = FALSE;
773
774   gst_pad_push (pad, gst_buffer_new ());
775   fail_unless (blocked == TRUE);
776   /* block_async_first flushes to unblock */
777   gst_pad_push_event (pad, gst_event_new_flush_stop ());
778
779   /* push again, this time block_async_second should be called */
780   gst_pad_push (pad, gst_buffer_new ());
781   fail_unless (blocked == TRUE);
782
783   gst_object_unref (pad);
784 }
785
786 GST_END_TEST;
787 #endif
788
789 static void
790 block_async_full_destroy (gpointer user_data)
791 {
792   gint *state = (gint *) user_data;
793
794   fail_unless (*state < 2);
795
796   GST_DEBUG ("setting state to 2");
797   *state = 2;
798 }
799
800 static GstProbeReturn
801 block_async_full_cb (GstPad * pad, GstProbeType type, gpointer type_data,
802     gpointer user_data)
803 {
804   *(gint *) user_data = (gint) TRUE;
805
806   gst_pad_push_event (pad, gst_event_new_flush_start ());
807   GST_DEBUG ("setting state to 1");
808
809   return GST_PROBE_OK;
810 }
811
812 GST_START_TEST (test_block_async_full_destroy)
813 {
814   GstPad *pad;
815   /* 0 = unblocked, 1 = blocked, 2 = destroyed */
816   gint state = 0;
817   gulong id;
818
819   pad = gst_pad_new ("src", GST_PAD_SRC);
820   fail_unless (pad != NULL);
821   gst_pad_set_active (pad, TRUE);
822
823   id = gst_pad_add_probe (pad, GST_PROBE_TYPE_BLOCK, block_async_full_cb,
824       &state, block_async_full_destroy);
825   fail_unless (state == 0);
826
827   gst_pad_push (pad, gst_buffer_new ());
828   /* block_async_full_cb sets state to 1 and then flushes to unblock temporarily
829    */
830   fail_unless (state == 1);
831   gst_pad_push_event (pad, gst_event_new_flush_stop ());
832
833   /* unblock callback is called */
834   gst_pad_remove_probe (pad, id);
835   fail_unless (state == 2);
836
837   gst_object_unref (pad);
838 }
839
840 GST_END_TEST;
841
842 GST_START_TEST (test_block_async_full_destroy_dispose)
843 {
844   GstPad *pad;
845   /* 0 = unblocked, 1 = blocked, 2 = destroyed */
846   gint state = 0;
847
848   pad = gst_pad_new ("src", GST_PAD_SRC);
849   fail_unless (pad != NULL);
850   gst_pad_set_active (pad, TRUE);
851
852   (void) gst_pad_add_probe (pad, GST_PROBE_TYPE_BLOCK, block_async_full_cb,
853       &state, block_async_full_destroy);
854
855   gst_pad_push (pad, gst_buffer_new ());
856   /* block_async_full_cb sets state to 1 and then flushes to unblock temporarily
857    */
858   fail_unless_equals_int (state, 1);
859   gst_pad_push_event (pad, gst_event_new_flush_stop ());
860
861   /* gst_BLOCK calls the destroy_notify function if necessary */
862   gst_object_unref (pad);
863
864   fail_unless_equals_int (state, 2);
865 }
866
867 GST_END_TEST;
868
869
870 #if 0
871 static void
872 unblock_async_no_flush_cb (GstPad * pad, gboolean blocked, gpointer user_data)
873 {
874   gboolean *bool_user_data = (gboolean *) user_data;
875
876   /* here we should have blocked == 1 unblocked == 0 */
877
878   fail_unless (blocked == FALSE);
879
880   fail_unless (bool_user_data[0] == TRUE);
881   fail_unless (bool_user_data[1] == TRUE);
882   fail_unless (bool_user_data[2] == FALSE);
883
884   bool_user_data[2] = TRUE;
885 }
886 #endif
887
888
889 #if 0
890 static void
891 unblock_async_not_called (GstPad * pad, gboolean blocked, gpointer user_data)
892 {
893   g_warn_if_reached ();
894 }
895 #endif
896
897 static GstProbeReturn
898 block_async_second_no_flush (GstPad * pad, GstProbeType type,
899     gpointer type_data, gpointer user_data)
900 {
901   gboolean *bool_user_data = (gboolean *) user_data;
902
903   GST_DEBUG ("second probe called");
904
905   fail_unless (type & GST_PROBE_TYPE_BLOCK);
906
907   fail_unless (bool_user_data[0] == TRUE);
908   fail_unless (bool_user_data[1] == FALSE);
909   fail_unless (bool_user_data[2] == FALSE);
910
911   bool_user_data[1] = TRUE;
912
913   GST_DEBUG ("removing second probe with id %lu", id);
914   gst_pad_remove_probe (pad, id);
915
916   return GST_PROBE_OK;
917 }
918
919 static GstProbeReturn
920 block_async_first_no_flush (GstPad * pad, GstProbeType type, gpointer type_data,
921     gpointer user_data)
922 {
923   static int n_calls = 0;
924   gboolean *bool_user_data = (gboolean *) user_data;
925
926   fail_unless (type & GST_PROBE_TYPE_BLOCK);
927
928   GST_DEBUG ("first probe called");
929
930   if (++n_calls > 1)
931     /* we expect this callback to be called only once */
932     g_warn_if_reached ();
933
934   *bool_user_data = TRUE;
935
936   fail_unless (bool_user_data[0] == TRUE);
937   fail_unless (bool_user_data[1] == FALSE);
938   fail_unless (bool_user_data[2] == FALSE);
939
940   GST_DEBUG ("removing first probe with id %lu", id);
941   gst_pad_remove_probe (pad, id);
942
943   GST_DEBUG ("adding second probe");
944   /* replace block_async_first with block_async_second so next time the pad is
945    * blocked the latter should be called */
946   id = gst_pad_add_probe (pad, GST_PROBE_TYPE_BLOCK,
947       block_async_second_no_flush, user_data, NULL);
948   GST_DEBUG ("added probe with id %lu", id);
949
950   return GST_PROBE_OK;
951 }
952
953 GST_START_TEST (test_block_async_replace_callback_no_flush)
954 {
955   GstPad *pad;
956   gboolean bool_user_data[3] = { FALSE, FALSE, FALSE };
957
958   pad = gst_pad_new ("src", GST_PAD_SRC);
959   fail_unless (pad != NULL);
960   gst_pad_set_active (pad, TRUE);
961
962   GST_DEBUG ("adding probe");
963   id = gst_pad_add_probe (pad, GST_PROBE_TYPE_BLOCK,
964       block_async_first_no_flush, bool_user_data, NULL);
965   GST_DEBUG ("added probe with id %lu", id);
966   fail_if (id == 0);
967
968   GST_DEBUG ("pushing buffer");
969   gst_pad_push (pad, gst_buffer_new ());
970   fail_unless (bool_user_data[0] == TRUE);
971   fail_unless (bool_user_data[1] == TRUE);
972   fail_unless (bool_user_data[2] == FALSE);
973
974   gst_object_unref (pad);
975 }
976
977 GST_END_TEST;
978
979
980 static Suite *
981 gst_pad_suite (void)
982 {
983   Suite *s = suite_create ("GstPad");
984   TCase *tc_chain = tcase_create ("general");
985
986   /* turn off timeout */
987   tcase_set_timeout (tc_chain, 60);
988
989   suite_add_tcase (s, tc_chain);
990   tcase_add_test (tc_chain, test_link);
991   tcase_add_test (tc_chain, test_refcount);
992   tcase_add_test (tc_chain, test_get_allowed_caps);
993   tcase_add_test (tc_chain, test_link_unlink_threaded);
994   tcase_add_test (tc_chain, test_name_is_valid);
995   tcase_add_test (tc_chain, test_push_unlinked);
996   tcase_add_test (tc_chain, test_push_linked);
997   tcase_add_test (tc_chain, test_push_buffer_list_compat);
998   tcase_add_test (tc_chain, test_flowreturn);
999   tcase_add_test (tc_chain, test_push_negotiation);
1000   tcase_add_test (tc_chain, test_src_unref_unlink);
1001   tcase_add_test (tc_chain, test_sink_unref_unlink);
1002   tcase_add_test (tc_chain, test_block_async);
1003 #if 0
1004   tcase_add_test (tc_chain, test_block_async_replace_callback);
1005 #endif
1006   tcase_add_test (tc_chain, test_block_async_full_destroy);
1007   tcase_add_test (tc_chain, test_block_async_full_destroy_dispose);
1008   tcase_add_test (tc_chain, test_block_async_replace_callback_no_flush);
1009
1010   return s;
1011 }
1012
1013 GST_CHECK_MAIN (gst_pad);