tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.git] / gst / playback / gstplaybasebin.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst-i18n-plugin.h>
25 #include <string.h>
26 #include "gstplaybasebin.h"
27 #include "gststreamselector.h"
28 #include "gstplay-marshal.h"
29
30 #include <gst/pbutils/pbutils.h>
31
32 #include "gst/glib-compat-private.h"
33
34 GST_DEBUG_CATEGORY_STATIC (gst_play_base_bin_debug);
35 #define GST_CAT_DEFAULT gst_play_base_bin_debug
36
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
41
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)
46
47 /* props */
48 enum
49 {
50   ARG_0,
51   ARG_URI,
52   ARG_SUBURI,
53   ARG_QUEUE_SIZE,
54   ARG_QUEUE_THRESHOLD,
55   ARG_QUEUE_MIN_THRESHOLD,
56   ARG_NSTREAMS,
57   ARG_STREAMINFO,
58   ARG_STREAMINFO_VALUES,
59   ARG_SOURCE,
60   ARG_VIDEO,
61   ARG_AUDIO,
62   ARG_TEXT,
63   ARG_SUBTITLE_ENCODING,
64   ARG_CONNECTION_SPEED
65 };
66
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);
71
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,
77     GstMessage * msg);
78
79 static GstStateChangeReturn gst_play_base_bin_change_state (GstElement *
80     element, GstStateChange transition);
81
82 static const GList *gst_play_base_bin_get_streaminfo (GstPlayBaseBin * bin);
83 static GValueArray *gst_play_base_bin_get_streaminfo_value_array (GstPlayBaseBin
84     * play_base_bin);
85 static void preroll_remove_overrun (GstElement * element,
86     GstPlayBaseBin * play_base_bin);
87 static void queue_remove_probe (GstElement * queue, GstPlayBaseBin
88     * play_base_bin);
89
90 static GstElement *make_decoder (GstPlayBaseBin * play_base_bin);
91 static gboolean has_all_raw_caps (GstPad * pad, gboolean * all_raw);
92
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,
97     gpointer user_data);
98 static void setup_substreams (GstPlayBaseBin * play_base_bin);
99
100 static GstPipelineClass *parent_class;
101
102 /*
103  * GObject playbasebin wrappers.
104  */
105
106 GType
107 gst_play_base_bin_get_type (void)
108 {
109   static GType gst_play_base_bin_type = 0;
110
111   if (!gst_play_base_bin_type) {
112     static const GTypeInfo gst_play_base_bin_info = {
113       sizeof (GstPlayBaseBinClass),
114       NULL,
115       NULL,
116       (GClassInitFunc) gst_play_base_bin_class_init,
117       NULL,
118       NULL,
119       sizeof (GstPlayBaseBin),
120       0,
121       (GInstanceInitFunc) gst_play_base_bin_init,
122       NULL
123     };
124
125     gst_play_base_bin_type = g_type_register_static (GST_TYPE_PIPELINE,
126         "GstPlayBaseBin", &gst_play_base_bin_info, 0);
127   }
128
129   return gst_play_base_bin_type;
130 }
131
132 static void
133 gst_play_base_bin_class_init (GstPlayBaseBinClass * klass)
134 {
135   GObjectClass *gobject_klass;
136   GstElementClass *gstelement_klass;
137   GstBinClass *gstbin_klass;
138
139   gobject_klass = (GObjectClass *) klass;
140   gstelement_klass = (GstElementClass *) klass;
141   gstbin_klass = (GstBinClass *) klass;
142
143   parent_class = g_type_class_peek_parent (klass);
144
145   gobject_klass->set_property = gst_play_base_bin_set_property;
146   gobject_klass->get_property = gst_play_base_bin_get_property;
147
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));
154
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));
169
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));
185
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));
205   /**
206    * GstPlayBaseBin:connection-speed
207    *
208    * Network connection speed in kbps (0 = unknown)
209    * <note><simpara>
210    * Since version 0.10.10 in #GstPlayBin, at 0.10.15 moved to #GstPlayBaseBin
211    * </simpara></note>
212    *
213    * Since: 0.10.10
214    */
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));
220
221   GST_DEBUG_CATEGORY_INIT (gst_play_base_bin_debug, "playbasebin", 0,
222       "playbasebin");
223
224   gobject_klass->dispose = gst_play_base_bin_dispose;
225   gobject_klass->finalize = gst_play_base_bin_finalize;
226
227   gstbin_klass->handle_message =
228       GST_DEBUG_FUNCPTR (gst_play_base_bin_handle_message_func);
229
230   gstelement_klass->change_state =
231       GST_DEBUG_FUNCPTR (gst_play_base_bin_change_state);
232 }
233
234 static void
235 gst_play_base_bin_init (GstPlayBaseBin * play_base_bin)
236 {
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 ();
247
248   play_base_bin->group_lock = g_mutex_new ();
249   play_base_bin->group_cond = g_cond_new ();
250
251   play_base_bin->building_group = NULL;
252   play_base_bin->queued_groups = NULL;
253
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;
258 }
259
260 static void
261 gst_play_base_bin_dispose (GObject * object)
262 {
263   GstPlayBaseBin *play_base_bin;
264
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;
275   }
276   G_OBJECT_CLASS (parent_class)->dispose (object);
277 }
278
279 static void
280 gst_play_base_bin_finalize (GObject * object)
281 {
282   GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (object);
283
284   g_mutex_free (play_base_bin->group_lock);
285   g_cond_free (play_base_bin->group_cond);
286
287   g_mutex_free (play_base_bin->sub_lock);
288
289   G_OBJECT_CLASS (parent_class)->finalize (object);
290 }
291
292 /*
293  * playbasebingroup stuff.
294  */
295
296 static GstPlayBaseGroup *
297 group_create (GstPlayBaseBin * play_base_bin)
298 {
299   GstPlayBaseGroup *group;
300
301   group = g_new0 (GstPlayBaseGroup, 1);
302   group->bin = play_base_bin;
303   group->streaminfo_value_array = g_value_array_new (0);
304
305   GST_DEBUG_OBJECT (play_base_bin, "created new group %p", group);
306
307   return group;
308 }
309
310 /*
311  * Gets the currently playing group.
312  *
313  * Callers must have group-lock held when calling this.
314  */
315
316 static GstPlayBaseGroup *
317 get_active_group (GstPlayBaseBin * play_base_bin)
318 {
319   GstPlayBaseGroup *group = NULL;
320
321   if (play_base_bin->queued_groups)
322     group = play_base_bin->queued_groups->data;
323
324   return group;
325 }
326
327 /*
328  * get the group used for discovering the different streams.
329  * This function creates a group is there is none.
330  *
331  * Callers must have group-lock held when calling this.
332  */
333 static GstPlayBaseGroup *
334 get_building_group (GstPlayBaseBin * play_base_bin)
335 {
336   GstPlayBaseGroup *group;
337
338   group = play_base_bin->building_group;
339   if (group == NULL) {
340     group = group_create (play_base_bin);
341     play_base_bin->building_group = group;
342   }
343
344   return group;
345 }
346
347 /*
348  * Callers must have lock held when calling this!
349  */
350
351 static void
352 group_destroy (GstPlayBaseGroup * group)
353 {
354   GstPlayBaseBin *play_base_bin = group->bin;
355   gint n;
356
357   GST_LOG ("removing group %p", group);
358
359   /* remove the preroll queues */
360   for (n = 0; n < NUM_TYPES; n++) {
361     GstElement *element = group->type[n].preroll;
362     GstElement *fakesrc;
363     GstElement *sel;
364     const GList *item;
365
366     if (!element)
367       continue;
368
369     sel = group->type[n].selector;
370
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);
374       guint sig_id;
375
376       if (GST_PAD_DIRECTION (pad) != GST_PAD_SINK)
377         continue;
378
379       sig_id =
380           GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pad), "unlinked_id"));
381
382       if (sig_id != 0) {
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));
386       }
387
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);
394       }
395     }
396
397     /* if the group is currently being played, we have to remove the element
398      * from the thread */
399     gst_element_set_state (element, GST_STATE_NULL);
400     gst_element_set_state (group->type[n].selector, GST_STATE_NULL);
401
402     GST_LOG ("removing preroll element %s", GST_ELEMENT_NAME (element));
403
404     gst_bin_remove (group->type[n].bin, element);
405     gst_bin_remove (group->type[n].bin, group->type[n].selector);
406
407     group->type[n].preroll = NULL;
408     group->type[n].selector = NULL;
409     group->type[n].bin = NULL;
410   }
411
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);
416   g_free (group);
417 }
418
419 /*
420  * is called when the current building group is completely finished
421  * and ready for playback
422  *
423  * This function grabs lock, so take care when calling.
424  */
425 static void
426 group_commit (GstPlayBaseBin * play_base_bin, gboolean fatal, gboolean subtitle)
427 {
428   GstPlayBaseGroup *group;
429   gboolean had_active_group;
430
431   GROUP_LOCK (play_base_bin);
432   group = play_base_bin->building_group;
433   had_active_group = (get_active_group (play_base_bin) != NULL);
434
435   GST_DEBUG_OBJECT (play_base_bin, "commit group %p, had active %d",
436       group, had_active_group);
437
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 */
440   if (group == NULL) {
441     if (!fatal) {
442       GROUP_UNLOCK (play_base_bin);
443       return;
444     } else {
445       GST_DEBUG_OBJECT (play_base_bin, "Group loading failed, bailing out");
446     }
447   } else {
448     if (!subtitle) {
449       gint n;
450
451       GST_DEBUG_OBJECT (play_base_bin, "group %p done", group);
452
453       play_base_bin->queued_groups =
454           g_list_append (play_base_bin->queued_groups, group);
455
456       play_base_bin->building_group = NULL;
457
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;
462
463         if (!element)
464           continue;
465
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);
469       }
470     } else {
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;
475     }
476   }
477
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");
481
482   if (!subtitle && !had_active_group) {
483     if (!prepare_output (play_base_bin)) {
484       GROUP_UNLOCK (play_base_bin);
485       return;
486     }
487
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");
493
494     GROUP_UNLOCK (play_base_bin);
495
496     g_object_notify (G_OBJECT (play_base_bin), "stream-info");
497   } else {
498     GROUP_UNLOCK (play_base_bin);
499   }
500 }
501
502 /*
503  * check if there are streams in the group that are not muted
504  *
505  * Callers must have group-lock held when calling this.
506  */
507 static gboolean
508 group_is_muted (GstPlayBaseGroup * group)
509 {
510   gint n;
511
512   for (n = 0; n < NUM_TYPES; n++) {
513     if (group->type[n].preroll && !group->type[n].done)
514       return FALSE;
515   }
516
517   return TRUE;
518 }
519
520 /*
521  * Buffer/cache checking.
522  */
523
524 static inline void
525 fill_buffer (GstPlayBaseBin * play_base_bin, gint percent)
526 {
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));
530 }
531
532 static gboolean
533 check_queue_event (GstPad * pad, GstEvent * event, gpointer user_data)
534 {
535   GstElement *queue = GST_ELEMENT_CAST (user_data);
536
537   switch (GST_EVENT_TYPE (event)) {
538     case GST_EVENT_EOS:
539       GST_DEBUG ("EOS event, mark EOS");
540       g_object_set_data (G_OBJECT (queue), "eos", GINT_TO_POINTER (1));
541       break;
542     case GST_EVENT_FLUSH_STOP:
543       GST_DEBUG ("FLUSH_STOP event, remove EOS");
544       g_object_set_data (G_OBJECT (queue), "eos", NULL);
545       break;
546     default:
547       GST_DEBUG ("uninteresting event %s", GST_EVENT_TYPE_NAME (event));
548       break;
549   }
550   return TRUE;
551 }
552
553 static gboolean
554 check_queue (GstPad * pad, GstBuffer * data, gpointer user_data)
555 {
556   GstElement *queue = GST_ELEMENT_CAST (user_data);
557   GstPlayBaseBin *play_base_bin = g_object_get_data (G_OBJECT (queue), "pbb");
558   guint64 level = 0;
559
560   GST_DEBUG_OBJECT (queue, "check queue triggered");
561
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));
565
566   if (play_base_bin->queue_threshold > 0) {
567     level = level * 99 / play_base_bin->queue_threshold;
568     if (level > 99)
569       level = 99;
570   } else
571     level = 99;
572
573   fill_buffer (play_base_bin, level);
574
575   /* continue! */
576   return TRUE;
577 }
578
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:
581  *
582  *  1) the max-bytes is hit and
583  *  2) the min-time is not hit.
584  *
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
588  * done).
589  */
590 static void
591 queue_deadlock_check (GstElement * queue, GstPlayBaseBin * play_base_bin)
592 {
593   guint64 time, min_time;
594   guint bytes;
595
596   GST_DEBUG_OBJECT (play_base_bin, "overrun signal received from queue %s",
597       GST_ELEMENT_NAME (queue));
598
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);
602
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);
607
608   /* if the bytes in the queue represent time, we disable bytes
609    * overrun checking to not cause deadlocks.
610    */
611   if (bytes && time != 0 && time < min_time) {
612     GST_DEBUG_OBJECT (play_base_bin,
613         "possible deadlock found, removing byte limit");
614
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);
619
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);
623   } else {
624     GST_DEBUG_OBJECT (play_base_bin, "no deadlock");
625   }
626 }
627
628 static void
629 queue_remove_probe (GstElement * queue, GstPlayBaseBin * play_base_bin)
630 {
631   gpointer data;
632   GstPad *sinkpad;
633
634   data = g_object_get_data (G_OBJECT (queue), "probe");
635   sinkpad = gst_element_get_static_pad (queue, "sink");
636
637   if (data) {
638     GST_DEBUG_OBJECT (play_base_bin,
639         "Removing buffer probe from pad %s:%s (%p)",
640         GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
641
642     g_object_set_data (G_OBJECT (queue), "probe", NULL);
643     gst_pad_remove_buffer_probe (sinkpad, GPOINTER_TO_INT (data));
644   } else {
645     GST_DEBUG_OBJECT (play_base_bin,
646         "No buffer probe to remove from %s:%s (%p)",
647         GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
648   }
649   gst_object_unref (sinkpad);
650 }
651
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. */
655 static void
656 queue_threshold_reached (GstElement * queue, GstPlayBaseBin * play_base_bin)
657 {
658   GstPlayBaseGroup *group;
659   gint n;
660
661   GST_DEBUG_OBJECT (play_base_bin, "running signal received from queue %s",
662       GST_ELEMENT_NAME (queue));
663
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);
667
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);
671   } else {
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);
679   }
680
681   GROUP_LOCK (play_base_bin);
682   group = get_active_group (play_base_bin);
683   if (!group) {
684     GROUP_UNLOCK (play_base_bin);
685     return;
686   }
687
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;
692
693     if (!element)
694       continue;
695
696     queue_remove_probe (element, play_base_bin);
697   }
698
699   GROUP_UNLOCK (play_base_bin);
700
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);
705 }
706
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"
711  * signal is fired.
712  */
713 static void
714 queue_overrun (GstElement * queue, GstPlayBaseBin * play_base_bin)
715 {
716   GST_DEBUG_OBJECT (play_base_bin, "queue %s overrun",
717       GST_ELEMENT_NAME (queue));
718
719   preroll_remove_overrun (queue, play_base_bin);
720
721   group_commit (play_base_bin, FALSE,
722       GST_OBJECT_PARENT (GST_OBJECT_CAST (queue)) ==
723       GST_OBJECT_CAST (play_base_bin->subtitle));
724
725   /* notify end of buffering */
726   queue_threshold_reached (queue, play_base_bin);
727 }
728
729 /* this signal is only added when in streaming mode to catch underruns
730  */
731 static void
732 queue_out_of_data (GstElement * queue, GstPlayBaseBin * play_base_bin)
733 {
734   GST_DEBUG_OBJECT (play_base_bin, "underrun signal received from queue %s",
735       GST_ELEMENT_NAME (queue));
736
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.
739    *
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
742    * handler. */
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);
750
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")) {
754     GstPad *sinkpad;
755     guint id;
756
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);
764
765     fill_buffer (play_base_bin, 0);
766   }
767 }
768
769 /*
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.
774  *
775  * Callers must have the group-lock held when calling this.
776  */
777 static void
778 gen_preroll_element (GstPlayBaseBin * play_base_bin,
779     GstPlayBaseGroup * group, GstStreamType type, GstPad * pad,
780     GstStreamInfo * info)
781 {
782   GstElement *selector, *preroll;
783   gchar *name, *padname;
784   const gchar *prename;
785   guint overrun_sig;
786   GstPad *preroll_pad;
787   GstBin *target;
788   GstState state;
789
790   if (type == GST_STREAM_TYPE_VIDEO)
791     prename = "video";
792   else if (type == GST_STREAM_TYPE_TEXT)
793     prename = "text";
794   else if (type == GST_STREAM_TYPE_AUDIO)
795     prename = "audio";
796   else if (type == GST_STREAM_TYPE_SUBPICTURE)
797     prename = "subpicture";
798   else
799     g_return_if_reached ();
800
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);
805   g_free (name);
806
807   /* create preroll queue */
808   name = g_strdup_printf ("preroll_%s_%s", prename, padname);
809   preroll = gst_element_factory_make ("queue", name);
810   g_free (name);
811   g_free (padname);
812
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.
816    *
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!).
819    *
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.
825    */
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);
831     } else {
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);
835     }
836   } else {
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);
841   }
842
843   /* the overrun signal is always attached and serves two purposes:
844    *
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.
855    */
856   overrun_sig = g_signal_connect (G_OBJECT (preroll), "overrun",
857       G_CALLBACK (queue_overrun), play_base_bin);
858
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));
863
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))) {
869     GstPad *sinkpad;
870     guint id;
871
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);
876
877     /* attach pointer to playbasebin */
878     g_object_set_data (G_OBJECT (preroll), "pbb", play_base_bin);
879
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));
886
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),
889         preroll);
890     g_object_set_data (G_OBJECT (preroll), "eos_probe", GINT_TO_POINTER (id));
891
892     gst_object_unref (sinkpad);
893
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);
900   }
901
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);
906
907   /* add to group list */
908   group->type[type - 1].selector = selector;
909   group->type[type - 1].preroll = preroll;
910
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);
914   else
915     target = GST_BIN_CAST (play_base_bin);
916
917   group->type[type - 1].bin = target;
918   gst_bin_add (target, selector);
919   gst_bin_add (target, preroll);
920
921   gst_element_link (selector, preroll);
922
923   /* figure out target state and set */
924   state = (GST_STATE (play_base_bin) == GST_STATE_PLAYING ?
925       GST_STATE_PLAYING : GST_STATE_PAUSED);
926
927   gst_element_set_state (selector, state);
928   gst_element_set_state (preroll, state);
929 }
930
931 static void
932 preroll_remove_overrun (GstElement * element, GstPlayBaseBin * play_base_bin)
933 {
934   guint overrun_sig;
935   GObject *obj = G_OBJECT (element);
936
937   overrun_sig = GPOINTER_TO_INT (g_object_get_data (obj, "overrun_signal_id"));
938   if (overrun_sig) {
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
943      * data */
944     g_object_set_data (obj, "overrun_signal_id", NULL);
945   }
946 }
947
948 static void
949 remove_groups (GstPlayBaseBin * play_base_bin)
950 {
951   GROUP_LOCK (play_base_bin);
952
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;
957   }
958
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;
963
964   /* clear subs */
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;
969   }
970
971   GROUP_UNLOCK (play_base_bin);
972 }
973
974 /*
975  * Add/remove a single stream to current building group.
976  *
977  * Must be called with group-lock held.
978  */
979 static void
980 add_stream (GstPlayBaseGroup * group, GstStreamInfo * info)
981 {
982   GValue v = { 0, };
983   GST_DEBUG ("add stream to group %p", group);
984
985   /* keep ref to the group */
986   g_object_set_data (G_OBJECT (info), "group", group);
987
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);
991   g_value_unset (&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++;
995   }
996 }
997
998 static gboolean
999 string_arr_has_str (const gchar * values[], const gchar * value)
1000 {
1001   if (values && value) {
1002     while (*values != NULL) {
1003       if (strcmp (value, *values) == 0)
1004         return TRUE;
1005       ++values;
1006     }
1007   }
1008   return FALSE;
1009 }
1010
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
1015 };
1016
1017 #define IS_BLACKLISTED_MIME(type) (string_arr_has_str(blacklisted_mimes,type))
1018
1019 static void
1020 gst_play_base_bin_handle_message_func (GstBin * bin, GstMessage * msg)
1021 {
1022   if (gst_is_missing_plugin_message (msg)) {
1023     gchar *detail;
1024     guint i;
1025
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);
1031         g_free (detail);
1032         return;
1033       }
1034     }
1035     g_free (detail);
1036   }
1037   GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
1038 }
1039
1040 /*
1041  * signal fired when an unknown stream is found. We create a new
1042  * UNKNOWN streaminfo object.
1043  */
1044 static void
1045 unknown_type (GstElement * element, GstPad * pad, GstCaps * caps,
1046     GstPlayBaseBin * play_base_bin)
1047 {
1048   const gchar *type_name;
1049   GstStreamInfo *info;
1050   GstPlayBaseGroup *group;
1051
1052   type_name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
1053   if (type_name && !IS_BLACKLISTED_MIME (type_name)) {
1054     gchar *capsstr;
1055
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);
1060     g_free (capsstr);
1061   } else {
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);
1065   }
1066
1067   GROUP_LOCK (play_base_bin);
1068
1069   group = get_building_group (play_base_bin);
1070
1071   /* add the stream to the list */
1072   info = gst_stream_info_new (GST_OBJECT_CAST (pad), GST_STREAM_TYPE_UNKNOWN,
1073       NULL, caps);
1074   info->origin = GST_OBJECT_CAST (pad);
1075   add_stream (group, info);
1076
1077   GROUP_UNLOCK (play_base_bin);
1078 }
1079
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...
1084  */
1085 static void
1086 add_element_stream (GstElement * element, GstPlayBaseBin * play_base_bin)
1087 {
1088   GstStreamInfo *info;
1089   GstPlayBaseGroup *group;
1090
1091   GROUP_LOCK (play_base_bin);
1092
1093   group = get_building_group (play_base_bin);
1094
1095   /* add the stream to the list */
1096   info =
1097       gst_stream_info_new (GST_OBJECT_CAST (element), GST_STREAM_TYPE_ELEMENT,
1098       NULL, NULL);
1099   info->origin = GST_OBJECT_CAST (element);
1100   add_stream (group, info);
1101
1102   GROUP_UNLOCK (play_base_bin);
1103 }
1104
1105 /* when the decoder element signals that no more pads will be generated, we
1106  * can commit the current group.
1107  */
1108 static void
1109 no_more_pads_full (GstElement * element, gboolean subs,
1110     GstPlayBaseBin * play_base_bin)
1111 {
1112   /* setup phase */
1113   GST_DEBUG_OBJECT (element, "no more pads, %d pending",
1114       play_base_bin->pending);
1115
1116   /* nothing pending, we can exit */
1117   if (play_base_bin->pending == 0)
1118     return;
1119
1120   /* the object has no pending no_more_pads */
1121   if (!g_object_get_data (G_OBJECT (element), "pending"))
1122     return;
1123
1124   g_object_set_data (G_OBJECT (element), "pending", NULL);
1125
1126   play_base_bin->pending--;
1127
1128   GST_DEBUG_OBJECT (element, "remove pending, now %d pending",
1129       play_base_bin->pending);
1130
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);
1134   }
1135 }
1136
1137 static void
1138 no_more_pads (GstElement * element, GstPlayBaseBin * play_base_bin)
1139 {
1140   no_more_pads_full (element, FALSE, play_base_bin);
1141 }
1142
1143 static void
1144 sub_no_more_pads (GstElement * element, GstPlayBaseBin * play_base_bin)
1145 {
1146   no_more_pads_full (element, TRUE, play_base_bin);
1147 }
1148
1149 static void
1150 source_no_more_pads (GstElement * element, GstPlayBaseBin * bin)
1151 {
1152   GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
1153       GST_ELEMENT_NAME (element));
1154
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;
1159
1160   no_more_pads_full (element, FALSE, bin);
1161 }
1162
1163 static gboolean
1164 probe_triggered (GstPad * pad, GstEvent * event, gpointer user_data)
1165 {
1166   GstPlayBaseGroup *group;
1167   GstPlayBaseBin *play_base_bin;
1168   GstStreamInfo *info;
1169   GstEventType type;
1170
1171   type = GST_EVENT_TYPE (event);
1172
1173   GST_LOG ("probe triggered, (%d) %s", type, gst_event_type_get_name (type));
1174
1175   /* we only care about EOS */
1176   if (type != GST_EVENT_EOS)
1177     return TRUE;
1178
1179   info = GST_STREAM_INFO (user_data);
1180   group = (GstPlayBaseGroup *) g_object_get_data (G_OBJECT (info), "group");
1181   play_base_bin = group->bin;
1182
1183   if (type == GST_EVENT_EOS) {
1184     gint num_groups = 0;
1185     gboolean have_left;
1186
1187     GST_DEBUG_OBJECT (play_base_bin, "probe got EOS in group %p", group);
1188
1189     GROUP_LOCK (play_base_bin);
1190
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;
1195
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)
1199       num_groups++;
1200     have_left = (num_groups > 1);
1201
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);
1208
1209       GROUP_UNLOCK (play_base_bin);
1210
1211       /* remove the EOS if we have something left */
1212       return !have_left;
1213     }
1214
1215     if (have_left) {
1216       /* ok, get rid of the current group then */
1217       //group_destroy (group);
1218       /* removing the current group brings the next group
1219        * active */
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);
1225       }
1226       group = play_base_bin->queued_groups->data;
1227
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);
1234
1235       GROUP_UNLOCK (play_base_bin);
1236
1237       g_object_notify (G_OBJECT (play_base_bin), "stream-info");
1238
1239       /* get rid of the EOS event */
1240       return FALSE;
1241     } else {
1242       GROUP_UNLOCK (play_base_bin);
1243       GST_LOG ("Last group done, EOS");
1244     }
1245   }
1246
1247   return TRUE;
1248 }
1249
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..
1253  */
1254 static void
1255 preroll_unlinked (GstPad * pad, GstPad * peerpad,
1256     GstPlayBaseBin * play_base_bin)
1257 {
1258   GstElement *fakesrc;
1259   guint sig_id;
1260   GstPad *srcpad;
1261
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);
1265
1266   GST_DEBUG ("patching unlinked pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1267
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);
1272
1273   /* keep track of these patch elements */
1274   g_object_set_data (G_OBJECT (pad), "fakesrc", fakesrc);
1275
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"));
1279   if (sig_id != 0) {
1280     g_signal_handler_disconnect (G_OBJECT (pad), sig_id);
1281     g_object_set_data (G_OBJECT (pad), "unlinked_id", GINT_TO_POINTER (0));
1282   }
1283 }
1284
1285 /* Mute stream on first data - for header-is-in-stream-stuff
1286  * (vorbis, ogmtext). */
1287 static gboolean
1288 mute_stream (GstPad * pad, GstBuffer * buf, gpointer data)
1289 {
1290   GstStreamInfo *info = GST_STREAM_INFO (data);
1291   guint id;
1292
1293   GST_DEBUG ("mute stream triggered");
1294
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);
1298   if (id > 0)
1299     gst_pad_remove_buffer_probe (GST_PAD_CAST (info->object), id);
1300
1301   /* no data */
1302   return FALSE;
1303 }
1304
1305 /* Eat data. */
1306 static gboolean
1307 silence_stream (GstPad * pad, GstMiniObject * data, gpointer user_data)
1308 {
1309   GST_DEBUG ("silence stream triggered");
1310
1311   /* no data */
1312   return FALSE;
1313 }
1314
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
1318  * from decodebin */
1319 static void
1320 new_decoded_pad_full (GstElement * element, GstPad * pad, gboolean last,
1321     GstPlayBaseBin * play_base_bin, gboolean is_subs)
1322 {
1323   GstStructure *structure;
1324   const gchar *mimetype;
1325   GstCaps *caps;
1326   GstStreamInfo *info;
1327   GstStreamType type = GST_STREAM_TYPE_UNKNOWN;
1328   GstPad *sinkpad;
1329   GstPlayBaseGroup *group;
1330   guint sig;
1331   GstObject *parent;
1332   gboolean first_pad;
1333
1334   GST_DEBUG ("play base: new decoded pad. Last: %d", last);
1335
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))
1339     goto no_type;
1340
1341   /* get the mime type */
1342   structure = gst_caps_get_structure (caps, 0);
1343   mimetype = gst_structure_get_name (structure);
1344
1345   GROUP_LOCK (play_base_bin);
1346
1347   group = get_building_group (play_base_bin);
1348
1349   group->nstreams++;
1350
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;
1363   }
1364   gst_object_unref (parent);
1365
1366   info = gst_stream_info_new (GST_OBJECT_CAST (pad), type, NULL, caps);
1367   gst_caps_unref (caps);
1368
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);
1373
1374     GROUP_UNLOCK (play_base_bin);
1375
1376     /* signal the no more pads after adding the stream */
1377     if (last)
1378       no_more_pads_full (element, is_subs, play_base_bin);
1379
1380     return;
1381   }
1382
1383   /* first pad of each type gets a selector + preroll queue */
1384   first_pad = (group->type[type - 1].npads == 0);
1385
1386   if (first_pad) {
1387     GST_DEBUG ("play base: pad needs new preroll");
1388     gen_preroll_element (play_base_bin, group, type, pad, info);
1389   }
1390
1391   /* add to stream selector */
1392   sinkpad =
1393       gst_element_get_request_pad (group->type[type - 1].selector, "sink%d");
1394
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);
1402
1403   gst_pad_link (pad, sinkpad);
1404   gst_object_unref (sinkpad);
1405
1406   /* select 1st for now - we'll select a preferred one after preroll */
1407   if (!first_pad) {
1408     guint id;
1409
1410     GST_DEBUG ("Adding silence_stream data probe on type %d (npads %d)", type,
1411         group->type[type - 1].npads);
1412
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));
1416   }
1417
1418   /* add the stream to the list */
1419   add_stream (group, info);
1420
1421   GROUP_UNLOCK (play_base_bin);
1422
1423   /* signal the no more pads after adding the stream */
1424   if (last)
1425     no_more_pads_full (element, is_subs, play_base_bin);
1426
1427   return;
1428
1429   /* ERRORS */
1430 no_type:
1431   {
1432     g_warning ("no type on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1433     if (caps)
1434       gst_caps_unref (caps);
1435     return;
1436   }
1437 }
1438
1439 static void
1440 new_decoded_pad (GstElement * element, GstPad * pad, gboolean last,
1441     GstPlayBaseBin * play_base_bin)
1442 {
1443   new_decoded_pad_full (element, pad, last, play_base_bin, FALSE);
1444 }
1445
1446 static void
1447 subs_new_decoded_pad (GstElement * element, GstPad * pad, gboolean last,
1448     GstPlayBaseBin * play_base_bin)
1449 {
1450   new_decoded_pad_full (element, pad, last, play_base_bin, TRUE);
1451 }
1452
1453 static void
1454 set_encoding_element (GstElement * element, gchar * encoding)
1455 {
1456   GST_DEBUG_OBJECT (element, "setting encoding to %s", GST_STR_NULL (encoding));
1457   g_object_set (G_OBJECT (element), "subtitle-encoding", encoding, NULL);
1458 }
1459
1460
1461 static void
1462 decodebin_element_added_cb (GstBin * decodebin, GstElement * element,
1463     gpointer data)
1464 {
1465   GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (data);
1466   gchar *encoding;
1467
1468   if (!g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1469           "subtitle-encoding")) {
1470     return;
1471   }
1472
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);
1478
1479   set_encoding_element (element, encoding);
1480   g_free (encoding);
1481 }
1482
1483 static void
1484 decodebin_element_removed_cb (GstBin * decodebin, GstElement * element,
1485     gpointer data)
1486 {
1487   GstPlayBaseBin *play_base_bin = GST_PLAY_BASE_BIN (data);
1488
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);
1493 }
1494
1495
1496 /*
1497  * Generate source ! subparse bins.
1498  */
1499
1500 static GstElement *
1501 setup_subtitle (GstPlayBaseBin * play_base_bin, gchar * sub_uri)
1502 {
1503   GstElement *source, *subdecodebin, *subbin;
1504
1505   if (!gst_uri_is_valid (sub_uri))
1506     goto invalid_uri;
1507
1508   source = gst_element_make_from_uri (GST_URI_SRC, sub_uri, NULL);
1509   if (!source)
1510     goto unknown_uri;
1511
1512   if (g_getenv ("USE_DECODEBIN2"))
1513     subdecodebin = gst_element_factory_make ("decodebin2", "subtitle-decoder");
1514   else
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);
1522
1523   gst_element_link (source, subdecodebin);
1524
1525   /* return the subtitle GstElement object */
1526   return subbin;
1527
1528   /* WARNINGS */
1529 invalid_uri:
1530   {
1531     GST_ELEMENT_WARNING (play_base_bin, RESOURCE, NOT_FOUND,
1532         (_("Invalid subtitle URI \"%s\", subtitles disabled."), sub_uri),
1533         (NULL));
1534     return NULL;
1535   }
1536 unknown_uri:
1537   {
1538     gchar *prot = gst_uri_get_protocol (sub_uri);
1539
1540     if (prot) {
1541       gchar *desc;
1542
1543       gst_element_post_message (GST_ELEMENT (play_base_bin),
1544           gst_missing_uri_source_message_new (GST_ELEMENT (play_base_bin),
1545               prot));
1546
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));
1551       g_free (desc);
1552       g_free (prot);
1553     } else
1554       goto invalid_uri;
1555
1556     return NULL;
1557   }
1558 }
1559
1560 /* helper function to lookup stuff in lists */
1561 static gboolean
1562 array_has_value (const gchar * values[], const gchar * value)
1563 {
1564   gint i;
1565
1566   for (i = 0; values[i]; i++) {
1567     if (g_str_has_prefix (value, values[i]))
1568       return TRUE;
1569   }
1570   return FALSE;
1571 }
1572
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
1577 };
1578
1579 /* blacklisted URIs, we know they will always fail. */
1580 static const gchar *blacklisted_uris[] = { NULL };
1581
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
1586 };
1587
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
1591 };
1592
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))
1597
1598 /*
1599  * Generate and configure a source element.
1600  */
1601 static GstElement *
1602 gen_source_element (GstPlayBaseBin * play_base_bin, GstElement ** subbin)
1603 {
1604   GstElement *source;
1605
1606   if (!play_base_bin->uri)
1607     goto no_uri;
1608
1609   if (!gst_uri_is_valid (play_base_bin->uri))
1610     goto invalid_uri;
1611
1612   if (IS_BLACKLISTED_URI (play_base_bin->uri))
1613     goto uri_blacklisted;
1614
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);
1620   } else {
1621     /* no subtitle specified */
1622     *subbin = NULL;
1623   }
1624
1625   source = gst_element_make_from_uri (GST_URI_SRC, play_base_bin->uri,
1626       "source");
1627   if (!source)
1628     goto no_source;
1629
1630   play_base_bin->is_stream = IS_STREAM_URI (play_base_bin->uri);
1631
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),
1636           "iradio-mode")) {
1637     g_object_set (source, "iradio-mode", TRUE, NULL);
1638   }
1639
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);
1647   }
1648
1649   return source;
1650
1651   /* ERRORS */
1652 no_uri:
1653   {
1654     GST_ELEMENT_ERROR (play_base_bin, RESOURCE, NOT_FOUND,
1655         (_("No URI specified to play from.")), (NULL));
1656     return NULL;
1657   }
1658 invalid_uri:
1659   {
1660     GST_ELEMENT_ERROR (play_base_bin, RESOURCE, NOT_FOUND,
1661         (_("Invalid URI \"%s\"."), play_base_bin->uri), (NULL));
1662     return NULL;
1663   }
1664 uri_blacklisted:
1665   {
1666     GST_ELEMENT_ERROR (play_base_bin, RESOURCE, FAILED,
1667         (_("RTSP streams cannot be played yet.")), (NULL));
1668     return NULL;
1669   }
1670 no_source:
1671   {
1672     gchar *prot = gst_uri_get_protocol (play_base_bin->uri);
1673
1674     /* whoops, could not create the source element, dig a little deeper to
1675      * figure out what might be wrong. */
1676     if (prot) {
1677       gchar *desc;
1678
1679       gst_element_post_message (GST_ELEMENT (play_base_bin),
1680           gst_missing_uri_source_message_new (GST_ELEMENT (play_base_bin),
1681               prot));
1682
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));
1687       g_free (desc);
1688       g_free (prot);
1689     } else
1690       goto invalid_uri;
1691
1692     return NULL;
1693   }
1694 }
1695
1696 /* is called when a dynamic source element created a new pad. */
1697 static void
1698 source_new_pad (GstElement * element, GstPad * pad, GstPlayBaseBin * bin)
1699 {
1700   GstElement *decoder;
1701   gboolean is_raw;
1702
1703   GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
1704       GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
1705
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);
1711     return;
1712   }
1713
1714   /* not raw, create decoder */
1715   decoder = make_decoder (bin);
1716   if (!decoder)
1717     goto no_decodebin;
1718
1719   /* and link to decoder */
1720   if (!gst_element_link (bin->source, decoder))
1721     goto could_not_link;
1722
1723   gst_element_set_state (decoder, GST_STATE_PAUSED);
1724
1725   return;
1726
1727   /* ERRORS */
1728 no_decodebin:
1729   {
1730     /* error was posted */
1731     return;
1732   }
1733 could_not_link:
1734   {
1735     GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
1736         (NULL), ("Can't link source to decoder element"));
1737     return;
1738   }
1739 }
1740
1741 /*
1742  * Setup the substreams (is called right after group_commit () when
1743  * loading a new group, or after switching groups).
1744  *
1745  * Should be called with group-lock held.
1746  */
1747 static void
1748 setup_substreams (GstPlayBaseBin * play_base_bin)
1749 {
1750   GstPlayBaseGroup *group;
1751   gint n;
1752   const GList *item;
1753
1754   GST_DEBUG_OBJECT (play_base_bin, "setting up substreams");
1755
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;
1760     gpointer data;
1761
1762     data = g_object_get_data (G_OBJECT (info->object), "eat_probe");
1763     if (data) {
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);
1767     }
1768
1769     /* now remove unknown pads */
1770     if (info->type == GST_STREAM_TYPE_UNKNOWN) {
1771       guint id;
1772
1773       id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info), "mute_probe"));
1774       if (id == 0) {
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));
1778       }
1779     }
1780   }
1781
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;
1789     }
1790   }
1791
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]);
1798   }
1799 }
1800
1801 /**
1802  * has_all_raw_caps:
1803  * @pad: a #GstPad
1804  * @all_raw: pointer to hold the result
1805  *
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.
1808  *
1809  * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1810  */
1811 static gboolean
1812 has_all_raw_caps (GstPad * pad, gboolean * all_raw)
1813 {
1814   GstCaps *caps;
1815   gint capssize;
1816   guint i, num_raw = 0;
1817   gboolean res = FALSE;
1818
1819   caps = gst_pad_get_caps (pad);
1820   if (caps == NULL)
1821     return FALSE;
1822
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))
1826     goto done;
1827
1828   /* count the number of raw formats in the caps */
1829   for (i = 0; i < capssize; ++i) {
1830     GstStructure *s;
1831     const gchar *mime_type;
1832
1833     s = gst_caps_get_structure (caps, i);
1834     mime_type = gst_structure_get_name (s);
1835
1836     if (IS_RAW_MIME (mime_type))
1837       ++num_raw;
1838   }
1839
1840   *all_raw = (num_raw == capssize);
1841   res = TRUE;
1842
1843 done:
1844   gst_caps_unref (caps);
1845   return res;
1846 }
1847
1848 /**
1849  * analyse_source:
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
1854  *
1855  * Check the source of @play_base_bin and collect information about it.
1856  *
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.
1860  *
1861  * @have_out: will be set to TRUE if the source has output pads.
1862  *
1863  * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1864  * on.
1865  *
1866  * Returns: FALSE if a fatal error occured while scanning.
1867  */
1868 static gboolean
1869 analyse_source (GstPlayBaseBin * play_base_bin, gboolean * is_raw,
1870     gboolean * have_out, gboolean * is_dynamic)
1871 {
1872   GstIterator *pads_iter;
1873   gboolean done = FALSE;
1874   gboolean res = TRUE;
1875
1876   *have_out = FALSE;
1877   *is_raw = FALSE;
1878   *is_dynamic = FALSE;
1879
1880   pads_iter = gst_element_iterate_src_pads (play_base_bin->source);
1881   while (!done) {
1882     GstPad *pad = NULL;
1883
1884     switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
1885       case GST_ITERATOR_ERROR:
1886         res = FALSE;
1887         /* FALLTROUGH */
1888       case GST_ITERATOR_DONE:
1889         done = TRUE;
1890         break;
1891       case GST_ITERATOR_RESYNC:
1892         /* reset results and resync */
1893         *have_out = FALSE;
1894         *is_raw = FALSE;
1895         *is_dynamic = FALSE;
1896         gst_iterator_resync (pads_iter);
1897         break;
1898       case GST_ITERATOR_OK:
1899         /* we now officially have an output pad */
1900         *have_out = TRUE;
1901
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);
1905           break;
1906         }
1907
1908         /* caps on source pad are all raw, we can add the pad */
1909         if (*is_raw) {
1910           new_decoded_pad_full (play_base_bin->source, pad, FALSE,
1911               play_base_bin, FALSE);
1912         }
1913
1914         gst_object_unref (pad);
1915         break;
1916     }
1917   }
1918   gst_iterator_free (pads_iter);
1919
1920   if (!*have_out) {
1921     GstElementClass *elemclass;
1922     GList *walk;
1923
1924     /* element has no output pads, check for padtemplates that list SOMETIMES
1925      * pads. */
1926     elemclass = GST_ELEMENT_GET_CLASS (play_base_bin->source);
1927
1928     walk = gst_element_class_get_pad_template_list (elemclass);
1929     while (walk != NULL) {
1930       GstPadTemplate *templ;
1931
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) {
1935           *is_dynamic = TRUE;
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 */
1940         }
1941       }
1942       walk = g_list_next (walk);
1943     }
1944   }
1945
1946   return res;
1947 }
1948
1949 static void
1950 remove_decoders (GstPlayBaseBin * bin)
1951 {
1952   GSList *walk;
1953
1954   for (walk = bin->decoders; walk; walk = g_slist_next (walk)) {
1955     GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1956
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);
1970
1971     gst_element_set_state (decoder, GST_STATE_NULL);
1972     gst_bin_remove (GST_BIN_CAST (bin), decoder);
1973   }
1974   g_slist_free (bin->decoders);
1975   bin->decoders = NULL;
1976 }
1977
1978 static GstElement *
1979 make_decoder (GstPlayBaseBin * play_base_bin)
1980 {
1981   GstElement *decoder;
1982
1983   /* now create the decoder element */
1984   if (g_getenv ("USE_DECODEBIN2"))
1985     decoder = gst_element_factory_make ("decodebin2", NULL);
1986   else
1987     decoder = gst_element_factory_make ("decodebin", NULL);
1988   if (!decoder)
1989     goto no_decodebin;
1990
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);
1995
1996   gst_bin_add (GST_BIN_CAST (play_base_bin), decoder);
1997
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++;
2008
2009   GST_DEBUG_OBJECT (play_base_bin, "created decodebin, %d pending",
2010       play_base_bin->pending);
2011
2012   play_base_bin->decoders = g_slist_prepend (play_base_bin->decoders, decoder);
2013
2014   return decoder;
2015
2016   /* ERRORS */
2017 no_decodebin:
2018   {
2019     GST_ELEMENT_ERROR (play_base_bin, CORE, MISSING_PLUGIN,
2020         (_("Could not create \"decodebin\" element.")), (NULL));
2021     return NULL;
2022   }
2023 }
2024
2025 static void
2026 remove_source (GstPlayBaseBin * bin)
2027 {
2028   GstElement *source = bin->source;
2029
2030   if (source) {
2031     GST_DEBUG_OBJECT (bin, "removing old src element");
2032     gst_element_set_state (source, GST_STATE_NULL);
2033
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;
2037     }
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;
2041     }
2042     gst_bin_remove (GST_BIN_CAST (bin), source);
2043     bin->source = NULL;
2044   }
2045 }
2046
2047 static GstBusSyncReply
2048 subbin_startup_sync_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
2049 {
2050   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
2051     GstPlayBaseBin *play_base_bin;
2052
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");
2061     }
2062   }
2063   return GST_BUS_PASS;
2064 }
2065
2066 /* construct and run the source and decoder elements until we found
2067  * all the streams or until a preroll queue has been filled.
2068 */
2069 static gboolean
2070 setup_source (GstPlayBaseBin * play_base_bin)
2071 {
2072   GstElement *subbin = NULL;
2073   gboolean is_raw, have_out, is_dynamic;
2074
2075   if (!play_base_bin->need_rebuild)
2076     return TRUE;
2077   play_base_bin->raw_decoding_mode = FALSE;
2078
2079   GST_DEBUG_OBJECT (play_base_bin, "setup source");
2080
2081   /* delete old src */
2082   remove_source (play_base_bin);
2083
2084   /* create and configure an element that can handle the uri */
2085   if (!(play_base_bin->source = gen_source_element (play_base_bin, &subbin)))
2086     goto no_source;
2087
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");
2092
2093   /* remove the old decoders now, if any */
2094   remove_decoders (play_base_bin);
2095
2096   /* remove our previous preroll queues */
2097   remove_groups (play_base_bin);
2098
2099   /* clear pending dynamic elements */
2100   play_base_bin->pending = 0;
2101
2102   /* do subs */
2103   if (subbin) {
2104     GstElement *db;
2105     GstBus *bus;
2106
2107     play_base_bin->subtitle = subbin;
2108     db = gst_bin_get_by_name (GST_BIN_CAST (subbin), "subtitle-decoder");
2109
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++;
2119
2120     GST_DEBUG_OBJECT (play_base_bin, "we have subtitles, %d pending",
2121         play_base_bin->pending);
2122
2123     if (!play_base_bin->is_stream) {
2124       GstStateChangeReturn sret;
2125
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
2129        */
2130       GST_DEBUG_OBJECT (play_base_bin, "starting subtitle bin");
2131
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;
2137
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);
2143
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);
2152
2153         if (!play_base_bin->building_group ||
2154             play_base_bin->building_group->type[GST_STREAM_TYPE_TEXT -
2155                 1].npads == 0) {
2156
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;
2161         } else {
2162           GST_DEBUG_OBJECT (play_base_bin, "Subtitle set-up successful");
2163         }
2164       } else {
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;
2169       }
2170
2171       gst_bus_set_sync_handler (bus, NULL, NULL);
2172       gst_element_set_bus (subbin, NULL);
2173       gst_object_unref (bus);
2174     }
2175     gst_object_unref (db);
2176   }
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;
2183
2184   if (is_raw) {
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);
2189     return TRUE;
2190   }
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);
2197     return TRUE;
2198   }
2199   if (is_dynamic) {
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);
2212   } else {
2213     GstElement *decoder;
2214
2215     /* no dynamic source, we can link now */
2216     decoder = make_decoder (play_base_bin);
2217     if (!decoder)
2218       goto no_decodebin;
2219
2220     if (!gst_element_link (play_base_bin->source, decoder))
2221       goto could_not_link;
2222   }
2223
2224   if (play_base_bin->subtitle)
2225     gst_bin_add (GST_BIN_CAST (play_base_bin), play_base_bin->subtitle);
2226
2227   play_base_bin->need_rebuild = FALSE;
2228
2229   return TRUE;
2230
2231   /* ERRORS */
2232 no_source:
2233   {
2234     /* error message was already posted */
2235     return FALSE;
2236   }
2237 invalid_source:
2238   {
2239     GST_ELEMENT_ERROR (play_base_bin, CORE, FAILED,
2240         (_("Source element is invalid.")), (NULL));
2241     return FALSE;
2242   }
2243 no_decodebin:
2244   {
2245     /* message was posted */
2246     return FALSE;
2247   }
2248 could_not_link:
2249   {
2250     GST_ELEMENT_ERROR (play_base_bin, CORE, NEGOTIATION,
2251         (NULL), ("Can't link source to decoder element"));
2252     return FALSE;
2253   }
2254 }
2255
2256 static void
2257 finish_source (GstPlayBaseBin * play_base_bin)
2258 {
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);
2264     }
2265   }
2266 }
2267
2268 /*
2269  * Caller must have group-lock held.
2270  *
2271  * We iterate over all detected streams in the streaminfo and try to find
2272  * impossible cases, like subtitles without video.
2273  */
2274 static gboolean
2275 prepare_output (GstPlayBaseBin * play_base_bin)
2276 {
2277   const GList *item;
2278   gboolean stream_found = FALSE, no_media = FALSE;
2279   gboolean got_video = FALSE, got_subtitle = FALSE;
2280   GstPlayBaseGroup *group;
2281
2282   group = get_active_group (play_base_bin);
2283
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);
2289
2290     if (info->type == GST_STREAM_TYPE_VIDEO) {
2291       stream_found = TRUE;
2292       got_video = TRUE;
2293       break;
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)) {
2306         const gchar *mime =
2307             gst_structure_get_name (gst_caps_get_structure (info->caps, 0));
2308
2309         no_media = IS_NO_MEDIA_MIME (mime);
2310       }
2311     }
2312   }
2313
2314   if (!stream_found) {
2315     if (got_subtitle) {
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));
2324     } else {
2325       GST_ELEMENT_ERROR (play_base_bin, STREAM, WRONG_TYPE,
2326           (_("This is not a media file")), (NULL));
2327     }
2328     return FALSE;
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));
2332     return FALSE;
2333   }
2334
2335   return TRUE;
2336 }
2337
2338 /*
2339  * Multi-stream management. -1 = none.
2340  *
2341  * Caller has group-lock held.
2342  */
2343 static gint
2344 get_active_source (GstPlayBaseBin * play_base_bin, GstStreamType type)
2345 {
2346   GstPlayBaseGroup *group;
2347   GList *s;
2348   gint num = 0;
2349
2350   group = get_active_group (play_base_bin);
2351   if (!group)
2352     return -1;
2353
2354   for (s = group->streaminfo; s; s = s->next) {
2355     GstStreamInfo *info = s->data;
2356
2357     if (info->type == type) {
2358       if (!info->mute && !g_object_get_data (G_OBJECT (info), "mute_probe")) {
2359         return num;
2360       } else {
2361         num++;
2362       }
2363     }
2364   }
2365
2366   return -1;
2367 }
2368
2369 /* Kill pad reactivation on state change. */
2370
2371 #if 0
2372 static void muted_group_change_state (GstElement * element,
2373     gint old_state, gint new_state, gpointer data);
2374 #endif
2375
2376 static void
2377 mute_group_type (GstPlayBaseGroup * group, GstStreamType type, gboolean mute)
2378 {
2379   gboolean active = !mute;
2380   GstPad *pad;
2381
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);
2391
2392 #if 0
2393   if (mute) {
2394     g_signal_connect (group->type[type - 1].preroll, "state-changed",
2395         G_CALLBACK (muted_group_change_state), group);
2396   } else {
2397     g_signal_handlers_disconnect_by_func (group->type[type - 1].preroll,
2398         G_CALLBACK (muted_group_change_state), group);
2399   }
2400 #endif
2401 }
2402
2403 #if 0
2404 static void
2405 muted_group_change_state (GstElement * element,
2406     gint old_state, gint new_state, gpointer data)
2407 {
2408   GstPlayBaseGroup *group = data;
2409
2410   GROUP_LOCK (group->bin);
2411
2412   if (new_state == GST_STATE_PLAYING) {
2413     gint n;
2414
2415     for (n = 0; n < NUM_TYPES; n++) {
2416       if (group->type[n].selector == element) {
2417         mute_group_type (group, n + 1, TRUE);
2418       }
2419     }
2420   }
2421
2422   GROUP_UNLOCK (group->bin);
2423 }
2424 #endif
2425
2426 static void
2427 set_subtitles_visible (GstPlayBaseBin * play_base_bin, gboolean visible)
2428 {
2429   GstPlayBaseBinClass *klass = GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin);
2430
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);
2435 }
2436
2437 static void
2438 set_audio_mute (GstPlayBaseBin * play_base_bin, gboolean mute)
2439 {
2440   GstPlayBaseBinClass *klass = GST_PLAY_BASE_BIN_GET_CLASS (play_base_bin);
2441
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);
2446 }
2447
2448 /*
2449  * Caller has group-lock held.
2450  */
2451
2452 static void
2453 set_active_source (GstPlayBaseBin * play_base_bin,
2454     GstStreamType type, gint source_num)
2455 {
2456   GstPlayBaseGroup *group;
2457   GList *s;
2458   gint num = 0;
2459   gboolean have_active = FALSE;
2460   GstElement *sel;
2461
2462   GST_LOG ("Changing active source of type %d to %d", type, source_num);
2463   play_base_bin->current[type - 1] = source_num;
2464
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);
2468     return;
2469   }
2470
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);
2476
2477     set_subtitles_visible (play_base_bin, visible);
2478     if (!visible)
2479       return;
2480   } else if (type == GST_STREAM_TYPE_AUDIO) {
2481     gboolean mute = (source_num == -1);
2482
2483     set_audio_mute (play_base_bin, mute);
2484
2485     if (mute)
2486       return;
2487   }
2488
2489   sel = group->type[type - 1].selector;
2490
2491   for (s = group->streaminfo; s; s = s->next) {
2492     GstStreamInfo *info = s->data;
2493
2494     if (info->type == type) {
2495       if (num == source_num) {
2496         GstPad *sel_pad;
2497
2498         GST_LOG ("Unmuting (if already muted) source %d of type %d", source_num,
2499             type);
2500         g_object_set (info, "mute", FALSE, NULL);
2501
2502         /* Tell the stream selector which pad to accept */
2503         sel_pad = GST_PAD_CAST (g_object_get_data (G_OBJECT (info->object),
2504                 "pb_sel_pad"));
2505
2506         if (sel && sel_pad != NULL) {
2507           g_object_set (G_OBJECT (sel), "active-pad", sel_pad, NULL);
2508         }
2509
2510         have_active = TRUE;
2511       } else {
2512         guint id;
2513
2514         GST_LOG_OBJECT (info->object, "Muting source %d of type %d", num, type);
2515
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));
2519       }
2520       num++;
2521     }
2522   }
2523
2524   if (!have_active) {
2525     GST_LOG ("Muting group type: %d", type);
2526     g_object_set (sel, "active-pad", NULL, NULL);
2527   } else {
2528     GST_LOG ("Unmuting group type: %d", type);
2529   }
2530   mute_group_type (group, type, !have_active);
2531 }
2532
2533 static void
2534 gst_play_base_bin_set_property (GObject * object, guint prop_id,
2535     const GValue * value, GParamSpec * pspec)
2536 {
2537   GstPlayBaseBin *play_base_bin;
2538
2539   g_return_if_fail (GST_IS_PLAY_BASE_BIN (object));
2540
2541   play_base_bin = GST_PLAY_BASE_BIN (object);
2542
2543   switch (prop_id) {
2544     case ARG_URI:
2545     {
2546       const gchar *uri = g_value_get_string (value);
2547
2548       if (uri == NULL) {
2549         g_warning ("cannot set NULL uri");
2550         return;
2551       }
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);
2557
2558         GST_DEBUG ("setting new uri to %s", uri);
2559
2560         play_base_bin->need_rebuild = TRUE;
2561       }
2562       break;
2563     }
2564     case ARG_SUBURI:{
2565       const gchar *suburi = g_value_get_string (value);
2566
2567       if ((!suburi && !play_base_bin->suburi) ||
2568           (suburi && play_base_bin->suburi &&
2569               !strcmp (play_base_bin->suburi, suburi)))
2570         return;
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;
2575       break;
2576     }
2577     case ARG_QUEUE_SIZE:
2578       play_base_bin->queue_size = g_value_get_uint64 (value);
2579       break;
2580     case ARG_QUEUE_THRESHOLD:
2581       play_base_bin->queue_threshold = g_value_get_uint64 (value);
2582       break;
2583     case ARG_QUEUE_MIN_THRESHOLD:
2584       play_base_bin->queue_min_threshold = g_value_get_uint64 (value);
2585       break;
2586     case ARG_CONNECTION_SPEED:
2587       play_base_bin->connection_speed = g_value_get_uint (value) * 1000;
2588       break;
2589     case ARG_VIDEO:
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);
2594       break;
2595     case ARG_AUDIO:
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);
2600       break;
2601     case ARG_TEXT:
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);
2606       break;
2607     case ARG_SUBTITLE_ENCODING:
2608     {
2609       const gchar *encoding;
2610       GSList *list;
2611
2612       encoding = g_value_get_string (value);
2613       if (encoding && play_base_bin->subencoding &&
2614           !strcmp (play_base_bin->subencoding, encoding)) {
2615         return;
2616       }
2617       if (encoding == NULL && play_base_bin->subencoding == NULL)
2618         return;
2619
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);
2626
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);
2633       break;
2634     }
2635     default:
2636       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2637       break;
2638   }
2639 }
2640
2641 static void
2642 gst_play_base_bin_get_property (GObject * object, guint prop_id, GValue * value,
2643     GParamSpec * pspec)
2644 {
2645   GstPlayBaseBin *play_base_bin;
2646
2647   g_return_if_fail (GST_IS_PLAY_BASE_BIN (object));
2648
2649   play_base_bin = GST_PLAY_BASE_BIN (object);
2650
2651   switch (prop_id) {
2652     case ARG_URI:
2653       g_value_set_string (value, play_base_bin->uri);
2654       break;
2655     case ARG_SUBURI:
2656       g_value_set_string (value, play_base_bin->suburi);
2657       break;
2658     case ARG_NSTREAMS:
2659     {
2660       GstPlayBaseGroup *group;
2661
2662       GROUP_LOCK (play_base_bin);
2663       group = get_active_group (play_base_bin);
2664       if (group) {
2665         g_value_set_int (value, group->nstreams);
2666       } else {
2667         g_value_set_int (value, 0);
2668       }
2669       GROUP_UNLOCK (play_base_bin);
2670       break;
2671     }
2672     case ARG_QUEUE_SIZE:
2673       g_value_set_uint64 (value, play_base_bin->queue_size);
2674       break;
2675     case ARG_QUEUE_THRESHOLD:
2676       g_value_set_uint64 (value, play_base_bin->queue_threshold);
2677       break;
2678     case ARG_QUEUE_MIN_THRESHOLD:
2679       g_value_set_uint64 (value, play_base_bin->queue_min_threshold);
2680       break;
2681     case ARG_CONNECTION_SPEED:
2682       g_value_set_uint (value, play_base_bin->connection_speed / 1000);
2683       break;
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));
2688       break;
2689     case ARG_STREAMINFO_VALUES:{
2690       GValueArray *copy;
2691
2692       copy = gst_play_base_bin_get_streaminfo_value_array (play_base_bin);
2693       g_value_take_boxed (value, copy);
2694       break;
2695     }
2696     case ARG_SOURCE:
2697       g_value_set_object (value, play_base_bin->source);
2698       break;
2699     case ARG_VIDEO:
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);
2704       break;
2705     case ARG_AUDIO:
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);
2710       break;
2711     case ARG_TEXT:
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);
2716       break;
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);
2721       break;
2722     default:
2723       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2724       break;
2725   }
2726 }
2727
2728 static GstStateChangeReturn
2729 gst_play_base_bin_change_state (GstElement * element, GstStateChange transition)
2730 {
2731   GstStateChangeReturn ret;
2732   GstPlayBaseBin *play_base_bin;
2733
2734   play_base_bin = GST_PLAY_BASE_BIN (element);
2735
2736   switch (transition) {
2737     case GST_STATE_CHANGE_READY_TO_PAUSED:
2738       if (!setup_source (play_base_bin))
2739         goto source_failed;
2740       break;
2741     default:
2742       break;
2743   }
2744
2745   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2746
2747   switch (transition) {
2748     case GST_STATE_CHANGE_READY_TO_PAUSED:
2749       if (ret == GST_STATE_CHANGE_FAILURE)
2750         goto cleanup_groups;
2751
2752       finish_source (play_base_bin);
2753       break;
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);
2761       break;
2762     default:
2763       break;
2764   }
2765   return ret;
2766
2767   /* ERRORS */
2768 source_failed:
2769   {
2770     play_base_bin->need_rebuild = TRUE;
2771
2772     return GST_STATE_CHANGE_FAILURE;
2773   }
2774 cleanup_groups:
2775   {
2776     /* clean up leftover groups */
2777     remove_groups (play_base_bin);
2778     play_base_bin->need_rebuild = TRUE;
2779
2780     return GST_STATE_CHANGE_FAILURE;
2781   }
2782 }
2783
2784 static const GList *
2785 gst_play_base_bin_get_streaminfo (GstPlayBaseBin * play_base_bin)
2786 {
2787   GstPlayBaseGroup *group = get_active_group (play_base_bin);
2788   GList *info = NULL;
2789
2790   if (group) {
2791     info = group->streaminfo;
2792   }
2793   return info;
2794 }
2795
2796 static GValueArray *
2797 gst_play_base_bin_get_streaminfo_value_array (GstPlayBaseBin * play_base_bin)
2798 {
2799   GstPlayBaseGroup *group;
2800   GValueArray *array = NULL;
2801
2802   GROUP_LOCK (play_base_bin);
2803   group = get_active_group (play_base_bin);
2804   if (group) {
2805     array = g_value_array_copy (group->streaminfo_value_array);
2806   } else {
2807     array = g_value_array_new (0);
2808   }
2809   GROUP_UNLOCK (play_base_bin);
2810
2811   return array;
2812 }