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