2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/gst-i18n-plugin.h>
26 #include "gstplaybasebin.h"
27 #include "gststreamselector.h"
28 #include "gstplay-marshal.h"
30 #include <gst/pbutils/pbutils.h>
32 #include "gst/glib-compat-private.h"
34 GST_DEBUG_CATEGORY_STATIC (gst_play_base_bin_debug);
35 #define GST_CAT_DEFAULT gst_play_base_bin_debug
37 #define DEFAULT_QUEUE_SIZE (3 * GST_SECOND)
38 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
39 #define DEFAULT_QUEUE_THRESHOLD ((DEFAULT_QUEUE_SIZE * 95) / 100)
40 #define DEFAULT_CONNECTION_SPEED 0
42 #define GROUP_LOCK(pbb) g_mutex_lock (pbb->group_lock)
43 #define GROUP_UNLOCK(pbb) g_mutex_unlock (pbb->group_lock)
44 #define GROUP_WAIT(pbb) g_cond_wait (pbb->group_cond, pbb->group_lock)
45 #define GROUP_SIGNAL(pbb) g_cond_signal (pbb->group_cond)
55 ARG_QUEUE_MIN_THRESHOLD,
58 ARG_STREAMINFO_VALUES,
63 ARG_SUBTITLE_ENCODING,
67 static void gst_play_base_bin_class_init (GstPlayBaseBinClass * klass);
68 static void gst_play_base_bin_init (GstPlayBaseBin * play_base_bin);
69 static void gst_play_base_bin_dispose (GObject * object);
70 static void gst_play_base_bin_finalize (GObject * object);
72 static void gst_play_base_bin_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * spec);
74 static void gst_play_base_bin_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * spec);
76 static void gst_play_base_bin_handle_message_func (GstBin * bin,
79 static GstStateChangeReturn gst_play_base_bin_change_state (GstElement *
80 element, GstStateChange transition);
82 static const GList *gst_play_base_bin_get_streaminfo (GstPlayBaseBin * bin);
83 static GValueArray *gst_play_base_bin_get_streaminfo_value_array (GstPlayBaseBin
85 static void preroll_remove_overrun (GstElement * element,
86 GstPlayBaseBin * play_base_bin);
87 static void queue_remove_probe (GstElement * queue, GstPlayBaseBin
90 static GstElement *make_decoder (GstPlayBaseBin * play_base_bin);
91 static gboolean has_all_raw_caps (GstPad * pad, gboolean * all_raw);
93 static gboolean prepare_output (GstPlayBaseBin * play_base_bin);
94 static void set_active_source (GstPlayBaseBin * play_base_bin,
95 GstStreamType type, gint source_num);
96 static gboolean probe_triggered (GstPad * pad, GstEvent * event,
98 static void setup_substreams (GstPlayBaseBin * play_base_bin);
100 static GstPipelineClass *parent_class;
103 * GObject playbasebin wrappers.
107 gst_play_base_bin_get_type (void)
109 static GType gst_play_base_bin_type = 0;
111 if (!gst_play_base_bin_type) {
112 static const GTypeInfo gst_play_base_bin_info = {
113 sizeof (GstPlayBaseBinClass),
116 (GClassInitFunc) gst_play_base_bin_class_init,
119 sizeof (GstPlayBaseBin),
121 (GInstanceInitFunc) gst_play_base_bin_init,
125 gst_play_base_bin_type = g_type_register_static (GST_TYPE_PIPELINE,
126 "GstPlayBaseBin", &gst_play_base_bin_info, 0);
129 return gst_play_base_bin_type;
133 gst_play_base_bin_class_init (GstPlayBaseBinClass * klass)
135 GObjectClass *gobject_klass;
136 GstElementClass *gstelement_klass;
137 GstBinClass *gstbin_klass;
139 gobject_klass = (GObjectClass *) klass;
140 gstelement_klass = (GstElementClass *) klass;
141 gstbin_klass = (GstBinClass *) klass;
143 parent_class = g_type_class_peek_parent (klass);
145 gobject_klass->set_property = gst_play_base_bin_set_property;
146 gobject_klass->get_property = gst_play_base_bin_get_property;
148 g_object_class_install_property (gobject_klass, ARG_URI,
149 g_param_spec_string ("uri", "URI", "URI of the media to play",
150 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151 g_object_class_install_property (gobject_klass, ARG_SUBURI,
152 g_param_spec_string ("suburi", ".sub-URI", "Optional URI of a subtitle",
153 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 g_object_class_install_property (gobject_klass, ARG_QUEUE_SIZE,
156 g_param_spec_uint64 ("queue-size", "Queue size",
157 "Size of internal queues in nanoseconds", 0, G_MAXINT64,
158 DEFAULT_QUEUE_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_klass, ARG_QUEUE_THRESHOLD,
160 g_param_spec_uint64 ("queue-threshold", "Queue threshold",
161 "Buffering threshold of internal queues in nanoseconds", 0,
162 G_MAXINT64, DEFAULT_QUEUE_THRESHOLD,
163 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 g_object_class_install_property (gobject_klass, ARG_QUEUE_MIN_THRESHOLD,
165 g_param_spec_uint64 ("queue-min-threshold", "Queue min threshold",
166 "Buffering low threshold of internal queues in nanoseconds", 0,
167 G_MAXINT64, DEFAULT_QUEUE_MIN_THRESHOLD,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 g_object_class_install_property (gobject_klass, ARG_NSTREAMS,
171 g_param_spec_int ("nstreams", "NStreams", "number of streams",
172 0, G_MAXINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
173 g_object_class_install_property (gobject_klass, ARG_STREAMINFO,
174 g_param_spec_pointer ("stream-info", "Stream info", "List of streaminfo",
175 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
176 g_object_class_install_property (gobject_klass, ARG_STREAMINFO_VALUES,
177 g_param_spec_value_array ("stream-info-value-array",
178 "StreamInfo GValueArray", "value array of streaminfo",
179 g_param_spec_object ("streaminfo", "StreamInfo", "Streaminfo object",
180 GST_TYPE_STREAM_INFO, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS),
181 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (gobject_klass, ARG_SOURCE,
183 g_param_spec_object ("source", "Source", "Source element",
184 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (gobject_klass, ARG_VIDEO,
187 g_param_spec_int ("current-video", "Current video",
188 "Currently playing video stream (-1 = none)",
189 -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190 g_object_class_install_property (gobject_klass, ARG_AUDIO,
191 g_param_spec_int ("current-audio", "Current audio",
192 "Currently playing audio stream (-1 = none)",
193 -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (gobject_klass, ARG_TEXT,
195 g_param_spec_int ("current-text", "Current text",
196 "Currently playing text stream (-1 = none)",
197 -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_klass, ARG_SUBTITLE_ENCODING,
199 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
200 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
201 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
202 "be checked for an encoding to use. If that is not set either, "
203 "ISO-8859-15 will be assumed.", NULL,
204 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 * GstPlayBaseBin:connection-speed
208 * Network connection speed in kbps (0 = unknown)
210 * Since version 0.10.10 in #GstPlayBin, at 0.10.15 moved to #GstPlayBaseBin
215 g_object_class_install_property (gobject_klass, ARG_CONNECTION_SPEED,
216 g_param_spec_uint ("connection-speed", "Connection Speed",
217 "Network connection speed in kbps (0 = unknown)",
218 0, G_MAXUINT, DEFAULT_CONNECTION_SPEED,
219 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 GST_DEBUG_CATEGORY_INIT (gst_play_base_bin_debug, "playbasebin", 0,
224 gobject_klass->dispose = gst_play_base_bin_dispose;
225 gobject_klass->finalize = gst_play_base_bin_finalize;
227 gstbin_klass->handle_message =
228 GST_DEBUG_FUNCPTR (gst_play_base_bin_handle_message_func);
230 gstelement_klass->change_state =
231 GST_DEBUG_FUNCPTR (gst_play_base_bin_change_state);
235 gst_play_base_bin_init (GstPlayBaseBin * play_base_bin)
237 play_base_bin->uri = NULL;
238 play_base_bin->suburi = NULL;
239 play_base_bin->need_rebuild = TRUE;
240 play_base_bin->is_stream = FALSE;
241 play_base_bin->source = NULL;
242 play_base_bin->decoders = NULL;
243 play_base_bin->subtitle = NULL;
244 play_base_bin->subencoding = NULL;
245 play_base_bin->subtitle_elements = NULL;
246 play_base_bin->sub_lock = g_mutex_new ();
248 play_base_bin->group_lock = g_mutex_new ();
249 play_base_bin->group_cond = g_cond_new ();
251 play_base_bin->building_group = NULL;
252 play_base_bin->queued_groups = NULL;
254 play_base_bin->queue_size = DEFAULT_QUEUE_SIZE;
255 play_base_bin->queue_threshold = DEFAULT_QUEUE_THRESHOLD;
256 play_base_bin->queue_min_threshold = DEFAULT_QUEUE_MIN_THRESHOLD;
257 play_base_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
261 gst_play_base_bin_dispose (GObject * object)
263 GstPlayBaseBin *play_base_bin;
265 play_base_bin = GST_PLAY_BASE_BIN (object);
266 g_free (play_base_bin->uri);
267 play_base_bin->uri = NULL;
268 g_free (play_base_bin->suburi);
269 play_base_bin->suburi = NULL;
270 g_free (play_base_bin->subencoding);
271 play_base_bin->subencoding = NULL;
272 if (play_base_bin->subtitle_elements) {
273 g_slist_free (play_base_bin->subtitle_elements);
274 play_base_bin->subtitle_elements = NULL;
276 G_OBJECT_CLASS (parent_class)->dispose (object);
280 gst_play_base_bin_finalize (GObject * object)
282 GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (object);
284 g_mutex_free (play_base_bin->group_lock);
285 g_cond_free (play_base_bin->group_cond);
287 g_mutex_free (play_base_bin->sub_lock);
289 G_OBJECT_CLASS (parent_class)->finalize (object);
293 * playbasebingroup stuff.
296 static GstPlayBaseGroup *
297 group_create (GstPlayBaseBin * play_base_bin)
299 GstPlayBaseGroup *group;
301 group = g_new0 (GstPlayBaseGroup, 1);
302 group->bin = play_base_bin;
303 group->streaminfo_value_array = g_value_array_new (0);
305 GST_DEBUG_OBJECT (play_base_bin, "created new group %p", group);
311 * Gets the currently playing group.
313 * Callers must have group-lock held when calling this.
316 static GstPlayBaseGroup *
317 get_active_group (GstPlayBaseBin * play_base_bin)
319 GstPlayBaseGroup *group = NULL;
321 if (play_base_bin->queued_groups)
322 group = play_base_bin->queued_groups->data;
328 * get the group used for discovering the different streams.
329 * This function creates a group is there is none.
331 * Callers must have group-lock held when calling this.
333 static GstPlayBaseGroup *
334 get_building_group (GstPlayBaseBin * play_base_bin)
336 GstPlayBaseGroup *group;
338 group = play_base_bin->building_group;
340 group = group_create (play_base_bin);
341 play_base_bin->building_group = group;
348 * Callers must have lock held when calling this!
352 group_destroy (GstPlayBaseGroup * group)
354 GstPlayBaseBin *play_base_bin = group->bin;
357 GST_LOG ("removing group %p", group);
359 /* remove the preroll queues */
360 for (n = 0; n < NUM_TYPES; n++) {
361 GstElement *element = group->type[n].preroll;
369 sel = group->type[n].selector;
371 /* remove any fakesrc elements for this preroll element */
372 for (item = sel->pads; item != NULL; item = item->next) {
373 GstPad *pad = GST_PAD (item->data);
376 if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)
380 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pad), "unlinked_id"));
383 GST_LOG ("removing unlink signal %s:%s", GST_DEBUG_PAD_NAME (pad));
384 g_signal_handler_disconnect (G_OBJECT (pad), sig_id);
385 g_object_set_data (G_OBJECT (pad), "unlinked_id", GINT_TO_POINTER (0));
388 fakesrc = (GstElement *) g_object_get_data (G_OBJECT (pad), "fakesrc");
389 if (fakesrc != NULL) {
390 GST_LOG ("removing fakesrc from %s:%s",
391 GST_PAD_NAME (pad), GST_ELEMENT_NAME (GST_PAD_PARENT (pad)));
392 gst_element_set_state (fakesrc, GST_STATE_NULL);
393 gst_bin_remove (GST_BIN_CAST (play_base_bin), fakesrc);
397 /* if the group is currently being played, we have to remove the element
399 gst_element_set_state (element, GST_STATE_NULL);
400 gst_element_set_state (group->type[n].selector, GST_STATE_NULL);
402 GST_LOG ("removing preroll element %s", GST_ELEMENT_NAME (element));
404 gst_bin_remove (group->type[n].bin, element);
405 gst_bin_remove (group->type[n].bin, group->type[n].selector);
407 group->type[n].preroll = NULL;
408 group->type[n].selector = NULL;
409 group->type[n].bin = NULL;
412 /* free the streaminfo too */
413 g_list_foreach (group->streaminfo, (GFunc) g_object_unref, NULL);
414 g_list_free (group->streaminfo);
415 g_value_array_free (group->streaminfo_value_array);
420 * is called when the current building group is completely finished
421 * and ready for playback
423 * This function grabs lock, so take care when calling.
426 group_commit (GstPlayBaseBin * play_base_bin, gboolean fatal, gboolean subtitle)
428 GstPlayBaseGroup *group;
429 gboolean had_active_group;
431 GROUP_LOCK (play_base_bin);
432 group = play_base_bin->building_group;
433 had_active_group = (get_active_group (play_base_bin) != NULL);
435 GST_DEBUG_OBJECT (play_base_bin, "commit group %p, had active %d",
436 group, had_active_group);
438 /* if an element signalled a no-more-pads after we stopped due
439 * to preroll, the group is NULL. This is not an error */
442 GROUP_UNLOCK (play_base_bin);
445 GST_DEBUG_OBJECT (play_base_bin, "Group loading failed, bailing out");
451 GST_DEBUG_OBJECT (play_base_bin, "group %p done", group);
453 play_base_bin->queued_groups =
454 g_list_append (play_base_bin->queued_groups, group);
456 play_base_bin->building_group = NULL;
458 /* remove signals. We don't want anymore signals from the preroll
459 * elements at this stage. */
460 for (n = 0; n < NUM_TYPES; n++) {
461 GstElement *element = group->type[n].preroll;
466 preroll_remove_overrun (element, play_base_bin);
467 /* if overrun is removed, probe alse has to be removed */
468 queue_remove_probe (element, play_base_bin);
471 /* this is a special subtitle bin, we don't commit the group but
472 * mark the subtitles as detected before we signal. */
473 GST_DEBUG_OBJECT (play_base_bin, "marking subtitle bin as complete");
474 play_base_bin->subtitle_done = TRUE;
478 GST_DEBUG_OBJECT (play_base_bin, "signal group done");
479 GROUP_SIGNAL (play_base_bin);
480 GST_DEBUG_OBJECT (play_base_bin, "signaled group done");
482 if (!subtitle && !had_active_group) {
483 if (!prepare_output (play_base_bin)) {
484 GROUP_UNLOCK (play_base_bin);
488 setup_substreams (play_base_bin);
489 GST_DEBUG_OBJECT (play_base_bin, "Emitting signal");
490 GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin)->setup_output_pads
491 (play_base_bin, group);
492 GST_DEBUG_OBJECT (play_base_bin, "done");
494 GROUP_UNLOCK (play_base_bin);
496 g_object_notify (G_OBJECT (play_base_bin), "stream-info");
498 GROUP_UNLOCK (play_base_bin);
503 * check if there are streams in the group that are not muted
505 * Callers must have group-lock held when calling this.
508 group_is_muted (GstPlayBaseGroup * group)
512 for (n = 0; n < NUM_TYPES; n++) {
513 if (group->type[n].preroll && !group->type[n].done)
521 * Buffer/cache checking.
525 fill_buffer (GstPlayBaseBin * play_base_bin, gint percent)
527 GST_DEBUG_OBJECT (play_base_bin, "buffering %d", percent);
528 gst_element_post_message (GST_ELEMENT_CAST (play_base_bin),
529 gst_message_new_buffering (GST_OBJECT_CAST (play_base_bin), percent));
533 check_queue_event (GstPad * pad, GstEvent * event, gpointer user_data)
535 GstElement *queue = GST_ELEMENT_CAST (user_data);
537 switch (GST_EVENT_TYPE (event)) {
539 GST_DEBUG ("EOS event, mark EOS");
540 g_object_set_data (G_OBJECT (queue), "eos", GINT_TO_POINTER (1));
542 case GST_EVENT_FLUSH_STOP:
543 GST_DEBUG ("FLUSH_STOP event, remove EOS");
544 g_object_set_data (G_OBJECT (queue), "eos", NULL);
547 GST_DEBUG ("uninteresting event %s", GST_EVENT_TYPE_NAME (event));
554 check_queue (GstPad * pad, GstBuffer * data, gpointer user_data)
556 GstElement *queue = GST_ELEMENT_CAST (user_data);
557 GstPlayBaseBin *play_base_bin = g_object_get_data (G_OBJECT (queue), "pbb");
560 GST_DEBUG_OBJECT (queue, "check queue triggered");
562 g_object_get (G_OBJECT (queue), "current-level-time", &level, NULL);
563 GST_DEBUG_OBJECT (play_base_bin, "Queue size: %" GST_TIME_FORMAT,
564 GST_TIME_ARGS (level));
566 if (play_base_bin->queue_threshold > 0) {
567 level = level * 99 / play_base_bin->queue_threshold;
573 fill_buffer (play_base_bin, level);
579 /* If a queue overruns and we are buffer in streaming mode (we have a min-time)
580 * we can potentially create a deadlock when:
582 * 1) the max-bytes is hit and
583 * 2) the min-time is not hit.
585 * We recover from this situation in this callback by
586 * setting the max-bytes to unlimited if we see that there is
587 * a current-time-level (which means some sort of timestamping is
591 queue_deadlock_check (GstElement * queue, GstPlayBaseBin * play_base_bin)
593 guint64 time, min_time;
596 GST_DEBUG_OBJECT (play_base_bin, "overrun signal received from queue %s",
597 GST_ELEMENT_NAME (queue));
599 /* figure out where we are */
600 g_object_get (G_OBJECT (queue), "current-level-time", &time,
601 "current-level-bytes", &bytes, "min-threshold-time", &min_time, NULL);
603 GST_DEBUG_OBJECT (play_base_bin, "streaming mode, queue %s current %"
604 GST_TIME_FORMAT ", min %" GST_TIME_FORMAT
605 ", bytes %d", GST_ELEMENT_NAME (queue),
606 GST_TIME_ARGS (time), GST_TIME_ARGS (min_time), bytes);
608 /* if the bytes in the queue represent time, we disable bytes
609 * overrun checking to not cause deadlocks.
611 if (bytes && time != 0 && time < min_time) {
612 GST_DEBUG_OBJECT (play_base_bin,
613 "possible deadlock found, removing byte limit");
615 /* queue knows about time but is filled with bytes that do
616 * not represent min-threshold time, disable bytes checking so
617 * the queue can grow some more. */
618 g_object_set (G_OBJECT (queue), "max-size-bytes", 0, NULL);
620 /* bytes limit is removed, we cannot deadlock anymore */
621 g_signal_handlers_disconnect_by_func (queue,
622 (gpointer) queue_deadlock_check, play_base_bin);
624 GST_DEBUG_OBJECT (play_base_bin, "no deadlock");
629 queue_remove_probe (GstElement * queue, GstPlayBaseBin * play_base_bin)
634 data = g_object_get_data (G_OBJECT (queue), "probe");
635 sinkpad = gst_element_get_static_pad (queue, "sink");
638 GST_DEBUG_OBJECT (play_base_bin,
639 "Removing buffer probe from pad %s:%s (%p)",
640 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
642 g_object_set_data (G_OBJECT (queue), "probe", NULL);
643 gst_pad_remove_buffer_probe (sinkpad, GPOINTER_TO_INT (data));
645 GST_DEBUG_OBJECT (play_base_bin,
646 "No buffer probe to remove from %s:%s (%p)",
647 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
649 gst_object_unref (sinkpad);
652 /* Used for time-based buffering in streaming mode and is called when a queue
653 * emits the running signal. This means that the high watermark threshold is
654 * reached and the buffering is completed. */
656 queue_threshold_reached (GstElement * queue, GstPlayBaseBin * play_base_bin)
658 GstPlayBaseGroup *group;
661 GST_DEBUG_OBJECT (play_base_bin, "running signal received from queue %s",
662 GST_ELEMENT_NAME (queue));
664 /* we disconnect the signal so that we don't get called for every buffer. */
665 g_signal_handlers_disconnect_by_func (queue,
666 (gpointer) queue_threshold_reached, play_base_bin);
668 if (g_object_get_data (G_OBJECT (queue), "eos")) {
669 GST_DEBUG_OBJECT (play_base_bin, "disable min threshold time, we are EOS");
670 g_object_set (queue, "min-threshold-time", (guint64) 0, NULL);
672 /* now place the limits at the low threshold. When we hit this limit, the
673 * underrun signal will be called. The underrun signal is always connected. */
674 GST_DEBUG_OBJECT (play_base_bin,
675 "setting min threshold time to %" G_GUINT64_FORMAT,
676 play_base_bin->queue_min_threshold);
677 g_object_set (queue, "min-threshold-time",
678 play_base_bin->queue_min_threshold, NULL);
681 GROUP_LOCK (play_base_bin);
682 group = get_active_group (play_base_bin);
684 GROUP_UNLOCK (play_base_bin);
688 /* we remove the probe now because we don't need it anymore to give progress
689 * about the buffering. */
690 for (n = 0; n < NUM_TYPES; n++) {
691 GstElement *element = group->type[n].preroll;
696 queue_remove_probe (element, play_base_bin);
699 GROUP_UNLOCK (play_base_bin);
701 /* we post a 100% buffering message to notify the app that buffering is
702 * completed and playback can start/continue */
703 if (play_base_bin->is_stream)
704 fill_buffer (play_base_bin, 100);
707 /* this signal will be fired when one of the queues with raw
708 * data is filled. This means that the group building stage is over
709 * and playback of the new queued group should start. This is a rather unusual
710 * situation because normally the group is committed when the "no_more_pads"
714 queue_overrun (GstElement * queue, GstPlayBaseBin * play_base_bin)
716 GST_DEBUG_OBJECT (play_base_bin, "queue %s overrun",
717 GST_ELEMENT_NAME (queue));
719 preroll_remove_overrun (queue, play_base_bin);
721 group_commit (play_base_bin, FALSE,
722 GST_OBJECT_PARENT (GST_OBJECT_CAST (queue)) ==
723 GST_OBJECT_CAST (play_base_bin->subtitle));
725 /* notify end of buffering */
726 queue_threshold_reached (queue, play_base_bin);
729 /* this signal is only added when in streaming mode to catch underruns
732 queue_out_of_data (GstElement * queue, GstPlayBaseBin * play_base_bin)
734 GST_DEBUG_OBJECT (play_base_bin, "underrun signal received from queue %s",
735 GST_ELEMENT_NAME (queue));
737 /* On underrun, we want to temporarily pause playback, set a "min-size"
738 * threshold and wait for the running signal and then play again.
740 * This signal could never be called because the queue max-size limits are set
741 * too low. We take care of this possible deadlock in the overrun signal
743 g_signal_connect (G_OBJECT (queue), "pushing",
744 G_CALLBACK (queue_threshold_reached), play_base_bin);
745 GST_DEBUG_OBJECT (play_base_bin,
746 "setting min threshold time to %" G_GUINT64_FORMAT,
747 (guint64) play_base_bin->queue_threshold);
748 g_object_set (queue, "min-threshold-time",
749 (guint64) play_base_bin->queue_threshold, NULL);
751 /* re-connect probe, this will fire feedback about the percentage that we
752 * buffered and is posted in the BUFFERING message. */
753 if (!g_object_get_data (G_OBJECT (queue), "probe")) {
757 sinkpad = gst_element_get_static_pad (queue, "sink");
758 id = gst_pad_add_buffer_probe (sinkpad, G_CALLBACK (check_queue), queue);
759 g_object_set_data (G_OBJECT (queue), "probe", GINT_TO_POINTER (id));
760 GST_DEBUG_OBJECT (play_base_bin,
761 "Re-attaching buffering probe to pad %s:%s %p",
762 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
763 gst_object_unref (sinkpad);
765 fill_buffer (play_base_bin, 0);
770 * generate a preroll element which is simply a queue. While there
771 * are still dynamic elements in the pipeline, we wait for one
772 * of the queues to fill. The assumption is that all the dynamic
773 * streams will be detected by that time.
775 * Callers must have the group-lock held when calling this.
778 gen_preroll_element (GstPlayBaseBin * play_base_bin,
779 GstPlayBaseGroup * group, GstStreamType type, GstPad * pad,
780 GstStreamInfo * info)
782 GstElement *selector, *preroll;
783 gchar *name, *padname;
784 const gchar *prename;
790 if (type == GST_STREAM_TYPE_VIDEO)
792 else if (type == GST_STREAM_TYPE_TEXT)
794 else if (type == GST_STREAM_TYPE_AUDIO)
796 else if (type == GST_STREAM_TYPE_SUBPICTURE)
797 prename = "subpicture";
799 g_return_if_reached ();
801 /* create stream selector */
802 padname = gst_pad_get_name (pad);
803 name = g_strdup_printf ("selector_%s_%s", prename, padname);
804 selector = g_object_new (GST_TYPE_STREAM_SELECTOR, "name", name, NULL);
807 /* create preroll queue */
808 name = g_strdup_printf ("preroll_%s_%s", prename, padname);
809 preroll = gst_element_factory_make ("queue", name);
813 /* for buffering of raw data we ideally want to buffer a
814 * very small amount of buffers since the memory used by
815 * this raw data can be enormously huge.
817 * We use an upper limit of typically a few seconds here but
818 * cap in case no timestamps are set on the raw data (bad!).
820 * FIXME: we abuse this buffer to do network buffering since
821 * we can then easily do time-based buffering. The better
822 * solution would be to add a specific network queue right
823 * after the source that measures the datarate and scales this
824 * queue of encoded data instead.
826 if (play_base_bin->raw_decoding_mode) {
827 if (type == GST_STREAM_TYPE_VIDEO) {
828 g_object_set (G_OBJECT (preroll),
829 "max-size-buffers", 2, "max-size-bytes", 0,
830 "max-size-time", (guint64) 0, NULL);
832 g_object_set (G_OBJECT (preroll),
833 "max-size-buffers", 0, "max-size-bytes",
834 2 * 1024 * 1024, "max-size-time", play_base_bin->queue_size, NULL);
837 g_object_set (G_OBJECT (preroll),
838 "max-size-buffers", 0, "max-size-bytes",
839 ((type == GST_STREAM_TYPE_VIDEO) ? 25 : 2) * 1024 * 1024,
840 "max-size-time", play_base_bin->queue_size, NULL);
843 /* the overrun signal is always attached and serves two purposes:
845 * 1) when we are building a group and the overrun is called, we commit the
846 * group. The reason being that if we fill the entire queue without a
847 * normal group commit (with _no_more_pads()) we can assume the
848 * audio/video is completely wacked or the element just does not know when
849 * it is ready with all the pads (mpeg).
850 * 2) When we are doing network buffering, we keep track of low/high
851 * watermarks in the queue. It is possible that we set the high watermark
852 * higher than the max-size limits to trigger an overrun. In this case we
853 * will never get a running signal but we can use the overrun signal to
854 * detect this deadlock and correct it.
856 overrun_sig = g_signal_connect (G_OBJECT (preroll), "overrun",
857 G_CALLBACK (queue_overrun), play_base_bin);
859 /* keep a ref to the signal id so that we can disconnect the signal callback
860 * when we are done with the preroll */
861 g_object_set_data (G_OBJECT (preroll), "overrun_signal_id",
862 GINT_TO_POINTER (overrun_sig));
864 if (play_base_bin->is_stream &&
865 ((type == GST_STREAM_TYPE_VIDEO &&
866 group->type[GST_STREAM_TYPE_AUDIO - 1].npads == 0) ||
867 (type == GST_STREAM_TYPE_AUDIO &&
868 group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0))) {
872 /* catch deadlocks when we are network buffering in time but the max-limit
873 * in bytes is hit. */
874 g_signal_connect (G_OBJECT (preroll), "overrun",
875 G_CALLBACK (queue_deadlock_check), play_base_bin);
877 /* attach pointer to playbasebin */
878 g_object_set_data (G_OBJECT (preroll), "pbb", play_base_bin);
880 /* give updates on queue size */
881 sinkpad = gst_element_get_static_pad (preroll, "sink");
882 id = gst_pad_add_buffer_probe (sinkpad, G_CALLBACK (check_queue), preroll);
883 GST_DEBUG_OBJECT (play_base_bin, "Attaching probe to pad %s:%s (%p)",
884 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
885 g_object_set_data (G_OBJECT (preroll), "probe", GINT_TO_POINTER (id));
887 /* catch eos and flush events so that we can ignore underruns */
888 id = gst_pad_add_event_probe (sinkpad, G_CALLBACK (check_queue_event),
890 g_object_set_data (G_OBJECT (preroll), "eos_probe", GINT_TO_POINTER (id));
892 gst_object_unref (sinkpad);
894 /* When we connect this queue, it will start running and immediately
895 * fire an underrun. */
896 g_signal_connect (G_OBJECT (preroll), "underrun",
897 G_CALLBACK (queue_out_of_data), play_base_bin);
898 /* configure threshold and callbacks */
899 queue_out_of_data (preroll, play_base_bin);
902 /* listen for EOS so we can switch groups when one ended. */
903 preroll_pad = gst_element_get_static_pad (preroll, "src");
904 gst_pad_add_event_probe (preroll_pad, G_CALLBACK (probe_triggered), info);
905 gst_object_unref (preroll_pad);
907 /* add to group list */
908 group->type[type - 1].selector = selector;
909 group->type[type - 1].preroll = preroll;
911 /* figure out where the preroll element should go */
912 if (type == GST_STREAM_TYPE_TEXT && play_base_bin->subtitle)
913 target = GST_BIN_CAST (play_base_bin->subtitle);
915 target = GST_BIN_CAST (play_base_bin);
917 group->type[type - 1].bin = target;
918 gst_bin_add (target, selector);
919 gst_bin_add (target, preroll);
921 gst_element_link (selector, preroll);
923 /* figure out target state and set */
924 state = (GST_STATE (play_base_bin) == GST_STATE_PLAYING ?
925 GST_STATE_PLAYING : GST_STATE_PAUSED);
927 gst_element_set_state (selector, state);
928 gst_element_set_state (preroll, state);
932 preroll_remove_overrun (GstElement * element, GstPlayBaseBin * play_base_bin)
935 GObject *obj = G_OBJECT (element);
937 overrun_sig = GPOINTER_TO_INT (g_object_get_data (obj, "overrun_signal_id"));
939 GST_LOG_OBJECT (play_base_bin, "removing preroll signal %s",
940 GST_ELEMENT_NAME (element));
941 g_signal_handler_disconnect (obj, overrun_sig);
942 /* We have disconnected this signal, remove the signal_id from the object
944 g_object_set_data (obj, "overrun_signal_id", NULL);
949 remove_groups (GstPlayBaseBin * play_base_bin)
951 GROUP_LOCK (play_base_bin);
953 /* first destroy the group we were building if any */
954 if (play_base_bin->building_group) {
955 group_destroy (play_base_bin->building_group);
956 play_base_bin->building_group = NULL;
959 /* remove the queued groups */
960 g_list_foreach (play_base_bin->queued_groups, (GFunc) group_destroy, NULL);
961 g_list_free (play_base_bin->queued_groups);
962 play_base_bin->queued_groups = NULL;
965 if (play_base_bin->subtitle) {
966 gst_element_set_state (play_base_bin->subtitle, GST_STATE_NULL);
967 gst_bin_remove (GST_BIN_CAST (play_base_bin), play_base_bin->subtitle);
968 play_base_bin->subtitle = NULL;
971 GROUP_UNLOCK (play_base_bin);
975 * Add/remove a single stream to current building group.
977 * Must be called with group-lock held.
980 add_stream (GstPlayBaseGroup * group, GstStreamInfo * info)
983 GST_DEBUG ("add stream to group %p", group);
985 /* keep ref to the group */
986 g_object_set_data (G_OBJECT (info), "group", group);
988 g_value_init (&v, G_TYPE_OBJECT);
989 g_value_set_object (&v, info);
990 g_value_array_append (group->streaminfo_value_array, &v);
992 group->streaminfo = g_list_append (group->streaminfo, info);
993 if (info->type > 0 && info->type <= NUM_TYPES) {
994 group->type[info->type - 1].npads++;
999 string_arr_has_str (const gchar * values[], const gchar * value)
1001 if (values && value) {
1002 while (*values != NULL) {
1003 if (strcmp (value, *values) == 0)
1011 /* mime types we are not handling on purpose right now, don't post a
1012 * missing-plugin message for these */
1013 static const gchar *blacklisted_mimes[] = {
1014 "video/x-dvd-subpicture", NULL
1017 #define IS_BLACKLISTED_MIME(type) (string_arr_has_str(blacklisted_mimes,type))
1020 gst_play_base_bin_handle_message_func (GstBin * bin, GstMessage * msg)
1022 if (gst_is_missing_plugin_message (msg)) {
1026 detail = gst_missing_plugin_message_get_installer_detail (msg);
1027 for (i = 0; detail != NULL && blacklisted_mimes[i] != NULL; ++i) {
1028 if (strstr (detail, "|decoder-") && strstr (detail, blacklisted_mimes[i])) {
1029 GST_LOG_OBJECT (bin, "suppressing message %" GST_PTR_FORMAT, msg);
1030 gst_message_unref (msg);
1037 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
1041 * signal fired when an unknown stream is found. We create a new
1042 * UNKNOWN streaminfo object.
1045 unknown_type (GstElement * element, GstPad * pad, GstCaps * caps,
1046 GstPlayBaseBin * play_base_bin)
1048 const gchar *type_name;
1049 GstStreamInfo *info;
1050 GstPlayBaseGroup *group;
1052 type_name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
1053 if (type_name && !IS_BLACKLISTED_MIME (type_name)) {
1056 capsstr = gst_caps_to_string (caps);
1057 GST_DEBUG_OBJECT (play_base_bin, "don't know how to handle %s", capsstr);
1058 /* FIXME, g_message() ? */
1059 g_message ("don't know how to handle %s", capsstr);
1062 /* don't spew stuff to the terminal or send message if it's blacklisted */
1063 GST_DEBUG_OBJECT (play_base_bin, "media type %s not handled on purpose, "
1064 "not posting a missing-plugin message on the bus", type_name);
1067 GROUP_LOCK (play_base_bin);
1069 group = get_building_group (play_base_bin);
1071 /* add the stream to the list */
1072 info = gst_stream_info_new (GST_OBJECT_CAST (pad), GST_STREAM_TYPE_UNKNOWN,
1074 info->origin = GST_OBJECT_CAST (pad);
1075 add_stream (group, info);
1077 GROUP_UNLOCK (play_base_bin);
1080 /* add a streaminfo that indicates that the stream is handled by the
1081 * given element. This usually means that a stream without actual data is
1082 * produced but one that is sunken by an element. Examples of this are:
1083 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
1086 add_element_stream (GstElement * element, GstPlayBaseBin * play_base_bin)
1088 GstStreamInfo *info;
1089 GstPlayBaseGroup *group;
1091 GROUP_LOCK (play_base_bin);
1093 group = get_building_group (play_base_bin);
1095 /* add the stream to the list */
1097 gst_stream_info_new (GST_OBJECT_CAST (element), GST_STREAM_TYPE_ELEMENT,
1099 info->origin = GST_OBJECT_CAST (element);
1100 add_stream (group, info);
1102 GROUP_UNLOCK (play_base_bin);
1105 /* when the decoder element signals that no more pads will be generated, we
1106 * can commit the current group.
1109 no_more_pads_full (GstElement * element, gboolean subs,
1110 GstPlayBaseBin * play_base_bin)
1113 GST_DEBUG_OBJECT (element, "no more pads, %d pending",
1114 play_base_bin->pending);
1116 /* nothing pending, we can exit */
1117 if (play_base_bin->pending == 0)
1120 /* the object has no pending no_more_pads */
1121 if (!g_object_get_data (G_OBJECT (element), "pending"))
1124 g_object_set_data (G_OBJECT (element), "pending", NULL);
1126 play_base_bin->pending--;
1128 GST_DEBUG_OBJECT (element, "remove pending, now %d pending",
1129 play_base_bin->pending);
1131 if (play_base_bin->pending == 0) {
1132 /* we can commit this group for playback now */
1133 group_commit (play_base_bin, play_base_bin->is_stream, subs);
1138 no_more_pads (GstElement * element, GstPlayBaseBin * play_base_bin)
1140 no_more_pads_full (element, FALSE, play_base_bin);
1144 sub_no_more_pads (GstElement * element, GstPlayBaseBin * play_base_bin)
1146 no_more_pads_full (element, TRUE, play_base_bin);
1150 source_no_more_pads (GstElement * element, GstPlayBaseBin * bin)
1152 GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
1153 GST_ELEMENT_NAME (element));
1155 g_signal_handler_disconnect (G_OBJECT (element), bin->src_np_sig_id);
1156 bin->src_np_sig_id = 0;
1157 g_signal_handler_disconnect (G_OBJECT (element), bin->src_nmp_sig_id);
1158 bin->src_nmp_sig_id = 0;
1160 no_more_pads_full (element, FALSE, bin);
1164 probe_triggered (GstPad * pad, GstEvent * event, gpointer user_data)
1166 GstPlayBaseGroup *group;
1167 GstPlayBaseBin *play_base_bin;
1168 GstStreamInfo *info;
1171 type = GST_EVENT_TYPE (event);
1173 GST_LOG ("probe triggered, (%d) %s", type, gst_event_type_get_name (type));
1175 /* we only care about EOS */
1176 if (type != GST_EVENT_EOS)
1179 info = GST_STREAM_INFO (user_data);
1180 group = (GstPlayBaseGroup *) g_object_get_data (G_OBJECT (info), "group");
1181 play_base_bin = group->bin;
1183 if (type == GST_EVENT_EOS) {
1184 gint num_groups = 0;
1187 GST_DEBUG_OBJECT (play_base_bin, "probe got EOS in group %p", group);
1189 GROUP_LOCK (play_base_bin);
1191 /* mute this stream */
1192 g_object_set (G_OBJECT (info), "mute", TRUE, NULL);
1193 if (info->type > 0 && info->type <= NUM_TYPES)
1194 group->type[info->type - 1].done = TRUE;
1196 /* see if we have some more groups left to play */
1197 num_groups = g_list_length (play_base_bin->queued_groups);
1198 if (play_base_bin->building_group)
1200 have_left = (num_groups > 1);
1202 /* see if the complete group is muted */
1203 if (!group_is_muted (group)) {
1204 /* group is not completely muted, we remove the EOS event
1205 * and continue, eventually the other streams will be EOSed and
1206 * we can switch out this group. */
1207 GST_DEBUG ("group %p not completely muted", group);
1209 GROUP_UNLOCK (play_base_bin);
1211 /* remove the EOS if we have something left */
1216 /* ok, get rid of the current group then */
1217 //group_destroy (group);
1218 /* removing the current group brings the next group
1220 play_base_bin->queued_groups =
1221 g_list_remove (play_base_bin->queued_groups, group);
1222 /* and wait for the next one to be ready */
1223 while (!play_base_bin->queued_groups) {
1224 GROUP_WAIT (play_base_bin);
1226 group = play_base_bin->queued_groups->data;
1228 /* now activate the next one */
1229 setup_substreams (play_base_bin);
1230 GST_DEBUG ("switching to next group %p - emitting signal", group);
1231 /* and signal the new group */
1232 GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin)->setup_output_pads
1233 (play_base_bin, group);
1235 GROUP_UNLOCK (play_base_bin);
1237 g_object_notify (G_OBJECT (play_base_bin), "stream-info");
1239 /* get rid of the EOS event */
1242 GROUP_UNLOCK (play_base_bin);
1243 GST_LOG ("Last group done, EOS");
1250 /* This function will be called when the sinkpad of the preroll element
1251 * is unlinked, we have to connect something to the sinkpad or else the
1252 * state change will fail..
1255 preroll_unlinked (GstPad * pad, GstPad * peerpad,
1256 GstPlayBaseBin * play_base_bin)
1258 GstElement *fakesrc;
1262 /* make a fakesrc that will just emit one EOS */
1263 fakesrc = gst_element_factory_make ("fakesrc", NULL);
1264 g_object_set (G_OBJECT (fakesrc), "num-buffers", 0, NULL);
1266 GST_DEBUG ("patching unlinked pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1268 srcpad = gst_element_get_static_pad (fakesrc, "src");
1269 gst_bin_add (GST_BIN_CAST (play_base_bin), fakesrc);
1270 gst_pad_link (srcpad, pad);
1271 gst_object_unref (srcpad);
1273 /* keep track of these patch elements */
1274 g_object_set_data (G_OBJECT (pad), "fakesrc", fakesrc);
1276 /* now unlink the unlinked signal so that it is not called again when
1277 * we destroy the queue */
1278 sig_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pad), "unlinked_id"));
1280 g_signal_handler_disconnect (G_OBJECT (pad), sig_id);
1281 g_object_set_data (G_OBJECT (pad), "unlinked_id", GINT_TO_POINTER (0));
1285 /* Mute stream on first data - for header-is-in-stream-stuff
1286 * (vorbis, ogmtext). */
1288 mute_stream (GstPad * pad, GstBuffer * buf, gpointer data)
1290 GstStreamInfo *info = GST_STREAM_INFO (data);
1293 GST_DEBUG ("mute stream triggered");
1295 g_object_set (G_OBJECT (info), "mute", TRUE, NULL);
1296 id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info), "mute_probe"));
1297 g_object_set_data (G_OBJECT (info), "mute_probe", NULL);
1299 gst_pad_remove_buffer_probe (GST_PAD_CAST (info->object), id);
1307 silence_stream (GstPad * pad, GstMiniObject * data, gpointer user_data)
1309 GST_DEBUG ("silence stream triggered");
1315 /* Called by the signal handlers when a decodebin (main or subtitle) has
1316 * found a new raw pad. We create a preroll element if needed and the
1317 * appropriate streaminfo. Commits the group if there will be no more pads
1320 new_decoded_pad_full (GstElement * element, GstPad * pad, gboolean last,
1321 GstPlayBaseBin * play_base_bin, gboolean is_subs)
1323 GstStructure *structure;
1324 const gchar *mimetype;
1326 GstStreamInfo *info;
1327 GstStreamType type = GST_STREAM_TYPE_UNKNOWN;
1329 GstPlayBaseGroup *group;
1334 GST_DEBUG ("play base: new decoded pad. Last: %d", last);
1336 /* first see if this pad has interesting caps */
1337 caps = gst_pad_get_caps (pad);
1338 if (caps == NULL || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1341 /* get the mime type */
1342 structure = gst_caps_get_structure (caps, 0);
1343 mimetype = gst_structure_get_name (structure);
1345 GROUP_LOCK (play_base_bin);
1347 group = get_building_group (play_base_bin);
1351 parent = gst_object_get_parent (GST_OBJECT_CAST (element));
1352 if (g_str_has_prefix (mimetype, "audio/") &&
1353 parent != GST_OBJECT_CAST (play_base_bin->subtitle)) {
1354 type = GST_STREAM_TYPE_AUDIO;
1355 } else if (g_str_has_prefix (mimetype, "video/x-dvd-subpicture") &&
1356 parent != GST_OBJECT_CAST (play_base_bin->subtitle)) {
1357 type = GST_STREAM_TYPE_SUBPICTURE;
1358 } else if (g_str_has_prefix (mimetype, "video/") &&
1359 parent != GST_OBJECT_CAST (play_base_bin->subtitle)) {
1360 type = GST_STREAM_TYPE_VIDEO;
1361 } else if (g_str_has_prefix (mimetype, "text/")) {
1362 type = GST_STREAM_TYPE_TEXT;
1364 gst_object_unref (parent);
1366 info = gst_stream_info_new (GST_OBJECT_CAST (pad), type, NULL, caps);
1367 gst_caps_unref (caps);
1369 if (type == GST_STREAM_TYPE_UNKNOWN) {
1370 /* Unknown streams get added to the group, but the data
1371 * just gets ignored */
1372 add_stream (group, info);
1374 GROUP_UNLOCK (play_base_bin);
1376 /* signal the no more pads after adding the stream */
1378 no_more_pads_full (element, is_subs, play_base_bin);
1383 /* first pad of each type gets a selector + preroll queue */
1384 first_pad = (group->type[type - 1].npads == 0);
1387 GST_DEBUG ("play base: pad needs new preroll");
1388 gen_preroll_element (play_base_bin, group, type, pad, info);
1391 /* add to stream selector */
1393 gst_element_get_request_pad (group->type[type - 1].selector, "sink%d");
1395 /* make sure we catch unlink signals */
1396 sig = g_signal_connect (G_OBJECT (sinkpad), "unlinked",
1397 G_CALLBACK (preroll_unlinked), play_base_bin);
1398 /* keep a ref to the signal id so that we can disconnect the signal callback */
1399 g_object_set_data (G_OBJECT (sinkpad), "unlinked_id", GINT_TO_POINTER (sig));
1400 /* Store a pointer to the stream selector pad for this stream */
1401 g_object_set_data (G_OBJECT (pad), "pb_sel_pad", sinkpad);
1403 gst_pad_link (pad, sinkpad);
1404 gst_object_unref (sinkpad);
1406 /* select 1st for now - we'll select a preferred one after preroll */
1410 GST_DEBUG ("Adding silence_stream data probe on type %d (npads %d)", type,
1411 group->type[type - 1].npads);
1413 id = gst_pad_add_data_probe (GST_PAD_CAST (pad),
1414 G_CALLBACK (silence_stream), info);
1415 g_object_set_data (G_OBJECT (pad), "eat_probe", GINT_TO_POINTER (id));
1418 /* add the stream to the list */
1419 add_stream (group, info);
1421 GROUP_UNLOCK (play_base_bin);
1423 /* signal the no more pads after adding the stream */
1425 no_more_pads_full (element, is_subs, play_base_bin);
1432 g_warning ("no type on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1434 gst_caps_unref (caps);
1440 new_decoded_pad (GstElement * element, GstPad * pad, gboolean last,
1441 GstPlayBaseBin * play_base_bin)
1443 new_decoded_pad_full (element, pad, last, play_base_bin, FALSE);
1447 subs_new_decoded_pad (GstElement * element, GstPad * pad, gboolean last,
1448 GstPlayBaseBin * play_base_bin)
1450 new_decoded_pad_full (element, pad, last, play_base_bin, TRUE);
1454 set_encoding_element (GstElement * element, gchar * encoding)
1456 GST_DEBUG_OBJECT (element, "setting encoding to %s", GST_STR_NULL (encoding));
1457 g_object_set (G_OBJECT (element), "subtitle-encoding", encoding, NULL);
1462 decodebin_element_added_cb (GstBin * decodebin, GstElement * element,
1465 GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (data);
1468 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1469 "subtitle-encoding")) {
1473 g_mutex_lock (play_base_bin->sub_lock);
1474 play_base_bin->subtitle_elements =
1475 g_slist_append (play_base_bin->subtitle_elements, element);
1476 encoding = g_strdup (play_base_bin->subencoding);
1477 g_mutex_unlock (play_base_bin->sub_lock);
1479 set_encoding_element (element, encoding);
1484 decodebin_element_removed_cb (GstBin * decodebin, GstElement * element,
1487 GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (data);
1489 g_mutex_lock (play_base_bin->sub_lock);
1490 play_base_bin->subtitle_elements =
1491 g_slist_remove (play_base_bin->subtitle_elements, element);
1492 g_mutex_unlock (play_base_bin->sub_lock);
1497 * Generate source ! subparse bins.
1501 setup_subtitle (GstPlayBaseBin * play_base_bin, gchar * sub_uri)
1503 GstElement *source, *subdecodebin, *subbin;
1505 if (!gst_uri_is_valid (sub_uri))
1508 source = gst_element_make_from_uri (GST_URI_SRC, sub_uri, NULL);
1512 if (g_getenv ("USE_DECODEBIN2"))
1513 subdecodebin = gst_element_factory_make ("decodebin2", "subtitle-decoder");
1515 subdecodebin = gst_element_factory_make ("decodebin", "subtitle-decoder");
1516 g_signal_connect (subdecodebin, "element-added",
1517 G_CALLBACK (decodebin_element_added_cb), play_base_bin);
1518 g_signal_connect (subdecodebin, "element-removed",
1519 G_CALLBACK (decodebin_element_removed_cb), play_base_bin);
1520 subbin = gst_bin_new ("subtitle-bin");
1521 gst_bin_add_many (GST_BIN_CAST (subbin), source, subdecodebin, NULL);
1523 gst_element_link (source, subdecodebin);
1525 /* return the subtitle GstElement object */
1531 GST_ELEMENT_WARNING (play_base_bin, RESOURCE, NOT_FOUND,
1532 (_("Invalid subtitle URI \"%s\", subtitles disabled."), sub_uri),
1538 gchar *prot = gst_uri_get_protocol (sub_uri);
1543 gst_element_post_message (GST_ELEMENT (play_base_bin),
1544 gst_missing_uri_source_message_new (GST_ELEMENT (play_base_bin),
1547 desc = gst_pb_utils_get_source_description (prot);
1548 GST_ELEMENT_ERROR (play_base_bin, CORE, MISSING_PLUGIN,
1549 (_("A %s plugin is required to play this stream, but not installed."),
1550 desc), ("No URI handler to handle sub_uri: %s", sub_uri));
1560 /* helper function to lookup stuff in lists */
1562 array_has_value (const gchar * values[], const gchar * value)
1566 for (i = 0; values[i]; i++) {
1567 if (g_str_has_prefix (value, values[i]))
1573 /* list of URIs that we consider to be streams and that need buffering.
1574 * We have no mechanism yet to figure this out with a query. */
1575 static const gchar *stream_uris[] = { "http://", "mms://", "mmsh://",
1576 "mmsu://", "mmst://", "myth://", NULL
1579 /* blacklisted URIs, we know they will always fail. */
1580 static const gchar *blacklisted_uris[] = { NULL };
1582 /* mime types that we don't consider to be media types */
1583 static const gchar *no_media_mimes[] = {
1584 "application/x-executable", "application/x-bzip", "application/x-gzip",
1585 "application/zip", "application/x-compress", NULL
1588 /* mime types we consider raw media */
1589 static const gchar *raw_mimes[] = {
1590 "audio/x-raw", "video/x-raw", "video/x-dvd-subpicture", NULL
1593 #define IS_STREAM_URI(uri) (array_has_value (stream_uris, uri))
1594 #define IS_BLACKLISTED_URI(uri) (array_has_value (blacklisted_uris, uri))
1595 #define IS_NO_MEDIA_MIME(mime) (array_has_value (no_media_mimes, mime))
1596 #define IS_RAW_MIME(mime) (array_has_value (raw_mimes, mime))
1599 * Generate and configure a source element.
1602 gen_source_element (GstPlayBaseBin * play_base_bin, GstElement ** subbin)
1606 if (!play_base_bin->uri)
1609 if (!gst_uri_is_valid (play_base_bin->uri))
1612 if (IS_BLACKLISTED_URI (play_base_bin->uri))
1613 goto uri_blacklisted;
1615 if (play_base_bin->suburi) {
1616 GST_LOG_OBJECT (play_base_bin, "Creating decoder for subtitles URI %s",
1617 play_base_bin->suburi);
1618 /* subtitle specified */
1619 *subbin = setup_subtitle (play_base_bin, play_base_bin->suburi);
1621 /* no subtitle specified */
1625 source = gst_element_make_from_uri (GST_URI_SRC, play_base_bin->uri,
1630 play_base_bin->is_stream = IS_STREAM_URI (play_base_bin->uri);
1632 /* make HTTP sources send extra headers so we get icecast
1633 * metadata in case the stream is an icecast stream */
1634 if (!strncmp (play_base_bin->uri, "http://", 7) &&
1635 g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1637 g_object_set (source, "iradio-mode", TRUE, NULL);
1640 if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1641 "connection-speed")) {
1642 GST_DEBUG_OBJECT (play_base_bin,
1643 "setting connection-speed=%d to source element",
1644 play_base_bin->connection_speed / 1000);
1645 g_object_set (source, "connection-speed",
1646 play_base_bin->connection_speed / 1000, NULL);
1654 GST_ELEMENT_ERROR (play_base_bin, RESOURCE, NOT_FOUND,
1655 (_("No URI specified to play from.")), (NULL));
1660 GST_ELEMENT_ERROR (play_base_bin, RESOURCE, NOT_FOUND,
1661 (_("Invalid URI \"%s\"."), play_base_bin->uri), (NULL));
1666 GST_ELEMENT_ERROR (play_base_bin, RESOURCE, FAILED,
1667 (_("RTSP streams cannot be played yet.")), (NULL));
1672 gchar *prot = gst_uri_get_protocol (play_base_bin->uri);
1674 /* whoops, could not create the source element, dig a little deeper to
1675 * figure out what might be wrong. */
1679 gst_element_post_message (GST_ELEMENT (play_base_bin),
1680 gst_missing_uri_source_message_new (GST_ELEMENT (play_base_bin),
1683 desc = gst_pb_utils_get_source_description (prot);
1684 GST_ELEMENT_ERROR (play_base_bin, CORE, MISSING_PLUGIN,
1685 (_("A %s plugin is required to play this stream, but not installed."),
1686 desc), ("No URI handler for %s", prot));
1696 /* is called when a dynamic source element created a new pad. */
1698 source_new_pad (GstElement * element, GstPad * pad, GstPlayBaseBin * bin)
1700 GstElement *decoder;
1703 GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
1704 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
1706 /* if this is a pad with all raw caps, we can expose it */
1707 if (has_all_raw_caps (pad, &is_raw) && is_raw) {
1708 bin->raw_decoding_mode = TRUE;
1709 /* it's all raw, create output pads. */
1710 new_decoded_pad_full (element, pad, FALSE, bin, FALSE);
1714 /* not raw, create decoder */
1715 decoder = make_decoder (bin);
1719 /* and link to decoder */
1720 if (!gst_element_link (bin->source, decoder))
1721 goto could_not_link;
1723 gst_element_set_state (decoder, GST_STATE_PAUSED);
1730 /* error was posted */
1735 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
1736 (NULL), ("Can't link source to decoder element"));
1742 * Setup the substreams (is called right after group_commit () when
1743 * loading a new group, or after switching groups).
1745 * Should be called with group-lock held.
1748 setup_substreams (GstPlayBaseBin * play_base_bin)
1750 GstPlayBaseGroup *group;
1754 GST_DEBUG_OBJECT (play_base_bin, "setting up substreams");
1756 /* Remove the eat probes */
1757 group = get_active_group (play_base_bin);
1758 for (item = group->streaminfo; item; item = item->next) {
1759 GstStreamInfo *info = item->data;
1762 data = g_object_get_data (G_OBJECT (info->object), "eat_probe");
1764 gst_pad_remove_data_probe (GST_PAD_CAST (info->object),
1765 GPOINTER_TO_INT (data));
1766 g_object_set_data (G_OBJECT (info->object), "eat_probe", NULL);
1769 /* now remove unknown pads */
1770 if (info->type == GST_STREAM_TYPE_UNKNOWN) {
1773 id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info), "mute_probe"));
1775 id = gst_pad_add_buffer_probe (GST_PAD_CAST (info->object),
1776 G_CALLBACK (mute_stream), info);
1777 g_object_set_data (G_OBJECT (info), "mute_probe", GINT_TO_POINTER (id));
1782 /* now check if the requested current streams exist. If
1783 * current >= num_streams, decrease current so at least
1784 * we have output. Always keep it enabled. */
1785 for (n = 0; n < NUM_TYPES; n++) {
1786 if (play_base_bin->current[n] >= group->type[n].npads) {
1787 GST_DEBUG_OBJECT (play_base_bin, "reset type %d to current 0", n);
1788 play_base_bin->current[n] = 0;
1792 /* now activate the right sources. Don't forget that during preroll,
1793 * we set the first source to forwarding and ignored the rest. */
1794 for (n = 0; n < NUM_TYPES; n++) {
1795 GST_DEBUG_OBJECT (play_base_bin, "setting type %d to current %d", n,
1796 play_base_bin->current[n]);
1797 set_active_source (play_base_bin, n + 1, play_base_bin->current[n]);
1804 * @all_raw: pointer to hold the result
1806 * check if the caps of the pad are all raw. The caps are all raw if
1807 * all of its structures contain audio/x-raw or video/x-raw.
1809 * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1812 has_all_raw_caps (GstPad * pad, gboolean * all_raw)
1816 guint i, num_raw = 0;
1817 gboolean res = FALSE;
1819 caps = gst_pad_get_caps (pad);
1823 capssize = gst_caps_get_size (caps);
1824 /* no caps, skip and move to the next pad */
1825 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1828 /* count the number of raw formats in the caps */
1829 for (i = 0; i < capssize; ++i) {
1831 const gchar *mime_type;
1833 s = gst_caps_get_structure (caps, i);
1834 mime_type = gst_structure_get_name (s);
1836 if (IS_RAW_MIME (mime_type))
1840 *all_raw = (num_raw == capssize);
1844 gst_caps_unref (caps);
1850 * @play_base_bin: a #GstPlayBaseBin
1851 * @is_raw: are all pads raw data
1852 * @have_out: does the source have output
1853 * @is_dynamic: is this a dynamic source
1855 * Check the source of @play_base_bin and collect information about it.
1857 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1858 * function returns, all of the raw pad of the source will be added
1859 * to @play_base_bin.
1861 * @have_out: will be set to TRUE if the source has output pads.
1863 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1866 * Returns: FALSE if a fatal error occured while scanning.
1869 analyse_source (GstPlayBaseBin * play_base_bin, gboolean * is_raw,
1870 gboolean * have_out, gboolean * is_dynamic)
1872 GstIterator *pads_iter;
1873 gboolean done = FALSE;
1874 gboolean res = TRUE;
1878 *is_dynamic = FALSE;
1880 pads_iter = gst_element_iterate_src_pads (play_base_bin->source);
1884 switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
1885 case GST_ITERATOR_ERROR:
1888 case GST_ITERATOR_DONE:
1891 case GST_ITERATOR_RESYNC:
1892 /* reset results and resync */
1895 *is_dynamic = FALSE;
1896 gst_iterator_resync (pads_iter);
1898 case GST_ITERATOR_OK:
1899 /* we now officially have an output pad */
1902 /* if FALSE, this pad has no caps and we continue with the next pad. */
1903 if (!has_all_raw_caps (pad, is_raw)) {
1904 gst_object_unref (pad);
1908 /* caps on source pad are all raw, we can add the pad */
1910 new_decoded_pad_full (play_base_bin->source, pad, FALSE,
1911 play_base_bin, FALSE);
1914 gst_object_unref (pad);
1918 gst_iterator_free (pads_iter);
1921 GstElementClass *elemclass;
1924 /* element has no output pads, check for padtemplates that list SOMETIMES
1926 elemclass = GST_ELEMENT_GET_CLASS (play_base_bin->source);
1928 walk = gst_element_class_get_pad_template_list (elemclass);
1929 while (walk != NULL) {
1930 GstPadTemplate *templ;
1932 templ = (GstPadTemplate *) walk->data;
1933 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1934 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES) {
1936 break; /* only break out if we found a sometimes src pad
1937 continue walking through if say a request src pad is found
1938 elements such as mpegtsparse and dvbbasebin have request
1939 and sometimes src pads */
1942 walk = g_list_next (walk);
1950 remove_decoders (GstPlayBaseBin * bin)
1954 for (walk = bin->decoders; walk; walk = g_slist_next (walk)) {
1955 GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1957 GST_DEBUG_OBJECT (bin, "removing old decoder element");
1958 /* Disconnect all the signal handlers we attached to the decodebin before
1959 * we dispose of it */
1960 g_signal_handlers_disconnect_by_func (decoder,
1961 (gpointer) (decodebin_element_added_cb), bin);
1962 g_signal_handlers_disconnect_by_func (decoder,
1963 (gpointer) (decodebin_element_removed_cb), bin);
1964 g_signal_handlers_disconnect_by_func (decoder,
1965 (gpointer) (new_decoded_pad), bin);
1966 g_signal_handlers_disconnect_by_func (decoder,
1967 (gpointer) (no_more_pads), bin);
1968 g_signal_handlers_disconnect_by_func (decoder,
1969 (gpointer) (unknown_type), bin);
1971 gst_element_set_state (decoder, GST_STATE_NULL);
1972 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1974 g_slist_free (bin->decoders);
1975 bin->decoders = NULL;
1979 make_decoder (GstPlayBaseBin * play_base_bin)
1981 GstElement *decoder;
1983 /* now create the decoder element */
1984 if (g_getenv ("USE_DECODEBIN2"))
1985 decoder = gst_element_factory_make ("decodebin2", NULL);
1987 decoder = gst_element_factory_make ("decodebin", NULL);
1991 g_signal_connect (decoder, "element-added",
1992 G_CALLBACK (decodebin_element_added_cb), play_base_bin);
1993 g_signal_connect (decoder, "element-removed",
1994 G_CALLBACK (decodebin_element_removed_cb), play_base_bin);
1996 gst_bin_add (GST_BIN_CAST (play_base_bin), decoder);
1998 /* set up callbacks to create the links between decoded data
1999 * and video/audio/subtitle rendering/output. */
2000 g_signal_connect (G_OBJECT (decoder),
2001 "new-decoded-pad", G_CALLBACK (new_decoded_pad), play_base_bin);
2002 g_signal_connect (G_OBJECT (decoder), "no-more-pads",
2003 G_CALLBACK (no_more_pads), play_base_bin);
2004 g_signal_connect (G_OBJECT (decoder),
2005 "unknown-type", G_CALLBACK (unknown_type), play_base_bin);
2006 g_object_set_data (G_OBJECT (decoder), "pending", GINT_TO_POINTER (1));
2007 play_base_bin->pending++;
2009 GST_DEBUG_OBJECT (play_base_bin, "created decodebin, %d pending",
2010 play_base_bin->pending);
2012 play_base_bin->decoders = g_slist_prepend (play_base_bin->decoders, decoder);
2019 GST_ELEMENT_ERROR (play_base_bin, CORE, MISSING_PLUGIN,
2020 (_("Could not create \"decodebin\" element.")), (NULL));
2026 remove_source (GstPlayBaseBin * bin)
2028 GstElement *source = bin->source;
2031 GST_DEBUG_OBJECT (bin, "removing old src element");
2032 gst_element_set_state (source, GST_STATE_NULL);
2034 if (bin->src_np_sig_id) {
2035 g_signal_handler_disconnect (G_OBJECT (source), bin->src_np_sig_id);
2036 bin->src_np_sig_id = 0;
2038 if (bin->src_nmp_sig_id) {
2039 g_signal_handler_disconnect (G_OBJECT (source), bin->src_nmp_sig_id);
2040 bin->src_nmp_sig_id = 0;
2042 gst_bin_remove (GST_BIN_CAST (bin), source);
2047 static GstBusSyncReply
2048 subbin_startup_sync_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
2050 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
2051 GstPlayBaseBin *play_base_bin;
2053 play_base_bin = GST_PLAY_BASE_BIN (user_data);
2054 if (!play_base_bin->subtitle_done) {
2055 GST_WARNING_OBJECT (play_base_bin, "error starting up subtitle bin: %"
2056 GST_PTR_FORMAT, msg);
2057 play_base_bin->subtitle_done = TRUE;
2058 GST_DEBUG_OBJECT (play_base_bin, "signal group done");
2059 GROUP_SIGNAL (play_base_bin);
2060 GST_DEBUG_OBJECT (play_base_bin, "signaled group done");
2063 return GST_BUS_PASS;
2066 /* construct and run the source and decoder elements until we found
2067 * all the streams or until a preroll queue has been filled.
2070 setup_source (GstPlayBaseBin * play_base_bin)
2072 GstElement *subbin = NULL;
2073 gboolean is_raw, have_out, is_dynamic;
2075 if (!play_base_bin->need_rebuild)
2077 play_base_bin->raw_decoding_mode = FALSE;
2079 GST_DEBUG_OBJECT (play_base_bin, "setup source");
2081 /* delete old src */
2082 remove_source (play_base_bin);
2084 /* create and configure an element that can handle the uri */
2085 if (!(play_base_bin->source = gen_source_element (play_base_bin, &subbin)))
2088 /* state will be merged later - if file is not found, error will be
2089 * handled by the application right after. */
2090 gst_bin_add (GST_BIN_CAST (play_base_bin), play_base_bin->source);
2091 g_object_notify (G_OBJECT (play_base_bin), "source");
2093 /* remove the old decoders now, if any */
2094 remove_decoders (play_base_bin);
2096 /* remove our previous preroll queues */
2097 remove_groups (play_base_bin);
2099 /* clear pending dynamic elements */
2100 play_base_bin->pending = 0;
2107 play_base_bin->subtitle = subbin;
2108 db = gst_bin_get_by_name (GST_BIN_CAST (subbin), "subtitle-decoder");
2110 /* do type detection, without adding (so no preroll) */
2111 g_signal_connect (G_OBJECT (db), "new-decoded-pad",
2112 G_CALLBACK (subs_new_decoded_pad), play_base_bin);
2113 g_signal_connect (G_OBJECT (db), "no-more-pads",
2114 G_CALLBACK (sub_no_more_pads), play_base_bin);
2115 g_signal_connect (G_OBJECT (db), "unknown-type",
2116 G_CALLBACK (unknown_type), play_base_bin);
2117 g_object_set_data (G_OBJECT (db), "pending", GINT_TO_POINTER (1));
2118 play_base_bin->pending++;
2120 GST_DEBUG_OBJECT (play_base_bin, "we have subtitles, %d pending",
2121 play_base_bin->pending);
2123 if (!play_base_bin->is_stream) {
2124 GstStateChangeReturn sret;
2126 /* either when the queues are filled or when the decoder element
2127 * has no more dynamic streams, the cond is unlocked. We can remove
2128 * the signal handlers then
2130 GST_DEBUG_OBJECT (play_base_bin, "starting subtitle bin");
2132 /* for subtitles in a separate bin we will not commit the
2133 * current building group since we need to add the other
2134 * audio/video streams to the group. We check if we managed
2135 * to commit the subtitle group using an extra flag. */
2136 play_base_bin->subtitle_done = FALSE;
2138 /* since subbin is still a stand-alone bin, we need to add a custom bus
2139 * to intercept error messages, so we can stop waiting and continue */
2140 bus = gst_bus_new ();
2141 gst_element_set_bus (subbin, bus);
2142 gst_bus_set_sync_handler (bus, subbin_startup_sync_msg, play_base_bin);
2144 sret = gst_element_set_state (subbin, GST_STATE_PAUSED);
2145 if (sret != GST_STATE_CHANGE_FAILURE) {
2146 GROUP_LOCK (play_base_bin);
2147 GST_DEBUG ("waiting for subtitle to complete...");
2148 while (!play_base_bin->subtitle_done)
2149 GROUP_WAIT (play_base_bin);
2150 GST_DEBUG ("group done !");
2151 GROUP_UNLOCK (play_base_bin);
2153 if (!play_base_bin->building_group ||
2154 play_base_bin->building_group->type[GST_STREAM_TYPE_TEXT -
2157 GST_DEBUG ("No subtitle found - ignoring");
2158 gst_element_set_state (subbin, GST_STATE_NULL);
2159 gst_object_unref (play_base_bin->subtitle);
2160 play_base_bin->subtitle = NULL;
2162 GST_DEBUG_OBJECT (play_base_bin, "Subtitle set-up successful");
2165 GST_WARNING_OBJECT (play_base_bin, "Failed to start subtitle bin");
2166 gst_element_set_state (subbin, GST_STATE_NULL);
2167 gst_object_unref (play_base_bin->subtitle);
2168 play_base_bin->subtitle = NULL;
2171 gst_bus_set_sync_handler (bus, NULL, NULL);
2172 gst_element_set_bus (subbin, NULL);
2173 gst_object_unref (bus);
2175 gst_object_unref (db);
2177 /* see if the source element emits raw audio/video all by itself,
2178 * if so, we can create streams for the pads and be done with it.
2179 * Also check that is has source pads, if not, we assume it will
2180 * do everything itself. */
2181 if (!analyse_source (play_base_bin, &is_raw, &have_out, &is_dynamic))
2182 goto invalid_source;
2185 GST_DEBUG_OBJECT (play_base_bin, "Source provides all raw data");
2186 /* source provides raw data, we added the pads and we can now signal a
2187 * no_more pads because we are done. */
2188 group_commit (play_base_bin, play_base_bin->is_stream, FALSE);
2191 if (!have_out && !is_dynamic) {
2192 GST_DEBUG_OBJECT (play_base_bin, "Source has no output pads");
2193 /* create a stream to indicate that this uri is handled by a self
2194 * contained element. We are now done. */
2195 add_element_stream (play_base_bin->source, play_base_bin);
2196 group_commit (play_base_bin, play_base_bin->is_stream, FALSE);
2200 /* connect a handler for the new-pad signal */
2201 play_base_bin->src_np_sig_id =
2202 g_signal_connect (G_OBJECT (play_base_bin->source), "pad-added",
2203 G_CALLBACK (source_new_pad), play_base_bin);
2204 play_base_bin->src_nmp_sig_id =
2205 g_signal_connect (G_OBJECT (play_base_bin->source), "no-more-pads",
2206 G_CALLBACK (source_no_more_pads), play_base_bin);
2207 g_object_set_data (G_OBJECT (play_base_bin->source), "pending",
2208 GINT_TO_POINTER (1));
2209 play_base_bin->pending++;
2210 GST_DEBUG_OBJECT (play_base_bin,
2211 "Source has dynamic output pads, %d pending", play_base_bin->pending);
2213 GstElement *decoder;
2215 /* no dynamic source, we can link now */
2216 decoder = make_decoder (play_base_bin);
2220 if (!gst_element_link (play_base_bin->source, decoder))
2221 goto could_not_link;
2224 if (play_base_bin->subtitle)
2225 gst_bin_add (GST_BIN_CAST (play_base_bin), play_base_bin->subtitle);
2227 play_base_bin->need_rebuild = FALSE;
2234 /* error message was already posted */
2239 GST_ELEMENT_ERROR (play_base_bin, CORE, FAILED,
2240 (_("Source element is invalid.")), (NULL));
2245 /* message was posted */
2250 GST_ELEMENT_ERROR (play_base_bin, CORE, NEGOTIATION,
2251 (NULL), ("Can't link source to decoder element"));
2257 finish_source (GstPlayBaseBin * play_base_bin)
2259 /* FIXME: no need to grab the group lock here? (tpm) */
2260 if (get_active_group (play_base_bin) != NULL) {
2261 if (play_base_bin->subtitle) {
2262 /* make subs iterate from now on */
2263 gst_bin_add (GST_BIN_CAST (play_base_bin), play_base_bin->subtitle);
2269 * Caller must have group-lock held.
2271 * We iterate over all detected streams in the streaminfo and try to find
2272 * impossible cases, like subtitles without video.
2275 prepare_output (GstPlayBaseBin * play_base_bin)
2278 gboolean stream_found = FALSE, no_media = FALSE;
2279 gboolean got_video = FALSE, got_subtitle = FALSE;
2280 GstPlayBaseGroup *group;
2282 group = get_active_group (play_base_bin);
2284 /* check if we found any supported stream... if not, then
2285 * we detected stream type (or the above would've failed),
2286 * but linking/decoding failed - plugin probably missing. */
2287 for (item = group ? group->streaminfo : NULL; item != NULL; item = item->next) {
2288 GstStreamInfo *info = GST_STREAM_INFO (item->data);
2290 if (info->type == GST_STREAM_TYPE_VIDEO) {
2291 stream_found = TRUE;
2294 } else if (info->type == GST_STREAM_TYPE_ELEMENT) {
2295 stream_found = TRUE;
2296 } else if (info->type == GST_STREAM_TYPE_AUDIO) {
2297 stream_found = TRUE;
2298 } else if (info->type == GST_STREAM_TYPE_TEXT ||
2299 info->type == GST_STREAM_TYPE_SUBPICTURE) {
2300 got_subtitle = TRUE;
2301 } else if (!item->prev && !item->next) {
2302 /* We're no audio/video and the only stream... We could
2303 * be something not-media that's detected because then our
2304 * typefind doesn't mess up with mp3 (bz2, gz, elf, ...) */
2305 if (info->caps && !gst_caps_is_empty (info->caps)) {
2307 gst_structure_get_name (gst_caps_get_structure (info->caps, 0));
2309 no_media = IS_NO_MEDIA_MIME (mime);
2314 if (!stream_found) {
2316 GST_ELEMENT_ERROR (play_base_bin, STREAM, WRONG_TYPE,
2317 (_("Only a subtitle stream was detected. "
2318 "Either you are loading a subtitle file or some other type of "
2319 "text file, or the media file was not recognized.")), (NULL));
2320 } else if (!no_media) {
2321 GST_ELEMENT_ERROR (play_base_bin, STREAM, CODEC_NOT_FOUND,
2322 (_("You do not have a decoder installed to handle this file. "
2323 "You might need to install the necessary plugins.")), (NULL));
2325 GST_ELEMENT_ERROR (play_base_bin, STREAM, WRONG_TYPE,
2326 (_("This is not a media file")), (NULL));
2329 } else if (got_subtitle && !got_video) {
2330 GST_ELEMENT_ERROR (play_base_bin, STREAM, WRONG_TYPE,
2331 (_("A subtitle stream was detected, but no video stream.")), (NULL));
2339 * Multi-stream management. -1 = none.
2341 * Caller has group-lock held.
2344 get_active_source (GstPlayBaseBin * play_base_bin, GstStreamType type)
2346 GstPlayBaseGroup *group;
2350 group = get_active_group (play_base_bin);
2354 for (s = group->streaminfo; s; s = s->next) {
2355 GstStreamInfo *info = s->data;
2357 if (info->type == type) {
2358 if (!info->mute && !g_object_get_data (G_OBJECT (info), "mute_probe")) {
2369 /* Kill pad reactivation on state change. */
2372 static void muted_group_change_state (GstElement * element,
2373 gint old_state, gint new_state, gpointer data);
2377 mute_group_type (GstPlayBaseGroup * group, GstStreamType type, gboolean mute)
2379 gboolean active = !mute;
2382 pad = gst_element_get_static_pad (group->type[type - 1].preroll, "src");
2383 gst_pad_set_active (pad, active);
2384 gst_object_unref (pad);
2385 pad = gst_element_get_static_pad (group->type[type - 1].preroll, "sink");
2386 gst_pad_set_active (pad, active);
2387 gst_object_unref (pad);
2388 pad = gst_element_get_static_pad (group->type[type - 1].selector, "src");
2389 gst_pad_set_active (pad, active);
2390 gst_object_unref (pad);
2394 g_signal_connect (group->type[type - 1].preroll, "state-changed",
2395 G_CALLBACK (muted_group_change_state), group);
2397 g_signal_handlers_disconnect_by_func (group->type[type - 1].preroll,
2398 G_CALLBACK (muted_group_change_state), group);
2405 muted_group_change_state (GstElement * element,
2406 gint old_state, gint new_state, gpointer data)
2408 GstPlayBaseGroup *group = data;
2410 GROUP_LOCK (group->bin);
2412 if (new_state == GST_STATE_PLAYING) {
2415 for (n = 0; n < NUM_TYPES; n++) {
2416 if (group->type[n].selector == element) {
2417 mute_group_type (group, n + 1, TRUE);
2422 GROUP_UNLOCK (group->bin);
2427 set_subtitles_visible (GstPlayBaseBin * play_base_bin, gboolean visible)
2429 GstPlayBaseBinClass *klass = GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin);
2431 /* we use a vfunc for this since we don't have a reference to the
2432 * textoverlay element, but playbin does */
2433 if (klass != NULL && klass->set_subtitles_visible != NULL)
2434 klass->set_subtitles_visible (play_base_bin, visible);
2438 set_audio_mute (GstPlayBaseBin * play_base_bin, gboolean mute)
2440 GstPlayBaseBinClass *klass = GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin);
2442 /* we use a vfunc for this since we don't have a reference to the
2443 * textoverlay element, but playbin does */
2444 if (klass != NULL && klass->set_audio_mute != NULL)
2445 klass->set_audio_mute (play_base_bin, mute);
2449 * Caller has group-lock held.
2453 set_active_source (GstPlayBaseBin * play_base_bin,
2454 GstStreamType type, gint source_num)
2456 GstPlayBaseGroup *group;
2459 gboolean have_active = FALSE;
2462 GST_LOG ("Changing active source of type %d to %d", type, source_num);
2463 play_base_bin->current[type - 1] = source_num;
2465 group = get_active_group (play_base_bin);
2466 if (!group || !group->type[type - 1].preroll) {
2467 GST_LOG ("No active group, or group for type %d has no preroll", type);
2471 /* HACK: instead of unlinking the subtitle input (= lots of hassle,
2472 * especially if subtitles come from an external source), just tell
2473 * textoverlay not to render them */
2474 if (type == GST_STREAM_TYPE_TEXT) {
2475 gboolean visible = (source_num != -1);
2477 set_subtitles_visible (play_base_bin, visible);
2480 } else if (type == GST_STREAM_TYPE_AUDIO) {
2481 gboolean mute = (source_num == -1);
2483 set_audio_mute (play_base_bin, mute);
2489 sel = group->type[type - 1].selector;
2491 for (s = group->streaminfo; s; s = s->next) {
2492 GstStreamInfo *info = s->data;
2494 if (info->type == type) {
2495 if (num == source_num) {
2498 GST_LOG ("Unmuting (if already muted) source %d of type %d", source_num,
2500 g_object_set (info, "mute", FALSE, NULL);
2502 /* Tell the stream selector which pad to accept */
2503 sel_pad = GST_PAD_CAST (g_object_get_data (G_OBJECT (info->object),
2506 if (sel && sel_pad != NULL) {
2507 g_object_set (G_OBJECT (sel), "active-pad", sel_pad, NULL);
2514 GST_LOG_OBJECT (info->object, "Muting source %d of type %d", num, type);
2516 id = gst_pad_add_buffer_probe (GST_PAD_CAST (info->object),
2517 G_CALLBACK (mute_stream), info);
2518 g_object_set_data (G_OBJECT (info), "mute_probe", GINT_TO_POINTER (id));
2525 GST_LOG ("Muting group type: %d", type);
2526 g_object_set (sel, "active-pad", NULL, NULL);
2528 GST_LOG ("Unmuting group type: %d", type);
2530 mute_group_type (group, type, !have_active);
2534 gst_play_base_bin_set_property (GObject * object, guint prop_id,
2535 const GValue * value, GParamSpec * pspec)
2537 GstPlayBaseBin *play_base_bin;
2539 g_return_if_fail (GST_IS_PLAY_BASE_BIN (object));
2541 play_base_bin = GST_PLAY_BASE_BIN (object);
2546 const gchar *uri = g_value_get_string (value);
2549 g_warning ("cannot set NULL uri");
2552 /* if we have no previous uri, or the new uri is different from the
2553 * old one, replug */
2554 if (play_base_bin->uri == NULL || strcmp (play_base_bin->uri, uri) != 0) {
2555 g_free (play_base_bin->uri);
2556 play_base_bin->uri = g_strdup (uri);
2558 GST_DEBUG ("setting new uri to %s", uri);
2560 play_base_bin->need_rebuild = TRUE;
2565 const gchar *suburi = g_value_get_string (value);
2567 if ((!suburi && !play_base_bin->suburi) ||
2568 (suburi && play_base_bin->suburi &&
2569 !strcmp (play_base_bin->suburi, suburi)))
2571 g_free (play_base_bin->suburi);
2572 play_base_bin->suburi = g_strdup (suburi);
2573 GST_DEBUG ("setting new .sub uri to %s", suburi);
2574 play_base_bin->need_rebuild = TRUE;
2577 case ARG_QUEUE_SIZE:
2578 play_base_bin->queue_size = g_value_get_uint64 (value);
2580 case ARG_QUEUE_THRESHOLD:
2581 play_base_bin->queue_threshold = g_value_get_uint64 (value);
2583 case ARG_QUEUE_MIN_THRESHOLD:
2584 play_base_bin->queue_min_threshold = g_value_get_uint64 (value);
2586 case ARG_CONNECTION_SPEED:
2587 play_base_bin->connection_speed = g_value_get_uint (value) * 1000;
2590 GROUP_LOCK (play_base_bin);
2591 set_active_source (play_base_bin,
2592 GST_STREAM_TYPE_VIDEO, g_value_get_int (value));
2593 GROUP_UNLOCK (play_base_bin);
2596 GROUP_LOCK (play_base_bin);
2597 set_active_source (play_base_bin,
2598 GST_STREAM_TYPE_AUDIO, g_value_get_int (value));
2599 GROUP_UNLOCK (play_base_bin);
2602 GROUP_LOCK (play_base_bin);
2603 set_active_source (play_base_bin,
2604 GST_STREAM_TYPE_TEXT, g_value_get_int (value));
2605 GROUP_UNLOCK (play_base_bin);
2607 case ARG_SUBTITLE_ENCODING:
2609 const gchar *encoding;
2612 encoding = g_value_get_string (value);
2613 if (encoding && play_base_bin->subencoding &&
2614 !strcmp (play_base_bin->subencoding, encoding)) {
2617 if (encoding == NULL && play_base_bin->subencoding == NULL)
2620 g_mutex_lock (play_base_bin->sub_lock);
2621 g_free (play_base_bin->subencoding);
2622 play_base_bin->subencoding = g_strdup (encoding);
2623 list = g_slist_copy (play_base_bin->subtitle_elements);
2624 g_slist_foreach (list, (GFunc) gst_object_ref, NULL);
2625 g_mutex_unlock (play_base_bin->sub_lock);
2627 /* we can't hold a lock when calling g_object_set() on a child, since
2628 * the notify event will trigger GstObject to send a deep-notify event
2629 * which will try to take the lock ... */
2630 g_slist_foreach (list, (GFunc) set_encoding_element, (gpointer) encoding);
2631 g_slist_foreach (list, (GFunc) gst_object_unref, NULL);
2632 g_slist_free (list);
2636 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2642 gst_play_base_bin_get_property (GObject * object, guint prop_id, GValue * value,
2645 GstPlayBaseBin *play_base_bin;
2647 g_return_if_fail (GST_IS_PLAY_BASE_BIN (object));
2649 play_base_bin = GST_PLAY_BASE_BIN (object);
2653 g_value_set_string (value, play_base_bin->uri);
2656 g_value_set_string (value, play_base_bin->suburi);
2660 GstPlayBaseGroup *group;
2662 GROUP_LOCK (play_base_bin);
2663 group = get_active_group (play_base_bin);
2665 g_value_set_int (value, group->nstreams);
2667 g_value_set_int (value, 0);
2669 GROUP_UNLOCK (play_base_bin);
2672 case ARG_QUEUE_SIZE:
2673 g_value_set_uint64 (value, play_base_bin->queue_size);
2675 case ARG_QUEUE_THRESHOLD:
2676 g_value_set_uint64 (value, play_base_bin->queue_threshold);
2678 case ARG_QUEUE_MIN_THRESHOLD:
2679 g_value_set_uint64 (value, play_base_bin->queue_min_threshold);
2681 case ARG_CONNECTION_SPEED:
2682 g_value_set_uint (value, play_base_bin->connection_speed / 1000);
2684 case ARG_STREAMINFO:
2685 /* FIXME: hold some kind of lock here, use iterator */
2686 g_value_set_pointer (value,
2687 (gpointer) gst_play_base_bin_get_streaminfo (play_base_bin));
2689 case ARG_STREAMINFO_VALUES:{
2692 copy = gst_play_base_bin_get_streaminfo_value_array (play_base_bin);
2693 g_value_take_boxed (value, copy);
2697 g_value_set_object (value, play_base_bin->source);
2700 GROUP_LOCK (play_base_bin);
2701 g_value_set_int (value, get_active_source (play_base_bin,
2702 GST_STREAM_TYPE_VIDEO));
2703 GROUP_UNLOCK (play_base_bin);
2706 GROUP_LOCK (play_base_bin);
2707 g_value_set_int (value, get_active_source (play_base_bin,
2708 GST_STREAM_TYPE_AUDIO));
2709 GROUP_UNLOCK (play_base_bin);
2712 GROUP_LOCK (play_base_bin);
2713 g_value_set_int (value, get_active_source (play_base_bin,
2714 GST_STREAM_TYPE_TEXT));
2715 GROUP_UNLOCK (play_base_bin);
2717 case ARG_SUBTITLE_ENCODING:
2718 GST_OBJECT_LOCK (play_base_bin);
2719 g_value_set_string (value, play_base_bin->subencoding);
2720 GST_OBJECT_UNLOCK (play_base_bin);
2723 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2728 static GstStateChangeReturn
2729 gst_play_base_bin_change_state (GstElement * element, GstStateChange transition)
2731 GstStateChangeReturn ret;
2732 GstPlayBaseBin *play_base_bin;
2734 play_base_bin = GST_PLAY_BASE_BIN (element);
2736 switch (transition) {
2737 case GST_STATE_CHANGE_READY_TO_PAUSED:
2738 if (!setup_source (play_base_bin))
2745 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2747 switch (transition) {
2748 case GST_STATE_CHANGE_READY_TO_PAUSED:
2749 if (ret == GST_STATE_CHANGE_FAILURE)
2750 goto cleanup_groups;
2752 finish_source (play_base_bin);
2754 /* clean-up in both cases, READY=>NULL clean-up is if there was an error */
2755 case GST_STATE_CHANGE_PAUSED_TO_READY:
2756 case GST_STATE_CHANGE_READY_TO_NULL:
2757 play_base_bin->need_rebuild = TRUE;
2758 remove_decoders (play_base_bin);
2759 remove_groups (play_base_bin);
2760 remove_source (play_base_bin);
2770 play_base_bin->need_rebuild = TRUE;
2772 return GST_STATE_CHANGE_FAILURE;
2776 /* clean up leftover groups */
2777 remove_groups (play_base_bin);
2778 play_base_bin->need_rebuild = TRUE;
2780 return GST_STATE_CHANGE_FAILURE;
2784 static const GList *
2785 gst_play_base_bin_get_streaminfo (GstPlayBaseBin * play_base_bin)
2787 GstPlayBaseGroup *group = get_active_group (play_base_bin);
2791 info = group->streaminfo;
2796 static GValueArray *
2797 gst_play_base_bin_get_streaminfo_value_array (GstPlayBaseBin * play_base_bin)
2799 GstPlayBaseGroup *group;
2800 GValueArray *array = NULL;
2802 GROUP_LOCK (play_base_bin);
2803 group = get_active_group (play_base_bin);
2805 array = g_value_array_copy (group->streaminfo_value_array);
2807 array = g_value_array_new (0);
2809 GROUP_UNLOCK (play_base_bin);