tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.git] / tests / check / elements / decodebin2.c
1 /* GStreamer unit tests for decodebin2
2  *
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
5  *   Author: Tim-Philipp Müller <tim.muller@collabora.co.uk>, Collabora Ltd.
6  *           Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <gst/check/gstcheck.h>
29 #include <gst/base/gstbaseparse.h>
30 #include <unistd.h>
31
32 static const gchar dummytext[] =
33     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
34     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
35     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
36     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
37     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
38     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
39     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox "
40     "jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
41     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
42     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
43     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
44     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
45     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
46     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox ";
47
48 static void
49 src_handoff_cb (GstElement * src, GstBuffer * buf, GstPad * pad, gpointer data)
50 {
51   GST_BUFFER_DATA (buf) = (guint8 *) dummytext;
52   GST_BUFFER_SIZE (buf) = sizeof (dummytext);
53   GST_BUFFER_OFFSET (buf) = 0;
54   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_READONLY);
55 }
56
57 static void
58 decodebin_new_decoded_pad_cb (GstElement * decodebin, GstPad * pad,
59     gboolean last, gboolean * p_flag)
60 {
61   /* we should not be reached */
62   fail_unless (decodebin == NULL, "new-decoded-pad should not be emitted");
63 }
64
65 /* make sure that decodebin errors out instead of creating a new decoded pad
66  * if the entire stream is a plain text file */
67 GST_START_TEST (test_text_plain_streams)
68 {
69   GstElement *pipe, *src, *decodebin;
70   GstMessage *msg;
71
72   pipe = gst_pipeline_new (NULL);
73   fail_unless (pipe != NULL, "failed to create pipeline");
74
75   src = gst_element_factory_make ("fakesrc", "src");
76   fail_unless (src != NULL, "Failed to create fakesrc element");
77
78   g_object_set (src, "signal-handoffs", TRUE, NULL);
79   g_object_set (src, "num-buffers", 1, NULL);
80   g_object_set (src, "can-activate-pull", FALSE, NULL);
81   g_signal_connect (src, "handoff", G_CALLBACK (src_handoff_cb), NULL);
82
83   decodebin = gst_element_factory_make ("decodebin2", "decodebin");
84   fail_unless (decodebin != NULL, "Failed to create decodebin element");
85
86   g_signal_connect (decodebin, "new-decoded-pad",
87       G_CALLBACK (decodebin_new_decoded_pad_cb), NULL);
88
89   fail_unless (gst_bin_add (GST_BIN (pipe), src));
90   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
91   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
92
93   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
94       GST_STATE_CHANGE_SUCCESS);
95   /* it's push-based, so should be async */
96   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
97       GST_STATE_CHANGE_ASYNC);
98
99   /* it should error out at some point */
100   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, -1);
101   fail_unless (msg != NULL);
102   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
103   gst_message_unref (msg);
104
105   gst_element_set_state (pipe, GST_STATE_NULL);
106   gst_object_unref (pipe);
107 }
108
109 GST_END_TEST;
110
111 static void
112 new_decoded_pad_plug_fakesink_cb (GstElement * decodebin, GstPad * srcpad,
113     gboolean last, GstElement * pipeline)
114 {
115   GstElement *sink;
116   GstPad *sinkpad;
117
118   GST_LOG ("Linking fakesink");
119
120   sink = gst_element_factory_make ("fakesink", "sink");
121   fail_unless (sink != NULL, "Failed to create fakesink element");
122
123   gst_bin_add (GST_BIN (pipeline), sink);
124
125   sinkpad = gst_element_get_static_pad (sink, "sink");
126   fail_unless_equals_int (gst_pad_link (srcpad, sinkpad), GST_PAD_LINK_OK);
127   gst_object_unref (sinkpad);
128
129   gst_element_set_state (sink, GST_STATE_PLAYING);
130 }
131
132 GST_START_TEST (test_reuse_without_decoders)
133 {
134   GstElement *pipe, *src, *decodebin, *sink;
135
136   pipe = gst_pipeline_new (NULL);
137   fail_unless (pipe != NULL, "failed to create pipeline");
138
139   src = gst_element_factory_make ("audiotestsrc", "src");
140   fail_unless (src != NULL, "Failed to create audiotestsrc element");
141
142   decodebin = gst_element_factory_make ("decodebin2", "decodebin");
143   fail_unless (decodebin != NULL, "Failed to create decodebin element");
144
145   g_signal_connect (decodebin, "new-decoded-pad",
146       G_CALLBACK (new_decoded_pad_plug_fakesink_cb), pipe);
147
148   fail_unless (gst_bin_add (GST_BIN (pipe), src));
149   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
150   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
151
152   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
153       GST_STATE_CHANGE_SUCCESS);
154   /* it's push-based, so should be async */
155   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
156       GST_STATE_CHANGE_ASYNC);
157
158   /* wait for state change to complete */
159   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
160       GST_STATE_CHANGE_SUCCESS);
161
162   /* there shouldn't be any errors */
163   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
164
165   /* reset */
166   gst_element_set_state (pipe, GST_STATE_READY);
167
168   sink = gst_bin_get_by_name (GST_BIN (pipe), "sink");
169   gst_bin_remove (GST_BIN (pipe), sink);
170   gst_element_set_state (sink, GST_STATE_NULL);
171   gst_object_unref (sink);
172
173   GST_LOG ("second try");
174
175   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
176       GST_STATE_CHANGE_SUCCESS);
177   /* it's push-based, so should be async */
178   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
179       GST_STATE_CHANGE_ASYNC);
180
181   /* wait for state change to complete */
182   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
183       GST_STATE_CHANGE_SUCCESS);
184
185   /* there shouldn't be any errors */
186   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
187
188   gst_element_set_state (pipe, GST_STATE_NULL);
189   gst_object_unref (pipe);
190 }
191
192 GST_END_TEST;
193
194 /* Fake mp3 parser for test */
195 typedef GstBaseParse TestMpegAudioParse;
196 typedef GstBaseParseClass TestMpegAudioParseClass;
197
198 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
199     GST_PAD_SRC,
200     GST_PAD_ALWAYS,
201     GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, layer=[1,3], parsed=(b)true")
202     );
203
204 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
205     GST_PAD_SINK,
206     GST_PAD_ALWAYS,
207     GST_STATIC_CAPS ("audio/mpeg, mpegversion=1, parsed=(bool) { false, true }")
208     );
209
210 static GType test_mpeg_audio_parse_get_type (void);
211 static gboolean test_mpeg_audio_parse_start (GstBaseParse * parse);
212 static gboolean test_mpeg_audio_parse_stop (GstBaseParse * parse);
213 static gboolean test_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
214     GstBaseParseFrame * frame, guint * size, gint * skipsize);
215 static GstFlowReturn test_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
216     GstBaseParseFrame * frame);
217
218 GST_BOILERPLATE (TestMpegAudioParse, test_mpeg_audio_parse, GstBaseParse,
219     GST_TYPE_BASE_PARSE);
220
221 static void
222 test_mpeg_audio_parse_base_init (gpointer klass)
223 {
224   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
225
226   gst_element_class_add_static_pad_template (element_class,
227       &sink_template);
228   gst_element_class_add_static_pad_template (element_class, &src_template);
229
230   gst_element_class_set_details_simple (element_class, "MPEG1 Audio Parser",
231       "Codec/Parser/Audio", "Pretends to parse mpeg1 audio stream",
232       "Foo Bar <foo@bar.com>");
233 }
234
235 static void
236 test_mpeg_audio_parse_class_init (TestMpegAudioParseClass * klass)
237 {
238   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
239
240   parse_class->start = test_mpeg_audio_parse_start;
241   parse_class->stop = test_mpeg_audio_parse_stop;
242   parse_class->check_valid_frame = test_mpeg_audio_parse_check_valid_frame;
243   parse_class->parse_frame = test_mpeg_audio_parse_parse_frame;
244 }
245
246 static gint num_parse_instances = 0;
247
248 static void
249 test_mpeg_audio_parse_init (TestMpegAudioParse * mp3parse,
250     TestMpegAudioParseClass * klass)
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 gboolean
270 test_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
271     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
272 {
273   const guint8 *data = GST_BUFFER_DATA (frame->buffer);
274
275   if ((GST_READ_UINT16_BE (data) & 0xffe0) == 0xffe0) {
276     /* this framesize is hard-coded for ../test.mp3 */
277     *framesize = 1045;
278     return TRUE;
279   } else {
280     *skipsize = 1;
281     return FALSE;
282   }
283 }
284
285 static GstFlowReturn
286 test_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
287     GstBaseParseFrame * frame)
288 {
289   if (GST_BUFFER_OFFSET (frame->buffer) == 0) {
290     GstCaps *caps;
291
292     caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
293         "mpegaudioversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3,
294         "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
295     gst_buffer_set_caps (frame->buffer, caps);
296     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
297     gst_caps_unref (caps);
298   }
299   return GST_FLOW_OK;
300 }
301
302 static gboolean
303 plugin_init (GstPlugin * plugin)
304 {
305   return gst_element_register (plugin, "testmpegaudioparse", GST_RANK_NONE,
306       test_mpeg_audio_parse_get_type ());
307 }
308
309 GST_START_TEST (test_mp3_parser_loop)
310 {
311   GstStateChangeReturn sret;
312   GstPluginFeature *feature;
313   GstMessage *msg;
314   GstElement *pipe, *src, *dec;
315   gchar *path;
316
317   num_parse_instances = 0;
318
319   gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR,
320       "fakemp3parse", "fakemp3parse", plugin_init, VERSION, "LGPL",
321       "gst-plugins-base", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
322
323   feature = gst_default_registry_find_feature ("testmpegaudioparse",
324       GST_TYPE_ELEMENT_FACTORY);
325
326   gst_plugin_feature_set_rank (feature, GST_RANK_PRIMARY + 100);
327
328   pipe = gst_pipeline_new (NULL);
329
330   src = gst_element_factory_make ("filesrc", NULL);
331   fail_unless (src != NULL);
332
333   path = g_build_filename (GST_TEST_FILES_PATH, "test.mp3", NULL);
334   g_object_set (src, "location", path, NULL);
335   g_free (path);
336
337   dec = gst_element_factory_make ("decodebin2", NULL);
338   fail_unless (dec != NULL);
339
340   gst_bin_add_many (GST_BIN (pipe), src, dec, NULL);
341   gst_element_link_many (src, dec, NULL);
342
343   sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
344   fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
345
346   /* wait for unlinked error */
347   msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
348       GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR);
349   gst_message_unref (msg);
350
351   gst_element_set_state (pipe, GST_STATE_NULL);
352   gst_object_unref (pipe);
353
354   /* make sure out parser got plugged at all though */
355   fail_unless_equals_int (num_parse_instances, 1);
356
357   /* don't want to interfere with any other of the other tests */
358   gst_plugin_feature_set_rank (feature, GST_RANK_NONE);
359   gst_object_unref (feature);
360 }
361
362 GST_END_TEST;
363
364 /* Fake parser/decoder for parser_negotiation test */
365 static GType gst_fake_h264_parser_get_type (void);
366 static GType gst_fake_h264_decoder_get_type (void);
367
368 #undef parent_class
369 #define parent_class fake_h264_parser_parent_class
370 typedef struct _GstFakeH264Parser GstFakeH264Parser;
371 typedef GstElementClass GstFakeH264ParserClass;
372
373 struct _GstFakeH264Parser
374 {
375   GstElement parent;
376 };
377
378 GST_BOILERPLATE (GstFakeH264Parser, gst_fake_h264_parser, GstElement,
379     GST_TYPE_ELEMENT);
380
381 static void
382 gst_fake_h264_parser_base_init (gpointer klass)
383 {
384   static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
385       GST_PAD_SINK, GST_PAD_ALWAYS,
386       GST_STATIC_CAPS ("video/x-h264"));
387   static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
388       GST_PAD_SRC, GST_PAD_ALWAYS,
389       GST_STATIC_CAPS ("video/x-h264, "
390           "stream-format=(string) { avc, byte-stream }"));
391   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
392
393   gst_element_class_add_static_pad_template (element_class, &sink_templ);
394   gst_element_class_add_static_pad_template (element_class, &src_templ);
395   gst_element_class_set_details_simple (element_class,
396       "FakeH264Parser", "Codec/Parser/Converter/Video", "yep", "me");
397 }
398
399 static void
400 gst_fake_h264_parser_class_init (GstFakeH264ParserClass * klass)
401 {
402 }
403
404 static gboolean
405 gst_fake_h264_parser_sink_setcaps (GstPad * pad, GstCaps * caps)
406 {
407   GstElement *self = GST_ELEMENT (gst_pad_get_parent (pad));
408   GstPad *otherpad = gst_element_get_static_pad (self, "src");
409   GstCaps *accepted_caps;
410   GstStructure *s;
411   const gchar *stream_format;
412
413   accepted_caps = gst_pad_get_allowed_caps (otherpad);
414   accepted_caps = gst_caps_make_writable (accepted_caps);
415   gst_caps_truncate (accepted_caps);
416
417   s = gst_caps_get_structure (accepted_caps, 0);
418   stream_format = gst_structure_get_string (s, "stream-format");
419   if (!stream_format)
420     gst_structure_set (s, "stream-format", G_TYPE_STRING, "avc", NULL);
421
422   gst_pad_set_caps (otherpad, accepted_caps);
423   gst_caps_unref (accepted_caps);
424
425   gst_object_unref (otherpad);
426   gst_object_unref (self);
427
428   return TRUE;
429 }
430
431 static GstFlowReturn
432 gst_fake_h264_parser_sink_chain (GstPad * pad, GstBuffer * buf)
433 {
434   GstElement *self = GST_ELEMENT (gst_pad_get_parent (pad));
435   GstPad *otherpad = gst_element_get_static_pad (self, "src");
436   GstFlowReturn ret = GST_FLOW_OK;
437
438   buf = gst_buffer_make_metadata_writable (buf);
439   gst_buffer_set_caps (buf, GST_PAD_CAPS (otherpad));
440
441   ret = gst_pad_push (otherpad, buf);
442
443   gst_object_unref (otherpad);
444   gst_object_unref (self);
445
446   return ret;
447 }
448
449 static void
450 gst_fake_h264_parser_init (GstFakeH264Parser * self,
451     GstFakeH264ParserClass * klass)
452 {
453   GstPad *pad;
454
455   pad =
456       gst_pad_new_from_template (gst_element_class_get_pad_template
457       (GST_ELEMENT_GET_CLASS (self), "sink"), "sink");
458   gst_pad_set_setcaps_function (pad, gst_fake_h264_parser_sink_setcaps);
459   gst_pad_set_chain_function (pad, gst_fake_h264_parser_sink_chain);
460   gst_element_add_pad (GST_ELEMENT (self), pad);
461
462   pad =
463       gst_pad_new_from_template (gst_element_class_get_pad_template
464       (GST_ELEMENT_GET_CLASS (self), "src"), "src");
465   gst_element_add_pad (GST_ELEMENT (self), pad);
466 }
467
468 #undef parent_class
469 #define parent_class fake_h264_decoder_parent_class
470 typedef struct _GstFakeH264Decoder GstFakeH264Decoder;
471 typedef GstElementClass GstFakeH264DecoderClass;
472
473 struct _GstFakeH264Decoder
474 {
475   GstElement parent;
476 };
477
478 GST_BOILERPLATE (GstFakeH264Decoder, gst_fake_h264_decoder, GstElement,
479     GST_TYPE_ELEMENT);
480
481 static void
482 gst_fake_h264_decoder_base_init (gpointer klass)
483 {
484   static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
485       GST_PAD_SINK, GST_PAD_ALWAYS,
486       GST_STATIC_CAPS ("video/x-h264, " "stream-format=(string) byte-stream"));
487   static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
488       GST_PAD_SRC, GST_PAD_ALWAYS,
489       GST_STATIC_CAPS ("video/x-raw-yuv"));
490   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
491
492   gst_element_class_add_static_pad_template (element_class, &sink_templ);
493   gst_element_class_add_static_pad_template (element_class, &src_templ);
494   gst_element_class_set_details_simple (element_class,
495       "FakeH264Decoder", "Codec/Decoder/Video", "yep", "me");
496 }
497
498 static void
499 gst_fake_h264_decoder_class_init (GstFakeH264DecoderClass * klass)
500 {
501 }
502
503 static gboolean
504 gst_fake_h264_decoder_sink_setcaps (GstPad * pad, GstCaps * caps)
505 {
506   GstElement *self = GST_ELEMENT (gst_pad_get_parent (pad));
507   GstPad *otherpad = gst_element_get_static_pad (self, "src");
508
509   caps = gst_caps_new_simple ("video/x-raw-yuv", NULL);
510   gst_pad_set_caps (otherpad, caps);
511   gst_caps_unref (caps);
512
513   gst_object_unref (otherpad);
514   gst_object_unref (self);
515
516   return TRUE;
517 }
518
519 static GstFlowReturn
520 gst_fake_h264_decoder_sink_chain (GstPad * pad, GstBuffer * buf)
521 {
522   GstElement *self = GST_ELEMENT (gst_pad_get_parent (pad));
523   GstPad *otherpad = gst_element_get_static_pad (self, "src");
524   GstFlowReturn ret = GST_FLOW_OK;
525
526   buf = gst_buffer_make_metadata_writable (buf);
527   gst_buffer_set_caps (buf, GST_PAD_CAPS (otherpad));
528
529   ret = gst_pad_push (otherpad, buf);
530
531   gst_object_unref (otherpad);
532   gst_object_unref (self);
533
534   return ret;
535 }
536
537 static void
538 gst_fake_h264_decoder_init (GstFakeH264Decoder * self,
539     GstFakeH264DecoderClass * klass)
540 {
541   GstPad *pad;
542
543   pad =
544       gst_pad_new_from_template (gst_element_class_get_pad_template
545       (GST_ELEMENT_GET_CLASS (self), "sink"), "sink");
546   gst_pad_set_setcaps_function (pad, gst_fake_h264_decoder_sink_setcaps);
547   gst_pad_set_chain_function (pad, gst_fake_h264_decoder_sink_chain);
548   gst_element_add_pad (GST_ELEMENT (self), pad);
549
550   pad =
551       gst_pad_new_from_template (gst_element_class_get_pad_template
552       (GST_ELEMENT_GET_CLASS (self), "src"), "src");
553   gst_element_add_pad (GST_ELEMENT (self), pad);
554 }
555
556 static void
557 parser_negotiation_pad_added_cb (GstElement * dec, GstPad * pad,
558     gpointer user_data)
559 {
560   GstBin *pipe = user_data;
561   GstElement *sink;
562   GstPad *sinkpad;
563
564   sink = gst_element_factory_make ("fakesink", NULL);
565   gst_bin_add (pipe, sink);
566   gst_element_sync_state_with_parent (sink);
567   sinkpad = gst_element_get_static_pad (sink, "sink");
568   gst_pad_link (pad, sinkpad);
569   gst_object_unref (sinkpad);
570 }
571
572 GST_START_TEST (test_parser_negotiation)
573 {
574   GstStateChangeReturn sret;
575   GstMessage *msg;
576   GstCaps *caps;
577   GstElement *pipe, *src, *filter, *dec;
578
579   gst_element_register (NULL, "fakeh264parse", GST_RANK_PRIMARY + 101,
580       gst_fake_h264_parser_get_type ());
581   gst_element_register (NULL, "fakeh264dec", GST_RANK_PRIMARY + 100,
582       gst_fake_h264_decoder_get_type ());
583
584   pipe = gst_pipeline_new (NULL);
585
586   src = gst_element_factory_make ("fakesrc", NULL);
587   fail_unless (src != NULL);
588   g_object_set (G_OBJECT (src), "num-buffers", 5, "sizetype", 2, "filltype", 2,
589       "can-activate-pull", FALSE, NULL);
590
591   filter = gst_element_factory_make ("capsfilter", NULL);
592   fail_unless (filter != NULL);
593   caps = gst_caps_from_string ("video/x-h264");
594   g_object_set (G_OBJECT (filter), "caps", caps, NULL);
595   gst_caps_unref (caps);
596
597   dec = gst_element_factory_make ("decodebin2", NULL);
598   fail_unless (dec != NULL);
599
600   g_signal_connect (dec, "pad-added",
601       G_CALLBACK (parser_negotiation_pad_added_cb), pipe);
602
603   gst_bin_add_many (GST_BIN (pipe), src, filter, dec, NULL);
604   gst_element_link_many (src, filter, dec, NULL);
605
606   sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
607   fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
608
609   /* wait for EOS or error */
610   msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
611       GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
612   fail_unless (msg != NULL);
613   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
614   gst_message_unref (msg);
615
616   gst_element_set_state (pipe, GST_STATE_NULL);
617   gst_object_unref (pipe);
618 }
619
620 GST_END_TEST;
621
622 static Suite *
623 decodebin2_suite (void)
624 {
625   Suite *s = suite_create ("decodebin2");
626   TCase *tc_chain = tcase_create ("general");
627
628   suite_add_tcase (s, tc_chain);
629   tcase_add_test (tc_chain, test_text_plain_streams);
630   tcase_add_test (tc_chain, test_reuse_without_decoders);
631   tcase_add_test (tc_chain, test_mp3_parser_loop);
632   tcase_add_test (tc_chain, test_parser_negotiation);
633
634   return s;
635 }
636
637 GST_CHECK_MAIN (decodebin2);