gst/playback/gstplaybin.c: first try autovideosink, then xvimagesink, then error out
[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 void 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_object_unref (play_bin->audio_sink);
234     play_bin->audio_sink = NULL;
235   }
236   if (play_bin->video_sink != NULL) {
237     gst_object_unref (play_bin->video_sink);
238     play_bin->video_sink = NULL;
239   }
240   if (play_bin->visualisation != NULL) {
241     gst_object_unref (play_bin->visualisation);
242     play_bin->visualisation = NULL;
243   }
244   g_free (play_bin->font_desc);
245   play_bin->font_desc = NULL;
246
247   G_OBJECT_CLASS (parent_class)->dispose (object);
248 }
249
250
251 static void
252 gst_play_bin_set_property (GObject * object, guint prop_id,
253     const GValue * value, GParamSpec * pspec)
254 {
255   GstPlayBin *play_bin;
256
257   g_return_if_fail (GST_IS_PLAY_BIN (object));
258
259   play_bin = GST_PLAY_BIN (object);
260
261   switch (prop_id) {
262     case ARG_VIDEO_SINK:
263       if (play_bin->video_sink != NULL) {
264         gst_object_unref (play_bin->video_sink);
265       }
266       play_bin->video_sink = g_value_get_object (value);
267       if (play_bin->video_sink != NULL) {
268         gst_object_ref (play_bin->video_sink);
269         gst_object_sink (GST_OBJECT (play_bin->video_sink));
270       }
271       /* when changing the videosink, we just remove the
272        * video pipeline from the cache so that it will be 
273        * regenerated with the new sink element */
274       g_hash_table_remove (play_bin->cache, "vbin");
275       break;
276     case ARG_AUDIO_SINK:
277       if (play_bin->audio_sink != NULL) {
278         gst_object_unref (play_bin->audio_sink);
279       }
280       play_bin->audio_sink = g_value_get_object (value);
281       if (play_bin->audio_sink != NULL) {
282         gst_object_ref (play_bin->audio_sink);
283         gst_object_sink (GST_OBJECT (play_bin->audio_sink));
284       }
285       g_hash_table_remove (play_bin->cache, "abin");
286       break;
287     case ARG_VIS_PLUGIN:
288       if (play_bin->visualisation != NULL) {
289         gst_object_unref (play_bin->visualisation);
290       }
291       play_bin->visualisation = g_value_get_object (value);
292       if (play_bin->visualisation != NULL) {
293         gst_object_ref (play_bin->visualisation);
294         gst_object_sink (GST_OBJECT (play_bin->visualisation));
295       }
296       break;
297     case ARG_VOLUME:
298       play_bin->volume = g_value_get_double (value);
299       if (play_bin->volume_element) {
300         g_object_set (G_OBJECT (play_bin->volume_element), "volume",
301             play_bin->volume, NULL);
302       }
303       break;
304     case ARG_FONT_DESC:
305       g_free (play_bin->font_desc);
306       play_bin->font_desc = g_strdup (g_value_get_string (value));
307       if (play_bin->textoverlay_element) {
308         g_object_set (G_OBJECT (play_bin->textoverlay_element),
309             "font-desc", g_value_get_string (value), NULL);
310       }
311       break;
312     default:
313       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314       break;
315   }
316 }
317
318 static void
319 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
320     GParamSpec * pspec)
321 {
322   GstPlayBin *play_bin;
323
324   g_return_if_fail (GST_IS_PLAY_BIN (object));
325
326   play_bin = GST_PLAY_BIN (object);
327
328   switch (prop_id) {
329     case ARG_VIDEO_SINK:
330       g_value_set_object (value, play_bin->video_sink);
331       break;
332     case ARG_AUDIO_SINK:
333       g_value_set_object (value, play_bin->audio_sink);
334       break;
335     case ARG_VIS_PLUGIN:
336       g_value_set_object (value, play_bin->visualisation);
337       break;
338     case ARG_VOLUME:
339       g_value_set_double (value, play_bin->volume);
340       break;
341     case ARG_FRAME:
342       gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
343       break;
344     default:
345       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346       break;
347   }
348 }
349
350 /* signal fired when the identity has received a new buffer. This is used for
351  * making screenshots.
352  */
353 static void
354 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
355 {
356   GstPlayBin *play_bin = GST_PLAY_BIN (data);
357
358   if (play_bin->frame) {
359     gst_buffer_unref (play_bin->frame);
360   }
361   play_bin->frame = gst_buffer_ref (frame);
362 }
363
364 /* make the element (bin) that contains the elements needed to perform
365  * video display. We connect a handoff signal to identity so that we
366  * can grab snapshots. Identity's sinkpad is ghosted to vbin.
367  *
368  *  +-------------------------------------------------------------+
369  *  | vbin                                                        |
370  *  |      +--------+   +----------+   +----------+   +---------+ |
371  *  |      |identity|   |colorspace|   |videoscale|   |videosink| |
372  *  |   +-sink     src-sink       src-sink       src-sink       | |
373  *  |   |  +---+----+   +----------+   +----------+   +---------+ |
374  * sink-+      |                                                  |
375  *  +----------|--------------------------------------------------+
376  *           handoff
377  */
378 /* FIXME: this might return NULL if no videosink was found, handle
379  * this in callers */
380 static GstElement *
381 gen_video_element (GstPlayBin * play_bin)
382 {
383   GstElement *element;
384   GstElement *conv;
385
386   GstElement *scale;
387   GstElement *sink;
388   GstElement *identity;
389   GstPad *pad;
390
391   /* first see if we have it in the cache */
392   element = g_hash_table_lookup (play_bin->cache, "vbin");
393   if (element != NULL) {
394     return element;
395   }
396
397   if (play_bin->video_sink) {
398     sink = play_bin->video_sink;
399   } else {
400     sink = gst_element_factory_make ("autovideosink", "videosink");
401     if (sink == NULL) {
402       sink = gst_element_factory_make ("xvimagesink", "videosink");
403     }
404     /* FIXME: this warrants adding a CORE error category for missing
405      * elements/plugins */
406     if (sink == NULL) {
407       GST_ELEMENT_ERROR (play_bin, CORE, FAILED,
408           (_("Both autovideosink and xvimagesink elements are missing.")),
409           (NULL));
410       return NULL;
411     }
412   }
413   gst_object_ref (sink);
414   g_hash_table_insert (play_bin->cache, "video_sink", sink);
415
416
417   element = gst_bin_new ("vbin");
418   identity = gst_element_factory_make ("identity", "id");
419   g_object_set (identity, "silent", TRUE, NULL);
420   g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
421   conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
422   scale = gst_element_factory_make ("videoscale", "vscale");
423   gst_bin_add (GST_BIN (element), identity);
424   gst_bin_add (GST_BIN (element), conv);
425   gst_bin_add (GST_BIN (element), scale);
426   gst_bin_add (GST_BIN (element), sink);
427   gst_element_link_pads (identity, "src", conv, "sink");
428   gst_element_link_pads (conv, "src", scale, "sink");
429   gst_element_link_pads (scale, "src", sink, "sink");
430
431   pad = gst_element_get_pad (identity, "sink");
432   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
433   gst_object_unref (pad);
434
435   gst_element_set_state (element, GST_STATE_READY);
436
437   /* since we're gonna add it to a bin but don't want to lose it,
438    * we keep a reference. */
439   gst_object_ref (element);
440   g_hash_table_insert (play_bin->cache, "vbin", element);
441
442   return element;
443 }
444
445 /* make an element for playback of video with subtitles embedded.
446  *
447  *  +--------------------------------------------------+
448  *  | tbin                  +-------------+            |
449  *  |          +-----+      | textoverlay |   +------+ |
450  *  |          | csp | +--video_sink      |   | vbin | |
451  * video_sink-sink  src+ +-text_sink     src-sink    | |
452  *  |          +-----+   |  +-------------+   +------+ |
453  * text_sink-------------+                             |
454  *  +--------------------------------------------------+
455  */
456
457 static GstElement *
458 gen_text_element (GstPlayBin * play_bin)
459 {
460   GstElement *element, *csp, *overlay, *vbin;
461   GstPad *pad;
462
463   overlay = gst_element_factory_make ("textoverlay", "overlay");
464   g_object_set (G_OBJECT (overlay),
465       "halign", "center", "valign", "bottom", NULL);
466   play_bin->textoverlay_element = overlay;
467   if (play_bin->font_desc) {
468     g_object_set (G_OBJECT (play_bin->textoverlay_element),
469         "font-desc", play_bin->font_desc, NULL);
470   }
471   vbin = gen_video_element (play_bin);
472   if (!overlay) {
473     g_warning ("No overlay (pango) element, subtitles disabled");
474     return vbin;
475   }
476   csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
477   element = gst_bin_new ("textbin");
478   gst_element_link_many (csp, overlay, vbin, NULL);
479   gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
480
481   pad = gst_element_get_pad (overlay, "text_sink");
482 #define gst_element_add_ghost_pad(element, pad, name) \
483     gst_element_add_pad (element, gst_ghost_pad_new (name, pad))
484   gst_element_add_ghost_pad (element, pad, "text_sink");
485   gst_object_unref (pad);
486
487   pad = gst_element_get_pad (csp, "sink");
488   gst_element_add_ghost_pad (element, pad, "sink");
489   gst_object_unref (pad);
490
491   return element;
492 }
493
494 /* make the element (bin) that contains the elements needed to perform
495  * audio playback. 
496  *
497  *  +-------------------------------------------------------------+
498  *  | abin                                                        |
499  *  |      +---------+   +----------+   +---------+   +---------+ |
500  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
501  *  |   +-sink      src-sink       src-sink      src-sink       | |
502  *  |   |  +---------+   +----------+   +---------+   +---------+ |
503  * sink-+                                                         |
504  *  +-------------------------------------------------------------+
505  *                  
506  */
507 static GstElement *
508 gen_audio_element (GstPlayBin * play_bin)
509 {
510   GstElement *element;
511   GstElement *conv;
512   GstElement *sink;
513   GstElement *volume;
514   GstElement *scale;
515   GstPad *pad;
516
517   element = g_hash_table_lookup (play_bin->cache, "abin");
518   if (element != NULL) {
519     return element;
520   }
521   element = gst_bin_new ("abin");
522   conv = gst_element_factory_make ("audioconvert", "aconv");
523   scale = gst_element_factory_make ("audioscale", "ascale");
524
525   volume = gst_element_factory_make ("volume", "volume");
526   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
527   play_bin->volume_element = volume;
528
529   if (play_bin->audio_sink) {
530     sink = play_bin->audio_sink;
531   } else {
532     sink = gst_element_factory_make ("alsasink", "audiosink");
533     if (sink == NULL) {
534       g_warning ("could not create autoaudiosink element");
535     }
536     play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
537   }
538
539   gst_object_ref (sink);
540   g_hash_table_insert (play_bin->cache, "audio_sink", sink);
541
542   gst_bin_add (GST_BIN (element), conv);
543   //gst_bin_add (GST_BIN (element), scale);
544   gst_bin_add (GST_BIN (element), volume);
545   gst_bin_add (GST_BIN (element), sink);
546
547   gst_element_link_pads (conv, "src",   /*scale, "sink");
548                                            gst_element_link_pads (scale, "src", */ volume, "sink");
549   gst_element_link_pads (volume, "src", sink, "sink");
550
551   pad = gst_element_get_pad (conv, "sink");
552   gst_element_add_ghost_pad (element, pad, "sink");
553   gst_object_unref (pad);
554
555   gst_element_set_state (element, GST_STATE_READY);
556
557   /* since we're gonna add it to a bin but don't want to lose it,
558    * we keep a reference. */
559   gst_object_ref (element);
560   g_hash_table_insert (play_bin->cache, "abin", element);
561
562   return element;
563 }
564
565 /* make the element (bin) that contains the elements needed to perform
566  * visualisation ouput.  The idea is to split the audio using tee, then 
567  * sending the output to the regular audio bin and the other output to
568  * the vis plugin that transforms it into a video that is rendered with the
569  * normal video bin. The video bin is run in a thread to make sure it does
570  * not block the audio playback pipeline.
571  *
572  *  +--------------------------------------------------------------------------+
573  *  | visbin                                                                   |
574  *  |      +------+   +----------------+                                       |
575  *  |      | tee  |   |   abin ...     |                                       |
576  *  |   +-sink   src-sink              |                                       |
577  *  |   |  |      |   +----------------+                 +-------------------+ |
578  *  |   |  |      |                                      | vthread           | |
579  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
580  *  |   |  |      |   |audioconv|   | vis  |   |vqueue|  | | vbin ...     |  | |
581  *  |   |  |     src-sink      src-sink   src-sink   src-sink             |  | |
582  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
583  *  |   |  |      |                                      +-------------------+ |
584  *  |   |  +------+                                                            |
585  * sink-+                                                                      |
586    +--------------------------------------------------------------------------+
587  */
588 static GstElement *
589 gen_vis_element (GstPlayBin * play_bin)
590 {
591   GstElement *element;
592   GstElement *tee;
593   GstElement *asink;
594   GstElement *vsink;
595   GstElement *conv;
596   GstElement *vis;
597   GstElement *vqueue, *aqueue;
598   GstPad *pad, *rpad;
599
600   element = gst_bin_new ("visbin");
601   tee = gst_element_factory_make ("tee", "tee");
602
603   vqueue = gst_element_factory_make ("queue", "vqueue");
604   aqueue = gst_element_factory_make ("queue", "aqueue");
605
606   asink = gen_audio_element (play_bin);
607   vsink = gen_video_element (play_bin);
608
609   gst_bin_add (GST_BIN (element), asink);
610   gst_bin_add (GST_BIN (element), vqueue);
611   gst_bin_add (GST_BIN (element), aqueue);
612   gst_bin_add (GST_BIN (element), vsink);
613   gst_bin_add (GST_BIN (element), tee);
614
615   conv = gst_element_factory_make ("audioconvert", "aconv");
616   if (play_bin->visualisation) {
617     gst_object_ref (play_bin->visualisation);
618     vis = play_bin->visualisation;
619   } else {
620     vis = gst_element_factory_make ("goom", "vis");
621   }
622
623   gst_bin_add (GST_BIN (element), conv);
624   gst_bin_add (GST_BIN (element), vis);
625
626   gst_element_link_pads (conv, "src", vis, "sink");
627   gst_element_link_pads (vis, "src", vqueue, "sink");
628
629   gst_element_link_pads (vqueue, "src", vsink, "sink");
630
631   pad = gst_element_get_pad (aqueue, "sink");
632   rpad = gst_element_get_request_pad (tee, "src%d");
633   gst_pad_link (rpad, pad);
634   gst_object_unref (rpad);
635   gst_object_unref (pad);
636   gst_element_link_pads (aqueue, "src", asink, "sink");
637
638   pad = gst_element_get_pad (conv, "sink");
639   rpad = gst_element_get_request_pad (tee, "src%d");
640   gst_pad_link (rpad, pad);
641   gst_object_unref (rpad);
642   gst_object_unref (pad);
643
644   pad = gst_element_get_pad (tee, "sink");
645   gst_element_add_ghost_pad (element, pad, "sink");
646   gst_object_unref (pad);
647
648   return element;
649 }
650
651 /* get rid of all installed sinks */
652 static void
653 remove_sinks (GstPlayBin * play_bin)
654 {
655   GList *sinks;
656   GstObject *parent;
657   GstElement *element;
658   GstPad *pad, *peer;
659
660   GST_DEBUG ("removesinks");
661   element = g_hash_table_lookup (play_bin->cache, "abin");
662   if (element != NULL) {
663     parent = gst_element_get_parent (element);
664     if (parent != NULL) {
665       /* we remove the element from the parent so that
666        * there is no unwanted state change when the parent
667        * is disposed */
668       play_bin->sinks = g_list_remove (play_bin->sinks, element);
669       gst_element_set_state (element, GST_STATE_NULL);
670       gst_bin_remove (GST_BIN (parent), element);
671       gst_object_unref (parent);
672     }
673     pad = gst_element_get_pad (element, "sink");
674     if (pad != NULL) {
675       peer = gst_pad_get_peer (pad);
676       if (peer != NULL) {
677         gst_pad_unlink (peer, pad);
678         gst_object_unref (peer);
679       }
680       gst_object_unref (pad);
681     }
682   }
683   element = g_hash_table_lookup (play_bin->cache, "vbin");
684   if (element != NULL) {
685     parent = gst_element_get_parent (element);
686     if (parent != NULL) {
687       play_bin->sinks = g_list_remove (play_bin->sinks, element);
688       gst_element_set_state (element, GST_STATE_NULL);
689       gst_bin_remove (GST_BIN (parent), element);
690       gst_object_unref (parent);
691     }
692     pad = gst_element_get_pad (element, "sink");
693     if (pad != NULL) {
694       peer = gst_pad_get_peer (pad);
695       if (peer != NULL) {
696         gst_pad_unlink (peer, pad);
697         gst_object_unref (peer);
698       }
699       gst_object_unref (pad);
700     }
701   }
702
703   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
704     GstElement *element = GST_ELEMENT (sinks->data);
705     GstPad *pad;
706     GstPad *peer;
707
708     pad = gst_element_get_pad (element, "sink");
709
710     GST_LOG ("removing sink %p", element);
711
712     peer = gst_pad_get_peer (pad);
713     if (peer) {
714       gst_pad_unlink (peer, pad);
715       gst_object_unref (peer);
716     }
717     gst_object_unref (pad);
718
719     gst_element_set_state (element, GST_STATE_NULL);
720     gst_bin_remove (GST_BIN (play_bin), element);
721   }
722   g_list_free (play_bin->sinks);
723   play_bin->sinks = NULL;
724
725   /* FIXME: this is probably some refcounting problem */
726   if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
727     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
728     gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
729         play_bin->visualisation);
730   }
731
732   if (play_bin->frame) {
733     gst_buffer_unref (play_bin->frame);
734     play_bin->frame = NULL;
735   }
736
737   play_bin->textoverlay_element = NULL;
738 }
739
740 /* loop over the streams and set up the pipeline to play this
741  * media file. First we count the number of audio and video streams.
742  * If there is no video stream but there exists an audio stream,
743  * we install a visualisation pipeline.
744  * 
745  * Also make sure to only connect the first audio and video pad. FIXME
746  * this should eventually be handled with a tuner interface so that
747  * one can switch the streams.
748  */
749 static gboolean
750 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
751 {
752   GstPad *sinkpad;
753   GstPadLinkReturn res;
754   GstElement *parent;
755
756   gst_bin_add (GST_BIN (play_bin), sink);
757
758   /* we found a sink for this stream, now try to install it */
759   sinkpad = gst_element_get_pad (sink, "sink");
760   res = gst_pad_link (srcpad, sinkpad);
761   gst_object_unref (sinkpad);
762
763   /* this is only for debugging */
764   parent = gst_pad_get_parent_element (srcpad);
765   if (parent) {
766     GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
767         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
768     gst_object_unref (parent);
769   }
770
771   /* try to link the pad of the sink to the stream */
772   if (res < 0) {
773     gchar *capsstr;
774
775     /* could not link this stream */
776     capsstr = gst_caps_to_string (gst_pad_get_caps (srcpad));
777     g_warning ("could not link %s: %d", capsstr, res);
778     g_free (capsstr);
779
780     gst_element_set_state (sink, GST_STATE_NULL);
781     gst_bin_remove (GST_BIN (play_bin), sink);
782   } else {
783     /* we got the sink succesfully linked, now keep the sink
784      * in out internal list */
785     play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
786     gst_element_set_state (sink,
787         (GST_STATE (play_bin) == GST_STATE_PLAYING) ?
788         GST_STATE_PLAYING : GST_STATE_PAUSED);
789   }
790
791   return res;
792 }
793
794 static void
795 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
796 {
797   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
798   GList *streaminfo = NULL, *s;
799   gboolean need_vis = FALSE;
800   gboolean need_text = FALSE;
801   GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
802   GstElement *sink;
803
804   /* get rid of existing sinks */
805   if (play_bin->sinks) {
806     remove_sinks (play_bin);
807   }
808   GST_DEBUG ("setupsinks");
809
810   /* find out what to do */
811   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
812       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
813     need_text = TRUE;
814   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
815       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
816       play_bin->visualisation != NULL) {
817     need_vis = TRUE;
818   }
819
820   /* now actually connect everything */
821   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
822   for (s = streaminfo; s; s = g_list_next (s)) {
823     GObject *obj = G_OBJECT (s->data);
824     gint type;
825     GstObject *object;
826
827     g_object_get (obj, "type", &type, NULL);
828     g_object_get (obj, "object", &object, NULL);
829   }
830
831   /* link audio */
832   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
833     if (need_vis) {
834       sink = gen_vis_element (play_bin);
835     } else {
836       sink = gen_audio_element (play_bin);
837     }
838     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
839         "src");
840     add_sink (play_bin, sink, pad);
841     gst_object_unref (pad);
842   }
843
844   /* link video */
845   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
846     if (need_text) {
847       sink = gen_text_element (play_bin);
848
849       textsinkpad = gst_element_get_pad (sink, "text_sink");
850       textsrcpad =
851           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
852           "src");
853       gst_pad_link (textsrcpad, textsinkpad);
854       gst_object_unref (textsinkpad);
855       gst_object_unref (textsrcpad);
856     } else {
857       sink = gen_video_element (play_bin);
858     }
859     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
860         "src");
861     add_sink (play_bin, sink, pad);
862     gst_object_unref (pad);
863   }
864
865   /* remove the sinks now, pipeline get_state will now wait for the
866    * sinks to preroll */
867   if (play_bin->fakesink) {
868     gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
869     gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
870     play_bin->fakesink = NULL;
871   }
872 }
873
874 /* Send an event to our sinks until one of them works; don't then send to the
875  * remaining sinks (unlike GstBin)
876  */
877 static gboolean
878 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
879 {
880   GList *sinks = play_bin->sinks;
881   gboolean res = TRUE;
882
883   while (sinks) {
884     GstElement *sink = GST_ELEMENT_CAST (sinks->data);
885
886     gst_event_ref (event);
887     if ((res = gst_element_send_event (sink, event)))
888       break;
889
890     sinks = g_list_next (sinks);
891   }
892
893   gst_event_unref (event);
894
895   return res;
896 }
897
898 static gboolean
899 do_playbin_seek (GstElement * element, GstEvent * event)
900 {
901   gdouble rate;
902   GstSeekFlags flags;
903   gboolean flush;
904   gboolean was_playing = FALSE;
905   gboolean res;
906
907   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
908
909   flush = flags & GST_SEEK_FLAG_FLUSH;
910
911   if (flush) {
912     GstState state;
913
914     /* need to call _get_state() since a bin state is only updated
915      * with this call. */
916     gst_element_get_state (element, &state, NULL, 0);
917     was_playing = state == GST_STATE_PLAYING;
918
919     if (was_playing) {
920       gst_element_set_state (element, GST_STATE_PAUSED);
921     }
922   }
923
924   res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
925
926   if (flush && res) {
927     /* need to reset the stream time to 0 after a flushing seek */
928     gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
929     if (was_playing)
930       /* and continue playing */
931       gst_element_set_state (element, GST_STATE_PLAYING);
932   }
933   return res;
934 }
935
936 /* We only want to send the event to a single sink (overriding GstBin's 
937  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
938  * events appropriately. So, this is a messy duplication of code. */
939 static gboolean
940 gst_play_bin_send_event (GstElement * element, GstEvent * event)
941 {
942   gboolean res = FALSE;
943   GstEventType event_type = GST_EVENT_TYPE (event);
944
945
946   switch (event_type) {
947     case GST_EVENT_SEEK:
948       res = do_playbin_seek (element, event);
949       break;
950     default:
951       res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
952       break;
953   }
954
955   return res;
956 }
957
958 static GstStateChangeReturn
959 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
960 {
961   GstStateChangeReturn ret;
962   GstPlayBin *play_bin;
963
964   play_bin = GST_PLAY_BIN (element);
965
966
967   switch (transition) {
968     case GST_STATE_CHANGE_READY_TO_PAUSED:
969       /* this really is the easiest way to make the state change return
970        * ASYNC until we added the sinks */
971       if (!play_bin->fakesink) {
972         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
973         gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
974       }
975       break;
976     default:
977       break;
978   }
979
980   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
981   if (ret == GST_STATE_CHANGE_FAILURE)
982     return ret;
983
984   switch (transition) {
985     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
986       /* Set audio sink state to NULL to release the sound device,
987        * but only if we own it (else we might be in chain-transition). */
988       //if (play_bin->audio_sink != NULL &&
989       //    GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
990       //  gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
991       //}
992       break;
993     case GST_STATE_CHANGE_PAUSED_TO_READY:
994       /* Check for NULL because the state transition may be done by
995        * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
996        * case, we don't want to run remove_sinks.
997        * FIXME: should the NULL test be done in remove_sinks? Should we just
998        * set the state to NULL in gst_play_bin_dispose?
999        */
1000       if (play_bin->cache != NULL) {
1001         remove_sinks (play_bin);
1002       }
1003       if (play_bin->fakesink) {
1004         gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1005         gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1006         play_bin->fakesink = NULL;
1007       }
1008       break;
1009     default:
1010       break;
1011   }
1012
1013   return ret;
1014 }
1015
1016 static gboolean
1017 plugin_init (GstPlugin * plugin)
1018 {
1019   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1020
1021   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1022       GST_TYPE_PLAY_BIN);
1023 }
1024
1025 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1026     GST_VERSION_MINOR,
1027     "playbin",
1028     "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1029     GST_PACKAGE_ORIGIN)