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