Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / tests / check / elements / queue2.c
1 /* GStreamer unit tests for queue2
2  *
3  * Copyright (C) 2011 Tim-Philipp Müller <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gst/check/gstcheck.h>
22
23 static GstElement *
24 setup_queue2 (GstElement * pipe, GstElement * input, GstElement * output)
25 {
26   GstElement *queue2;
27   GstPad *sinkpad, *srcpad;
28
29   queue2 = gst_element_factory_make ("queue2", NULL);
30   fail_unless (queue2 != NULL, "failed to create 'queue2' element");
31
32   gst_bin_add (GST_BIN (pipe), queue2);
33   gst_bin_add (GST_BIN (pipe), input);
34   gst_bin_add (GST_BIN (pipe), output);
35
36   sinkpad = gst_element_get_static_pad (queue2, "sink");
37   fail_unless (sinkpad != NULL, "failed to get queue2 sink pad");
38
39   srcpad = gst_element_get_static_pad (input, "src");
40   fail_unless (srcpad != NULL, "failed to find src pad for input element");
41
42   fail_unless_equals_int (GST_PAD_LINK_OK, gst_pad_link (srcpad, sinkpad));
43   gst_object_unref (srcpad);
44   gst_object_unref (sinkpad);
45
46   srcpad = gst_element_get_static_pad (queue2, "src");
47   fail_unless (srcpad != NULL);
48
49   sinkpad = gst_element_get_static_pad (output, "sink");
50   fail_unless (sinkpad != NULL, "failed to find sink pad of output element");
51
52   fail_unless_equals_int (GST_PAD_LINK_OK, gst_pad_link (srcpad, sinkpad));
53
54   gst_object_unref (srcpad);
55   gst_object_unref (sinkpad);
56
57   return queue2;
58 }
59
60 GST_START_TEST (test_simple_pipeline)
61 {
62   GstElement *pipe, *input, *output;
63   GstMessage *msg;
64
65   pipe = gst_pipeline_new ("pipeline");
66
67   input = gst_element_factory_make ("fakesrc", NULL);
68   fail_unless (input != NULL, "failed to create 'fakesrc' element");
69   g_object_set (input, "num-buffers", 256, "sizetype", 3, NULL);
70
71   output = gst_element_factory_make ("fakesink", NULL);
72   fail_unless (output != NULL, "failed to create 'fakesink' element");
73
74   setup_queue2 (pipe, input, output);
75
76   gst_element_set_state (pipe, GST_STATE_PLAYING);
77
78   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe),
79       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
80
81   fail_if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR,
82       "Expected EOS message, got ERROR message");
83   gst_message_unref (msg);
84
85   GST_LOG ("Got EOS, cleaning up");
86
87   gst_element_set_state (pipe, GST_STATE_NULL);
88   gst_object_unref (pipe);
89 }
90
91 GST_END_TEST;
92
93 GST_START_TEST (test_simple_pipeline_ringbuffer)
94 {
95   GstElement *pipe, *queue2, *input, *output;
96   GstMessage *msg;
97
98   pipe = gst_pipeline_new ("pipeline");
99
100   input = gst_element_factory_make ("fakesrc", NULL);
101   fail_unless (input != NULL, "failed to create 'fakesrc' element");
102   g_object_set (input, "num-buffers", 256, "sizetype", 3, NULL);
103
104   output = gst_element_factory_make ("fakesink", NULL);
105   fail_unless (output != NULL, "failed to create 'fakesink' element");
106
107   queue2 = setup_queue2 (pipe, input, output);
108   g_object_set (queue2, "ring-buffer-max-size", (guint64) 1024 * 50, NULL);
109
110   gst_element_set_state (pipe, GST_STATE_PLAYING);
111
112   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe),
113       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
114
115   fail_if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR,
116       "Expected EOS message, got ERROR message");
117   gst_message_unref (msg);
118
119   GST_LOG ("Got EOS, cleaning up");
120
121   gst_element_set_state (pipe, GST_STATE_NULL);
122   gst_object_unref (pipe);
123 }
124
125 GST_END_TEST;
126
127 static void
128 do_test_simple_shutdown_while_running (guint64 ring_buffer_max_size)
129 {
130   GstElement *pipe, *q2;
131   GstElement *input;
132   GstElement *output;
133   GstMessage *msg;
134
135   pipe = gst_pipeline_new ("pipeline");
136
137   input = gst_element_factory_make ("fakesrc", NULL);
138   fail_unless (input != NULL, "failed to create 'fakesrc' element");
139   g_object_set (input, "format", GST_FORMAT_TIME, "sizetype", 2,
140       "sizemax", 10, NULL);
141
142   output = gst_element_factory_make ("fakesink", NULL);
143   fail_unless (output != NULL, "failed to create 'fakesink' element");
144
145   q2 = setup_queue2 (pipe, input, output);
146
147   if (ring_buffer_max_size > 0) {
148     g_object_set (q2, "ring-buffer-max-size", ring_buffer_max_size,
149         "temp-template", NULL, NULL);
150   }
151
152   gst_element_set_state (pipe, GST_STATE_PAUSED);
153
154   /* wait until pipeline is up and running */
155   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe),
156       GST_MESSAGE_ERROR | GST_MESSAGE_ASYNC_DONE, -1);
157   fail_if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR, "Got ERROR message");
158   gst_message_unref (msg);
159
160   GST_LOG ("pipeline is running now");
161   gst_element_set_state (pipe, GST_STATE_PLAYING);
162   g_usleep (G_USEC_PER_SEC / 20);
163
164   /* now shut down only the sink, so the queue gets a wrong-state flow return */
165   gst_element_set_state (output, GST_STATE_NULL);
166   GST_LOG ("Cleaning up");
167
168   gst_element_set_state (pipe, GST_STATE_NULL);
169   gst_object_unref (pipe);
170 }
171
172 GST_START_TEST (test_simple_shutdown_while_running)
173 {
174   int i;
175
176   /* run a couple of iterations, gives higher chance of different code paths
177    * being executed at time the flush is detected (esp. useful to make sure
178    * things are cleaned up properly when running under valgrind) */
179   for (i = 0; i < 10; ++i) {
180     do_test_simple_shutdown_while_running (0);
181   }
182 }
183
184 GST_END_TEST;
185
186 GST_START_TEST (test_simple_shutdown_while_running_ringbuffer)
187 {
188   int i;
189
190   /* run a couple of iterations, gives higher chance of different code paths
191    * being executed at time the flush is detected (esp. useful to make sure
192    * things are cleaned up properly when running under valgrind) */
193   for (i = 0; i < 10; ++i) {
194     do_test_simple_shutdown_while_running (1024 * 1024);
195   }
196 }
197
198 GST_END_TEST;
199
200 GST_START_TEST (test_simple_create_destroy)
201 {
202   GstElement *queue2;
203
204   queue2 = gst_element_factory_make ("queue2", NULL);
205   gst_object_unref (queue2);
206 }
207
208 GST_END_TEST;
209
210 static Suite *
211 queue2_suite (void)
212 {
213   Suite *s = suite_create ("queue2");
214   TCase *tc_chain = tcase_create ("general");
215
216   suite_add_tcase (s, tc_chain);
217   tcase_add_test (tc_chain, test_simple_create_destroy);
218   tcase_add_test (tc_chain, test_simple_pipeline);
219   tcase_add_test (tc_chain, test_simple_pipeline_ringbuffer);
220   tcase_add_test (tc_chain, test_simple_shutdown_while_running);
221   tcase_add_test (tc_chain, test_simple_shutdown_while_running_ringbuffer);
222   return s;
223 }
224
225 GST_CHECK_MAIN (queue2)