1 /* GStreamer unit tests for decodebin
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
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.
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.
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.
26 #include <gst/check/gstcheck.h>
27 #include <gst/base/gstbaseparse.h>
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 ";
47 src_need_data_cb (GstElement * src, guint size, gpointer data)
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,
58 GST_BUFFER_OFFSET (buf) = 0;
60 g_signal_emit_by_name (src, "push-buffer", buf, &ret);
62 fail_unless (ret == GST_FLOW_OK);
66 decodebin_pad_added_cb (GstElement * decodebin, GstPad * pad, gboolean * p_flag)
68 /* we should not be reached */
69 fail_unless (decodebin == NULL, "pad-added should not be emitted");
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)
76 GstElement *pipe, *src, *decodebin;
79 pipe = gst_pipeline_new (NULL);
80 fail_unless (pipe != NULL, "failed to create pipeline");
82 src = gst_element_factory_make ("appsrc", "src");
83 fail_unless (src != NULL, "Failed to create appsrc element");
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);
89 decodebin = gst_element_factory_make ("decodebin", "decodebin");
90 fail_unless (decodebin != NULL, "Failed to create decodebin element");
92 g_signal_connect (decodebin, "pad-added",
93 G_CALLBACK (decodebin_pad_added_cb), NULL);
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");
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);
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);
111 gst_element_set_state (pipe, GST_STATE_NULL);
112 gst_object_unref (pipe);
118 pad_added_plug_fakesink_cb (GstElement * decodebin, GstPad * srcpad,
119 GstElement * pipeline)
124 GST_LOG ("Linking fakesink");
126 sink = gst_element_factory_make ("fakesink", "sink");
127 fail_unless (sink != NULL, "Failed to create fakesink element");
129 gst_bin_add (GST_BIN (pipeline), sink);
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);
135 gst_element_set_state (sink, GST_STATE_PLAYING);
138 GST_START_TEST (test_reuse_without_decoders)
140 GstElement *pipe, *src, *decodebin, *sink;
142 pipe = gst_pipeline_new (NULL);
143 fail_unless (pipe != NULL, "failed to create pipeline");
145 src = gst_element_factory_make ("audiotestsrc", "src");
146 fail_unless (src != NULL, "Failed to create audiotestsrc element");
148 decodebin = gst_element_factory_make ("decodebin", "decodebin");
149 fail_unless (decodebin != NULL, "Failed to create decodebin element");
151 g_signal_connect (decodebin, "pad-added",
152 G_CALLBACK (pad_added_plug_fakesink_cb), pipe);
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");
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);
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);
168 /* there shouldn't be any errors */
169 fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
171 GST_DEBUG ("Resetting pipeline");
174 gst_element_set_state (pipe, GST_STATE_READY);
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);
181 GST_LOG ("second try");
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);
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);
193 /* there shouldn't be any errors */
194 fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
196 gst_element_set_state (pipe, GST_STATE_NULL);
197 gst_object_unref (pipe);
202 /* Fake mp3 parser for test */
203 typedef GstBaseParse TestMpegAudioParse;
204 typedef GstBaseParseClass TestMpegAudioParseClass;
206 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
209 GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, layer=[1,3], parsed=(b)true")
212 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
215 GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, parsed=(bool) { false, true }")
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);
226 G_DEFINE_TYPE (TestMpegAudioParse, test_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
229 test_mpeg_audio_parse_class_init (TestMpegAudioParseClass * klass)
231 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
232 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
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));
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>");
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;
249 static gint num_parse_instances = 0;
252 test_mpeg_audio_parse_init (TestMpegAudioParse * mp3parse)
254 /* catch decodebin plugging parsers in a loop early */
255 fail_unless (++num_parse_instances < 10);
259 test_mpeg_audio_parse_start (GstBaseParse * parse)
261 gst_base_parse_set_min_frame_size (parse, 6);
266 test_mpeg_audio_parse_stop (GstBaseParse * parse)
272 test_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
273 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
277 gst_buffer_extract (frame->buffer, 0, data, 2);
279 if ((GST_READ_UINT16_BE (data) & 0xffe0) == 0xffe0) {
280 /* this framesize is hard-coded for ../test.mp3 */
290 test_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
291 GstBaseParseFrame * frame)
293 if (GST_BUFFER_OFFSET (frame->buffer) == 0) {
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);
306 plugin_init (GstPlugin * plugin)
308 return gst_element_register (plugin, "testmpegaudioparse", GST_RANK_NONE,
309 test_mpeg_audio_parse_get_type ());
312 GST_START_TEST (test_mp3_parser_loop)
314 GstStateChangeReturn sret;
315 GstPluginFeature *feature;
317 GstElement *pipe, *src, *dec;
320 num_parse_instances = 0;
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);
326 feature = gst_default_registry_find_feature ("testmpegaudioparse",
327 GST_TYPE_ELEMENT_FACTORY);
329 gst_plugin_feature_set_rank (feature, GST_RANK_PRIMARY + 100);
331 pipe = gst_pipeline_new (NULL);
333 src = gst_element_factory_make ("filesrc", NULL);
334 fail_unless (src != NULL);
336 path = g_build_filename (GST_TEST_FILES_PATH, "test.mp3", NULL);
337 g_object_set (src, "location", path, NULL);
340 dec = gst_element_factory_make ("decodebin", NULL);
341 fail_unless (dec != NULL);
343 gst_bin_add_many (GST_BIN (pipe), src, dec, NULL);
344 gst_element_link_many (src, dec, NULL);
346 sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
347 fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
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);
354 gst_element_set_state (pipe, GST_STATE_NULL);
355 gst_object_unref (pipe);
357 /* make sure out parser got plugged at all though */
358 fail_unless_equals_int (num_parse_instances, 1);
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);
368 decodebin_suite (void)
370 Suite *s = suite_create ("decodebin");
371 TCase *tc_chain = tcase_create ("general");
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);
381 GST_CHECK_MAIN (decodebin);