ffa6b5953e3e5adf32b27b79386a60f04fb29bcb
[platform/upstream/gstreamer.git] / tests / check / elements / decodebin.c
1 /* GStreamer unit tests for decodebin
2  *
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <gst/check/gstcheck.h>
27 #include <gst/base/gstbaseparse.h>
28 #include <unistd.h>
29
30 static const gchar dummytext[] =
31     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
32     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
33     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
34     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
35     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
36     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
37     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox "
38     "jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
39     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
40     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
41     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
42     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
43     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
44     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox ";
45
46 static void
47 src_need_data_cb (GstElement * src, guint size, gpointer data)
48 {
49   GstBuffer *buf;
50   GstFlowReturn ret;
51
52   buf = gst_buffer_new ();
53   gst_buffer_take_memory (buf, 0,
54       gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
55           (gpointer) dummytext, NULL, sizeof (dummytext), 0,
56           sizeof (dummytext)));
57
58   GST_BUFFER_OFFSET (buf) = 0;
59
60   g_signal_emit_by_name (src, "push-buffer", buf, &ret);
61
62   fail_unless (ret == GST_FLOW_OK);
63 }
64
65 static void
66 decodebin_pad_added_cb (GstElement * decodebin, GstPad * pad, gboolean * p_flag)
67 {
68   /* we should not be reached */
69   fail_unless (decodebin == NULL, "pad-added should not be emitted");
70 }
71
72 /* make sure that decodebin errors out instead of creating a new decoded pad
73  * if the entire stream is a plain text file */
74 GST_START_TEST (test_text_plain_streams)
75 {
76   GstElement *pipe, *src, *decodebin;
77   GstMessage *msg;
78
79   pipe = gst_pipeline_new (NULL);
80   fail_unless (pipe != NULL, "failed to create pipeline");
81
82   src = gst_element_factory_make ("appsrc", "src");
83   fail_unless (src != NULL, "Failed to create appsrc element");
84
85   g_object_set (src, "emit-signals", TRUE, NULL);
86   g_object_set (src, "num-buffers", 1, NULL);
87   g_signal_connect (src, "need-data", G_CALLBACK (src_need_data_cb), NULL);
88
89   decodebin = gst_element_factory_make ("decodebin", "decodebin");
90   fail_unless (decodebin != NULL, "Failed to create decodebin element");
91
92   g_signal_connect (decodebin, "pad-added",
93       G_CALLBACK (decodebin_pad_added_cb), NULL);
94
95   fail_unless (gst_bin_add (GST_BIN (pipe), src));
96   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
97   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
98
99   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
100       GST_STATE_CHANGE_SUCCESS);
101   /* it's push-based, so should be async */
102   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
103       GST_STATE_CHANGE_ASYNC);
104
105   /* it should error out at some point */
106   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, -1);
107   fail_unless (msg != NULL);
108   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
109   gst_message_unref (msg);
110
111   gst_element_set_state (pipe, GST_STATE_NULL);
112   gst_object_unref (pipe);
113 }
114
115 GST_END_TEST;
116
117 static void
118 pad_added_plug_fakesink_cb (GstElement * decodebin, GstPad * srcpad,
119     GstElement * pipeline)
120 {
121   GstElement *sink;
122   GstPad *sinkpad;
123
124   GST_LOG ("Linking fakesink");
125
126   sink = gst_element_factory_make ("fakesink", "sink");
127   fail_unless (sink != NULL, "Failed to create fakesink element");
128
129   gst_bin_add (GST_BIN (pipeline), sink);
130
131   sinkpad = gst_element_get_static_pad (sink, "sink");
132   fail_unless_equals_int (gst_pad_link (srcpad, sinkpad), GST_PAD_LINK_OK);
133   gst_object_unref (sinkpad);
134
135   gst_element_set_state (sink, GST_STATE_PLAYING);
136 }
137
138 GST_START_TEST (test_reuse_without_decoders)
139 {
140   GstElement *pipe, *src, *decodebin, *sink;
141
142   pipe = gst_pipeline_new (NULL);
143   fail_unless (pipe != NULL, "failed to create pipeline");
144
145   src = gst_element_factory_make ("audiotestsrc", "src");
146   fail_unless (src != NULL, "Failed to create audiotestsrc element");
147
148   decodebin = gst_element_factory_make ("decodebin", "decodebin");
149   fail_unless (decodebin != NULL, "Failed to create decodebin element");
150
151   g_signal_connect (decodebin, "pad-added",
152       G_CALLBACK (pad_added_plug_fakesink_cb), pipe);
153
154   fail_unless (gst_bin_add (GST_BIN (pipe), src));
155   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
156   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
157
158   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
159       GST_STATE_CHANGE_SUCCESS);
160   /* it's push-based, so should be async */
161   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
162       GST_STATE_CHANGE_ASYNC);
163
164   /* wait for state change to complete */
165   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
166       GST_STATE_CHANGE_SUCCESS);
167
168   /* there shouldn't be any errors */
169   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
170
171   GST_DEBUG ("Resetting pipeline");
172
173   /* reset */
174   gst_element_set_state (pipe, GST_STATE_READY);
175
176   sink = gst_bin_get_by_name (GST_BIN (pipe), "sink");
177   gst_bin_remove (GST_BIN (pipe), sink);
178   gst_element_set_state (sink, GST_STATE_NULL);
179   gst_object_unref (sink);
180
181   GST_LOG ("second try");
182
183   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
184       GST_STATE_CHANGE_SUCCESS);
185   /* it's push-based, so should be async */
186   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
187       GST_STATE_CHANGE_ASYNC);
188
189   /* wait for state change to complete */
190   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
191       GST_STATE_CHANGE_SUCCESS);
192
193   /* there shouldn't be any errors */
194   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
195
196   gst_element_set_state (pipe, GST_STATE_NULL);
197   gst_object_unref (pipe);
198 }
199
200 GST_END_TEST;
201
202 /* Fake mp3 parser for test */
203 typedef GstBaseParse TestMpegAudioParse;
204 typedef GstBaseParseClass TestMpegAudioParseClass;
205
206 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
207     GST_PAD_SRC,
208     GST_PAD_ALWAYS,
209     GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, layer=[1,3], parsed=(b)true")
210     );
211
212 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
213     GST_PAD_SINK,
214     GST_PAD_ALWAYS,
215     GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, parsed=(bool) { false, true }")
216     );
217
218 static GType test_mpeg_audio_parse_get_type (void);
219 static gboolean test_mpeg_audio_parse_start (GstBaseParse * parse);
220 static gboolean test_mpeg_audio_parse_stop (GstBaseParse * parse);
221 static gboolean test_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
222     GstBaseParseFrame * frame, guint * size, gint * skipsize);
223 static GstFlowReturn test_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
224     GstBaseParseFrame * frame);
225
226 G_DEFINE_TYPE (TestMpegAudioParse, test_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
227
228 static void
229 test_mpeg_audio_parse_class_init (TestMpegAudioParseClass * klass)
230 {
231   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
232   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
233
234   gst_element_class_add_pad_template (element_class,
235       gst_static_pad_template_get (&sink_template));
236   gst_element_class_add_pad_template (element_class,
237       gst_static_pad_template_get (&src_template));
238
239   gst_element_class_set_details_simple (element_class, "MPEG1 Audio Parser",
240       "Codec/Parser/Audio", "Pretends to parse mpeg1 audio stream",
241       "Foo Bar <foo@bar.com>");
242
243   parse_class->start = test_mpeg_audio_parse_start;
244   parse_class->stop = test_mpeg_audio_parse_stop;
245   parse_class->check_valid_frame = test_mpeg_audio_parse_check_valid_frame;
246   parse_class->parse_frame = test_mpeg_audio_parse_parse_frame;
247 }
248
249 static gint num_parse_instances = 0;
250
251 static void
252 test_mpeg_audio_parse_init (TestMpegAudioParse * mp3parse)
253 {
254   /* catch decodebin plugging parsers in a loop early */
255   fail_unless (++num_parse_instances < 10);
256 }
257
258 static gboolean
259 test_mpeg_audio_parse_start (GstBaseParse * parse)
260 {
261   gst_base_parse_set_min_frame_size (parse, 6);
262   return TRUE;
263 }
264
265 static gboolean
266 test_mpeg_audio_parse_stop (GstBaseParse * parse)
267 {
268   return TRUE;
269 }
270
271 static gboolean
272 test_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
273     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
274 {
275   guint8 data[2];
276
277   gst_buffer_extract (frame->buffer, 0, data, 2);
278
279   if ((GST_READ_UINT16_BE (data) & 0xffe0) == 0xffe0) {
280     /* this framesize is hard-coded for ../test.mp3 */
281     *framesize = 1045;
282     return TRUE;
283   } else {
284     *skipsize = 1;
285     return FALSE;
286   }
287 }
288
289 static GstFlowReturn
290 test_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
291     GstBaseParseFrame * frame)
292 {
293   if (GST_BUFFER_OFFSET (frame->buffer) == 0) {
294     GstCaps *caps;
295
296     caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
297         "mpegaudioversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3,
298         "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
299     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
300     gst_caps_unref (caps);
301   }
302   return GST_FLOW_OK;
303 }
304
305 static gboolean
306 plugin_init (GstPlugin * plugin)
307 {
308   return gst_element_register (plugin, "testmpegaudioparse", GST_RANK_NONE,
309       test_mpeg_audio_parse_get_type ());
310 }
311
312 GST_START_TEST (test_mp3_parser_loop)
313 {
314   GstStateChangeReturn sret;
315   GstPluginFeature *feature;
316   GstMessage *msg;
317   GstElement *pipe, *src, *dec;
318   gchar *path;
319
320   num_parse_instances = 0;
321
322   gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR,
323       "fakemp3parse", "fakemp3parse", plugin_init, VERSION, "LGPL",
324       "gst-plugins-base", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
325
326   feature = gst_default_registry_find_feature ("testmpegaudioparse",
327       GST_TYPE_ELEMENT_FACTORY);
328
329   gst_plugin_feature_set_rank (feature, GST_RANK_PRIMARY + 100);
330
331   pipe = gst_pipeline_new (NULL);
332
333   src = gst_element_factory_make ("filesrc", NULL);
334   fail_unless (src != NULL);
335
336   path = g_build_filename (GST_TEST_FILES_PATH, "test.mp3", NULL);
337   g_object_set (src, "location", path, NULL);
338   g_free (path);
339
340   dec = gst_element_factory_make ("decodebin", NULL);
341   fail_unless (dec != NULL);
342
343   gst_bin_add_many (GST_BIN (pipe), src, dec, NULL);
344   gst_element_link_many (src, dec, NULL);
345
346   sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
347   fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
348
349   /* wait for unlinked error */
350   msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
351       GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR);
352   gst_message_unref (msg);
353
354   gst_element_set_state (pipe, GST_STATE_NULL);
355   gst_object_unref (pipe);
356
357   /* make sure out parser got plugged at all though */
358   fail_unless_equals_int (num_parse_instances, 1);
359
360   /* don't want to interfere with any other of the other tests */
361   gst_plugin_feature_set_rank (feature, GST_RANK_NONE);
362   gst_object_unref (feature);
363 }
364
365 GST_END_TEST;
366
367 static Suite *
368 decodebin_suite (void)
369 {
370   Suite *s = suite_create ("decodebin");
371   TCase *tc_chain = tcase_create ("general");
372
373   suite_add_tcase (s, tc_chain);
374   tcase_add_test (tc_chain, test_text_plain_streams);
375   tcase_add_test (tc_chain, test_reuse_without_decoders);
376   tcase_add_test (tc_chain, test_mp3_parser_loop);
377
378   return s;
379 }
380
381 GST_CHECK_MAIN (decodebin);