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