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