• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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 
29 static const gchar dummytext[] =
30     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
31     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
32     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
33     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
34     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
35     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
36     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox "
37     "jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
38     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
39     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
40     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
41     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
42     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
43     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox ";
44 
45 static void
src_need_data_cb(GstElement * src,guint size,gpointer data)46 src_need_data_cb (GstElement * src, guint size, gpointer data)
47 {
48   GstBuffer *buf;
49   GstFlowReturn ret;
50 
51   buf = gst_buffer_new ();
52   gst_buffer_append_memory (buf,
53       gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
54           (gpointer) dummytext, sizeof (dummytext), 0,
55           sizeof (dummytext), NULL, NULL));
56 
57   GST_BUFFER_OFFSET (buf) = 0;
58 
59   g_signal_emit_by_name (src, "push-buffer", buf, &ret);
60   gst_buffer_unref (buf);
61 
62   fail_unless (ret == GST_FLOW_OK);
63 }
64 
65 static void
decodebin_pad_added_cb(GstElement * decodebin,GstPad * pad,gboolean * p_flag)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 */
GST_START_TEST(test_text_plain_streams)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
pad_added_plug_fakesink_cb(GstElement * decodebin,GstPad * srcpad,GstElement * pipeline)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 
GST_START_TEST(test_reuse_without_decoders)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")
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 GstFlowReturn test_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
222     GstBaseParseFrame * frame, gint * skipsize);
223 
224 G_DEFINE_TYPE (TestMpegAudioParse, test_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
225 
226 static void
test_mpeg_audio_parse_class_init(TestMpegAudioParseClass * klass)227 test_mpeg_audio_parse_class_init (TestMpegAudioParseClass * klass)
228 {
229   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
230   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
231 
232   gst_element_class_add_static_pad_template (element_class, &sink_template);
233   gst_element_class_add_static_pad_template (element_class, &src_template);
234 
235   gst_element_class_set_metadata (element_class, "MPEG1 Audio Parser",
236       "Codec/Parser/Audio", "Pretends to parse mpeg1 audio stream",
237       "Foo Bar <foo@bar.com>");
238 
239   parse_class->start = test_mpeg_audio_parse_start;
240   parse_class->stop = test_mpeg_audio_parse_stop;
241   parse_class->handle_frame = test_mpeg_audio_parse_handle_frame;
242 }
243 
244 static gint num_parse_instances = 0;
245 
246 static void
test_mpeg_audio_parse_init(TestMpegAudioParse * mp3parse)247 test_mpeg_audio_parse_init (TestMpegAudioParse * mp3parse)
248 {
249   /* catch decodebin plugging parsers in a loop early */
250   fail_unless (++num_parse_instances < 10);
251 }
252 
253 static gboolean
test_mpeg_audio_parse_start(GstBaseParse * parse)254 test_mpeg_audio_parse_start (GstBaseParse * parse)
255 {
256   gst_base_parse_set_min_frame_size (parse, 6);
257   return TRUE;
258 }
259 
260 static gboolean
test_mpeg_audio_parse_stop(GstBaseParse * parse)261 test_mpeg_audio_parse_stop (GstBaseParse * parse)
262 {
263   return TRUE;
264 }
265 
266 static GstFlowReturn
test_mpeg_audio_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)267 test_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
268     GstBaseParseFrame * frame, gint * skipsize)
269 {
270   guint8 data[2];
271 
272   gst_buffer_extract (frame->buffer, 0, data, 2);
273 
274   if ((GST_READ_UINT16_BE (data) & 0xffe0) == 0xffe0) {
275     if (GST_BUFFER_OFFSET (frame->buffer) == 0) {
276       GstCaps *caps;
277 
278       caps = gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1,
279           "mpegaudioversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3,
280           "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 2, NULL);
281       gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
282       gst_caps_unref (caps);
283     }
284 
285     /* this framesize is hard-coded for ../test.mp3 */
286     return gst_base_parse_finish_frame (parse, frame, 1045);
287   } else {
288     *skipsize = 1;
289     return GST_FLOW_OK;
290   }
291 }
292 
293 static gboolean
plugin_init(GstPlugin * plugin)294 plugin_init (GstPlugin * plugin)
295 {
296   return gst_element_register (plugin, "testmpegaudioparse", GST_RANK_NONE,
297       test_mpeg_audio_parse_get_type ());
298 }
299 
GST_START_TEST(test_mp3_parser_loop)300 GST_START_TEST (test_mp3_parser_loop)
301 {
302   GstStateChangeReturn sret;
303   GstPluginFeature *feature;
304   GstMessage *msg;
305   GstElement *pipe, *src, *dec;
306   gchar *path;
307 
308   num_parse_instances = 0;
309 
310   gst_plugin_register_static (GST_VERSION_MAJOR, GST_VERSION_MINOR,
311       "fakemp3parse", "fakemp3parse", plugin_init, VERSION, "LGPL",
312       "gst-plugins-base", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
313 
314   feature = gst_registry_find_feature (gst_registry_get (),
315       "testmpegaudioparse", GST_TYPE_ELEMENT_FACTORY);
316 
317   gst_plugin_feature_set_rank (feature, GST_RANK_PRIMARY + 100);
318 
319   pipe = gst_pipeline_new (NULL);
320 
321   src = gst_element_factory_make ("filesrc", NULL);
322   fail_unless (src != NULL);
323 
324   path = g_build_filename (GST_TEST_FILES_PATH, "test.mp3", NULL);
325   g_object_set (src, "location", path, NULL);
326   g_free (path);
327 
328   dec = gst_element_factory_make ("decodebin", NULL);
329   fail_unless (dec != NULL);
330 
331   gst_bin_add_many (GST_BIN (pipe), src, dec, NULL);
332   gst_element_link_many (src, dec, NULL);
333 
334   sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
335   fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
336 
337   /* wait for unlinked error */
338   msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
339       GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR);
340   gst_message_unref (msg);
341 
342   gst_element_set_state (pipe, GST_STATE_NULL);
343   gst_object_unref (pipe);
344 
345   /* make sure out parser got plugged at all though */
346   fail_unless_equals_int (num_parse_instances, 1);
347 
348   /* don't want to interfere with any other of the other tests */
349   gst_plugin_feature_set_rank (feature, GST_RANK_NONE);
350   gst_object_unref (feature);
351 }
352 
353 GST_END_TEST;
354 
355 /* Fake parser/decoder for parser_negotiation test */
356 static GType gst_fake_h264_parser_get_type (void);
357 static GType gst_fake_h264_decoder_get_type (void);
358 
359 #undef parent_class
360 #define parent_class fake_h264_parser_parent_class
361 typedef struct _GstFakeH264Parser GstFakeH264Parser;
362 typedef GstElementClass GstFakeH264ParserClass;
363 
364 struct _GstFakeH264Parser
365 {
366   GstElement parent;
367 };
368 
369 G_DEFINE_TYPE (GstFakeH264Parser, gst_fake_h264_parser, GST_TYPE_ELEMENT);
370 
371 static void
gst_fake_h264_parser_class_init(GstFakeH264ParserClass * klass)372 gst_fake_h264_parser_class_init (GstFakeH264ParserClass * klass)
373 {
374   static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
375       GST_PAD_SINK, GST_PAD_ALWAYS,
376       GST_STATIC_CAPS ("video/x-h264"));
377   static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
378       GST_PAD_SRC, GST_PAD_ALWAYS,
379       GST_STATIC_CAPS ("video/x-h264, "
380           "stream-format=(string) { avc, byte-stream }"));
381   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
382 
383   gst_element_class_add_static_pad_template (element_class, &sink_templ);
384   gst_element_class_add_static_pad_template (element_class, &src_templ);
385   gst_element_class_set_metadata (element_class,
386       "FakeH264Parser", "Codec/Parser/Converter/Video", "yep", "me");
387 }
388 
389 static gboolean
gst_fake_h264_parser_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)390 gst_fake_h264_parser_sink_event (GstPad * pad, GstObject * parent,
391     GstEvent * event)
392 {
393   GstElement *self = GST_ELEMENT (parent);
394   GstPad *otherpad = gst_element_get_static_pad (self, "src");
395   GstCaps *accepted_caps;
396   GstStructure *s;
397   const gchar *stream_format;
398   gboolean ret = TRUE;
399 
400   switch (GST_EVENT_TYPE (event)) {
401     case GST_EVENT_CAPS:
402       accepted_caps = gst_pad_get_allowed_caps (otherpad);
403       accepted_caps = gst_caps_truncate (accepted_caps);
404 
405       s = gst_caps_get_structure (accepted_caps, 0);
406       stream_format = gst_structure_get_string (s, "stream-format");
407       if (!stream_format)
408         gst_structure_set (s, "stream-format", G_TYPE_STRING, "avc", NULL);
409 
410       gst_pad_set_caps (otherpad, accepted_caps);
411       gst_caps_unref (accepted_caps);
412       gst_event_unref (event);
413       event = NULL;
414       break;
415     default:
416       break;
417   }
418 
419   if (event)
420     ret = gst_pad_push_event (otherpad, event);
421   gst_object_unref (otherpad);
422 
423   return ret;
424 }
425 
426 static GstFlowReturn
gst_fake_h264_parser_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)427 gst_fake_h264_parser_sink_chain (GstPad * pad, GstObject * parent,
428     GstBuffer * buf)
429 {
430   GstElement *self = GST_ELEMENT (parent);
431   GstPad *otherpad = gst_element_get_static_pad (self, "src");
432   GstFlowReturn ret = GST_FLOW_OK;
433 
434   buf = gst_buffer_make_writable (buf);
435 
436   ret = gst_pad_push (otherpad, buf);
437 
438   gst_object_unref (otherpad);
439 
440   return ret;
441 }
442 
443 static void
gst_fake_h264_parser_init(GstFakeH264Parser * self)444 gst_fake_h264_parser_init (GstFakeH264Parser * self)
445 {
446   GstPad *pad;
447 
448   pad =
449       gst_pad_new_from_template (gst_element_class_get_pad_template
450       (GST_ELEMENT_GET_CLASS (self), "sink"), "sink");
451   gst_pad_set_event_function (pad, gst_fake_h264_parser_sink_event);
452   gst_pad_set_chain_function (pad, gst_fake_h264_parser_sink_chain);
453   gst_element_add_pad (GST_ELEMENT (self), pad);
454 
455   pad =
456       gst_pad_new_from_template (gst_element_class_get_pad_template
457       (GST_ELEMENT_GET_CLASS (self), "src"), "src");
458   gst_element_add_pad (GST_ELEMENT (self), pad);
459 }
460 
461 #undef parent_class
462 #define parent_class fake_h264_decoder_parent_class
463 typedef struct _GstFakeH264Decoder GstFakeH264Decoder;
464 typedef GstElementClass GstFakeH264DecoderClass;
465 
466 struct _GstFakeH264Decoder
467 {
468   GstElement parent;
469 };
470 
471 G_DEFINE_TYPE (GstFakeH264Decoder, gst_fake_h264_decoder, GST_TYPE_ELEMENT);
472 
473 static void
gst_fake_h264_decoder_class_init(GstFakeH264DecoderClass * klass)474 gst_fake_h264_decoder_class_init (GstFakeH264DecoderClass * klass)
475 {
476   static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
477       GST_PAD_SINK, GST_PAD_ALWAYS,
478       GST_STATIC_CAPS ("video/x-h264, " "stream-format=(string) byte-stream"));
479   static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
480       GST_PAD_SRC, GST_PAD_ALWAYS,
481       GST_STATIC_CAPS ("video/x-raw"));
482   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
483 
484   gst_element_class_add_static_pad_template (element_class, &sink_templ);
485   gst_element_class_add_static_pad_template (element_class, &src_templ);
486   gst_element_class_set_metadata (element_class,
487       "FakeH264Decoder", "Codec/Decoder/Video", "yep", "me");
488 }
489 
490 static gboolean
gst_fake_h264_decoder_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)491 gst_fake_h264_decoder_sink_event (GstPad * pad, GstObject * parent,
492     GstEvent * event)
493 {
494   GstElement *self = GST_ELEMENT (parent);
495   GstPad *otherpad = gst_element_get_static_pad (self, "src");
496   GstCaps *caps;
497   gboolean ret = TRUE;
498 
499   switch (GST_EVENT_TYPE (event)) {
500     case GST_EVENT_CAPS:
501       caps = gst_caps_new_empty_simple ("video/x-raw");
502       gst_pad_set_caps (otherpad, caps);
503       gst_caps_unref (caps);
504       gst_event_unref (event);
505       event = NULL;
506       break;
507     default:
508       break;
509   }
510 
511   if (event)
512     ret = gst_pad_push_event (otherpad, event);
513   gst_object_unref (otherpad);
514 
515   return ret;
516 }
517 
518 static GstFlowReturn
gst_fake_h264_decoder_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)519 gst_fake_h264_decoder_sink_chain (GstPad * pad, GstObject * parent,
520     GstBuffer * buf)
521 {
522   GstElement *self = GST_ELEMENT (parent);
523   GstPad *otherpad = gst_element_get_static_pad (self, "src");
524   GstFlowReturn ret = GST_FLOW_OK;
525 
526   buf = gst_buffer_make_writable (buf);
527 
528   ret = gst_pad_push (otherpad, buf);
529 
530   gst_object_unref (otherpad);
531 
532   return ret;
533 }
534 
535 static void
gst_fake_h264_decoder_init(GstFakeH264Decoder * self)536 gst_fake_h264_decoder_init (GstFakeH264Decoder * self)
537 {
538   GstPad *pad;
539 
540   pad =
541       gst_pad_new_from_template (gst_element_class_get_pad_template
542       (GST_ELEMENT_GET_CLASS (self), "sink"), "sink");
543   gst_pad_set_event_function (pad, gst_fake_h264_decoder_sink_event);
544   gst_pad_set_chain_function (pad, gst_fake_h264_decoder_sink_chain);
545   gst_element_add_pad (GST_ELEMENT (self), pad);
546 
547   pad =
548       gst_pad_new_from_template (gst_element_class_get_pad_template
549       (GST_ELEMENT_GET_CLASS (self), "src"), "src");
550   gst_element_add_pad (GST_ELEMENT (self), pad);
551 }
552 
553 static void
parser_negotiation_pad_added_cb(GstElement * dec,GstPad * pad,gpointer user_data)554 parser_negotiation_pad_added_cb (GstElement * dec, GstPad * pad,
555     gpointer user_data)
556 {
557   GstBin *pipe = user_data;
558   GstElement *sink;
559   GstPad *sinkpad;
560 
561   sink = gst_element_factory_make ("fakesink", NULL);
562   gst_bin_add (pipe, sink);
563   gst_element_sync_state_with_parent (sink);
564   sinkpad = gst_element_get_static_pad (sink, "sink");
565   gst_pad_link (pad, sinkpad);
566   gst_object_unref (sinkpad);
567 }
568 
GST_START_TEST(test_parser_negotiation)569 GST_START_TEST (test_parser_negotiation)
570 {
571   GstStateChangeReturn sret;
572   GstMessage *msg;
573   GstCaps *caps;
574   GstElement *pipe, *src, *filter, *dec;
575 
576   gst_element_register (NULL, "fakeh264parse", GST_RANK_PRIMARY + 101,
577       gst_fake_h264_parser_get_type ());
578   gst_element_register (NULL, "fakeh264dec", GST_RANK_PRIMARY + 100,
579       gst_fake_h264_decoder_get_type ());
580 
581   pipe = gst_pipeline_new (NULL);
582 
583   src = gst_element_factory_make ("fakesrc", NULL);
584   fail_unless (src != NULL);
585   g_object_set (G_OBJECT (src), "num-buffers", 5, "sizetype", 2, "filltype", 2,
586       "can-activate-pull", FALSE, NULL);
587 
588   filter = gst_element_factory_make ("capsfilter", NULL);
589   fail_unless (filter != NULL);
590   caps = gst_caps_from_string ("video/x-h264");
591   g_object_set (G_OBJECT (filter), "caps", caps, NULL);
592   gst_caps_unref (caps);
593 
594   dec = gst_element_factory_make ("decodebin", NULL);
595   fail_unless (dec != NULL);
596 
597   g_signal_connect (dec, "pad-added",
598       G_CALLBACK (parser_negotiation_pad_added_cb), pipe);
599 
600   gst_bin_add_many (GST_BIN (pipe), src, filter, dec, NULL);
601   gst_element_link_many (src, filter, dec, NULL);
602 
603   sret = gst_element_set_state (pipe, GST_STATE_PLAYING);
604   fail_unless_equals_int (sret, GST_STATE_CHANGE_ASYNC);
605 
606   /* wait for EOS or error */
607   msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipe),
608       GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
609   fail_unless (msg != NULL);
610   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
611   gst_message_unref (msg);
612 
613   gst_element_set_state (pipe, GST_STATE_NULL);
614   gst_object_unref (pipe);
615 }
616 
617 GST_END_TEST;
618 
GST_START_TEST(test_buffering_aggregation)619 GST_START_TEST (test_buffering_aggregation)
620 {
621   GstElement *pipe, *decodebin;
622   GstMessage *msg;
623   GstElement *mq0, *mq1, *mq2;
624   gint perc;
625 
626   pipe = gst_pipeline_new (NULL);
627   fail_unless (pipe != NULL, "failed to create pipeline");
628 
629   decodebin = gst_element_factory_make ("decodebin", "decodebin");
630   fail_unless (decodebin != NULL, "Failed to create decodebin element");
631 
632   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
633 
634   /* to simulate the buffering scenarios we stuff 2 multiqueues inside
635    * decodebin. This is hacky, but sould make decodebin handle its buffering
636    * messages all the same */
637   mq0 = gst_element_factory_make ("multiqueue", NULL);
638   mq1 = gst_element_factory_make ("multiqueue", NULL);
639   mq2 = gst_element_factory_make ("multiqueue", NULL);
640 
641   fail_unless (gst_bin_add (GST_BIN (decodebin), mq0));
642   fail_unless (gst_bin_add (GST_BIN (decodebin), mq1));
643   fail_unless (gst_bin_add (GST_BIN (decodebin), mq2));
644 
645   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
646       GST_STATE_CHANGE_SUCCESS);
647   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
648       GST_STATE_CHANGE_ASYNC);
649 
650   /* currently we shoud have no buffering messages */
651   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_BUFFERING, 0);
652   fail_unless (msg == NULL);
653 
654   /* only a single element buffering, the buffering percent should be the
655    * same as it */
656   gst_element_post_message (mq0, gst_message_new_buffering (GST_OBJECT (mq0),
657           50));
658   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_BUFFERING, 0);
659   fail_unless (msg != NULL);
660   fail_unless (GST_MESSAGE_SRC (msg) == (GstObject *) mq0);
661   gst_message_parse_buffering (msg, &perc);
662   fail_unless (perc == 50);
663   gst_message_unref (msg);
664 
665   /* two elements buffering, the buffering percent should be the
666    * lowest one */
667   gst_element_post_message (mq1, gst_message_new_buffering (GST_OBJECT (mq1),
668           20));
669   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_BUFFERING, 0);
670   fail_unless (msg != NULL);
671   fail_unless (GST_MESSAGE_SRC (msg) == (GstObject *) mq1);
672   gst_message_parse_buffering (msg, &perc);
673   fail_unless (perc == 20);
674   gst_message_unref (msg);
675 
676   /* a 100% message should be ignored */
677   gst_element_post_message (mq2, gst_message_new_buffering (GST_OBJECT (mq2),
678           100));
679   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_BUFFERING, 0);
680   fail_unless (msg != NULL);
681   fail_unless (GST_MESSAGE_SRC (msg) == (GstObject *) mq1);
682   gst_message_parse_buffering (msg, &perc);
683   fail_unless (perc == 20);
684   gst_message_unref (msg);
685 
686   /* a new buffering message is posted with a higher value, go with the 20 */
687   gst_element_post_message (mq2, gst_message_new_buffering (GST_OBJECT (mq2),
688           80));
689   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_BUFFERING, 0);
690   fail_unless (msg != NULL);
691   fail_unless (GST_MESSAGE_SRC (msg) == (GstObject *) mq1);
692   gst_message_parse_buffering (msg, &perc);
693   fail_unless (perc == 20);
694   gst_message_unref (msg);
695 
696   /* The mq1 finishes buffering, new buffering status is now 50% from mq0 */
697   gst_element_post_message (mq1, gst_message_new_buffering (GST_OBJECT (mq1),
698           100));
699   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_BUFFERING, 0);
700   fail_unless (msg != NULL);
701   fail_unless (GST_MESSAGE_SRC (msg) == (GstObject *) mq0);
702   gst_message_parse_buffering (msg, &perc);
703   fail_unless (perc == 50);
704   gst_message_unref (msg);
705 
706   gst_element_set_state (pipe, GST_STATE_NULL);
707   gst_object_unref (pipe);
708 }
709 
710 GST_END_TEST;
711 
712 static Suite *
decodebin_suite(void)713 decodebin_suite (void)
714 {
715   Suite *s = suite_create ("decodebin");
716   TCase *tc_chain = tcase_create ("general");
717 
718   suite_add_tcase (s, tc_chain);
719   tcase_add_test (tc_chain, test_text_plain_streams);
720   tcase_add_test (tc_chain, test_reuse_without_decoders);
721   tcase_add_test (tc_chain, test_mp3_parser_loop);
722   tcase_add_test (tc_chain, test_parser_negotiation);
723   tcase_add_test (tc_chain, test_buffering_aggregation);
724 
725   return s;
726 }
727 
728 GST_CHECK_MAIN (decodebin);
729