Add docs for adder, use GST_ELEMENT_DETAILS macro, define GstElementDetails at the top
[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 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_ref (gst_play_base_bin_get_type ());
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   conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
623   scale = gst_element_factory_make ("videoscale", "vscale");
624   gst_bin_add (GST_BIN (element), identity);
625   gst_bin_add (GST_BIN (element), conv);
626   gst_bin_add (GST_BIN (element), scale);
627   gst_bin_add (GST_BIN (element), sink);
628   gst_element_link_pads (identity, "src", conv, "sink");
629   gst_element_link_pads (conv, "src", scale, "sink");
630   gst_element_link_pads (scale, "src", sink, "sink");
631
632   pad = gst_element_get_pad (identity, "sink");
633   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
634   gst_object_unref (pad);
635
636   gst_element_set_state (element, GST_STATE_READY);
637
638   /* since we're gonna add it to a bin but don't want to lose it,
639    * we keep a reference. */
640   gst_object_ref (element);
641   g_hash_table_insert (play_bin->cache, "vbin", element);
642
643   return element;
644 }
645
646 /* make an element for playback of video with subtitles embedded.
647  *
648  *  +--------------------------------------------------+
649  *  | tbin                  +-------------+            |
650  *  |          +-----+      | textoverlay |   +------+ |
651  *  |          | csp | +--video_sink      |   | vbin | |
652  * video_sink-sink  src+ +-text_sink     src-sink    | |
653  *  |          +-----+   |  +-------------+   +------+ |
654  * text_sink-------------+                             |
655  *  +--------------------------------------------------+
656  */
657
658 static GstElement *
659 gen_text_element (GstPlayBin * play_bin)
660 {
661   GstElement *element, *csp, *overlay, *vbin;
662   GstPad *pad;
663
664   /* Create our bin */
665   element = gst_bin_new ("textbin");
666
667   /* Text overlay */
668   overlay = gst_element_factory_make ("textoverlay", "overlay");
669
670   /* Create the video rendering bin */
671   vbin = gen_video_element (play_bin);
672
673   /* If no overlay return the video bin */
674   if (!overlay) {
675     GST_WARNING ("No overlay (pango) element, subtitles disabled");
676     return vbin;
677   }
678
679   /* Set some parameters */
680   g_object_set (G_OBJECT (overlay),
681       "halign", "center", "valign", "bottom", NULL);
682   if (play_bin->font_desc) {
683     g_object_set (G_OBJECT (overlay), "font-desc", play_bin->font_desc, NULL);
684   }
685
686   /* Take a ref */
687   play_bin->textoverlay_element = GST_ELEMENT (gst_object_ref (overlay));
688
689   csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
690
691   /* Add our elements */
692   gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
693
694   /* Link */
695   gst_element_link_pads (csp, "src", overlay, "video_sink");
696   gst_element_link_pads (overlay, "src", vbin, "sink");
697
698   /* Add ghost pads on the subtitle bin */
699   pad = gst_element_get_pad (overlay, "text_sink");
700   gst_element_add_pad (element, gst_ghost_pad_new ("text_sink", pad));
701   gst_object_unref (pad);
702
703   pad = gst_element_get_pad (csp, "sink");
704   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
705   gst_object_unref (pad);
706
707   /* Set state to READY */
708   gst_element_set_state (element, GST_STATE_READY);
709
710   return element;
711 }
712
713 /* make the element (bin) that contains the elements needed to perform
714  * audio playback.
715  *
716  *  +-------------------------------------------------------------+
717  *  | abin                                                        |
718  *  |      +---------+   +----------+   +---------+   +---------+ |
719  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
720  *  |   +-sink      src-sink       src-sink      src-sink       | |
721  *  |   |  +---------+   +----------+   +---------+   +---------+ |
722  * sink-+                                                         |
723  *  +-------------------------------------------------------------+
724  *
725  */
726 static GstElement *
727 gen_audio_element (GstPlayBin * play_bin)
728 {
729   GstElement *element;
730   GstElement *conv;
731   GstElement *sink;
732   GstElement *volume;
733   GstElement *scale;
734   GstPad *pad;
735
736   element = g_hash_table_lookup (play_bin->cache, "abin");
737   if (element != NULL) {
738     return element;
739   }
740   element = gst_bin_new ("abin");
741   conv = gst_element_factory_make ("audioconvert", "aconv");
742   scale = gst_element_factory_make ("audioscale", "ascale");
743
744   volume = gst_element_factory_make ("volume", "volume");
745   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
746   play_bin->volume_element = volume;
747
748   if (play_bin->audio_sink) {
749     sink = play_bin->audio_sink;
750   } else {
751     sink = gst_element_factory_make ("autoaudiosink", "audiosink");
752     if (sink == NULL) {
753       sink = gst_element_factory_make ("alsasink", "audiosink");
754     }
755     if (sink == NULL) {
756       GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
757           (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
758       return NULL;
759     }
760     play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
761   }
762
763   gst_object_ref (sink);
764   g_hash_table_insert (play_bin->cache, "audio_sink", sink);
765
766   gst_bin_add (GST_BIN (element), conv);
767   //gst_bin_add (GST_BIN (element), scale);
768   gst_bin_add (GST_BIN (element), volume);
769   gst_bin_add (GST_BIN (element), sink);
770
771   gst_element_link_pads (conv, "src",   /*scale, "sink");
772                                            gst_element_link_pads (scale, "src", */ volume, "sink");
773   gst_element_link_pads (volume, "src", sink, "sink");
774
775   pad = gst_element_get_pad (conv, "sink");
776   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
777   gst_object_unref (pad);
778
779   gst_element_set_state (element, GST_STATE_READY);
780
781   /* since we're gonna add it to a bin but don't want to lose it,
782    * we keep a reference. */
783   gst_object_ref (element);
784   g_hash_table_insert (play_bin->cache, "abin", element);
785
786   return element;
787 }
788
789 /* make the element (bin) that contains the elements needed to perform
790  * visualisation ouput.  The idea is to split the audio using tee, then 
791  * sending the output to the regular audio bin and the other output to
792  * the vis plugin that transforms it into a video that is rendered with the
793  * normal video bin. The video bin is run in a thread to make sure it does
794  * not block the audio playback pipeline.
795  *
796  *  +--------------------------------------------------------------------+
797  *  | visbin                                                             |
798  *  |      +------+   +--------+   +----------------+                    |
799  *  |      | tee  |   | aqueue |   |   abin ...     |                    |
800  *  |   +-sink   src-sink     src-sink              |                    |
801  *  |   |  |      |   +--------+   +----------------+                    |
802  *  |   |  |      |                                                      |
803  *  |   |  |      |   +------+   +---------+   +------+   +-----------+  |
804  *  |   |  |      |   |vqueue|   |audioconv|   | vis  |   | vbin ...  |  |
805  *  |   |  |     src-sink   src-sink      src-sink   src-sink         |  |
806  *  |   |  |      |   +------+   +---------+   +------+   +-----------+  |
807  *  |   |  |      |                                                      |
808  *  |   |  +------+                                                      |
809  * sink-+                                                                |
810    +---------------------------------------------------------------------+
811  */
812 static GstElement *
813 gen_vis_element (GstPlayBin * play_bin)
814 {
815   GstElement *element;
816   GstElement *tee;
817   GstElement *asink;
818   GstElement *vsink;
819   GstElement *conv;
820   GstElement *vis;
821   GstElement *vqueue, *aqueue;
822   GstPad *pad, *rpad;
823
824   asink = gen_audio_element (play_bin);
825   if (!asink)
826     return NULL;
827   vsink = gen_video_element (play_bin);
828   if (!vsink) {
829     gst_object_unref (asink);
830     return NULL;
831   }
832
833   element = gst_bin_new ("visbin");
834   tee = gst_element_factory_make ("tee", "tee");
835
836   vqueue = gst_element_factory_make ("queue", "vqueue");
837   aqueue = gst_element_factory_make ("queue", "aqueue");
838
839   gst_bin_add (GST_BIN (element), asink);
840   gst_bin_add (GST_BIN (element), vqueue);
841   gst_bin_add (GST_BIN (element), aqueue);
842   gst_bin_add (GST_BIN (element), vsink);
843   gst_bin_add (GST_BIN (element), tee);
844
845   conv = gst_element_factory_make ("audioconvert", "aconv");
846   if (play_bin->visualisation) {
847     gst_object_ref (play_bin->visualisation);
848     vis = play_bin->visualisation;
849   } else {
850     vis = gst_element_factory_make ("goom", "vis");
851   }
852
853   gst_bin_add (GST_BIN (element), conv);
854   gst_bin_add (GST_BIN (element), vis);
855
856   gst_element_link_pads (vqueue, "src", conv, "sink");
857   gst_element_link_pads (conv, "src", vis, "sink");
858   gst_element_link_pads (vis, "src", vsink, "sink");
859
860   pad = gst_element_get_pad (aqueue, "sink");
861   rpad = gst_element_get_request_pad (tee, "src%d");
862   gst_pad_link (rpad, pad);
863   gst_object_unref (rpad);
864   gst_object_unref (pad);
865   gst_element_link_pads (aqueue, "src", asink, "sink");
866
867   pad = gst_element_get_pad (vqueue, "sink");
868   rpad = gst_element_get_request_pad (tee, "src%d");
869   gst_pad_link (rpad, pad);
870   gst_object_unref (rpad);
871   gst_object_unref (pad);
872
873   pad = gst_element_get_pad (tee, "sink");
874   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
875   gst_object_unref (pad);
876
877   return element;
878 }
879
880 /* get rid of all installed sinks */
881 static void
882 remove_sinks (GstPlayBin * play_bin)
883 {
884   GList *sinks;
885   GstObject *parent;
886   GstElement *element;
887   GstPad *pad, *peer;
888
889   GST_DEBUG ("removesinks");
890   element = g_hash_table_lookup (play_bin->cache, "abin");
891   if (element != NULL) {
892     parent = gst_element_get_parent (element);
893     if (parent != NULL) {
894       /* we remove the element from the parent so that
895        * there is no unwanted state change when the parent
896        * is disposed */
897       play_bin->sinks = g_list_remove (play_bin->sinks, element);
898       gst_element_set_state (element, GST_STATE_NULL);
899       gst_bin_remove (GST_BIN (parent), element);
900       gst_object_unref (parent);
901     }
902     pad = gst_element_get_pad (element, "sink");
903     if (pad != NULL) {
904       peer = gst_pad_get_peer (pad);
905       if (peer != NULL) {
906         gst_pad_unlink (peer, pad);
907         gst_object_unref (peer);
908       }
909       gst_object_unref (pad);
910     }
911   }
912   element = g_hash_table_lookup (play_bin->cache, "vbin");
913   if (element != NULL) {
914     parent = gst_element_get_parent (element);
915     if (parent != NULL) {
916       play_bin->sinks = g_list_remove (play_bin->sinks, element);
917       gst_element_set_state (element, GST_STATE_NULL);
918       gst_bin_remove (GST_BIN (parent), element);
919       gst_object_unref (parent);
920     }
921     pad = gst_element_get_pad (element, "sink");
922     if (pad != NULL) {
923       peer = gst_pad_get_peer (pad);
924       if (peer != NULL) {
925         gst_pad_unlink (peer, pad);
926         gst_object_unref (peer);
927       }
928       gst_object_unref (pad);
929     }
930   }
931
932   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
933     GstElement *element = GST_ELEMENT (sinks->data);
934     GstPad *pad;
935     GstPad *peer;
936
937     pad = gst_element_get_pad (element, "sink");
938
939     GST_LOG ("removing sink %p", element);
940
941     peer = gst_pad_get_peer (pad);
942     if (peer) {
943       gst_pad_unlink (peer, pad);
944       gst_object_unref (peer);
945     }
946     gst_object_unref (pad);
947
948     gst_element_set_state (element, GST_STATE_NULL);
949     gst_bin_remove (GST_BIN (play_bin), element);
950   }
951   g_list_free (play_bin->sinks);
952   play_bin->sinks = NULL;
953
954   /* FIXME: this is probably some refcounting problem */
955   if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
956     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
957     gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
958         play_bin->visualisation);
959   }
960
961   if (play_bin->frame) {
962     gst_buffer_unref (play_bin->frame);
963     play_bin->frame = NULL;
964   }
965
966   if (play_bin->textoverlay_element) {
967     gst_object_unref (play_bin->textoverlay_element);
968     play_bin->textoverlay_element = NULL;
969   }
970 }
971
972 /* loop over the streams and set up the pipeline to play this
973  * media file. First we count the number of audio and video streams.
974  * If there is no video stream but there exists an audio stream,
975  * we install a visualisation pipeline.
976  * 
977  * Also make sure to only connect the first audio and video pad. FIXME
978  * this should eventually be handled with a tuner interface so that
979  * one can switch the streams.
980  */
981 static gboolean
982 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
983     GstPad * subtitle_pad)
984 {
985   GstPad *sinkpad;
986   GstPadLinkReturn linkres;
987   GstElement *parent;
988   GstStateChangeReturn stateret;
989
990   g_return_val_if_fail (sink != NULL, FALSE);
991   /* this is only for debugging */
992   parent = gst_pad_get_parent_element (srcpad);
993   if (parent) {
994     GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
995         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
996     gst_object_unref (parent);
997   }
998
999   /* bring it to the PAUSED state so we can link to the peer without
1000    * breaking the flow */
1001   if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
1002       GST_STATE_CHANGE_FAILURE)
1003     goto state_failed;
1004
1005   gst_bin_add (GST_BIN (play_bin), sink);
1006
1007   /* we found a sink for this stream, now try to install it */
1008   sinkpad = gst_element_get_pad (sink, "sink");
1009   linkres = gst_pad_link (srcpad, sinkpad);
1010   gst_object_unref (sinkpad);
1011
1012   /* try to link the pad of the sink to the stream */
1013   if (GST_PAD_LINK_FAILED (linkres))
1014     goto link_failed;
1015
1016   if (GST_IS_PAD (subtitle_pad)) {
1017     sinkpad = gst_element_get_pad (sink, "text_sink");
1018     linkres = gst_pad_link (subtitle_pad, sinkpad);
1019     gst_object_unref (sinkpad);
1020   }
1021
1022   /* try to link the subtitle pad of the sink to the stream */
1023   if (GST_PAD_LINK_FAILED (linkres)) {
1024     goto subtitle_failed;
1025   }
1026
1027   /* we got the sink succesfully linked, now keep the sink
1028    * in our internal list */
1029   play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1030
1031   return TRUE;
1032
1033   /* ERRORS */
1034 state_failed:
1035   {
1036     GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1037     return FALSE;
1038   }
1039 link_failed:
1040   {
1041     gchar *capsstr;
1042     GstCaps *caps;
1043
1044     /* could not link this stream */
1045     caps = gst_pad_get_caps (srcpad);
1046     capsstr = gst_caps_to_string (caps);
1047     g_warning ("could not link %s: %d", capsstr, linkres);
1048     GST_DEBUG_OBJECT (play_bin,
1049         "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1050     g_free (capsstr);
1051     g_free (caps);
1052
1053     gst_element_set_state (sink, GST_STATE_NULL);
1054     gst_bin_remove (GST_BIN (play_bin), sink);
1055     return FALSE;
1056   }
1057 subtitle_failed:
1058   {
1059     gchar *capsstr;
1060     GstCaps *caps;
1061
1062     /* could not link this stream */
1063     caps = gst_pad_get_caps (subtitle_pad);
1064     capsstr = gst_caps_to_string (caps);
1065     GST_DEBUG_OBJECT (play_bin,
1066         "subtitle link failed when adding sink, caps %s, reason %d", capsstr,
1067         linkres);
1068     g_free (capsstr);
1069     g_free (caps);
1070
1071     return TRUE;
1072   }
1073 }
1074
1075 static gboolean
1076 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1077 {
1078   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1079   GList *streaminfo = NULL, *s;
1080   gboolean need_vis = FALSE;
1081   gboolean need_text = FALSE;
1082   GstPad *textsrcpad = NULL, *pad = NULL;
1083   GstElement *sink;
1084   gboolean res = TRUE;
1085
1086   /* get rid of existing sinks */
1087   if (play_bin->sinks) {
1088     remove_sinks (play_bin);
1089   }
1090   GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1091
1092   /* find out what to do */
1093   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1094       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1095     need_text = TRUE;
1096   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1097       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1098       play_bin->visualisation != NULL) {
1099     need_vis = TRUE;
1100   }
1101
1102   /* now actually connect everything */
1103   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1104   for (s = streaminfo; s; s = g_list_next (s)) {
1105     GObject *obj = G_OBJECT (s->data);
1106     gint type;
1107     GstObject *object;
1108
1109     g_object_get (obj, "type", &type, NULL);
1110     g_object_get (obj, "object", &object, NULL);
1111   }
1112
1113   /* link audio */
1114   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1115     if (need_vis) {
1116       sink = gen_vis_element (play_bin);
1117     } else {
1118       sink = gen_audio_element (play_bin);
1119     }
1120     if (!sink)
1121       return FALSE;
1122     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1123         "src");
1124     res = add_sink (play_bin, sink, pad, NULL);
1125     gst_object_unref (pad);
1126   }
1127
1128   /* link video */
1129   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1130     if (need_text) {
1131       GstObject *parent = NULL, *grandparent = NULL;
1132       GstPad *ghost = NULL;
1133
1134       sink = gen_text_element (play_bin);
1135       textsrcpad =
1136           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1137           "src");
1138       /* This pad is from subtitle-bin, we need to create a ghost pad to have
1139          common grandparents */
1140       parent = gst_object_get_parent (GST_OBJECT (textsrcpad));
1141       if (!parent) {
1142         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1143         gst_object_unref (textsrcpad);
1144         textsrcpad = NULL;
1145         goto beach;
1146       }
1147
1148       grandparent = gst_object_get_parent (parent);
1149       if (!grandparent) {
1150         GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1151         gst_object_unref (parent);
1152         gst_object_unref (textsrcpad);
1153         textsrcpad = NULL;
1154         goto beach;
1155       }
1156
1157       /* We ghost the pad on subtitle_bin only, if the text pad is from the
1158          media demuxer we keep it as it is */
1159       if (!GST_IS_PLAY_BIN (grandparent)) {
1160         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1161             "file, ghosting to a suitable hierarchy");
1162         ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1163         if (!GST_IS_PAD (ghost)) {
1164           GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1165               "subtitle-bin");
1166           gst_object_unref (parent);
1167           gst_object_unref (grandparent);
1168           gst_object_unref (textsrcpad);
1169           textsrcpad = NULL;
1170           goto beach;
1171         }
1172
1173         if (gst_element_add_pad (GST_ELEMENT (grandparent), ghost)) {
1174           gst_object_unref (textsrcpad);
1175           textsrcpad = gst_object_ref (ghost);
1176         } else {
1177           GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1178           gst_object_unref (ghost);
1179           gst_object_unref (textsrcpad);
1180           textsrcpad = NULL;
1181         }
1182       } else {
1183         GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1184             "no changes to hierarchy needed");
1185       }
1186
1187       gst_object_unref (parent);
1188       gst_object_unref (grandparent);
1189     } else {
1190       sink = gen_video_element (play_bin);
1191     }
1192   beach:
1193     if (!sink)
1194       return FALSE;
1195     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1196         "src");
1197     res = add_sink (play_bin, sink, pad, textsrcpad);
1198     gst_object_unref (pad);
1199     if (textsrcpad) {
1200       gst_object_unref (textsrcpad);
1201     }
1202   }
1203
1204   /* remove the sinks now, pipeline get_state will now wait for the
1205    * sinks to preroll */
1206   if (play_bin->fakesink) {
1207     gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1208     gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1209     play_bin->fakesink = NULL;
1210   }
1211
1212   return res;
1213 }
1214
1215 /* Send an event to our sinks until one of them works; don't then send to the
1216  * remaining sinks (unlike GstBin)
1217  */
1218 static gboolean
1219 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1220 {
1221   GList *sinks = play_bin->sinks;
1222   gboolean res = TRUE;
1223
1224   while (sinks) {
1225     GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1226
1227     gst_event_ref (event);
1228     if ((res = gst_element_send_event (sink, event)))
1229       break;
1230
1231     sinks = g_list_next (sinks);
1232   }
1233
1234   gst_event_unref (event);
1235
1236   return res;
1237 }
1238
1239 static gboolean
1240 do_playbin_seek (GstElement * element, GstEvent * event)
1241 {
1242   gdouble rate;
1243   GstSeekFlags flags;
1244   gboolean flush;
1245   gboolean was_playing = FALSE;
1246   gboolean res;
1247
1248   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
1249
1250   flush = flags & GST_SEEK_FLAG_FLUSH;
1251
1252   if (flush) {
1253     GstState state;
1254
1255     /* need to call _get_state() since a bin state is only updated
1256      * with this call. */
1257     gst_element_get_state (element, &state, NULL, 0);
1258     was_playing = state == GST_STATE_PLAYING;
1259
1260     if (was_playing) {
1261       gst_element_set_state (element, GST_STATE_PAUSED);
1262     }
1263   }
1264
1265   res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1266
1267   if (flush && res) {
1268     /* need to reset the stream time to 0 after a flushing seek */
1269     gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
1270     if (was_playing)
1271       /* and continue playing */
1272       gst_element_set_state (element, GST_STATE_PLAYING);
1273   }
1274   return res;
1275 }
1276
1277 /* We only want to send the event to a single sink (overriding GstBin's 
1278  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1279  * events appropriately. So, this is a messy duplication of code. */
1280 static gboolean
1281 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1282 {
1283   gboolean res = FALSE;
1284   GstEventType event_type = GST_EVENT_TYPE (event);
1285
1286
1287   switch (event_type) {
1288     case GST_EVENT_SEEK:
1289       res = do_playbin_seek (element, event);
1290       break;
1291     default:
1292       res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1293       break;
1294   }
1295
1296   return res;
1297 }
1298
1299 static GstStateChangeReturn
1300 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1301 {
1302   GstStateChangeReturn ret;
1303   GstPlayBin *play_bin;
1304
1305   play_bin = GST_PLAY_BIN (element);
1306
1307
1308   switch (transition) {
1309     case GST_STATE_CHANGE_READY_TO_PAUSED:
1310       /* this really is the easiest way to make the state change return
1311        * ASYNC until we added the sinks */
1312       if (!play_bin->fakesink) {
1313         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1314         gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1315       }
1316       break;
1317     default:
1318       break;
1319   }
1320
1321   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1322   if (ret == GST_STATE_CHANGE_FAILURE)
1323     return ret;
1324
1325   switch (transition) {
1326     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1327       /* Set audio sink state to NULL to release the sound device,
1328        * but only if we own it (else we might be in chain-transition). */
1329       //if (play_bin->audio_sink != NULL &&
1330       //    GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1331       //  gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1332       //}
1333       break;
1334     case GST_STATE_CHANGE_PAUSED_TO_READY:
1335       /* Check for NULL because the state transition may be done by
1336        * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1337        * case, we don't want to run remove_sinks.
1338        * FIXME: should the NULL test be done in remove_sinks? Should we just
1339        * set the state to NULL in gst_play_bin_dispose?
1340        */
1341       if (play_bin->cache != NULL) {
1342         remove_sinks (play_bin);
1343       }
1344       if (play_bin->fakesink) {
1345         gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1346         gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1347         play_bin->fakesink = NULL;
1348       }
1349       break;
1350     default:
1351       break;
1352   }
1353
1354   return ret;
1355 }
1356
1357 static gboolean
1358 plugin_init (GstPlugin * plugin)
1359 {
1360   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1361
1362 #ifdef ENABLE_NLS
1363   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1364       LOCALEDIR);
1365   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1366 #endif /* ENABLE_NLS */
1367
1368   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1369       GST_TYPE_PLAY_BIN);
1370 }
1371
1372 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1373     GST_VERSION_MINOR,
1374     "playbin",
1375     "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1376     GST_PACKAGE_ORIGIN)