Merge branch 'master' into 0.11-fdo
[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 static GstBuffer *
403 buffer_from_string (const gchar * str)
404 {
405   guint size;
406   GstBuffer *buf;
407   gpointer data;
408
409   size = strlen (str);
410   buf = gst_buffer_new_and_alloc (size);
411
412   data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
413   memcpy (data, str, size);
414   gst_buffer_unmap (buf, data, size);
415
416   return buf;
417 }
418
419 static gboolean
420 buffer_compare (GstBuffer * buf, const gchar * str, gsize size)
421 {
422   gboolean res;
423   gpointer data;
424
425   data = gst_buffer_map (buf, NULL, NULL, GST_MAP_READ);
426   res = memcmp (data, str, size) == 0;
427   GST_DEBUG ("%s <-> %s: %d", (gchar *) data, str, res);
428   gst_buffer_unmap (buf, data, size);
429
430   return res;
431 }
432
433 GST_START_TEST (test_push_buffer_list_compat)
434 {
435   GstPad *src, *sink;
436   GstPadLinkReturn plr;
437   GstCaps *caps;
438   GstBufferList *list;
439   GstBufferListIterator *it;
440   GstBuffer *buffer;
441
442   /* setup */
443   sink = gst_pad_new ("sink", GST_PAD_SINK);
444   fail_if (sink == NULL);
445   gst_pad_set_chain_function (sink, gst_check_chain_func);
446   /* leave chainlistfunc unset */
447
448   src = gst_pad_new ("src", GST_PAD_SRC);
449   fail_if (src == NULL);
450
451   caps = gst_caps_from_string ("foo/bar");
452
453   gst_pad_set_caps (src, caps);
454   gst_pad_set_caps (sink, caps);
455
456   plr = gst_pad_link (src, sink);
457   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
458
459   list = gst_buffer_list_new ();
460
461   /* activate pads */
462   gst_pad_set_active (src, TRUE);
463   gst_pad_set_active (sink, TRUE);
464
465   /* test */
466   /* adding to a buffer list will drop the ref to the buffer */
467   it = gst_buffer_list_iterate (list);
468   gst_buffer_list_iterator_add_group (it);
469   gst_buffer_list_iterator_add (it, buffer_from_string ("List"));
470   gst_buffer_list_iterator_add (it, buffer_from_string ("Group"));
471   gst_buffer_list_iterator_add_group (it);
472   gst_buffer_list_iterator_add (it, buffer_from_string ("Another"));
473   gst_buffer_list_iterator_add (it, buffer_from_string ("List"));
474   gst_buffer_list_iterator_add (it, buffer_from_string ("Group"));
475   gst_buffer_list_iterator_free (it);
476   fail_unless (gst_pad_push_list (src, list) == GST_FLOW_OK);
477   fail_unless_equals_int (g_list_length (buffers), 2);
478   buffer = GST_BUFFER (buffers->data);
479   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
480   fail_unless (buffer_compare (buffer, "ListGroup", 9));
481   gst_buffer_unref (buffer);
482   buffers = g_list_delete_link (buffers, buffers);
483   buffer = GST_BUFFER (buffers->data);
484   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
485   fail_unless (buffer_compare (buffer, "AnotherListGroup", 16));
486   gst_buffer_unref (buffer);
487   buffers = g_list_delete_link (buffers, buffers);
488   fail_unless (buffers == NULL);
489
490   /* teardown */
491   gst_pad_unlink (src, sink);
492   gst_object_unref (src);
493   gst_object_unref (sink);
494   ASSERT_CAPS_REFCOUNT (caps, "caps", 1);
495   gst_caps_unref (caps);
496 }
497
498 GST_END_TEST;
499
500 GST_START_TEST (test_flowreturn)
501 {
502   GstFlowReturn ret;
503   GQuark quark;
504
505   /* test some of the macros */
506   ret = GST_FLOW_UNEXPECTED;
507   fail_if (strcmp (gst_flow_get_name (ret), "unexpected"));
508   quark = gst_flow_to_quark (ret);
509   fail_if (strcmp (g_quark_to_string (quark), "unexpected"));
510
511   ret = GST_FLOW_RESEND;
512   fail_if (strcmp (gst_flow_get_name (ret), "resend"));
513   quark = gst_flow_to_quark (ret);
514   fail_if (strcmp (g_quark_to_string (quark), "resend"));
515
516   /* custom returns */
517   ret = GST_FLOW_CUSTOM_SUCCESS;
518   fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
519   quark = gst_flow_to_quark (ret);
520   fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
521
522   ret = GST_FLOW_CUSTOM_ERROR;
523   fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
524   quark = gst_flow_to_quark (ret);
525   fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
526
527   /* custom returns clamping */
528   ret = GST_FLOW_CUSTOM_SUCCESS + 2;
529   fail_if (strcmp (gst_flow_get_name (ret), "custom-success"));
530   quark = gst_flow_to_quark (ret);
531   fail_if (strcmp (g_quark_to_string (quark), "custom-success"));
532
533   ret = GST_FLOW_CUSTOM_ERROR - 2;
534   fail_if (strcmp (gst_flow_get_name (ret), "custom-error"));
535   quark = gst_flow_to_quark (ret);
536   fail_if (strcmp (g_quark_to_string (quark), "custom-error"));
537
538   /* unknown values */
539   ret = GST_FLOW_CUSTOM_ERROR + 2;
540   fail_if (strcmp (gst_flow_get_name (ret), "unknown"));
541   quark = gst_flow_to_quark (ret);
542   fail_unless (quark == 0);
543 }
544
545 GST_END_TEST;
546
547 GST_START_TEST (test_push_negotiation)
548 {
549   GstPad *src, *sink;
550   GstPadLinkReturn plr;
551   GstCaps *srccaps =
552       gst_caps_from_string ("audio/x-raw-int,width={16,32},depth={16,32}");
553   GstCaps *sinkcaps =
554       gst_caps_from_string ("audio/x-raw-int,width=32,depth={16,32}");
555   GstPadTemplate *src_template;
556   GstPadTemplate *sink_template;
557   GstCaps *caps;
558   GstBuffer *buffer;
559
560   /* setup */
561   src_template = gst_pad_template_new ("src", GST_PAD_SRC,
562       GST_PAD_ALWAYS, srccaps);
563   sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
564       GST_PAD_ALWAYS, sinkcaps);
565
566   sink = gst_pad_new_from_template (sink_template, "sink");
567   fail_if (sink == NULL);
568   gst_pad_set_chain_function (sink, gst_check_chain_func);
569
570   src = gst_pad_new_from_template (src_template, "src");
571   fail_if (src == NULL);
572
573   plr = gst_pad_link (src, sink);
574   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
575
576   buffer = gst_buffer_new ();
577
578   /* activate pads */
579   gst_pad_set_active (src, TRUE);
580   gst_pad_set_active (sink, TRUE);
581
582   caps = gst_caps_from_string ("audio/x-raw-int,width=16,depth=16");
583
584   /* Should fail if src pad caps are incompatible with sink pad caps */
585   gst_pad_set_caps (src, caps);
586   gst_buffer_set_caps (buffer, caps);
587   gst_buffer_ref (buffer);
588   fail_unless (gst_pad_push (src, buffer) == GST_FLOW_NOT_NEGOTIATED);
589   ASSERT_MINI_OBJECT_REFCOUNT (buffer, "buffer", 1);
590   gst_buffer_unref (buffer);
591
592   /* teardown */
593   gst_pad_unlink (src, sink);
594   gst_object_unref (src);
595   gst_object_unref (sink);
596   gst_caps_unref (caps);
597   gst_object_unref (sink_template);
598   gst_object_unref (src_template);
599 }
600
601 GST_END_TEST;
602
603 /* see that an unref also unlinks the pads */
604 GST_START_TEST (test_src_unref_unlink)
605 {
606   GstPad *src, *sink;
607   GstCaps *caps;
608   GstPadLinkReturn plr;
609
610   sink = gst_pad_new ("sink", GST_PAD_SINK);
611   fail_if (sink == NULL);
612
613   src = gst_pad_new ("src", GST_PAD_SRC);
614   fail_if (src == NULL);
615
616   caps = gst_caps_from_string ("foo/bar");
617
618   gst_pad_set_caps (src, caps);
619   gst_pad_set_caps (sink, caps);
620
621   plr = gst_pad_link (src, sink);
622   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
623
624   /* unref the srcpad */
625   gst_object_unref (src);
626
627   /* sink should be unlinked now */
628   fail_if (gst_pad_is_linked (sink));
629
630   /* cleanup */
631   gst_object_unref (sink);
632   gst_caps_unref (caps);
633 }
634
635 GST_END_TEST;
636
637 /* see that an unref also unlinks the pads */
638 GST_START_TEST (test_sink_unref_unlink)
639 {
640   GstPad *src, *sink;
641   GstCaps *caps;
642   GstPadLinkReturn plr;
643
644   sink = gst_pad_new ("sink", GST_PAD_SINK);
645   fail_if (sink == NULL);
646
647   src = gst_pad_new ("src", GST_PAD_SRC);
648   fail_if (src == NULL);
649
650   caps = gst_caps_from_string ("foo/bar");
651
652   gst_pad_set_caps (src, caps);
653   gst_pad_set_caps (sink, caps);
654
655   plr = gst_pad_link (src, sink);
656   fail_unless (GST_PAD_LINK_SUCCESSFUL (plr));
657
658   /* unref the sinkpad */
659   gst_object_unref (sink);
660
661   /* src should be unlinked now */
662   fail_if (gst_pad_is_linked (src));
663
664   /* cleanup */
665   gst_object_unref (src);
666   gst_caps_unref (caps);
667 }
668
669 GST_END_TEST;
670
671 static void
672 unblock_async_cb (GstPad * pad, gboolean blocked, gpointer user_data)
673 {
674   gboolean *bool_user_data = (gboolean *) user_data;
675
676   /* here we should have blocked == 1 unblocked == 0 */
677   fail_unless (bool_user_data[0] == TRUE);
678   fail_unless (bool_user_data[1] == FALSE);
679
680   bool_user_data[1] = TRUE;
681 }
682
683 static void
684 block_async_cb (GstPad * pad, gboolean blocked, gpointer user_data)
685 {
686   gboolean *bool_user_data = (gboolean *) user_data;
687
688   /* here we should have blocked == 0 unblocked == 0 */
689   fail_unless (bool_user_data[0] == FALSE);
690   fail_unless (bool_user_data[1] == FALSE);
691
692   bool_user_data[0] = blocked;
693
694   gst_pad_set_blocked_async (pad, FALSE, unblock_async_cb, user_data);
695 }
696
697 GST_START_TEST (test_block_async)
698 {
699   GstPad *pad;
700   /* we set data[0] = TRUE when the pad is blocked, data[1] = TRUE when it's
701    * unblocked */
702   gboolean data[2] = { FALSE, FALSE };
703
704   pad = gst_pad_new ("src", GST_PAD_SRC);
705   fail_unless (pad != NULL);
706
707   gst_pad_set_active (pad, TRUE);
708   gst_pad_set_blocked_async (pad, TRUE, block_async_cb, &data);
709
710   fail_unless (data[0] == FALSE);
711   fail_unless (data[1] == FALSE);
712   gst_pad_push (pad, gst_buffer_new ());
713
714   gst_object_unref (pad);
715 }
716
717 GST_END_TEST;
718
719 #if 0
720 static void
721 block_async_second (GstPad * pad, gboolean blocked, gpointer user_data)
722 {
723   gst_pad_set_blocked_async (pad, FALSE, unblock_async_cb, NULL);
724 }
725
726 static void
727 block_async_first (GstPad * pad, gboolean blocked, gpointer user_data)
728 {
729   static int n_calls = 0;
730   gboolean *bool_user_data = (gboolean *) user_data;
731
732   if (++n_calls > 1)
733     /* we expect this callback to be called only once */
734     g_warn_if_reached ();
735
736   *bool_user_data = blocked;
737
738   /* replace block_async_first with block_async_second so next time the pad is
739    * blocked the latter should be called */
740   gst_pad_set_blocked_async (pad, TRUE, block_async_second, NULL);
741
742   /* unblock temporarily, in the next push block_async_second should be called
743    */
744   gst_pad_push_event (pad, gst_event_new_flush_start ());
745 }
746
747 GST_START_TEST (test_block_async_replace_callback)
748 {
749   GstPad *pad;
750   gboolean blocked;
751
752   pad = gst_pad_new ("src", GST_PAD_SRC);
753   fail_unless (pad != NULL);
754   gst_pad_set_active (pad, TRUE);
755
756   gst_pad_set_blocked_async (pad, TRUE, block_async_first, &blocked);
757   blocked = FALSE;
758
759   gst_pad_push (pad, gst_buffer_new ());
760   fail_unless (blocked == TRUE);
761   /* block_async_first flushes to unblock */
762   gst_pad_push_event (pad, gst_event_new_flush_stop ());
763
764   /* push again, this time block_async_second should be called */
765   gst_pad_push (pad, gst_buffer_new ());
766   fail_unless (blocked == TRUE);
767
768   gst_object_unref (pad);
769 }
770
771 GST_END_TEST;
772 #endif
773
774 static void
775 block_async_full_destroy (gpointer user_data)
776 {
777   gint *state = (gint *) user_data;
778
779   fail_unless (*state < 2);
780
781   GST_DEBUG ("setting state to 2");
782   *state = 2;
783 }
784
785 static void
786 block_async_full_cb (GstPad * pad, gboolean blocked, gpointer user_data)
787 {
788   *(gint *) user_data = (gint) blocked;
789
790   gst_pad_push_event (pad, gst_event_new_flush_start ());
791   GST_DEBUG ("setting state to 1");
792 }
793
794 GST_START_TEST (test_block_async_full_destroy)
795 {
796   GstPad *pad;
797   /* 0 = unblocked, 1 = blocked, 2 = destroyed */
798   gint state = 0;
799
800   pad = gst_pad_new ("src", GST_PAD_SRC);
801   fail_unless (pad != NULL);
802   gst_pad_set_active (pad, TRUE);
803
804   gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
805       &state, block_async_full_destroy);
806   fail_unless (state == 0);
807
808   gst_pad_push (pad, gst_buffer_new ());
809   /* block_async_full_cb sets state to 1 and then flushes to unblock temporarily
810    */
811   fail_unless (state == 1);
812   gst_pad_push_event (pad, gst_event_new_flush_stop ());
813
814   /* pad was already blocked so nothing happens */
815   gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
816       &state, block_async_full_destroy);
817   fail_unless (state == 1);
818
819   /* unblock with the same data, callback is called */
820   gst_pad_set_blocked_async_full (pad, FALSE, block_async_full_cb,
821       &state, block_async_full_destroy);
822   fail_unless (state == 2);
823
824   /* block with the same data, callback is called */
825   state = 1;
826   gst_pad_set_blocked_async_full (pad, TRUE, block_async_full_cb,
827       &state, block_async_full_destroy);
828   fail_unless (state == 2);
829
830   /* now change user_data (to NULL in this case) so destroy_notify should be
831    * called */
832   state = 1;
833   gst_pad_set_blocked_async_full (pad, FALSE, block_async_full_cb,
834       NULL, block_async_full_destroy);
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   gst_pad_set_blocked_async_full (pad, TRUE, 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_pad_dispose 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 static void
871 unblock_async_no_flush_cb (GstPad * pad, gboolean blocked, gpointer user_data)
872 {
873   gboolean *bool_user_data = (gboolean *) user_data;
874
875   /* here we should have blocked == 1 unblocked == 0 */
876
877   fail_unless (blocked == FALSE);
878
879   fail_unless (bool_user_data[0] == TRUE);
880   fail_unless (bool_user_data[1] == TRUE);
881   fail_unless (bool_user_data[2] == FALSE);
882
883   bool_user_data[2] = TRUE;
884 }
885
886
887 static void
888 unblock_async_not_called (GstPad * pad, gboolean blocked, gpointer user_data)
889 {
890   g_warn_if_reached ();
891 }
892
893 static void
894 block_async_second_no_flush (GstPad * pad, gboolean blocked, gpointer user_data)
895 {
896   gboolean *bool_user_data = (gboolean *) user_data;
897
898   fail_unless (blocked == TRUE);
899
900   fail_unless (bool_user_data[0] == TRUE);
901   fail_unless (bool_user_data[1] == FALSE);
902   fail_unless (bool_user_data[2] == FALSE);
903
904   bool_user_data[1] = TRUE;
905
906   fail_unless (gst_pad_set_blocked_async (pad, FALSE, unblock_async_no_flush_cb,
907           user_data));
908 }
909
910 static void
911 block_async_first_no_flush (GstPad * pad, gboolean blocked, gpointer user_data)
912 {
913   static int n_calls = 0;
914   gboolean *bool_user_data = (gboolean *) user_data;
915
916   fail_unless (blocked == TRUE);
917
918   if (++n_calls > 1)
919     /* we expect this callback to be called only once */
920     g_warn_if_reached ();
921
922   *bool_user_data = blocked;
923
924   fail_unless (bool_user_data[0] == TRUE);
925   fail_unless (bool_user_data[1] == FALSE);
926   fail_unless (bool_user_data[2] == FALSE);
927
928   fail_unless (gst_pad_set_blocked_async (pad, FALSE, unblock_async_not_called,
929           NULL));
930
931   /* replace block_async_first with block_async_second so next time the pad is
932    * blocked the latter should be called */
933   fail_unless (gst_pad_set_blocked_async (pad, TRUE,
934           block_async_second_no_flush, user_data));
935 }
936
937 GST_START_TEST (test_block_async_replace_callback_no_flush)
938 {
939   GstPad *pad;
940   gboolean bool_user_data[3] = { FALSE, FALSE, FALSE };
941
942   pad = gst_pad_new ("src", GST_PAD_SRC);
943   fail_unless (pad != NULL);
944   gst_pad_set_active (pad, TRUE);
945
946   fail_unless (gst_pad_set_blocked_async (pad, TRUE, block_async_first_no_flush,
947           bool_user_data));
948
949   gst_pad_push (pad, gst_buffer_new ());
950   fail_unless (bool_user_data[0] == TRUE);
951   fail_unless (bool_user_data[1] == TRUE);
952   fail_unless (bool_user_data[2] == TRUE);
953
954   gst_object_unref (pad);
955 }
956
957 GST_END_TEST;
958
959
960 static Suite *
961 gst_pad_suite (void)
962 {
963   Suite *s = suite_create ("GstPad");
964   TCase *tc_chain = tcase_create ("general");
965
966   /* turn off timeout */
967   tcase_set_timeout (tc_chain, 60);
968
969   suite_add_tcase (s, tc_chain);
970   tcase_add_test (tc_chain, test_link);
971   tcase_add_test (tc_chain, test_refcount);
972   tcase_add_test (tc_chain, test_get_allowed_caps);
973   tcase_add_test (tc_chain, test_link_unlink_threaded);
974   tcase_add_test (tc_chain, test_name_is_valid);
975   tcase_add_test (tc_chain, test_push_unlinked);
976   tcase_add_test (tc_chain, test_push_linked);
977   tcase_add_test (tc_chain, test_push_buffer_list_compat);
978   tcase_add_test (tc_chain, test_flowreturn);
979   tcase_add_test (tc_chain, test_push_negotiation);
980   tcase_add_test (tc_chain, test_src_unref_unlink);
981   tcase_add_test (tc_chain, test_sink_unref_unlink);
982   tcase_add_test (tc_chain, test_block_async);
983 #if 0
984   tcase_add_test (tc_chain, test_block_async_replace_callback);
985 #endif
986   tcase_add_test (tc_chain, test_block_async_full_destroy);
987   tcase_add_test (tc_chain, test_block_async_full_destroy_dispose);
988   tcase_add_test (tc_chain, test_block_async_replace_callback_no_flush);
989
990   return s;
991 }
992
993 GST_CHECK_MAIN (gst_pad);