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