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