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