All plugins updated for element state changes.
[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 "gstplaybasebin.h"
28
29 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_debug);
30 #define GST_CAT_DEFAULT gst_play_bin_debug
31
32 #define GST_TYPE_PLAY_BIN               (gst_play_bin_get_type())
33 #define GST_PLAY_BIN(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PLAY_BIN,GstPlayBin))
34 #define GST_PLAY_BIN_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PLAY_BIN,GstPlayBinClass))
35 #define GST_IS_PLAY_BIN(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PLAY_BIN))
36 #define GST_IS_PLAY_BIN_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PLAY_BIN))
37
38 #define VOLUME_MAX_DOUBLE 4.0
39
40 typedef struct _GstPlayBin GstPlayBin;
41 typedef struct _GstPlayBinClass GstPlayBinClass;
42
43 struct _GstPlayBin
44 {
45   GstPlayBaseBin parent;
46
47   /* the configurable elements */
48   GstElement *fakesink;
49   GstElement *audio_sink;
50   GstElement *video_sink;
51   GstElement *visualisation;
52   GstElement *volume_element;
53   GstElement *textoverlay_element;
54   gfloat volume;
55
56   /* these are the currently active sinks */
57   GList *sinks;
58
59   /* the last captured frame for snapshots */
60   GstBuffer *frame;
61
62   /* our cache for the sinks */
63   GHashTable *cache;
64
65   /* font description */
66   gchar *font_desc;
67 };
68
69 struct _GstPlayBinClass
70 {
71   GstPlayBaseBinClass parent_class;
72 };
73
74 /* props */
75 enum
76 {
77   ARG_0,
78   ARG_AUDIO_SINK,
79   ARG_VIDEO_SINK,
80   ARG_VIS_PLUGIN,
81   ARG_VOLUME,
82   ARG_FRAME,
83   ARG_FONT_DESC
84 };
85
86 /* signals */
87 enum
88 {
89   LAST_SIGNAL
90 };
91
92 static void gst_play_bin_class_init (GstPlayBinClass * klass);
93 static void gst_play_bin_init (GstPlayBin * play_bin);
94 static void gst_play_bin_dispose (GObject * object);
95
96 static void setup_sinks (GstPlayBaseBin * play_base_bin,
97     GstPlayBaseGroup * group);
98 static void remove_sinks (GstPlayBin * play_bin);
99
100 static void gst_play_bin_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * spec);
102 static void gst_play_bin_get_property (GObject * object, guint prop_id,
103     GValue * value, GParamSpec * spec);
104 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
105     GstStateChange transition);
106
107 static GstElementClass *parent_class;
108
109 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
110
111 static GstElementDetails gst_play_bin_details = {
112   "Player Bin",
113   "Generic/Bin/Player",
114   "Autoplug and play media from an uri",
115   "Wim Taymans <wim@fluendo.com>"
116 };
117
118 static GType
119 gst_play_bin_get_type (void)
120 {
121   static GType gst_play_bin_type = 0;
122
123   if (!gst_play_bin_type) {
124     static const GTypeInfo gst_play_bin_info = {
125       sizeof (GstPlayBinClass),
126       NULL,
127       NULL,
128       (GClassInitFunc) gst_play_bin_class_init,
129       NULL,
130       NULL,
131       sizeof (GstPlayBin),
132       0,
133       (GInstanceInitFunc) gst_play_bin_init,
134       NULL
135     };
136
137     gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
138         "GstPlayBin", &gst_play_bin_info, 0);
139   }
140
141   return gst_play_bin_type;
142 }
143
144 static void
145 gst_play_bin_class_init (GstPlayBinClass * klass)
146 {
147   GObjectClass *gobject_klass;
148   GstElementClass *gstelement_klass;
149   GstBinClass *gstbin_klass;
150   GstPlayBaseBinClass *playbasebin_klass;
151
152   gobject_klass = (GObjectClass *) klass;
153   gstelement_klass = (GstElementClass *) klass;
154   gstbin_klass = (GstBinClass *) klass;
155   playbasebin_klass = (GstPlayBaseBinClass *) klass;
156
157   parent_class = g_type_class_ref (gst_play_base_bin_get_type ());
158
159   gobject_klass->set_property = gst_play_bin_set_property;
160   gobject_klass->get_property = gst_play_bin_get_property;
161
162   g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
163       g_param_spec_object ("video-sink", "Video Sink",
164           "the video output element to use (NULL = default sink)",
165           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
166   g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
167       g_param_spec_object ("audio-sink", "Audio Sink",
168           "the audio output element to use (NULL = default sink)",
169           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
170   g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
171       g_param_spec_object ("vis-plugin", "Vis plugin",
172           "the visualization element to use (NULL = none)",
173           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
174   g_object_class_install_property (gobject_klass, ARG_VOLUME,
175       g_param_spec_double ("volume", "volume", "volume",
176           0.0, VOLUME_MAX_DOUBLE, 1.0, G_PARAM_READWRITE));
177   g_object_class_install_property (gobject_klass, ARG_FRAME,
178       gst_param_spec_mini_object ("frame", "Frame",
179           "The last frame (NULL = no video available)",
180           GST_TYPE_BUFFER, G_PARAM_READABLE));
181   g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
182       g_param_spec_string ("subtitle-font-desc",
183           "Subtitle font description",
184           "Pango font description of font "
185           "to be used for subtitle rendering", NULL, G_PARAM_WRITABLE));
186
187   gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_dispose);
188
189   gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
190
191   gstelement_klass->change_state =
192       GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
193
194   playbasebin_klass->setup_output_pads = setup_sinks;
195 }
196
197 static void
198 gst_play_bin_init (GstPlayBin * play_bin)
199 {
200   play_bin->video_sink = NULL;
201   play_bin->audio_sink = NULL;
202   play_bin->visualisation = NULL;
203   play_bin->volume_element = NULL;
204   play_bin->textoverlay_element = NULL;
205   play_bin->volume = 1.0;
206   play_bin->sinks = NULL;
207   play_bin->frame = NULL;
208   play_bin->font_desc = NULL;
209   play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
210       NULL, (GDestroyNotify) gst_object_unref);
211 }
212
213 static void
214 gst_play_bin_dispose (GObject * object)
215 {
216   GstPlayBin *play_bin;
217
218   play_bin = GST_PLAY_BIN (object);
219
220   if (play_bin->cache != NULL) {
221     remove_sinks (play_bin);
222     g_hash_table_destroy (play_bin->cache);
223     play_bin->cache = NULL;
224   }
225
226   if (play_bin->audio_sink != NULL) {
227     gst_object_unref (play_bin->audio_sink);
228     play_bin->audio_sink = NULL;
229   }
230   if (play_bin->video_sink != NULL) {
231     gst_object_unref (play_bin->video_sink);
232     play_bin->video_sink = NULL;
233   }
234   if (play_bin->visualisation != NULL) {
235     gst_object_unref (play_bin->visualisation);
236     play_bin->visualisation = NULL;
237   }
238   g_free (play_bin->font_desc);
239   play_bin->font_desc = NULL;
240
241   G_OBJECT_CLASS (parent_class)->dispose (object);
242 }
243
244
245 static void
246 gst_play_bin_set_property (GObject * object, guint prop_id,
247     const GValue * value, GParamSpec * pspec)
248 {
249   GstPlayBin *play_bin;
250
251   g_return_if_fail (GST_IS_PLAY_BIN (object));
252
253   play_bin = GST_PLAY_BIN (object);
254
255   switch (prop_id) {
256     case ARG_VIDEO_SINK:
257       if (play_bin->video_sink != NULL) {
258         gst_object_unref (play_bin->video_sink);
259       }
260       play_bin->video_sink = g_value_get_object (value);
261       if (play_bin->video_sink != NULL) {
262         gst_object_ref (play_bin->video_sink);
263         gst_object_sink (GST_OBJECT (play_bin->video_sink));
264       }
265       /* when changing the videosink, we just remove the
266        * video pipeline from the cache so that it will be 
267        * regenerated with the new sink element */
268       g_hash_table_remove (play_bin->cache, "vbin");
269       break;
270     case ARG_AUDIO_SINK:
271       if (play_bin->audio_sink != NULL) {
272         gst_object_unref (play_bin->audio_sink);
273       }
274       play_bin->audio_sink = g_value_get_object (value);
275       if (play_bin->audio_sink != NULL) {
276         gst_object_ref (play_bin->audio_sink);
277         gst_object_sink (GST_OBJECT (play_bin->audio_sink));
278       }
279       g_hash_table_remove (play_bin->cache, "abin");
280       break;
281     case ARG_VIS_PLUGIN:
282       if (play_bin->visualisation != NULL) {
283         gst_object_unref (play_bin->visualisation);
284       }
285       play_bin->visualisation = g_value_get_object (value);
286       if (play_bin->visualisation != NULL) {
287         gst_object_ref (play_bin->visualisation);
288         gst_object_sink (GST_OBJECT (play_bin->visualisation));
289       }
290       break;
291     case ARG_VOLUME:
292       play_bin->volume = g_value_get_double (value);
293       if (play_bin->volume_element) {
294         g_object_set (G_OBJECT (play_bin->volume_element), "volume",
295             play_bin->volume, NULL);
296       }
297       break;
298     case ARG_FONT_DESC:
299       g_free (play_bin->font_desc);
300       play_bin->font_desc = g_strdup (g_value_get_string (value));
301       if (play_bin->textoverlay_element) {
302         g_object_set (G_OBJECT (play_bin->textoverlay_element),
303             "font-desc", g_value_get_string (value), NULL);
304       }
305       break;
306     default:
307       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308       break;
309   }
310 }
311
312 static void
313 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
314     GParamSpec * pspec)
315 {
316   GstPlayBin *play_bin;
317
318   g_return_if_fail (GST_IS_PLAY_BIN (object));
319
320   play_bin = GST_PLAY_BIN (object);
321
322   switch (prop_id) {
323     case ARG_VIDEO_SINK:
324       g_value_set_object (value, play_bin->video_sink);
325       break;
326     case ARG_AUDIO_SINK:
327       g_value_set_object (value, play_bin->audio_sink);
328       break;
329     case ARG_VIS_PLUGIN:
330       g_value_set_object (value, play_bin->visualisation);
331       break;
332     case ARG_VOLUME:
333       g_value_set_double (value, play_bin->volume);
334       break;
335     case ARG_FRAME:
336       gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
337       break;
338     default:
339       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340       break;
341   }
342 }
343
344 /* signal fired when the identity has received a new buffer. This is used for
345  * making screenshots.
346  */
347 static void
348 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
349 {
350   GstPlayBin *play_bin = GST_PLAY_BIN (data);
351
352   if (play_bin->frame) {
353     gst_buffer_unref (play_bin->frame);
354   }
355   play_bin->frame = gst_buffer_ref (frame);
356 }
357
358 /* make the element (bin) that contains the elements needed to perform
359  * video display. We connect a handoff signal to identity so that we
360  * can grab snapshots. Identity's sinkpad is ghosted to vbin.
361  *
362  *  +-------------------------------------------------------------+
363  *  | vbin                                                        |
364  *  |      +--------+   +----------+   +----------+   +---------+ |
365  *  |      |identity|   |colorspace|   |videoscale|   |videosink| |
366  *  |   +-sink     src-sink       src-sink       src-sink       | |
367  *  |   |  +---+----+   +----------+   +----------+   +---------+ |
368  * sink-+      |                                                  |
369  *  +----------|--------------------------------------------------+
370  *           handoff
371  */
372 static GstElement *
373 gen_video_element (GstPlayBin * play_bin)
374 {
375   GstElement *element;
376   GstElement *conv;
377
378   GstElement *scale;
379   GstElement *sink;
380   GstElement *identity;
381   GstPad *pad;
382
383   /* first see if we have it in the cache */
384   element = g_hash_table_lookup (play_bin->cache, "vbin");
385   if (element != NULL) {
386     return element;
387   }
388
389   element = gst_bin_new ("vbin");
390   identity = gst_element_factory_make ("identity", "id");
391   g_object_set (identity, "silent", TRUE, NULL);
392   g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
393   conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
394   scale = gst_element_factory_make ("videoscale", "vscale");
395   if (play_bin->video_sink) {
396     sink = play_bin->video_sink;
397   } else {
398     sink = gst_element_factory_make ("autovideosink", "videosink");
399   }
400   gst_object_ref (sink);
401   g_hash_table_insert (play_bin->cache, "video_sink", sink);
402
403   gst_bin_add (GST_BIN (element), identity);
404   gst_bin_add (GST_BIN (element), conv);
405   gst_bin_add (GST_BIN (element), scale);
406   gst_bin_add (GST_BIN (element), sink);
407   gst_element_link_pads (identity, "src", conv, "sink");
408   gst_element_link_pads (conv, "src", scale, "sink");
409   gst_element_link_pads (scale, "src", sink, "sink");
410
411   pad = gst_element_get_pad (identity, "sink");
412   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
413   gst_object_unref (pad);
414
415   gst_element_set_state (element, GST_STATE_READY);
416
417   /* since we're gonna add it to a bin but don't want to lose it,
418    * we keep a reference. */
419   gst_object_ref (element);
420   g_hash_table_insert (play_bin->cache, "vbin", element);
421
422   return element;
423 }
424
425 /* make an element for playback of video with subtitles embedded.
426  *
427  *  +--------------------------------------------------+
428  *  | tbin                  +-------------+            |
429  *  |          +-----+      | textoverlay |   +------+ |
430  *  |          | csp | +--video_sink      |   | vbin | |
431  * video_sink-sink  src+ +-text_sink     src-sink    | |
432  *  |          +-----+   |  +-------------+   +------+ |
433  * text_sink-------------+                             |
434  *  +--------------------------------------------------+
435  */
436
437 static GstElement *
438 gen_text_element (GstPlayBin * play_bin)
439 {
440   GstElement *element, *csp, *overlay, *vbin;
441   GstPad *pad;
442
443   overlay = gst_element_factory_make ("textoverlay", "overlay");
444   g_object_set (G_OBJECT (overlay),
445       "halign", "center", "valign", "bottom", NULL);
446   play_bin->textoverlay_element = overlay;
447   if (play_bin->font_desc) {
448     g_object_set (G_OBJECT (play_bin->textoverlay_element),
449         "font-desc", play_bin->font_desc, NULL);
450   }
451   vbin = gen_video_element (play_bin);
452   if (!overlay) {
453     g_warning ("No overlay (pango) element, subtitles disabled");
454     return vbin;
455   }
456   csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
457   element = gst_bin_new ("textbin");
458   gst_element_link_many (csp, overlay, vbin, NULL);
459   gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
460
461   pad = gst_element_get_pad (overlay, "text_sink");
462 #define gst_element_add_ghost_pad(element, pad, name) \
463     gst_element_add_pad (element, gst_ghost_pad_new (name, pad))
464   gst_element_add_ghost_pad (element, pad, "text_sink");
465   gst_object_unref (pad);
466
467   pad = gst_element_get_pad (csp, "sink");
468   gst_element_add_ghost_pad (element, pad, "sink");
469   gst_object_unref (pad);
470
471   return element;
472 }
473
474 /* make the element (bin) that contains the elements needed to perform
475  * audio playback. 
476  *
477  *  +-------------------------------------------------------------+
478  *  | abin                                                        |
479  *  |      +---------+   +----------+   +---------+   +---------+ |
480  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
481  *  |   +-sink      src-sink       src-sink      src-sink       | |
482  *  |   |  +---------+   +----------+   +---------+   +---------+ |
483  * sink-+                                                         |
484  *  +-------------------------------------------------------------+
485  *                  
486  */
487 static GstElement *
488 gen_audio_element (GstPlayBin * play_bin)
489 {
490   GstElement *element;
491   GstElement *conv;
492   GstElement *sink;
493   GstElement *volume;
494   GstElement *scale;
495   GstPad *pad;
496
497   element = g_hash_table_lookup (play_bin->cache, "abin");
498   if (element != NULL) {
499     return element;
500   }
501   element = gst_bin_new ("abin");
502   conv = gst_element_factory_make ("audioconvert", "aconv");
503   scale = gst_element_factory_make ("audioscale", "ascale");
504
505   volume = gst_element_factory_make ("volume", "volume");
506   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
507   play_bin->volume_element = volume;
508
509   if (play_bin->audio_sink) {
510     sink = play_bin->audio_sink;
511   } else {
512     sink = gst_element_factory_make ("autoaudiosink", "audiosink");
513     play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
514   }
515
516   gst_object_ref (sink);
517   g_hash_table_insert (play_bin->cache, "audio_sink", sink);
518
519   gst_bin_add (GST_BIN (element), conv);
520   //gst_bin_add (GST_BIN (element), scale);
521   gst_bin_add (GST_BIN (element), volume);
522   gst_bin_add (GST_BIN (element), sink);
523
524   gst_element_link_pads (conv, "src",   /*scale, "sink");
525                                            gst_element_link_pads (scale, "src", */ volume, "sink");
526   gst_element_link_pads (volume, "src", sink, "sink");
527
528   pad = gst_element_get_pad (conv, "sink");
529   gst_element_add_ghost_pad (element, pad, "sink");
530   gst_object_unref (pad);
531
532   gst_element_set_state (element, GST_STATE_READY);
533
534   /* since we're gonna add it to a bin but don't want to lose it,
535    * we keep a reference. */
536   gst_object_ref (element);
537   g_hash_table_insert (play_bin->cache, "abin", element);
538
539   return element;
540 }
541
542 /* make the element (bin) that contains the elements needed to perform
543  * visualisation ouput.  The idea is to split the audio using tee, then 
544  * sending the output to the regular audio bin and the other output to
545  * the vis plugin that transforms it into a video that is rendered with the
546  * normal video bin. The video bin is run in a thread to make sure it does
547  * not block the audio playback pipeline.
548  *
549  *  +--------------------------------------------------------------------------+
550  *  | visbin                                                                   |
551  *  |      +------+   +----------------+                                       |
552  *  |      | tee  |   |   abin ...     |                                       |
553  *  |   +-sink   src-sink              |                                       |
554  *  |   |  |      |   +----------------+                 +-------------------+ |
555  *  |   |  |      |                                      | vthread           | |
556  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
557  *  |   |  |      |   |audioconv|   | vis  |   |vqueue|  | | vbin ...     |  | |
558  *  |   |  |     src-sink      src-sink   src-sink   src-sink             |  | |
559  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
560  *  |   |  |      |                                      +-------------------+ |
561  *  |   |  +------+                                                            |
562  * sink-+                                                                      |
563    +--------------------------------------------------------------------------+
564  */
565 static GstElement *
566 gen_vis_element (GstPlayBin * play_bin)
567 {
568   GstElement *element;
569   GstElement *tee;
570   GstElement *asink;
571   GstElement *vsink;
572   GstElement *conv;
573   GstElement *vis;
574   GstElement *vqueue, *aqueue;
575   GstPad *pad, *rpad;
576
577   element = gst_bin_new ("visbin");
578   tee = gst_element_factory_make ("tee", "tee");
579
580   vqueue = gst_element_factory_make ("queue", "vqueue");
581   aqueue = gst_element_factory_make ("queue", "aqueue");
582
583   asink = gen_audio_element (play_bin);
584   vsink = gen_video_element (play_bin);
585
586   gst_bin_add (GST_BIN (element), asink);
587   gst_bin_add (GST_BIN (element), vqueue);
588   gst_bin_add (GST_BIN (element), aqueue);
589   gst_bin_add (GST_BIN (element), vsink);
590   gst_bin_add (GST_BIN (element), tee);
591
592   conv = gst_element_factory_make ("audioconvert", "aconv");
593   if (play_bin->visualisation) {
594     gst_object_ref (play_bin->visualisation);
595     vis = play_bin->visualisation;
596   } else {
597     vis = gst_element_factory_make ("goom", "vis");
598   }
599
600   gst_bin_add (GST_BIN (element), conv);
601   gst_bin_add (GST_BIN (element), vis);
602
603   gst_element_link_pads (conv, "src", vis, "sink");
604   gst_element_link_pads (vis, "src", vqueue, "sink");
605
606   gst_element_link_pads (vqueue, "src", vsink, "sink");
607
608   pad = gst_element_get_pad (aqueue, "sink");
609   rpad = gst_element_get_request_pad (tee, "src%d");
610   gst_pad_link (rpad, pad);
611   gst_object_unref (rpad);
612   gst_object_unref (pad);
613   gst_element_link_pads (aqueue, "src", asink, "sink");
614
615   pad = gst_element_get_pad (conv, "sink");
616   rpad = gst_element_get_request_pad (tee, "src%d");
617   gst_pad_link (rpad, pad);
618   gst_object_unref (rpad);
619   gst_object_unref (pad);
620
621   pad = gst_element_get_pad (tee, "sink");
622   gst_element_add_ghost_pad (element, pad, "sink");
623   gst_object_unref (pad);
624
625   return element;
626 }
627
628 /* get rid of all installed sinks */
629 static void
630 remove_sinks (GstPlayBin * play_bin)
631 {
632   GList *sinks;
633   GstObject *parent;
634   GstElement *element;
635   GstPad *pad, *peer;
636
637   GST_DEBUG ("removesinks");
638   element = g_hash_table_lookup (play_bin->cache, "abin");
639   if (element != NULL) {
640     parent = gst_element_get_parent (element);
641     if (parent != NULL) {
642       /* we remove the element from the parent so that
643        * there is no unwanted state change when the parent
644        * is disposed */
645       play_bin->sinks = g_list_remove (play_bin->sinks, element);
646       gst_bin_remove (GST_BIN (parent), element);
647       gst_object_unref (parent);
648     }
649     pad = gst_element_get_pad (element, "sink");
650     if (pad != NULL) {
651       peer = gst_pad_get_peer (pad);
652       if (peer != NULL) {
653         gst_pad_unlink (peer, pad);
654         gst_object_unref (peer);
655       }
656       gst_object_unref (pad);
657     }
658   }
659   element = g_hash_table_lookup (play_bin->cache, "vbin");
660   if (element != NULL) {
661     parent = gst_element_get_parent (element);
662     if (parent != NULL) {
663       play_bin->sinks = g_list_remove (play_bin->sinks, element);
664       gst_bin_remove (GST_BIN (parent), element);
665       gst_object_unref (parent);
666     }
667     pad = gst_element_get_pad (element, "sink");
668     if (pad != NULL) {
669       peer = gst_pad_get_peer (pad);
670       if (peer != NULL) {
671         gst_pad_unlink (peer, pad);
672         gst_object_unref (peer);
673       }
674       gst_object_unref (pad);
675     }
676   }
677
678   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
679     GstElement *element = GST_ELEMENT (sinks->data);
680     GstPad *pad;
681     GstPad *peer;
682
683     pad = gst_element_get_pad (element, "sink");
684
685     GST_LOG ("removing sink %p", element);
686
687     peer = gst_pad_get_peer (pad);
688     if (peer) {
689       gst_pad_unlink (peer, pad);
690       gst_object_unref (peer);
691     }
692     gst_object_unref (pad);
693
694     gst_bin_remove (GST_BIN (play_bin), element);
695   }
696   g_list_free (play_bin->sinks);
697   play_bin->sinks = NULL;
698
699   /* FIXME: this is probably some refcounting problem */
700   if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
701     gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
702         play_bin->visualisation);
703     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
704   }
705
706   if (play_bin->frame) {
707     gst_buffer_unref (play_bin->frame);
708     play_bin->frame = NULL;
709   }
710
711   play_bin->textoverlay_element = NULL;
712 }
713
714 /* loop over the streams and set up the pipeline to play this
715  * media file. First we count the number of audio and video streams.
716  * If there is no video stream but there exists an audio stream,
717  * we install a visualisation pipeline.
718  * 
719  * Also make sure to only connect the first audio and video pad. FIXME
720  * this should eventually be handled with a tuner interface so that
721  * one can switch the streams.
722  */
723 static gboolean
724 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
725 {
726   GstPad *sinkpad;
727   GstPadLinkReturn res;
728   GstElement *parent;
729
730   gst_bin_add (GST_BIN (play_bin), sink);
731
732   /* we found a sink for this stream, now try to install it */
733   sinkpad = gst_element_get_pad (sink, "sink");
734   res = gst_pad_link (srcpad, sinkpad);
735   gst_object_unref (sinkpad);
736
737   /* this is only for debugging */
738   parent = gst_pad_get_parent_element (srcpad);
739   if (parent) {
740     GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
741         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
742     gst_object_unref (parent);
743   }
744
745   /* try to link the pad of the sink to the stream */
746   if (res < 0) {
747     gchar *capsstr;
748
749     /* could not link this stream */
750     capsstr = gst_caps_to_string (gst_pad_get_caps (srcpad));
751     g_warning ("could not link %s: %d", capsstr, res);
752     g_free (capsstr);
753
754     gst_bin_remove (GST_BIN (play_bin), sink);
755   } else {
756     /* we got the sink succesfully linked, now keep the sink
757      * in out internal list */
758     play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
759     gst_element_set_state (sink,
760         (GST_STATE (play_bin) == GST_STATE_PLAYING) ?
761         GST_STATE_PLAYING : GST_STATE_PAUSED);
762   }
763
764   return res;
765 }
766
767 static void
768 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
769 {
770   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
771   GList *streaminfo = NULL, *s;
772   gboolean need_vis = FALSE;
773   gboolean need_text = FALSE;
774   GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
775   GstElement *sink;
776
777   /* get rid of existing sinks */
778   if (play_bin->sinks) {
779     remove_sinks (play_bin);
780   }
781   GST_DEBUG ("setupsinks");
782
783   /* find out what to do */
784   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
785       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
786     need_text = TRUE;
787   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
788       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
789       play_bin->visualisation != NULL) {
790     need_vis = TRUE;
791   }
792
793   /* now actually connect everything */
794   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
795   for (s = streaminfo; s; s = g_list_next (s)) {
796     GObject *obj = G_OBJECT (s->data);
797     gint type;
798     GstObject *object;
799
800     g_object_get (obj, "type", &type, NULL);
801     g_object_get (obj, "object", &object, NULL);
802   }
803
804   /* link audio */
805   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
806     if (need_vis) {
807       sink = gen_vis_element (play_bin);
808     } else {
809       sink = gen_audio_element (play_bin);
810     }
811     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
812         "src");
813     add_sink (play_bin, sink, pad);
814     gst_object_unref (pad);
815   }
816
817   /* link video */
818   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
819     if (need_text) {
820       sink = gen_text_element (play_bin);
821
822       textsinkpad = gst_element_get_pad (sink, "text_sink");
823       textsrcpad =
824           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
825           "src");
826       gst_pad_link (textsrcpad, textsinkpad);
827       gst_object_unref (textsinkpad);
828       gst_object_unref (textsrcpad);
829     } else {
830       sink = gen_video_element (play_bin);
831     }
832     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
833         "src");
834     add_sink (play_bin, sink, pad);
835     gst_object_unref (pad);
836   }
837
838   /* remove the sinks now, pipeline get_state will now wait for the
839    * sinks to preroll */
840   if (play_bin->fakesink) {
841     gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
842     play_bin->fakesink = NULL;
843   }
844 }
845
846 static GstStateChangeReturn
847 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
848 {
849   GstStateChangeReturn ret;
850   GstPlayBin *play_bin;
851
852   play_bin = GST_PLAY_BIN (element);
853
854
855   switch (transition) {
856     case GST_STATE_CHANGE_READY_TO_PAUSED:
857       /* this really is the easiest way to make the state change return
858        * ASYNC until we added the sinks */
859       if (!play_bin->fakesink) {
860         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
861         gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
862       }
863       break;
864     default:
865       break;
866   }
867
868   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
869   if (ret == GST_STATE_CHANGE_FAILURE)
870     return ret;
871
872   switch (transition) {
873     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
874       /* Set audio sink state to NULL to release the sound device,
875        * but only if we own it (else we might be in chain-transition). */
876       //if (play_bin->audio_sink != NULL &&
877       //    GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
878       //  gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
879       //}
880       break;
881     case GST_STATE_CHANGE_PAUSED_TO_READY:
882       /* Check for NULL because the state transition may be done by
883        * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
884        * case, we don't want to run remove_sinks.
885        * FIXME: should the NULL test be done in remove_sinks? Should we just
886        * set the state to NULL in gst_play_bin_dispose?
887        */
888       if (play_bin->cache != NULL) {
889         remove_sinks (play_bin);
890       }
891       if (play_bin->fakesink) {
892         gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
893         play_bin->fakesink = NULL;
894       }
895       break;
896     default:
897       break;
898   }
899
900   return ret;
901 }
902
903 static gboolean
904 plugin_init (GstPlugin * plugin)
905 {
906   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
907
908   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
909       GST_TYPE_PLAY_BIN);
910 }
911
912 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
913     GST_VERSION_MINOR,
914     "playbin",
915     "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)