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