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