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