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