Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / 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 static gboolean have_eos = FALSE;
28
29 static 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 static gboolean
37 event_func (GstPad * pad, GstEvent * event)
38 {
39   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
40     have_eos = TRUE;
41     gst_event_unref (event);
42     return TRUE;
43   }
44
45   gst_event_unref (event);
46   return FALSE;
47 }
48
49 static GstElement *
50 setup_fakesrc (void)
51 {
52   GstElement *fakesrc;
53
54   GST_DEBUG ("setup_fakesrc");
55   fakesrc = gst_check_setup_element ("fakesrc");
56   mysinkpad = gst_check_setup_sink_pad (fakesrc, &sinktemplate, NULL);
57   gst_pad_set_event_function (mysinkpad, event_func);
58   gst_pad_set_active (mysinkpad, TRUE);
59   have_eos = FALSE;
60   return fakesrc;
61 }
62
63 static void
64 cleanup_fakesrc (GstElement * fakesrc)
65 {
66   gst_pad_set_active (mysinkpad, FALSE);
67   gst_check_teardown_sink_pad (fakesrc);
68   gst_check_teardown_element (fakesrc);
69 }
70
71 GST_START_TEST (test_num_buffers)
72 {
73   GstElement *src;
74
75   src = setup_fakesrc ();
76   g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
77   fail_unless (gst_element_set_state (src,
78           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
79       "could not set to playing");
80
81   while (!have_eos) {
82     g_usleep (1000);
83   }
84
85   fail_unless_equals_int (g_list_length (buffers), 3);
86   gst_check_drop_buffers ();
87
88   fail_unless (gst_element_set_state (src,
89           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
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_equals_int (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   gst_check_drop_buffers ();
124
125   fail_unless (gst_element_set_state (src,
126           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
127
128   /* cleanup */
129   cleanup_fakesrc (src);
130 }
131
132 GST_END_TEST;
133
134 GST_START_TEST (test_sizetype_fixed)
135 {
136   GstElement *src;
137   GList *l;
138
139   src = setup_fakesrc ();
140
141   g_object_set (G_OBJECT (src), "sizetype", 2, NULL);
142   g_object_set (G_OBJECT (src), "sizemax", 8192, NULL);
143   g_object_set (G_OBJECT (src), "num-buffers", 100, NULL);
144
145   fail_unless (gst_element_set_state (src,
146           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
147       "could not set to playing");
148
149   while (!have_eos) {
150     g_usleep (1000);
151   }
152
153   fail_unless (g_list_length (buffers) == 100);
154   l = buffers;
155   while (l) {
156     GstBuffer *buf = l->data;
157
158     fail_unless (GST_BUFFER_SIZE (buf) == 8192);
159     l = l->next;
160   }
161   gst_check_drop_buffers ();
162
163   fail_unless (gst_element_set_state (src,
164           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
165
166   /* cleanup */
167   cleanup_fakesrc (src);
168 }
169
170 GST_END_TEST;
171
172 GST_START_TEST (test_sizetype_random)
173 {
174   GstElement *src;
175   GList *l;
176
177   src = setup_fakesrc ();
178
179   g_object_set (G_OBJECT (src), "sizetype", 3, NULL);
180   g_object_set (G_OBJECT (src), "sizemin", 4096, NULL);
181   g_object_set (G_OBJECT (src), "sizemax", 8192, NULL);
182   g_object_set (G_OBJECT (src), "num-buffers", 100, NULL);
183
184   fail_unless (gst_element_set_state (src,
185           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
186       "could not set to playing");
187
188   while (!have_eos) {
189     g_usleep (1000);
190   }
191
192   fail_unless (g_list_length (buffers) == 100);
193   l = buffers;
194   while (l) {
195     GstBuffer *buf = l->data;
196
197     fail_if (GST_BUFFER_SIZE (buf) > 8192);
198     fail_if (GST_BUFFER_SIZE (buf) < 4096);
199     l = l->next;
200   }
201   gst_check_drop_buffers ();
202
203   fail_unless (gst_element_set_state (src,
204           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
205
206   /* cleanup */
207   cleanup_fakesrc (src);
208 }
209
210 GST_END_TEST;
211
212 GST_START_TEST (test_no_preroll)
213 {
214   GstElement *src;
215   GstStateChangeReturn ret;
216
217   src = setup_fakesrc ();
218
219   g_object_set (G_OBJECT (src), "is-live", TRUE, NULL);
220
221   ret = gst_element_set_state (src, GST_STATE_PAUSED);
222
223   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
224       "error going to paused the first time");
225
226   ret = gst_element_set_state (src, GST_STATE_PAUSED);
227
228   fail_unless (ret == GST_STATE_CHANGE_NO_PREROLL,
229       "error going to paused the second time");
230
231   fail_unless (gst_element_set_state (src,
232           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
233
234   /* cleanup */
235   cleanup_fakesrc (src);
236 }
237
238 GST_END_TEST;
239
240 static Suite *
241 fakesrc_suite (void)
242 {
243   Suite *s = suite_create ("fakesrc");
244   TCase *tc_chain = tcase_create ("general");
245
246   suite_add_tcase (s, tc_chain);
247   tcase_add_test (tc_chain, test_num_buffers);
248   tcase_add_test (tc_chain, test_sizetype_empty);
249   tcase_add_test (tc_chain, test_sizetype_fixed);
250   tcase_add_test (tc_chain, test_sizetype_random);
251   tcase_add_test (tc_chain, test_no_preroll);
252
253   return s;
254 }
255
256 GST_CHECK_MAIN (fakesrc);