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