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 conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
623 scale = gst_element_factory_make ("videoscale", "vscale");
624 gst_bin_add (GST_BIN (element), identity);
625 gst_bin_add (GST_BIN (element), conv);
626 gst_bin_add (GST_BIN (element), scale);
627 gst_bin_add (GST_BIN (element), sink);
628 gst_element_link_pads (identity, "src", conv, "sink");
629 gst_element_link_pads (conv, "src", scale, "sink");
630 gst_element_link_pads (scale, "src", sink, "sink");
632 pad = gst_element_get_pad (identity, "sink");
633 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
634 gst_object_unref (pad);
636 gst_element_set_state (element, GST_STATE_READY);
638 /* since we're gonna add it to a bin but don't want to lose it,
639 * we keep a reference. */
640 gst_object_ref (element);
641 g_hash_table_insert (play_bin->cache, "vbin", element);
646 /* make an element for playback of video with subtitles embedded.
648 * +--------------------------------------------------+
649 * | tbin +-------------+ |
650 * | +-----+ | textoverlay | +------+ |
651 * | | csp | +--video_sink | | vbin | |
652 * video_sink-sink src+ +-text_sink src-sink | |
653 * | +-----+ | +-------------+ +------+ |
654 * text_sink-------------+ |
655 * +--------------------------------------------------+
659 gen_text_element (GstPlayBin * play_bin)
661 GstElement *element, *csp, *overlay, *vbin;
665 element = gst_bin_new ("textbin");
668 overlay = gst_element_factory_make ("textoverlay", "overlay");
670 /* Create the video rendering bin */
671 vbin = gen_video_element (play_bin);
673 /* If no overlay return the video bin */
675 GST_WARNING ("No overlay (pango) element, subtitles disabled");
679 /* Set some parameters */
680 g_object_set (G_OBJECT (overlay),
681 "halign", "center", "valign", "bottom", NULL);
682 if (play_bin->font_desc) {
683 g_object_set (G_OBJECT (overlay), "font-desc", play_bin->font_desc, NULL);
687 play_bin->textoverlay_element = GST_ELEMENT (gst_object_ref (overlay));
689 csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
691 /* Add our elements */
692 gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
695 gst_element_link_pads (csp, "src", overlay, "video_sink");
696 gst_element_link_pads (overlay, "src", vbin, "sink");
698 /* Add ghost pads on the subtitle bin */
699 pad = gst_element_get_pad (overlay, "text_sink");
700 gst_element_add_pad (element, gst_ghost_pad_new ("text_sink", pad));
701 gst_object_unref (pad);
703 pad = gst_element_get_pad (csp, "sink");
704 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
705 gst_object_unref (pad);
707 /* Set state to READY */
708 gst_element_set_state (element, GST_STATE_READY);
713 /* make the element (bin) that contains the elements needed to perform
716 * +-------------------------------------------------------------+
718 * | +---------+ +----------+ +---------+ +---------+ |
719 * | |audioconv| |audioscale| | volume | |audiosink| |
720 * | +-sink src-sink src-sink src-sink | |
721 * | | +---------+ +----------+ +---------+ +---------+ |
723 * +-------------------------------------------------------------+
727 gen_audio_element (GstPlayBin * play_bin)
736 element = g_hash_table_lookup (play_bin->cache, "abin");
737 if (element != NULL) {
740 element = gst_bin_new ("abin");
741 conv = gst_element_factory_make ("audioconvert", "aconv");
742 scale = gst_element_factory_make ("audioscale", "ascale");
744 volume = gst_element_factory_make ("volume", "volume");
745 g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
746 play_bin->volume_element = volume;
748 if (play_bin->audio_sink) {
749 sink = play_bin->audio_sink;
751 sink = gst_element_factory_make ("autoaudiosink", "audiosink");
753 sink = gst_element_factory_make ("alsasink", "audiosink");
756 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
757 (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
760 play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
763 gst_object_ref (sink);
764 g_hash_table_insert (play_bin->cache, "audio_sink", sink);
766 gst_bin_add (GST_BIN (element), conv);
767 //gst_bin_add (GST_BIN (element), scale);
768 gst_bin_add (GST_BIN (element), volume);
769 gst_bin_add (GST_BIN (element), sink);
771 gst_element_link_pads (conv, "src", /*scale, "sink");
772 gst_element_link_pads (scale, "src", */ volume, "sink");
773 gst_element_link_pads (volume, "src", sink, "sink");
775 pad = gst_element_get_pad (conv, "sink");
776 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
777 gst_object_unref (pad);
779 gst_element_set_state (element, GST_STATE_READY);
781 /* since we're gonna add it to a bin but don't want to lose it,
782 * we keep a reference. */
783 gst_object_ref (element);
784 g_hash_table_insert (play_bin->cache, "abin", element);
789 /* make the element (bin) that contains the elements needed to perform
790 * visualisation ouput. The idea is to split the audio using tee, then
791 * sending the output to the regular audio bin and the other output to
792 * the vis plugin that transforms it into a video that is rendered with the
793 * normal video bin. The video bin is run in a thread to make sure it does
794 * not block the audio playback pipeline.
796 * +--------------------------------------------------------------------+
798 * | +------+ +--------+ +----------------+ |
799 * | | tee | | aqueue | | abin ... | |
800 * | +-sink src-sink src-sink | |
801 * | | | | +--------+ +----------------+ |
803 * | | | | +------+ +---------+ +------+ +-----------+ |
804 * | | | | |vqueue| |audioconv| | vis | | vbin ... | |
805 * | | | src-sink src-sink src-sink src-sink | |
806 * | | | | +------+ +---------+ +------+ +-----------+ |
810 +---------------------------------------------------------------------+
813 gen_vis_element (GstPlayBin * play_bin)
821 GstElement *vqueue, *aqueue;
824 asink = gen_audio_element (play_bin);
827 vsink = gen_video_element (play_bin);
829 gst_object_unref (asink);
833 element = gst_bin_new ("visbin");
834 tee = gst_element_factory_make ("tee", "tee");
836 vqueue = gst_element_factory_make ("queue", "vqueue");
837 aqueue = gst_element_factory_make ("queue", "aqueue");
839 gst_bin_add (GST_BIN (element), asink);
840 gst_bin_add (GST_BIN (element), vqueue);
841 gst_bin_add (GST_BIN (element), aqueue);
842 gst_bin_add (GST_BIN (element), vsink);
843 gst_bin_add (GST_BIN (element), tee);
845 conv = gst_element_factory_make ("audioconvert", "aconv");
846 if (play_bin->visualisation) {
847 gst_object_ref (play_bin->visualisation);
848 vis = play_bin->visualisation;
850 vis = gst_element_factory_make ("goom", "vis");
853 gst_bin_add (GST_BIN (element), conv);
854 gst_bin_add (GST_BIN (element), vis);
856 gst_element_link_pads (vqueue, "src", conv, "sink");
857 gst_element_link_pads (conv, "src", vis, "sink");
858 gst_element_link_pads (vis, "src", vsink, "sink");
860 pad = gst_element_get_pad (aqueue, "sink");
861 rpad = gst_element_get_request_pad (tee, "src%d");
862 gst_pad_link (rpad, pad);
863 gst_object_unref (rpad);
864 gst_object_unref (pad);
865 gst_element_link_pads (aqueue, "src", asink, "sink");
867 pad = gst_element_get_pad (vqueue, "sink");
868 rpad = gst_element_get_request_pad (tee, "src%d");
869 gst_pad_link (rpad, pad);
870 gst_object_unref (rpad);
871 gst_object_unref (pad);
873 pad = gst_element_get_pad (tee, "sink");
874 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
875 gst_object_unref (pad);
880 /* get rid of all installed sinks */
882 remove_sinks (GstPlayBin * play_bin)
889 GST_DEBUG ("removesinks");
890 element = g_hash_table_lookup (play_bin->cache, "abin");
891 if (element != NULL) {
892 parent = gst_element_get_parent (element);
893 if (parent != NULL) {
894 /* we remove the element from the parent so that
895 * there is no unwanted state change when the parent
897 play_bin->sinks = g_list_remove (play_bin->sinks, element);
898 gst_element_set_state (element, GST_STATE_NULL);
899 gst_bin_remove (GST_BIN (parent), element);
900 gst_object_unref (parent);
902 pad = gst_element_get_pad (element, "sink");
904 peer = gst_pad_get_peer (pad);
906 gst_pad_unlink (peer, pad);
907 gst_object_unref (peer);
909 gst_object_unref (pad);
912 element = g_hash_table_lookup (play_bin->cache, "vbin");
913 if (element != NULL) {
914 parent = gst_element_get_parent (element);
915 if (parent != NULL) {
916 play_bin->sinks = g_list_remove (play_bin->sinks, element);
917 gst_element_set_state (element, GST_STATE_NULL);
918 gst_bin_remove (GST_BIN (parent), element);
919 gst_object_unref (parent);
921 pad = gst_element_get_pad (element, "sink");
923 peer = gst_pad_get_peer (pad);
925 gst_pad_unlink (peer, pad);
926 gst_object_unref (peer);
928 gst_object_unref (pad);
932 for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
933 GstElement *element = GST_ELEMENT (sinks->data);
937 pad = gst_element_get_pad (element, "sink");
939 GST_LOG ("removing sink %p", element);
941 peer = gst_pad_get_peer (pad);
943 gst_pad_unlink (peer, pad);
944 gst_object_unref (peer);
946 gst_object_unref (pad);
948 gst_element_set_state (element, GST_STATE_NULL);
949 gst_bin_remove (GST_BIN (play_bin), element);
951 g_list_free (play_bin->sinks);
952 play_bin->sinks = NULL;
954 /* FIXME: this is probably some refcounting problem */
955 if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
956 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
957 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
958 play_bin->visualisation);
961 if (play_bin->frame) {
962 gst_buffer_unref (play_bin->frame);
963 play_bin->frame = NULL;
966 if (play_bin->textoverlay_element) {
967 gst_object_unref (play_bin->textoverlay_element);
968 play_bin->textoverlay_element = NULL;
972 /* loop over the streams and set up the pipeline to play this
973 * media file. First we count the number of audio and video streams.
974 * If there is no video stream but there exists an audio stream,
975 * we install a visualisation pipeline.
977 * Also make sure to only connect the first audio and video pad. FIXME
978 * this should eventually be handled with a tuner interface so that
979 * one can switch the streams.
982 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
983 GstPad * subtitle_pad)
986 GstPadLinkReturn linkres;
988 GstStateChangeReturn stateret;
990 g_return_val_if_fail (sink != NULL, FALSE);
991 /* this is only for debugging */
992 parent = gst_pad_get_parent_element (srcpad);
994 GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
995 GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
996 gst_object_unref (parent);
999 /* bring it to the PAUSED state so we can link to the peer without
1000 * breaking the flow */
1001 if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
1002 GST_STATE_CHANGE_FAILURE)
1005 gst_bin_add (GST_BIN (play_bin), sink);
1007 /* we found a sink for this stream, now try to install it */
1008 sinkpad = gst_element_get_pad (sink, "sink");
1009 linkres = gst_pad_link (srcpad, sinkpad);
1010 gst_object_unref (sinkpad);
1012 /* try to link the pad of the sink to the stream */
1013 if (GST_PAD_LINK_FAILED (linkres))
1016 if (GST_IS_PAD (subtitle_pad)) {
1017 sinkpad = gst_element_get_pad (sink, "text_sink");
1018 linkres = gst_pad_link (subtitle_pad, sinkpad);
1019 gst_object_unref (sinkpad);
1022 /* try to link the subtitle pad of the sink to the stream */
1023 if (GST_PAD_LINK_FAILED (linkres)) {
1024 goto subtitle_failed;
1027 /* we got the sink succesfully linked, now keep the sink
1028 * in our internal list */
1029 play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1036 GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1044 /* could not link this stream */
1045 caps = gst_pad_get_caps (srcpad);
1046 capsstr = gst_caps_to_string (caps);
1047 g_warning ("could not link %s: %d", capsstr, linkres);
1048 GST_DEBUG_OBJECT (play_bin,
1049 "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1053 gst_element_set_state (sink, GST_STATE_NULL);
1054 gst_bin_remove (GST_BIN (play_bin), sink);
1062 /* could not link this stream */
1063 caps = gst_pad_get_caps (subtitle_pad);
1064 capsstr = gst_caps_to_string (caps);
1065 GST_DEBUG_OBJECT (play_bin,
1066 "subtitle link failed when adding sink, caps %s, reason %d", capsstr,
1076 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1078 GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1079 GList *streaminfo = NULL, *s;
1080 gboolean need_vis = FALSE;
1081 gboolean need_text = FALSE;
1082 GstPad *textsrcpad = NULL, *pad = NULL;
1084 gboolean res = TRUE;
1086 /* get rid of existing sinks */
1087 if (play_bin->sinks) {
1088 remove_sinks (play_bin);
1090 GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1092 /* find out what to do */
1093 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1094 group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1096 } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1097 group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1098 play_bin->visualisation != NULL) {
1102 /* now actually connect everything */
1103 g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1104 for (s = streaminfo; s; s = g_list_next (s)) {
1105 GObject *obj = G_OBJECT (s->data);
1109 g_object_get (obj, "type", &type, NULL);
1110 g_object_get (obj, "object", &object, NULL);
1114 if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1116 sink = gen_vis_element (play_bin);
1118 sink = gen_audio_element (play_bin);
1122 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1124 res = add_sink (play_bin, sink, pad, NULL);
1125 gst_object_unref (pad);
1129 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1131 GstObject *parent = NULL, *grandparent = NULL;
1132 GstPad *ghost = NULL;
1134 sink = gen_text_element (play_bin);
1136 gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1138 /* This pad is from subtitle-bin, we need to create a ghost pad to have
1139 common grandparents */
1140 parent = gst_object_get_parent (GST_OBJECT (textsrcpad));
1142 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1143 gst_object_unref (textsrcpad);
1148 grandparent = gst_object_get_parent (parent);
1150 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1151 gst_object_unref (parent);
1152 gst_object_unref (textsrcpad);
1157 /* We ghost the pad on subtitle_bin only, if the text pad is from the
1158 media demuxer we keep it as it is */
1159 if (!GST_IS_PLAY_BIN (grandparent)) {
1160 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1161 "file, ghosting to a suitable hierarchy");
1162 ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1163 if (!GST_IS_PAD (ghost)) {
1164 GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1166 gst_object_unref (parent);
1167 gst_object_unref (grandparent);
1168 gst_object_unref (textsrcpad);
1173 if (gst_element_add_pad (GST_ELEMENT (grandparent), ghost)) {
1174 gst_object_unref (textsrcpad);
1175 textsrcpad = gst_object_ref (ghost);
1177 GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1178 gst_object_unref (ghost);
1179 gst_object_unref (textsrcpad);
1183 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1184 "no changes to hierarchy needed");
1187 gst_object_unref (parent);
1188 gst_object_unref (grandparent);
1190 sink = gen_video_element (play_bin);
1195 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1197 res = add_sink (play_bin, sink, pad, textsrcpad);
1198 gst_object_unref (pad);
1200 gst_object_unref (textsrcpad);
1204 /* remove the sinks now, pipeline get_state will now wait for the
1205 * sinks to preroll */
1206 if (play_bin->fakesink) {
1207 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1208 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1209 play_bin->fakesink = NULL;
1215 /* Send an event to our sinks until one of them works; don't then send to the
1216 * remaining sinks (unlike GstBin)
1219 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1221 GList *sinks = play_bin->sinks;
1222 gboolean res = TRUE;
1225 GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1227 gst_event_ref (event);
1228 if ((res = gst_element_send_event (sink, event)))
1231 sinks = g_list_next (sinks);
1234 gst_event_unref (event);
1240 do_playbin_seek (GstElement * element, GstEvent * event)
1245 gboolean was_playing = FALSE;
1248 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
1250 flush = flags & GST_SEEK_FLAG_FLUSH;
1255 /* need to call _get_state() since a bin state is only updated
1256 * with this call. */
1257 gst_element_get_state (element, &state, NULL, 0);
1258 was_playing = state == GST_STATE_PLAYING;
1261 gst_element_set_state (element, GST_STATE_PAUSED);
1265 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1268 /* need to reset the stream time to 0 after a flushing seek */
1269 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
1271 /* and continue playing */
1272 gst_element_set_state (element, GST_STATE_PLAYING);
1277 /* We only want to send the event to a single sink (overriding GstBin's
1278 * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1279 * events appropriately. So, this is a messy duplication of code. */
1281 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1283 gboolean res = FALSE;
1284 GstEventType event_type = GST_EVENT_TYPE (event);
1287 switch (event_type) {
1288 case GST_EVENT_SEEK:
1289 res = do_playbin_seek (element, event);
1292 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1299 static GstStateChangeReturn
1300 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1302 GstStateChangeReturn ret;
1303 GstPlayBin *play_bin;
1305 play_bin = GST_PLAY_BIN (element);
1308 switch (transition) {
1309 case GST_STATE_CHANGE_READY_TO_PAUSED:
1310 /* this really is the easiest way to make the state change return
1311 * ASYNC until we added the sinks */
1312 if (!play_bin->fakesink) {
1313 play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1314 gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1321 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1322 if (ret == GST_STATE_CHANGE_FAILURE)
1325 switch (transition) {
1326 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1327 /* Set audio sink state to NULL to release the sound device,
1328 * but only if we own it (else we might be in chain-transition). */
1329 //if (play_bin->audio_sink != NULL &&
1330 // GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1331 // gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1334 case GST_STATE_CHANGE_PAUSED_TO_READY:
1335 /* Check for NULL because the state transition may be done by
1336 * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1337 * case, we don't want to run remove_sinks.
1338 * FIXME: should the NULL test be done in remove_sinks? Should we just
1339 * set the state to NULL in gst_play_bin_dispose?
1341 if (play_bin->cache != NULL) {
1342 remove_sinks (play_bin);
1344 if (play_bin->fakesink) {
1345 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1346 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1347 play_bin->fakesink = NULL;
1358 plugin_init (GstPlugin * plugin)
1360 GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1363 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1365 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1366 #endif /* ENABLE_NLS */
1368 return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1372 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1375 "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,