tests: videorate: remove obsolete color-matrix caps field
[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_append_memory (buf,
54       gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
55           (gpointer) dummytext, sizeof (dummytext), 0,
56           sizeof (dummytext), NULL, NULL));
57
58   GST_BUFFER_OFFSET (buf) = 0;
59
60   g_signal_emit_by_name (src, "push-buffer", buf, &ret);
61   gst_buffer_unref (buf);
62
63   fail_unless (ret == GST_FLOW_OK);
64 }
65
66 static void
67 decodebin_pad_added_cb (GstElement * decodebin, GstPad * pad, gboolean * p_flag)
68 {
69   /* we should not be reached */
70   fail_unless (decodebin == NULL, "pad-added should not be emitted");
71 }
72
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)
76 {
77   GstElement *pipe, *src, *decodebin;
78   GstMessage *msg;
79
80   pipe = gst_pipeline_new (NULL);
81   fail_unless (pipe != NULL, "failed to create pipeline");
82
83   src = gst_element_factory_make ("appsrc", "src");
84   fail_unless (src != NULL, "Failed to create appsrc element");
85
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);
89
90   decodebin = gst_element_factory_make ("decodebin", "decodebin");
91   fail_unless (decodebin != NULL, "Failed to create decodebin element");
92
93   g_signal_connect (decodebin, "pad-added",
94       G_CALLBACK (decodebin_pad_added_cb), NULL);
95
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");
99
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);
105
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);
111
112   gst_element_set_state (pipe, GST_STATE_NULL);
113   gst_object_unref (pipe);
114 }
115
116 GST_END_TEST;
117
118 static void
119 pad_added_plug_fakesink_cb (GstElement * decodebin, GstPad * srcpad,
120     GstElement * pipeline)
121 {
122   GstElement *sink;
123   GstPad *sinkpad;
124
125   GST_LOG ("Linking fakesink");
126
127   sink = gst_element_factory_make ("fakesink", "sink");
128   fail_unless (sink != NULL, "Failed to create fakesink element");
129
130   gst_bin_add (GST_BIN (pipeline), sink);
131
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);
135
136   gst_element_set_state (sink, GST_STATE_PLAYING);
137 }
138
139 GST_START_TEST (test_reuse_without_decoders)
140 {
141   GstElement *pipe, *src, *decodebin, *sink;
142
143   pipe = gst_pipeline_new (NULL);
144   fail_unless (pipe != NULL, "failed to create pipeline");
145
146   src = gst_element_factory_make ("audiotestsrc", "src");
147   fail_unless (src != NULL, "Failed to create audiotestsrc element");
148
149   decodebin = gst_element_factory_make ("decodebin", "decodebin");
150   fail_unless (decodebin != NULL, "Failed to create decodebin element");
151
152   g_signal_connect (decodebin, "pad-added",
153       G_CALLBACK (pad_added_plug_fakesink_cb), pipe);
154
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");
158
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);
164
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);
168
169   /* there shouldn't be any errors */
170   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
171
172   GST_DEBUG ("Resetting pipeline");
173
174   /* reset */
175   gst_element_set_state (pipe, GST_STATE_READY);
176
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);
181
182   GST_LOG ("second try");
183
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);
189
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);
193
194   /* there shouldn't be any errors */
195   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
196
197   gst_element_set_state (pipe, GST_STATE_NULL);
198   gst_object_unref (pipe);
199 }
200
201 GST_END_TEST;
202
203 /* Fake mp3 parser for test */
204 typedef GstBaseParse TestMpegAudioParse;
205 typedef GstBaseParseClass TestMpegAudioParseClass;
206
207 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
208     GST_PAD_SRC,
209     GST_PAD_ALWAYS,
210     GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, layer=[1,3], parsed=(b)true")
211     );
212
213 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
214     GST_PAD_SINK,
215     GST_PAD_ALWAYS,
216     GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, parsed=(bool) { false, true }")
217     );
218
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);
224
225 G_DEFINE_TYPE (TestMpegAudioParse, test_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
226
227 static void
228 test_mpeg_audio_parse_class_init (TestMpegAudioParseClass * klass)
229 {
230   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
231   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
232
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));
237
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>");
241
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;
245 }
246
247 static gint num_parse_instances = 0;
248
249 static void
250 test_mpeg_audio_parse_init (TestMpegAudioParse * mp3parse)
251 {
252   /* catch decodebin plugging parsers in a loop early */
253   fail_unless (++num_parse_instances < 10);
254 }
255
256 static gboolean
257 test_mpeg_audio_parse_start (GstBaseParse * parse)
258 {
259   gst_base_parse_set_min_frame_size (parse, 6);
260   return TRUE;
261 }
262
263 static gboolean
264 test_mpeg_audio_parse_stop (GstBaseParse * parse)
265 {
266   return TRUE;
267 }
268
269 static GstFlowReturn
270 test_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
271     GstBaseParseFrame * frame, gint * skipsize)
272 {
273   guint8 data[2];
274
275   gst_buffer_extract (frame->buffer, 0, data, 2);
276
277   if ((GST_READ_UINT16_BE (data) & 0xffe0) == 0xffe0) {
278     if (GST_BUFFER_OFFSET (frame->buffer) == 0) {
279       GstCaps *caps;
280
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);
286     }
287
288     /* this framesize is hard-coded for ../test.mp3 */
289     return gst_base_parse_finish_frame (parse, frame, 1045);
290   } else {
291     *skipsize = 1;
292     return GST_FLOW_OK;
293   }
294 }
295
296 static gboolean
297 plugin_init (GstPlugin * plugin)
298 {
299   return gst_element_register (plugin, "testmpegaudioparse", GST_RANK_NONE,
300       test_mpeg_audio_parse_get_type ());
301 }
302
303 GST_START_TEST (test_mp3_parser_loop)
304 {
305   GstStateChangeReturn sret;
306   GstPluginFeature *feature;
307   GstMessage *msg;
308   GstElement *pipe, *src, *dec;
309   gchar *path;
310
311   num_parse_instances = 0;
312
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);
316
317   feature = gst_registry_find_feature (gst_registry_get (),
318       "testmpegaudioparse", GST_TYPE_ELEMENT_FACTORY);
319
320   gst_plugin_feature_set_rank (feature, GST_RANK_PRIMARY + 100);
321
322   pipe = gst_pipeline_new (NULL);
323
324   src = gst_element_factory_make ("filesrc", NULL);
325   fail_unless (src != NULL);
326
327   path = g_build_filename (GST_TEST_FILES_PATH, "test.mp3", NULL);
328   g_object_set (src, "location", path, NULL);
329   g_free (path);
330
331   dec = gst_element_factory_make ("decodebin", NULL);
332   fail_unless (dec != NULL);
333
334   gst_bin_add_many (GST_BIN (pipe), src, dec, NULL);
335   gst_element_link_many (src, dec, NULL);
336
337   sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
338   fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
339
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);
344
345   gst_element_set_state (pipe, GST_STATE_NULL);
346   gst_object_unref (pipe);
347
348   /* make sure out parser got plugged at all though */
349   fail_unless_equals_int (num_parse_instances, 1);
350
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);
354 }
355
356 GST_END_TEST;
357
358 static Suite *
359 decodebin_suite (void)
360 {
361   Suite *s = suite_create ("decodebin");
362   TCase *tc_chain = tcase_create ("general");
363
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);
368
369   return s;
370 }
371
372 GST_CHECK_MAIN (decodebin);