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 GST_DEBUG_CATEGORY_STATIC (gst_play_base_bin_debug);
33 #define GST_CAT_DEFAULT gst_play_base_bin_debug
35 #define DEFAULT_QUEUE_SIZE (3 * GST_SECOND)
36 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
37 #define DEFAULT_QUEUE_THRESHOLD ((DEFAULT_QUEUE_SIZE * 95) / 100)
38 #define DEFAULT_CONNECTION_SPEED 0
40 #define GROUP_LOCK(pbb) g_mutex_lock (pbb->group_lock)
41 #define GROUP_UNLOCK(pbb) g_mutex_unlock (pbb->group_lock)
42 #define GROUP_WAIT(pbb) g_cond_wait (pbb->group_cond, pbb->group_lock)
43 #define GROUP_SIGNAL(pbb) g_cond_signal (pbb->group_cond)
53 ARG_QUEUE_MIN_THRESHOLD,
56 ARG_STREAMINFO_VALUES,
61 ARG_SUBTITLE_ENCODING,
65 static void gst_play_base_bin_class_init (GstPlayBaseBinClass * klass);
66 static void gst_play_base_bin_init (GstPlayBaseBin * play_base_bin);
67 static void gst_play_base_bin_dispose (GObject * object);
68 static void gst_play_base_bin_finalize (GObject * object);
70 static void gst_play_base_bin_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * spec);
72 static void gst_play_base_bin_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * spec);
74 static void gst_play_base_bin_handle_message_func (GstBin * bin,
77 static GstStateChangeReturn gst_play_base_bin_change_state (GstElement *
78 element, GstStateChange transition);
80 static const GList *gst_play_base_bin_get_streaminfo (GstPlayBaseBin * bin);
81 static GValueArray *gst_play_base_bin_get_streaminfo_value_array (GstPlayBaseBin
83 static void preroll_remove_overrun (GstElement * element,
84 GstPlayBaseBin * play_base_bin);
85 static void queue_remove_probe (GstElement * queue, GstPlayBaseBin
88 static GstElement *make_decoder (GstPlayBaseBin * play_base_bin);
89 static gboolean has_all_raw_caps (GstPad * pad, gboolean * all_raw);
91 static gboolean prepare_output (GstPlayBaseBin * play_base_bin);
92 static void set_active_source (GstPlayBaseBin * play_base_bin,
93 GstStreamType type, gint source_num);
94 static gboolean probe_triggered (GstPad * pad, GstEvent * event,
96 static void setup_substreams (GstPlayBaseBin * play_base_bin);
98 static GstPipelineClass *parent_class;
101 * GObject playbasebin wrappers.
105 gst_play_base_bin_get_type (void)
107 static GType gst_play_base_bin_type = 0;
109 if (!gst_play_base_bin_type) {
110 static const GTypeInfo gst_play_base_bin_info = {
111 sizeof (GstPlayBaseBinClass),
114 (GClassInitFunc) gst_play_base_bin_class_init,
117 sizeof (GstPlayBaseBin),
119 (GInstanceInitFunc) gst_play_base_bin_init,
123 gst_play_base_bin_type = g_type_register_static (GST_TYPE_PIPELINE,
124 "GstPlayBaseBin", &gst_play_base_bin_info, 0);
127 return gst_play_base_bin_type;
131 gst_play_base_bin_class_init (GstPlayBaseBinClass * klass)
133 GObjectClass *gobject_klass;
134 GstElementClass *gstelement_klass;
135 GstBinClass *gstbin_klass;
137 gobject_klass = (GObjectClass *) klass;
138 gstelement_klass = (GstElementClass *) klass;
139 gstbin_klass = (GstBinClass *) klass;
141 parent_class = g_type_class_peek_parent (klass);
143 gobject_klass->set_property = gst_play_base_bin_set_property;
144 gobject_klass->get_property = gst_play_base_bin_get_property;
146 g_object_class_install_property (gobject_klass, ARG_URI,
147 g_param_spec_string ("uri", "URI", "URI of the media to play",
148 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 g_object_class_install_property (gobject_klass, ARG_SUBURI,
150 g_param_spec_string ("suburi", ".sub-URI", "Optional URI of a subtitle",
151 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 g_object_class_install_property (gobject_klass, ARG_QUEUE_SIZE,
154 g_param_spec_uint64 ("queue-size", "Queue size",
155 "Size of internal queues in nanoseconds", 0, G_MAXINT64,
156 DEFAULT_QUEUE_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_klass, ARG_QUEUE_THRESHOLD,
158 g_param_spec_uint64 ("queue-threshold", "Queue threshold",
159 "Buffering threshold of internal queues in nanoseconds", 0,
160 G_MAXINT64, DEFAULT_QUEUE_THRESHOLD,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_klass, ARG_QUEUE_MIN_THRESHOLD,
163 g_param_spec_uint64 ("queue-min-threshold", "Queue min threshold",
164 "Buffering low threshold of internal queues in nanoseconds", 0,
165 G_MAXINT64, DEFAULT_QUEUE_MIN_THRESHOLD,
166 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_klass, ARG_NSTREAMS,
169 g_param_spec_int ("nstreams", "NStreams", "number of streams",
170 0, G_MAXINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
171 g_object_class_install_property (gobject_klass, ARG_STREAMINFO,
172 g_param_spec_pointer ("stream-info", "Stream info", "List of streaminfo",
173 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (gobject_klass, ARG_STREAMINFO_VALUES,
175 g_param_spec_value_array ("stream-info-value-array",
176 "StreamInfo GValueArray", "value array of streaminfo",
177 g_param_spec_object ("streaminfo", "StreamInfo", "Streaminfo object",
178 GST_TYPE_STREAM_INFO, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS),
179 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (gobject_klass, ARG_SOURCE,
181 g_param_spec_object ("source", "Source", "Source element",
182 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_klass, ARG_VIDEO,
185 g_param_spec_int ("current-video", "Current video",
186 "Currently playing video stream (-1 = none)",
187 -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (gobject_klass, ARG_AUDIO,
189 g_param_spec_int ("current-audio", "Current audio",
190 "Currently playing audio stream (-1 = none)",
191 -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_klass, ARG_TEXT,
193 g_param_spec_int ("current-text", "Current text",
194 "Currently playing text stream (-1 = none)",
195 -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (gobject_klass, ARG_SUBTITLE_ENCODING,
197 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
198 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
199 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
200 "be checked for an encoding to use. If that is not set either, "
201 "ISO-8859-15 will be assumed.", NULL,
202 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 * GstPlayBaseBin:connection-speed
206 * Network connection speed in kbps (0 = unknown)
208 * Since version 0.10.10 in #GstPlayBin, at 0.10.15 moved to #GstPlayBaseBin
213 g_object_class_install_property (gobject_klass, ARG_CONNECTION_SPEED,
214 g_param_spec_uint ("connection-speed", "Connection Speed",
215 "Network connection speed in kbps (0 = unknown)",
216 0, G_MAXUINT, DEFAULT_CONNECTION_SPEED,
217 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 GST_DEBUG_CATEGORY_INIT (gst_play_base_bin_debug, "playbasebin", 0,
222 gobject_klass->dispose = gst_play_base_bin_dispose;
223 gobject_klass->finalize = gst_play_base_bin_finalize;
225 gstbin_klass->handle_message =
226 GST_DEBUG_FUNCPTR (gst_play_base_bin_handle_message_func);
228 gstelement_klass->change_state =
229 GST_DEBUG_FUNCPTR (gst_play_base_bin_change_state);
233 gst_play_base_bin_init (GstPlayBaseBin * play_base_bin)
235 play_base_bin->uri = NULL;
236 play_base_bin->suburi = NULL;
237 play_base_bin->need_rebuild = TRUE;
238 play_base_bin->is_stream = FALSE;
239 play_base_bin->source = NULL;
240 play_base_bin->decoders = NULL;
241 play_base_bin->subtitle = NULL;
242 play_base_bin->subencoding = NULL;
243 play_base_bin->subtitle_elements = NULL;
244 play_base_bin->sub_lock = g_mutex_new ();
246 play_base_bin->group_lock = g_mutex_new ();
247 play_base_bin->group_cond = g_cond_new ();
249 play_base_bin->building_group = NULL;
250 play_base_bin->queued_groups = NULL;
252 play_base_bin->queue_size = DEFAULT_QUEUE_SIZE;
253 play_base_bin->queue_threshold = DEFAULT_QUEUE_THRESHOLD;
254 play_base_bin->queue_min_threshold = DEFAULT_QUEUE_MIN_THRESHOLD;
255 play_base_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
259 gst_play_base_bin_dispose (GObject * object)
261 GstPlayBaseBin *play_base_bin;
263 play_base_bin = GST_PLAY_BASE_BIN (object);
264 g_free (play_base_bin->uri);
265 play_base_bin->uri = NULL;
266 g_free (play_base_bin->suburi);
267 play_base_bin->suburi = NULL;
268 g_free (play_base_bin->subencoding);
269 play_base_bin->subencoding = NULL;
270 if (play_base_bin->subtitle_elements) {
271 g_slist_free (play_base_bin->subtitle_elements);
272 play_base_bin->subtitle_elements = NULL;
274 G_OBJECT_CLASS (parent_class)->dispose (object);
278 gst_play_base_bin_finalize (GObject * object)
280 GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (object);
282 g_mutex_free (play_base_bin->group_lock);
283 g_cond_free (play_base_bin->group_cond);
285 g_mutex_free (play_base_bin->sub_lock);
287 G_OBJECT_CLASS (parent_class)->finalize (object);
291 * playbasebingroup stuff.
294 static GstPlayBaseGroup *
295 group_create (GstPlayBaseBin * play_base_bin)
297 GstPlayBaseGroup *group;
299 group = g_new0 (GstPlayBaseGroup, 1);
300 group->bin = play_base_bin;
301 group->streaminfo_value_array = g_value_array_new (0);
303 GST_DEBUG_OBJECT (play_base_bin, "created new group %p", group);
309 * Gets the currently playing group.
311 * Callers must have group-lock held when calling this.
314 static GstPlayBaseGroup *
315 get_active_group (GstPlayBaseBin * play_base_bin)
317 GstPlayBaseGroup *group = NULL;
319 if (play_base_bin->queued_groups)
320 group = play_base_bin->queued_groups->data;
326 * get the group used for discovering the different streams.
327 * This function creates a group is there is none.
329 * Callers must have group-lock held when calling this.
331 static GstPlayBaseGroup *
332 get_building_group (GstPlayBaseBin * play_base_bin)
334 GstPlayBaseGroup *group;
336 group = play_base_bin->building_group;
338 group = group_create (play_base_bin);
339 play_base_bin->building_group = group;
346 * Callers must have lock held when calling this!
350 group_destroy (GstPlayBaseGroup * group)
352 GstPlayBaseBin *play_base_bin = group->bin;
355 GST_LOG ("removing group %p", group);
357 /* remove the preroll queues */
358 for (n = 0; n < NUM_TYPES; n++) {
359 GstElement *element = group->type[n].preroll;
367 sel = group->type[n].selector;
369 /* remove any fakesrc elements for this preroll element */
370 for (item = sel->pads; item != NULL; item = item->next) {
371 GstPad *pad = GST_PAD (item->data);
374 if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)
378 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pad), "unlinked_id"));
381 GST_LOG ("removing unlink signal %s:%s", GST_DEBUG_PAD_NAME (pad));
382 g_signal_handler_disconnect (G_OBJECT (pad), sig_id);
383 g_object_set_data (G_OBJECT (pad), "unlinked_id", GINT_TO_POINTER (0));
386 fakesrc = (GstElement *) g_object_get_data (G_OBJECT (pad), "fakesrc");
387 if (fakesrc != NULL) {
388 GST_LOG ("removing fakesrc from %s:%s",
389 GST_PAD_NAME (pad), GST_ELEMENT_NAME (GST_PAD_PARENT (pad)));
390 gst_element_set_state (fakesrc, GST_STATE_NULL);
391 gst_bin_remove (GST_BIN_CAST (play_base_bin), fakesrc);
395 /* if the group is currently being played, we have to remove the element
397 gst_element_set_state (element, GST_STATE_NULL);
398 gst_element_set_state (group->type[n].selector, GST_STATE_NULL);
400 GST_LOG ("removing preroll element %s", GST_ELEMENT_NAME (element));
402 gst_bin_remove (group->type[n].bin, element);
403 gst_bin_remove (group->type[n].bin, group->type[n].selector);
405 group->type[n].preroll = NULL;
406 group->type[n].selector = NULL;
407 group->type[n].bin = NULL;
410 /* free the streaminfo too */
411 g_list_foreach (group->streaminfo, (GFunc) g_object_unref, NULL);
412 g_list_free (group->streaminfo);
413 g_value_array_free (group->streaminfo_value_array);
418 * is called when the current building group is completely finished
419 * and ready for playback
421 * This function grabs lock, so take care when calling.
424 group_commit (GstPlayBaseBin * play_base_bin, gboolean fatal, gboolean subtitle)
426 GstPlayBaseGroup *group;
427 gboolean had_active_group;
429 GROUP_LOCK (play_base_bin);
430 group = play_base_bin->building_group;
431 had_active_group = (get_active_group (play_base_bin) != NULL);
433 GST_DEBUG_OBJECT (play_base_bin, "commit group %p, had active %d",
434 group, had_active_group);
436 /* if an element signalled a no-more-pads after we stopped due
437 * to preroll, the group is NULL. This is not an error */
440 GROUP_UNLOCK (play_base_bin);
443 GST_DEBUG_OBJECT (play_base_bin, "Group loading failed, bailing out");
449 GST_DEBUG_OBJECT (play_base_bin, "group %p done", group);
451 play_base_bin->queued_groups =
452 g_list_append (play_base_bin->queued_groups, group);
454 play_base_bin->building_group = NULL;
456 /* remove signals. We don't want anymore signals from the preroll
457 * elements at this stage. */
458 for (n = 0; n < NUM_TYPES; n++) {
459 GstElement *element = group->type[n].preroll;
464 preroll_remove_overrun (element, play_base_bin);
465 /* if overrun is removed, probe alse has to be removed */
466 queue_remove_probe (element, play_base_bin);
469 /* this is a special subtitle bin, we don't commit the group but
470 * mark the subtitles as detected before we signal. */
471 GST_DEBUG_OBJECT (play_base_bin, "marking subtitle bin as complete");
472 play_base_bin->subtitle_done = TRUE;
476 GST_DEBUG_OBJECT (play_base_bin, "signal group done");
477 GROUP_SIGNAL (play_base_bin);
478 GST_DEBUG_OBJECT (play_base_bin, "signaled group done");
480 if (!subtitle && !had_active_group) {
481 if (!prepare_output (play_base_bin)) {
482 GROUP_UNLOCK (play_base_bin);
486 setup_substreams (play_base_bin);
487 GST_DEBUG_OBJECT (play_base_bin, "Emitting signal");
488 GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin)->setup_output_pads
489 (play_base_bin, group);
490 GST_DEBUG_OBJECT (play_base_bin, "done");
492 GROUP_UNLOCK (play_base_bin);
494 g_object_notify (G_OBJECT (play_base_bin), "stream-info");
496 GROUP_UNLOCK (play_base_bin);
501 * check if there are streams in the group that are not muted
503 * Callers must have group-lock held when calling this.
506 group_is_muted (GstPlayBaseGroup * group)
510 for (n = 0; n < NUM_TYPES; n++) {
511 if (group->type[n].preroll && !group->type[n].done)
519 * Buffer/cache checking.
523 fill_buffer (GstPlayBaseBin * play_base_bin, gint percent)
525 GST_DEBUG_OBJECT (play_base_bin, "buffering %d", percent);
526 gst_element_post_message (GST_ELEMENT_CAST (play_base_bin),
527 gst_message_new_buffering (GST_OBJECT_CAST (play_base_bin), percent));
531 check_queue_event (GstPad * pad, GstEvent * event, gpointer user_data)
533 GstElement *queue = GST_ELEMENT_CAST (user_data);
535 switch (GST_EVENT_TYPE (event)) {
537 GST_DEBUG ("EOS event, mark EOS");
538 g_object_set_data (G_OBJECT (queue), "eos", GINT_TO_POINTER (1));
540 case GST_EVENT_FLUSH_STOP:
541 GST_DEBUG ("FLUSH_STOP event, remove EOS");
542 g_object_set_data (G_OBJECT (queue), "eos", NULL);
545 GST_DEBUG ("uninteresting event %s", GST_EVENT_TYPE_NAME (event));
552 check_queue (GstPad * pad, GstBuffer * data, gpointer user_data)
554 GstElement *queue = GST_ELEMENT_CAST (user_data);
555 GstPlayBaseBin *play_base_bin = g_object_get_data (G_OBJECT (queue), "pbb");
558 GST_DEBUG_OBJECT (queue, "check queue triggered");
560 g_object_get (G_OBJECT (queue), "current-level-time", &level, NULL);
561 GST_DEBUG_OBJECT (play_base_bin, "Queue size: %" GST_TIME_FORMAT,
562 GST_TIME_ARGS (level));
564 if (play_base_bin->queue_threshold > 0) {
565 level = level * 99 / play_base_bin->queue_threshold;
571 fill_buffer (play_base_bin, level);
577 /* If a queue overruns and we are buffer in streaming mode (we have a min-time)
578 * we can potentially create a deadlock when:
580 * 1) the max-bytes is hit and
581 * 2) the min-time is not hit.
583 * We recover from this situation in this callback by
584 * setting the max-bytes to unlimited if we see that there is
585 * a current-time-level (which means some sort of timestamping is
589 queue_deadlock_check (GstElement * queue, GstPlayBaseBin * play_base_bin)
591 guint64 time, min_time;
594 GST_DEBUG_OBJECT (play_base_bin, "overrun signal received from queue %s",
595 GST_ELEMENT_NAME (queue));
597 /* figure out where we are */
598 g_object_get (G_OBJECT (queue), "current-level-time", &time,
599 "current-level-bytes", &bytes, "min-threshold-time", &min_time, NULL);
601 GST_DEBUG_OBJECT (play_base_bin, "streaming mode, queue %s current %"
602 GST_TIME_FORMAT ", min %" GST_TIME_FORMAT
603 ", bytes %d", GST_ELEMENT_NAME (queue),
604 GST_TIME_ARGS (time), GST_TIME_ARGS (min_time), bytes);
606 /* if the bytes in the queue represent time, we disable bytes
607 * overrun checking to not cause deadlocks.
609 if (bytes && time != 0 && time < min_time) {
610 GST_DEBUG_OBJECT (play_base_bin,
611 "possible deadlock found, removing byte limit");
613 /* queue knows about time but is filled with bytes that do
614 * not represent min-threshold time, disable bytes checking so
615 * the queue can grow some more. */
616 g_object_set (G_OBJECT (queue), "max-size-bytes", 0, NULL);
618 /* bytes limit is removed, we cannot deadlock anymore */
619 g_signal_handlers_disconnect_by_func (queue,
620 (gpointer) queue_deadlock_check, play_base_bin);
622 GST_DEBUG_OBJECT (play_base_bin, "no deadlock");
627 queue_remove_probe (GstElement * queue, GstPlayBaseBin * play_base_bin)
632 data = g_object_get_data (G_OBJECT (queue), "probe");
633 sinkpad = gst_element_get_static_pad (queue, "sink");
636 GST_DEBUG_OBJECT (play_base_bin,
637 "Removing buffer probe from pad %s:%s (%p)",
638 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
640 g_object_set_data (G_OBJECT (queue), "probe", NULL);
641 gst_pad_remove_buffer_probe (sinkpad, GPOINTER_TO_INT (data));
643 GST_DEBUG_OBJECT (play_base_bin,
644 "No buffer probe to remove from %s:%s (%p)",
645 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
647 gst_object_unref (sinkpad);
650 /* Used for time-based buffering in streaming mode and is called when a queue
651 * emits the running signal. This means that the high watermark threshold is
652 * reached and the buffering is completed. */
654 queue_threshold_reached (GstElement * queue, GstPlayBaseBin * play_base_bin)
656 GstPlayBaseGroup *group;
659 GST_DEBUG_OBJECT (play_base_bin, "running signal received from queue %s",
660 GST_ELEMENT_NAME (queue));
662 /* we disconnect the signal so that we don't get called for every buffer. */
663 g_signal_handlers_disconnect_by_func (queue,
664 (gpointer) queue_threshold_reached, play_base_bin);
666 if (g_object_get_data (G_OBJECT (queue), "eos")) {
667 GST_DEBUG_OBJECT (play_base_bin, "disable min threshold time, we are EOS");
668 g_object_set (queue, "min-threshold-time", (guint64) 0, NULL);
670 /* now place the limits at the low threshold. When we hit this limit, the
671 * underrun signal will be called. The underrun signal is always connected. */
672 GST_DEBUG_OBJECT (play_base_bin,
673 "setting min threshold time to %" G_GUINT64_FORMAT,
674 play_base_bin->queue_min_threshold);
675 g_object_set (queue, "min-threshold-time",
676 play_base_bin->queue_min_threshold, NULL);
679 GROUP_LOCK (play_base_bin);
680 group = get_active_group (play_base_bin);
682 GROUP_UNLOCK (play_base_bin);
686 /* we remove the probe now because we don't need it anymore to give progress
687 * about the buffering. */
688 for (n = 0; n < NUM_TYPES; n++) {
689 GstElement *element = group->type[n].preroll;
694 queue_remove_probe (element, play_base_bin);
697 GROUP_UNLOCK (play_base_bin);
699 /* we post a 100% buffering message to notify the app that buffering is
700 * completed and playback can start/continue */
701 if (play_base_bin->is_stream)
702 fill_buffer (play_base_bin, 100);
705 /* this signal will be fired when one of the queues with raw
706 * data is filled. This means that the group building stage is over
707 * and playback of the new queued group should start. This is a rather unusual
708 * situation because normally the group is commited when the "no_more_pads"
712 queue_overrun (GstElement * queue, GstPlayBaseBin * play_base_bin)
714 GST_DEBUG_OBJECT (play_base_bin, "queue %s overrun",
715 GST_ELEMENT_NAME (queue));
717 preroll_remove_overrun (queue, play_base_bin);
719 group_commit (play_base_bin, FALSE,
720 GST_OBJECT_PARENT (GST_OBJECT_CAST (queue)) ==
721 GST_OBJECT_CAST (play_base_bin->subtitle));
723 /* notify end of buffering */
724 queue_threshold_reached (queue, play_base_bin);
727 /* this signal is only added when in streaming mode to catch underruns
730 queue_out_of_data (GstElement * queue, GstPlayBaseBin * play_base_bin)
732 GST_DEBUG_OBJECT (play_base_bin, "underrun signal received from queue %s",
733 GST_ELEMENT_NAME (queue));
735 /* On underrun, we want to temoprarily pause playback, set a "min-size"
736 * threshold and wait for the running signal and then play again.
738 * This signal could never be called because the queue max-size limits are set
739 * too low. We take care of this possible deadlock in the the overrun signal
741 g_signal_connect (G_OBJECT (queue), "pushing",
742 G_CALLBACK (queue_threshold_reached), play_base_bin);
743 GST_DEBUG_OBJECT (play_base_bin,
744 "setting min threshold time to %" G_GUINT64_FORMAT,
745 (guint64) play_base_bin->queue_threshold);
746 g_object_set (queue, "min-threshold-time",
747 (guint64) play_base_bin->queue_threshold, NULL);
749 /* re-connect probe, this will fire feedback about the percentage that we
750 * buffered and is posted in the BUFFERING message. */
751 if (!g_object_get_data (G_OBJECT (queue), "probe")) {
755 sinkpad = gst_element_get_static_pad (queue, "sink");
756 id = gst_pad_add_buffer_probe (sinkpad, G_CALLBACK (check_queue), queue);
757 g_object_set_data (G_OBJECT (queue), "probe", GINT_TO_POINTER (id));
758 GST_DEBUG_OBJECT (play_base_bin,
759 "Re-attaching buffering probe to pad %s:%s %p",
760 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
761 gst_object_unref (sinkpad);
763 fill_buffer (play_base_bin, 0);
768 * generate a preroll element which is simply a queue. While there
769 * are still dynamic elements in the pipeline, we wait for one
770 * of the queues to fill. The assumption is that all the dynamic
771 * streams will be detected by that time.
773 * Callers must have the group-lock held when calling this.
776 gen_preroll_element (GstPlayBaseBin * play_base_bin,
777 GstPlayBaseGroup * group, GstStreamType type, GstPad * pad,
778 GstStreamInfo * info)
780 GstElement *selector, *preroll;
781 gchar *name, *padname;
782 const gchar *prename;
788 if (type == GST_STREAM_TYPE_VIDEO)
790 else if (type == GST_STREAM_TYPE_TEXT)
792 else if (type == GST_STREAM_TYPE_AUDIO)
794 else if (type == GST_STREAM_TYPE_SUBPICTURE)
795 prename = "subpicture";
797 g_return_if_reached ();
799 /* create stream selector */
800 selector = g_object_new (GST_TYPE_STREAM_SELECTOR, NULL);
801 padname = gst_pad_get_name (pad);
802 name = g_strdup_printf ("selector_%s_%s", prename, padname);
803 gst_object_set_name (GST_OBJECT_CAST (selector), name);
806 /* create preroll queue */
807 name = g_strdup_printf ("preroll_%s_%s", prename, padname);
808 preroll = gst_element_factory_make ("queue", name);
812 /* for buffering of raw data we ideally want to buffer a
813 * very small amount of buffers since the memory used by
814 * this raw data can be enormously huge.
816 * We use an upper limit of typically a few seconds here but
817 * cap in case no timestamps are set on the raw data (bad!).
819 * FIXME: we abuse this buffer to do network buffering since
820 * we can then easily do time-based buffering. The better
821 * solution would be to add a specific network queue right
822 * after the source that measures the datarate and scales this
823 * queue of encoded data instead.
825 if (play_base_bin->raw_decoding_mode) {
826 if (type == GST_STREAM_TYPE_VIDEO) {
827 g_object_set (G_OBJECT (preroll),
828 "max-size-buffers", 2, "max-size-bytes", 0,
829 "max-size-time", (guint64) 0, NULL);
831 g_object_set (G_OBJECT (preroll),
832 "max-size-buffers", 0, "max-size-bytes",
833 2 * 1024 * 1024, "max-size-time", play_base_bin->queue_size, NULL);
836 g_object_set (G_OBJECT (preroll),
837 "max-size-buffers", 0, "max-size-bytes",
838 ((type == GST_STREAM_TYPE_VIDEO) ? 25 : 2) * 1024 * 1024,
839 "max-size-time", play_base_bin->queue_size, NULL);
842 /* the overrun signal is always attached and serves two purposes:
844 * 1) when we are building a group and the overrun is called, we commit the
845 * group. The reason being that if we fill the entire queue without a
846 * normal group commit (with _no_more_pads()) we can assume the
847 * audio/video is completely wacked or the element just does not know when
848 * it is ready with all the pads (mpeg).
849 * 2) When we are doing network buffering, we keep track of low/high
850 * watermarks in the queue. It is possible that we set the high watermark
851 * higher than the max-size limits to trigger an overrun. In this case we
852 * will never get a running signal but we can use the overrun signal to
853 * detect this deadlock and correct it.
855 overrun_sig = g_signal_connect (G_OBJECT (preroll), "overrun",
856 G_CALLBACK (queue_overrun), play_base_bin);
858 /* keep a ref to the signal id so that we can disconnect the signal callback
859 * when we are done with the preroll */
860 g_object_set_data (G_OBJECT (preroll), "overrun_signal_id",
861 GINT_TO_POINTER (overrun_sig));
863 if (play_base_bin->is_stream &&
864 ((type == GST_STREAM_TYPE_VIDEO &&
865 group->type[GST_STREAM_TYPE_AUDIO - 1].npads == 0) ||
866 (type == GST_STREAM_TYPE_AUDIO &&
867 group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0))) {
871 /* catch deadlocks when we are network buffering in time but the max-limit
872 * in bytes is hit. */
873 g_signal_connect (G_OBJECT (preroll), "overrun",
874 G_CALLBACK (queue_deadlock_check), play_base_bin);
876 /* attach pointer to playbasebin */
877 g_object_set_data (G_OBJECT (preroll), "pbb", play_base_bin);
879 /* give updates on queue size */
880 sinkpad = gst_element_get_static_pad (preroll, "sink");
881 id = gst_pad_add_buffer_probe (sinkpad, G_CALLBACK (check_queue), preroll);
882 GST_DEBUG_OBJECT (play_base_bin, "Attaching probe to pad %s:%s (%p)",
883 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
884 g_object_set_data (G_OBJECT (preroll), "probe", GINT_TO_POINTER (id));
886 /* catch eos and flush events so that we can ignore underruns */
887 id = gst_pad_add_event_probe (sinkpad, G_CALLBACK (check_queue_event),
889 g_object_set_data (G_OBJECT (preroll), "eos_probe", GINT_TO_POINTER (id));
891 gst_object_unref (sinkpad);
893 /* When we connect this queue, it will start running and immediatly
894 * fire an underrun. */
895 g_signal_connect (G_OBJECT (preroll), "underrun",
896 G_CALLBACK (queue_out_of_data), play_base_bin);
897 /* configure threshold and callbacks */
898 queue_out_of_data (preroll, play_base_bin);
901 /* listen for EOS so we can switch groups when one ended. */
902 preroll_pad = gst_element_get_static_pad (preroll, "src");
903 gst_pad_add_event_probe (preroll_pad, G_CALLBACK (probe_triggered), info);
904 gst_object_unref (preroll_pad);
906 /* add to group list */
907 group->type[type - 1].selector = selector;
908 group->type[type - 1].preroll = preroll;
910 /* figure out where the preroll element should go */
911 if (type == GST_STREAM_TYPE_TEXT && play_base_bin->subtitle)
912 target = GST_BIN_CAST (play_base_bin->subtitle);
914 target = GST_BIN_CAST (play_base_bin);
916 group->type[type - 1].bin = target;
917 gst_bin_add (target, selector);
918 gst_bin_add (target, preroll);
920 gst_element_link (selector, preroll);
922 /* figure out target state and set */
923 state = (GST_STATE (play_base_bin) == GST_STATE_PLAYING ?
924 GST_STATE_PLAYING : GST_STATE_PAUSED);
926 gst_element_set_state (selector, state);
927 gst_element_set_state (preroll, state);
931 preroll_remove_overrun (GstElement * element, GstPlayBaseBin * play_base_bin)
934 GObject *obj = G_OBJECT (element);
936 overrun_sig = GPOINTER_TO_INT (g_object_get_data (obj, "overrun_signal_id"));
938 GST_LOG_OBJECT (play_base_bin, "removing preroll signal %s",
939 GST_ELEMENT_NAME (element));
940 g_signal_handler_disconnect (obj, overrun_sig);
941 /* We have disconnected this signal, remove the signal_id from the object
943 g_object_set_data (obj, "overrun_signal_id", NULL);
948 remove_groups (GstPlayBaseBin * play_base_bin)
950 GROUP_LOCK (play_base_bin);
952 /* first destroy the group we were building if any */
953 if (play_base_bin->building_group) {
954 group_destroy (play_base_bin->building_group);
955 play_base_bin->building_group = NULL;
958 /* remove the queued groups */
959 g_list_foreach (play_base_bin->queued_groups, (GFunc) group_destroy, NULL);
960 g_list_free (play_base_bin->queued_groups);
961 play_base_bin->queued_groups = NULL;
964 if (play_base_bin->subtitle) {
965 gst_element_set_state (play_base_bin->subtitle, GST_STATE_NULL);
966 gst_bin_remove (GST_BIN_CAST (play_base_bin), play_base_bin->subtitle);
967 play_base_bin->subtitle = NULL;
970 GROUP_UNLOCK (play_base_bin);
974 * Add/remove a single stream to current building group.
976 * Must be called with group-lock held.
979 add_stream (GstPlayBaseGroup * group, GstStreamInfo * info)
982 GST_DEBUG ("add stream to group %p", group);
984 /* keep ref to the group */
985 g_object_set_data (G_OBJECT (info), "group", group);
987 g_value_init (&v, G_TYPE_OBJECT);
988 g_value_set_object (&v, info);
989 g_value_array_append (group->streaminfo_value_array, &v);
991 group->streaminfo = g_list_append (group->streaminfo, info);
992 if (info->type > 0 && info->type <= NUM_TYPES) {
993 group->type[info->type - 1].npads++;
998 string_arr_has_str (const gchar * values[], const gchar * value)
1000 if (values && value) {
1001 while (*values != NULL) {
1002 if (strcmp (value, *values) == 0)
1010 /* mime types we are not handling on purpose right now, don't post a
1011 * missing-plugin message for these */
1012 static const gchar *blacklisted_mimes[] = {
1013 "video/x-dvd-subpicture", NULL
1016 #define IS_BLACKLISTED_MIME(type) (string_arr_has_str(blacklisted_mimes,type))
1019 gst_play_base_bin_handle_message_func (GstBin * bin, GstMessage * msg)
1021 if (gst_is_missing_plugin_message (msg)) {
1025 detail = gst_missing_plugin_message_get_installer_detail (msg);
1026 for (i = 0; detail != NULL && blacklisted_mimes[i] != NULL; ++i) {
1027 if (strstr (detail, "|decoder-") && strstr (detail, blacklisted_mimes[i])) {
1028 GST_LOG_OBJECT (bin, "suppressing message %" GST_PTR_FORMAT, msg);
1029 gst_message_unref (msg);
1036 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
1040 * signal fired when an unknown stream is found. We create a new
1041 * UNKNOWN streaminfo object.
1044 unknown_type (GstElement * element, GstPad * pad, GstCaps * caps,
1045 GstPlayBaseBin * play_base_bin)
1047 const gchar *type_name;
1048 GstStreamInfo *info;
1049 GstPlayBaseGroup *group;
1051 type_name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
1052 if (type_name && !IS_BLACKLISTED_MIME (type_name)) {
1055 capsstr = gst_caps_to_string (caps);
1056 GST_DEBUG_OBJECT (play_base_bin, "don't know how to handle %s", capsstr);
1057 /* FIXME, g_message() ? */
1058 g_message ("don't know how to handle %s", capsstr);
1061 /* don't spew stuff to the terminal or send message if it's blacklisted */
1062 GST_DEBUG_OBJECT (play_base_bin, "media type %s not handled on purpose, "
1063 "not posting a missing-plugin message on the bus", type_name);
1066 GROUP_LOCK (play_base_bin);
1068 group = get_building_group (play_base_bin);
1070 /* add the stream to the list */
1071 info = gst_stream_info_new (GST_OBJECT_CAST (pad), GST_STREAM_TYPE_UNKNOWN,
1073 info->origin = GST_OBJECT_CAST (pad);
1074 add_stream (group, info);
1076 GROUP_UNLOCK (play_base_bin);
1079 /* add a streaminfo that indicates that the stream is handled by the
1080 * given element. This usually means that a stream without actual data is
1081 * produced but one that is sunken by an element. Examples of this are:
1082 * cdaudio, a hardware decoder/sink, dvd meta bins etc...
1085 add_element_stream (GstElement * element, GstPlayBaseBin * play_base_bin)
1087 GstStreamInfo *info;
1088 GstPlayBaseGroup *group;
1090 GROUP_LOCK (play_base_bin);
1092 group = get_building_group (play_base_bin);
1094 /* add the stream to the list */
1096 gst_stream_info_new (GST_OBJECT_CAST (element), GST_STREAM_TYPE_ELEMENT,
1098 info->origin = GST_OBJECT_CAST (element);
1099 add_stream (group, info);
1101 GROUP_UNLOCK (play_base_bin);
1104 /* when the decoder element signals that no more pads will be generated, we
1105 * can commit the current group.
1108 no_more_pads_full (GstElement * element, gboolean subs,
1109 GstPlayBaseBin * play_base_bin)
1112 GST_DEBUG_OBJECT (element, "no more pads, %d pending",
1113 play_base_bin->pending);
1115 /* nothing pending, we can exit */
1116 if (play_base_bin->pending == 0)
1119 /* the object has no pending no_more_pads */
1120 if (!g_object_get_data (G_OBJECT (element), "pending"))
1123 g_object_set_data (G_OBJECT (element), "pending", NULL);
1125 play_base_bin->pending--;
1127 GST_DEBUG_OBJECT (element, "remove pending, now %d pending",
1128 play_base_bin->pending);
1130 if (play_base_bin->pending == 0) {
1131 /* we can commit this group for playback now */
1132 group_commit (play_base_bin, play_base_bin->is_stream, subs);
1137 no_more_pads (GstElement * element, GstPlayBaseBin * play_base_bin)
1139 no_more_pads_full (element, FALSE, play_base_bin);
1143 sub_no_more_pads (GstElement * element, GstPlayBaseBin * play_base_bin)
1145 no_more_pads_full (element, TRUE, play_base_bin);
1149 source_no_more_pads (GstElement * element, GstPlayBaseBin * bin)
1151 GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
1152 GST_ELEMENT_NAME (element));
1154 g_signal_handler_disconnect (G_OBJECT (element), bin->src_np_sig_id);
1155 bin->src_np_sig_id = 0;
1156 g_signal_handler_disconnect (G_OBJECT (element), bin->src_nmp_sig_id);
1157 bin->src_nmp_sig_id = 0;
1159 no_more_pads_full (element, FALSE, bin);
1163 probe_triggered (GstPad * pad, GstEvent * event, gpointer user_data)
1165 GstPlayBaseGroup *group;
1166 GstPlayBaseBin *play_base_bin;
1167 GstStreamInfo *info;
1170 type = GST_EVENT_TYPE (event);
1172 GST_LOG ("probe triggered, (%d) %s", type, gst_event_type_get_name (type));
1174 /* we only care about EOS */
1175 if (type != GST_EVENT_EOS)
1178 info = GST_STREAM_INFO (user_data);
1179 group = (GstPlayBaseGroup *) g_object_get_data (G_OBJECT (info), "group");
1180 play_base_bin = group->bin;
1182 if (type == GST_EVENT_EOS) {
1183 gint num_groups = 0;
1186 GST_DEBUG_OBJECT (play_base_bin, "probe got EOS in group %p", group);
1188 GROUP_LOCK (play_base_bin);
1190 /* mute this stream */
1191 g_object_set (G_OBJECT (info), "mute", TRUE, NULL);
1192 if (info->type > 0 && info->type <= NUM_TYPES)
1193 group->type[info->type - 1].done = TRUE;
1195 /* see if we have some more groups left to play */
1196 num_groups = g_list_length (play_base_bin->queued_groups);
1197 if (play_base_bin->building_group)
1199 have_left = (num_groups > 1);
1201 /* see if the complete group is muted */
1202 if (!group_is_muted (group)) {
1203 /* group is not completely muted, we remove the EOS event
1204 * and continue, eventually the other streams will be EOSed and
1205 * we can switch out this group. */
1206 GST_DEBUG ("group %p not completely muted", group);
1208 GROUP_UNLOCK (play_base_bin);
1210 /* remove the EOS if we have something left */
1215 /* ok, get rid of the current group then */
1216 //group_destroy (group);
1217 /* removing the current group brings the next group
1219 play_base_bin->queued_groups =
1220 g_list_remove (play_base_bin->queued_groups, group);
1221 /* and wait for the next one to be ready */
1222 while (!play_base_bin->queued_groups) {
1223 GROUP_WAIT (play_base_bin);
1225 group = play_base_bin->queued_groups->data;
1227 /* now activate the next one */
1228 setup_substreams (play_base_bin);
1229 GST_DEBUG ("switching to next group %p - emitting signal", group);
1230 /* and signal the new group */
1231 GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin)->setup_output_pads
1232 (play_base_bin, group);
1234 GROUP_UNLOCK (play_base_bin);
1236 g_object_notify (G_OBJECT (play_base_bin), "stream-info");
1238 /* get rid of the EOS event */
1241 GROUP_UNLOCK (play_base_bin);
1242 GST_LOG ("Last group done, EOS");
1249 /* This function will be called when the sinkpad of the preroll element
1250 * is unlinked, we have to connect something to the sinkpad or else the
1251 * state change will fail..
1254 preroll_unlinked (GstPad * pad, GstPad * peerpad,
1255 GstPlayBaseBin * play_base_bin)
1257 GstElement *fakesrc;
1261 /* make a fakesrc that will just emit one EOS */
1262 fakesrc = gst_element_factory_make ("fakesrc", NULL);
1263 g_object_set (G_OBJECT (fakesrc), "num-buffers", 0, NULL);
1265 GST_DEBUG ("patching unlinked pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1267 srcpad = gst_element_get_static_pad (fakesrc, "src");
1268 gst_bin_add (GST_BIN_CAST (play_base_bin), fakesrc);
1269 gst_pad_link (srcpad, pad);
1270 gst_object_unref (srcpad);
1272 /* keep track of these patch elements */
1273 g_object_set_data (G_OBJECT (pad), "fakesrc", fakesrc);
1275 /* now unlink the unlinked signal so that it is not called again when
1276 * we destroy the queue */
1277 sig_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pad), "unlinked_id"));
1279 g_signal_handler_disconnect (G_OBJECT (pad), sig_id);
1280 g_object_set_data (G_OBJECT (pad), "unlinked_id", GINT_TO_POINTER (0));
1284 /* Mute stream on first data - for header-is-in-stream-stuff
1285 * (vorbis, ogmtext). */
1287 mute_stream (GstPad * pad, GstBuffer * buf, gpointer data)
1289 GstStreamInfo *info = GST_STREAM_INFO (data);
1292 GST_DEBUG ("mute stream triggered");
1294 g_object_set (G_OBJECT (info), "mute", TRUE, NULL);
1295 id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info), "mute_probe"));
1296 g_object_set_data (G_OBJECT (info), "mute_probe", NULL);
1298 gst_pad_remove_buffer_probe (GST_PAD_CAST (info->object), id);
1306 silence_stream (GstPad * pad, GstMiniObject * data, gpointer user_data)
1308 GST_DEBUG ("silence stream triggered");
1314 /* Called by the signal handlers when a decodebin (main or subtitle) has
1315 * found a new raw pad. We create a preroll element if needed and the
1316 * appropriate streaminfo. Commits the group if there will be no more pads
1319 new_decoded_pad_full (GstElement * element, GstPad * pad, gboolean last,
1320 GstPlayBaseBin * play_base_bin, gboolean is_subs)
1322 GstStructure *structure;
1323 const gchar *mimetype;
1325 GstStreamInfo *info;
1326 GstStreamType type = GST_STREAM_TYPE_UNKNOWN;
1328 GstPlayBaseGroup *group;
1333 GST_DEBUG ("play base: new decoded pad. Last: %d", last);
1335 /* first see if this pad has interesting caps */
1336 caps = gst_pad_get_caps (pad);
1337 if (caps == NULL || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1340 /* get the mime type */
1341 structure = gst_caps_get_structure (caps, 0);
1342 mimetype = gst_structure_get_name (structure);
1344 GROUP_LOCK (play_base_bin);
1346 group = get_building_group (play_base_bin);
1350 parent = gst_object_get_parent (GST_OBJECT_CAST (element));
1351 if (g_str_has_prefix (mimetype, "audio/") &&
1352 parent != GST_OBJECT_CAST (play_base_bin->subtitle)) {
1353 type = GST_STREAM_TYPE_AUDIO;
1354 } else if (g_str_has_prefix (mimetype, "video/x-dvd-subpicture") &&
1355 parent != GST_OBJECT_CAST (play_base_bin->subtitle)) {
1356 type = GST_STREAM_TYPE_SUBPICTURE;
1357 } else if (g_str_has_prefix (mimetype, "video/") &&
1358 parent != GST_OBJECT_CAST (play_base_bin->subtitle)) {
1359 type = GST_STREAM_TYPE_VIDEO;
1360 } else if (g_str_has_prefix (mimetype, "text/")) {
1361 type = GST_STREAM_TYPE_TEXT;
1363 gst_object_unref (parent);
1365 info = gst_stream_info_new (GST_OBJECT_CAST (pad), type, NULL, caps);
1366 gst_caps_unref (caps);
1368 if (type == GST_STREAM_TYPE_UNKNOWN) {
1369 /* Unknown streams get added to the group, but the data
1370 * just gets ignored */
1371 add_stream (group, info);
1373 GROUP_UNLOCK (play_base_bin);
1375 /* signal the no more pads after adding the stream */
1377 no_more_pads_full (element, is_subs, play_base_bin);
1382 /* first pad of each type gets a selector + preroll queue */
1383 first_pad = (group->type[type - 1].npads == 0);
1386 GST_DEBUG ("play base: pad needs new preroll");
1387 gen_preroll_element (play_base_bin, group, type, pad, info);
1390 /* add to stream selector */
1392 gst_element_get_request_pad (group->type[type - 1].selector, "sink%d");
1394 /* make sure we catch unlink signals */
1395 sig = g_signal_connect (G_OBJECT (sinkpad), "unlinked",
1396 G_CALLBACK (preroll_unlinked), play_base_bin);
1397 /* keep a ref to the signal id so that we can disconnect the signal callback */
1398 g_object_set_data (G_OBJECT (sinkpad), "unlinked_id", GINT_TO_POINTER (sig));
1399 /* Store a pointer to the stream selector pad for this stream */
1400 g_object_set_data (G_OBJECT (pad), "pb_sel_pad", sinkpad);
1402 gst_pad_link (pad, sinkpad);
1403 gst_object_unref (sinkpad);
1405 /* select 1st for now - we'll select a preferred one after preroll */
1409 GST_DEBUG ("Adding silence_stream data probe on type %d (npads %d)", type,
1410 group->type[type - 1].npads);
1412 id = gst_pad_add_data_probe (GST_PAD_CAST (pad),
1413 G_CALLBACK (silence_stream), info);
1414 g_object_set_data (G_OBJECT (pad), "eat_probe", GINT_TO_POINTER (id));
1417 /* add the stream to the list */
1418 add_stream (group, info);
1420 GROUP_UNLOCK (play_base_bin);
1422 /* signal the no more pads after adding the stream */
1424 no_more_pads_full (element, is_subs, play_base_bin);
1431 g_warning ("no type on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1433 gst_caps_unref (caps);
1439 new_decoded_pad (GstElement * element, GstPad * pad, gboolean last,
1440 GstPlayBaseBin * play_base_bin)
1442 new_decoded_pad_full (element, pad, last, play_base_bin, FALSE);
1446 subs_new_decoded_pad (GstElement * element, GstPad * pad, gboolean last,
1447 GstPlayBaseBin * play_base_bin)
1449 new_decoded_pad_full (element, pad, last, play_base_bin, TRUE);
1453 set_encoding_element (GstElement * element, gchar * encoding)
1455 GST_DEBUG_OBJECT (element, "setting encoding to %s", GST_STR_NULL (encoding));
1456 g_object_set (G_OBJECT (element), "subtitle-encoding", encoding, NULL);
1461 decodebin_element_added_cb (GstBin * decodebin, GstElement * element,
1464 GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (data);
1467 if (!g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1468 "subtitle-encoding")) {
1472 g_mutex_lock (play_base_bin->sub_lock);
1473 play_base_bin->subtitle_elements =
1474 g_slist_append (play_base_bin->subtitle_elements, element);
1475 encoding = g_strdup (play_base_bin->subencoding);
1476 g_mutex_unlock (play_base_bin->sub_lock);
1478 set_encoding_element (element, encoding);
1483 decodebin_element_removed_cb (GstBin * decodebin, GstElement * element,
1486 GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (data);
1488 g_mutex_lock (play_base_bin->sub_lock);
1489 play_base_bin->subtitle_elements =
1490 g_slist_remove (play_base_bin->subtitle_elements, element);
1491 g_mutex_unlock (play_base_bin->sub_lock);
1496 * Generate source ! subparse bins.
1500 setup_subtitle (GstPlayBaseBin * play_base_bin, gchar * sub_uri)
1502 GstElement *source, *subdecodebin, *subbin;
1504 if (!gst_uri_is_valid (sub_uri))
1507 source = gst_element_make_from_uri (GST_URI_SRC, sub_uri, NULL);
1511 if (g_getenv ("USE_DECODEBIN2"))
1512 subdecodebin = gst_element_factory_make ("decodebin2", "subtitle-decoder");
1514 subdecodebin = gst_element_factory_make ("decodebin", "subtitle-decoder");
1515 g_signal_connect (subdecodebin, "element-added",
1516 G_CALLBACK (decodebin_element_added_cb), play_base_bin);
1517 g_signal_connect (subdecodebin, "element-removed",
1518 G_CALLBACK (decodebin_element_removed_cb), play_base_bin);
1519 subbin = gst_bin_new ("subtitle-bin");
1520 gst_bin_add_many (GST_BIN_CAST (subbin), source, subdecodebin, NULL);
1522 gst_element_link (source, subdecodebin);
1524 /* return the subtitle GstElement object */
1530 GST_ELEMENT_WARNING (play_base_bin, RESOURCE, NOT_FOUND,
1531 (_("Invalid subtitle URI \"%s\", subtitles disabled."), sub_uri),
1537 gchar *prot = gst_uri_get_protocol (sub_uri);
1542 gst_element_post_message (GST_ELEMENT (play_base_bin),
1543 gst_missing_uri_source_message_new (GST_ELEMENT (play_base_bin),
1546 desc = gst_pb_utils_get_source_description (prot);
1547 GST_ELEMENT_ERROR (play_base_bin, CORE, MISSING_PLUGIN,
1548 (_("A %s plugin is required to play this stream, but not installed."),
1549 desc), ("No URI handler to handle sub_uri: %s", sub_uri));
1559 /* helper function to lookup stuff in lists */
1561 array_has_value (const gchar * values[], const gchar * value)
1565 for (i = 0; values[i]; i++) {
1566 if (g_str_has_prefix (value, values[i]))
1572 /* list of URIs that we consider to be streams and that need buffering.
1573 * We have no mechanism yet to figure this out with a query. */
1574 static const gchar *stream_uris[] = { "http://", "mms://", "mmsh://",
1575 "mmsu://", "mmst://", "myth://", NULL
1578 /* blacklisted URIs, we know they will always fail. */
1579 static const gchar *blacklisted_uris[] = { NULL };
1581 /* mime types that we don't consider to be media types */
1582 static const gchar *no_media_mimes[] = {
1583 "application/x-executable", "application/x-bzip", "application/x-gzip",
1584 "application/zip", "application/x-compress", NULL
1587 /* mime types we consider raw media */
1588 static const gchar *raw_mimes[] = {
1589 "audio/x-raw", "video/x-raw", "video/x-dvd-subpicture", NULL
1592 #define IS_STREAM_URI(uri) (array_has_value (stream_uris, uri))
1593 #define IS_BLACKLISTED_URI(uri) (array_has_value (blacklisted_uris, uri))
1594 #define IS_NO_MEDIA_MIME(mime) (array_has_value (no_media_mimes, mime))
1595 #define IS_RAW_MIME(mime) (array_has_value (raw_mimes, mime))
1598 * Generate and configure a source element.
1601 gen_source_element (GstPlayBaseBin * play_base_bin, GstElement ** subbin)
1605 if (!play_base_bin->uri)
1608 if (!gst_uri_is_valid (play_base_bin->uri))
1611 if (IS_BLACKLISTED_URI (play_base_bin->uri))
1612 goto uri_blacklisted;
1614 if (play_base_bin->suburi) {
1615 GST_LOG_OBJECT (play_base_bin, "Creating decoder for subtitles URI %s",
1616 play_base_bin->suburi);
1617 /* subtitle specified */
1618 *subbin = setup_subtitle (play_base_bin, play_base_bin->suburi);
1620 /* no subtitle specified */
1624 source = gst_element_make_from_uri (GST_URI_SRC, play_base_bin->uri,
1629 play_base_bin->is_stream = IS_STREAM_URI (play_base_bin->uri);
1631 /* make HTTP sources send extra headers so we get icecast
1632 * metadata in case the stream is an icecast stream */
1633 if (!strncmp (play_base_bin->uri, "http://", 7) &&
1634 g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1636 g_object_set (source, "iradio-mode", TRUE, NULL);
1639 if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1640 "connection-speed")) {
1641 GST_DEBUG_OBJECT (play_base_bin,
1642 "setting connection-speed=%d to source element",
1643 play_base_bin->connection_speed / 1000);
1644 g_object_set (source, "connection-speed",
1645 play_base_bin->connection_speed / 1000, NULL);
1653 GST_ELEMENT_ERROR (play_base_bin, RESOURCE, NOT_FOUND,
1654 (_("No URI specified to play from.")), (NULL));
1659 GST_ELEMENT_ERROR (play_base_bin, RESOURCE, NOT_FOUND,
1660 (_("Invalid URI \"%s\"."), play_base_bin->uri), (NULL));
1665 GST_ELEMENT_ERROR (play_base_bin, RESOURCE, FAILED,
1666 (_("RTSP streams cannot be played yet.")), (NULL));
1671 gchar *prot = gst_uri_get_protocol (play_base_bin->uri);
1673 /* whoops, could not create the source element, dig a little deeper to
1674 * figure out what might be wrong. */
1678 gst_element_post_message (GST_ELEMENT (play_base_bin),
1679 gst_missing_uri_source_message_new (GST_ELEMENT (play_base_bin),
1682 desc = gst_pb_utils_get_source_description (prot);
1683 GST_ELEMENT_ERROR (play_base_bin, CORE, MISSING_PLUGIN,
1684 (_("A %s plugin is required to play this stream, but not installed."),
1685 desc), ("No URI handler for %s", prot));
1695 /* is called when a dynamic source element created a new pad. */
1697 source_new_pad (GstElement * element, GstPad * pad, GstPlayBaseBin * bin)
1699 GstElement *decoder;
1702 GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
1703 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
1705 /* if this is a pad with all raw caps, we can expose it */
1706 if (has_all_raw_caps (pad, &is_raw) && is_raw) {
1707 bin->raw_decoding_mode = TRUE;
1708 /* it's all raw, create output pads. */
1709 new_decoded_pad_full (element, pad, FALSE, bin, FALSE);
1713 /* not raw, create decoder */
1714 decoder = make_decoder (bin);
1718 /* and link to decoder */
1719 if (!gst_element_link (bin->source, decoder))
1720 goto could_not_link;
1722 gst_element_set_state (decoder, GST_STATE_PAUSED);
1729 /* error was posted */
1734 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
1735 (NULL), ("Can't link source to decoder element"));
1741 * Setup the substreams (is called right after group_commit () when
1742 * loading a new group, or after switching groups).
1744 * Should be called with group-lock held.
1747 setup_substreams (GstPlayBaseBin * play_base_bin)
1749 GstPlayBaseGroup *group;
1753 GST_DEBUG_OBJECT (play_base_bin, "setting up substreams");
1755 /* Remove the eat probes */
1756 group = get_active_group (play_base_bin);
1757 for (item = group->streaminfo; item; item = item->next) {
1758 GstStreamInfo *info = item->data;
1761 data = g_object_get_data (G_OBJECT (info->object), "eat_probe");
1763 gst_pad_remove_data_probe (GST_PAD_CAST (info->object),
1764 GPOINTER_TO_INT (data));
1765 g_object_set_data (G_OBJECT (info->object), "eat_probe", NULL);
1768 /* now remove unknown pads */
1769 if (info->type == GST_STREAM_TYPE_UNKNOWN) {
1772 id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info), "mute_probe"));
1774 id = gst_pad_add_buffer_probe (GST_PAD_CAST (info->object),
1775 G_CALLBACK (mute_stream), info);
1776 g_object_set_data (G_OBJECT (info), "mute_probe", GINT_TO_POINTER (id));
1781 /* now check if the requested current streams exist. If
1782 * current >= num_streams, decrease current so at least
1783 * we have output. Always keep it enabled. */
1784 for (n = 0; n < NUM_TYPES; n++) {
1785 if (play_base_bin->current[n] >= group->type[n].npads) {
1786 GST_DEBUG_OBJECT (play_base_bin, "reset type %d to current 0", n);
1787 play_base_bin->current[n] = 0;
1791 /* now activate the right sources. Don't forget that during preroll,
1792 * we set the first source to forwarding and ignored the rest. */
1793 for (n = 0; n < NUM_TYPES; n++) {
1794 GST_DEBUG_OBJECT (play_base_bin, "setting type %d to current %d", n,
1795 play_base_bin->current[n]);
1796 set_active_source (play_base_bin, n + 1, play_base_bin->current[n]);
1803 * @all_raw: pointer to hold the result
1805 * check if the caps of the pad are all raw. The caps are all raw if
1806 * all of its structures contain audio/x-raw or video/x-raw.
1808 * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1811 has_all_raw_caps (GstPad * pad, gboolean * all_raw)
1815 guint i, num_raw = 0;
1816 gboolean res = FALSE;
1818 caps = gst_pad_get_caps (pad);
1822 capssize = gst_caps_get_size (caps);
1823 /* no caps, skip and move to the next pad */
1824 if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1827 /* count the number of raw formats in the caps */
1828 for (i = 0; i < capssize; ++i) {
1830 const gchar *mime_type;
1832 s = gst_caps_get_structure (caps, i);
1833 mime_type = gst_structure_get_name (s);
1835 if (IS_RAW_MIME (mime_type))
1839 *all_raw = (num_raw == capssize);
1843 gst_caps_unref (caps);
1849 * @play_base_bin: a #GstPlayBaseBin
1850 * @is_raw: are all pads raw data
1851 * @have_out: does the source have output
1852 * @is_dynamic: is this a dynamic source
1854 * Check the source of @play_base_bin and collect information about it.
1856 * @is_raw will be set to TRUE if the source only produces raw pads. When this
1857 * function returns, all of the raw pad of the source will be added
1858 * to @play_base_bin.
1860 * @have_out: will be set to TRUE if the source has output pads.
1862 * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1865 * Returns: FALSE if a fatal error occured while scanning.
1868 analyse_source (GstPlayBaseBin * play_base_bin, gboolean * is_raw,
1869 gboolean * have_out, gboolean * is_dynamic)
1871 GstIterator *pads_iter;
1872 gboolean done = FALSE;
1873 gboolean res = TRUE;
1877 *is_dynamic = FALSE;
1879 pads_iter = gst_element_iterate_src_pads (play_base_bin->source);
1883 switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
1884 case GST_ITERATOR_ERROR:
1887 case GST_ITERATOR_DONE:
1890 case GST_ITERATOR_RESYNC:
1891 /* reset results and resync */
1894 *is_dynamic = FALSE;
1895 gst_iterator_resync (pads_iter);
1897 case GST_ITERATOR_OK:
1898 /* we now officially have an ouput pad */
1901 /* if FALSE, this pad has no caps and we continue with the next pad. */
1902 if (!has_all_raw_caps (pad, is_raw)) {
1903 gst_object_unref (pad);
1907 /* caps on source pad are all raw, we can add the pad */
1909 new_decoded_pad_full (play_base_bin->source, pad, FALSE,
1910 play_base_bin, FALSE);
1913 gst_object_unref (pad);
1917 gst_iterator_free (pads_iter);
1920 GstElementClass *elemclass;
1923 /* element has no output pads, check for padtemplates that list SOMETIMES
1925 elemclass = GST_ELEMENT_GET_CLASS (play_base_bin->source);
1927 walk = gst_element_class_get_pad_template_list (elemclass);
1928 while (walk != NULL) {
1929 GstPadTemplate *templ;
1931 templ = (GstPadTemplate *) walk->data;
1932 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1933 if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES) {
1935 break; /* only break out if we found a sometimes src pad
1936 continue walking through if say a request src pad is found
1937 elements such as mpegtsparse and dvbbasebin have request
1938 and sometimes src pads */
1941 walk = g_list_next (walk);
1949 remove_decoders (GstPlayBaseBin * bin)
1953 for (walk = bin->decoders; walk; walk = g_slist_next (walk)) {
1954 GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1956 GST_DEBUG_OBJECT (bin, "removing old decoder element");
1957 /* Disconnect all the signal handlers we attached to the decodebin before
1958 * we dispose of it */
1959 g_signal_handlers_disconnect_by_func (decoder,
1960 (gpointer) (decodebin_element_added_cb), bin);
1961 g_signal_handlers_disconnect_by_func (decoder,
1962 (gpointer) (decodebin_element_removed_cb), bin);
1963 g_signal_handlers_disconnect_by_func (decoder,
1964 (gpointer) (new_decoded_pad), bin);
1965 g_signal_handlers_disconnect_by_func (decoder,
1966 (gpointer) (no_more_pads), bin);
1967 g_signal_handlers_disconnect_by_func (decoder,
1968 (gpointer) (unknown_type), bin);
1970 gst_element_set_state (decoder, GST_STATE_NULL);
1971 gst_bin_remove (GST_BIN_CAST (bin), decoder);
1973 g_slist_free (bin->decoders);
1974 bin->decoders = NULL;
1978 make_decoder (GstPlayBaseBin * play_base_bin)
1980 GstElement *decoder;
1982 /* now create the decoder element */
1983 if (g_getenv ("USE_DECODEBIN2"))
1984 decoder = gst_element_factory_make ("decodebin2", NULL);
1986 decoder = gst_element_factory_make ("decodebin", NULL);
1990 g_signal_connect (decoder, "element-added",
1991 G_CALLBACK (decodebin_element_added_cb), play_base_bin);
1992 g_signal_connect (decoder, "element-removed",
1993 G_CALLBACK (decodebin_element_removed_cb), play_base_bin);
1995 gst_bin_add (GST_BIN_CAST (play_base_bin), decoder);
1997 /* set up callbacks to create the links between decoded data
1998 * and video/audio/subtitle rendering/output. */
1999 g_signal_connect (G_OBJECT (decoder),
2000 "new-decoded-pad", G_CALLBACK (new_decoded_pad), play_base_bin);
2001 g_signal_connect (G_OBJECT (decoder), "no-more-pads",
2002 G_CALLBACK (no_more_pads), play_base_bin);
2003 g_signal_connect (G_OBJECT (decoder),
2004 "unknown-type", G_CALLBACK (unknown_type), play_base_bin);
2005 g_object_set_data (G_OBJECT (decoder), "pending", GINT_TO_POINTER (1));
2006 play_base_bin->pending++;
2008 GST_DEBUG_OBJECT (play_base_bin, "created decodebin, %d pending",
2009 play_base_bin->pending);
2011 play_base_bin->decoders = g_slist_prepend (play_base_bin->decoders, decoder);
2018 GST_ELEMENT_ERROR (play_base_bin, CORE, MISSING_PLUGIN,
2019 (_("Could not create \"decodebin\" element.")), (NULL));
2025 remove_source (GstPlayBaseBin * bin)
2027 GstElement *source = bin->source;
2030 GST_DEBUG_OBJECT (bin, "removing old src element");
2031 gst_element_set_state (source, GST_STATE_NULL);
2033 if (bin->src_np_sig_id) {
2034 g_signal_handler_disconnect (G_OBJECT (source), bin->src_np_sig_id);
2035 bin->src_np_sig_id = 0;
2037 if (bin->src_nmp_sig_id) {
2038 g_signal_handler_disconnect (G_OBJECT (source), bin->src_nmp_sig_id);
2039 bin->src_nmp_sig_id = 0;
2041 gst_bin_remove (GST_BIN_CAST (bin), source);
2046 static GstBusSyncReply
2047 subbin_startup_sync_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
2049 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
2050 GstPlayBaseBin *play_base_bin;
2052 play_base_bin = GST_PLAY_BASE_BIN (user_data);
2053 if (!play_base_bin->subtitle_done) {
2054 GST_WARNING_OBJECT (play_base_bin, "error starting up subtitle bin: %"
2055 GST_PTR_FORMAT, msg);
2056 play_base_bin->subtitle_done = TRUE;
2057 GST_DEBUG_OBJECT (play_base_bin, "signal group done");
2058 GROUP_SIGNAL (play_base_bin);
2059 GST_DEBUG_OBJECT (play_base_bin, "signaled group done");
2062 return GST_BUS_PASS;
2065 /* construct and run the source and decoder elements until we found
2066 * all the streams or until a preroll queue has been filled.
2069 setup_source (GstPlayBaseBin * play_base_bin)
2071 GstElement *subbin = NULL;
2072 gboolean is_raw, have_out, is_dynamic;
2074 if (!play_base_bin->need_rebuild)
2076 play_base_bin->raw_decoding_mode = FALSE;
2078 GST_DEBUG_OBJECT (play_base_bin, "setup source");
2080 /* delete old src */
2081 remove_source (play_base_bin);
2083 /* create and configure an element that can handle the uri */
2084 if (!(play_base_bin->source = gen_source_element (play_base_bin, &subbin)))
2087 /* state will be merged later - if file is not found, error will be
2088 * handled by the application right after. */
2089 gst_bin_add (GST_BIN_CAST (play_base_bin), play_base_bin->source);
2090 g_object_notify (G_OBJECT (play_base_bin), "source");
2092 /* remove the old decoders now, if any */
2093 remove_decoders (play_base_bin);
2095 /* remove our previous preroll queues */
2096 remove_groups (play_base_bin);
2098 /* clear pending dynamic elements */
2099 play_base_bin->pending = 0;
2106 play_base_bin->subtitle = subbin;
2107 db = gst_bin_get_by_name (GST_BIN_CAST (subbin), "subtitle-decoder");
2109 /* do type detection, without adding (so no preroll) */
2110 g_signal_connect (G_OBJECT (db), "new-decoded-pad",
2111 G_CALLBACK (subs_new_decoded_pad), play_base_bin);
2112 g_signal_connect (G_OBJECT (db), "no-more-pads",
2113 G_CALLBACK (sub_no_more_pads), play_base_bin);
2114 g_signal_connect (G_OBJECT (db), "unknown-type",
2115 G_CALLBACK (unknown_type), play_base_bin);
2116 g_object_set_data (G_OBJECT (db), "pending", GINT_TO_POINTER (1));
2117 play_base_bin->pending++;
2119 GST_DEBUG_OBJECT (play_base_bin, "we have subtitles, %d pending",
2120 play_base_bin->pending);
2122 if (!play_base_bin->is_stream) {
2123 GstStateChangeReturn sret;
2125 /* either when the queues are filled or when the decoder element
2126 * has no more dynamic streams, the cond is unlocked. We can remove
2127 * the signal handlers then
2129 GST_DEBUG_OBJECT (play_base_bin, "starting subtitle bin");
2131 /* for subtitles in a separate bin we will not commit the
2132 * current building group since we need to add the other
2133 * audio/video streams to the group. We check if we managed
2134 * to commit the subtitle group using an extra flag. */
2135 play_base_bin->subtitle_done = FALSE;
2137 /* since subbin is still a stand-alone bin, we need to add a custom bus
2138 * to intercept error messages, so we can stop waiting and continue */
2139 bus = gst_bus_new ();
2140 gst_element_set_bus (subbin, bus);
2141 gst_bus_set_sync_handler (bus, subbin_startup_sync_msg, play_base_bin);
2143 sret = gst_element_set_state (subbin, GST_STATE_PAUSED);
2144 if (sret != GST_STATE_CHANGE_FAILURE) {
2145 GROUP_LOCK (play_base_bin);
2146 GST_DEBUG ("waiting for subtitle to complete...");
2147 while (!play_base_bin->subtitle_done)
2148 GROUP_WAIT (play_base_bin);
2149 GST_DEBUG ("group done !");
2150 GROUP_UNLOCK (play_base_bin);
2152 if (!play_base_bin->building_group ||
2153 play_base_bin->building_group->type[GST_STREAM_TYPE_TEXT -
2156 GST_DEBUG ("No subtitle found - ignoring");
2157 gst_element_set_state (subbin, GST_STATE_NULL);
2158 gst_object_unref (play_base_bin->subtitle);
2159 play_base_bin->subtitle = NULL;
2161 GST_DEBUG_OBJECT (play_base_bin, "Subtitle set-up successful");
2164 GST_WARNING_OBJECT (play_base_bin, "Failed to start subtitle bin");
2165 gst_element_set_state (subbin, GST_STATE_NULL);
2166 gst_object_unref (play_base_bin->subtitle);
2167 play_base_bin->subtitle = NULL;
2170 gst_bus_set_sync_handler (bus, NULL, NULL);
2171 gst_element_set_bus (subbin, NULL);
2172 gst_object_unref (bus);
2174 gst_object_unref (db);
2176 /* see if the source element emits raw audio/video all by itself,
2177 * if so, we can create streams for the pads and be done with it.
2178 * Also check that is has source pads, if not, we assume it will
2179 * do everything itself. */
2180 if (!analyse_source (play_base_bin, &is_raw, &have_out, &is_dynamic))
2181 goto invalid_source;
2184 GST_DEBUG_OBJECT (play_base_bin, "Source provides all raw data");
2185 /* source provides raw data, we added the pads and we can now signal a
2186 * no_more pads because we are done. */
2187 group_commit (play_base_bin, play_base_bin->is_stream, FALSE);
2190 if (!have_out && !is_dynamic) {
2191 GST_DEBUG_OBJECT (play_base_bin, "Source has no output pads");
2192 /* create a stream to indicate that this uri is handled by a self
2193 * contained element. We are now done. */
2194 add_element_stream (play_base_bin->source, play_base_bin);
2195 group_commit (play_base_bin, play_base_bin->is_stream, FALSE);
2199 /* connect a handler for the new-pad signal */
2200 play_base_bin->src_np_sig_id =
2201 g_signal_connect (G_OBJECT (play_base_bin->source), "pad-added",
2202 G_CALLBACK (source_new_pad), play_base_bin);
2203 play_base_bin->src_nmp_sig_id =
2204 g_signal_connect (G_OBJECT (play_base_bin->source), "no-more-pads",
2205 G_CALLBACK (source_no_more_pads), play_base_bin);
2206 g_object_set_data (G_OBJECT (play_base_bin->source), "pending",
2207 GINT_TO_POINTER (1));
2208 play_base_bin->pending++;
2209 GST_DEBUG_OBJECT (play_base_bin,
2210 "Source has dynamic output pads, %d pending", play_base_bin->pending);
2212 GstElement *decoder;
2214 /* no dynamic source, we can link now */
2215 decoder = make_decoder (play_base_bin);
2219 if (!gst_element_link (play_base_bin->source, decoder))
2220 goto could_not_link;
2223 if (play_base_bin->subtitle)
2224 gst_bin_add (GST_BIN_CAST (play_base_bin), play_base_bin->subtitle);
2226 play_base_bin->need_rebuild = FALSE;
2233 /* error message was already posted */
2238 GST_ELEMENT_ERROR (play_base_bin, CORE, FAILED,
2239 (_("Source element is invalid.")), (NULL));
2244 /* message was posted */
2249 GST_ELEMENT_ERROR (play_base_bin, CORE, NEGOTIATION,
2250 (NULL), ("Can't link source to decoder element"));
2256 finish_source (GstPlayBaseBin * play_base_bin)
2258 /* FIXME: no need to grab the group lock here? (tpm) */
2259 if (get_active_group (play_base_bin) != NULL) {
2260 if (play_base_bin->subtitle) {
2261 /* make subs iterate from now on */
2262 gst_bin_add (GST_BIN_CAST (play_base_bin), play_base_bin->subtitle);
2268 * Caller must have group-lock held.
2270 * We iterate over all detected streams in the streaminfo and try to find
2271 * impossible cases, like subtitles without video.
2274 prepare_output (GstPlayBaseBin * play_base_bin)
2277 gboolean stream_found = FALSE, no_media = FALSE;
2278 gboolean got_video = FALSE, got_subtitle = FALSE;
2279 GstPlayBaseGroup *group;
2281 group = get_active_group (play_base_bin);
2283 /* check if we found any supported stream... if not, then
2284 * we detected stream type (or the above would've failed),
2285 * but linking/decoding failed - plugin probably missing. */
2286 for (item = group ? group->streaminfo : NULL; item != NULL; item = item->next) {
2287 GstStreamInfo *info = GST_STREAM_INFO (item->data);
2289 if (info->type == GST_STREAM_TYPE_VIDEO) {
2290 stream_found = TRUE;
2293 } else if (info->type == GST_STREAM_TYPE_ELEMENT) {
2294 stream_found = TRUE;
2295 } else if (info->type == GST_STREAM_TYPE_AUDIO) {
2296 stream_found = TRUE;
2297 } else if (info->type == GST_STREAM_TYPE_TEXT ||
2298 info->type == GST_STREAM_TYPE_SUBPICTURE) {
2299 got_subtitle = TRUE;
2300 } else if (!item->prev && !item->next) {
2301 /* We're no audio/video and the only stream... We could
2302 * be something not-media that's detected because then our
2303 * typefind doesn't mess up with mp3 (bz2, gz, elf, ...) */
2304 if (info->caps && !gst_caps_is_empty (info->caps)) {
2306 gst_structure_get_name (gst_caps_get_structure (info->caps, 0));
2308 no_media = IS_NO_MEDIA_MIME (mime);
2313 if (!stream_found) {
2315 GST_ELEMENT_ERROR (play_base_bin, STREAM, WRONG_TYPE,
2316 (_("Only a subtitle stream was detected. "
2317 "Either you are loading a subtitle file or some other type of "
2318 "text file, or the media file was not recognized.")), (NULL));
2319 } else if (!no_media) {
2320 GST_ELEMENT_ERROR (play_base_bin, STREAM, CODEC_NOT_FOUND,
2321 (_("You do not have a decoder installed to handle this file. "
2322 "You might need to install the necessary plugins.")), (NULL));
2324 GST_ELEMENT_ERROR (play_base_bin, STREAM, WRONG_TYPE,
2325 (_("This is not a media file")), (NULL));
2328 } else if (got_subtitle && !got_video) {
2329 GST_ELEMENT_ERROR (play_base_bin, STREAM, WRONG_TYPE,
2330 (_("A subtitle stream was detected, but no video stream.")), (NULL));
2338 * Multi-stream management. -1 = none.
2340 * Caller has group-lock held.
2343 get_active_source (GstPlayBaseBin * play_base_bin, GstStreamType type)
2345 GstPlayBaseGroup *group;
2349 group = get_active_group (play_base_bin);
2353 for (s = group->streaminfo; s; s = s->next) {
2354 GstStreamInfo *info = s->data;
2356 if (info->type == type) {
2357 if (!info->mute && !g_object_get_data (G_OBJECT (info), "mute_probe")) {
2368 /* Kill pad reactivation on state change. */
2371 static void muted_group_change_state (GstElement * element,
2372 gint old_state, gint new_state, gpointer data);
2376 mute_group_type (GstPlayBaseGroup * group, GstStreamType type, gboolean mute)
2378 gboolean active = !mute;
2381 pad = gst_element_get_static_pad (group->type[type - 1].preroll, "src");
2382 gst_pad_set_active (pad, active);
2383 gst_object_unref (pad);
2384 pad = gst_element_get_static_pad (group->type[type - 1].preroll, "sink");
2385 gst_pad_set_active (pad, active);
2386 gst_object_unref (pad);
2387 pad = gst_element_get_static_pad (group->type[type - 1].selector, "src");
2388 gst_pad_set_active (pad, active);
2389 gst_object_unref (pad);
2393 g_signal_connect (group->type[type - 1].preroll, "state-changed",
2394 G_CALLBACK (muted_group_change_state), group);
2396 g_signal_handlers_disconnect_by_func (group->type[type - 1].preroll,
2397 G_CALLBACK (muted_group_change_state), group);
2404 muted_group_change_state (GstElement * element,
2405 gint old_state, gint new_state, gpointer data)
2407 GstPlayBaseGroup *group = data;
2409 GROUP_LOCK (group->bin);
2411 if (new_state == GST_STATE_PLAYING) {
2414 for (n = 0; n < NUM_TYPES; n++) {
2415 if (group->type[n].selector == element) {
2416 mute_group_type (group, n + 1, TRUE);
2421 GROUP_UNLOCK (group->bin);
2426 set_subtitles_visible (GstPlayBaseBin * play_base_bin, gboolean visible)
2428 GstPlayBaseBinClass *klass = GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin);
2430 /* we use a vfunc for this since we don't have a reference to the
2431 * textoverlay element, but playbin does */
2432 if (klass != NULL && klass->set_subtitles_visible != NULL)
2433 klass->set_subtitles_visible (play_base_bin, visible);
2437 set_audio_mute (GstPlayBaseBin * play_base_bin, gboolean mute)
2439 GstPlayBaseBinClass *klass = GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin);
2441 /* we use a vfunc for this since we don't have a reference to the
2442 * textoverlay element, but playbin does */
2443 if (klass != NULL && klass->set_audio_mute != NULL)
2444 klass->set_audio_mute (play_base_bin, mute);
2448 * Caller has group-lock held.
2452 set_active_source (GstPlayBaseBin * play_base_bin,
2453 GstStreamType type, gint source_num)
2455 GstPlayBaseGroup *group;
2458 gboolean have_active = FALSE;
2461 GST_LOG ("Changing active source of type %d to %d", type, source_num);
2462 play_base_bin->current[type - 1] = source_num;
2464 group = get_active_group (play_base_bin);
2465 if (!group || !group->type[type - 1].preroll) {
2466 GST_LOG ("No active group, or group for type %d has no preroll", type);
2470 /* HACK: instead of unlinking the subtitle input (= lots of hassle,
2471 * especially if subtitles come from an external source), just tell
2472 * textoverlay not to render them */
2473 if (type == GST_STREAM_TYPE_TEXT) {
2474 gboolean visible = (source_num != -1);
2476 set_subtitles_visible (play_base_bin, visible);
2479 } else if (type == GST_STREAM_TYPE_AUDIO) {
2480 gboolean mute = (source_num == -1);
2482 set_audio_mute (play_base_bin, mute);
2488 sel = group->type[type - 1].selector;
2490 for (s = group->streaminfo; s; s = s->next) {
2491 GstStreamInfo *info = s->data;
2493 if (info->type == type) {
2494 if (num == source_num) {
2497 GST_LOG ("Unmuting (if already muted) source %d of type %d", source_num,
2499 g_object_set (info, "mute", FALSE, NULL);
2501 /* Tell the stream selector which pad to accept */
2502 sel_pad = GST_PAD_CAST (g_object_get_data (G_OBJECT (info->object),
2505 if (sel && sel_pad != NULL) {
2506 g_object_set (G_OBJECT (sel), "active-pad", sel_pad, NULL);
2513 GST_LOG_OBJECT (info->object, "Muting source %d of type %d", num, type);
2515 id = gst_pad_add_buffer_probe (GST_PAD_CAST (info->object),
2516 G_CALLBACK (mute_stream), info);
2517 g_object_set_data (G_OBJECT (info), "mute_probe", GINT_TO_POINTER (id));
2524 GST_LOG ("Muting group type: %d", type);
2525 g_object_set (sel, "active-pad", NULL, NULL);
2527 GST_LOG ("Unmuting group type: %d", type);
2529 mute_group_type (group, type, !have_active);
2533 gst_play_base_bin_set_property (GObject * object, guint prop_id,
2534 const GValue * value, GParamSpec * pspec)
2536 GstPlayBaseBin *play_base_bin;
2538 g_return_if_fail (GST_IS_PLAY_BASE_BIN (object));
2540 play_base_bin = GST_PLAY_BASE_BIN (object);
2545 const gchar *uri = g_value_get_string (value);
2548 g_warning ("cannot set NULL uri");
2551 /* if we have no previous uri, or the new uri is different from the
2552 * old one, replug */
2553 if (play_base_bin->uri == NULL || strcmp (play_base_bin->uri, uri) != 0) {
2554 g_free (play_base_bin->uri);
2555 play_base_bin->uri = g_strdup (uri);
2557 GST_DEBUG ("setting new uri to %s", uri);
2559 play_base_bin->need_rebuild = TRUE;
2564 const gchar *suburi = g_value_get_string (value);
2566 if ((!suburi && !play_base_bin->suburi) ||
2567 (suburi && play_base_bin->suburi &&
2568 !strcmp (play_base_bin->suburi, suburi)))
2570 g_free (play_base_bin->suburi);
2571 play_base_bin->suburi = g_strdup (suburi);
2572 GST_DEBUG ("setting new .sub uri to %s", suburi);
2573 play_base_bin->need_rebuild = TRUE;
2576 case ARG_QUEUE_SIZE:
2577 play_base_bin->queue_size = g_value_get_uint64 (value);
2579 case ARG_QUEUE_THRESHOLD:
2580 play_base_bin->queue_threshold = g_value_get_uint64 (value);
2582 case ARG_QUEUE_MIN_THRESHOLD:
2583 play_base_bin->queue_min_threshold = g_value_get_uint64 (value);
2585 case ARG_CONNECTION_SPEED:
2586 play_base_bin->connection_speed = g_value_get_uint (value) * 1000;
2589 GROUP_LOCK (play_base_bin);
2590 set_active_source (play_base_bin,
2591 GST_STREAM_TYPE_VIDEO, g_value_get_int (value));
2592 GROUP_UNLOCK (play_base_bin);
2595 GROUP_LOCK (play_base_bin);
2596 set_active_source (play_base_bin,
2597 GST_STREAM_TYPE_AUDIO, g_value_get_int (value));
2598 GROUP_UNLOCK (play_base_bin);
2601 GROUP_LOCK (play_base_bin);
2602 set_active_source (play_base_bin,
2603 GST_STREAM_TYPE_TEXT, g_value_get_int (value));
2604 GROUP_UNLOCK (play_base_bin);
2606 case ARG_SUBTITLE_ENCODING:
2608 const gchar *encoding;
2611 encoding = g_value_get_string (value);
2612 if (encoding && play_base_bin->subencoding &&
2613 !strcmp (play_base_bin->subencoding, encoding)) {
2616 if (encoding == NULL && play_base_bin->subencoding == NULL)
2619 g_mutex_lock (play_base_bin->sub_lock);
2620 g_free (play_base_bin->subencoding);
2621 play_base_bin->subencoding = g_strdup (encoding);
2622 list = g_slist_copy (play_base_bin->subtitle_elements);
2623 g_slist_foreach (list, (GFunc) gst_object_ref, NULL);
2624 g_mutex_unlock (play_base_bin->sub_lock);
2626 /* we can't hold a lock when calling g_object_set() on a child, since
2627 * the notify event will trigger GstObject to send a deep-notify event
2628 * which will try to take the lock ... */
2629 g_slist_foreach (list, (GFunc) set_encoding_element, (gpointer) encoding);
2630 g_slist_foreach (list, (GFunc) gst_object_unref, NULL);
2631 g_slist_free (list);
2635 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2641 gst_play_base_bin_get_property (GObject * object, guint prop_id, GValue * value,
2644 GstPlayBaseBin *play_base_bin;
2646 g_return_if_fail (GST_IS_PLAY_BASE_BIN (object));
2648 play_base_bin = GST_PLAY_BASE_BIN (object);
2652 g_value_set_string (value, play_base_bin->uri);
2655 g_value_set_string (value, play_base_bin->suburi);
2659 GstPlayBaseGroup *group;
2661 GROUP_LOCK (play_base_bin);
2662 group = get_active_group (play_base_bin);
2664 g_value_set_int (value, group->nstreams);
2666 g_value_set_int (value, 0);
2668 GROUP_UNLOCK (play_base_bin);
2671 case ARG_QUEUE_SIZE:
2672 g_value_set_uint64 (value, play_base_bin->queue_size);
2674 case ARG_QUEUE_THRESHOLD:
2675 g_value_set_uint64 (value, play_base_bin->queue_threshold);
2677 case ARG_QUEUE_MIN_THRESHOLD:
2678 g_value_set_uint64 (value, play_base_bin->queue_min_threshold);
2680 case ARG_CONNECTION_SPEED:
2681 g_value_set_uint (value, play_base_bin->connection_speed / 1000);
2683 case ARG_STREAMINFO:
2684 /* FIXME: hold some kind of lock here, use iterator */
2685 g_value_set_pointer (value,
2686 (gpointer) gst_play_base_bin_get_streaminfo (play_base_bin));
2688 case ARG_STREAMINFO_VALUES:{
2691 copy = gst_play_base_bin_get_streaminfo_value_array (play_base_bin);
2692 g_value_take_boxed (value, copy);
2696 g_value_set_object (value, play_base_bin->source);
2699 GROUP_LOCK (play_base_bin);
2700 g_value_set_int (value, get_active_source (play_base_bin,
2701 GST_STREAM_TYPE_VIDEO));
2702 GROUP_UNLOCK (play_base_bin);
2705 GROUP_LOCK (play_base_bin);
2706 g_value_set_int (value, get_active_source (play_base_bin,
2707 GST_STREAM_TYPE_AUDIO));
2708 GROUP_UNLOCK (play_base_bin);
2711 GROUP_LOCK (play_base_bin);
2712 g_value_set_int (value, get_active_source (play_base_bin,
2713 GST_STREAM_TYPE_TEXT));
2714 GROUP_UNLOCK (play_base_bin);
2716 case ARG_SUBTITLE_ENCODING:
2717 GST_OBJECT_LOCK (play_base_bin);
2718 g_value_set_string (value, play_base_bin->subencoding);
2719 GST_OBJECT_UNLOCK (play_base_bin);
2722 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2727 static GstStateChangeReturn
2728 gst_play_base_bin_change_state (GstElement * element, GstStateChange transition)
2730 GstStateChangeReturn ret;
2731 GstPlayBaseBin *play_base_bin;
2733 play_base_bin = GST_PLAY_BASE_BIN (element);
2735 switch (transition) {
2736 case GST_STATE_CHANGE_READY_TO_PAUSED:
2737 if (!setup_source (play_base_bin))
2744 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2746 switch (transition) {
2747 case GST_STATE_CHANGE_READY_TO_PAUSED:
2748 if (ret == GST_STATE_CHANGE_FAILURE)
2749 goto cleanup_groups;
2751 finish_source (play_base_bin);
2753 /* clean-up in both cases, READY=>NULL clean-up is if there was an error */
2754 case GST_STATE_CHANGE_PAUSED_TO_READY:
2755 case GST_STATE_CHANGE_READY_TO_NULL:
2756 play_base_bin->need_rebuild = TRUE;
2757 remove_decoders (play_base_bin);
2758 remove_groups (play_base_bin);
2759 remove_source (play_base_bin);
2769 play_base_bin->need_rebuild = TRUE;
2771 return GST_STATE_CHANGE_FAILURE;
2775 /* clean up leftover groups */
2776 remove_groups (play_base_bin);
2777 play_base_bin->need_rebuild = TRUE;
2779 return GST_STATE_CHANGE_FAILURE;
2783 static const GList *
2784 gst_play_base_bin_get_streaminfo (GstPlayBaseBin * play_base_bin)
2786 GstPlayBaseGroup *group = get_active_group (play_base_bin);
2790 info = group->streaminfo;
2795 static GValueArray *
2796 gst_play_base_bin_get_streaminfo_value_array (GstPlayBaseBin * play_base_bin)
2798 GstPlayBaseGroup *group;
2799 GValueArray *array = NULL;
2801 GROUP_LOCK (play_base_bin);
2802 group = get_active_group (play_base_bin);
2804 array = g_value_array_copy (group->streaminfo_value_array);
2806 array = g_value_array_new (0);
2808 GROUP_UNLOCK (play_base_bin);