Add -Wmissing-declarations -Wmissing-prototypes to warning flags
[platform/upstream/gstreamer.git] / gst / playback / gstplaybin.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 /**
21  * SECTION:element-playbin
22  *
23  * Playbin provides a stand-alone everything-in-one abstraction for an
24  * audio and/or video player.
25  *
26  * It can handle both audio and video files and features
27  * <itemizedlist>
28  * <listitem>
29  * automatic file type recognition and based on that automatic
30  * selection and usage of the right audio/video/subtitle demuxers/decoders
31  * </listitem>
32  * <listitem>
33  * visualisations for audio files
34  * </listitem>
35  * <listitem>
36  * subtitle support for video files
37  * </listitem>
38  * <listitem>
39  * stream selection between different audio/subtitles streams
40  * </listitem>
41  * <listitem>
42  * meta info (tag) extraction
43  * </listitem>
44  * <listitem>
45  * easy access to the last video frame
46  * </listitem>
47  * <listitem>
48  * buffering when playing streams over a network
49  * </listitem>
50  * <listitem>
51  * volume control
52  * </listitem>
53  * </itemizedlist>
54  *
55  * <refsect2>
56  * <title>Usage</title>
57  * <para>
58  * A playbin element can be created just like any other element using
59  * gst_element_factory_make(). The file/URI to play should be set via the #GstPlayBin:uri
60  * property. This must be an absolute URI, relative file paths are not allowed.
61  * Example URIs are file:///home/joe/movie.avi or http://www.joedoe.com/foo.ogg
62  *
63  * Playbin is a #GstPipeline. It will notify the application of everything
64  * that's happening (errors, end of stream, tags found, state changes, etc.)
65  * by posting messages on its #GstBus. The application needs to watch the
66  * bus.
67  *
68  * Playback can be initiated by setting the element to PLAYING state using
69  * gst_element_set_state(). Note that the state change will take place in
70  * the background in a separate thread, when the function returns playback
71  * is probably not happening yet and any errors might not have occured yet.
72  * Applications using playbin should ideally be written to deal with things
73  * completely asynchroneous.
74  *
75  * When playback has finished (an EOS message has been received on the bus)
76  * or an error has occured (an ERROR message has been received on the bus) or
77  * the user wants to play a different track, playbin should be set back to
78  * READY or NULL state, then the #GstPlayBin:uri property should be set to the
79  * new location and then playbin be set to PLAYING state again.
80  *
81  * Seeking can be done using gst_element_seek_simple() or gst_element_seek()
82  * on the playbin element. Again, the seek will not be executed
83  * instantaneously, but will be done in a background thread. When the seek
84  * call returns the seek will most likely still be in process. An application
85  * may wait for the seek to finish (or fail) using gst_element_get_state() with
86  * -1 as the timeout, but this will block the user interface and is not
87  * recommended at all.
88  *
89  * Applications may query the current position and duration of the stream
90  * via gst_element_query_position() and gst_element_query_duration() and
91  * setting the format passed to GST_FORMAT_TIME. If the query was successful,
92  * the duration or position will have been returned in units of nanoseconds.
93  * </para>
94  * </refsect2>
95  * <refsect2>
96  * <title>Advanced Usage: specifying the audio and video sink</title>
97  * <para>
98  * By default, if no audio sink or video sink has been specified via the
99  * #GstPlayBin:audio-sink or #GstPlayBin:video-sink property, playbin will use
100  * the autoaudiosink and autovideosink elements to find the first-best
101  * available output method.
102  * This should work in most cases, but is not always desirable. Often either
103  * the user or application might want to specify more explicitly what to use
104  * for audio and video output.
105  *
106  * If the application wants more control over how audio or video should be
107  * output, it may create the audio/video sink elements itself (for example
108  * using gst_element_factory_make()) and provide them to playbin using the
109  * #GstPlayBin:audio-sink or #GstPlayBin:video-sink property.
110  *
111  * GNOME-based applications, for example, will usually want to create
112  * gconfaudiosink and gconfvideosink elements and make playbin use those,
113  * so that output happens to whatever the user has configured in the GNOME
114  * Multimedia System Selector confinguration dialog.
115  *
116  * The sink elements do not necessarily need to be ready-made sinks. It is
117  * possible to create container elements that look like a sink to playbin,
118  * but in reality contain a number of custom elements linked together. This
119  * can be achieved by creating a #GstBin and putting elements in there and
120  * linking them, and then creating a sink #GstGhostPad for the bin and pointing
121  * it to the sink pad of the first element within the bin. This can be used
122  * for a number of purposes, for example to force output to a particular
123  * format or to modify or observe the data before it is output.
124  *
125  * It is also possible to 'suppress' audio and/or video output by using
126  * 'fakesink' elements (or capture it from there using the fakesink element's
127  * "handoff" signal, which, nota bene, is fired from the streaming thread!).
128  * </para>
129  * </refsect2>
130  * <refsect2>
131  * <title>Retrieving Tags and Other Meta Data</title>
132  * <para>
133  * Most of the common meta data (artist, title, etc.) can be retrieved by
134  * watching for TAG messages on the pipeline's bus (see above).
135  *
136  * Other more specific meta information like width/height/framerate of video
137  * streams or samplerate/number of channels of audio streams can be obtained
138  * using the  #GstPlayBaseBin:stream-info property, which will return a GList of
139  * stream info objects, one for each stream. These are opaque objects that can
140  * only be accessed via the standard GObject property interface, ie. g_object_get().
141  * Each stream info object has the following properties:
142  * <itemizedlist>
143  * <listitem>"object" (GstObject) (the decoder source pad usually)</listitem>
144  * <listitem>"type" (enum) (if this is an audio/video/subtitle stream)</listitem>
145  * <listitem>"decoder" (string) (name of decoder used to decode this stream)</listitem>
146  * <listitem>"mute" (boolean) (to mute or unmute this stream)</listitem>
147  * <listitem>"caps" (GstCaps) (caps of the decoded stream)</listitem>
148  * <listitem>"language-code" (string) (ISO-639 language code for this stream, mostly used for audio/subtitle streams)</listitem>
149  * <listitem>"codec" (string) (format this stream was encoded in)</listitem>
150  * </itemizedlist>
151  * Stream information from the #GstPlayBaseBin:stream-info property is best queried once
152  * playbin has changed into PAUSED or PLAYING state (which can be detected
153  * via a state-changed message on the #GstBus where old_state=READY and
154  * new_state=PAUSED), since before that the list might not be complete yet or
155  * not contain all available information (like language-codes).
156  * </para>
157  * </refsect2>
158  * <refsect2>
159  * <title>Buffering</title>
160  * Playbin handles buffering automatically for the most part, but applications
161  * need to handle parts of the buffering process as well. Whenever playbin is
162  * buffering, it will post BUFFERING messages on the bus with a percentage
163  * value that shows the progress of the buffering process. Applications need
164  * to set playbin to PLAYING or PAUSED state in response to these messages.
165  * They may also want to convey the buffering progress to the user in some
166  * way. Here is how to extract the percentage information from the message
167  * (requires GStreamer >= 0.10.11):
168  * |[
169  * switch (GST_MESSAGE_TYPE (msg)) {
170  *   case GST_MESSAGE_BUFFERING: {
171  *     gint percent = 0;
172  *     gst_message_parse_buffering (msg, &amp;percent);
173  *     g_print ("Buffering (%%u percent done)", percent);
174  *     break;
175  *   }
176  *   ...
177  * }
178  * ]|
179  * Note that applications should keep/set the pipeline in the PAUSED state when
180  * a BUFFERING message is received with a buffer percent value < 100 and set
181  * the pipeline back to PLAYING state when a BUFFERING message with a value
182  * of 100 percent is received (if PLAYING is the desired state, that is).
183  * </refsect2>
184  * <refsect2>
185  * <title>Embedding the video window in your application</title>
186  * By default, playbin (or rather the video sinks used) will create their own
187  * window. Applications will usually want to force output to a window of their
188  * own, however. This can be done using the #GstXOverlay interface, which most
189  * video sinks implement. See the documentation there for more details.
190  * </refsect2>
191  * <refsect2>
192  * <title>Specifying which CD/DVD device to use</title>
193  * The device to use for CDs/DVDs needs to be set on the source element
194  * playbin creates before it is opened. The only way to do this at the moment
195  * is to connect to playbin's "notify::source" signal, which will be emitted
196  * by playbin when it has created the source element for a particular URI.
197  * In the signal callback you can check if the source element has a "device"
198  * property and set it appropriately. In future ways might be added to specify
199  * the device as part of the URI, but at the time of writing this is not
200  * possible yet.
201  * </refsect2>
202  * <refsect2>
203  * <title>Examples</title>
204  * |[
205  * gst-launch -v playbin uri=file:///path/to/somefile.avi
206  * ]| This will play back the given AVI video file, given that the video and
207  * audio decoders required to decode the content are installed. Since no
208  * special audio sink or video sink is supplied (not possible via gst-launch),
209  * playbin will try to find a suitable audio and video sink automatically
210  * using the autoaudiosink and autovideosink elements.
211  * |[
212  * gst-launch -v playbin uri=cdda://4
213  * ]| This will play back track 4 on an audio CD in your disc drive (assuming
214  * the drive is detected automatically by the plugin).
215  * |[
216  * gst-launch -v playbin uri=dvd://1
217  * ]| This will play back title 1 of a DVD in your disc drive (assuming
218  * the drive is detected automatically by the plugin).
219  * </refsect2>
220  */
221
222 #ifdef HAVE_CONFIG_H
223 #include "config.h"
224 #endif
225
226 #include <string.h>
227 #include <gst/gst.h>
228
229 #include <gst/gst-i18n-plugin.h>
230 #include <gst/pbutils/pbutils.h>
231
232 #include "gstplaybasebin.h"
233 #include "gstplayback.h"
234
235 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_debug);
236 #define GST_CAT_DEFAULT gst_play_bin_debug
237
238 #define GST_TYPE_PLAY_BIN               (gst_play_bin_get_type())
239 #define GST_PLAY_BIN(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PLAY_BIN,GstPlayBin))
240 #define GST_PLAY_BIN_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PLAY_BIN,GstPlayBinClass))
241 #define GST_IS_PLAY_BIN(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PLAY_BIN))
242 #define GST_IS_PLAY_BIN_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PLAY_BIN))
243
244 #define VOLUME_MAX_DOUBLE 10.0
245
246 typedef struct _GstPlayBin GstPlayBin;
247 typedef struct _GstPlayBinClass GstPlayBinClass;
248
249 /**
250  * GstPlayBin:
251  *
252  * High-level player element
253  */
254 struct _GstPlayBin
255 {
256   GstPlayBaseBin parent;
257
258   /* the configurable elements */
259   GstElement *fakesink;
260   GstElement *audio_sink;
261   GstElement *video_sink;
262   GstElement *visualisation;
263   GstElement *pending_visualisation;
264   GstElement *volume_element;
265   GstElement *textoverlay_element;
266   GstElement *spu_element;
267   gfloat volume;
268
269   /* these are the currently active sinks */
270   GList *sinks;
271
272   /* the last captured frame for snapshots */
273   GstBuffer *frame;
274
275   /* our cache for the sinks */
276   GHashTable *cache;
277
278   /* font description */
279   gchar *font_desc;
280
281   /* indication if the pipeline is live */
282   gboolean is_live;
283 };
284
285 struct _GstPlayBinClass
286 {
287   GstPlayBaseBinClass parent_class;
288 };
289
290 /* props */
291 enum
292 {
293   ARG_0,
294   ARG_AUDIO_SINK,
295   ARG_VIDEO_SINK,
296   ARG_VIS_PLUGIN,
297   ARG_VOLUME,
298   ARG_FRAME,
299   ARG_FONT_DESC
300 };
301
302 /* signals */
303 enum
304 {
305   LAST_SIGNAL
306 };
307
308 static void gst_play_bin_class_init (GstPlayBinClass * klass);
309 static void gst_play_bin_init (GstPlayBin * play_bin);
310 static void gst_play_bin_dispose (GObject * object);
311
312 static gboolean setup_sinks (GstPlayBaseBin * play_base_bin,
313     GstPlayBaseGroup * group);
314 static void remove_sinks (GstPlayBin * play_bin);
315 static void playbin_set_subtitles_visible (GstPlayBaseBin * play_base_bin,
316     gboolean visible);
317 static void playbin_set_audio_mute (GstPlayBaseBin * play_base_bin,
318     gboolean mute);
319
320 static void gst_play_bin_set_property (GObject * object, guint prop_id,
321     const GValue * value, GParamSpec * spec);
322 static void gst_play_bin_get_property (GObject * object, guint prop_id,
323     GValue * value, GParamSpec * spec);
324
325 static gboolean gst_play_bin_send_event (GstElement * element,
326     GstEvent * event);
327 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
328     GstStateChange transition);
329
330 static void gst_play_bin_handle_message (GstBin * bin, GstMessage * message);
331
332 static GstElementClass *parent_class;
333
334 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
335
336 static const GstElementDetails gst_play_bin_details =
337 GST_ELEMENT_DETAILS ("Player Bin",
338     "Generic/Bin/Player",
339     "Autoplug and play media from an uri",
340     "Wim Taymans <wim.taymans@gmail.com>");
341
342 static GType
343 gst_play_bin_get_type (void)
344 {
345   static GType gst_play_bin_type = 0;
346
347   if (!gst_play_bin_type) {
348     static const GTypeInfo gst_play_bin_info = {
349       sizeof (GstPlayBinClass),
350       NULL,
351       NULL,
352       (GClassInitFunc) gst_play_bin_class_init,
353       NULL,
354       NULL,
355       sizeof (GstPlayBin),
356       0,
357       (GInstanceInitFunc) gst_play_bin_init,
358       NULL
359     };
360
361     gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
362         "GstPlayBin", &gst_play_bin_info, 0);
363   }
364
365   return gst_play_bin_type;
366 }
367
368 static void
369 gst_play_bin_class_init (GstPlayBinClass * klass)
370 {
371   GObjectClass *gobject_klass;
372   GstElementClass *gstelement_klass;
373   GstBinClass *gstbin_klass;
374   GstPlayBaseBinClass *playbasebin_klass;
375
376   gobject_klass = (GObjectClass *) klass;
377   gstelement_klass = (GstElementClass *) klass;
378   gstbin_klass = (GstBinClass *) klass;
379   playbasebin_klass = (GstPlayBaseBinClass *) klass;
380
381   parent_class = g_type_class_peek_parent (klass);
382
383   gobject_klass->set_property = gst_play_bin_set_property;
384   gobject_klass->get_property = gst_play_bin_get_property;
385
386   g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
387       g_param_spec_object ("video-sink", "Video Sink",
388           "the video output element to use (NULL = default sink)",
389           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390   g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
391       g_param_spec_object ("audio-sink", "Audio Sink",
392           "the audio output element to use (NULL = default sink)",
393           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394   g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
395       g_param_spec_object ("vis-plugin", "Vis plugin",
396           "the visualization element to use (NULL = none)",
397           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398   /**
399    * GstPlayBin:volume:
400    *
401    * Get or set the current audio stream volume. 1.0 means 100%,
402    * 0.0 means mute. This uses a linear volume scale.
403    *
404    */
405   g_object_class_install_property (gobject_klass, ARG_VOLUME,
406       g_param_spec_double ("volume", "volume", "volume",
407           0.0, VOLUME_MAX_DOUBLE, 1.0,
408           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409   g_object_class_install_property (gobject_klass, ARG_FRAME,
410       gst_param_spec_mini_object ("frame", "Frame",
411           "The last frame (NULL = no video available)", GST_TYPE_BUFFER,
412           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
413   g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
414       g_param_spec_string ("subtitle-font-desc", "Subtitle font description",
415           "Pango font description of font " "to be used for subtitle rendering",
416           NULL, G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
417
418   gobject_klass->dispose = gst_play_bin_dispose;
419
420   gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
421
422   gstelement_klass->change_state =
423       GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
424   gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
425
426   gstbin_klass->handle_message =
427       GST_DEBUG_FUNCPTR (gst_play_bin_handle_message);
428
429   playbasebin_klass->setup_output_pads = setup_sinks;
430   playbasebin_klass->set_subtitles_visible = playbin_set_subtitles_visible;
431   playbasebin_klass->set_audio_mute = playbin_set_audio_mute;
432 }
433
434 static void
435 gst_play_bin_init (GstPlayBin * play_bin)
436 {
437   play_bin->video_sink = NULL;
438   play_bin->audio_sink = NULL;
439   play_bin->visualisation = NULL;
440   play_bin->pending_visualisation = NULL;
441   play_bin->volume_element = NULL;
442   play_bin->textoverlay_element = NULL;
443   play_bin->spu_element = NULL;
444   play_bin->volume = 1.0;
445   play_bin->sinks = NULL;
446   play_bin->frame = NULL;
447   play_bin->font_desc = NULL;
448   play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
449       NULL, (GDestroyNotify) gst_object_unref);
450 }
451
452 static void
453 gst_play_bin_dispose (GObject * object)
454 {
455   GstPlayBin *play_bin;
456
457   play_bin = GST_PLAY_BIN (object);
458
459   if (play_bin->cache != NULL) {
460     remove_sinks (play_bin);
461     g_hash_table_destroy (play_bin->cache);
462     play_bin->cache = NULL;
463   }
464
465   if (play_bin->audio_sink != NULL) {
466     gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
467     gst_object_unref (play_bin->audio_sink);
468     play_bin->audio_sink = NULL;
469   }
470   if (play_bin->video_sink != NULL) {
471     gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
472     gst_object_unref (play_bin->video_sink);
473     play_bin->video_sink = NULL;
474   }
475   if (play_bin->visualisation != NULL) {
476     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
477     gst_object_unref (play_bin->visualisation);
478     play_bin->visualisation = NULL;
479   }
480   if (play_bin->pending_visualisation != NULL) {
481     gst_element_set_state (play_bin->pending_visualisation, GST_STATE_NULL);
482     gst_object_unref (play_bin->pending_visualisation);
483     play_bin->pending_visualisation = NULL;
484   }
485   if (play_bin->textoverlay_element != NULL) {
486     gst_object_unref (play_bin->textoverlay_element);
487     play_bin->textoverlay_element = NULL;
488   }
489   if (play_bin->spu_element != NULL) {
490     gst_object_unref (play_bin->spu_element);
491     play_bin->spu_element = NULL;
492   }
493   g_free (play_bin->font_desc);
494   play_bin->font_desc = NULL;
495
496   G_OBJECT_CLASS (parent_class)->dispose (object);
497 }
498
499 static void
500 gst_play_bin_vis_unblocked (GstPad * tee_pad, gboolean blocked,
501     gpointer user_data)
502 {
503   GstPlayBin *play_bin = GST_PLAY_BIN (user_data);
504
505   if (play_bin->pending_visualisation)
506     gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_bin_vis_unblocked,
507         play_bin);
508 }
509
510 static void
511 gst_play_bin_vis_blocked (GstPad * tee_pad, gboolean blocked,
512     gpointer user_data)
513 {
514   GstPlayBin *play_bin = GST_PLAY_BIN (user_data);
515   GstBin *vis_bin = NULL;
516   GstPad *vis_sink_pad = NULL, *vis_src_pad = NULL, *vqueue_pad = NULL;
517   GstState bin_state;
518   GstElement *pending_visualisation;
519
520   GST_OBJECT_LOCK (play_bin);
521   pending_visualisation = play_bin->pending_visualisation;
522   play_bin->pending_visualisation = NULL;
523   GST_OBJECT_UNLOCK (play_bin);
524
525   /* We want to disable visualisation */
526   if (!GST_IS_ELEMENT (pending_visualisation)) {
527     /* Set visualisation element to READY */
528     gst_element_set_state (play_bin->visualisation, GST_STATE_READY);
529     goto beach;
530   }
531
532   vis_bin =
533       GST_BIN_CAST (gst_object_get_parent (GST_OBJECT_CAST
534           (play_bin->visualisation)));
535
536   if (!GST_IS_BIN (vis_bin) || !GST_IS_PAD (tee_pad)) {
537     goto beach;
538   }
539
540   vis_src_pad = gst_element_get_static_pad (play_bin->visualisation, "src");
541   vis_sink_pad = gst_pad_get_peer (tee_pad);
542
543   /* Can be fakesink */
544   if (GST_IS_PAD (vis_src_pad)) {
545     vqueue_pad = gst_pad_get_peer (vis_src_pad);
546   }
547
548   if (!GST_IS_PAD (vis_sink_pad)) {
549     goto beach;
550   }
551
552   /* Check the bin's state */
553   GST_OBJECT_LOCK (vis_bin);
554   bin_state = GST_STATE (vis_bin);
555   GST_OBJECT_UNLOCK (vis_bin);
556
557   /* Unlink */
558   gst_pad_unlink (tee_pad, vis_sink_pad);
559   gst_object_unref (vis_sink_pad);
560   vis_sink_pad = NULL;
561
562   if (GST_IS_PAD (vqueue_pad)) {
563     gst_pad_unlink (vis_src_pad, vqueue_pad);
564     gst_object_unref (vis_src_pad);
565     vis_src_pad = NULL;
566   }
567
568   /* Remove from vis_bin */
569   gst_bin_remove (vis_bin, play_bin->visualisation);
570   /* Set state to NULL */
571   gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
572   /* And loose our ref */
573   gst_object_unref (play_bin->visualisation);
574
575   if (pending_visualisation) {
576     /* Ref this new visualisation element before adding to the bin */
577     gst_object_ref (pending_visualisation);
578     /* Add the new one */
579     gst_bin_add (vis_bin, pending_visualisation);
580     /* Synchronizing state */
581     gst_element_set_state (pending_visualisation, bin_state);
582
583     vis_sink_pad = gst_element_get_static_pad (pending_visualisation, "sink");
584     vis_src_pad = gst_element_get_static_pad (pending_visualisation, "src");
585
586     if (!GST_IS_PAD (vis_sink_pad) || !GST_IS_PAD (vis_src_pad)) {
587       goto beach;
588     }
589
590     /* Link */
591     gst_pad_link (tee_pad, vis_sink_pad);
592     gst_pad_link (vis_src_pad, vqueue_pad);
593   }
594
595   /* We are done */
596   gst_object_unref (play_bin->visualisation);
597   play_bin->visualisation = pending_visualisation;
598
599 beach:
600   if (vis_sink_pad) {
601     gst_object_unref (vis_sink_pad);
602   }
603   if (vis_src_pad) {
604     gst_object_unref (vis_src_pad);
605   }
606   if (vqueue_pad) {
607     gst_object_unref (vqueue_pad);
608   }
609   if (vis_bin) {
610     gst_object_unref (vis_bin);
611   }
612
613   /* Unblock the pad */
614   gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_bin_vis_unblocked,
615       play_bin);
616 }
617
618 static void
619 gst_play_bin_set_property (GObject * object, guint prop_id,
620     const GValue * value, GParamSpec * pspec)
621 {
622   GstPlayBin *play_bin;
623
624   play_bin = GST_PLAY_BIN (object);
625
626   switch (prop_id) {
627     case ARG_VIDEO_SINK:
628       if (play_bin->video_sink != NULL) {
629         gst_object_unref (play_bin->video_sink);
630       }
631       play_bin->video_sink = g_value_get_object (value);
632       if (play_bin->video_sink != NULL) {
633         gst_object_ref (play_bin->video_sink);
634         gst_object_sink (GST_OBJECT_CAST (play_bin->video_sink));
635       }
636       /* when changing the videosink, we just remove the
637        * video pipeline from the cache so that it will be
638        * regenerated with the new sink element */
639       g_hash_table_remove (play_bin->cache, "vbin");
640       break;
641     case ARG_AUDIO_SINK:
642       if (play_bin->audio_sink != NULL) {
643         gst_object_unref (play_bin->audio_sink);
644       }
645       play_bin->audio_sink = g_value_get_object (value);
646       if (play_bin->audio_sink != NULL) {
647         gst_object_ref (play_bin->audio_sink);
648         gst_object_sink (GST_OBJECT_CAST (play_bin->audio_sink));
649       }
650       g_hash_table_remove (play_bin->cache, "abin");
651       break;
652     case ARG_VIS_PLUGIN:
653     {
654       GstElement *pending_visualisation =
655           GST_ELEMENT_CAST (g_value_get_object (value));
656
657       /* Take ownership */
658       if (pending_visualisation) {
659         gst_object_ref (pending_visualisation);
660         gst_object_sink (pending_visualisation);
661       }
662
663       /* Do we already have a visualisation change pending ? */
664       GST_OBJECT_LOCK (play_bin);
665       if (play_bin->pending_visualisation) {
666         gst_object_unref (play_bin->pending_visualisation);
667         play_bin->pending_visualisation = pending_visualisation;
668         GST_OBJECT_UNLOCK (play_bin);
669       } else {
670         GST_OBJECT_UNLOCK (play_bin);
671         /* Was there a visualisation already set ? */
672         if (play_bin->visualisation != NULL) {
673           GstBin *vis_bin = NULL;
674
675           vis_bin =
676               GST_BIN_CAST (gst_object_get_parent (GST_OBJECT_CAST
677                   (play_bin->visualisation)));
678
679           /* Check if the visualisation is already in a bin */
680           if (GST_IS_BIN (vis_bin)) {
681             GstPad *vis_sink_pad = NULL, *tee_pad = NULL;
682
683             /* Now get tee pad and block it async */
684             vis_sink_pad = gst_element_get_static_pad (play_bin->visualisation,
685                 "sink");
686             if (!GST_IS_PAD (vis_sink_pad)) {
687               goto beach;
688             }
689             tee_pad = gst_pad_get_peer (vis_sink_pad);
690             if (!GST_IS_PAD (tee_pad)) {
691               goto beach;
692             }
693
694             play_bin->pending_visualisation = pending_visualisation;
695             /* Block with callback */
696             gst_pad_set_blocked_async (tee_pad, TRUE, gst_play_bin_vis_blocked,
697                 play_bin);
698           beach:
699             if (vis_sink_pad) {
700               gst_object_unref (vis_sink_pad);
701             }
702             if (tee_pad) {
703               gst_object_unref (tee_pad);
704             }
705             gst_object_unref (vis_bin);
706           } else {
707             play_bin->visualisation = pending_visualisation;
708           }
709         } else {
710           play_bin->visualisation = pending_visualisation;
711         }
712       }
713       break;
714     }
715     case ARG_VOLUME:
716       play_bin->volume = g_value_get_double (value);
717       if (play_bin->volume_element) {
718         g_object_set (G_OBJECT (play_bin->volume_element), "volume",
719             play_bin->volume, NULL);
720       }
721       break;
722     case ARG_FONT_DESC:
723       g_free (play_bin->font_desc);
724       play_bin->font_desc = g_strdup (g_value_get_string (value));
725       if (play_bin->textoverlay_element) {
726         g_object_set (G_OBJECT (play_bin->textoverlay_element),
727             "font-desc", g_value_get_string (value), NULL);
728       }
729       break;
730     default:
731       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
732       break;
733   }
734 }
735
736 static void
737 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
738     GParamSpec * pspec)
739 {
740   GstPlayBin *play_bin;
741
742   play_bin = GST_PLAY_BIN (object);
743
744   switch (prop_id) {
745     case ARG_VIDEO_SINK:
746       g_value_set_object (value, play_bin->video_sink);
747       break;
748     case ARG_AUDIO_SINK:
749       g_value_set_object (value, play_bin->audio_sink);
750       break;
751     case ARG_VIS_PLUGIN:
752       g_value_set_object (value, play_bin->visualisation);
753       break;
754     case ARG_VOLUME:
755       g_value_set_double (value, play_bin->volume);
756       break;
757     case ARG_FRAME:{
758       GstBuffer *cur_frame = NULL;
759
760       gst_buffer_replace (&cur_frame, play_bin->frame);
761       gst_value_take_buffer (value, cur_frame);
762       break;
763     }
764     default:
765       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
766       break;
767   }
768 }
769
770 /* signal fired when the identity has received a new buffer. This is used for
771  * making screenshots.
772  */
773 static void
774 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
775 {
776   GstPlayBin *play_bin = GST_PLAY_BIN (data);
777
778   /* applications need to know the buffer caps,
779    * make sure they are always set on the frame */
780   if (GST_BUFFER_CAPS (frame) == NULL) {
781     GstPad *pad;
782
783     if ((pad = gst_element_get_static_pad (identity, "sink"))) {
784       gst_buffer_set_caps (frame, GST_PAD_CAPS (pad));
785       gst_object_unref (pad);
786     }
787   }
788
789   gst_buffer_replace (&play_bin->frame, frame);
790 }
791
792 static void
793 post_missing_element_message (GstPlayBin * playbin, const gchar * name)
794 {
795   GstMessage *msg;
796
797   msg = gst_missing_element_message_new (GST_ELEMENT_CAST (playbin), name);
798   gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
799 }
800
801 /* make the element (bin) that contains the elements needed to perform
802  * video display. We connect a handoff signal to identity so that we
803  * can grab snapshots. Identity's sinkpad is ghosted to vbin.
804  *
805  *  +-------------------------------------------------------------+
806  *  | vbin                                                        |
807  *  |      +--------+   +----------+   +----------+   +---------+ |
808  *  |      |identity|   |colorspace|   |videoscale|   |videosink| |
809  *  |   +-sink     src-sink       src-sink       src-sink       | |
810  *  |   |  +---+----+   +----------+   +----------+   +---------+ |
811  * sink-+      |                                                  |
812  *  +----------|--------------------------------------------------+
813  *           handoff
814  */
815 static GstElement *
816 gen_video_element (GstPlayBin * play_bin)
817 {
818   GstElement *element;
819   GstElement *conv;
820
821   GstElement *scale;
822   GstElement *sink;
823   GstElement *identity;
824   GstPad *pad;
825
826   /* first see if we have it in the cache */
827   element = g_hash_table_lookup (play_bin->cache, "vbin");
828   if (element != NULL) {
829     return element;
830   }
831
832   if (play_bin->video_sink) {
833     sink = play_bin->video_sink;
834   } else {
835     sink = gst_element_factory_make ("autovideosink", "videosink");
836     if (sink == NULL) {
837       sink = gst_element_factory_make ("xvimagesink", "videosink");
838     }
839     if (sink == NULL)
840       goto no_sinks;
841   }
842   gst_object_ref (sink);
843   g_hash_table_insert (play_bin->cache, "video_sink", sink);
844
845   /* create a bin to hold objects, as we create them we add them to this bin so
846    * that when something goes wrong we only need to unref the bin */
847   element = gst_bin_new ("vbin");
848   gst_bin_add (GST_BIN_CAST (element), sink);
849
850   conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
851   if (conv == NULL)
852     goto no_colorspace;
853   gst_bin_add (GST_BIN_CAST (element), conv);
854
855   scale = gst_element_factory_make ("videoscale", "vscale");
856   if (scale == NULL)
857     goto no_videoscale;
858   gst_bin_add (GST_BIN_CAST (element), scale);
859
860   identity = gst_element_factory_make ("identity", "id");
861   g_object_set (identity, "silent", TRUE, NULL);
862   g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
863   gst_bin_add (GST_BIN_CAST (element), identity);
864
865   gst_element_link_pads (identity, "src", conv, "sink");
866   gst_element_link_pads (conv, "src", scale, "sink");
867   /* be more careful with the pad from the custom sink element, it might not
868    * be named 'sink' */
869   if (!gst_element_link_pads (scale, "src", sink, NULL))
870     goto link_failed;
871
872   pad = gst_element_get_static_pad (identity, "sink");
873   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
874   gst_object_unref (pad);
875
876   gst_element_set_state (element, GST_STATE_READY);
877
878   /* since we're gonna add it to a bin but don't want to lose it,
879    * we keep a reference. */
880   gst_object_ref (element);
881   g_hash_table_insert (play_bin->cache, "vbin", element);
882
883   return element;
884
885   /* ERRORS */
886 no_sinks:
887   {
888     post_missing_element_message (play_bin, "autovideosink");
889     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
890         (_("Both autovideosink and xvimagesink elements are missing.")),
891         (NULL));
892     return NULL;
893   }
894 no_colorspace:
895   {
896     post_missing_element_message (play_bin, "ffmpegcolorspace");
897     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
898         (_("Missing element '%s' - check your GStreamer installation."),
899             "ffmpegcolorspace"), (NULL));
900     gst_object_unref (element);
901     return NULL;
902   }
903
904 no_videoscale:
905   {
906     post_missing_element_message (play_bin, "videoscale");
907     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
908         (_("Missing element '%s' - check your GStreamer installation."),
909             "videoscale"), ("possibly a liboil version mismatch?"));
910     gst_object_unref (element);
911     return NULL;
912   }
913 link_failed:
914   {
915     GST_ELEMENT_ERROR (play_bin, CORE, PAD,
916         (NULL), ("Failed to configure the video sink."));
917     gst_object_unref (element);
918     return NULL;
919   }
920 }
921
922 /* make an element for playback of video with subtitles embedded.
923  *
924  *  +--------------------------------------------------+
925  *  | tbin                  +-------------+            |
926  *  |          +-----+      | textoverlay |   +------+ |
927  *  |          | csp | +--video_sink      |   | vbin | |
928  * video_sink-sink  src+ +-text_sink    src---sink   | |
929  *  |          +-----+   |  +-------------+   +------+ |
930  * text_sink-------------+                             |
931  *  +--------------------------------------------------+
932  *
933  *  If there is no subtitle renderer this function will simply return the
934  *  videosink without the text_sink pad.
935  */
936 static GstElement *
937 add_text_element (GstPlayBin * play_bin, GstElement * vbin)
938 {
939   GstElement *element, *csp, *overlay;
940   GstPad *pad;
941
942   /* Text overlay */
943   overlay = gst_element_factory_make ("textoverlay", "overlay");
944
945   /* If no overlay return the video bin without subtitle support. */
946   if (!overlay)
947     goto no_overlay;
948
949   /* Create our bin */
950   element = gst_bin_new ("textbin");
951
952   /* Set some parameters */
953   g_object_set (G_OBJECT (overlay),
954       "halign", "center", "valign", "bottom", NULL);
955   if (play_bin->font_desc) {
956     g_object_set (G_OBJECT (overlay), "font-desc", play_bin->font_desc, NULL);
957   }
958
959   /* Take a ref */
960   play_bin->textoverlay_element = GST_ELEMENT_CAST (gst_object_ref (overlay));
961
962   /* we know this will succeed, as the video bin already created one before */
963   csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
964
965   /* Add our elements */
966   gst_bin_add_many (GST_BIN_CAST (element), csp, overlay, vbin, NULL);
967
968   /* Link */
969   gst_element_link_pads (csp, "src", overlay, "video_sink");
970   gst_element_link_pads (overlay, "src", vbin, "sink");
971
972   /* Add ghost pads on the subtitle bin */
973   pad = gst_element_get_static_pad (overlay, "text_sink");
974   gst_element_add_pad (element, gst_ghost_pad_new ("text_sink", pad));
975   gst_object_unref (pad);
976
977   pad = gst_element_get_static_pad (csp, "sink");
978   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
979   gst_object_unref (pad);
980
981   /* If the vbin provides a subpicture sink pad, ghost it too */
982   pad = gst_element_get_static_pad (vbin, "subpicture_sink");
983   if (pad) {
984     gst_element_add_pad (element, gst_ghost_pad_new ("subpicture_sink", pad));
985     gst_object_unref (pad);
986   }
987
988   /* Set state to READY */
989   gst_element_set_state (element, GST_STATE_READY);
990
991   return element;
992
993   /* ERRORS */
994 no_overlay:
995   {
996     post_missing_element_message (play_bin, "textoverlay");
997     GST_WARNING_OBJECT (play_bin,
998         "No overlay (pango) element, subtitles disabled");
999     return vbin;
1000   }
1001 }
1002
1003 /* make an element for rendering DVD subpictures onto output video
1004  *
1005  *  +---------------------------------------------+
1006  *  | tbin                   +--------+           |
1007  *  |          +-----+       |        |  +------+ |
1008  *  |          | csp | src-videosink  |  | vbin | |
1009  * video_sink-sink  src+     |       src-sink   | |
1010  *  |          +-----+   +subpicture  |  +------+ |
1011  * subpicture_pad--------+   +--------+           |
1012  *  +---------- ----------------------------------+
1013  *
1014  */
1015 static GstElement *
1016 add_spu_element (GstPlayBin * play_bin, GstElement * vbin)
1017 {
1018   GstElement *element, *csp, *overlay;
1019   GstPad *pad;
1020
1021   /* DVD spu overlay */
1022   GST_DEBUG_OBJECT (play_bin, "Attempting to insert DVD SPU element");
1023
1024   overlay = gst_element_factory_make ("dvdspu", "overlay");
1025
1026   /* If no overlay return the video bin without subpicture support. */
1027   if (!overlay)
1028     goto no_overlay;
1029
1030   /* Create our bin */
1031   element = gst_bin_new ("spubin");
1032
1033   /* Take a ref */
1034   play_bin->spu_element = GST_ELEMENT_CAST (gst_object_ref (overlay));
1035
1036   /* we know this will succeed, as the video bin already created one before */
1037   csp = gst_element_factory_make ("ffmpegcolorspace", "spucsp");
1038
1039   /* Add our elements */
1040   gst_bin_add_many (GST_BIN_CAST (element), csp, overlay, vbin, NULL);
1041
1042   /* Link */
1043   gst_element_link_pads (csp, "src", overlay, "video");
1044   gst_element_link_pads (overlay, "src", vbin, "sink");
1045
1046   /* Add ghost pad on the subpicture bin so it looks like vbin */
1047   pad = gst_element_get_static_pad (csp, "sink");
1048   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
1049   gst_object_unref (pad);
1050
1051   pad = gst_element_get_static_pad (overlay, "subpicture");
1052   gst_element_add_pad (element, gst_ghost_pad_new ("subpicture_sink", pad));
1053   gst_object_unref (pad);
1054
1055   /* Set state to READY */
1056   gst_element_set_state (element, GST_STATE_READY);
1057
1058   return element;
1059
1060   /* ERRORS */
1061 no_overlay:
1062   {
1063     post_missing_element_message (play_bin, "dvdspu");
1064     GST_WARNING_OBJECT (play_bin,
1065         "No DVD overlay (dvdspu) element. "
1066         "menu highlight/subtitles unavailable");
1067     return vbin;
1068   }
1069 }
1070
1071 /* make the element (bin) that contains the elements needed to perform
1072  * audio playback.
1073  *
1074  *  +-------------------------------------------------------------+
1075  *  | abin                                                        |
1076  *  |      +---------+   +----------+   +---------+   +---------+ |
1077  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
1078  *  |   +-sink      src-sink       src-sink      src-sink       | |
1079  *  |   |  +---------+   +----------+   +---------+   +---------+ |
1080  * sink-+                                                         |
1081  *  +-------------------------------------------------------------+
1082  */
1083 static GstElement *
1084 gen_audio_element (GstPlayBin * play_bin)
1085 {
1086   gboolean res;
1087   GstElement *element;
1088   GstElement *conv;
1089   GstElement *scale;
1090   GstElement *sink;
1091   GstElement *volume;
1092   GstPad *pad;
1093
1094   element = g_hash_table_lookup (play_bin->cache, "abin");
1095   if (element != NULL)
1096     return element;
1097
1098   if (play_bin->audio_sink) {
1099     sink = play_bin->audio_sink;
1100   } else {
1101     sink = gst_element_factory_make ("autoaudiosink", "audiosink");
1102     if (sink == NULL) {
1103       sink = gst_element_factory_make ("alsasink", "audiosink");
1104     }
1105     if (sink == NULL)
1106       goto no_sinks;
1107
1108     play_bin->audio_sink = GST_ELEMENT_CAST (gst_object_ref (sink));
1109   }
1110
1111   gst_object_ref (sink);
1112   g_hash_table_insert (play_bin->cache, "audio_sink", sink);
1113
1114   element = gst_bin_new ("abin");
1115   gst_bin_add (GST_BIN_CAST (element), sink);
1116
1117   conv = gst_element_factory_make ("audioconvert", "aconv");
1118   if (conv == NULL)
1119     goto no_audioconvert;
1120   gst_bin_add (GST_BIN_CAST (element), conv);
1121
1122   scale = gst_element_factory_make ("audioresample", "aresample");
1123   if (scale == NULL)
1124     goto no_audioresample;
1125   gst_bin_add (GST_BIN_CAST (element), scale);
1126
1127   volume = gst_element_factory_make ("volume", "volume");
1128   if (volume == NULL)
1129     goto no_volume;
1130   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
1131   play_bin->volume_element = GST_ELEMENT_CAST (gst_object_ref (volume));
1132   gst_bin_add (GST_BIN_CAST (element), volume);
1133
1134   res = gst_element_link_pads (conv, "src", scale, "sink");
1135   res &= gst_element_link_pads (scale, "src", volume, "sink");
1136   res &= gst_element_link_pads (volume, "src", sink, NULL);
1137   if (!res)
1138     goto link_failed;
1139
1140   pad = gst_element_get_static_pad (conv, "sink");
1141   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
1142   gst_object_unref (pad);
1143
1144   gst_element_set_state (element, GST_STATE_READY);
1145
1146   /* since we're gonna add it to a bin but don't want to lose it,
1147    * we keep a reference. */
1148   gst_object_ref (element);
1149   g_hash_table_insert (play_bin->cache, "abin", element);
1150
1151   return element;
1152
1153   /* ERRORS */
1154 no_sinks:
1155   {
1156     post_missing_element_message (play_bin, "autoaudiosink");
1157     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1158         (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
1159     return NULL;
1160   }
1161 no_audioconvert:
1162   {
1163     post_missing_element_message (play_bin, "audioconvert");
1164     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1165         (_("Missing element '%s' - check your GStreamer installation."),
1166             "audioconvert"), ("possibly a liboil version mismatch?"));
1167     gst_object_unref (element);
1168     return NULL;
1169   }
1170 no_audioresample:
1171   {
1172     post_missing_element_message (play_bin, "audioresample");
1173     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1174         (_("Missing element '%s' - check your GStreamer installation."),
1175             "audioresample"), ("possibly a liboil version mismatch?"));
1176     gst_object_unref (element);
1177     return NULL;
1178   }
1179 no_volume:
1180   {
1181     post_missing_element_message (play_bin, "volume");
1182     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1183         (_("Missing element '%s' - check your GStreamer installation."),
1184             "volume"), ("possibly a liboil version mismatch?"));
1185     gst_object_unref (element);
1186     return NULL;
1187   }
1188 link_failed:
1189   {
1190     GST_ELEMENT_ERROR (play_bin, CORE, PAD,
1191         (NULL), ("Failed to configure the audio sink."));
1192     gst_object_unref (element);
1193     return NULL;
1194   }
1195 }
1196
1197 /* make the element (bin) that contains the elements needed to perform
1198  * visualisation ouput.  The idea is to split the audio using tee, then
1199  * sending the output to the regular audio bin and the other output to
1200  * the vis plugin that transforms it into a video that is rendered with the
1201  * normal video bin. The video and audio bins are run in threads to make sure
1202  * they don't block eachother.
1203  *
1204  *  +-----------------------------------------------------------------------+
1205  *  | visbin                                                                |
1206  *  |      +------+   +--------+   +----------------+                       |
1207  *  |      | tee  |   | aqueue |   |   abin ...     |                       |
1208  *  |   +-sink   src-sink     src-sink              |                       |
1209  *  |   |  |      |   +--------+   +----------------+                       |
1210  *  |   |  |      |                                                         |
1211  *  |   |  |      |   +------+   +------------+   +------+   +-----------+  |
1212  *  |   |  |      |   |vqueue|   | audioconv  |   | vis  |   | vbin ...  |  |
1213  *  |   |  |     src-sink   src-sink + samp  src-sink   src-sink         |  |
1214  *  |   |  |      |   +------+   +------------+   +------+   +-----------+  |
1215  *  |   |  |      |                                                         |
1216  *  |   |  +------+                                                         |
1217  * sink-+                                                                   |
1218  *  +-----------------------------------------------------------------------+
1219  */
1220 static GstElement *
1221 gen_vis_element (GstPlayBin * play_bin)
1222 {
1223   gboolean res;
1224   GstElement *element;
1225   GstElement *tee;
1226   GstElement *asink;
1227   GstElement *vsink;
1228   GstElement *conv;
1229   GstElement *resamp;
1230   GstElement *conv2;
1231   GstElement *vis;
1232   GstElement *vqueue, *aqueue;
1233   GstPad *pad, *rpad;
1234
1235   /* errors are already posted when these fail. */
1236   asink = gen_audio_element (play_bin);
1237   if (!asink)
1238     return NULL;
1239   vsink = gen_video_element (play_bin);
1240   if (!vsink) {
1241     gst_object_unref (asink);
1242     return NULL;
1243   }
1244
1245   element = gst_bin_new ("visbin");
1246   tee = gst_element_factory_make ("tee", "tee");
1247
1248   vqueue = gst_element_factory_make ("queue", "vqueue");
1249   aqueue = gst_element_factory_make ("queue", "aqueue");
1250
1251   gst_bin_add (GST_BIN_CAST (element), asink);
1252   gst_bin_add (GST_BIN_CAST (element), vqueue);
1253   gst_bin_add (GST_BIN_CAST (element), aqueue);
1254   gst_bin_add (GST_BIN_CAST (element), vsink);
1255   gst_bin_add (GST_BIN_CAST (element), tee);
1256
1257   conv = gst_element_factory_make ("audioconvert", "aconv");
1258   if (conv == NULL)
1259     goto no_audioconvert;
1260   gst_bin_add (GST_BIN_CAST (element), conv);
1261
1262   resamp = gst_element_factory_make ("audioresample", "aresamp");
1263   if (resamp == NULL)
1264     goto no_audioresample;
1265   gst_bin_add (GST_BIN_CAST (element), resamp);
1266
1267   conv2 = gst_element_factory_make ("audioconvert", "aconv2");
1268   if (conv2 == NULL)
1269     goto no_audioconvert;
1270   gst_bin_add (GST_BIN_CAST (element), conv2);
1271
1272   if (play_bin->visualisation) {
1273     gst_object_ref (play_bin->visualisation);
1274     vis = play_bin->visualisation;
1275   } else {
1276     vis = gst_element_factory_make ("goom", "vis");
1277     if (!vis)
1278       goto no_goom;
1279   }
1280   gst_bin_add (GST_BIN_CAST (element), vis);
1281
1282   res = gst_element_link_pads (vqueue, "src", conv, "sink");
1283   res &= gst_element_link_pads (conv, "src", resamp, "sink");
1284   res &= gst_element_link_pads (resamp, "src", conv2, "sink");
1285   res &= gst_element_link_pads (conv2, "src", vis, "sink");
1286   res &= gst_element_link_pads (vis, "src", vsink, "sink");
1287   if (!res)
1288     goto link_failed;
1289
1290   pad = gst_element_get_static_pad (aqueue, "sink");
1291   rpad = gst_element_get_request_pad (tee, "src%d");
1292   gst_pad_link (rpad, pad);
1293   gst_object_unref (rpad);
1294   gst_object_unref (pad);
1295   gst_element_link_pads (aqueue, "src", asink, "sink");
1296
1297   pad = gst_element_get_static_pad (vqueue, "sink");
1298   rpad = gst_element_get_request_pad (tee, "src%d");
1299   gst_pad_link (rpad, pad);
1300   gst_object_unref (rpad);
1301   gst_object_unref (pad);
1302
1303   pad = gst_element_get_static_pad (tee, "sink");
1304   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
1305   gst_object_unref (pad);
1306
1307   return element;
1308
1309   /* ERRORS */
1310 no_audioconvert:
1311   {
1312     post_missing_element_message (play_bin, "audioconvert");
1313     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1314         (_("Missing element '%s' - check your GStreamer installation."),
1315             "audioconvert"), ("possibly a liboil version mismatch?"));
1316     gst_object_unref (element);
1317     return NULL;
1318   }
1319 no_audioresample:
1320   {
1321     post_missing_element_message (play_bin, "audioresample");
1322     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1323         (_("Missing element '%s' - check your GStreamer installation."),
1324             "audioresample"), (NULL));
1325     gst_object_unref (element);
1326     return NULL;
1327   }
1328 no_goom:
1329   {
1330     post_missing_element_message (play_bin, "goom");
1331     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1332         (_("Missing element '%s' - check your GStreamer installation."),
1333             "goom"), (NULL));
1334     gst_object_unref (element);
1335     return NULL;
1336   }
1337 link_failed:
1338   {
1339     GST_ELEMENT_ERROR (play_bin, CORE, PAD,
1340         (NULL), ("Failed to configure the visualisation element."));
1341     gst_object_unref (element);
1342     return NULL;
1343   }
1344 }
1345
1346 /* get rid of all installed sinks */
1347 static void
1348 remove_sinks (GstPlayBin * play_bin)
1349 {
1350   GList *sinks;
1351   GstObject *parent;
1352   GstElement *element;
1353   GstPad *pad, *peer;
1354
1355   if (play_bin->cache == NULL)
1356     return;
1357
1358   GST_DEBUG ("removesinks");
1359   element = g_hash_table_lookup (play_bin->cache, "abin");
1360   if (element != NULL) {
1361     parent = gst_element_get_parent (element);
1362     if (parent != NULL) {
1363       /* we remove the element from the parent so that
1364        * there is no unwanted state change when the parent
1365        * is disposed */
1366       play_bin->sinks = g_list_remove (play_bin->sinks, element);
1367       gst_element_set_state (element, GST_STATE_NULL);
1368       gst_bin_remove (GST_BIN_CAST (parent), element);
1369       gst_object_unref (parent);
1370     }
1371     pad = gst_element_get_static_pad (element, "sink");
1372     if (pad != NULL) {
1373       peer = gst_pad_get_peer (pad);
1374       if (peer != NULL) {
1375         gst_pad_unlink (peer, pad);
1376         gst_object_unref (peer);
1377       }
1378       gst_object_unref (pad);
1379     }
1380   }
1381   element = g_hash_table_lookup (play_bin->cache, "vbin");
1382   if (element != NULL) {
1383     parent = gst_element_get_parent (element);
1384     if (parent != NULL) {
1385       play_bin->sinks = g_list_remove (play_bin->sinks, element);
1386       gst_element_set_state (element, GST_STATE_NULL);
1387       gst_bin_remove (GST_BIN_CAST (parent), element);
1388       gst_object_unref (parent);
1389     }
1390     pad = gst_element_get_static_pad (element, "sink");
1391     if (pad != NULL) {
1392       peer = gst_pad_get_peer (pad);
1393       if (peer != NULL) {
1394         gst_pad_unlink (peer, pad);
1395         gst_object_unref (peer);
1396       }
1397       gst_object_unref (pad);
1398     }
1399   }
1400
1401   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
1402     GstElement *element = GST_ELEMENT_CAST (sinks->data);
1403     GstPad *pad;
1404     GstPad *peer;
1405
1406     pad = gst_element_get_static_pad (element, "sink");
1407
1408     GST_LOG ("removing sink %p", element);
1409
1410     peer = gst_pad_get_peer (pad);
1411     if (peer) {
1412       gst_pad_unlink (peer, pad);
1413       gst_object_unref (peer);
1414     }
1415     gst_object_unref (pad);
1416
1417     gst_element_set_state (element, GST_STATE_NULL);
1418     gst_bin_remove (GST_BIN_CAST (play_bin), element);
1419   }
1420   g_list_free (play_bin->sinks);
1421   play_bin->sinks = NULL;
1422
1423   if (play_bin->visualisation) {
1424     GstElement *vis_bin;
1425
1426     vis_bin =
1427         GST_ELEMENT_CAST (gst_element_get_parent (play_bin->visualisation));
1428
1429     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
1430
1431     if (vis_bin) {
1432       gst_bin_remove (GST_BIN_CAST (vis_bin), play_bin->visualisation);
1433       gst_object_unref (vis_bin);
1434     }
1435   }
1436
1437   if (play_bin->frame) {
1438     gst_buffer_unref (play_bin->frame);
1439     play_bin->frame = NULL;
1440   }
1441
1442   if (play_bin->textoverlay_element) {
1443     gst_object_unref (play_bin->textoverlay_element);
1444     play_bin->textoverlay_element = NULL;
1445   }
1446
1447   if (play_bin->volume_element) {
1448     gst_object_unref (play_bin->volume_element);
1449     play_bin->volume_element = NULL;
1450   }
1451 }
1452
1453 /* loop over the streams and set up the pipeline to play this
1454  * media file. First we count the number of audio and video streams.
1455  * If there is no video stream but there exists an audio stream,
1456  * we install a visualisation pipeline.
1457  *
1458  * Also make sure to only connect the first audio and video pad. FIXME
1459  * this should eventually be handled with a tuner interface so that
1460  * one can switch the streams.
1461  *
1462  * This function takes ownership of @sink.*
1463  */
1464 static gboolean
1465 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
1466     GstPad * subtitle_pad)
1467 {
1468   GstPad *sinkpad;
1469   GstPadLinkReturn linkres;
1470   GstElement *parent;
1471   GstStateChangeReturn stateret;
1472   GstState state;
1473
1474   g_return_val_if_fail (sink != NULL, FALSE);
1475
1476   state = GST_STATE_PAUSED;
1477
1478   /* this is only for debugging */
1479   parent = gst_pad_get_parent_element (srcpad);
1480   if (parent) {
1481     GST_DEBUG ("Adding sink %" GST_PTR_FORMAT
1482         " with state %d (parent: %d, peer: %d)", sink,
1483         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
1484     gst_object_unref (parent);
1485   }
1486   gst_bin_add (GST_BIN_CAST (play_bin), sink);
1487
1488   /* bring it to the required state so we can link to the peer without
1489    * breaking the flow */
1490   stateret = gst_element_set_state (sink, state);
1491   if (stateret == GST_STATE_CHANGE_FAILURE)
1492     goto state_failed;
1493
1494   /* we found a sink for this stream, now try to install it */
1495   sinkpad = gst_element_get_static_pad (sink, "sink");
1496   linkres = gst_pad_link (srcpad, sinkpad);
1497   gst_object_unref (sinkpad);
1498
1499   /* try to link the pad of the sink to the stream */
1500   if (GST_PAD_LINK_FAILED (linkres))
1501     goto link_failed;
1502
1503   if (GST_IS_PAD (subtitle_pad)) {
1504     sinkpad = gst_element_get_static_pad (sink, "text_sink");
1505     linkres = gst_pad_link (subtitle_pad, sinkpad);
1506     gst_object_unref (sinkpad);
1507   }
1508
1509   /* try to link the subtitle pad of the sink to the stream, this is not
1510    * fatal. */
1511   if (GST_PAD_LINK_FAILED (linkres))
1512     goto subtitle_failed;
1513
1514 done:
1515   /* we got the sink succesfully linked, now keep the sink
1516    * in our internal list */
1517   play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1518
1519   return TRUE;
1520
1521   /* ERRORS */
1522 state_failed:
1523   {
1524     gst_element_set_state (sink, GST_STATE_NULL);
1525     gst_bin_remove (GST_BIN_CAST (play_bin), sink);
1526     GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1527     return FALSE;
1528   }
1529 link_failed:
1530   {
1531     gchar *capsstr;
1532     GstCaps *caps;
1533
1534     /* could not link this stream */
1535     caps = gst_pad_get_caps (srcpad);
1536     capsstr = gst_caps_to_string (caps);
1537     g_warning ("could not link %s: %d", capsstr, linkres);
1538     GST_DEBUG_OBJECT (play_bin,
1539         "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1540     g_free (capsstr);
1541     gst_caps_unref (caps);
1542
1543     gst_element_set_state (sink, GST_STATE_NULL);
1544     gst_bin_remove (GST_BIN_CAST (play_bin), sink);
1545     return FALSE;
1546   }
1547 subtitle_failed:
1548   {
1549     GstCaps *caps;
1550
1551     /* could not link this stream */
1552     caps = gst_pad_get_caps (subtitle_pad);
1553     GST_WARNING_OBJECT (play_bin, "subtitle link failed when adding sink, "
1554         "caps = %" GST_PTR_FORMAT ", reason %d", caps, linkres);
1555     gst_caps_unref (caps);
1556
1557     /* not fatal */
1558     goto done;
1559   }
1560 }
1561
1562 static void
1563 dummy_blocked_cb (GstPad * pad, gboolean blocked, gpointer user_data)
1564 {
1565 }
1566
1567 static gboolean
1568 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1569 {
1570   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1571   gboolean have_video = FALSE;
1572   gboolean need_vis = FALSE;
1573   gboolean need_text = FALSE;
1574   gboolean need_spu = FALSE;
1575   GstPad *textsrcpad = NULL, *pad = NULL, *origtextsrcpad = NULL;
1576   GstElement *sink;
1577   gboolean res = TRUE;
1578
1579   /* get rid of existing sinks */
1580   if (play_bin->sinks) {
1581     remove_sinks (play_bin);
1582   }
1583   GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1584
1585   /* find out what to do */
1586   have_video = (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0);
1587   need_spu = (group->type[GST_STREAM_TYPE_SUBPICTURE - 1].npads != 0);
1588
1589   if (have_video && group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1590     need_text = TRUE;
1591   } else if (!have_video &&
1592       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1593       play_bin->visualisation != NULL) {
1594     need_vis = TRUE;
1595   }
1596
1597   /* now actually connect everything */
1598
1599   /* link audio */
1600   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1601     if (need_vis) {
1602       sink = gen_vis_element (play_bin);
1603     } else {
1604       sink = gen_audio_element (play_bin);
1605     }
1606     if (!sink)
1607       return FALSE;
1608
1609     pad =
1610         gst_element_get_static_pad (group->type[GST_STREAM_TYPE_AUDIO -
1611             1].preroll, "src");
1612     res = add_sink (play_bin, sink, pad, NULL);
1613     gst_object_unref (pad);
1614   }
1615
1616   /* link video */
1617   if (have_video) {
1618     /* Create the video rendering bin, error is posted when this fails. */
1619     sink = gen_video_element (play_bin);
1620     if (!sink)
1621       return FALSE;
1622     if (need_spu) {
1623       sink = add_spu_element (play_bin, sink);
1624     }
1625
1626     if (need_text) {
1627       GstObject *parent = NULL, *grandparent = NULL;
1628       GstPad *ghost = NULL;
1629
1630       /* Add the subtitle overlay element into the video sink */
1631       sink = add_text_element (play_bin, sink);
1632
1633       /* Link the incoming subtitle stream into the output bin */
1634       textsrcpad =
1635           gst_element_get_static_pad (group->type[GST_STREAM_TYPE_TEXT -
1636               1].preroll, "src");
1637
1638       /* This pad is from subtitle-bin, we need to create a ghost pad to have
1639          common grandparents */
1640       parent = gst_object_get_parent (GST_OBJECT_CAST (textsrcpad));
1641       if (!parent) {
1642         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1643         gst_object_unref (textsrcpad);
1644         textsrcpad = NULL;
1645         goto beach;
1646       }
1647
1648       grandparent = gst_object_get_parent (parent);
1649       if (!grandparent) {
1650         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1651         gst_object_unref (parent);
1652         gst_object_unref (textsrcpad);
1653         textsrcpad = NULL;
1654         goto beach;
1655       }
1656
1657       /* We ghost the pad on subtitle_bin only, if the text pad is from the
1658          media demuxer we keep it as it is */
1659       if (!GST_IS_PLAY_BIN (grandparent)) {
1660         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1661             "file, ghosting to a suitable hierarchy");
1662         /* Block the pad first, because as soon as we add a ghostpad, the queue
1663          * will try and start pushing */
1664         gst_pad_set_blocked_async (textsrcpad, TRUE, dummy_blocked_cb, NULL);
1665         origtextsrcpad = gst_object_ref (textsrcpad);
1666
1667         ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1668         if (!GST_IS_PAD (ghost)) {
1669           GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1670               "subtitle-bin");
1671           gst_object_unref (parent);
1672           gst_object_unref (grandparent);
1673           gst_object_unref (textsrcpad);
1674           textsrcpad = NULL;
1675           goto beach;
1676         }
1677
1678         gst_pad_set_active (ghost, TRUE);
1679         if (gst_element_add_pad (GST_ELEMENT_CAST (grandparent), ghost)) {
1680           gst_object_unref (textsrcpad);
1681           textsrcpad = gst_object_ref (ghost);
1682         } else {
1683           GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1684           gst_pad_set_active (ghost, FALSE);
1685           gst_object_unref (ghost);
1686           gst_object_unref (textsrcpad);
1687           textsrcpad = NULL;
1688         }
1689       } else {
1690         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1691             "no changes to hierarchy needed");
1692       }
1693
1694       gst_object_unref (parent);
1695       gst_object_unref (grandparent);
1696     }
1697   beach:
1698     if (!sink)
1699       return FALSE;
1700     pad =
1701         gst_element_get_static_pad (group->type[GST_STREAM_TYPE_VIDEO -
1702             1].preroll, "src");
1703     res = add_sink (play_bin, sink, pad, textsrcpad);
1704     gst_object_unref (pad);
1705     if (textsrcpad)
1706       gst_object_unref (textsrcpad);
1707     if (origtextsrcpad) {
1708       gst_pad_set_blocked_async (origtextsrcpad, FALSE, dummy_blocked_cb, NULL);
1709       gst_object_unref (origtextsrcpad);
1710     }
1711
1712     /* If we have a DVD subpicture stream, link it to the SPU now */
1713     if (need_spu) {
1714       GstPad *subpic_pad;
1715       GstPad *spu_sink_pad;
1716
1717       subpic_pad =
1718           gst_element_get_static_pad (group->type[GST_STREAM_TYPE_SUBPICTURE
1719               - 1].preroll, "src");
1720       spu_sink_pad = gst_element_get_static_pad (sink, "subpicture_sink");
1721       if (subpic_pad && spu_sink_pad) {
1722         GST_LOG_OBJECT (play_bin, "Linking DVD subpicture stream onto SPU");
1723         gst_pad_set_blocked_async (subpic_pad, TRUE, dummy_blocked_cb, NULL);
1724         if (gst_pad_link (subpic_pad, spu_sink_pad) != GST_PAD_LINK_OK) {
1725           GST_WARNING_OBJECT (play_bin,
1726               "Failed to link DVD subpicture stream onto SPU");
1727         }
1728         gst_pad_set_blocked_async (subpic_pad, FALSE, dummy_blocked_cb, NULL);
1729       }
1730       if (subpic_pad)
1731         gst_object_unref (subpic_pad);
1732       if (spu_sink_pad)
1733         gst_object_unref (spu_sink_pad);
1734     }
1735   }
1736
1737   /* remove the sinks now, pipeline get_state will now wait for the
1738    * sinks to preroll */
1739   if (play_bin->fakesink) {
1740     gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1741     gst_bin_remove (GST_BIN_CAST (play_bin), play_bin->fakesink);
1742     play_bin->fakesink = NULL;
1743   }
1744
1745   return res;
1746 }
1747
1748 static void
1749 playbin_set_subtitles_visible (GstPlayBaseBin * play_base_bin, gboolean visible)
1750 {
1751   GstPlayBin *playbin = GST_PLAY_BIN (play_base_bin);
1752
1753   /* we're ignoring the case of someone setting the 'current-text' property
1754    * before textoverlay is set up (which is probably okay, since playbasebin
1755    * will just select the first subtitle stream as active stream regardless) */
1756   if (playbin->textoverlay_element != NULL) {
1757     GST_LOG_OBJECT (playbin, "setting subtitle visibility to %d", visible);
1758     g_object_set (playbin->textoverlay_element, "silent", !visible, NULL);
1759   }
1760 }
1761
1762 static void
1763 playbin_set_audio_mute (GstPlayBaseBin * play_base_bin, gboolean mute)
1764 {
1765   GstPlayBin *playbin = GST_PLAY_BIN (play_base_bin);
1766
1767   if (playbin->volume_element) {
1768     g_object_set (G_OBJECT (playbin->volume_element), "mute", mute, NULL);
1769   }
1770 }
1771
1772 /* Send an event to our sinks until one of them works; don't then send to the
1773  * remaining sinks (unlike GstBin)
1774  */
1775 static gboolean
1776 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1777 {
1778   GList *sinks = play_bin->sinks;
1779   gboolean res = TRUE;
1780
1781   while (sinks) {
1782     GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1783
1784     gst_event_ref (event);
1785     if ((res = gst_element_send_event (sink, event))) {
1786       GST_DEBUG_OBJECT (play_bin,
1787           "Sent event succesfully to sink %" GST_PTR_FORMAT, sink);
1788       break;
1789     }
1790     GST_DEBUG_OBJECT (play_bin,
1791         "Event failed when sent to sink %" GST_PTR_FORMAT, sink);
1792
1793     sinks = g_list_next (sinks);
1794   }
1795
1796   gst_event_unref (event);
1797
1798   return res;
1799 }
1800
1801 /* We only want to send the event to a single sink (overriding GstBin's
1802  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1803  * events appropriately. So, this is a messy duplication of code. */
1804 static gboolean
1805 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1806 {
1807   gboolean res = FALSE;
1808   GstEventType event_type = GST_EVENT_TYPE (event);
1809
1810   switch (event_type) {
1811     case GST_EVENT_SEEK:
1812       GST_DEBUG_OBJECT (element, "Sending seek event to a sink");
1813       res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1814       break;
1815     default:
1816       res = parent_class->send_event (element, event);
1817       break;
1818   }
1819
1820   return res;
1821 }
1822
1823 static void
1824 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
1825     GList * structure_list)
1826 {
1827   GList *l;
1828
1829   for (l = structure_list; l != NULL; l = l->next) {
1830     GValue val = { 0, };
1831
1832     if (*first == NULL)
1833       *first = gst_structure_copy ((GstStructure *) l->data);
1834
1835     g_value_init (&val, GST_TYPE_STRUCTURE);
1836     g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
1837     gst_value_list_append_value (list_val, &val);
1838     g_value_unset (&val);
1839   }
1840 }
1841
1842 /* if it's a redirect message with multiple redirect locations we might
1843  * want to pick a different 'best' location depending on the required
1844  * bitrates and the connection speed */
1845 static GstMessage *
1846 gst_play_bin_handle_redirect_message (GstPlayBin * playbin, GstMessage * msg)
1847 {
1848   const GValue *locations_list, *location_val;
1849   GstMessage *new_msg;
1850   GstStructure *new_structure = NULL;
1851   GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
1852   GValue new_list = { 0, };
1853   guint size, i;
1854   GstPlayBaseBin *playbasebin = GST_PLAY_BASE_BIN (playbin);
1855   guint connection_speed = playbasebin->connection_speed;
1856
1857   GST_DEBUG_OBJECT (playbin, "redirect message: %" GST_PTR_FORMAT, msg);
1858   GST_DEBUG_OBJECT (playbin, "connection speed: %u", connection_speed);
1859
1860   if (connection_speed == 0 || msg->structure == NULL)
1861     return msg;
1862
1863   locations_list = gst_structure_get_value (msg->structure, "locations");
1864   if (locations_list == NULL)
1865     return msg;
1866
1867   size = gst_value_list_get_size (locations_list);
1868   if (size < 2)
1869     return msg;
1870
1871   /* maintain existing order as much as possible, just sort references
1872    * with too high a bitrate to the end (the assumption being that if
1873    * bitrates are given they are given for all interesting streams and
1874    * that the you-need-at-least-version-xyz redirect has the same bitrate
1875    * as the lowest referenced redirect alternative) */
1876   for (i = 0; i < size; ++i) {
1877     const GstStructure *s;
1878     gint bitrate = 0;
1879
1880     location_val = gst_value_list_get_value (locations_list, i);
1881     s = (const GstStructure *) g_value_get_boxed (location_val);
1882     if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
1883       GST_DEBUG_OBJECT (playbin, "no bitrate: %" GST_PTR_FORMAT, s);
1884       l_neutral = g_list_append (l_neutral, (gpointer) s);
1885     } else if (bitrate > connection_speed) {
1886       GST_DEBUG_OBJECT (playbin, "bitrate too high: %" GST_PTR_FORMAT, s);
1887       l_bad = g_list_append (l_bad, (gpointer) s);
1888     } else if (bitrate <= connection_speed) {
1889       GST_DEBUG_OBJECT (playbin, "bitrate OK: %" GST_PTR_FORMAT, s);
1890       l_good = g_list_append (l_good, (gpointer) s);
1891     }
1892   }
1893
1894   g_value_init (&new_list, GST_TYPE_LIST);
1895   value_list_append_structure_list (&new_list, &new_structure, l_good);
1896   value_list_append_structure_list (&new_list, &new_structure, l_neutral);
1897   value_list_append_structure_list (&new_list, &new_structure, l_bad);
1898   gst_structure_set_value (new_structure, "locations", &new_list);
1899   g_value_unset (&new_list);
1900
1901   g_list_free (l_good);
1902   g_list_free (l_neutral);
1903   g_list_free (l_bad);
1904
1905   new_msg = gst_message_new_element (msg->src, new_structure);
1906   gst_message_unref (msg);
1907
1908   GST_DEBUG_OBJECT (playbin, "new redirect message: %" GST_PTR_FORMAT, new_msg);
1909   return new_msg;
1910 }
1911
1912 static void
1913 gst_play_bin_handle_message (GstBin * bin, GstMessage * msg)
1914 {
1915   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT && msg->structure != NULL
1916       && gst_structure_has_name (msg->structure, "redirect")) {
1917     msg = gst_play_bin_handle_redirect_message (GST_PLAY_BIN (bin), msg);
1918   }
1919
1920   GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
1921 }
1922
1923 static GstStateChangeReturn
1924 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1925 {
1926   GstStateChangeReturn ret;
1927   GstPlayBin *play_bin;
1928
1929   play_bin = GST_PLAY_BIN (element);
1930
1931
1932   switch (transition) {
1933     case GST_STATE_CHANGE_READY_TO_PAUSED:
1934       /* this really is the easiest way to make the state change return
1935        * ASYNC until we added the sinks */
1936       if (!play_bin->fakesink) {
1937         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1938         gst_bin_add (GST_BIN_CAST (play_bin), play_bin->fakesink);
1939       }
1940       break;
1941     default:
1942       break;
1943   }
1944
1945   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1946   if (ret == GST_STATE_CHANGE_FAILURE)
1947     return ret;
1948
1949   switch (transition) {
1950     case GST_STATE_CHANGE_READY_TO_PAUSED:
1951       /* remember us being a live pipeline */
1952       play_bin->is_live = (ret == GST_STATE_CHANGE_NO_PREROLL);
1953       GST_DEBUG_OBJECT (play_bin, "is live: %d", play_bin->is_live);
1954       break;
1955     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1956       /* FIXME Release audio device when we implement that */
1957       break;
1958     case GST_STATE_CHANGE_PAUSED_TO_READY:
1959     case GST_STATE_CHANGE_READY_TO_NULL:
1960       /* remove sinks we added */
1961       remove_sinks (play_bin);
1962       /* and there might be a fakesink we need to clean up now */
1963       if (play_bin->fakesink) {
1964         gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1965         gst_bin_remove (GST_BIN_CAST (play_bin), play_bin->fakesink);
1966         play_bin->fakesink = NULL;
1967       }
1968       break;
1969     default:
1970       break;
1971   }
1972
1973   return ret;
1974 }
1975
1976 gboolean
1977 gst_play_bin_plugin_init (GstPlugin * plugin)
1978 {
1979   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1980
1981   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1982       GST_TYPE_PLAY_BIN);
1983 }