gst/playback/gstplaybin.c: Make playbin automatically plug an 'audioresample' element...
[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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <gst/gst.h>
26
27 #include <gst/gst-i18n-plugin.h>
28
29 #include "gstplaybasebin.h"
30
31 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_debug);
32 #define GST_CAT_DEFAULT gst_play_bin_debug
33
34 #define GST_TYPE_PLAY_BIN               (gst_play_bin_get_type())
35 #define GST_PLAY_BIN(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PLAY_BIN,GstPlayBin))
36 #define GST_PLAY_BIN_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PLAY_BIN,GstPlayBinClass))
37 #define GST_IS_PLAY_BIN(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PLAY_BIN))
38 #define GST_IS_PLAY_BIN_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PLAY_BIN))
39
40 #define VOLUME_MAX_DOUBLE 4.0
41
42 #ifndef GST_HAVE_GLIB_2_8
43 #define _gst_gvalue_set_gstobject(gvalue,obj)  \
44       if (obj != NULL) {                       \
45         gst_object_ref (obj);                  \
46         g_value_set_object (gvalue, obj);      \
47         g_object_unref (obj);                  \
48       } else {                                 \
49         g_value_set_object (gvalue, NULL);     \
50       }
51 #else
52 #define _gst_gvalue_set_gstobject(gvalue,obj)  \
53       g_value_set_object (gvalue, obj);
54 #endif
55
56 typedef struct _GstPlayBin GstPlayBin;
57 typedef struct _GstPlayBinClass GstPlayBinClass;
58
59 struct _GstPlayBin
60 {
61   GstPlayBaseBin parent;
62
63   /* the configurable elements */
64   GstElement *fakesink;
65   GstElement *audio_sink;
66   GstElement *video_sink;
67   GstElement *visualisation;
68   GstElement *pending_visualisation;
69   GstElement *volume_element;
70   GstElement *textoverlay_element;
71   gfloat volume;
72
73   /* these are the currently active sinks */
74   GList *sinks;
75
76   /* the last captured frame for snapshots */
77   GstBuffer *frame;
78
79   /* our cache for the sinks */
80   GHashTable *cache;
81
82   /* font description */
83   gchar *font_desc;
84 };
85
86 struct _GstPlayBinClass
87 {
88   GstPlayBaseBinClass parent_class;
89 };
90
91 /* props */
92 enum
93 {
94   ARG_0,
95   ARG_AUDIO_SINK,
96   ARG_VIDEO_SINK,
97   ARG_VIS_PLUGIN,
98   ARG_VOLUME,
99   ARG_FRAME,
100   ARG_FONT_DESC
101 };
102
103 /* signals */
104 enum
105 {
106   LAST_SIGNAL
107 };
108
109 static void gst_play_bin_class_init (GstPlayBinClass * klass);
110 static void gst_play_bin_init (GstPlayBin * play_bin);
111 static void gst_play_bin_dispose (GObject * object);
112
113 static gboolean setup_sinks (GstPlayBaseBin * play_base_bin,
114     GstPlayBaseGroup * group);
115 static void remove_sinks (GstPlayBin * play_bin);
116
117 static void gst_play_bin_set_property (GObject * object, guint prop_id,
118     const GValue * value, GParamSpec * spec);
119 static void gst_play_bin_get_property (GObject * object, guint prop_id,
120     GValue * value, GParamSpec * spec);
121
122 static gboolean gst_play_bin_send_event (GstElement * element,
123     GstEvent * event);
124 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
125     GstStateChange transition);
126
127 static GstElementClass *parent_class;
128
129 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
130
131 static const GstElementDetails gst_play_bin_details =
132 GST_ELEMENT_DETAILS ("Player Bin",
133     "Generic/Bin/Player",
134     "Autoplug and play media from an uri",
135     "Wim Taymans <wim@fluendo.com>");
136
137 static GType
138 gst_play_bin_get_type (void)
139 {
140   static GType gst_play_bin_type = 0;
141
142   if (!gst_play_bin_type) {
143     static const GTypeInfo gst_play_bin_info = {
144       sizeof (GstPlayBinClass),
145       NULL,
146       NULL,
147       (GClassInitFunc) gst_play_bin_class_init,
148       NULL,
149       NULL,
150       sizeof (GstPlayBin),
151       0,
152       (GInstanceInitFunc) gst_play_bin_init,
153       NULL
154     };
155
156     gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
157         "GstPlayBin", &gst_play_bin_info, 0);
158   }
159
160   return gst_play_bin_type;
161 }
162
163 static void
164 gst_play_bin_class_init (GstPlayBinClass * klass)
165 {
166   GObjectClass *gobject_klass;
167   GstElementClass *gstelement_klass;
168   GstBinClass *gstbin_klass;
169   GstPlayBaseBinClass *playbasebin_klass;
170
171   gobject_klass = (GObjectClass *) klass;
172   gstelement_klass = (GstElementClass *) klass;
173   gstbin_klass = (GstBinClass *) klass;
174   playbasebin_klass = (GstPlayBaseBinClass *) klass;
175
176   parent_class = g_type_class_peek_parent (klass);
177
178   gobject_klass->set_property = gst_play_bin_set_property;
179   gobject_klass->get_property = gst_play_bin_get_property;
180
181   g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
182       g_param_spec_object ("video-sink", "Video Sink",
183           "the video output element to use (NULL = default sink)",
184           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
185   g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
186       g_param_spec_object ("audio-sink", "Audio Sink",
187           "the audio output element to use (NULL = default sink)",
188           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
189   g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
190       g_param_spec_object ("vis-plugin", "Vis plugin",
191           "the visualization element to use (NULL = none)",
192           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
193   g_object_class_install_property (gobject_klass, ARG_VOLUME,
194       g_param_spec_double ("volume", "volume", "volume",
195           0.0, VOLUME_MAX_DOUBLE, 1.0, G_PARAM_READWRITE));
196   g_object_class_install_property (gobject_klass, ARG_FRAME,
197       gst_param_spec_mini_object ("frame", "Frame",
198           "The last frame (NULL = no video available)",
199           GST_TYPE_BUFFER, G_PARAM_READABLE));
200   g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
201       g_param_spec_string ("subtitle-font-desc",
202           "Subtitle font description",
203           "Pango font description of font "
204           "to be used for subtitle rendering", NULL, G_PARAM_WRITABLE));
205
206   gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_dispose);
207
208   gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
209
210   gstelement_klass->change_state =
211       GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
212   gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
213
214   playbasebin_klass->setup_output_pads = setup_sinks;
215 }
216
217 static void
218 gst_play_bin_init (GstPlayBin * play_bin)
219 {
220   play_bin->video_sink = NULL;
221   play_bin->audio_sink = NULL;
222   play_bin->visualisation = NULL;
223   play_bin->pending_visualisation = NULL;
224   play_bin->volume_element = NULL;
225   play_bin->textoverlay_element = NULL;
226   play_bin->volume = 1.0;
227   play_bin->sinks = NULL;
228   play_bin->frame = NULL;
229   play_bin->font_desc = NULL;
230   play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
231       NULL, (GDestroyNotify) gst_object_unref);
232 }
233
234 static void
235 gst_play_bin_dispose (GObject * object)
236 {
237   GstPlayBin *play_bin;
238
239   play_bin = GST_PLAY_BIN (object);
240
241   if (play_bin->cache != NULL) {
242     remove_sinks (play_bin);
243     g_hash_table_destroy (play_bin->cache);
244     play_bin->cache = NULL;
245   }
246
247   if (play_bin->audio_sink != NULL) {
248     gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
249     gst_object_unref (play_bin->audio_sink);
250     play_bin->audio_sink = NULL;
251   }
252   if (play_bin->video_sink != NULL) {
253     gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
254     gst_object_unref (play_bin->video_sink);
255     play_bin->video_sink = NULL;
256   }
257   if (play_bin->visualisation != NULL) {
258     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
259     gst_object_unref (play_bin->visualisation);
260     play_bin->visualisation = NULL;
261   }
262   if (play_bin->pending_visualisation != NULL) {
263     gst_element_set_state (play_bin->pending_visualisation, GST_STATE_NULL);
264     gst_object_unref (play_bin->pending_visualisation);
265     play_bin->pending_visualisation = NULL;
266   }
267   if (play_bin->textoverlay_element != NULL) {
268     gst_object_unref (play_bin->textoverlay_element);
269     play_bin->textoverlay_element = NULL;
270   }
271   g_free (play_bin->font_desc);
272   play_bin->font_desc = NULL;
273
274   G_OBJECT_CLASS (parent_class)->dispose (object);
275 }
276
277 static void
278 gst_play_bin_vis_unblocked (GstPad * tee_pad, gboolean blocked,
279     gpointer user_data)
280 {
281   /* Unblocked */
282 }
283
284 static void
285 gst_play_bin_vis_blocked (GstPad * tee_pad, gboolean blocked,
286     gpointer user_data)
287 {
288   GstPlayBin *play_bin = GST_PLAY_BIN (user_data);
289   GstBin *vis_bin = NULL;
290   GstPad *vis_sink_pad = NULL, *vis_src_pad = NULL, *vqueue_pad = NULL;
291   GstState bin_state;
292
293   /* We want to disable visualisation */
294   if (!GST_IS_ELEMENT (play_bin->pending_visualisation)) {
295     /* Set visualisation element to READY */
296     gst_element_set_state (play_bin->visualisation, GST_STATE_READY);
297     goto beach;
298   }
299
300   vis_bin =
301       GST_BIN (gst_object_get_parent (GST_OBJECT (play_bin->visualisation)));
302
303   if (!GST_IS_BIN (vis_bin) || !GST_IS_PAD (tee_pad)) {
304     goto beach;
305   }
306
307   vis_src_pad = gst_element_get_pad (play_bin->visualisation, "src");
308   vis_sink_pad = gst_pad_get_peer (tee_pad);
309
310   /* Can be fakesink */
311   if (GST_IS_PAD (vis_src_pad)) {
312     vqueue_pad = gst_pad_get_peer (vis_src_pad);
313   }
314
315   if (!GST_IS_PAD (vis_sink_pad)) {
316     goto beach;
317   }
318
319   /* Check the bin's state */
320   GST_OBJECT_LOCK (vis_bin);
321   bin_state = GST_STATE (vis_bin);
322   GST_OBJECT_UNLOCK (vis_bin);
323
324   /* Unlink */
325   gst_pad_unlink (tee_pad, vis_sink_pad);
326   gst_object_unref (vis_sink_pad);
327   vis_sink_pad = NULL;
328
329   if (GST_IS_PAD (vqueue_pad)) {
330     gst_pad_unlink (vis_src_pad, vqueue_pad);
331     gst_object_unref (vis_src_pad);
332     vis_src_pad = NULL;
333   }
334
335   /* Remove from vis_bin */
336   gst_bin_remove (vis_bin, play_bin->visualisation);
337   /* Set state to NULL */
338   gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
339   /* And loose our ref */
340   gst_object_unref (play_bin->visualisation);
341
342   if (play_bin->pending_visualisation) {
343     /* Ref this new visualisation element before adding to the bin */
344     gst_object_ref (play_bin->pending_visualisation);
345     /* Add the new one */
346     gst_bin_add (vis_bin, play_bin->pending_visualisation);
347     /* Synchronizing state */
348     gst_element_set_state (play_bin->pending_visualisation, bin_state);
349
350     vis_sink_pad = gst_element_get_pad (play_bin->pending_visualisation,
351         "sink");
352     vis_src_pad = gst_element_get_pad (play_bin->pending_visualisation, "src");
353
354     if (!GST_IS_PAD (vis_sink_pad) || !GST_IS_PAD (vis_src_pad)) {
355       goto beach;
356     }
357
358     /* Link */
359     gst_pad_link (tee_pad, vis_sink_pad);
360     gst_pad_link (vis_src_pad, vqueue_pad);
361   }
362
363   /* We are done */
364   gst_object_unref (play_bin->visualisation);
365   play_bin->visualisation = play_bin->pending_visualisation;
366   play_bin->pending_visualisation = NULL;
367
368 beach:
369   if (vis_sink_pad) {
370     gst_object_unref (vis_sink_pad);
371   }
372   if (vis_src_pad) {
373     gst_object_unref (vis_src_pad);
374   }
375   if (vqueue_pad) {
376     gst_object_unref (vqueue_pad);
377   }
378   if (vis_bin) {
379     gst_object_unref (vis_bin);
380   }
381
382   /* Unblock the pad */
383   gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_bin_vis_unblocked,
384       play_bin);
385 }
386
387 static void
388 gst_play_bin_set_property (GObject * object, guint prop_id,
389     const GValue * value, GParamSpec * pspec)
390 {
391   GstPlayBin *play_bin;
392
393   g_return_if_fail (GST_IS_PLAY_BIN (object));
394
395   play_bin = GST_PLAY_BIN (object);
396
397   switch (prop_id) {
398     case ARG_VIDEO_SINK:
399       if (play_bin->video_sink != NULL) {
400         gst_object_unref (play_bin->video_sink);
401       }
402       play_bin->video_sink = g_value_get_object (value);
403       if (play_bin->video_sink != NULL) {
404         gst_object_ref (play_bin->video_sink);
405         gst_object_sink (GST_OBJECT (play_bin->video_sink));
406       }
407       /* when changing the videosink, we just remove the
408        * video pipeline from the cache so that it will be 
409        * regenerated with the new sink element */
410       g_hash_table_remove (play_bin->cache, "vbin");
411       break;
412     case ARG_AUDIO_SINK:
413       if (play_bin->audio_sink != NULL) {
414         gst_object_unref (play_bin->audio_sink);
415       }
416       play_bin->audio_sink = g_value_get_object (value);
417       if (play_bin->audio_sink != NULL) {
418         gst_object_ref (play_bin->audio_sink);
419         gst_object_sink (GST_OBJECT (play_bin->audio_sink));
420       }
421       g_hash_table_remove (play_bin->cache, "abin");
422       break;
423     case ARG_VIS_PLUGIN:
424     {
425       /* Do we already have a visualisation change pending ? */
426       if (play_bin->pending_visualisation) {
427         gst_object_unref (play_bin->pending_visualisation);
428         play_bin->pending_visualisation = g_value_get_object (value);
429         /* Take ownership */
430         if (play_bin->pending_visualisation) {
431           gst_object_ref (play_bin->pending_visualisation);
432           gst_object_sink (GST_OBJECT (play_bin->pending_visualisation));
433         }
434       } else {
435         play_bin->pending_visualisation = g_value_get_object (value);
436
437         /* Take ownership */
438         if (play_bin->pending_visualisation) {
439           gst_object_ref (play_bin->pending_visualisation);
440           gst_object_sink (GST_OBJECT (play_bin->pending_visualisation));
441         }
442
443         /* Was there a visualisation already set ? */
444         if (play_bin->visualisation != NULL) {
445           GstBin *vis_bin = NULL;
446
447           vis_bin =
448               GST_BIN (gst_object_get_parent (GST_OBJECT (play_bin->
449                       visualisation)));
450
451           /* Check if the visualisation is already in a bin */
452           if (GST_IS_BIN (vis_bin)) {
453             GstPad *vis_sink_pad = NULL, *tee_pad = NULL;
454
455             /* Now get tee pad and block it async */
456             vis_sink_pad = gst_element_get_pad (play_bin->visualisation,
457                 "sink");
458             if (!GST_IS_PAD (vis_sink_pad)) {
459               goto beach;
460             }
461             tee_pad = gst_pad_get_peer (vis_sink_pad);
462             if (!GST_IS_PAD (tee_pad)) {
463               goto beach;
464             }
465
466             /* Block with callback */
467             gst_pad_set_blocked_async (tee_pad, TRUE, gst_play_bin_vis_blocked,
468                 play_bin);
469           beach:
470             if (vis_sink_pad) {
471               gst_object_unref (vis_sink_pad);
472             }
473             if (tee_pad) {
474               gst_object_unref (tee_pad);
475             }
476             gst_object_unref (vis_bin);
477           } else {
478             play_bin->visualisation = play_bin->pending_visualisation;
479             play_bin->pending_visualisation = NULL;
480           }
481         } else {
482           play_bin->visualisation = play_bin->pending_visualisation;
483           play_bin->pending_visualisation = NULL;
484         }
485       }
486       break;
487     }
488     case ARG_VOLUME:
489       play_bin->volume = g_value_get_double (value);
490       if (play_bin->volume_element) {
491         g_object_set (G_OBJECT (play_bin->volume_element), "volume",
492             play_bin->volume, NULL);
493       }
494       break;
495     case ARG_FONT_DESC:
496       g_free (play_bin->font_desc);
497       play_bin->font_desc = g_strdup (g_value_get_string (value));
498       if (play_bin->textoverlay_element) {
499         g_object_set (G_OBJECT (play_bin->textoverlay_element),
500             "font-desc", g_value_get_string (value), NULL);
501       }
502       break;
503     default:
504       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505       break;
506   }
507 }
508
509 static void
510 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
511     GParamSpec * pspec)
512 {
513   GstPlayBin *play_bin;
514
515   g_return_if_fail (GST_IS_PLAY_BIN (object));
516
517   play_bin = GST_PLAY_BIN (object);
518
519   switch (prop_id) {
520     case ARG_VIDEO_SINK:
521       _gst_gvalue_set_gstobject (value, play_bin->video_sink);
522       break;
523     case ARG_AUDIO_SINK:
524       _gst_gvalue_set_gstobject (value, play_bin->audio_sink);
525       break;
526     case ARG_VIS_PLUGIN:
527       _gst_gvalue_set_gstobject (value, play_bin->visualisation);
528       break;
529     case ARG_VOLUME:
530       g_value_set_double (value, play_bin->volume);
531       break;
532     case ARG_FRAME:
533       gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
534       break;
535     default:
536       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537       break;
538   }
539 }
540
541 /* signal fired when the identity has received a new buffer. This is used for
542  * making screenshots.
543  */
544 static void
545 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
546 {
547   GstPlayBin *play_bin = GST_PLAY_BIN (data);
548   GstBuffer **frame_p = &play_bin->frame;
549
550   gst_mini_object_replace ((GstMiniObject **) frame_p,
551       GST_MINI_OBJECT_CAST (frame));
552
553   /* applications need to know the buffer caps,
554    * make sure they are always set on the frame */
555   if (GST_BUFFER_CAPS (play_bin->frame) == NULL) {
556     GstPad *pad;
557
558     if ((pad = gst_element_get_pad (identity, "sink"))) {
559       gst_buffer_set_caps (play_bin->frame, GST_PAD_CAPS (pad));
560       gst_object_unref (pad);
561     }
562   }
563 }
564
565 /* make the element (bin) that contains the elements needed to perform
566  * video display. We connect a handoff signal to identity so that we
567  * can grab snapshots. Identity's sinkpad is ghosted to vbin.
568  *
569  *  +-------------------------------------------------------------+
570  *  | vbin                                                        |
571  *  |      +--------+   +----------+   +----------+   +---------+ |
572  *  |      |identity|   |colorspace|   |videoscale|   |videosink| |
573  *  |   +-sink     src-sink       src-sink       src-sink       | |
574  *  |   |  +---+----+   +----------+   +----------+   +---------+ |
575  * sink-+      |                                                  |
576  *  +----------|--------------------------------------------------+
577  *           handoff
578  */
579 /* FIXME: this might return NULL if no videosink was found, handle
580  * this in callers */
581 static GstElement *
582 gen_video_element (GstPlayBin * play_bin)
583 {
584   GstElement *element;
585   GstElement *conv;
586
587   GstElement *scale;
588   GstElement *sink;
589   GstElement *identity;
590   GstPad *pad;
591
592   /* first see if we have it in the cache */
593   element = g_hash_table_lookup (play_bin->cache, "vbin");
594   if (element != NULL) {
595     return element;
596   }
597
598   if (play_bin->video_sink) {
599     sink = play_bin->video_sink;
600   } else {
601     sink = gst_element_factory_make ("autovideosink", "videosink");
602     if (sink == NULL) {
603       sink = gst_element_factory_make ("xvimagesink", "videosink");
604     }
605     /* FIXME: this warrants adding a CORE error category for missing
606      * elements/plugins */
607     if (sink == NULL) {
608       GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
609           (_("Both autovideosink and xvimagesink elements are missing.")),
610           (NULL));
611       return NULL;
612     }
613   }
614   gst_object_ref (sink);
615   g_hash_table_insert (play_bin->cache, "video_sink", sink);
616
617
618   element = gst_bin_new ("vbin");
619   identity = gst_element_factory_make ("identity", "id");
620   g_object_set (identity, "silent", TRUE, NULL);
621   g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
622   gst_bin_add (GST_BIN (element), identity);
623   conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
624   if (conv == NULL)
625     goto no_colorspace;
626   scale = gst_element_factory_make ("videoscale", "vscale");
627   if (scale == NULL)
628     goto no_videoscale;
629   gst_bin_add (GST_BIN (element), conv);
630   gst_bin_add (GST_BIN (element), scale);
631   gst_bin_add (GST_BIN (element), sink);
632   gst_element_link_pads (identity, "src", conv, "sink");
633   gst_element_link_pads (conv, "src", scale, "sink");
634   gst_element_link_pads (scale, "src", sink, "sink");
635
636   pad = gst_element_get_pad (identity, "sink");
637   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
638   gst_object_unref (pad);
639
640   gst_element_set_state (element, GST_STATE_READY);
641
642   /* since we're gonna add it to a bin but don't want to lose it,
643    * we keep a reference. */
644   gst_object_ref (element);
645   g_hash_table_insert (play_bin->cache, "vbin", element);
646
647   return element;
648
649 no_colorspace:
650   {
651     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
652         (_("Missing element '%s' - check your GStreamer installation."),
653             "ffmpegcolorspace"), (NULL));
654     gst_object_unref (element);
655     return NULL;
656   }
657
658 no_videoscale:
659   {
660     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
661         (_("Missing element '%s' - check your GStreamer installation."),
662             "videoscale"), ("possibly a liboil version mismatch?"));
663     gst_object_unref (element);
664     return NULL;
665   }
666 }
667
668 /* make an element for playback of video with subtitles embedded.
669  *
670  *  +--------------------------------------------------+
671  *  | tbin                  +-------------+            |
672  *  |          +-----+      | textoverlay |   +------+ |
673  *  |          | csp | +--video_sink      |   | vbin | |
674  * video_sink-sink  src+ +-text_sink     src-sink    | |
675  *  |          +-----+   |  +-------------+   +------+ |
676  * text_sink-------------+                             |
677  *  +--------------------------------------------------+
678  */
679
680 static GstElement *
681 gen_text_element (GstPlayBin * play_bin)
682 {
683   GstElement *element, *csp, *overlay, *vbin;
684   GstPad *pad;
685
686   /* Create our bin */
687   element = gst_bin_new ("textbin");
688
689   /* Text overlay */
690   overlay = gst_element_factory_make ("textoverlay", "overlay");
691
692   /* Create the video rendering bin */
693   vbin = gen_video_element (play_bin);
694
695   /* If no overlay return the video bin */
696   if (!overlay) {
697     GST_WARNING ("No overlay (pango) element, subtitles disabled");
698     return vbin;
699   }
700
701   /* Set some parameters */
702   g_object_set (G_OBJECT (overlay),
703       "halign", "center", "valign", "bottom", NULL);
704   if (play_bin->font_desc) {
705     g_object_set (G_OBJECT (overlay), "font-desc", play_bin->font_desc, NULL);
706   }
707
708   /* Take a ref */
709   play_bin->textoverlay_element = GST_ELEMENT (gst_object_ref (overlay));
710
711   /* we know this will succeed, as the video bin already created one before */
712   csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
713
714   /* Add our elements */
715   gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
716
717   /* Link */
718   gst_element_link_pads (csp, "src", overlay, "video_sink");
719   gst_element_link_pads (overlay, "src", vbin, "sink");
720
721   /* Add ghost pads on the subtitle bin */
722   pad = gst_element_get_pad (overlay, "text_sink");
723   gst_element_add_pad (element, gst_ghost_pad_new ("text_sink", pad));
724   gst_object_unref (pad);
725
726   pad = gst_element_get_pad (csp, "sink");
727   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
728   gst_object_unref (pad);
729
730   /* Set state to READY */
731   gst_element_set_state (element, GST_STATE_READY);
732
733   return element;
734 }
735
736 /* make the element (bin) that contains the elements needed to perform
737  * audio playback.
738  *
739  *  +-------------------------------------------------------------+
740  *  | abin                                                        |
741  *  |      +---------+   +----------+   +---------+   +---------+ |
742  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
743  *  |   +-sink      src-sink       src-sink      src-sink       | |
744  *  |   |  +---------+   +----------+   +---------+   +---------+ |
745  * sink-+                                                         |
746  *  +-------------------------------------------------------------+
747  *
748  */
749 static GstElement *
750 gen_audio_element (GstPlayBin * play_bin)
751 {
752   GstElement *element;
753   GstElement *conv;
754   GstElement *scale;
755   GstElement *sink;
756   GstElement *volume;
757   GstPad *pad;
758
759   element = g_hash_table_lookup (play_bin->cache, "abin");
760   if (element != NULL) {
761     return element;
762   }
763   element = gst_bin_new ("abin");
764   conv = gst_element_factory_make ("audioconvert", "aconv");
765   if (conv == NULL)
766     goto no_audioconvert;
767
768   scale = gst_element_factory_make ("audioresample", "aresample");
769   if (scale == NULL)
770     goto no_audioresample;
771
772   volume = gst_element_factory_make ("volume", "volume");
773   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
774   play_bin->volume_element = volume;
775
776   if (play_bin->audio_sink) {
777     sink = play_bin->audio_sink;
778   } else {
779     sink = gst_element_factory_make ("autoaudiosink", "audiosink");
780     if (sink == NULL) {
781       sink = gst_element_factory_make ("alsasink", "audiosink");
782     }
783     if (sink == NULL) {
784       GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
785           (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
786       return NULL;
787     }
788     play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
789   }
790
791   gst_object_ref (sink);
792   g_hash_table_insert (play_bin->cache, "audio_sink", sink);
793
794   gst_bin_add (GST_BIN (element), conv);
795   gst_bin_add (GST_BIN (element), scale);
796   gst_bin_add (GST_BIN (element), volume);
797   gst_bin_add (GST_BIN (element), sink);
798
799   gst_element_link_pads (conv, "src", scale, "sink");
800   gst_element_link_pads (scale, "src", volume, "sink");
801   gst_element_link_pads (volume, "src", sink, "sink");
802
803   pad = gst_element_get_pad (conv, "sink");
804   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
805   gst_object_unref (pad);
806
807   gst_element_set_state (element, GST_STATE_READY);
808
809   /* since we're gonna add it to a bin but don't want to lose it,
810    * we keep a reference. */
811   gst_object_ref (element);
812   g_hash_table_insert (play_bin->cache, "abin", element);
813
814   return element;
815
816 no_audioconvert:
817   {
818     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
819         (_("Missing element '%s' - check your GStreamer installation."),
820             "audioconvert"), ("possibly a liboil version mismatch?"));
821     gst_object_unref (element);
822     return NULL;
823   }
824
825 no_audioresample:
826   {
827     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
828         (_("Missing element '%s' - check your GStreamer installation."),
829             "audioresample"), ("possibly a liboil version mismatch?"));
830     gst_object_unref (element);
831     return NULL;
832   }
833 }
834
835 /* make the element (bin) that contains the elements needed to perform
836  * visualisation ouput.  The idea is to split the audio using tee, then 
837  * sending the output to the regular audio bin and the other output to
838  * the vis plugin that transforms it into a video that is rendered with the
839  * normal video bin. The video bin is run in a thread to make sure it does
840  * not block the audio playback pipeline.
841  *
842  *  +--------------------------------------------------------------------+
843  *  | visbin                                                             |
844  *  |      +------+   +--------+   +----------------+                    |
845  *  |      | tee  |   | aqueue |   |   abin ...     |                    |
846  *  |   +-sink   src-sink     src-sink              |                    |
847  *  |   |  |      |   +--------+   +----------------+                    |
848  *  |   |  |      |                                                      |
849  *  |   |  |      |   +------+   +---------+   +------+   +-----------+  |
850  *  |   |  |      |   |vqueue|   |audioconv|   | vis  |   | vbin ...  |  |
851  *  |   |  |     src-sink   src-sink      src-sink   src-sink         |  |
852  *  |   |  |      |   +------+   +---------+   +------+   +-----------+  |
853  *  |   |  |      |                                                      |
854  *  |   |  +------+                                                      |
855  * sink-+                                                                |
856    +---------------------------------------------------------------------+
857  */
858 static GstElement *
859 gen_vis_element (GstPlayBin * play_bin)
860 {
861   GstElement *element;
862   GstElement *tee;
863   GstElement *asink;
864   GstElement *vsink;
865   GstElement *conv;
866   GstElement *vis;
867   GstElement *vqueue, *aqueue;
868   GstPad *pad, *rpad;
869
870   asink = gen_audio_element (play_bin);
871   if (!asink)
872     return NULL;
873   vsink = gen_video_element (play_bin);
874   if (!vsink) {
875     gst_object_unref (asink);
876     return NULL;
877   }
878
879   element = gst_bin_new ("visbin");
880   tee = gst_element_factory_make ("tee", "tee");
881
882   vqueue = gst_element_factory_make ("queue", "vqueue");
883   aqueue = gst_element_factory_make ("queue", "aqueue");
884
885   gst_bin_add (GST_BIN (element), asink);
886   gst_bin_add (GST_BIN (element), vqueue);
887   gst_bin_add (GST_BIN (element), aqueue);
888   gst_bin_add (GST_BIN (element), vsink);
889   gst_bin_add (GST_BIN (element), tee);
890
891   conv = gst_element_factory_make ("audioconvert", "aconv");
892   if (conv == NULL)
893     goto no_audioconvert;
894
895   if (play_bin->visualisation) {
896     gst_object_ref (play_bin->visualisation);
897     vis = play_bin->visualisation;
898   } else {
899     vis = gst_element_factory_make ("goom", "vis");
900   }
901
902   gst_bin_add (GST_BIN (element), conv);
903   gst_bin_add (GST_BIN (element), vis);
904
905   gst_element_link_pads (vqueue, "src", conv, "sink");
906   gst_element_link_pads (conv, "src", vis, "sink");
907   gst_element_link_pads (vis, "src", vsink, "sink");
908
909   pad = gst_element_get_pad (aqueue, "sink");
910   rpad = gst_element_get_request_pad (tee, "src%d");
911   gst_pad_link (rpad, pad);
912   gst_object_unref (rpad);
913   gst_object_unref (pad);
914   gst_element_link_pads (aqueue, "src", asink, "sink");
915
916   pad = gst_element_get_pad (vqueue, "sink");
917   rpad = gst_element_get_request_pad (tee, "src%d");
918   gst_pad_link (rpad, pad);
919   gst_object_unref (rpad);
920   gst_object_unref (pad);
921
922   pad = gst_element_get_pad (tee, "sink");
923   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
924   gst_object_unref (pad);
925
926   return element;
927
928 no_audioconvert:
929   {
930     GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
931         (_("Missing element '%s' - check your GStreamer installation."),
932             "audioconvert"), ("possibly a liboil version mismatch?"));
933     gst_object_unref (element);
934     return NULL;
935   }
936 }
937
938 /* get rid of all installed sinks */
939 static void
940 remove_sinks (GstPlayBin * play_bin)
941 {
942   GList *sinks;
943   GstObject *parent;
944   GstElement *element;
945   GstPad *pad, *peer;
946
947   GST_DEBUG ("removesinks");
948   element = g_hash_table_lookup (play_bin->cache, "abin");
949   if (element != NULL) {
950     parent = gst_element_get_parent (element);
951     if (parent != NULL) {
952       /* we remove the element from the parent so that
953        * there is no unwanted state change when the parent
954        * is disposed */
955       play_bin->sinks = g_list_remove (play_bin->sinks, element);
956       gst_element_set_state (element, GST_STATE_NULL);
957       gst_bin_remove (GST_BIN (parent), element);
958       gst_object_unref (parent);
959     }
960     pad = gst_element_get_pad (element, "sink");
961     if (pad != NULL) {
962       peer = gst_pad_get_peer (pad);
963       if (peer != NULL) {
964         gst_pad_unlink (peer, pad);
965         gst_object_unref (peer);
966       }
967       gst_object_unref (pad);
968     }
969   }
970   element = g_hash_table_lookup (play_bin->cache, "vbin");
971   if (element != NULL) {
972     parent = gst_element_get_parent (element);
973     if (parent != NULL) {
974       play_bin->sinks = g_list_remove (play_bin->sinks, element);
975       gst_element_set_state (element, GST_STATE_NULL);
976       gst_bin_remove (GST_BIN (parent), element);
977       gst_object_unref (parent);
978     }
979     pad = gst_element_get_pad (element, "sink");
980     if (pad != NULL) {
981       peer = gst_pad_get_peer (pad);
982       if (peer != NULL) {
983         gst_pad_unlink (peer, pad);
984         gst_object_unref (peer);
985       }
986       gst_object_unref (pad);
987     }
988   }
989
990   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
991     GstElement *element = GST_ELEMENT (sinks->data);
992     GstPad *pad;
993     GstPad *peer;
994
995     pad = gst_element_get_pad (element, "sink");
996
997     GST_LOG ("removing sink %p", element);
998
999     peer = gst_pad_get_peer (pad);
1000     if (peer) {
1001       gst_pad_unlink (peer, pad);
1002       gst_object_unref (peer);
1003     }
1004     gst_object_unref (pad);
1005
1006     gst_element_set_state (element, GST_STATE_NULL);
1007     gst_bin_remove (GST_BIN (play_bin), element);
1008   }
1009   g_list_free (play_bin->sinks);
1010   play_bin->sinks = NULL;
1011
1012   /* FIXME: this is probably some refcounting problem */
1013   if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
1014     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
1015     gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
1016         play_bin->visualisation);
1017   }
1018
1019   if (play_bin->frame) {
1020     gst_buffer_unref (play_bin->frame);
1021     play_bin->frame = NULL;
1022   }
1023
1024   if (play_bin->textoverlay_element) {
1025     gst_object_unref (play_bin->textoverlay_element);
1026     play_bin->textoverlay_element = NULL;
1027   }
1028 }
1029
1030 /* loop over the streams and set up the pipeline to play this
1031  * media file. First we count the number of audio and video streams.
1032  * If there is no video stream but there exists an audio stream,
1033  * we install a visualisation pipeline.
1034  * 
1035  * Also make sure to only connect the first audio and video pad. FIXME
1036  * this should eventually be handled with a tuner interface so that
1037  * one can switch the streams.
1038  */
1039 static gboolean
1040 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
1041     GstPad * subtitle_pad)
1042 {
1043   GstPad *sinkpad;
1044   GstPadLinkReturn linkres;
1045   GstElement *parent;
1046   GstStateChangeReturn stateret;
1047
1048   g_return_val_if_fail (sink != NULL, FALSE);
1049   /* this is only for debugging */
1050   parent = gst_pad_get_parent_element (srcpad);
1051   if (parent) {
1052     GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
1053         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
1054     gst_object_unref (parent);
1055   }
1056
1057   /* bring it to the PAUSED state so we can link to the peer without
1058    * breaking the flow */
1059   if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
1060       GST_STATE_CHANGE_FAILURE)
1061     goto state_failed;
1062
1063   gst_bin_add (GST_BIN (play_bin), sink);
1064
1065   /* we found a sink for this stream, now try to install it */
1066   sinkpad = gst_element_get_pad (sink, "sink");
1067   linkres = gst_pad_link (srcpad, sinkpad);
1068   gst_object_unref (sinkpad);
1069
1070   /* try to link the pad of the sink to the stream */
1071   if (GST_PAD_LINK_FAILED (linkres))
1072     goto link_failed;
1073
1074   if (GST_IS_PAD (subtitle_pad)) {
1075     sinkpad = gst_element_get_pad (sink, "text_sink");
1076     linkres = gst_pad_link (subtitle_pad, sinkpad);
1077     gst_object_unref (sinkpad);
1078   }
1079
1080   /* try to link the subtitle pad of the sink to the stream */
1081   if (GST_PAD_LINK_FAILED (linkres)) {
1082     goto subtitle_failed;
1083   }
1084
1085   /* we got the sink succesfully linked, now keep the sink
1086    * in our internal list */
1087   play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1088
1089   return TRUE;
1090
1091   /* ERRORS */
1092 state_failed:
1093   {
1094     GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1095     return FALSE;
1096   }
1097 link_failed:
1098   {
1099     gchar *capsstr;
1100     GstCaps *caps;
1101
1102     /* could not link this stream */
1103     caps = gst_pad_get_caps (srcpad);
1104     capsstr = gst_caps_to_string (caps);
1105     g_warning ("could not link %s: %d", capsstr, linkres);
1106     GST_DEBUG_OBJECT (play_bin,
1107         "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1108     g_free (capsstr);
1109     gst_caps_unref (caps);
1110
1111     gst_element_set_state (sink, GST_STATE_NULL);
1112     gst_bin_remove (GST_BIN (play_bin), sink);
1113     return FALSE;
1114   }
1115 subtitle_failed:
1116   {
1117     gchar *capsstr;
1118     GstCaps *caps;
1119
1120     /* could not link this stream */
1121     caps = gst_pad_get_caps (subtitle_pad);
1122     capsstr = gst_caps_to_string (caps);
1123     GST_DEBUG_OBJECT (play_bin,
1124         "subtitle link failed when adding sink, caps %s, reason %d", capsstr,
1125         linkres);
1126     g_free (capsstr);
1127     gst_caps_unref (caps);
1128
1129     return TRUE;
1130   }
1131 }
1132
1133 static gboolean
1134 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1135 {
1136   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1137   GList *streaminfo = NULL, *s;
1138   gboolean need_vis = FALSE;
1139   gboolean need_text = FALSE;
1140   GstPad *textsrcpad = NULL, *pad = NULL;
1141   GstElement *sink;
1142   gboolean res = TRUE;
1143
1144   /* get rid of existing sinks */
1145   if (play_bin->sinks) {
1146     remove_sinks (play_bin);
1147   }
1148   GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1149
1150   /* find out what to do */
1151   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1152       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1153     need_text = TRUE;
1154   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1155       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1156       play_bin->visualisation != NULL) {
1157     need_vis = TRUE;
1158   }
1159
1160   /* now actually connect everything */
1161   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1162   for (s = streaminfo; s; s = g_list_next (s)) {
1163     GObject *obj = G_OBJECT (s->data);
1164     gint type;
1165     GstObject *object;
1166
1167     g_object_get (obj, "type", &type, NULL);
1168     g_object_get (obj, "object", &object, NULL);
1169   }
1170
1171   /* link audio */
1172   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1173     if (need_vis) {
1174       sink = gen_vis_element (play_bin);
1175     } else {
1176       sink = gen_audio_element (play_bin);
1177     }
1178     if (!sink)
1179       return FALSE;
1180     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1181         "src");
1182     res = add_sink (play_bin, sink, pad, NULL);
1183     gst_object_unref (pad);
1184   }
1185
1186   /* link video */
1187   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1188     if (need_text) {
1189       GstObject *parent = NULL, *grandparent = NULL;
1190       GstPad *ghost = NULL;
1191
1192       sink = gen_text_element (play_bin);
1193       textsrcpad =
1194           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1195           "src");
1196       /* This pad is from subtitle-bin, we need to create a ghost pad to have
1197          common grandparents */
1198       parent = gst_object_get_parent (GST_OBJECT (textsrcpad));
1199       if (!parent) {
1200         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1201         gst_object_unref (textsrcpad);
1202         textsrcpad = NULL;
1203         goto beach;
1204       }
1205
1206       grandparent = gst_object_get_parent (parent);
1207       if (!grandparent) {
1208         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1209         gst_object_unref (parent);
1210         gst_object_unref (textsrcpad);
1211         textsrcpad = NULL;
1212         goto beach;
1213       }
1214
1215       /* We ghost the pad on subtitle_bin only, if the text pad is from the
1216          media demuxer we keep it as it is */
1217       if (!GST_IS_PLAY_BIN (grandparent)) {
1218         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1219             "file, ghosting to a suitable hierarchy");
1220         ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1221         if (!GST_IS_PAD (ghost)) {
1222           GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1223               "subtitle-bin");
1224           gst_object_unref (parent);
1225           gst_object_unref (grandparent);
1226           gst_object_unref (textsrcpad);
1227           textsrcpad = NULL;
1228           goto beach;
1229         }
1230
1231         if (gst_element_add_pad (GST_ELEMENT (grandparent), ghost)) {
1232           gst_object_unref (textsrcpad);
1233           textsrcpad = gst_object_ref (ghost);
1234         } else {
1235           GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1236           gst_object_unref (ghost);
1237           gst_object_unref (textsrcpad);
1238           textsrcpad = NULL;
1239         }
1240       } else {
1241         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1242             "no changes to hierarchy needed");
1243       }
1244
1245       gst_object_unref (parent);
1246       gst_object_unref (grandparent);
1247     } else {
1248       sink = gen_video_element (play_bin);
1249     }
1250   beach:
1251     if (!sink)
1252       return FALSE;
1253     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1254         "src");
1255     res = add_sink (play_bin, sink, pad, textsrcpad);
1256     gst_object_unref (pad);
1257     if (textsrcpad) {
1258       gst_object_unref (textsrcpad);
1259     }
1260   }
1261
1262   /* remove the sinks now, pipeline get_state will now wait for the
1263    * sinks to preroll */
1264   if (play_bin->fakesink) {
1265     gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1266     gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1267     play_bin->fakesink = NULL;
1268   }
1269
1270   return res;
1271 }
1272
1273 /* Send an event to our sinks until one of them works; don't then send to the
1274  * remaining sinks (unlike GstBin)
1275  */
1276 static gboolean
1277 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1278 {
1279   GList *sinks = play_bin->sinks;
1280   gboolean res = TRUE;
1281
1282   while (sinks) {
1283     GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1284
1285     gst_event_ref (event);
1286     if ((res = gst_element_send_event (sink, event))) {
1287       GST_DEBUG_OBJECT (play_bin,
1288           "Sent event succesfully to sink %" GST_PTR_FORMAT, sink);
1289       break;
1290     }
1291     GST_DEBUG_OBJECT (play_bin,
1292         "Event failed when sent to sink %" GST_PTR_FORMAT, sink);
1293
1294     sinks = g_list_next (sinks);
1295   }
1296
1297   gst_event_unref (event);
1298
1299   return res;
1300 }
1301
1302 static gboolean
1303 do_playbin_seek (GstElement * element, GstEvent * event)
1304 {
1305   gdouble rate;
1306   GstSeekFlags flags;
1307   gboolean flush;
1308   gboolean was_playing = FALSE;
1309   gboolean res;
1310
1311   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
1312
1313   flush = flags & GST_SEEK_FLAG_FLUSH;
1314
1315   if (flush) {
1316     GstState state;
1317
1318     /* need to call _get_state() since a bin state is only updated
1319      * with this call. */
1320     gst_element_get_state (element, &state, NULL, 0);
1321     was_playing = state == GST_STATE_PLAYING;
1322
1323     if (was_playing) {
1324       gst_element_set_state (element, GST_STATE_PAUSED);
1325       gst_element_get_state (element, NULL, NULL, 50 * GST_MSECOND);
1326     }
1327   }
1328
1329   GST_DEBUG_OBJECT (element, "Sending seek event to a sink");
1330   res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1331
1332   if (flush) {
1333     /* need to reset the stream time to 0 after a flushing seek */
1334     if (res)
1335       gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
1336
1337     if (was_playing)
1338       /* and continue playing */
1339       gst_element_set_state (element, GST_STATE_PLAYING);
1340   }
1341   return res;
1342 }
1343
1344 /* We only want to send the event to a single sink (overriding GstBin's 
1345  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1346  * events appropriately. So, this is a messy duplication of code. */
1347 static gboolean
1348 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1349 {
1350   gboolean res = FALSE;
1351   GstEventType event_type = GST_EVENT_TYPE (event);
1352
1353
1354   switch (event_type) {
1355     case GST_EVENT_SEEK:
1356       res = do_playbin_seek (element, event);
1357       break;
1358     default:
1359       res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1360       break;
1361   }
1362
1363   return res;
1364 }
1365
1366 static GstStateChangeReturn
1367 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1368 {
1369   GstStateChangeReturn ret;
1370   GstPlayBin *play_bin;
1371
1372   play_bin = GST_PLAY_BIN (element);
1373
1374
1375   switch (transition) {
1376     case GST_STATE_CHANGE_READY_TO_PAUSED:
1377       /* this really is the easiest way to make the state change return
1378        * ASYNC until we added the sinks */
1379       if (!play_bin->fakesink) {
1380         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1381         gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1382       }
1383       break;
1384     default:
1385       break;
1386   }
1387
1388   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1389   if (ret == GST_STATE_CHANGE_FAILURE)
1390     return ret;
1391
1392   switch (transition) {
1393     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1394       /* Set audio sink state to NULL to release the sound device,
1395        * but only if we own it (else we might be in chain-transition). */
1396       //if (play_bin->audio_sink != NULL &&
1397       //    GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1398       //  gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1399       //}
1400       break;
1401     case GST_STATE_CHANGE_PAUSED_TO_READY:
1402       /* Check for NULL because the state transition may be done by
1403        * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1404        * case, we don't want to run remove_sinks.
1405        * FIXME: should the NULL test be done in remove_sinks? Should we just
1406        * set the state to NULL in gst_play_bin_dispose?
1407        */
1408       if (play_bin->cache != NULL) {
1409         remove_sinks (play_bin);
1410       }
1411       if (play_bin->fakesink) {
1412         gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1413         gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1414         play_bin->fakesink = NULL;
1415       }
1416       break;
1417     default:
1418       break;
1419   }
1420
1421   return ret;
1422 }
1423
1424 static gboolean
1425 plugin_init (GstPlugin * plugin)
1426 {
1427   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1428
1429 #ifdef ENABLE_NLS
1430   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1431       LOCALEDIR);
1432   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1433 #endif /* ENABLE_NLS */
1434
1435   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1436       GST_TYPE_PLAY_BIN);
1437 }
1438
1439 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1440     GST_VERSION_MINOR,
1441     "playbin",
1442     "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1443     GST_PACKAGE_ORIGIN)