Make switching off of subtitles work. To avoid all kind of problems with unlinking...
[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   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
1056   play_bin->volume_element = volume;
1057   gst_bin_add (GST_BIN_CAST (element), volume);
1058
1059   res = gst_element_link_pads (conv, "src", scale, "sink");
1060   res &= gst_element_link_pads (scale, "src", volume, "sink");
1061   res &= gst_element_link_pads (volume, "src", sink, NULL);
1062   if (!res)
1063     goto link_failed;
1064
1065   pad = gst_element_get_pad (conv, "sink");
1066   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
1067   gst_object_unref (pad);
1068
1069   gst_element_set_state (element, GST_STATE_READY);
1070
1071   /* since we're gonna add it to a bin but don't want to lose it,
1072    * we keep a reference. */
1073   gst_object_ref (element);
1074   g_hash_table_insert (play_bin->cache, "abin", element);
1075
1076   return element;
1077
1078   /* ERRORS */
1079 no_sinks:
1080   {
1081     post_missing_element_message (play_bin, "autoaudiosink");
1082     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1083         (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
1084     return NULL;
1085   }
1086 no_audioconvert:
1087   {
1088     post_missing_element_message (play_bin, "audioconvert");
1089     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1090         (_("Missing element '%s' - check your GStreamer installation."),
1091             "audioconvert"), ("possibly a liboil version mismatch?"));
1092     gst_object_unref (element);
1093     return NULL;
1094   }
1095
1096 no_audioresample:
1097   {
1098     post_missing_element_message (play_bin, "audioresample");
1099     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1100         (_("Missing element '%s' - check your GStreamer installation."),
1101             "audioresample"), ("possibly a liboil version mismatch?"));
1102     gst_object_unref (element);
1103     return NULL;
1104   }
1105 link_failed:
1106   {
1107     GST_ELEMENT_ERROR (play_bin, CORE, PAD,
1108         (NULL), ("Failed to configure the audio sink."));
1109     gst_object_unref (element);
1110     return NULL;
1111   }
1112 }
1113
1114 /* make the element (bin) that contains the elements needed to perform
1115  * visualisation ouput.  The idea is to split the audio using tee, then
1116  * sending the output to the regular audio bin and the other output to
1117  * the vis plugin that transforms it into a video that is rendered with the
1118  * normal video bin. The video and audio bins are run in threads to make sure
1119  * they don't block eachother.
1120  *
1121  *  +-----------------------------------------------------------------------+
1122  *  | visbin                                                                |
1123  *  |      +------+   +--------+   +----------------+                       |
1124  *  |      | tee  |   | aqueue |   |   abin ...     |                       |
1125  *  |   +-sink   src-sink     src-sink              |                       |
1126  *  |   |  |      |   +--------+   +----------------+                       |
1127  *  |   |  |      |                                                         |
1128  *  |   |  |      |   +------+   +------------+   +------+   +-----------+  |
1129  *  |   |  |      |   |vqueue|   | audioconv  |   | vis  |   | vbin ...  |  |
1130  *  |   |  |     src-sink   src-sink + samp  src-sink   src-sink         |  |
1131  *  |   |  |      |   +------+   +------------+   +------+   +-----------+  |
1132  *  |   |  |      |                                                         |
1133  *  |   |  +------+                                                         |
1134  * sink-+                                                                   |
1135  *  +-----------------------------------------------------------------------+
1136  */
1137 static GstElement *
1138 gen_vis_element (GstPlayBin * play_bin)
1139 {
1140   gboolean res;
1141   GstElement *element;
1142   GstElement *tee;
1143   GstElement *asink;
1144   GstElement *vsink;
1145   GstElement *conv;
1146   GstElement *resamp;
1147   GstElement *conv2;
1148   GstElement *vis;
1149   GstElement *vqueue, *aqueue;
1150   GstPad *pad, *rpad;
1151
1152   /* errors are already posted when these fail. */
1153   asink = gen_audio_element (play_bin);
1154   if (!asink)
1155     return NULL;
1156   vsink = gen_video_element (play_bin);
1157   if (!vsink) {
1158     gst_object_unref (asink);
1159     return NULL;
1160   }
1161
1162   element = gst_bin_new ("visbin");
1163   tee = gst_element_factory_make ("tee", "tee");
1164
1165   vqueue = gst_element_factory_make ("queue", "vqueue");
1166   aqueue = gst_element_factory_make ("queue", "aqueue");
1167
1168   gst_bin_add (GST_BIN_CAST (element), asink);
1169   gst_bin_add (GST_BIN_CAST (element), vqueue);
1170   gst_bin_add (GST_BIN_CAST (element), aqueue);
1171   gst_bin_add (GST_BIN_CAST (element), vsink);
1172   gst_bin_add (GST_BIN_CAST (element), tee);
1173
1174   conv = gst_element_factory_make ("audioconvert", "aconv");
1175   if (conv == NULL)
1176     goto no_audioconvert;
1177   gst_bin_add (GST_BIN_CAST (element), conv);
1178
1179   resamp = gst_element_factory_make ("audioresample", "aresamp");
1180   if (resamp == NULL)
1181     goto no_audioresample;
1182   gst_bin_add (GST_BIN_CAST (element), resamp);
1183
1184   conv2 = gst_element_factory_make ("audioconvert", "aconv2");
1185   if (conv2 == NULL)
1186     goto no_audioconvert;
1187   gst_bin_add (GST_BIN_CAST (element), conv2);
1188
1189   if (play_bin->visualisation) {
1190     gst_object_ref (play_bin->visualisation);
1191     vis = play_bin->visualisation;
1192   } else {
1193     vis = gst_element_factory_make ("goom", "vis");
1194     if (!vis)
1195       goto no_goom;
1196   }
1197   gst_bin_add (GST_BIN_CAST (element), vis);
1198
1199   res = gst_element_link_pads (vqueue, "src", conv, "sink");
1200   res &= gst_element_link_pads (conv, "src", resamp, "sink");
1201   res &= gst_element_link_pads (resamp, "src", conv2, "sink");
1202   res &= gst_element_link_pads (conv2, "src", vis, "sink");
1203   res &= gst_element_link_pads (vis, "src", vsink, "sink");
1204   if (!res)
1205     goto link_failed;
1206
1207   pad = gst_element_get_pad (aqueue, "sink");
1208   rpad = gst_element_get_request_pad (tee, "src%d");
1209   gst_pad_link (rpad, pad);
1210   gst_object_unref (rpad);
1211   gst_object_unref (pad);
1212   gst_element_link_pads (aqueue, "src", asink, "sink");
1213
1214   pad = gst_element_get_pad (vqueue, "sink");
1215   rpad = gst_element_get_request_pad (tee, "src%d");
1216   gst_pad_link (rpad, pad);
1217   gst_object_unref (rpad);
1218   gst_object_unref (pad);
1219
1220   pad = gst_element_get_pad (tee, "sink");
1221   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
1222   gst_object_unref (pad);
1223
1224   return element;
1225
1226   /* ERRORS */
1227 no_audioconvert:
1228   {
1229     post_missing_element_message (play_bin, "audioconvert");
1230     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1231         (_("Missing element '%s' - check your GStreamer installation."),
1232             "audioconvert"), ("possibly a liboil version mismatch?"));
1233     gst_object_unref (element);
1234     return NULL;
1235   }
1236 no_audioresample:
1237   {
1238     post_missing_element_message (play_bin, "audioresample");
1239     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1240         (_("Missing element '%s' - check your GStreamer installation."),
1241             "audioresample"), (NULL));
1242     gst_object_unref (element);
1243     return NULL;
1244   }
1245 no_goom:
1246   {
1247     post_missing_element_message (play_bin, "goom");
1248     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1249         (_("Missing element '%s' - check your GStreamer installation."),
1250             "goom"), (NULL));
1251     gst_object_unref (element);
1252     return NULL;
1253   }
1254 link_failed:
1255   {
1256     GST_ELEMENT_ERROR (play_bin, CORE, PAD,
1257         (NULL), ("Failed to configure the visualisation element."));
1258     gst_object_unref (element);
1259     return NULL;
1260   }
1261 }
1262
1263 /* get rid of all installed sinks */
1264 static void
1265 remove_sinks (GstPlayBin * play_bin)
1266 {
1267   GList *sinks;
1268   GstObject *parent;
1269   GstElement *element;
1270   GstPad *pad, *peer;
1271
1272   if (play_bin->cache == NULL)
1273     return;
1274
1275   GST_DEBUG ("removesinks");
1276   element = g_hash_table_lookup (play_bin->cache, "abin");
1277   if (element != NULL) {
1278     parent = gst_element_get_parent (element);
1279     if (parent != NULL) {
1280       /* we remove the element from the parent so that
1281        * there is no unwanted state change when the parent
1282        * is disposed */
1283       play_bin->sinks = g_list_remove (play_bin->sinks, element);
1284       gst_element_set_state (element, GST_STATE_NULL);
1285       gst_bin_remove (GST_BIN_CAST (parent), element);
1286       gst_object_unref (parent);
1287     }
1288     pad = gst_element_get_pad (element, "sink");
1289     if (pad != NULL) {
1290       peer = gst_pad_get_peer (pad);
1291       if (peer != NULL) {
1292         gst_pad_unlink (peer, pad);
1293         gst_object_unref (peer);
1294       }
1295       gst_object_unref (pad);
1296     }
1297   }
1298   element = g_hash_table_lookup (play_bin->cache, "vbin");
1299   if (element != NULL) {
1300     parent = gst_element_get_parent (element);
1301     if (parent != NULL) {
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
1318   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
1319     GstElement *element = GST_ELEMENT_CAST (sinks->data);
1320     GstPad *pad;
1321     GstPad *peer;
1322
1323     pad = gst_element_get_pad (element, "sink");
1324
1325     GST_LOG ("removing sink %p", element);
1326
1327     peer = gst_pad_get_peer (pad);
1328     if (peer) {
1329       gst_pad_unlink (peer, pad);
1330       gst_object_unref (peer);
1331     }
1332     gst_object_unref (pad);
1333
1334     gst_element_set_state (element, GST_STATE_NULL);
1335     gst_bin_remove (GST_BIN_CAST (play_bin), element);
1336   }
1337   g_list_free (play_bin->sinks);
1338   play_bin->sinks = NULL;
1339
1340   if (play_bin->visualisation) {
1341     GstElement *vis_bin;
1342
1343     vis_bin =
1344         GST_ELEMENT_CAST (gst_element_get_parent (play_bin->visualisation));
1345
1346     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
1347
1348     if (vis_bin) {
1349       gst_bin_remove (GST_BIN_CAST (vis_bin), play_bin->visualisation);
1350       gst_object_unref (vis_bin);
1351     }
1352   }
1353
1354   if (play_bin->frame) {
1355     gst_buffer_unref (play_bin->frame);
1356     play_bin->frame = NULL;
1357   }
1358
1359   if (play_bin->textoverlay_element) {
1360     gst_object_unref (play_bin->textoverlay_element);
1361     play_bin->textoverlay_element = NULL;
1362   }
1363 }
1364
1365 /* loop over the streams and set up the pipeline to play this
1366  * media file. First we count the number of audio and video streams.
1367  * If there is no video stream but there exists an audio stream,
1368  * we install a visualisation pipeline.
1369  *
1370  * Also make sure to only connect the first audio and video pad. FIXME
1371  * this should eventually be handled with a tuner interface so that
1372  * one can switch the streams.
1373  *
1374  * This function takes ownership of @sink.*
1375  */
1376 static gboolean
1377 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
1378     GstPad * subtitle_pad)
1379 {
1380   GstPad *sinkpad;
1381   GstPadLinkReturn linkres;
1382   GstElement *parent;
1383   GstStateChangeReturn stateret;
1384   GstState state;
1385
1386   g_return_val_if_fail (sink != NULL, FALSE);
1387
1388   state = GST_STATE_PAUSED;
1389
1390   /* this is only for debugging */
1391   parent = gst_pad_get_parent_element (srcpad);
1392   if (parent) {
1393     GST_DEBUG ("Adding sink %" GST_PTR_FORMAT
1394         " with state %d (parent: %d, peer: %d)", sink,
1395         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
1396     gst_object_unref (parent);
1397   }
1398   gst_bin_add (GST_BIN_CAST (play_bin), sink);
1399
1400   /* bring it to the required state so we can link to the peer without
1401    * breaking the flow */
1402   stateret = gst_element_set_state (sink, state);
1403   if (stateret == GST_STATE_CHANGE_FAILURE)
1404     goto state_failed;
1405
1406   /* we found a sink for this stream, now try to install it */
1407   sinkpad = gst_element_get_pad (sink, "sink");
1408   linkres = gst_pad_link (srcpad, sinkpad);
1409   gst_object_unref (sinkpad);
1410
1411   /* try to link the pad of the sink to the stream */
1412   if (GST_PAD_LINK_FAILED (linkres))
1413     goto link_failed;
1414
1415   if (GST_IS_PAD (subtitle_pad)) {
1416     sinkpad = gst_element_get_pad (sink, "text_sink");
1417     linkres = gst_pad_link (subtitle_pad, sinkpad);
1418     gst_object_unref (sinkpad);
1419   }
1420
1421   /* try to link the subtitle pad of the sink to the stream, this is not
1422    * fatal. */
1423   if (GST_PAD_LINK_FAILED (linkres))
1424     goto subtitle_failed;
1425
1426 done:
1427   /* we got the sink succesfully linked, now keep the sink
1428    * in our internal list */
1429   play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1430
1431   return TRUE;
1432
1433   /* ERRORS */
1434 state_failed:
1435   {
1436     gst_element_set_state (sink, GST_STATE_NULL);
1437     gst_bin_remove (GST_BIN_CAST (play_bin), sink);
1438     GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1439     return FALSE;
1440   }
1441 link_failed:
1442   {
1443     gchar *capsstr;
1444     GstCaps *caps;
1445
1446     /* could not link this stream */
1447     caps = gst_pad_get_caps (srcpad);
1448     capsstr = gst_caps_to_string (caps);
1449     g_warning ("could not link %s: %d", capsstr, linkres);
1450     GST_DEBUG_OBJECT (play_bin,
1451         "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1452     g_free (capsstr);
1453     gst_caps_unref (caps);
1454
1455     gst_element_set_state (sink, GST_STATE_NULL);
1456     gst_bin_remove (GST_BIN_CAST (play_bin), sink);
1457     return FALSE;
1458   }
1459 subtitle_failed:
1460   {
1461     GstCaps *caps;
1462
1463     /* could not link this stream */
1464     caps = gst_pad_get_caps (subtitle_pad);
1465     GST_WARNING_OBJECT (play_bin, "subtitle link failed when adding sink, "
1466         "caps = %" GST_PTR_FORMAT ", reason %d", caps, linkres);
1467     gst_caps_unref (caps);
1468
1469     /* not fatal */
1470     goto done;
1471   }
1472 }
1473
1474 static void
1475 dummy_blocked_cb (GstPad * pad, gboolean blocked, gpointer user_data)
1476 {
1477 }
1478
1479 static gboolean
1480 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1481 {
1482   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1483   GList *streaminfo = NULL, *s;
1484   gboolean need_vis = FALSE;
1485   gboolean need_text = FALSE;
1486   GstPad *textsrcpad = NULL, *pad = NULL, *origtextsrcpad = NULL;
1487   GstElement *sink;
1488   gboolean res = TRUE;
1489
1490   /* get rid of existing sinks */
1491   if (play_bin->sinks) {
1492     remove_sinks (play_bin);
1493   }
1494   GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1495
1496   /* find out what to do */
1497   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1498       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1499     need_text = TRUE;
1500   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1501       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1502       play_bin->visualisation != NULL) {
1503     need_vis = TRUE;
1504   }
1505
1506   /* now actually connect everything */
1507   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1508   for (s = streaminfo; s; s = g_list_next (s)) {
1509     GObject *obj = G_OBJECT (s->data);
1510     gint type;
1511     GstObject *object;
1512
1513     g_object_get (obj, "type", &type, NULL);
1514     g_object_get (obj, "object", &object, NULL);
1515   }
1516
1517   /* link audio */
1518   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1519     if (need_vis) {
1520       sink = gen_vis_element (play_bin);
1521     } else {
1522       sink = gen_audio_element (play_bin);
1523     }
1524     if (!sink)
1525       return FALSE;
1526
1527     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1528         "src");
1529     res = add_sink (play_bin, sink, pad, NULL);
1530     gst_object_unref (pad);
1531   }
1532
1533   /* link video */
1534   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1535     if (need_text) {
1536       GstObject *parent = NULL, *grandparent = NULL;
1537       GstPad *ghost = NULL;
1538
1539       sink = gen_text_element (play_bin);
1540       textsrcpad =
1541           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1542           "src");
1543
1544       /* This pad is from subtitle-bin, we need to create a ghost pad to have
1545          common grandparents */
1546       parent = gst_object_get_parent (GST_OBJECT_CAST (textsrcpad));
1547       if (!parent) {
1548         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1549         gst_object_unref (textsrcpad);
1550         textsrcpad = NULL;
1551         goto beach;
1552       }
1553
1554       grandparent = gst_object_get_parent (parent);
1555       if (!grandparent) {
1556         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1557         gst_object_unref (parent);
1558         gst_object_unref (textsrcpad);
1559         textsrcpad = NULL;
1560         goto beach;
1561       }
1562
1563       /* We ghost the pad on subtitle_bin only, if the text pad is from the
1564          media demuxer we keep it as it is */
1565       if (!GST_IS_PLAY_BIN (grandparent)) {
1566         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1567             "file, ghosting to a suitable hierarchy");
1568         /* Block the pad first, because as soon as we add a ghostpad, the queue
1569          * will try and start pushing */
1570         gst_pad_set_blocked_async (textsrcpad, TRUE, dummy_blocked_cb, NULL);
1571         origtextsrcpad = gst_object_ref (textsrcpad);
1572
1573         ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1574         if (!GST_IS_PAD (ghost)) {
1575           GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1576               "subtitle-bin");
1577           gst_object_unref (parent);
1578           gst_object_unref (grandparent);
1579           gst_object_unref (textsrcpad);
1580           textsrcpad = NULL;
1581           goto beach;
1582         }
1583
1584         gst_pad_set_active (ghost, TRUE);
1585         if (gst_element_add_pad (GST_ELEMENT_CAST (grandparent), ghost)) {
1586           gst_object_unref (textsrcpad);
1587           textsrcpad = gst_object_ref (ghost);
1588         } else {
1589           GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1590           gst_pad_set_active (ghost, FALSE);
1591           gst_object_unref (ghost);
1592           gst_object_unref (textsrcpad);
1593           textsrcpad = NULL;
1594         }
1595       } else {
1596         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1597             "no changes to hierarchy needed");
1598       }
1599
1600       gst_object_unref (parent);
1601       gst_object_unref (grandparent);
1602     } else {
1603       sink = gen_video_element (play_bin);
1604     }
1605   beach:
1606     if (!sink)
1607       return FALSE;
1608     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1609         "src");
1610     res = add_sink (play_bin, sink, pad, textsrcpad);
1611     gst_object_unref (pad);
1612     if (textsrcpad)
1613       gst_object_unref (textsrcpad);
1614     if (origtextsrcpad) {
1615       gst_pad_set_blocked_async (origtextsrcpad, FALSE, dummy_blocked_cb, NULL);
1616       gst_object_unref (origtextsrcpad);
1617     }
1618   }
1619
1620   /* remove the sinks now, pipeline get_state will now wait for the
1621    * sinks to preroll */
1622   if (play_bin->fakesink) {
1623     gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1624     gst_bin_remove (GST_BIN_CAST (play_bin), play_bin->fakesink);
1625     play_bin->fakesink = NULL;
1626   }
1627
1628   return res;
1629 }
1630
1631 static void
1632 playbin_set_subtitles_visible (GstPlayBaseBin * play_base_bin, gboolean visible)
1633 {
1634   GstPlayBin *playbin = GST_PLAY_BIN (play_base_bin);
1635
1636   /* we're ignoring the case of someone setting the 'current-text' property
1637    * before textoverlay is set up (which is probably okay, since playbasebin
1638    * will just select the first subtitle stream as active stream regardless) */
1639   if (playbin->textoverlay_element != NULL) {
1640     GST_LOG_OBJECT (playbin, "setting subtitle visibility to %d", visible);
1641     g_object_set (playbin->textoverlay_element, "silent", !visible, NULL);
1642   }
1643 }
1644
1645 /* Send an event to our sinks until one of them works; don't then send to the
1646  * remaining sinks (unlike GstBin)
1647  */
1648 static gboolean
1649 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1650 {
1651   GList *sinks = play_bin->sinks;
1652   gboolean res = TRUE;
1653
1654   while (sinks) {
1655     GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1656
1657     gst_event_ref (event);
1658     if ((res = gst_element_send_event (sink, event))) {
1659       GST_DEBUG_OBJECT (play_bin,
1660           "Sent event succesfully to sink %" GST_PTR_FORMAT, sink);
1661       break;
1662     }
1663     GST_DEBUG_OBJECT (play_bin,
1664         "Event failed when sent to sink %" GST_PTR_FORMAT, sink);
1665
1666     sinks = g_list_next (sinks);
1667   }
1668
1669   gst_event_unref (event);
1670
1671   return res;
1672 }
1673
1674 /* We only want to send the event to a single sink (overriding GstBin's
1675  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1676  * events appropriately. So, this is a messy duplication of code. */
1677 static gboolean
1678 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1679 {
1680   gboolean res = FALSE;
1681   GstEventType event_type = GST_EVENT_TYPE (event);
1682
1683   switch (event_type) {
1684     case GST_EVENT_SEEK:
1685       GST_DEBUG_OBJECT (element, "Sending seek event to a sink");
1686       res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1687       break;
1688     default:
1689       res = parent_class->send_event (element, event);
1690       break;
1691   }
1692
1693   return res;
1694 }
1695
1696 static void
1697 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
1698     GList * structure_list)
1699 {
1700   GList *l;
1701
1702   for (l = structure_list; l != NULL; l = l->next) {
1703     GValue val = { 0, };
1704
1705     if (*first == NULL)
1706       *first = gst_structure_copy ((GstStructure *) l->data);
1707
1708     g_value_init (&val, GST_TYPE_STRUCTURE);
1709     g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
1710     gst_value_list_append_value (list_val, &val);
1711     g_value_unset (&val);
1712   }
1713 }
1714
1715 /* if it's a redirect message with multiple redirect locations we might
1716  * want to pick a different 'best' location depending on the required
1717  * bitrates and the connection speed */
1718 static GstMessage *
1719 gst_play_bin_handle_redirect_message (GstPlayBin * playbin, GstMessage * msg)
1720 {
1721   const GValue *locations_list, *location_val;
1722   GstMessage *new_msg;
1723   GstStructure *new_structure = NULL;
1724   GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
1725   GValue new_list = { 0, };
1726   guint size, i;
1727   GstPlayBaseBin *playbasebin = GST_PLAY_BASE_BIN (playbin);
1728   guint connection_speed = playbasebin->connection_speed;
1729
1730   GST_DEBUG_OBJECT (playbin, "redirect message: %" GST_PTR_FORMAT, msg);
1731   GST_DEBUG_OBJECT (playbin, "connection speed: %u", connection_speed);
1732
1733   if (connection_speed == 0 || msg->structure == NULL)
1734     return msg;
1735
1736   locations_list = gst_structure_get_value (msg->structure, "locations");
1737   if (locations_list == NULL)
1738     return msg;
1739
1740   size = gst_value_list_get_size (locations_list);
1741   if (size < 2)
1742     return msg;
1743
1744   /* maintain existing order as much as possible, just sort references
1745    * with too high a bitrate to the end (the assumption being that if
1746    * bitrates are given they are given for all interesting streams and
1747    * that the you-need-at-least-version-xyz redirect has the same bitrate
1748    * as the lowest referenced redirect alternative) */
1749   for (i = 0; i < size; ++i) {
1750     const GstStructure *s;
1751     gint bitrate = 0;
1752
1753     location_val = gst_value_list_get_value (locations_list, i);
1754     s = (const GstStructure *) g_value_get_boxed (location_val);
1755     if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
1756       GST_DEBUG_OBJECT (playbin, "no bitrate: %" GST_PTR_FORMAT, s);
1757       l_neutral = g_list_append (l_neutral, (gpointer) s);
1758     } else if (bitrate > connection_speed) {
1759       GST_DEBUG_OBJECT (playbin, "bitrate too high: %" GST_PTR_FORMAT, s);
1760       l_bad = g_list_append (l_bad, (gpointer) s);
1761     } else if (bitrate <= connection_speed) {
1762       GST_DEBUG_OBJECT (playbin, "bitrate OK: %" GST_PTR_FORMAT, s);
1763       l_good = g_list_append (l_good, (gpointer) s);
1764     }
1765   }
1766
1767   g_value_init (&new_list, GST_TYPE_LIST);
1768   value_list_append_structure_list (&new_list, &new_structure, l_good);
1769   value_list_append_structure_list (&new_list, &new_structure, l_neutral);
1770   value_list_append_structure_list (&new_list, &new_structure, l_bad);
1771   gst_structure_set_value (new_structure, "locations", &new_list);
1772   g_value_unset (&new_list);
1773
1774   g_list_free (l_good);
1775   g_list_free (l_neutral);
1776   g_list_free (l_bad);
1777
1778   new_msg = gst_message_new_element (msg->src, new_structure);
1779   gst_message_unref (msg);
1780
1781   GST_DEBUG_OBJECT (playbin, "new redirect message: %" GST_PTR_FORMAT, new_msg);
1782   return new_msg;
1783 }
1784
1785 static void
1786 gst_play_bin_handle_message (GstBin * bin, GstMessage * msg)
1787 {
1788   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT && msg->structure != NULL
1789       && gst_structure_has_name (msg->structure, "redirect")) {
1790     msg = gst_play_bin_handle_redirect_message (GST_PLAY_BIN (bin), msg);
1791   }
1792
1793   GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
1794 }
1795
1796 static GstStateChangeReturn
1797 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1798 {
1799   GstStateChangeReturn ret;
1800   GstPlayBin *play_bin;
1801
1802   play_bin = GST_PLAY_BIN (element);
1803
1804
1805   switch (transition) {
1806     case GST_STATE_CHANGE_READY_TO_PAUSED:
1807       /* this really is the easiest way to make the state change return
1808        * ASYNC until we added the sinks */
1809       if (!play_bin->fakesink) {
1810         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1811         gst_bin_add (GST_BIN_CAST (play_bin), play_bin->fakesink);
1812       }
1813       break;
1814     default:
1815       break;
1816   }
1817
1818   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1819   if (ret == GST_STATE_CHANGE_FAILURE)
1820     return ret;
1821
1822   switch (transition) {
1823     case GST_STATE_CHANGE_READY_TO_PAUSED:
1824       /* remember us being a live pipeline */
1825       play_bin->is_live = (ret == GST_STATE_CHANGE_NO_PREROLL);
1826       GST_DEBUG_OBJECT (play_bin, "is live: %d", play_bin->is_live);
1827       break;
1828     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1829       /* FIXME Release audio device when we implement that */
1830       break;
1831     case GST_STATE_CHANGE_PAUSED_TO_READY:
1832     case GST_STATE_CHANGE_READY_TO_NULL:
1833       /* remove sinks we added */
1834       remove_sinks (play_bin);
1835       /* and there might be a fakesink we need to clean up now */
1836       if (play_bin->fakesink) {
1837         gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1838         gst_bin_remove (GST_BIN_CAST (play_bin), play_bin->fakesink);
1839         play_bin->fakesink = NULL;
1840       }
1841       break;
1842     default:
1843       break;
1844   }
1845
1846   return ret;
1847 }
1848
1849 gboolean
1850 gst_play_bin_plugin_init (GstPlugin * plugin)
1851 {
1852   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1853
1854   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1855       GST_TYPE_PLAY_BIN);
1856 }