gst-libs/gst/audio/gstringbuffer.c: Don't assert on normal stuff.
[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_element_set_state (element, GST_STATE_NULL);
657       gst_bin_remove (GST_BIN (parent), element);
658       gst_object_unref (parent);
659     }
660     pad = gst_element_get_pad (element, "sink");
661     if (pad != NULL) {
662       peer = gst_pad_get_peer (pad);
663       if (peer != NULL) {
664         gst_pad_unlink (peer, pad);
665         gst_object_unref (peer);
666       }
667       gst_object_unref (pad);
668     }
669   }
670   element = g_hash_table_lookup (play_bin->cache, "vbin");
671   if (element != NULL) {
672     parent = gst_element_get_parent (element);
673     if (parent != NULL) {
674       play_bin->sinks = g_list_remove (play_bin->sinks, element);
675       gst_element_set_state (element, GST_STATE_NULL);
676       gst_bin_remove (GST_BIN (parent), element);
677       gst_object_unref (parent);
678     }
679     pad = gst_element_get_pad (element, "sink");
680     if (pad != NULL) {
681       peer = gst_pad_get_peer (pad);
682       if (peer != NULL) {
683         gst_pad_unlink (peer, pad);
684         gst_object_unref (peer);
685       }
686       gst_object_unref (pad);
687     }
688   }
689
690   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
691     GstElement *element = GST_ELEMENT (sinks->data);
692     GstPad *pad;
693     GstPad *peer;
694
695     pad = gst_element_get_pad (element, "sink");
696
697     GST_LOG ("removing sink %p", element);
698
699     peer = gst_pad_get_peer (pad);
700     if (peer) {
701       gst_pad_unlink (peer, pad);
702       gst_object_unref (peer);
703     }
704     gst_object_unref (pad);
705
706     gst_element_set_state (element, GST_STATE_NULL);
707     gst_bin_remove (GST_BIN (play_bin), element);
708   }
709   g_list_free (play_bin->sinks);
710   play_bin->sinks = NULL;
711
712   /* FIXME: this is probably some refcounting problem */
713   if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
714     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
715     gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
716         play_bin->visualisation);
717   }
718
719   if (play_bin->frame) {
720     gst_buffer_unref (play_bin->frame);
721     play_bin->frame = NULL;
722   }
723
724   play_bin->textoverlay_element = NULL;
725 }
726
727 /* loop over the streams and set up the pipeline to play this
728  * media file. First we count the number of audio and video streams.
729  * If there is no video stream but there exists an audio stream,
730  * we install a visualisation pipeline.
731  * 
732  * Also make sure to only connect the first audio and video pad. FIXME
733  * this should eventually be handled with a tuner interface so that
734  * one can switch the streams.
735  */
736 static gboolean
737 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
738 {
739   GstPad *sinkpad;
740   GstPadLinkReturn res;
741   GstElement *parent;
742
743   gst_bin_add (GST_BIN (play_bin), sink);
744
745   /* we found a sink for this stream, now try to install it */
746   sinkpad = gst_element_get_pad (sink, "sink");
747   res = gst_pad_link (srcpad, sinkpad);
748   gst_object_unref (sinkpad);
749
750   /* this is only for debugging */
751   parent = gst_pad_get_parent_element (srcpad);
752   if (parent) {
753     GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
754         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
755     gst_object_unref (parent);
756   }
757
758   /* try to link the pad of the sink to the stream */
759   if (res < 0) {
760     gchar *capsstr;
761
762     /* could not link this stream */
763     capsstr = gst_caps_to_string (gst_pad_get_caps (srcpad));
764     g_warning ("could not link %s: %d", capsstr, res);
765     g_free (capsstr);
766
767     gst_element_set_state (sink, GST_STATE_NULL);
768     gst_bin_remove (GST_BIN (play_bin), sink);
769   } else {
770     /* we got the sink succesfully linked, now keep the sink
771      * in out internal list */
772     play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
773     gst_element_set_state (sink,
774         (GST_STATE (play_bin) == GST_STATE_PLAYING) ?
775         GST_STATE_PLAYING : GST_STATE_PAUSED);
776   }
777
778   return res;
779 }
780
781 static void
782 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
783 {
784   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
785   GList *streaminfo = NULL, *s;
786   gboolean need_vis = FALSE;
787   gboolean need_text = FALSE;
788   GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
789   GstElement *sink;
790
791   /* get rid of existing sinks */
792   if (play_bin->sinks) {
793     remove_sinks (play_bin);
794   }
795   GST_DEBUG ("setupsinks");
796
797   /* find out what to do */
798   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
799       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
800     need_text = TRUE;
801   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
802       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
803       play_bin->visualisation != NULL) {
804     need_vis = TRUE;
805   }
806
807   /* now actually connect everything */
808   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
809   for (s = streaminfo; s; s = g_list_next (s)) {
810     GObject *obj = G_OBJECT (s->data);
811     gint type;
812     GstObject *object;
813
814     g_object_get (obj, "type", &type, NULL);
815     g_object_get (obj, "object", &object, NULL);
816   }
817
818   /* link audio */
819   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
820     if (need_vis) {
821       sink = gen_vis_element (play_bin);
822     } else {
823       sink = gen_audio_element (play_bin);
824     }
825     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
826         "src");
827     add_sink (play_bin, sink, pad);
828     gst_object_unref (pad);
829   }
830
831   /* link video */
832   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
833     if (need_text) {
834       sink = gen_text_element (play_bin);
835
836       textsinkpad = gst_element_get_pad (sink, "text_sink");
837       textsrcpad =
838           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
839           "src");
840       gst_pad_link (textsrcpad, textsinkpad);
841       gst_object_unref (textsinkpad);
842       gst_object_unref (textsrcpad);
843     } else {
844       sink = gen_video_element (play_bin);
845     }
846     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
847         "src");
848     add_sink (play_bin, sink, pad);
849     gst_object_unref (pad);
850   }
851
852   /* remove the sinks now, pipeline get_state will now wait for the
853    * sinks to preroll */
854   if (play_bin->fakesink) {
855     gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
856     gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
857     play_bin->fakesink = NULL;
858   }
859 }
860
861 /* Send an event to our sinks until one of them works; don't then send to the
862  * remaining sinks (unlike GstBin)
863  */
864 static gboolean
865 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
866 {
867   GList *sinks = play_bin->sinks;
868   gboolean res = TRUE;
869
870   while (sinks) {
871     GstElement *sink = GST_ELEMENT_CAST (sinks->data);
872
873     gst_event_ref (event);
874     if ((res = gst_element_send_event (sink, event)))
875       break;
876
877     sinks = g_list_next (sinks);
878   }
879
880   gst_event_unref (event);
881
882   return res;
883 }
884
885 static gboolean
886 do_playbin_seek (GstElement * element, GstEvent * event)
887 {
888   gdouble rate;
889   GstSeekFlags flags;
890   gboolean flush;
891   gboolean was_playing = FALSE;
892   gboolean res;
893
894   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
895
896   flush = flags & GST_SEEK_FLAG_FLUSH;
897
898   if (flush) {
899     GstState state;
900
901     /* need to call _get_state() since a bin state is only updated
902      * with this call. */
903     gst_element_get_state (element, &state, NULL, 0);
904     was_playing = state == GST_STATE_PLAYING;
905
906     if (was_playing) {
907       gst_element_set_state (element, GST_STATE_PAUSED);
908     }
909   }
910
911   res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
912
913   if (flush && res) {
914     /* need to reset the stream time to 0 after a flushing seek */
915     gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
916     if (was_playing)
917       /* and continue playing */
918       gst_element_set_state (element, GST_STATE_PLAYING);
919   }
920   return res;
921 }
922
923 /* We only want to send the event to a single sink (overriding GstBin's 
924  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
925  * events appropriately. So, this is a messy duplication of code. */
926 static gboolean
927 gst_play_bin_send_event (GstElement * element, GstEvent * event)
928 {
929   gboolean res = FALSE;
930   GstEventType event_type = GST_EVENT_TYPE (event);
931
932
933   switch (event_type) {
934     case GST_EVENT_SEEK:
935       res = do_playbin_seek (element, event);
936       break;
937     default:
938       res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
939       break;
940   }
941
942   return res;
943 }
944
945 static GstStateChangeReturn
946 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
947 {
948   GstStateChangeReturn ret;
949   GstPlayBin *play_bin;
950
951   play_bin = GST_PLAY_BIN (element);
952
953
954   switch (transition) {
955     case GST_STATE_CHANGE_READY_TO_PAUSED:
956       /* this really is the easiest way to make the state change return
957        * ASYNC until we added the sinks */
958       if (!play_bin->fakesink) {
959         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
960         gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
961       }
962       break;
963     default:
964       break;
965   }
966
967   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
968   if (ret == GST_STATE_CHANGE_FAILURE)
969     return ret;
970
971   switch (transition) {
972     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
973       /* Set audio sink state to NULL to release the sound device,
974        * but only if we own it (else we might be in chain-transition). */
975       //if (play_bin->audio_sink != NULL &&
976       //    GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
977       //  gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
978       //}
979       break;
980     case GST_STATE_CHANGE_PAUSED_TO_READY:
981       /* Check for NULL because the state transition may be done by
982        * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
983        * case, we don't want to run remove_sinks.
984        * FIXME: should the NULL test be done in remove_sinks? Should we just
985        * set the state to NULL in gst_play_bin_dispose?
986        */
987       if (play_bin->cache != NULL) {
988         remove_sinks (play_bin);
989       }
990       if (play_bin->fakesink) {
991         gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
992         gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
993         play_bin->fakesink = NULL;
994       }
995       break;
996     default:
997       break;
998   }
999
1000   return ret;
1001 }
1002
1003 static gboolean
1004 plugin_init (GstPlugin * plugin)
1005 {
1006   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1007
1008   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1009       GST_TYPE_PLAY_BIN);
1010 }
1011
1012 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1013     GST_VERSION_MINOR,
1014     "playbin",
1015     "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)