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