gst/playback/gstdecodebin.c: free plugin list correctly
[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     if (sink == NULL) {
400       g_warning ("could not create autovideosink element");
401     }
402   }
403   gst_object_ref (sink);
404   g_hash_table_insert (play_bin->cache, "video_sink", sink);
405
406   gst_bin_add (GST_BIN (element), identity);
407   gst_bin_add (GST_BIN (element), conv);
408   gst_bin_add (GST_BIN (element), scale);
409   gst_bin_add (GST_BIN (element), sink);
410   gst_element_link_pads (identity, "src", conv, "sink");
411   gst_element_link_pads (conv, "src", scale, "sink");
412   gst_element_link_pads (scale, "src", sink, "sink");
413
414   pad = gst_element_get_pad (identity, "sink");
415   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
416   gst_object_unref (pad);
417
418   gst_element_set_state (element, GST_STATE_READY);
419
420   /* since we're gonna add it to a bin but don't want to lose it,
421    * we keep a reference. */
422   gst_object_ref (element);
423   g_hash_table_insert (play_bin->cache, "vbin", element);
424
425   return element;
426 }
427
428 /* make an element for playback of video with subtitles embedded.
429  *
430  *  +--------------------------------------------------+
431  *  | tbin                  +-------------+            |
432  *  |          +-----+      | textoverlay |   +------+ |
433  *  |          | csp | +--video_sink      |   | vbin | |
434  * video_sink-sink  src+ +-text_sink     src-sink    | |
435  *  |          +-----+   |  +-------------+   +------+ |
436  * text_sink-------------+                             |
437  *  +--------------------------------------------------+
438  */
439
440 static GstElement *
441 gen_text_element (GstPlayBin * play_bin)
442 {
443   GstElement *element, *csp, *overlay, *vbin;
444   GstPad *pad;
445
446   overlay = gst_element_factory_make ("textoverlay", "overlay");
447   g_object_set (G_OBJECT (overlay),
448       "halign", "center", "valign", "bottom", NULL);
449   play_bin->textoverlay_element = overlay;
450   if (play_bin->font_desc) {
451     g_object_set (G_OBJECT (play_bin->textoverlay_element),
452         "font-desc", play_bin->font_desc, NULL);
453   }
454   vbin = gen_video_element (play_bin);
455   if (!overlay) {
456     g_warning ("No overlay (pango) element, subtitles disabled");
457     return vbin;
458   }
459   csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
460   element = gst_bin_new ("textbin");
461   gst_element_link_many (csp, overlay, vbin, NULL);
462   gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
463
464   pad = gst_element_get_pad (overlay, "text_sink");
465 #define gst_element_add_ghost_pad(element, pad, name) \
466     gst_element_add_pad (element, gst_ghost_pad_new (name, pad))
467   gst_element_add_ghost_pad (element, pad, "text_sink");
468   gst_object_unref (pad);
469
470   pad = gst_element_get_pad (csp, "sink");
471   gst_element_add_ghost_pad (element, pad, "sink");
472   gst_object_unref (pad);
473
474   return element;
475 }
476
477 /* make the element (bin) that contains the elements needed to perform
478  * audio playback. 
479  *
480  *  +-------------------------------------------------------------+
481  *  | abin                                                        |
482  *  |      +---------+   +----------+   +---------+   +---------+ |
483  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
484  *  |   +-sink      src-sink       src-sink      src-sink       | |
485  *  |   |  +---------+   +----------+   +---------+   +---------+ |
486  * sink-+                                                         |
487  *  +-------------------------------------------------------------+
488  *                  
489  */
490 static GstElement *
491 gen_audio_element (GstPlayBin * play_bin)
492 {
493   GstElement *element;
494   GstElement *conv;
495   GstElement *sink;
496   GstElement *volume;
497   GstElement *scale;
498   GstPad *pad;
499
500   element = g_hash_table_lookup (play_bin->cache, "abin");
501   if (element != NULL) {
502     return element;
503   }
504   element = gst_bin_new ("abin");
505   conv = gst_element_factory_make ("audioconvert", "aconv");
506   scale = gst_element_factory_make ("audioscale", "ascale");
507
508   volume = gst_element_factory_make ("volume", "volume");
509   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
510   play_bin->volume_element = volume;
511
512   if (play_bin->audio_sink) {
513     sink = play_bin->audio_sink;
514   } else {
515     sink = gst_element_factory_make ("autoaudiosink", "audiosink");
516     if (sink == NULL) {
517       g_warning ("could not create autoaudiosink element");
518     }
519     play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
520   }
521
522   gst_object_ref (sink);
523   g_hash_table_insert (play_bin->cache, "audio_sink", sink);
524
525   gst_bin_add (GST_BIN (element), conv);
526   //gst_bin_add (GST_BIN (element), scale);
527   gst_bin_add (GST_BIN (element), volume);
528   gst_bin_add (GST_BIN (element), sink);
529
530   gst_element_link_pads (conv, "src",   /*scale, "sink");
531                                            gst_element_link_pads (scale, "src", */ volume, "sink");
532   gst_element_link_pads (volume, "src", sink, "sink");
533
534   pad = gst_element_get_pad (conv, "sink");
535   gst_element_add_ghost_pad (element, pad, "sink");
536   gst_object_unref (pad);
537
538   gst_element_set_state (element, GST_STATE_READY);
539
540   /* since we're gonna add it to a bin but don't want to lose it,
541    * we keep a reference. */
542   gst_object_ref (element);
543   g_hash_table_insert (play_bin->cache, "abin", element);
544
545   return element;
546 }
547
548 /* make the element (bin) that contains the elements needed to perform
549  * visualisation ouput.  The idea is to split the audio using tee, then 
550  * sending the output to the regular audio bin and the other output to
551  * the vis plugin that transforms it into a video that is rendered with the
552  * normal video bin. The video bin is run in a thread to make sure it does
553  * not block the audio playback pipeline.
554  *
555  *  +--------------------------------------------------------------------------+
556  *  | visbin                                                                   |
557  *  |      +------+   +----------------+                                       |
558  *  |      | tee  |   |   abin ...     |                                       |
559  *  |   +-sink   src-sink              |                                       |
560  *  |   |  |      |   +----------------+                 +-------------------+ |
561  *  |   |  |      |                                      | vthread           | |
562  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
563  *  |   |  |      |   |audioconv|   | vis  |   |vqueue|  | | vbin ...     |  | |
564  *  |   |  |     src-sink      src-sink   src-sink   src-sink             |  | |
565  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
566  *  |   |  |      |                                      +-------------------+ |
567  *  |   |  +------+                                                            |
568  * sink-+                                                                      |
569    +--------------------------------------------------------------------------+
570  */
571 static GstElement *
572 gen_vis_element (GstPlayBin * play_bin)
573 {
574   GstElement *element;
575   GstElement *tee;
576   GstElement *asink;
577   GstElement *vsink;
578   GstElement *conv;
579   GstElement *vis;
580   GstElement *vqueue, *aqueue;
581   GstPad *pad, *rpad;
582
583   element = gst_bin_new ("visbin");
584   tee = gst_element_factory_make ("tee", "tee");
585
586   vqueue = gst_element_factory_make ("queue", "vqueue");
587   aqueue = gst_element_factory_make ("queue", "aqueue");
588
589   asink = gen_audio_element (play_bin);
590   vsink = gen_video_element (play_bin);
591
592   gst_bin_add (GST_BIN (element), asink);
593   gst_bin_add (GST_BIN (element), vqueue);
594   gst_bin_add (GST_BIN (element), aqueue);
595   gst_bin_add (GST_BIN (element), vsink);
596   gst_bin_add (GST_BIN (element), tee);
597
598   conv = gst_element_factory_make ("audioconvert", "aconv");
599   if (play_bin->visualisation) {
600     gst_object_ref (play_bin->visualisation);
601     vis = play_bin->visualisation;
602   } else {
603     vis = gst_element_factory_make ("goom", "vis");
604   }
605
606   gst_bin_add (GST_BIN (element), conv);
607   gst_bin_add (GST_BIN (element), vis);
608
609   gst_element_link_pads (conv, "src", vis, "sink");
610   gst_element_link_pads (vis, "src", vqueue, "sink");
611
612   gst_element_link_pads (vqueue, "src", vsink, "sink");
613
614   pad = gst_element_get_pad (aqueue, "sink");
615   rpad = gst_element_get_request_pad (tee, "src%d");
616   gst_pad_link (rpad, pad);
617   gst_object_unref (rpad);
618   gst_object_unref (pad);
619   gst_element_link_pads (aqueue, "src", asink, "sink");
620
621   pad = gst_element_get_pad (conv, "sink");
622   rpad = gst_element_get_request_pad (tee, "src%d");
623   gst_pad_link (rpad, pad);
624   gst_object_unref (rpad);
625   gst_object_unref (pad);
626
627   pad = gst_element_get_pad (tee, "sink");
628   gst_element_add_ghost_pad (element, pad, "sink");
629   gst_object_unref (pad);
630
631   return element;
632 }
633
634 /* get rid of all installed sinks */
635 static void
636 remove_sinks (GstPlayBin * play_bin)
637 {
638   GList *sinks;
639   GstObject *parent;
640   GstElement *element;
641   GstPad *pad, *peer;
642
643   GST_DEBUG ("removesinks");
644   element = g_hash_table_lookup (play_bin->cache, "abin");
645   if (element != NULL) {
646     parent = gst_element_get_parent (element);
647     if (parent != NULL) {
648       /* we remove the element from the parent so that
649        * there is no unwanted state change when the parent
650        * is disposed */
651       play_bin->sinks = g_list_remove (play_bin->sinks, element);
652       gst_bin_remove (GST_BIN (parent), element);
653       gst_object_unref (parent);
654     }
655     pad = gst_element_get_pad (element, "sink");
656     if (pad != NULL) {
657       peer = gst_pad_get_peer (pad);
658       if (peer != NULL) {
659         gst_pad_unlink (peer, pad);
660         gst_object_unref (peer);
661       }
662       gst_object_unref (pad);
663     }
664   }
665   element = g_hash_table_lookup (play_bin->cache, "vbin");
666   if (element != NULL) {
667     parent = gst_element_get_parent (element);
668     if (parent != NULL) {
669       play_bin->sinks = g_list_remove (play_bin->sinks, element);
670       gst_bin_remove (GST_BIN (parent), element);
671       gst_object_unref (parent);
672     }
673     pad = gst_element_get_pad (element, "sink");
674     if (pad != NULL) {
675       peer = gst_pad_get_peer (pad);
676       if (peer != NULL) {
677         gst_pad_unlink (peer, pad);
678         gst_object_unref (peer);
679       }
680       gst_object_unref (pad);
681     }
682   }
683
684   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
685     GstElement *element = GST_ELEMENT (sinks->data);
686     GstPad *pad;
687     GstPad *peer;
688
689     pad = gst_element_get_pad (element, "sink");
690
691     GST_LOG ("removing sink %p", element);
692
693     peer = gst_pad_get_peer (pad);
694     if (peer) {
695       gst_pad_unlink (peer, pad);
696       gst_object_unref (peer);
697     }
698     gst_object_unref (pad);
699
700     gst_bin_remove (GST_BIN (play_bin), element);
701   }
702   g_list_free (play_bin->sinks);
703   play_bin->sinks = NULL;
704
705   /* FIXME: this is probably some refcounting problem */
706   if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
707     gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
708         play_bin->visualisation);
709     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
710   }
711
712   if (play_bin->frame) {
713     gst_buffer_unref (play_bin->frame);
714     play_bin->frame = NULL;
715   }
716
717   play_bin->textoverlay_element = NULL;
718 }
719
720 /* loop over the streams and set up the pipeline to play this
721  * media file. First we count the number of audio and video streams.
722  * If there is no video stream but there exists an audio stream,
723  * we install a visualisation pipeline.
724  * 
725  * Also make sure to only connect the first audio and video pad. FIXME
726  * this should eventually be handled with a tuner interface so that
727  * one can switch the streams.
728  */
729 static gboolean
730 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
731 {
732   GstPad *sinkpad;
733   GstPadLinkReturn res;
734   GstElement *parent;
735
736   gst_bin_add (GST_BIN (play_bin), sink);
737
738   /* we found a sink for this stream, now try to install it */
739   sinkpad = gst_element_get_pad (sink, "sink");
740   res = gst_pad_link (srcpad, sinkpad);
741   gst_object_unref (sinkpad);
742
743   /* this is only for debugging */
744   parent = gst_pad_get_parent_element (srcpad);
745   if (parent) {
746     GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
747         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
748     gst_object_unref (parent);
749   }
750
751   /* try to link the pad of the sink to the stream */
752   if (res < 0) {
753     gchar *capsstr;
754
755     /* could not link this stream */
756     capsstr = gst_caps_to_string (gst_pad_get_caps (srcpad));
757     g_warning ("could not link %s: %d", capsstr, res);
758     g_free (capsstr);
759
760     gst_bin_remove (GST_BIN (play_bin), sink);
761   } else {
762     /* we got the sink succesfully linked, now keep the sink
763      * in out internal list */
764     play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
765     gst_element_set_state (sink,
766         (GST_STATE (play_bin) == GST_STATE_PLAYING) ?
767         GST_STATE_PLAYING : GST_STATE_PAUSED);
768   }
769
770   return res;
771 }
772
773 static void
774 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
775 {
776   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
777   GList *streaminfo = NULL, *s;
778   gboolean need_vis = FALSE;
779   gboolean need_text = FALSE;
780   GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
781   GstElement *sink;
782
783   /* get rid of existing sinks */
784   if (play_bin->sinks) {
785     remove_sinks (play_bin);
786   }
787   GST_DEBUG ("setupsinks");
788
789   /* find out what to do */
790   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
791       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
792     need_text = TRUE;
793   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
794       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
795       play_bin->visualisation != NULL) {
796     need_vis = TRUE;
797   }
798
799   /* now actually connect everything */
800   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
801   for (s = streaminfo; s; s = g_list_next (s)) {
802     GObject *obj = G_OBJECT (s->data);
803     gint type;
804     GstObject *object;
805
806     g_object_get (obj, "type", &type, NULL);
807     g_object_get (obj, "object", &object, NULL);
808   }
809
810   /* link audio */
811   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
812     if (need_vis) {
813       sink = gen_vis_element (play_bin);
814     } else {
815       sink = gen_audio_element (play_bin);
816     }
817     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
818         "src");
819     add_sink (play_bin, sink, pad);
820     gst_object_unref (pad);
821   }
822
823   /* link video */
824   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
825     if (need_text) {
826       sink = gen_text_element (play_bin);
827
828       textsinkpad = gst_element_get_pad (sink, "text_sink");
829       textsrcpad =
830           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
831           "src");
832       gst_pad_link (textsrcpad, textsinkpad);
833       gst_object_unref (textsinkpad);
834       gst_object_unref (textsrcpad);
835     } else {
836       sink = gen_video_element (play_bin);
837     }
838     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
839         "src");
840     add_sink (play_bin, sink, pad);
841     gst_object_unref (pad);
842   }
843
844   /* remove the sinks now, pipeline get_state will now wait for the
845    * sinks to preroll */
846   if (play_bin->fakesink) {
847     gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
848     play_bin->fakesink = NULL;
849   }
850 }
851
852 static GstStateChangeReturn
853 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
854 {
855   GstStateChangeReturn ret;
856   GstPlayBin *play_bin;
857
858   play_bin = GST_PLAY_BIN (element);
859
860
861   switch (transition) {
862     case GST_STATE_CHANGE_READY_TO_PAUSED:
863       /* this really is the easiest way to make the state change return
864        * ASYNC until we added the sinks */
865       if (!play_bin->fakesink) {
866         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
867         gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
868       }
869       break;
870     default:
871       break;
872   }
873
874   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
875   if (ret == GST_STATE_CHANGE_FAILURE)
876     return ret;
877
878   switch (transition) {
879     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
880       /* Set audio sink state to NULL to release the sound device,
881        * but only if we own it (else we might be in chain-transition). */
882       //if (play_bin->audio_sink != NULL &&
883       //    GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
884       //  gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
885       //}
886       break;
887     case GST_STATE_CHANGE_PAUSED_TO_READY:
888       /* Check for NULL because the state transition may be done by
889        * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
890        * case, we don't want to run remove_sinks.
891        * FIXME: should the NULL test be done in remove_sinks? Should we just
892        * set the state to NULL in gst_play_bin_dispose?
893        */
894       if (play_bin->cache != NULL) {
895         remove_sinks (play_bin);
896       }
897       if (play_bin->fakesink) {
898         gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
899         play_bin->fakesink = NULL;
900       }
901       break;
902     default:
903       break;
904   }
905
906   return ret;
907 }
908
909 static gboolean
910 plugin_init (GstPlugin * plugin)
911 {
912   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
913
914   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
915       GST_TYPE_PLAY_BIN);
916 }
917
918 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
919     GST_VERSION_MINOR,
920     "playbin",
921     "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)