2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
27 #include <gst/gst-i18n-plugin.h>
29 #include "gstplaybasebin.h"
31 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_debug);
32 #define GST_CAT_DEFAULT gst_play_bin_debug
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))
40 #define VOLUME_MAX_DOUBLE 4.0
42 #ifndef GST_HAVE_GLIB_2_8
43 #define _gst_gvalue_set_gstobject(gvalue,obj) \
45 gst_object_ref (obj); \
46 g_value_set_object (gvalue, obj); \
47 g_object_unref (obj); \
49 g_value_set_object (gvalue, NULL); \
52 #define _gst_gvalue_set_gstobject(gvalue,obj) \
53 g_value_set_object (gvalue, obj);
56 typedef struct _GstPlayBin GstPlayBin;
57 typedef struct _GstPlayBinClass GstPlayBinClass;
61 GstPlayBaseBin parent;
63 /* the configurable elements */
65 GstElement *audio_sink;
66 GstElement *video_sink;
67 GstElement *visualisation;
68 GstElement *pending_visualisation;
69 GstElement *volume_element;
70 GstElement *textoverlay_element;
73 /* these are the currently active sinks */
76 /* the last captured frame for snapshots */
79 /* our cache for the sinks */
82 /* font description */
86 struct _GstPlayBinClass
88 GstPlayBaseBinClass parent_class;
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);
113 static gboolean setup_sinks (GstPlayBaseBin * play_base_bin,
114 GstPlayBaseGroup * group);
115 static void remove_sinks (GstPlayBin * play_bin);
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);
122 static gboolean gst_play_bin_send_event (GstElement * element,
124 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
125 GstStateChange transition);
127 static GstElementClass *parent_class;
129 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
131 static GstElementDetails gst_play_bin_details =
132 GST_ELEMENT_DETAILS ("Player Bin",
133 "Generic/Bin/Player",
134 "Autoplug and play media from an uri",
135 "Wim Taymans <wim@fluendo.com>");
138 gst_play_bin_get_type (void)
140 static GType gst_play_bin_type = 0;
142 if (!gst_play_bin_type) {
143 static const GTypeInfo gst_play_bin_info = {
144 sizeof (GstPlayBinClass),
147 (GClassInitFunc) gst_play_bin_class_init,
152 (GInstanceInitFunc) gst_play_bin_init,
156 gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
157 "GstPlayBin", &gst_play_bin_info, 0);
160 return gst_play_bin_type;
164 gst_play_bin_class_init (GstPlayBinClass * klass)
166 GObjectClass *gobject_klass;
167 GstElementClass *gstelement_klass;
168 GstBinClass *gstbin_klass;
169 GstPlayBaseBinClass *playbasebin_klass;
171 gobject_klass = (GObjectClass *) klass;
172 gstelement_klass = (GstElementClass *) klass;
173 gstbin_klass = (GstBinClass *) klass;
174 playbasebin_klass = (GstPlayBaseBinClass *) klass;
176 parent_class = g_type_class_ref (gst_play_base_bin_get_type ());
178 gobject_klass->set_property = gst_play_bin_set_property;
179 gobject_klass->get_property = gst_play_bin_get_property;
181 g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
182 g_param_spec_object ("video-sink", "Video Sink",
183 "the video output element to use (NULL = default sink)",
184 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
185 g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
186 g_param_spec_object ("audio-sink", "Audio Sink",
187 "the audio output element to use (NULL = default sink)",
188 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
189 g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
190 g_param_spec_object ("vis-plugin", "Vis plugin",
191 "the visualization element to use (NULL = none)",
192 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
193 g_object_class_install_property (gobject_klass, ARG_VOLUME,
194 g_param_spec_double ("volume", "volume", "volume",
195 0.0, VOLUME_MAX_DOUBLE, 1.0, G_PARAM_READWRITE));
196 g_object_class_install_property (gobject_klass, ARG_FRAME,
197 gst_param_spec_mini_object ("frame", "Frame",
198 "The last frame (NULL = no video available)",
199 GST_TYPE_BUFFER, G_PARAM_READABLE));
200 g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
201 g_param_spec_string ("subtitle-font-desc",
202 "Subtitle font description",
203 "Pango font description of font "
204 "to be used for subtitle rendering", NULL, G_PARAM_WRITABLE));
206 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_dispose);
208 gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
210 gstelement_klass->change_state =
211 GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
212 gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
214 playbasebin_klass->setup_output_pads = setup_sinks;
218 gst_play_bin_init (GstPlayBin * play_bin)
220 play_bin->video_sink = NULL;
221 play_bin->audio_sink = NULL;
222 play_bin->visualisation = NULL;
223 play_bin->pending_visualisation = NULL;
224 play_bin->volume_element = NULL;
225 play_bin->textoverlay_element = NULL;
226 play_bin->volume = 1.0;
227 play_bin->sinks = NULL;
228 play_bin->frame = NULL;
229 play_bin->font_desc = NULL;
230 play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
231 NULL, (GDestroyNotify) gst_object_unref);
235 gst_play_bin_dispose (GObject * object)
237 GstPlayBin *play_bin;
239 play_bin = GST_PLAY_BIN (object);
241 if (play_bin->cache != NULL) {
242 remove_sinks (play_bin);
243 g_hash_table_destroy (play_bin->cache);
244 play_bin->cache = NULL;
247 if (play_bin->audio_sink != NULL) {
248 gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
249 gst_object_unref (play_bin->audio_sink);
250 play_bin->audio_sink = NULL;
252 if (play_bin->video_sink != NULL) {
253 gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
254 gst_object_unref (play_bin->video_sink);
255 play_bin->video_sink = NULL;
257 if (play_bin->visualisation != NULL) {
258 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
259 gst_object_unref (play_bin->visualisation);
260 play_bin->visualisation = NULL;
262 if (play_bin->pending_visualisation != NULL) {
263 gst_element_set_state (play_bin->pending_visualisation, GST_STATE_NULL);
264 gst_object_unref (play_bin->pending_visualisation);
265 play_bin->pending_visualisation = NULL;
267 if (play_bin->textoverlay_element != NULL) {
268 gst_object_unref (play_bin->textoverlay_element);
269 play_bin->textoverlay_element = NULL;
271 g_free (play_bin->font_desc);
272 play_bin->font_desc = NULL;
274 G_OBJECT_CLASS (parent_class)->dispose (object);
278 gst_play_bin_vis_unblocked (GstPad * tee_pad, gboolean blocked,
285 gst_play_bin_vis_blocked (GstPad * tee_pad, gboolean blocked,
288 GstPlayBin *play_bin = GST_PLAY_BIN (user_data);
289 GstBin *vis_bin = NULL;
290 GstPad *vis_sink_pad = NULL, *vis_src_pad = NULL, *vqueue_pad = NULL;
293 /* We want to disable visualisation */
294 if (!GST_IS_ELEMENT (play_bin->pending_visualisation)) {
295 /* Set visualisation element to READY */
296 gst_element_set_state (play_bin->visualisation, GST_STATE_READY);
301 GST_BIN (gst_object_get_parent (GST_OBJECT (play_bin->visualisation)));
303 if (!GST_IS_BIN (vis_bin) || !GST_IS_PAD (tee_pad)) {
307 vis_src_pad = gst_element_get_pad (play_bin->visualisation, "src");
308 vis_sink_pad = gst_pad_get_peer (tee_pad);
310 /* Can be fakesink */
311 if (GST_IS_PAD (vis_src_pad)) {
312 vqueue_pad = gst_pad_get_peer (vis_src_pad);
315 if (!GST_IS_PAD (vis_sink_pad)) {
319 /* Check the bin's state */
320 GST_OBJECT_LOCK (vis_bin);
321 bin_state = GST_STATE (vis_bin);
322 GST_OBJECT_UNLOCK (vis_bin);
325 gst_pad_unlink (tee_pad, vis_sink_pad);
326 gst_object_unref (vis_sink_pad);
329 if (GST_IS_PAD (vqueue_pad)) {
330 gst_pad_unlink (vis_src_pad, vqueue_pad);
331 gst_object_unref (vis_src_pad);
335 /* Remove from vis_bin */
336 gst_bin_remove (vis_bin, play_bin->visualisation);
337 /* Set state to NULL */
338 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
339 /* And loose our ref */
340 gst_object_unref (play_bin->visualisation);
342 if (play_bin->pending_visualisation) {
343 /* Ref this new visualisation element before adding to the bin */
344 gst_object_ref (play_bin->pending_visualisation);
345 /* Add the new one */
346 gst_bin_add (vis_bin, play_bin->pending_visualisation);
347 /* Synchronizing state */
348 gst_element_set_state (play_bin->pending_visualisation, bin_state);
350 vis_sink_pad = gst_element_get_pad (play_bin->pending_visualisation,
352 vis_src_pad = gst_element_get_pad (play_bin->pending_visualisation, "src");
354 if (!GST_IS_PAD (vis_sink_pad) || !GST_IS_PAD (vis_src_pad)) {
359 gst_pad_link (tee_pad, vis_sink_pad);
360 gst_pad_link (vis_src_pad, vqueue_pad);
364 gst_object_unref (play_bin->visualisation);
365 play_bin->visualisation = play_bin->pending_visualisation;
366 play_bin->pending_visualisation = NULL;
370 gst_object_unref (vis_sink_pad);
373 gst_object_unref (vis_src_pad);
376 gst_object_unref (vqueue_pad);
379 gst_object_unref (vis_bin);
382 /* Unblock the pad */
383 gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_bin_vis_unblocked,
388 gst_play_bin_set_property (GObject * object, guint prop_id,
389 const GValue * value, GParamSpec * pspec)
391 GstPlayBin *play_bin;
393 g_return_if_fail (GST_IS_PLAY_BIN (object));
395 play_bin = GST_PLAY_BIN (object);
399 if (play_bin->video_sink != NULL) {
400 gst_object_unref (play_bin->video_sink);
402 play_bin->video_sink = g_value_get_object (value);
403 if (play_bin->video_sink != NULL) {
404 gst_object_ref (play_bin->video_sink);
405 gst_object_sink (GST_OBJECT (play_bin->video_sink));
407 /* when changing the videosink, we just remove the
408 * video pipeline from the cache so that it will be
409 * regenerated with the new sink element */
410 g_hash_table_remove (play_bin->cache, "vbin");
413 if (play_bin->audio_sink != NULL) {
414 gst_object_unref (play_bin->audio_sink);
416 play_bin->audio_sink = g_value_get_object (value);
417 if (play_bin->audio_sink != NULL) {
418 gst_object_ref (play_bin->audio_sink);
419 gst_object_sink (GST_OBJECT (play_bin->audio_sink));
421 g_hash_table_remove (play_bin->cache, "abin");
425 /* Do we already have a visualisation change pending ? */
426 if (play_bin->pending_visualisation) {
427 gst_object_unref (play_bin->pending_visualisation);
428 play_bin->pending_visualisation = g_value_get_object (value);
430 if (play_bin->pending_visualisation) {
431 gst_object_ref (play_bin->pending_visualisation);
432 gst_object_sink (GST_OBJECT (play_bin->pending_visualisation));
435 play_bin->pending_visualisation = g_value_get_object (value);
438 if (play_bin->pending_visualisation) {
439 gst_object_ref (play_bin->pending_visualisation);
440 gst_object_sink (GST_OBJECT (play_bin->pending_visualisation));
443 /* Was there a visualisation already set ? */
444 if (play_bin->visualisation != NULL) {
445 GstBin *vis_bin = NULL;
448 GST_BIN (gst_object_get_parent (GST_OBJECT (play_bin->
451 /* Check if the visualisation is already in a bin */
452 if (GST_IS_BIN (vis_bin)) {
453 GstPad *vis_sink_pad = NULL, *tee_pad = NULL;
455 /* Now get tee pad and block it async */
456 vis_sink_pad = gst_element_get_pad (play_bin->visualisation,
458 if (!GST_IS_PAD (vis_sink_pad)) {
461 tee_pad = gst_pad_get_peer (vis_sink_pad);
462 if (!GST_IS_PAD (tee_pad)) {
466 /* Block with callback */
467 gst_pad_set_blocked_async (tee_pad, TRUE, gst_play_bin_vis_blocked,
471 gst_object_unref (vis_sink_pad);
474 gst_object_unref (tee_pad);
476 gst_object_unref (vis_bin);
478 play_bin->visualisation = play_bin->pending_visualisation;
479 play_bin->pending_visualisation = NULL;
482 play_bin->visualisation = play_bin->pending_visualisation;
483 play_bin->pending_visualisation = NULL;
489 play_bin->volume = g_value_get_double (value);
490 if (play_bin->volume_element) {
491 g_object_set (G_OBJECT (play_bin->volume_element), "volume",
492 play_bin->volume, NULL);
496 g_free (play_bin->font_desc);
497 play_bin->font_desc = g_strdup (g_value_get_string (value));
498 if (play_bin->textoverlay_element) {
499 g_object_set (G_OBJECT (play_bin->textoverlay_element),
500 "font-desc", g_value_get_string (value), NULL);
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
510 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
513 GstPlayBin *play_bin;
515 g_return_if_fail (GST_IS_PLAY_BIN (object));
517 play_bin = GST_PLAY_BIN (object);
521 _gst_gvalue_set_gstobject (value, play_bin->video_sink);
524 _gst_gvalue_set_gstobject (value, play_bin->audio_sink);
527 _gst_gvalue_set_gstobject (value, play_bin->visualisation);
530 g_value_set_double (value, play_bin->volume);
533 gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
536 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541 /* signal fired when the identity has received a new buffer. This is used for
542 * making screenshots.
545 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
547 GstPlayBin *play_bin = GST_PLAY_BIN (data);
548 GstBuffer **frame_p = &play_bin->frame;
550 gst_mini_object_replace ((GstMiniObject **) frame_p,
551 GST_MINI_OBJECT_CAST (frame));
553 /* applications need to know the buffer caps,
554 * make sure they are always set on the frame */
555 if (GST_BUFFER_CAPS (play_bin->frame) == NULL) {
558 if ((pad = gst_element_get_pad (identity, "sink"))) {
559 gst_buffer_set_caps (play_bin->frame, GST_PAD_CAPS (pad));
560 gst_object_unref (pad);
565 /* make the element (bin) that contains the elements needed to perform
566 * video display. We connect a handoff signal to identity so that we
567 * can grab snapshots. Identity's sinkpad is ghosted to vbin.
569 * +-------------------------------------------------------------+
571 * | +--------+ +----------+ +----------+ +---------+ |
572 * | |identity| |colorspace| |videoscale| |videosink| |
573 * | +-sink src-sink src-sink src-sink | |
574 * | | +---+----+ +----------+ +----------+ +---------+ |
576 * +----------|--------------------------------------------------+
579 /* FIXME: this might return NULL if no videosink was found, handle
582 gen_video_element (GstPlayBin * play_bin)
589 GstElement *identity;
592 /* first see if we have it in the cache */
593 element = g_hash_table_lookup (play_bin->cache, "vbin");
594 if (element != NULL) {
598 if (play_bin->video_sink) {
599 sink = play_bin->video_sink;
601 sink = gst_element_factory_make ("autovideosink", "videosink");
603 sink = gst_element_factory_make ("xvimagesink", "videosink");
605 /* FIXME: this warrants adding a CORE error category for missing
606 * elements/plugins */
608 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
609 (_("Both autovideosink and xvimagesink elements are missing.")),
614 gst_object_ref (sink);
615 g_hash_table_insert (play_bin->cache, "video_sink", sink);
618 element = gst_bin_new ("vbin");
619 identity = gst_element_factory_make ("identity", "id");
620 g_object_set (identity, "silent", TRUE, NULL);
621 g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
622 gst_bin_add (GST_BIN (element), identity);
623 conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
626 scale = gst_element_factory_make ("videoscale", "vscale");
629 gst_bin_add (GST_BIN (element), conv);
630 gst_bin_add (GST_BIN (element), scale);
631 gst_bin_add (GST_BIN (element), sink);
632 gst_element_link_pads (identity, "src", conv, "sink");
633 gst_element_link_pads (conv, "src", scale, "sink");
634 gst_element_link_pads (scale, "src", sink, "sink");
636 pad = gst_element_get_pad (identity, "sink");
637 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
638 gst_object_unref (pad);
640 gst_element_set_state (element, GST_STATE_READY);
642 /* since we're gonna add it to a bin but don't want to lose it,
643 * we keep a reference. */
644 gst_object_ref (element);
645 g_hash_table_insert (play_bin->cache, "vbin", element);
651 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
652 (_("Missing element '%s' - check your GStreamer installation."),
653 "ffmpegcolorspace"), (NULL));
654 gst_object_unref (element);
660 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
661 (_("Missing element '%s' - check your GStreamer installation."),
662 "videoscale"), ("possibly a liboil version mismatch?"));
663 gst_object_unref (element);
668 /* make an element for playback of video with subtitles embedded.
670 * +--------------------------------------------------+
671 * | tbin +-------------+ |
672 * | +-----+ | textoverlay | +------+ |
673 * | | csp | +--video_sink | | vbin | |
674 * video_sink-sink src+ +-text_sink src-sink | |
675 * | +-----+ | +-------------+ +------+ |
676 * text_sink-------------+ |
677 * +--------------------------------------------------+
681 gen_text_element (GstPlayBin * play_bin)
683 GstElement *element, *csp, *overlay, *vbin;
687 element = gst_bin_new ("textbin");
690 overlay = gst_element_factory_make ("textoverlay", "overlay");
692 /* Create the video rendering bin */
693 vbin = gen_video_element (play_bin);
695 /* If no overlay return the video bin */
697 GST_WARNING ("No overlay (pango) element, subtitles disabled");
701 /* Set some parameters */
702 g_object_set (G_OBJECT (overlay),
703 "halign", "center", "valign", "bottom", NULL);
704 if (play_bin->font_desc) {
705 g_object_set (G_OBJECT (overlay), "font-desc", play_bin->font_desc, NULL);
709 play_bin->textoverlay_element = GST_ELEMENT (gst_object_ref (overlay));
711 /* we know this will succeed, as the video bin already created one before */
712 csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
714 /* Add our elements */
715 gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
718 gst_element_link_pads (csp, "src", overlay, "video_sink");
719 gst_element_link_pads (overlay, "src", vbin, "sink");
721 /* Add ghost pads on the subtitle bin */
722 pad = gst_element_get_pad (overlay, "text_sink");
723 gst_element_add_pad (element, gst_ghost_pad_new ("text_sink", pad));
724 gst_object_unref (pad);
726 pad = gst_element_get_pad (csp, "sink");
727 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
728 gst_object_unref (pad);
730 /* Set state to READY */
731 gst_element_set_state (element, GST_STATE_READY);
736 /* make the element (bin) that contains the elements needed to perform
739 * +-------------------------------------------------------------+
741 * | +---------+ +----------+ +---------+ +---------+ |
742 * | |audioconv| |audioscale| | volume | |audiosink| |
743 * | +-sink src-sink src-sink src-sink | |
744 * | | +---------+ +----------+ +---------+ +---------+ |
746 * +-------------------------------------------------------------+
750 gen_audio_element (GstPlayBin * play_bin)
757 /* GstElement *scale; */
760 element = g_hash_table_lookup (play_bin->cache, "abin");
761 if (element != NULL) {
764 element = gst_bin_new ("abin");
765 conv = gst_element_factory_make ("audioconvert", "aconv");
767 goto no_audioconvert;
769 scale = gst_element_factory_make ("audioresample", "aresample");
771 goto no_audioresample;
773 volume = gst_element_factory_make ("volume", "volume");
774 g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
775 play_bin->volume_element = volume;
777 if (play_bin->audio_sink) {
778 sink = play_bin->audio_sink;
780 sink = gst_element_factory_make ("autoaudiosink", "audiosink");
782 sink = gst_element_factory_make ("alsasink", "audiosink");
785 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
786 (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
789 play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
792 gst_object_ref (sink);
793 g_hash_table_insert (play_bin->cache, "audio_sink", sink);
795 gst_bin_add (GST_BIN (element), conv);
796 /* gst_bin_add (GST_BIN (element), scale); */
797 gst_bin_add (GST_BIN (element), volume);
798 gst_bin_add (GST_BIN (element), sink);
800 gst_element_link_pads (conv, "src", /*scale, "sink");
801 gst_element_link_pads (scale, "src", */ volume, "sink");
802 gst_element_link_pads (volume, "src", sink, "sink");
804 pad = gst_element_get_pad (conv, "sink");
805 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
806 gst_object_unref (pad);
808 gst_element_set_state (element, GST_STATE_READY);
810 /* since we're gonna add it to a bin but don't want to lose it,
811 * we keep a reference. */
812 gst_object_ref (element);
813 g_hash_table_insert (play_bin->cache, "abin", element);
819 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
820 (_("Missing element '%s' - check your GStreamer installation."),
821 "audioconvert"), ("possibly a liboil version mismatch?"));
822 gst_object_unref (element);
828 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
829 (_("Missing element '%s' - check your GStreamer installation."),
831 ("possibly a liboil version mismatch?"));
832 gst_object_unref (element);
838 /* make the element (bin) that contains the elements needed to perform
839 * visualisation ouput. The idea is to split the audio using tee, then
840 * sending the output to the regular audio bin and the other output to
841 * the vis plugin that transforms it into a video that is rendered with the
842 * normal video bin. The video bin is run in a thread to make sure it does
843 * not block the audio playback pipeline.
845 * +--------------------------------------------------------------------+
847 * | +------+ +--------+ +----------------+ |
848 * | | tee | | aqueue | | abin ... | |
849 * | +-sink src-sink src-sink | |
850 * | | | | +--------+ +----------------+ |
852 * | | | | +------+ +---------+ +------+ +-----------+ |
853 * | | | | |vqueue| |audioconv| | vis | | vbin ... | |
854 * | | | src-sink src-sink src-sink src-sink | |
855 * | | | | +------+ +---------+ +------+ +-----------+ |
859 +---------------------------------------------------------------------+
862 gen_vis_element (GstPlayBin * play_bin)
870 GstElement *vqueue, *aqueue;
873 asink = gen_audio_element (play_bin);
876 vsink = gen_video_element (play_bin);
878 gst_object_unref (asink);
882 element = gst_bin_new ("visbin");
883 tee = gst_element_factory_make ("tee", "tee");
885 vqueue = gst_element_factory_make ("queue", "vqueue");
886 aqueue = gst_element_factory_make ("queue", "aqueue");
888 gst_bin_add (GST_BIN (element), asink);
889 gst_bin_add (GST_BIN (element), vqueue);
890 gst_bin_add (GST_BIN (element), aqueue);
891 gst_bin_add (GST_BIN (element), vsink);
892 gst_bin_add (GST_BIN (element), tee);
894 conv = gst_element_factory_make ("audioconvert", "aconv");
896 goto no_audioconvert;
898 if (play_bin->visualisation) {
899 gst_object_ref (play_bin->visualisation);
900 vis = play_bin->visualisation;
902 vis = gst_element_factory_make ("goom", "vis");
905 gst_bin_add (GST_BIN (element), conv);
906 gst_bin_add (GST_BIN (element), vis);
908 gst_element_link_pads (vqueue, "src", conv, "sink");
909 gst_element_link_pads (conv, "src", vis, "sink");
910 gst_element_link_pads (vis, "src", vsink, "sink");
912 pad = gst_element_get_pad (aqueue, "sink");
913 rpad = gst_element_get_request_pad (tee, "src%d");
914 gst_pad_link (rpad, pad);
915 gst_object_unref (rpad);
916 gst_object_unref (pad);
917 gst_element_link_pads (aqueue, "src", asink, "sink");
919 pad = gst_element_get_pad (vqueue, "sink");
920 rpad = gst_element_get_request_pad (tee, "src%d");
921 gst_pad_link (rpad, pad);
922 gst_object_unref (rpad);
923 gst_object_unref (pad);
925 pad = gst_element_get_pad (tee, "sink");
926 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
927 gst_object_unref (pad);
933 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
934 (_("Missing element '%s' - check your GStreamer installation."),
935 "audioconvert"), ("possibly a liboil version mismatch?"));
936 gst_object_unref (element);
941 /* get rid of all installed sinks */
943 remove_sinks (GstPlayBin * play_bin)
950 GST_DEBUG ("removesinks");
951 element = g_hash_table_lookup (play_bin->cache, "abin");
952 if (element != NULL) {
953 parent = gst_element_get_parent (element);
954 if (parent != NULL) {
955 /* we remove the element from the parent so that
956 * there is no unwanted state change when the parent
958 play_bin->sinks = g_list_remove (play_bin->sinks, element);
959 gst_element_set_state (element, GST_STATE_NULL);
960 gst_bin_remove (GST_BIN (parent), element);
961 gst_object_unref (parent);
963 pad = gst_element_get_pad (element, "sink");
965 peer = gst_pad_get_peer (pad);
967 gst_pad_unlink (peer, pad);
968 gst_object_unref (peer);
970 gst_object_unref (pad);
973 element = g_hash_table_lookup (play_bin->cache, "vbin");
974 if (element != NULL) {
975 parent = gst_element_get_parent (element);
976 if (parent != NULL) {
977 play_bin->sinks = g_list_remove (play_bin->sinks, element);
978 gst_element_set_state (element, GST_STATE_NULL);
979 gst_bin_remove (GST_BIN (parent), element);
980 gst_object_unref (parent);
982 pad = gst_element_get_pad (element, "sink");
984 peer = gst_pad_get_peer (pad);
986 gst_pad_unlink (peer, pad);
987 gst_object_unref (peer);
989 gst_object_unref (pad);
993 for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
994 GstElement *element = GST_ELEMENT (sinks->data);
998 pad = gst_element_get_pad (element, "sink");
1000 GST_LOG ("removing sink %p", element);
1002 peer = gst_pad_get_peer (pad);
1004 gst_pad_unlink (peer, pad);
1005 gst_object_unref (peer);
1007 gst_object_unref (pad);
1009 gst_element_set_state (element, GST_STATE_NULL);
1010 gst_bin_remove (GST_BIN (play_bin), element);
1012 g_list_free (play_bin->sinks);
1013 play_bin->sinks = NULL;
1015 /* FIXME: this is probably some refcounting problem */
1016 if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
1017 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
1018 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
1019 play_bin->visualisation);
1022 if (play_bin->frame) {
1023 gst_buffer_unref (play_bin->frame);
1024 play_bin->frame = NULL;
1027 if (play_bin->textoverlay_element) {
1028 gst_object_unref (play_bin->textoverlay_element);
1029 play_bin->textoverlay_element = NULL;
1033 /* loop over the streams and set up the pipeline to play this
1034 * media file. First we count the number of audio and video streams.
1035 * If there is no video stream but there exists an audio stream,
1036 * we install a visualisation pipeline.
1038 * Also make sure to only connect the first audio and video pad. FIXME
1039 * this should eventually be handled with a tuner interface so that
1040 * one can switch the streams.
1043 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
1044 GstPad * subtitle_pad)
1047 GstPadLinkReturn linkres;
1049 GstStateChangeReturn stateret;
1051 g_return_val_if_fail (sink != NULL, FALSE);
1052 /* this is only for debugging */
1053 parent = gst_pad_get_parent_element (srcpad);
1055 GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
1056 GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
1057 gst_object_unref (parent);
1060 /* bring it to the PAUSED state so we can link to the peer without
1061 * breaking the flow */
1062 if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
1063 GST_STATE_CHANGE_FAILURE)
1066 gst_bin_add (GST_BIN (play_bin), sink);
1068 /* we found a sink for this stream, now try to install it */
1069 sinkpad = gst_element_get_pad (sink, "sink");
1070 linkres = gst_pad_link (srcpad, sinkpad);
1071 gst_object_unref (sinkpad);
1073 /* try to link the pad of the sink to the stream */
1074 if (GST_PAD_LINK_FAILED (linkres))
1077 if (GST_IS_PAD (subtitle_pad)) {
1078 sinkpad = gst_element_get_pad (sink, "text_sink");
1079 linkres = gst_pad_link (subtitle_pad, sinkpad);
1080 gst_object_unref (sinkpad);
1083 /* try to link the subtitle pad of the sink to the stream */
1084 if (GST_PAD_LINK_FAILED (linkres)) {
1085 goto subtitle_failed;
1088 /* we got the sink succesfully linked, now keep the sink
1089 * in our internal list */
1090 play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1097 GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1105 /* could not link this stream */
1106 caps = gst_pad_get_caps (srcpad);
1107 capsstr = gst_caps_to_string (caps);
1108 g_warning ("could not link %s: %d", capsstr, linkres);
1109 GST_DEBUG_OBJECT (play_bin,
1110 "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1114 gst_element_set_state (sink, GST_STATE_NULL);
1115 gst_bin_remove (GST_BIN (play_bin), sink);
1123 /* could not link this stream */
1124 caps = gst_pad_get_caps (subtitle_pad);
1125 capsstr = gst_caps_to_string (caps);
1126 GST_DEBUG_OBJECT (play_bin,
1127 "subtitle link failed when adding sink, caps %s, reason %d", capsstr,
1137 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1139 GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1140 GList *streaminfo = NULL, *s;
1141 gboolean need_vis = FALSE;
1142 gboolean need_text = FALSE;
1143 GstPad *textsrcpad = NULL, *pad = NULL;
1145 gboolean res = TRUE;
1147 /* get rid of existing sinks */
1148 if (play_bin->sinks) {
1149 remove_sinks (play_bin);
1151 GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1153 /* find out what to do */
1154 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1155 group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1157 } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1158 group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1159 play_bin->visualisation != NULL) {
1163 /* now actually connect everything */
1164 g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1165 for (s = streaminfo; s; s = g_list_next (s)) {
1166 GObject *obj = G_OBJECT (s->data);
1170 g_object_get (obj, "type", &type, NULL);
1171 g_object_get (obj, "object", &object, NULL);
1175 if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1177 sink = gen_vis_element (play_bin);
1179 sink = gen_audio_element (play_bin);
1183 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1185 res = add_sink (play_bin, sink, pad, NULL);
1186 gst_object_unref (pad);
1190 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1192 GstObject *parent = NULL, *grandparent = NULL;
1193 GstPad *ghost = NULL;
1195 sink = gen_text_element (play_bin);
1197 gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1199 /* This pad is from subtitle-bin, we need to create a ghost pad to have
1200 common grandparents */
1201 parent = gst_object_get_parent (GST_OBJECT (textsrcpad));
1203 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1204 gst_object_unref (textsrcpad);
1209 grandparent = gst_object_get_parent (parent);
1211 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1212 gst_object_unref (parent);
1213 gst_object_unref (textsrcpad);
1218 /* We ghost the pad on subtitle_bin only, if the text pad is from the
1219 media demuxer we keep it as it is */
1220 if (!GST_IS_PLAY_BIN (grandparent)) {
1221 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1222 "file, ghosting to a suitable hierarchy");
1223 ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1224 if (!GST_IS_PAD (ghost)) {
1225 GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1227 gst_object_unref (parent);
1228 gst_object_unref (grandparent);
1229 gst_object_unref (textsrcpad);
1234 if (gst_element_add_pad (GST_ELEMENT (grandparent), ghost)) {
1235 gst_object_unref (textsrcpad);
1236 textsrcpad = gst_object_ref (ghost);
1238 GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1239 gst_object_unref (ghost);
1240 gst_object_unref (textsrcpad);
1244 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1245 "no changes to hierarchy needed");
1248 gst_object_unref (parent);
1249 gst_object_unref (grandparent);
1251 sink = gen_video_element (play_bin);
1256 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1258 res = add_sink (play_bin, sink, pad, textsrcpad);
1259 gst_object_unref (pad);
1261 gst_object_unref (textsrcpad);
1265 /* remove the sinks now, pipeline get_state will now wait for the
1266 * sinks to preroll */
1267 if (play_bin->fakesink) {
1268 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1269 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1270 play_bin->fakesink = NULL;
1276 /* Send an event to our sinks until one of them works; don't then send to the
1277 * remaining sinks (unlike GstBin)
1280 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1282 GList *sinks = play_bin->sinks;
1283 gboolean res = TRUE;
1286 GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1288 gst_event_ref (event);
1289 if ((res = gst_element_send_event (sink, event))) {
1290 GST_DEBUG_OBJECT (play_bin,
1291 "Sent event succesfully to sink %" GST_PTR_FORMAT, sink);
1294 GST_DEBUG_OBJECT (play_bin,
1295 "Event failed when sent to sink %" GST_PTR_FORMAT, sink);
1297 sinks = g_list_next (sinks);
1300 gst_event_unref (event);
1306 do_playbin_seek (GstElement * element, GstEvent * event)
1311 gboolean was_playing = FALSE;
1314 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
1316 flush = flags & GST_SEEK_FLAG_FLUSH;
1321 /* need to call _get_state() since a bin state is only updated
1322 * with this call. */
1323 gst_element_get_state (element, &state, NULL, 0);
1324 was_playing = state == GST_STATE_PLAYING;
1327 gst_element_set_state (element, GST_STATE_PAUSED);
1328 gst_element_get_state (element, NULL, NULL, 50 * GST_MSECOND);
1332 GST_DEBUG_OBJECT (element, "Sending seek event to a sink");
1333 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1336 /* need to reset the stream time to 0 after a flushing seek */
1338 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
1341 /* and continue playing */
1342 gst_element_set_state (element, GST_STATE_PLAYING);
1347 /* We only want to send the event to a single sink (overriding GstBin's
1348 * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1349 * events appropriately. So, this is a messy duplication of code. */
1351 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1353 gboolean res = FALSE;
1354 GstEventType event_type = GST_EVENT_TYPE (event);
1357 switch (event_type) {
1358 case GST_EVENT_SEEK:
1359 res = do_playbin_seek (element, event);
1362 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1369 static GstStateChangeReturn
1370 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1372 GstStateChangeReturn ret;
1373 GstPlayBin *play_bin;
1375 play_bin = GST_PLAY_BIN (element);
1378 switch (transition) {
1379 case GST_STATE_CHANGE_READY_TO_PAUSED:
1380 /* this really is the easiest way to make the state change return
1381 * ASYNC until we added the sinks */
1382 if (!play_bin->fakesink) {
1383 play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1384 gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1391 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1392 if (ret == GST_STATE_CHANGE_FAILURE)
1395 switch (transition) {
1396 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1397 /* Set audio sink state to NULL to release the sound device,
1398 * but only if we own it (else we might be in chain-transition). */
1399 //if (play_bin->audio_sink != NULL &&
1400 // GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1401 // gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1404 case GST_STATE_CHANGE_PAUSED_TO_READY:
1405 /* Check for NULL because the state transition may be done by
1406 * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1407 * case, we don't want to run remove_sinks.
1408 * FIXME: should the NULL test be done in remove_sinks? Should we just
1409 * set the state to NULL in gst_play_bin_dispose?
1411 if (play_bin->cache != NULL) {
1412 remove_sinks (play_bin);
1414 if (play_bin->fakesink) {
1415 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1416 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1417 play_bin->fakesink = NULL;
1428 plugin_init (GstPlugin * plugin)
1430 GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1433 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1435 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1436 #endif /* ENABLE_NLS */
1438 return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1442 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1445 "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,