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