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