Fix FSF address
[platform/upstream/gstreamer.git] / tests / check / elements / funnel.c
1 /* GStreamer unit tests for the funnel
2  *
3  * Copyright (C) 2008 Collabora, Nokia
4  * @author: Olivier Crete <olivier.crete@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
19 */
20
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <gst/check/gstcheck.h>
27
28 struct TestData
29 {
30   GstElement *funnel;
31   GstPad *funnelsrc, *funnelsink11, *funnelsink22;
32   GstPad *mysink, *mysrc1, *mysrc2;
33   GstCaps *mycaps;
34 };
35
36 static void
37 setup_test_objects (struct TestData *td, GstPadChainFunction chain_func)
38 {
39   td->mycaps = gst_caps_new_empty_simple ("test/test");
40
41   td->funnel = gst_element_factory_make ("funnel", NULL);
42
43   td->funnelsrc = gst_element_get_static_pad (td->funnel, "src");
44   fail_unless (td->funnelsrc != NULL);
45
46   td->funnelsink11 = gst_element_get_request_pad (td->funnel, "sink_11");
47   fail_unless (td->funnelsink11 != NULL);
48   fail_unless (!strcmp (GST_OBJECT_NAME (td->funnelsink11), "sink_11"));
49
50   td->funnelsink22 = gst_element_get_request_pad (td->funnel, "sink_22");
51   fail_unless (td->funnelsink22 != NULL);
52   fail_unless (!strcmp (GST_OBJECT_NAME (td->funnelsink22), "sink_22"));
53
54   fail_unless (gst_element_set_state (td->funnel, GST_STATE_PLAYING) ==
55       GST_STATE_CHANGE_SUCCESS);
56
57   td->mysink = gst_pad_new ("sink", GST_PAD_SINK);
58   gst_pad_set_chain_function (td->mysink, chain_func);
59   gst_pad_set_active (td->mysink, TRUE);
60   gst_pad_set_caps (td->mysink, td->mycaps);
61
62   td->mysrc1 = gst_pad_new ("src1", GST_PAD_SRC);
63   gst_pad_set_active (td->mysrc1, TRUE);
64   gst_pad_set_caps (td->mysrc1, td->mycaps);
65
66   td->mysrc2 = gst_pad_new ("src2", GST_PAD_SRC);
67   gst_pad_set_active (td->mysrc2, TRUE);
68   gst_pad_set_caps (td->mysrc2, td->mycaps);
69
70   fail_unless (GST_PAD_LINK_SUCCESSFUL (gst_pad_link (td->funnelsrc,
71               td->mysink)));
72
73   fail_unless (GST_PAD_LINK_SUCCESSFUL (gst_pad_link (td->mysrc1,
74               td->funnelsink11)));
75
76   fail_unless (GST_PAD_LINK_SUCCESSFUL (gst_pad_link (td->mysrc2,
77               td->funnelsink22)));
78
79 }
80
81 static void
82 release_test_objects (struct TestData *td)
83 {
84   gst_pad_set_active (td->mysink, FALSE);
85   gst_pad_set_active (td->mysrc1, FALSE);
86   gst_pad_set_active (td->mysrc1, FALSE);
87
88   gst_object_unref (td->mysink);
89   gst_object_unref (td->mysrc1);
90   gst_object_unref (td->mysrc2);
91
92   fail_unless (gst_element_set_state (td->funnel, GST_STATE_NULL) ==
93       GST_STATE_CHANGE_SUCCESS);
94
95   gst_object_unref (td->funnelsrc);
96   gst_element_release_request_pad (td->funnel, td->funnelsink11);
97   gst_object_unref (td->funnelsink11);
98   gst_element_release_request_pad (td->funnel, td->funnelsink22);
99   gst_object_unref (td->funnelsink22);
100
101   gst_caps_unref (td->mycaps);
102   gst_object_unref (td->funnel);
103 }
104
105 static gint bufcount = 0;
106 static gint alloccount = 0;
107
108 static GstFlowReturn
109 chain_ok (GstPad * pad, GstObject * parent, GstBuffer * buffer)
110 {
111   bufcount++;
112
113   gst_buffer_unref (buffer);
114
115   return GST_FLOW_OK;
116 }
117
118 GST_START_TEST (test_funnel_simple)
119 {
120   struct TestData td;
121 #if 0
122   GstBuffer *buf1 = NULL;
123   GstBuffer *buf2 = NULL;
124 #endif
125
126   setup_test_objects (&td, chain_ok);
127
128   bufcount = 0;
129   alloccount = 0;
130
131   fail_unless (gst_pad_push (td.mysrc1, gst_buffer_new ()) == GST_FLOW_OK);
132   fail_unless (gst_pad_push (td.mysrc2, gst_buffer_new ()) == GST_FLOW_OK);
133
134   fail_unless (bufcount == 2);
135
136 #if 0
137   fail_unless (gst_pad_alloc_buffer (td.mysrc1, 0, 1024, td.mycaps,
138           &buf1) == GST_FLOW_OK);
139   fail_unless (gst_pad_alloc_buffer (td.mysrc2, 1024, 1024, td.mycaps,
140           &buf2) == GST_FLOW_OK);
141
142   fail_unless (alloccount == 2);
143
144   gst_buffer_unref (buf1);
145   gst_buffer_unref (buf2);
146 #endif
147
148   release_test_objects (&td);
149 }
150
151 GST_END_TEST;
152
153 guint num_eos = 0;
154
155 static gboolean
156 eos_event_func (GstPad * pad, GstObject * parent, GstEvent * event)
157 {
158   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
159     ++num_eos;
160
161   return gst_pad_event_default (pad, parent, event);
162 }
163
164 GST_START_TEST (test_funnel_eos)
165 {
166   struct TestData td;
167
168   setup_test_objects (&td, chain_ok);
169
170   num_eos = 0;
171   bufcount = 0;
172
173   gst_pad_set_event_function (td.mysink, eos_event_func);
174
175   fail_unless (gst_pad_push (td.mysrc1, gst_buffer_new ()) == GST_FLOW_OK);
176   fail_unless (gst_pad_push (td.mysrc2, gst_buffer_new ()) == GST_FLOW_OK);
177
178   fail_unless (bufcount == 2);
179
180   fail_unless (gst_pad_push_event (td.mysrc1, gst_event_new_eos ()));
181   fail_unless (num_eos == 0);
182
183   fail_unless (gst_pad_push (td.mysrc1, gst_buffer_new ()) == GST_FLOW_EOS);
184   fail_unless (gst_pad_push (td.mysrc2, gst_buffer_new ()) == GST_FLOW_OK);
185
186   fail_unless (bufcount == 3);
187
188   fail_unless (gst_pad_push_event (td.mysrc2, gst_event_new_eos ()));
189   fail_unless (num_eos == 1);
190
191   fail_unless (gst_pad_push (td.mysrc1, gst_buffer_new ()) == GST_FLOW_EOS);
192   fail_unless (gst_pad_push (td.mysrc2, gst_buffer_new ()) == GST_FLOW_EOS);
193
194   fail_unless (bufcount == 3);
195
196   fail_unless (gst_pad_push_event (td.mysrc1, gst_event_new_flush_start ()));
197   fail_unless (gst_pad_push_event (td.mysrc1, gst_event_new_flush_stop (TRUE)));
198
199   fail_unless (gst_pad_push (td.mysrc1, gst_buffer_new ()) == GST_FLOW_OK);
200   fail_unless (gst_pad_push (td.mysrc2, gst_buffer_new ()) == GST_FLOW_EOS);
201
202   fail_unless (bufcount == 4);
203
204   fail_unless (gst_pad_unlink (td.mysrc1, td.funnelsink11));
205   gst_element_release_request_pad (td.funnel, td.funnelsink11);
206   gst_object_unref (td.funnelsink11);
207   fail_unless (num_eos == 2);
208
209   td.funnelsink11 = gst_element_get_request_pad (td.funnel, "sink_11");
210   fail_unless (td.funnelsink11 != NULL);
211   fail_unless (!strcmp (GST_OBJECT_NAME (td.funnelsink11), "sink_11"));
212
213   fail_unless (GST_PAD_LINK_SUCCESSFUL (gst_pad_link (td.mysrc1,
214               td.funnelsink11)));
215
216   /* This will fail because everything is EOS already */
217   fail_if (gst_pad_push_event (td.mysrc1, gst_event_new_eos ()));
218   fail_unless (num_eos == 2);
219
220   fail_unless (gst_pad_unlink (td.mysrc1, td.funnelsink11));
221   gst_element_release_request_pad (td.funnel, td.funnelsink11);
222   gst_object_unref (td.funnelsink11);
223   fail_unless (num_eos == 2);
224
225   td.funnelsink11 = gst_element_get_request_pad (td.funnel, "sink_11");
226   fail_unless (td.funnelsink11 != NULL);
227   fail_unless (!strcmp (GST_OBJECT_NAME (td.funnelsink11), "sink_11"));
228
229   release_test_objects (&td);
230 }
231
232 GST_END_TEST;
233
234 static Suite *
235 funnel_suite (void)
236 {
237   Suite *s = suite_create ("funnel");
238   TCase *tc_chain;
239   GLogLevelFlags fatal_mask;
240
241   fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
242   fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
243   g_log_set_always_fatal (fatal_mask);
244
245   tc_chain = tcase_create ("funnel simple");
246   tcase_add_test (tc_chain, test_funnel_simple);
247   tcase_add_test (tc_chain, test_funnel_eos);
248   suite_add_tcase (s, tc_chain);
249
250   return s;
251 }
252
253 GST_CHECK_MAIN (funnel);