ext/: - a library should not call setlocale. see Libraries node in gettext manual
[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 #ifndef GST_HAVE_GLIB_2_8
43 #define _gst_gvalue_set_gstobject(gvalue,obj)  \
44       if (obj != NULL) {                       \
45         gst_object_ref (obj);                  \
46         g_value_set_object (gvalue, obj);      \
47         g_object_unref (obj);                  \
48       } else {                                 \
49         g_value_set_object (gvalue, NULL);     \
50       }
51 #else
52 #define _gst_gvalue_set_gstobject(gvalue,obj)  \
53       g_value_set_object (gvalue, obj);
54 #endif
55
56 typedef struct _GstPlayBin GstPlayBin;
57 typedef struct _GstPlayBinClass GstPlayBinClass;
58
59 struct _GstPlayBin
60 {
61   GstPlayBaseBin parent;
62
63   /* the configurable elements */
64   GstElement *fakesink;
65   GstElement *audio_sink;
66   GstElement *video_sink;
67   GstElement *visualisation;
68   GstElement *pending_visualisation;
69   GstElement *volume_element;
70   GstElement *textoverlay_element;
71   gfloat volume;
72
73   /* these are the currently active sinks */
74   GList *sinks;
75
76   /* the last captured frame for snapshots */
77   GstBuffer *frame;
78
79   /* our cache for the sinks */
80   GHashTable *cache;
81
82   /* font description */
83   gchar *font_desc;
84 };
85
86 struct _GstPlayBinClass
87 {
88   GstPlayBaseBinClass parent_class;
89 };
90
91 /* props */
92 enum
93 {
94   ARG_0,
95   ARG_AUDIO_SINK,
96   ARG_VIDEO_SINK,
97   ARG_VIS_PLUGIN,
98   ARG_VOLUME,
99   ARG_FRAME,
100   ARG_FONT_DESC
101 };
102
103 /* signals */
104 enum
105 {
106   LAST_SIGNAL
107 };
108
109 static void gst_play_bin_class_init (GstPlayBinClass * klass);
110 static void gst_play_bin_init (GstPlayBin * play_bin);
111 static void gst_play_bin_dispose (GObject * object);
112
113 static gboolean setup_sinks (GstPlayBaseBin * play_base_bin,
114     GstPlayBaseGroup * group);
115 static void remove_sinks (GstPlayBin * play_bin);
116
117 static void gst_play_bin_set_property (GObject * object, guint prop_id,
118     const GValue * value, GParamSpec * spec);
119 static void gst_play_bin_get_property (GObject * object, guint prop_id,
120     GValue * value, GParamSpec * spec);
121
122 static gboolean gst_play_bin_send_event (GstElement * element,
123     GstEvent * event);
124 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
125     GstStateChange transition);
126
127 static GstElementClass *parent_class;
128
129 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
130
131 static GstElementDetails gst_play_bin_details = {
132   "Player Bin",
133   "Generic/Bin/Player",
134   "Autoplug and play media from an uri",
135   "Wim Taymans <wim@fluendo.com>"
136 };
137
138 static GType
139 gst_play_bin_get_type (void)
140 {
141   static GType gst_play_bin_type = 0;
142
143   if (!gst_play_bin_type) {
144     static const GTypeInfo gst_play_bin_info = {
145       sizeof (GstPlayBinClass),
146       NULL,
147       NULL,
148       (GClassInitFunc) gst_play_bin_class_init,
149       NULL,
150       NULL,
151       sizeof (GstPlayBin),
152       0,
153       (GInstanceInitFunc) gst_play_bin_init,
154       NULL
155     };
156
157     gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
158         "GstPlayBin", &gst_play_bin_info, 0);
159   }
160
161   return gst_play_bin_type;
162 }
163
164 static void
165 gst_play_bin_class_init (GstPlayBinClass * klass)
166 {
167   GObjectClass *gobject_klass;
168   GstElementClass *gstelement_klass;
169   GstBinClass *gstbin_klass;
170   GstPlayBaseBinClass *playbasebin_klass;
171
172   gobject_klass = (GObjectClass *) klass;
173   gstelement_klass = (GstElementClass *) klass;
174   gstbin_klass = (GstBinClass *) klass;
175   playbasebin_klass = (GstPlayBaseBinClass *) klass;
176
177   parent_class = g_type_class_ref (gst_play_base_bin_get_type ());
178
179   gobject_klass->set_property = gst_play_bin_set_property;
180   gobject_klass->get_property = gst_play_bin_get_property;
181
182   g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
183       g_param_spec_object ("video-sink", "Video Sink",
184           "the video output element to use (NULL = default sink)",
185           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
186   g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
187       g_param_spec_object ("audio-sink", "Audio Sink",
188           "the audio output element to use (NULL = default sink)",
189           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
190   g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
191       g_param_spec_object ("vis-plugin", "Vis plugin",
192           "the visualization element to use (NULL = none)",
193           GST_TYPE_ELEMENT, G_PARAM_READWRITE));
194   g_object_class_install_property (gobject_klass, ARG_VOLUME,
195       g_param_spec_double ("volume", "volume", "volume",
196           0.0, VOLUME_MAX_DOUBLE, 1.0, G_PARAM_READWRITE));
197   g_object_class_install_property (gobject_klass, ARG_FRAME,
198       gst_param_spec_mini_object ("frame", "Frame",
199           "The last frame (NULL = no video available)",
200           GST_TYPE_BUFFER, G_PARAM_READABLE));
201   g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
202       g_param_spec_string ("subtitle-font-desc",
203           "Subtitle font description",
204           "Pango font description of font "
205           "to be used for subtitle rendering", NULL, G_PARAM_WRITABLE));
206
207   gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_dispose);
208
209   gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
210
211   gstelement_klass->change_state =
212       GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
213   gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
214
215   playbasebin_klass->setup_output_pads = setup_sinks;
216 }
217
218 static void
219 gst_play_bin_init (GstPlayBin * play_bin)
220 {
221   play_bin->video_sink = NULL;
222   play_bin->audio_sink = NULL;
223   play_bin->visualisation = NULL;
224   play_bin->pending_visualisation = NULL;
225   play_bin->volume_element = NULL;
226   play_bin->textoverlay_element = NULL;
227   play_bin->volume = 1.0;
228   play_bin->sinks = NULL;
229   play_bin->frame = NULL;
230   play_bin->font_desc = NULL;
231   play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
232       NULL, (GDestroyNotify) gst_object_unref);
233 }
234
235 static void
236 gst_play_bin_dispose (GObject * object)
237 {
238   GstPlayBin *play_bin;
239
240   play_bin = GST_PLAY_BIN (object);
241
242   if (play_bin->cache != NULL) {
243     remove_sinks (play_bin);
244     g_hash_table_destroy (play_bin->cache);
245     play_bin->cache = NULL;
246   }
247
248   if (play_bin->audio_sink != NULL) {
249     gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
250     gst_object_unref (play_bin->audio_sink);
251     play_bin->audio_sink = NULL;
252   }
253   if (play_bin->video_sink != NULL) {
254     gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
255     gst_object_unref (play_bin->video_sink);
256     play_bin->video_sink = NULL;
257   }
258   if (play_bin->visualisation != NULL) {
259     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
260     gst_object_unref (play_bin->visualisation);
261     play_bin->visualisation = NULL;
262   }
263   if (play_bin->pending_visualisation != NULL) {
264     gst_element_set_state (play_bin->pending_visualisation, GST_STATE_NULL);
265     gst_object_unref (play_bin->pending_visualisation);
266     play_bin->pending_visualisation = NULL;
267   }
268   g_free (play_bin->font_desc);
269   play_bin->font_desc = NULL;
270
271   G_OBJECT_CLASS (parent_class)->dispose (object);
272 }
273
274 static void
275 gst_play_bin_vis_unblocked (GstPad * tee_pad, gboolean blocked,
276     gpointer user_data)
277 {
278   /* Unblocked */
279 }
280
281 static void
282 gst_play_bin_vis_blocked (GstPad * tee_pad, gboolean blocked,
283     gpointer user_data)
284 {
285   GstPlayBin *play_bin = GST_PLAY_BIN (user_data);
286   GstBin *vis_bin = NULL;
287   GstPad *vis_sink_pad = NULL, *vis_src_pad = NULL, *vqueue_pad = NULL;
288   GstState bin_state;
289
290   /* We want to disable visualisation */
291   if (!GST_IS_ELEMENT (play_bin->pending_visualisation)) {
292     /* Set visualisation element to READY */
293     gst_element_set_state (play_bin->visualisation, GST_STATE_READY);
294     goto beach;
295   }
296
297   vis_bin =
298       GST_BIN (gst_object_get_parent (GST_OBJECT (play_bin->visualisation)));
299
300   if (!GST_IS_BIN (vis_bin) || !GST_IS_PAD (tee_pad)) {
301     goto beach;
302   }
303
304   vis_src_pad = gst_element_get_pad (play_bin->visualisation, "src");
305   vis_sink_pad = gst_pad_get_peer (tee_pad);
306
307   /* Can be fakesink */
308   if (GST_IS_PAD (vis_src_pad)) {
309     vqueue_pad = gst_pad_get_peer (vis_src_pad);
310   }
311
312   if (!GST_IS_PAD (vis_sink_pad)) {
313     goto beach;
314   }
315
316   /* Check the bin's state */
317   GST_OBJECT_LOCK (vis_bin);
318   bin_state = GST_STATE (vis_bin);
319   GST_OBJECT_UNLOCK (vis_bin);
320
321   /* Unlink */
322   gst_pad_unlink (tee_pad, vis_sink_pad);
323   gst_object_unref (vis_sink_pad);
324   vis_sink_pad = NULL;
325
326   if (GST_IS_PAD (vqueue_pad)) {
327     gst_pad_unlink (vis_src_pad, vqueue_pad);
328     gst_object_unref (vis_src_pad);
329     vis_src_pad = NULL;
330   }
331
332   /* Remove from vis_bin */
333   gst_bin_remove (vis_bin, play_bin->visualisation);
334   /* Set state to NULL */
335   gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
336   /* And loose our ref */
337   gst_object_unref (play_bin->visualisation);
338
339   if (play_bin->pending_visualisation) {
340     /* Ref this new visualisation element before adding to the bin */
341     gst_object_ref (play_bin->pending_visualisation);
342     /* Add the new one */
343     gst_bin_add (vis_bin, play_bin->pending_visualisation);
344     /* Synchronizing state */
345     gst_element_set_state (play_bin->pending_visualisation, bin_state);
346
347     vis_sink_pad = gst_element_get_pad (play_bin->pending_visualisation,
348         "sink");
349     vis_src_pad = gst_element_get_pad (play_bin->pending_visualisation, "src");
350
351     if (!GST_IS_PAD (vis_sink_pad) || !GST_IS_PAD (vis_src_pad)) {
352       goto beach;
353     }
354
355     /* Link */
356     gst_pad_link (tee_pad, vis_sink_pad);
357     gst_pad_link (vis_src_pad, vqueue_pad);
358   }
359
360   /* We are done */
361   gst_object_unref (play_bin->visualisation);
362   play_bin->visualisation = play_bin->pending_visualisation;
363   play_bin->pending_visualisation = NULL;
364
365 beach:
366   if (vis_sink_pad) {
367     gst_object_unref (vis_sink_pad);
368   }
369   if (vis_src_pad) {
370     gst_object_unref (vis_src_pad);
371   }
372   if (vqueue_pad) {
373     gst_object_unref (vqueue_pad);
374   }
375   if (vis_bin) {
376     gst_object_unref (vis_bin);
377   }
378
379   /* Unblock the pad */
380   gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_bin_vis_unblocked,
381       play_bin);
382 }
383
384 static void
385 gst_play_bin_set_property (GObject * object, guint prop_id,
386     const GValue * value, GParamSpec * pspec)
387 {
388   GstPlayBin *play_bin;
389
390   g_return_if_fail (GST_IS_PLAY_BIN (object));
391
392   play_bin = GST_PLAY_BIN (object);
393
394   switch (prop_id) {
395     case ARG_VIDEO_SINK:
396       if (play_bin->video_sink != NULL) {
397         gst_object_unref (play_bin->video_sink);
398       }
399       play_bin->video_sink = g_value_get_object (value);
400       if (play_bin->video_sink != NULL) {
401         gst_object_ref (play_bin->video_sink);
402         gst_object_sink (GST_OBJECT (play_bin->video_sink));
403       }
404       /* when changing the videosink, we just remove the
405        * video pipeline from the cache so that it will be 
406        * regenerated with the new sink element */
407       g_hash_table_remove (play_bin->cache, "vbin");
408       break;
409     case ARG_AUDIO_SINK:
410       if (play_bin->audio_sink != NULL) {
411         gst_object_unref (play_bin->audio_sink);
412       }
413       play_bin->audio_sink = g_value_get_object (value);
414       if (play_bin->audio_sink != NULL) {
415         gst_object_ref (play_bin->audio_sink);
416         gst_object_sink (GST_OBJECT (play_bin->audio_sink));
417       }
418       g_hash_table_remove (play_bin->cache, "abin");
419       break;
420     case ARG_VIS_PLUGIN:
421     {
422       /* Do we already have a visualisation change pending ? */
423       if (play_bin->pending_visualisation) {
424         gst_object_unref (play_bin->pending_visualisation);
425         play_bin->pending_visualisation = g_value_get_object (value);
426         /* Take ownership */
427         if (play_bin->pending_visualisation) {
428           gst_object_ref (play_bin->pending_visualisation);
429           gst_object_sink (GST_OBJECT (play_bin->pending_visualisation));
430         }
431       } else {
432         play_bin->pending_visualisation = g_value_get_object (value);
433
434         /* Take ownership */
435         if (play_bin->pending_visualisation) {
436           gst_object_ref (play_bin->pending_visualisation);
437           gst_object_sink (GST_OBJECT (play_bin->pending_visualisation));
438         }
439
440         /* Was there a visualisation already set ? */
441         if (play_bin->visualisation != NULL) {
442           GstBin *vis_bin = NULL;
443
444           vis_bin =
445               GST_BIN (gst_object_get_parent (GST_OBJECT (play_bin->
446                       visualisation)));
447
448           /* Check if the visualisation is already in a bin */
449           if (GST_IS_BIN (vis_bin)) {
450             GstPad *vis_sink_pad = NULL, *tee_pad = NULL;
451
452             /* Now get tee pad and block it async */
453             vis_sink_pad = gst_element_get_pad (play_bin->visualisation,
454                 "sink");
455             if (!GST_IS_PAD (vis_sink_pad)) {
456               goto beach;
457             }
458             tee_pad = gst_pad_get_peer (vis_sink_pad);
459             if (!GST_IS_PAD (tee_pad)) {
460               goto beach;
461             }
462
463             /* Block with callback */
464             gst_pad_set_blocked_async (tee_pad, TRUE, gst_play_bin_vis_blocked,
465                 play_bin);
466           beach:
467             if (vis_sink_pad) {
468               gst_object_unref (vis_sink_pad);
469             }
470             if (tee_pad) {
471               gst_object_unref (tee_pad);
472             }
473             gst_object_unref (vis_bin);
474           } else {
475             play_bin->visualisation = play_bin->pending_visualisation;
476             play_bin->pending_visualisation = NULL;
477           }
478         } else {
479           play_bin->visualisation = play_bin->pending_visualisation;
480           play_bin->pending_visualisation = NULL;
481         }
482       }
483       break;
484     }
485     case ARG_VOLUME:
486       play_bin->volume = g_value_get_double (value);
487       if (play_bin->volume_element) {
488         g_object_set (G_OBJECT (play_bin->volume_element), "volume",
489             play_bin->volume, NULL);
490       }
491       break;
492     case ARG_FONT_DESC:
493       g_free (play_bin->font_desc);
494       play_bin->font_desc = g_strdup (g_value_get_string (value));
495       if (play_bin->textoverlay_element) {
496         g_object_set (G_OBJECT (play_bin->textoverlay_element),
497             "font-desc", g_value_get_string (value), NULL);
498       }
499       break;
500     default:
501       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
502       break;
503   }
504 }
505
506 static void
507 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
508     GParamSpec * pspec)
509 {
510   GstPlayBin *play_bin;
511
512   g_return_if_fail (GST_IS_PLAY_BIN (object));
513
514   play_bin = GST_PLAY_BIN (object);
515
516   switch (prop_id) {
517     case ARG_VIDEO_SINK:
518       _gst_gvalue_set_gstobject (value, play_bin->video_sink);
519       break;
520     case ARG_AUDIO_SINK:
521       _gst_gvalue_set_gstobject (value, play_bin->audio_sink);
522       break;
523     case ARG_VIS_PLUGIN:
524       _gst_gvalue_set_gstobject (value, play_bin->visualisation);
525       break;
526     case ARG_VOLUME:
527       g_value_set_double (value, play_bin->volume);
528       break;
529     case ARG_FRAME:
530       gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
531       break;
532     default:
533       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534       break;
535   }
536 }
537
538 /* signal fired when the identity has received a new buffer. This is used for
539  * making screenshots.
540  */
541 static void
542 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
543 {
544   GstPlayBin *play_bin = GST_PLAY_BIN (data);
545
546   gst_mini_object_replace ((GstMiniObject **) & play_bin->frame,
547       GST_MINI_OBJECT_CAST (frame));
548
549   /* applications need to know the buffer caps,
550    * make sure they are always set on the frame */
551   if (GST_BUFFER_CAPS (play_bin->frame) == NULL) {
552     GstPad *pad;
553
554     if ((pad = gst_element_get_pad (identity, "sink"))) {
555       gst_buffer_set_caps (play_bin->frame, GST_PAD_CAPS (pad));
556       gst_object_unref (pad);
557     }
558   }
559 }
560
561 /* make the element (bin) that contains the elements needed to perform
562  * video display. We connect a handoff signal to identity so that we
563  * can grab snapshots. Identity's sinkpad is ghosted to vbin.
564  *
565  *  +-------------------------------------------------------------+
566  *  | vbin                                                        |
567  *  |      +--------+   +----------+   +----------+   +---------+ |
568  *  |      |identity|   |colorspace|   |videoscale|   |videosink| |
569  *  |   +-sink     src-sink       src-sink       src-sink       | |
570  *  |   |  +---+----+   +----------+   +----------+   +---------+ |
571  * sink-+      |                                                  |
572  *  +----------|--------------------------------------------------+
573  *           handoff
574  */
575 /* FIXME: this might return NULL if no videosink was found, handle
576  * this in callers */
577 static GstElement *
578 gen_video_element (GstPlayBin * play_bin)
579 {
580   GstElement *element;
581   GstElement *conv;
582
583   GstElement *scale;
584   GstElement *sink;
585   GstElement *identity;
586   GstPad *pad;
587
588   /* first see if we have it in the cache */
589   element = g_hash_table_lookup (play_bin->cache, "vbin");
590   if (element != NULL) {
591     return element;
592   }
593
594   if (play_bin->video_sink) {
595     sink = play_bin->video_sink;
596   } else {
597     sink = gst_element_factory_make ("autovideosink", "videosink");
598     if (sink == NULL) {
599       sink = gst_element_factory_make ("xvimagesink", "videosink");
600     }
601     /* FIXME: this warrants adding a CORE error category for missing
602      * elements/plugins */
603     if (sink == NULL) {
604       GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
605           (_("Both autovideosink and xvimagesink elements are missing.")),
606           (NULL));
607       return NULL;
608     }
609   }
610   gst_object_ref (sink);
611   g_hash_table_insert (play_bin->cache, "video_sink", sink);
612
613
614   element = gst_bin_new ("vbin");
615   identity = gst_element_factory_make ("identity", "id");
616   g_object_set (identity, "silent", TRUE, NULL);
617   g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
618   conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
619   scale = gst_element_factory_make ("videoscale", "vscale");
620   gst_bin_add (GST_BIN (element), identity);
621   gst_bin_add (GST_BIN (element), conv);
622   gst_bin_add (GST_BIN (element), scale);
623   gst_bin_add (GST_BIN (element), sink);
624   gst_element_link_pads (identity, "src", conv, "sink");
625   gst_element_link_pads (conv, "src", scale, "sink");
626   gst_element_link_pads (scale, "src", sink, "sink");
627
628   pad = gst_element_get_pad (identity, "sink");
629   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
630   gst_object_unref (pad);
631
632   gst_element_set_state (element, GST_STATE_READY);
633
634   /* since we're gonna add it to a bin but don't want to lose it,
635    * we keep a reference. */
636   gst_object_ref (element);
637   g_hash_table_insert (play_bin->cache, "vbin", element);
638
639   return element;
640 }
641
642 /* make an element for playback of video with subtitles embedded.
643  *
644  *  +--------------------------------------------------+
645  *  | tbin                  +-------------+            |
646  *  |          +-----+      | textoverlay |   +------+ |
647  *  |          | csp | +--video_sink      |   | vbin | |
648  * video_sink-sink  src+ +-text_sink     src-sink    | |
649  *  |          +-----+   |  +-------------+   +------+ |
650  * text_sink-------------+                             |
651  *  +--------------------------------------------------+
652  */
653
654 static GstElement *
655 gen_text_element (GstPlayBin * play_bin)
656 {
657   GstElement *element, *csp, *overlay, *vbin;
658   GstPad *pad;
659
660   overlay = gst_element_factory_make ("textoverlay", "overlay");
661   g_object_set (G_OBJECT (overlay),
662       "halign", "center", "valign", "bottom", NULL);
663   play_bin->textoverlay_element = overlay;
664   if (play_bin->font_desc) {
665     g_object_set (G_OBJECT (play_bin->textoverlay_element),
666         "font-desc", play_bin->font_desc, NULL);
667   }
668   vbin = gen_video_element (play_bin);
669   if (!overlay) {
670     g_warning ("No overlay (pango) element, subtitles disabled");
671     return vbin;
672   }
673   csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
674   element = gst_bin_new ("textbin");
675   gst_element_link_many (csp, overlay, vbin, NULL);
676   gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
677
678   pad = gst_element_get_pad (overlay, "text_sink");
679 #define gst_element_add_ghost_pad(element, pad, name) \
680     gst_element_add_pad (element, gst_ghost_pad_new (name, pad))
681   gst_element_add_ghost_pad (element, pad, "text_sink");
682   gst_object_unref (pad);
683
684   pad = gst_element_get_pad (csp, "sink");
685   gst_element_add_ghost_pad (element, pad, "sink");
686   gst_object_unref (pad);
687
688   return element;
689 }
690
691 /* make the element (bin) that contains the elements needed to perform
692  * audio playback.
693  *
694  *  +-------------------------------------------------------------+
695  *  | abin                                                        |
696  *  |      +---------+   +----------+   +---------+   +---------+ |
697  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
698  *  |   +-sink      src-sink       src-sink      src-sink       | |
699  *  |   |  +---------+   +----------+   +---------+   +---------+ |
700  * sink-+                                                         |
701  *  +-------------------------------------------------------------+
702  *
703  */
704 static GstElement *
705 gen_audio_element (GstPlayBin * play_bin)
706 {
707   GstElement *element;
708   GstElement *conv;
709   GstElement *sink;
710   GstElement *volume;
711   GstElement *scale;
712   GstPad *pad;
713
714   element = g_hash_table_lookup (play_bin->cache, "abin");
715   if (element != NULL) {
716     return element;
717   }
718   element = gst_bin_new ("abin");
719   conv = gst_element_factory_make ("audioconvert", "aconv");
720   scale = gst_element_factory_make ("audioscale", "ascale");
721
722   volume = gst_element_factory_make ("volume", "volume");
723   g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
724   play_bin->volume_element = volume;
725
726   if (play_bin->audio_sink) {
727     sink = play_bin->audio_sink;
728   } else {
729     sink = gst_element_factory_make ("autoaudiosink", "audiosink");
730     if (sink == NULL) {
731       sink = gst_element_factory_make ("alsasink", "audiosink");
732     }
733     if (sink == NULL) {
734       GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
735           (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
736       return NULL;
737     }
738     play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
739   }
740
741   gst_object_ref (sink);
742   g_hash_table_insert (play_bin->cache, "audio_sink", sink);
743
744   gst_bin_add (GST_BIN (element), conv);
745   //gst_bin_add (GST_BIN (element), scale);
746   gst_bin_add (GST_BIN (element), volume);
747   gst_bin_add (GST_BIN (element), sink);
748
749   gst_element_link_pads (conv, "src",   /*scale, "sink");
750                                            gst_element_link_pads (scale, "src", */ volume, "sink");
751   gst_element_link_pads (volume, "src", sink, "sink");
752
753   pad = gst_element_get_pad (conv, "sink");
754   gst_element_add_ghost_pad (element, pad, "sink");
755   gst_object_unref (pad);
756
757   gst_element_set_state (element, GST_STATE_READY);
758
759   /* since we're gonna add it to a bin but don't want to lose it,
760    * we keep a reference. */
761   gst_object_ref (element);
762   g_hash_table_insert (play_bin->cache, "abin", element);
763
764   return element;
765 }
766
767 /* make the element (bin) that contains the elements needed to perform
768  * visualisation ouput.  The idea is to split the audio using tee, then 
769  * sending the output to the regular audio bin and the other output to
770  * the vis plugin that transforms it into a video that is rendered with the
771  * normal video bin. The video bin is run in a thread to make sure it does
772  * not block the audio playback pipeline.
773  *
774  *  +--------------------------------------------------------------------------+
775  *  | visbin                                                                   |
776  *  |      +------+   +----------------+                                       |
777  *  |      | tee  |   |   abin ...     |                                       |
778  *  |   +-sink   src-sink              |                                       |
779  *  |   |  |      |   +----------------+                 +-------------------+ |
780  *  |   |  |      |                                      | vthread           | |
781  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
782  *  |   |  |      |   |audioconv|   | vis  |   |vqueue|  | | vbin ...     |  | |
783  *  |   |  |     src-sink      src-sink   src-sink   src-sink             |  | |
784  *  |   |  |      |   +---------+   +------+   +------+  | +--------------+  | |
785  *  |   |  |      |                                      +-------------------+ |
786  *  |   |  +------+                                                            |
787  * sink-+                                                                      |
788    +--------------------------------------------------------------------------+
789  */
790 static GstElement *
791 gen_vis_element (GstPlayBin * play_bin)
792 {
793   GstElement *element;
794   GstElement *tee;
795   GstElement *asink;
796   GstElement *vsink;
797   GstElement *conv;
798   GstElement *vis;
799   GstElement *vqueue, *aqueue;
800   GstPad *pad, *rpad;
801
802   asink = gen_audio_element (play_bin);
803   if (!asink)
804     return NULL;
805   vsink = gen_video_element (play_bin);
806   if (!vsink) {
807     gst_object_unref (asink);
808     return NULL;
809   }
810
811   element = gst_bin_new ("visbin");
812   tee = gst_element_factory_make ("tee", "tee");
813
814   vqueue = gst_element_factory_make ("queue", "vqueue");
815   aqueue = gst_element_factory_make ("queue", "aqueue");
816
817   gst_bin_add (GST_BIN (element), asink);
818   gst_bin_add (GST_BIN (element), vqueue);
819   gst_bin_add (GST_BIN (element), aqueue);
820   gst_bin_add (GST_BIN (element), vsink);
821   gst_bin_add (GST_BIN (element), tee);
822
823   conv = gst_element_factory_make ("audioconvert", "aconv");
824   if (play_bin->visualisation) {
825     gst_object_ref (play_bin->visualisation);
826     vis = play_bin->visualisation;
827   } else {
828     vis = gst_element_factory_make ("goom", "vis");
829   }
830
831   gst_bin_add (GST_BIN (element), conv);
832   gst_bin_add (GST_BIN (element), vis);
833
834   gst_element_link_pads (conv, "src", vis, "sink");
835   gst_element_link_pads (vis, "src", vqueue, "sink");
836
837   gst_element_link_pads (vqueue, "src", vsink, "sink");
838
839   pad = gst_element_get_pad (aqueue, "sink");
840   rpad = gst_element_get_request_pad (tee, "src%d");
841   gst_pad_link (rpad, pad);
842   gst_object_unref (rpad);
843   gst_object_unref (pad);
844   gst_element_link_pads (aqueue, "src", asink, "sink");
845
846   pad = gst_element_get_pad (conv, "sink");
847   rpad = gst_element_get_request_pad (tee, "src%d");
848   gst_pad_link (rpad, pad);
849   gst_object_unref (rpad);
850   gst_object_unref (pad);
851
852   pad = gst_element_get_pad (tee, "sink");
853   gst_element_add_ghost_pad (element, pad, "sink");
854   gst_object_unref (pad);
855
856   return element;
857 }
858
859 /* get rid of all installed sinks */
860 static void
861 remove_sinks (GstPlayBin * play_bin)
862 {
863   GList *sinks;
864   GstObject *parent;
865   GstElement *element;
866   GstPad *pad, *peer;
867
868   GST_DEBUG ("removesinks");
869   element = g_hash_table_lookup (play_bin->cache, "abin");
870   if (element != NULL) {
871     parent = gst_element_get_parent (element);
872     if (parent != NULL) {
873       /* we remove the element from the parent so that
874        * there is no unwanted state change when the parent
875        * is disposed */
876       play_bin->sinks = g_list_remove (play_bin->sinks, element);
877       gst_element_set_state (element, GST_STATE_NULL);
878       gst_bin_remove (GST_BIN (parent), element);
879       gst_object_unref (parent);
880     }
881     pad = gst_element_get_pad (element, "sink");
882     if (pad != NULL) {
883       peer = gst_pad_get_peer (pad);
884       if (peer != NULL) {
885         gst_pad_unlink (peer, pad);
886         gst_object_unref (peer);
887       }
888       gst_object_unref (pad);
889     }
890   }
891   element = g_hash_table_lookup (play_bin->cache, "vbin");
892   if (element != NULL) {
893     parent = gst_element_get_parent (element);
894     if (parent != NULL) {
895       play_bin->sinks = g_list_remove (play_bin->sinks, element);
896       gst_element_set_state (element, GST_STATE_NULL);
897       gst_bin_remove (GST_BIN (parent), element);
898       gst_object_unref (parent);
899     }
900     pad = gst_element_get_pad (element, "sink");
901     if (pad != NULL) {
902       peer = gst_pad_get_peer (pad);
903       if (peer != NULL) {
904         gst_pad_unlink (peer, pad);
905         gst_object_unref (peer);
906       }
907       gst_object_unref (pad);
908     }
909   }
910
911   for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
912     GstElement *element = GST_ELEMENT (sinks->data);
913     GstPad *pad;
914     GstPad *peer;
915
916     pad = gst_element_get_pad (element, "sink");
917
918     GST_LOG ("removing sink %p", element);
919
920     peer = gst_pad_get_peer (pad);
921     if (peer) {
922       gst_pad_unlink (peer, pad);
923       gst_object_unref (peer);
924     }
925     gst_object_unref (pad);
926
927     gst_element_set_state (element, GST_STATE_NULL);
928     gst_bin_remove (GST_BIN (play_bin), element);
929   }
930   g_list_free (play_bin->sinks);
931   play_bin->sinks = NULL;
932
933   /* FIXME: this is probably some refcounting problem */
934   if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
935     gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
936     gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
937         play_bin->visualisation);
938   }
939
940   if (play_bin->frame) {
941     gst_buffer_unref (play_bin->frame);
942     play_bin->frame = NULL;
943   }
944
945   play_bin->textoverlay_element = NULL;
946 }
947
948 /* loop over the streams and set up the pipeline to play this
949  * media file. First we count the number of audio and video streams.
950  * If there is no video stream but there exists an audio stream,
951  * we install a visualisation pipeline.
952  * 
953  * Also make sure to only connect the first audio and video pad. FIXME
954  * this should eventually be handled with a tuner interface so that
955  * one can switch the streams.
956  */
957 static gboolean
958 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
959 {
960   GstPad *sinkpad;
961   GstPadLinkReturn linkres;
962   GstElement *parent;
963   GstStateChangeReturn stateret;
964
965   g_return_val_if_fail (sink != NULL, FALSE);
966   /* this is only for debugging */
967   parent = gst_pad_get_parent_element (srcpad);
968   if (parent) {
969     GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
970         GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
971     gst_object_unref (parent);
972   }
973
974   /* bring it to the PAUSED state so we can link to the peer without
975    * breaking the flow */
976   if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
977       GST_STATE_CHANGE_FAILURE)
978     goto state_failed;
979
980   gst_bin_add (GST_BIN (play_bin), sink);
981
982   /* we found a sink for this stream, now try to install it */
983   sinkpad = gst_element_get_pad (sink, "sink");
984   linkres = gst_pad_link (srcpad, sinkpad);
985   gst_object_unref (sinkpad);
986
987   /* try to link the pad of the sink to the stream */
988   if (GST_PAD_LINK_FAILED (linkres))
989     goto link_failed;
990
991   /* we got the sink succesfully linked, now keep the sink
992    * in out internal list */
993   play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
994
995   return TRUE;
996
997   /* ERRORS */
998 state_failed:
999   {
1000     GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1001     return FALSE;
1002   }
1003 link_failed:
1004   {
1005     gchar *capsstr;
1006     GstCaps *caps;
1007
1008     /* could not link this stream */
1009     caps = gst_pad_get_caps (srcpad);
1010     capsstr = gst_caps_to_string (caps);
1011     g_warning ("could not link %s: %d", capsstr, linkres);
1012     GST_DEBUG_OBJECT (play_bin,
1013         "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1014     g_free (capsstr);
1015     g_free (caps);
1016
1017     gst_element_set_state (sink, GST_STATE_NULL);
1018     gst_bin_remove (GST_BIN (play_bin), sink);
1019     return FALSE;
1020   }
1021 }
1022
1023 static gboolean
1024 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1025 {
1026   GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1027   GList *streaminfo = NULL, *s;
1028   gboolean need_vis = FALSE;
1029   gboolean need_text = FALSE;
1030   GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
1031   GstElement *sink;
1032   gboolean res = TRUE;
1033
1034   /* get rid of existing sinks */
1035   if (play_bin->sinks) {
1036     remove_sinks (play_bin);
1037   }
1038   GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1039
1040   /* find out what to do */
1041   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1042       group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1043     need_text = TRUE;
1044   } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1045       group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1046       play_bin->visualisation != NULL) {
1047     need_vis = TRUE;
1048   }
1049
1050   /* now actually connect everything */
1051   g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1052   for (s = streaminfo; s; s = g_list_next (s)) {
1053     GObject *obj = G_OBJECT (s->data);
1054     gint type;
1055     GstObject *object;
1056
1057     g_object_get (obj, "type", &type, NULL);
1058     g_object_get (obj, "object", &object, NULL);
1059   }
1060
1061   /* link audio */
1062   if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1063     if (need_vis) {
1064       sink = gen_vis_element (play_bin);
1065     } else {
1066       sink = gen_audio_element (play_bin);
1067     }
1068     if (!sink)
1069       return FALSE;
1070     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1071         "src");
1072     res = add_sink (play_bin, sink, pad);
1073     gst_object_unref (pad);
1074   }
1075
1076   /* link video */
1077   if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1078     if (need_text) {
1079       sink = gen_text_element (play_bin);
1080
1081       textsinkpad = gst_element_get_pad (sink, "text_sink");
1082       textsrcpad =
1083           gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1084           "src");
1085       gst_pad_link (textsrcpad, textsinkpad);
1086       gst_object_unref (textsinkpad);
1087       gst_object_unref (textsrcpad);
1088     } else {
1089       sink = gen_video_element (play_bin);
1090     }
1091     if (!sink)
1092       return FALSE;
1093     pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1094         "src");
1095     res = add_sink (play_bin, sink, pad);
1096     gst_object_unref (pad);
1097   }
1098
1099   /* remove the sinks now, pipeline get_state will now wait for the
1100    * sinks to preroll */
1101   if (play_bin->fakesink) {
1102     gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1103     gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1104     play_bin->fakesink = NULL;
1105   }
1106
1107   return res;
1108 }
1109
1110 /* Send an event to our sinks until one of them works; don't then send to the
1111  * remaining sinks (unlike GstBin)
1112  */
1113 static gboolean
1114 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1115 {
1116   GList *sinks = play_bin->sinks;
1117   gboolean res = TRUE;
1118
1119   while (sinks) {
1120     GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1121
1122     gst_event_ref (event);
1123     if ((res = gst_element_send_event (sink, event)))
1124       break;
1125
1126     sinks = g_list_next (sinks);
1127   }
1128
1129   gst_event_unref (event);
1130
1131   return res;
1132 }
1133
1134 static gboolean
1135 do_playbin_seek (GstElement * element, GstEvent * event)
1136 {
1137   gdouble rate;
1138   GstSeekFlags flags;
1139   gboolean flush;
1140   gboolean was_playing = FALSE;
1141   gboolean res;
1142
1143   gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
1144
1145   flush = flags & GST_SEEK_FLAG_FLUSH;
1146
1147   if (flush) {
1148     GstState state;
1149
1150     /* need to call _get_state() since a bin state is only updated
1151      * with this call. */
1152     gst_element_get_state (element, &state, NULL, 0);
1153     was_playing = state == GST_STATE_PLAYING;
1154
1155     if (was_playing) {
1156       gst_element_set_state (element, GST_STATE_PAUSED);
1157     }
1158   }
1159
1160   res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1161
1162   if (flush && res) {
1163     /* need to reset the stream time to 0 after a flushing seek */
1164     gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
1165     if (was_playing)
1166       /* and continue playing */
1167       gst_element_set_state (element, GST_STATE_PLAYING);
1168   }
1169   return res;
1170 }
1171
1172 /* We only want to send the event to a single sink (overriding GstBin's 
1173  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1174  * events appropriately. So, this is a messy duplication of code. */
1175 static gboolean
1176 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1177 {
1178   gboolean res = FALSE;
1179   GstEventType event_type = GST_EVENT_TYPE (event);
1180
1181
1182   switch (event_type) {
1183     case GST_EVENT_SEEK:
1184       res = do_playbin_seek (element, event);
1185       break;
1186     default:
1187       res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1188       break;
1189   }
1190
1191   return res;
1192 }
1193
1194 static GstStateChangeReturn
1195 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1196 {
1197   GstStateChangeReturn ret;
1198   GstPlayBin *play_bin;
1199
1200   play_bin = GST_PLAY_BIN (element);
1201
1202
1203   switch (transition) {
1204     case GST_STATE_CHANGE_READY_TO_PAUSED:
1205       /* this really is the easiest way to make the state change return
1206        * ASYNC until we added the sinks */
1207       if (!play_bin->fakesink) {
1208         play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1209         gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1210       }
1211       break;
1212     default:
1213       break;
1214   }
1215
1216   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1217   if (ret == GST_STATE_CHANGE_FAILURE)
1218     return ret;
1219
1220   switch (transition) {
1221     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1222       /* Set audio sink state to NULL to release the sound device,
1223        * but only if we own it (else we might be in chain-transition). */
1224       //if (play_bin->audio_sink != NULL &&
1225       //    GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1226       //  gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1227       //}
1228       break;
1229     case GST_STATE_CHANGE_PAUSED_TO_READY:
1230       /* Check for NULL because the state transition may be done by
1231        * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1232        * case, we don't want to run remove_sinks.
1233        * FIXME: should the NULL test be done in remove_sinks? Should we just
1234        * set the state to NULL in gst_play_bin_dispose?
1235        */
1236       if (play_bin->cache != NULL) {
1237         remove_sinks (play_bin);
1238       }
1239       if (play_bin->fakesink) {
1240         gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1241         gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1242         play_bin->fakesink = NULL;
1243       }
1244       break;
1245     default:
1246       break;
1247   }
1248
1249   return ret;
1250 }
1251
1252 static gboolean
1253 plugin_init (GstPlugin * plugin)
1254 {
1255   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1256
1257 #ifdef ENABLE_NLS
1258   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1259       LOCALEDIR);
1260   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1261 #endif /* ENABLE_NLS */
1262
1263   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1264       GST_TYPE_PLAY_BIN);
1265 }
1266
1267 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1268     GST_VERSION_MINOR,
1269     "playbin",
1270     "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1271     GST_PACKAGE_ORIGIN)