1 /* GStreamer
2 *
3 * unit test for fakesrc
4 *
5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/check/gstcheck.h>
27
28 static gboolean have_eos = FALSE;
29
30 static GstPad *mysinkpad;
31
32 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
33 GST_PAD_SINK,
34 GST_PAD_ALWAYS,
35 GST_STATIC_CAPS_ANY);
36
37 static gboolean
event_func(GstPad * pad,GstObject * parent,GstEvent * event)38 event_func (GstPad * pad, GstObject * parent, GstEvent * event)
39 {
40 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
41 have_eos = TRUE;
42 }
43 gst_event_unref (event);
44 return TRUE;
45 }
46
47 static GstElement *
setup_fakesrc(void)48 setup_fakesrc (void)
49 {
50 GstElement *fakesrc;
51
52 GST_DEBUG ("setup_fakesrc");
53 fakesrc = gst_check_setup_element ("fakesrc");
54 mysinkpad = gst_check_setup_sink_pad (fakesrc, &sinktemplate);
55 gst_pad_set_event_function (mysinkpad, event_func);
56 gst_pad_set_active (mysinkpad, TRUE);
57 have_eos = FALSE;
58 return fakesrc;
59 }
60
61 static void
cleanup_fakesrc(GstElement * fakesrc)62 cleanup_fakesrc (GstElement * fakesrc)
63 {
64 gst_pad_set_active (mysinkpad, FALSE);
65 gst_check_teardown_sink_pad (fakesrc);
66 gst_check_teardown_element (fakesrc);
67 }
68
GST_START_TEST(test_num_buffers)69 GST_START_TEST (test_num_buffers)
70 {
71 GstElement *src;
72
73 src = setup_fakesrc ();
74 g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
75 fail_unless (gst_element_set_state (src,
76 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
77 "could not set to playing");
78
79 while (!have_eos) {
80 g_usleep (1000);
81 }
82
83 fail_unless_equals_int (g_list_length (buffers), 3);
84 gst_check_drop_buffers ();
85
86 fail_unless (gst_element_set_state (src,
87 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
88
89 /* cleanup */
90 cleanup_fakesrc (src);
91 }
92
93 GST_END_TEST;
94
GST_START_TEST(test_sizetype_empty)95 GST_START_TEST (test_sizetype_empty)
96 {
97 GstElement *src;
98 GList *l;
99
100 src = setup_fakesrc ();
101
102 g_object_set (G_OBJECT (src), "sizetype", 1, NULL);
103 g_object_set (G_OBJECT (src), "num-buffers", 100, NULL);
104
105 fail_unless (gst_element_set_state (src,
106 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
107 "could not set to playing");
108
109 while (!have_eos) {
110 g_usleep (1000);
111 }
112
113 fail_unless_equals_int (g_list_length (buffers), 100);
114 l = buffers;
115 while (l) {
116 GstBuffer *buf = l->data;
117
118 fail_unless (gst_buffer_get_size (buf) == 0);
119 l = l->next;
120 }
121 gst_check_drop_buffers ();
122
123 fail_unless (gst_element_set_state (src,
124 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
125
126 /* cleanup */
127 cleanup_fakesrc (src);
128 }
129
130 GST_END_TEST;
131
GST_START_TEST(test_sizetype_fixed)132 GST_START_TEST (test_sizetype_fixed)
133 {
134 GstElement *src;
135 GList *l;
136
137 src = setup_fakesrc ();
138
139 g_object_set (G_OBJECT (src), "sizetype", 2, NULL);
140 g_object_set (G_OBJECT (src), "sizemax", 8192, NULL);
141 g_object_set (G_OBJECT (src), "num-buffers", 100, NULL);
142
143 fail_unless (gst_element_set_state (src,
144 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
145 "could not set to playing");
146
147 while (!have_eos) {
148 g_usleep (1000);
149 }
150
151 fail_unless (g_list_length (buffers) == 100);
152 l = buffers;
153 while (l) {
154 GstBuffer *buf = l->data;
155
156 fail_unless (gst_buffer_get_size (buf) == 8192);
157 l = l->next;
158 }
159 gst_check_drop_buffers ();
160
161 fail_unless (gst_element_set_state (src,
162 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
163
164 /* cleanup */
165 cleanup_fakesrc (src);
166 }
167
168 GST_END_TEST;
169
GST_START_TEST(test_sizetype_random)170 GST_START_TEST (test_sizetype_random)
171 {
172 GstElement *src;
173 GList *l;
174
175 src = setup_fakesrc ();
176
177 g_object_set (G_OBJECT (src), "sizetype", 3, NULL);
178 g_object_set (G_OBJECT (src), "sizemin", 4096, NULL);
179 g_object_set (G_OBJECT (src), "sizemax", 8192, NULL);
180 g_object_set (G_OBJECT (src), "num-buffers", 100, NULL);
181
182 fail_unless (gst_element_set_state (src,
183 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
184 "could not set to playing");
185
186 while (!have_eos) {
187 g_usleep (1000);
188 }
189
190 fail_unless (g_list_length (buffers) == 100);
191 l = buffers;
192 while (l) {
193 GstBuffer *buf = l->data;
194
195 fail_if (gst_buffer_get_size (buf) > 8192);
196 fail_if (gst_buffer_get_size (buf) < 4096);
197 l = l->next;
198 }
199 gst_check_drop_buffers ();
200
201 fail_unless (gst_element_set_state (src,
202 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
203
204 /* cleanup */
205 cleanup_fakesrc (src);
206 }
207
208 GST_END_TEST;
209
GST_START_TEST(test_no_preroll)210 GST_START_TEST (test_no_preroll)
211 {
212 GstElement *src;
213 GstStateChangeReturn ret;
214
215 src = setup_fakesrc ();
216
217 g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
218
219 ret = gst_element_set_state (src, GST_STATE_PAUSED);
220
221 fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
222 "error going to paused the first time");
223
224 ret = gst_element_set_state (src, GST_STATE_PAUSED);
225
226 fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
227 "error going to paused the second time");
228
229 fail_unless (gst_element_set_state (src,
230 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
231
232 /* cleanup */
233 cleanup_fakesrc (src);
234 }
235
236 GST_END_TEST;
237
238 static void
handoff_cb(GstElement * element,GstBuffer * buf,GstPad * pad,gint * p_counter)239 handoff_cb (GstElement * element, GstBuffer * buf, GstPad * pad,
240 gint * p_counter)
241 {
242 *p_counter += 1;
243 GST_LOG ("counter = %d", *p_counter);
244 }
245
GST_START_TEST(test_reuse_push)246 GST_START_TEST (test_reuse_push)
247 {
248 GstElement *src, *sep, *sink, *pipeline;
249 GstBus *bus;
250 gint counter, repeat = 3, num_buffers = 10;
251
252 pipeline = gst_pipeline_new ("pipeline");
253 fail_unless (pipeline != NULL, "Failed to create pipeline!");
254
255 bus = gst_element_get_bus (pipeline);
256
257 src = gst_element_factory_make ("fakesrc", "fakesrc");
258 fail_unless (src != NULL, "Failed to create 'fakesrc' element!");
259
260 sep = gst_element_factory_make ("queue", "queue");
261 fail_unless (sep != NULL, "Failed to create 'queue' element");
262
263 sink = gst_element_factory_make ("fakesink", "fakesink");
264 fail_unless (sink != NULL, "Failed to create 'fakesink' element!");
265
266 g_object_set (sink, "signal-handoffs", TRUE, NULL);
267 g_signal_connect (sink, "handoff", G_CALLBACK (handoff_cb), &counter);
268
269 gst_bin_add_many (GST_BIN (pipeline), src, sep, sink, NULL);
270
271 fail_unless (gst_element_link (src, sep));
272 fail_unless (gst_element_link (sep, sink));
273
274 g_object_set (src, "num-buffers", num_buffers, NULL);
275
276 do {
277 GstStateChangeReturn state_ret;
278 GstMessage *msg;
279
280 GST_INFO ("====================== round %d ======================", repeat);
281
282 counter = 0;
283
284 state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
285 fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
286
287 if (state_ret == GST_STATE_CHANGE_ASYNC) {
288 GST_LOG ("waiting for pipeline to reach PAUSED state");
289 state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
290 fail_unless_equals_int (state_ret, GST_STATE_CHANGE_SUCCESS);
291 }
292
293 GST_LOG ("PAUSED, let's read all of it");
294
295 state_ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
296 fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
297
298 msg = gst_bus_poll (bus, GST_MESSAGE_EOS, -1);
299 fail_unless (msg != NULL, "Expected EOS message on bus!");
300
301 gst_message_unref (msg);
302
303 if (num_buffers >= 0) {
304 fail_unless_equals_int (counter, num_buffers);
305 }
306
307 fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
308 GST_STATE_CHANGE_SUCCESS);
309
310 --repeat;
311 } while (repeat > 0);
312
313 gst_object_unref (bus);
314 gst_object_unref (pipeline);
315 }
316
317 GST_END_TEST;
318
319 static Suite *
fakesrc_suite(void)320 fakesrc_suite (void)
321 {
322 Suite *s = suite_create ("fakesrc");
323 TCase *tc_chain = tcase_create ("general");
324
325 suite_add_tcase (s, tc_chain);
326 tcase_add_test (tc_chain, test_num_buffers);
327 tcase_add_test (tc_chain, test_sizetype_empty);
328 tcase_add_test (tc_chain, test_sizetype_fixed);
329 tcase_add_test (tc_chain, test_sizetype_random);
330 tcase_add_test (tc_chain, test_no_preroll);
331 tcase_add_test (tc_chain, test_reuse_push);
332
333 return s;
334 }
335
336 GST_CHECK_MAIN (fakesrc);
337