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 const 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_peek_parent (klass);
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)
759 element = g_hash_table_lookup (play_bin->cache, "abin");
760 if (element != NULL) {
763 element = gst_bin_new ("abin");
764 conv = gst_element_factory_make ("audioconvert", "aconv");
766 goto no_audioconvert;
768 scale = gst_element_factory_make ("audioresample", "aresample");
770 goto no_audioresample;
772 volume = gst_element_factory_make ("volume", "volume");
773 g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
774 play_bin->volume_element = volume;
776 if (play_bin->audio_sink) {
777 sink = play_bin->audio_sink;
779 sink = gst_element_factory_make ("autoaudiosink", "audiosink");
781 sink = gst_element_factory_make ("alsasink", "audiosink");
784 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
785 (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
788 play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
791 gst_object_ref (sink);
792 g_hash_table_insert (play_bin->cache, "audio_sink", sink);
794 gst_bin_add (GST_BIN (element), conv);
795 gst_bin_add (GST_BIN (element), scale);
796 gst_bin_add (GST_BIN (element), volume);
797 gst_bin_add (GST_BIN (element), sink);
799 gst_element_link_pads (conv, "src", scale, "sink");
800 gst_element_link_pads (scale, "src", volume, "sink");
801 gst_element_link_pads (volume, "src", sink, "sink");
803 pad = gst_element_get_pad (conv, "sink");
804 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
805 gst_object_unref (pad);
807 gst_element_set_state (element, GST_STATE_READY);
809 /* since we're gonna add it to a bin but don't want to lose it,
810 * we keep a reference. */
811 gst_object_ref (element);
812 g_hash_table_insert (play_bin->cache, "abin", element);
818 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
819 (_("Missing element '%s' - check your GStreamer installation."),
820 "audioconvert"), ("possibly a liboil version mismatch?"));
821 gst_object_unref (element);
827 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
828 (_("Missing element '%s' - check your GStreamer installation."),
829 "audioresample"), ("possibly a liboil version mismatch?"));
830 gst_object_unref (element);
835 /* make the element (bin) that contains the elements needed to perform
836 * visualisation ouput. The idea is to split the audio using tee, then
837 * sending the output to the regular audio bin and the other output to
838 * the vis plugin that transforms it into a video that is rendered with the
839 * normal video bin. The video bin is run in a thread to make sure it does
840 * not block the audio playback pipeline.
842 * +--------------------------------------------------------------------+
844 * | +------+ +--------+ +----------------+ |
845 * | | tee | | aqueue | | abin ... | |
846 * | +-sink src-sink src-sink | |
847 * | | | | +--------+ +----------------+ |
849 * | | | | +------+ +---------+ +------+ +-----------+ |
850 * | | | | |vqueue| |audioconv| | vis | | vbin ... | |
851 * | | | src-sink src-sink src-sink src-sink | |
852 * | | | | +------+ +---------+ +------+ +-----------+ |
856 +---------------------------------------------------------------------+
859 gen_vis_element (GstPlayBin * play_bin)
867 GstElement *vqueue, *aqueue;
870 asink = gen_audio_element (play_bin);
873 vsink = gen_video_element (play_bin);
875 gst_object_unref (asink);
879 element = gst_bin_new ("visbin");
880 tee = gst_element_factory_make ("tee", "tee");
882 vqueue = gst_element_factory_make ("queue", "vqueue");
883 aqueue = gst_element_factory_make ("queue", "aqueue");
885 gst_bin_add (GST_BIN (element), asink);
886 gst_bin_add (GST_BIN (element), vqueue);
887 gst_bin_add (GST_BIN (element), aqueue);
888 gst_bin_add (GST_BIN (element), vsink);
889 gst_bin_add (GST_BIN (element), tee);
891 conv = gst_element_factory_make ("audioconvert", "aconv");
893 goto no_audioconvert;
895 if (play_bin->visualisation) {
896 gst_object_ref (play_bin->visualisation);
897 vis = play_bin->visualisation;
899 vis = gst_element_factory_make ("goom", "vis");
902 gst_bin_add (GST_BIN (element), conv);
903 gst_bin_add (GST_BIN (element), vis);
905 gst_element_link_pads (vqueue, "src", conv, "sink");
906 gst_element_link_pads (conv, "src", vis, "sink");
907 gst_element_link_pads (vis, "src", vsink, "sink");
909 pad = gst_element_get_pad (aqueue, "sink");
910 rpad = gst_element_get_request_pad (tee, "src%d");
911 gst_pad_link (rpad, pad);
912 gst_object_unref (rpad);
913 gst_object_unref (pad);
914 gst_element_link_pads (aqueue, "src", asink, "sink");
916 pad = gst_element_get_pad (vqueue, "sink");
917 rpad = gst_element_get_request_pad (tee, "src%d");
918 gst_pad_link (rpad, pad);
919 gst_object_unref (rpad);
920 gst_object_unref (pad);
922 pad = gst_element_get_pad (tee, "sink");
923 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
924 gst_object_unref (pad);
930 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
931 (_("Missing element '%s' - check your GStreamer installation."),
932 "audioconvert"), ("possibly a liboil version mismatch?"));
933 gst_object_unref (element);
938 /* get rid of all installed sinks */
940 remove_sinks (GstPlayBin * play_bin)
947 GST_DEBUG ("removesinks");
948 element = g_hash_table_lookup (play_bin->cache, "abin");
949 if (element != NULL) {
950 parent = gst_element_get_parent (element);
951 if (parent != NULL) {
952 /* we remove the element from the parent so that
953 * there is no unwanted state change when the parent
955 play_bin->sinks = g_list_remove (play_bin->sinks, element);
956 gst_element_set_state (element, GST_STATE_NULL);
957 gst_bin_remove (GST_BIN (parent), element);
958 gst_object_unref (parent);
960 pad = gst_element_get_pad (element, "sink");
962 peer = gst_pad_get_peer (pad);
964 gst_pad_unlink (peer, pad);
965 gst_object_unref (peer);
967 gst_object_unref (pad);
970 element = g_hash_table_lookup (play_bin->cache, "vbin");
971 if (element != NULL) {
972 parent = gst_element_get_parent (element);
973 if (parent != NULL) {
974 play_bin->sinks = g_list_remove (play_bin->sinks, element);
975 gst_element_set_state (element, GST_STATE_NULL);
976 gst_bin_remove (GST_BIN (parent), element);
977 gst_object_unref (parent);
979 pad = gst_element_get_pad (element, "sink");
981 peer = gst_pad_get_peer (pad);
983 gst_pad_unlink (peer, pad);
984 gst_object_unref (peer);
986 gst_object_unref (pad);
990 for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
991 GstElement *element = GST_ELEMENT (sinks->data);
995 pad = gst_element_get_pad (element, "sink");
997 GST_LOG ("removing sink %p", element);
999 peer = gst_pad_get_peer (pad);
1001 gst_pad_unlink (peer, pad);
1002 gst_object_unref (peer);
1004 gst_object_unref (pad);
1006 gst_element_set_state (element, GST_STATE_NULL);
1007 gst_bin_remove (GST_BIN (play_bin), element);
1009 g_list_free (play_bin->sinks);
1010 play_bin->sinks = NULL;
1012 /* FIXME: this is probably some refcounting problem */
1013 if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
1014 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
1015 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
1016 play_bin->visualisation);
1019 if (play_bin->frame) {
1020 gst_buffer_unref (play_bin->frame);
1021 play_bin->frame = NULL;
1024 if (play_bin->textoverlay_element) {
1025 gst_object_unref (play_bin->textoverlay_element);
1026 play_bin->textoverlay_element = NULL;
1030 /* loop over the streams and set up the pipeline to play this
1031 * media file. First we count the number of audio and video streams.
1032 * If there is no video stream but there exists an audio stream,
1033 * we install a visualisation pipeline.
1035 * Also make sure to only connect the first audio and video pad. FIXME
1036 * this should eventually be handled with a tuner interface so that
1037 * one can switch the streams.
1040 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
1041 GstPad * subtitle_pad)
1044 GstPadLinkReturn linkres;
1046 GstStateChangeReturn stateret;
1048 g_return_val_if_fail (sink != NULL, FALSE);
1049 /* this is only for debugging */
1050 parent = gst_pad_get_parent_element (srcpad);
1052 GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
1053 GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
1054 gst_object_unref (parent);
1057 /* bring it to the PAUSED state so we can link to the peer without
1058 * breaking the flow */
1059 if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
1060 GST_STATE_CHANGE_FAILURE)
1063 gst_bin_add (GST_BIN (play_bin), sink);
1065 /* we found a sink for this stream, now try to install it */
1066 sinkpad = gst_element_get_pad (sink, "sink");
1067 linkres = gst_pad_link (srcpad, sinkpad);
1068 gst_object_unref (sinkpad);
1070 /* try to link the pad of the sink to the stream */
1071 if (GST_PAD_LINK_FAILED (linkres))
1074 if (GST_IS_PAD (subtitle_pad)) {
1075 sinkpad = gst_element_get_pad (sink, "text_sink");
1076 linkres = gst_pad_link (subtitle_pad, sinkpad);
1077 gst_object_unref (sinkpad);
1080 /* try to link the subtitle pad of the sink to the stream */
1081 if (GST_PAD_LINK_FAILED (linkres)) {
1082 goto subtitle_failed;
1085 /* we got the sink succesfully linked, now keep the sink
1086 * in our internal list */
1087 play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1094 GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1102 /* could not link this stream */
1103 caps = gst_pad_get_caps (srcpad);
1104 capsstr = gst_caps_to_string (caps);
1105 g_warning ("could not link %s: %d", capsstr, linkres);
1106 GST_DEBUG_OBJECT (play_bin,
1107 "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1109 gst_caps_unref (caps);
1111 gst_element_set_state (sink, GST_STATE_NULL);
1112 gst_bin_remove (GST_BIN (play_bin), sink);
1120 /* could not link this stream */
1121 caps = gst_pad_get_caps (subtitle_pad);
1122 capsstr = gst_caps_to_string (caps);
1123 GST_DEBUG_OBJECT (play_bin,
1124 "subtitle link failed when adding sink, caps %s, reason %d", capsstr,
1127 gst_caps_unref (caps);
1134 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1136 GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1137 GList *streaminfo = NULL, *s;
1138 gboolean need_vis = FALSE;
1139 gboolean need_text = FALSE;
1140 GstPad *textsrcpad = NULL, *pad = NULL;
1142 gboolean res = TRUE;
1144 /* get rid of existing sinks */
1145 if (play_bin->sinks) {
1146 remove_sinks (play_bin);
1148 GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1150 /* find out what to do */
1151 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1152 group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1154 } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1155 group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1156 play_bin->visualisation != NULL) {
1160 /* now actually connect everything */
1161 g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1162 for (s = streaminfo; s; s = g_list_next (s)) {
1163 GObject *obj = G_OBJECT (s->data);
1167 g_object_get (obj, "type", &type, NULL);
1168 g_object_get (obj, "object", &object, NULL);
1172 if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1174 sink = gen_vis_element (play_bin);
1176 sink = gen_audio_element (play_bin);
1180 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1182 res = add_sink (play_bin, sink, pad, NULL);
1183 gst_object_unref (pad);
1187 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1189 GstObject *parent = NULL, *grandparent = NULL;
1190 GstPad *ghost = NULL;
1192 sink = gen_text_element (play_bin);
1194 gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1196 /* This pad is from subtitle-bin, we need to create a ghost pad to have
1197 common grandparents */
1198 parent = gst_object_get_parent (GST_OBJECT (textsrcpad));
1200 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1201 gst_object_unref (textsrcpad);
1206 grandparent = gst_object_get_parent (parent);
1208 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1209 gst_object_unref (parent);
1210 gst_object_unref (textsrcpad);
1215 /* We ghost the pad on subtitle_bin only, if the text pad is from the
1216 media demuxer we keep it as it is */
1217 if (!GST_IS_PLAY_BIN (grandparent)) {
1218 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1219 "file, ghosting to a suitable hierarchy");
1220 ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1221 if (!GST_IS_PAD (ghost)) {
1222 GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1224 gst_object_unref (parent);
1225 gst_object_unref (grandparent);
1226 gst_object_unref (textsrcpad);
1231 if (gst_element_add_pad (GST_ELEMENT (grandparent), ghost)) {
1232 gst_object_unref (textsrcpad);
1233 textsrcpad = gst_object_ref (ghost);
1235 GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1236 gst_object_unref (ghost);
1237 gst_object_unref (textsrcpad);
1241 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1242 "no changes to hierarchy needed");
1245 gst_object_unref (parent);
1246 gst_object_unref (grandparent);
1248 sink = gen_video_element (play_bin);
1253 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1255 res = add_sink (play_bin, sink, pad, textsrcpad);
1256 gst_object_unref (pad);
1258 gst_object_unref (textsrcpad);
1262 /* remove the sinks now, pipeline get_state will now wait for the
1263 * sinks to preroll */
1264 if (play_bin->fakesink) {
1265 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1266 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1267 play_bin->fakesink = NULL;
1273 /* Send an event to our sinks until one of them works; don't then send to the
1274 * remaining sinks (unlike GstBin)
1277 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1279 GList *sinks = play_bin->sinks;
1280 gboolean res = TRUE;
1283 GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1285 gst_event_ref (event);
1286 if ((res = gst_element_send_event (sink, event))) {
1287 GST_DEBUG_OBJECT (play_bin,
1288 "Sent event succesfully to sink %" GST_PTR_FORMAT, sink);
1291 GST_DEBUG_OBJECT (play_bin,
1292 "Event failed when sent to sink %" GST_PTR_FORMAT, sink);
1294 sinks = g_list_next (sinks);
1297 gst_event_unref (event);
1303 do_playbin_seek (GstElement * element, GstEvent * event)
1308 gboolean was_playing = FALSE;
1311 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
1313 flush = flags & GST_SEEK_FLAG_FLUSH;
1318 /* need to call _get_state() since a bin state is only updated
1319 * with this call. */
1320 gst_element_get_state (element, &state, NULL, 0);
1321 was_playing = state == GST_STATE_PLAYING;
1324 gst_element_set_state (element, GST_STATE_PAUSED);
1325 gst_element_get_state (element, NULL, NULL, 50 * GST_MSECOND);
1329 GST_DEBUG_OBJECT (element, "Sending seek event to a sink");
1330 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1333 /* need to reset the stream time to 0 after a flushing seek */
1335 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
1338 /* and continue playing */
1339 gst_element_set_state (element, GST_STATE_PLAYING);
1344 /* We only want to send the event to a single sink (overriding GstBin's
1345 * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1346 * events appropriately. So, this is a messy duplication of code. */
1348 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1350 gboolean res = FALSE;
1351 GstEventType event_type = GST_EVENT_TYPE (event);
1354 switch (event_type) {
1355 case GST_EVENT_SEEK:
1356 res = do_playbin_seek (element, event);
1359 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1366 static GstStateChangeReturn
1367 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1369 GstStateChangeReturn ret;
1370 GstPlayBin *play_bin;
1372 play_bin = GST_PLAY_BIN (element);
1375 switch (transition) {
1376 case GST_STATE_CHANGE_READY_TO_PAUSED:
1377 /* this really is the easiest way to make the state change return
1378 * ASYNC until we added the sinks */
1379 if (!play_bin->fakesink) {
1380 play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1381 gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1388 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1389 if (ret == GST_STATE_CHANGE_FAILURE)
1392 switch (transition) {
1393 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1394 /* Set audio sink state to NULL to release the sound device,
1395 * but only if we own it (else we might be in chain-transition). */
1396 //if (play_bin->audio_sink != NULL &&
1397 // GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1398 // gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1401 case GST_STATE_CHANGE_PAUSED_TO_READY:
1402 /* Check for NULL because the state transition may be done by
1403 * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1404 * case, we don't want to run remove_sinks.
1405 * FIXME: should the NULL test be done in remove_sinks? Should we just
1406 * set the state to NULL in gst_play_bin_dispose?
1408 if (play_bin->cache != NULL) {
1409 remove_sinks (play_bin);
1411 if (play_bin->fakesink) {
1412 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1413 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1414 play_bin->fakesink = NULL;
1425 plugin_init (GstPlugin * plugin)
1427 GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1430 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1432 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1433 #endif /* ENABLE_NLS */
1435 return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1439 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1442 "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,