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_append_memory (buf,
54 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
55 (gpointer) dummytext, sizeof (dummytext), 0,
56 sizeof (dummytext), NULL, NULL));
58 GST_BUFFER_OFFSET (buf) = 0;
60 g_signal_emit_by_name (src, "push-buffer", buf, &ret);
61 gst_buffer_unref (buf);
63 fail_unless (ret == GST_FLOW_OK);
67 decodebin_pad_added_cb (GstElement * decodebin, GstPad * pad, gboolean * p_flag)
69 /* we should not be reached */
70 fail_unless (decodebin == NULL, "pad-added should not be emitted");
73 /* make sure that decodebin errors out instead of creating a new decoded pad
74 * if the entire stream is a plain text file */
75 GST_START_TEST (test_text_plain_streams)
77 GstElement *pipe, *src, *decodebin;
80 pipe = gst_pipeline_new (NULL);
81 fail_unless (pipe != NULL, "failed to create pipeline");
83 src = gst_element_factory_make ("appsrc", "src");
84 fail_unless (src != NULL, "Failed to create appsrc element");
86 g_object_set (src, "emit-signals", TRUE, NULL);
87 g_object_set (src, "num-buffers", 1, NULL);
88 g_signal_connect (src, "need-data", G_CALLBACK (src_need_data_cb), NULL);
90 decodebin = gst_element_factory_make ("decodebin", "decodebin");
91 fail_unless (decodebin != NULL, "Failed to create decodebin element");
93 g_signal_connect (decodebin, "pad-added",
94 G_CALLBACK (decodebin_pad_added_cb), NULL);
96 fail_unless (gst_bin_add (GST_BIN (pipe), src));
97 fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
98 fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
100 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
101 GST_STATE_CHANGE_SUCCESS);
102 /* it's push-based, so should be async */
103 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
104 GST_STATE_CHANGE_ASYNC);
106 /* it should error out at some point */
107 msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, -1);
108 fail_unless (msg != NULL);
109 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
110 gst_message_unref (msg);
112 gst_element_set_state (pipe, GST_STATE_NULL);
113 gst_object_unref (pipe);
119 pad_added_plug_fakesink_cb (GstElement * decodebin, GstPad * srcpad,
120 GstElement * pipeline)
125 GST_LOG ("Linking fakesink");
127 sink = gst_element_factory_make ("fakesink", "sink");
128 fail_unless (sink != NULL, "Failed to create fakesink element");
130 gst_bin_add (GST_BIN (pipeline), sink);
132 sinkpad = gst_element_get_static_pad (sink, "sink");
133 fail_unless_equals_int (gst_pad_link (srcpad, sinkpad), GST_PAD_LINK_OK);
134 gst_object_unref (sinkpad);
136 gst_element_set_state (sink, GST_STATE_PLAYING);
139 GST_START_TEST (test_reuse_without_decoders)
141 GstElement *pipe, *src, *decodebin, *sink;
143 pipe = gst_pipeline_new (NULL);
144 fail_unless (pipe != NULL, "failed to create pipeline");
146 src = gst_element_factory_make ("audiotestsrc", "src");
147 fail_unless (src != NULL, "Failed to create audiotestsrc element");
149 decodebin = gst_element_factory_make ("decodebin", "decodebin");
150 fail_unless (decodebin != NULL, "Failed to create decodebin element");
152 g_signal_connect (decodebin, "pad-added",
153 G_CALLBACK (pad_added_plug_fakesink_cb), pipe);
155 fail_unless (gst_bin_add (GST_BIN (pipe), src));
156 fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
157 fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
159 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
160 GST_STATE_CHANGE_SUCCESS);
161 /* it's push-based, so should be async */
162 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
163 GST_STATE_CHANGE_ASYNC);
165 /* wait for state change to complete */
166 fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
167 GST_STATE_CHANGE_SUCCESS);
169 /* there shouldn't be any errors */
170 fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
172 GST_DEBUG ("Resetting pipeline");
175 gst_element_set_state (pipe, GST_STATE_READY);
177 sink = gst_bin_get_by_name (GST_BIN (pipe), "sink");
178 gst_bin_remove (GST_BIN (pipe), sink);
179 gst_element_set_state (sink, GST_STATE_NULL);
180 gst_object_unref (sink);
182 GST_LOG ("second try");
184 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
185 GST_STATE_CHANGE_SUCCESS);
186 /* it's push-based, so should be async */
187 fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
188 GST_STATE_CHANGE_ASYNC);
190 /* wait for state change to complete */
191 fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
192 GST_STATE_CHANGE_SUCCESS);
194 /* there shouldn't be any errors */
195 fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
197 gst_element_set_state (pipe, GST_STATE_NULL);
198 gst_object_unref (pipe);
203 /* Fake mp3 parser for test */
204 typedef GstBaseParse TestMpegAudioParse;
205 typedef GstBaseParseClass TestMpegAudioParseClass;
207 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
210 GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, layer=[1,3], parsed=(b)true")
213 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
216 GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, parsed=(bool) { false, true }")
219 static GType test_mpeg_audio_parse_get_type (void);
220 static gboolean test_mpeg_audio_parse_start (GstBaseParse * parse);
221 static gboolean test_mpeg_audio_parse_stop (GstBaseParse * parse);
222 static GstFlowReturn test_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
223 GstBaseParseFrame * frame, gint * skipsize);
225 G_DEFINE_TYPE (TestMpegAudioParse, test_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
228 test_mpeg_audio_parse_class_init (TestMpegAudioParseClass * klass)
230 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
231 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
233 gst_element_class_add_pad_template (element_class,
234 gst_static_pad_template_get (&sink_template));
235 gst_element_class_add_pad_template (element_class,
236 gst_static_pad_template_get (&src_template));
238 gst_element_class_set_details_simple (element_class, "MPEG1 Audio Parser",
239 "Codec/Parser/Audio", "Pretends to parse mpeg1 audio stream",
240 "Foo Bar <foo@bar.com>");
242 parse_class->start = test_mpeg_audio_parse_start;
243 parse_class->stop = test_mpeg_audio_parse_stop;
244 parse_class->handle_frame = test_mpeg_audio_parse_handle_frame;
247 static gint num_parse_instances = 0;
250 test_mpeg_audio_parse_init (TestMpegAudioParse * mp3parse)
252 /* catch decodebin plugging parsers in a loop early */
253 fail_unless (++num_parse_instances < 10);
257 test_mpeg_audio_parse_start (GstBaseParse * parse)
259 gst_base_parse_set_min_frame_size (parse, 6);
264 test_mpeg_audio_parse_stop (GstBaseParse * parse)
270 test_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
271 GstBaseParseFrame * frame, gint * skipsize)
275 gst_buffer_extract (frame->buffer, 0, data, 2);
277 if ((GST_READ_UINT16_BE (data) & 0xffe0) == 0xffe0) {
278 if (GST_BUFFER_OFFSET (frame->buffer) == 0) {
281 caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
282 "mpegaudioversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3,
283 "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
284 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
285 gst_caps_unref (caps);
288 /* this framesize is hard-coded for ../test.mp3 */
289 return gst_base_parse_finish_frame (parse, frame, 1045);
297 plugin_init (GstPlugin * plugin)
299 return gst_element_register (plugin, "testmpegaudioparse", GST_RANK_NONE,
300 test_mpeg_audio_parse_get_type ());
303 GST_START_TEST (test_mp3_parser_loop)
305 GstStateChangeReturn sret;
306 GstPluginFeature *feature;
308 GstElement *pipe, *src, *dec;
311 num_parse_instances = 0;
313 gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR,
314 "fakemp3parse", "fakemp3parse", plugin_init, VERSION, "LGPL",
315 "gst-plugins-base", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
317 feature = gst_registry_find_feature (gst_registry_get (),
318 "testmpegaudioparse", GST_TYPE_ELEMENT_FACTORY);
320 gst_plugin_feature_set_rank (feature, GST_RANK_PRIMARY + 100);
322 pipe = gst_pipeline_new (NULL);
324 src = gst_element_factory_make ("filesrc", NULL);
325 fail_unless (src != NULL);
327 path = g_build_filename (GST_TEST_FILES_PATH, "test.mp3", NULL);
328 g_object_set (src, "location", path, NULL);
331 dec = gst_element_factory_make ("decodebin", NULL);
332 fail_unless (dec != NULL);
334 gst_bin_add_many (GST_BIN (pipe), src, dec, NULL);
335 gst_element_link_many (src, dec, NULL);
337 sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
338 fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
340 /* wait for unlinked error */
341 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
342 GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR);
343 gst_message_unref (msg);
345 gst_element_set_state (pipe, GST_STATE_NULL);
346 gst_object_unref (pipe);
348 /* make sure out parser got plugged at all though */
349 fail_unless_equals_int (num_parse_instances, 1);
351 /* don't want to interfere with any other of the other tests */
352 gst_plugin_feature_set_rank (feature, GST_RANK_NONE);
353 gst_object_unref (feature);
359 decodebin_suite (void)
361 Suite *s = suite_create ("decodebin");
362 TCase *tc_chain = tcase_create ("general");
364 suite_add_tcase (s, tc_chain);
365 tcase_add_test (tc_chain, test_text_plain_streams);
366 tcase_add_test (tc_chain, test_reuse_without_decoders);
367 tcase_add_test (tc_chain, test_mp3_parser_loop);
372 GST_CHECK_MAIN (decodebin);