1770e819e7fb6c446ddc9e3b56d7f9720bab2bbc
[platform/upstream/gstreamer.git] / tests / check / elements / fakesrc.c
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26
27 gboolean have_eos = FALSE;
28
29 GstPad *mysinkpad;
30
31 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
32     GST_PAD_SINK,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS_ANY);
35
36 gboolean
37 event_func (GstPad * pad, GstEvent * event)
38 {
39   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
40     /* we take the lock here because it's good practice to so, even though
41      * no buffers will be pushed anymore anyway */
42     GST_STREAM_LOCK (pad);
43     have_eos = TRUE;
44     GST_STREAM_UNLOCK (pad);
45     gst_event_unref (event);
46     return TRUE;
47   }
48
49   gst_event_unref (event);
50   return FALSE;
51 }
52
53 GstElement *
54 setup_fakesrc ()
55 {
56   GstElement *fakesrc;
57
58   GST_DEBUG ("setup_fakesrc");
59   fakesrc = gst_check_setup_element ("fakesrc");
60   mysinkpad = gst_check_setup_sink_pad (fakesrc, &sinktemplate, NULL);
61   gst_pad_set_event_function (mysinkpad, event_func);
62   gst_pad_set_active (mysinkpad, TRUE);
63   return fakesrc;
64 }
65
66 void
67 cleanup_fakesrc (GstElement * fakesrc)
68 {
69   gst_check_teardown_sink_pad (fakesrc);
70   gst_check_teardown_element (fakesrc);
71 }
72
73 GST_START_TEST (test_num_buffers)
74 {
75   GstElement *src;
76
77   src = setup_fakesrc ();
78   g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
79   fail_unless (gst_element_set_state (src,
80           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
81       "could not set to playing");
82
83   while (!have_eos) {
84     g_usleep (1000);
85   }
86
87   fail_unless (g_list_length (buffers) == 3);
88   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
89   g_list_free (buffers);
90
91   /* cleanup */
92   cleanup_fakesrc (src);
93 }
94
95 GST_END_TEST;
96
97 GST_START_TEST (test_sizetype_empty)
98 {
99   GstElement *src;
100   GList *l;
101
102   src = setup_fakesrc ();
103
104   g_object_set (G_OBJECT (src), "sizetype", 1, NULL);
105   g_object_set (G_OBJECT (src), "num-buffers", 100, NULL);
106
107   fail_unless (gst_element_set_state (src,
108           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
109       "could not set to playing");
110
111   while (!have_eos) {
112     g_usleep (1000);
113   }
114
115   fail_unless (g_list_length (buffers) == 100);
116   l = buffers;
117   while (l) {
118     GstBuffer *buf = l->data;
119
120     fail_unless (GST_BUFFER_SIZE (buf) == 0);
121     l = l->next;
122   }
123   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
124   g_list_free (buffers);
125
126   /* cleanup */
127   cleanup_fakesrc (src);
128 }
129
130 GST_END_TEST;
131
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_SIZE (buf) == 8192);
157     l = l->next;
158   }
159   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
160   g_list_free (buffers);
161
162   /* cleanup */
163   cleanup_fakesrc (src);
164 }
165
166 GST_END_TEST;
167
168 GST_START_TEST (test_sizetype_random)
169 {
170   GstElement *src;
171   GList *l;
172
173   src = setup_fakesrc ();
174
175   g_object_set (G_OBJECT (src), "sizetype", 3, NULL);
176   g_object_set (G_OBJECT (src), "sizemin", 4096, NULL);
177   g_object_set (G_OBJECT (src), "sizemax", 8192, NULL);
178   g_object_set (G_OBJECT (src), "num-buffers", 100, NULL);
179
180   fail_unless (gst_element_set_state (src,
181           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
182       "could not set to playing");
183
184   while (!have_eos) {
185     g_usleep (1000);
186   }
187
188   fail_unless (g_list_length (buffers) == 100);
189   l = buffers;
190   while (l) {
191     GstBuffer *buf = l->data;
192
193     fail_if (GST_BUFFER_SIZE (buf) > 8192);
194     fail_if (GST_BUFFER_SIZE (buf) < 4096);
195     l = l->next;
196   }
197   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
198   g_list_free (buffers);
199
200   /* cleanup */
201   cleanup_fakesrc (src);
202 }
203
204 GST_END_TEST;
205
206
207 Suite *
208 fakesrc_suite (void)
209 {
210   Suite *s = suite_create ("fakesrc");
211   TCase *tc_chain = tcase_create ("general");
212
213   suite_add_tcase (s, tc_chain);
214   tcase_add_test (tc_chain, test_num_buffers);
215   tcase_add_test (tc_chain, test_sizetype_empty);
216   tcase_add_test (tc_chain, test_sizetype_fixed);
217   tcase_add_test (tc_chain, test_sizetype_random);
218
219   return s;
220 }
221
222 int
223 main (int argc, char **argv)
224 {
225   int nf;
226
227   Suite *s = fakesrc_suite ();
228   SRunner *sr = srunner_create (s);
229
230   gst_check_init (&argc, &argv);
231
232   srunner_run_all (sr, CK_NORMAL);
233   nf = srunner_ntests_failed (sr);
234   srunner_free (sr);
235
236   return nf;
237 }