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) \
44 gst_object_ref (obj); \
45 g_value_set_object (gvalue, obj); \
48 #define _gst_gvalue_set_gstobject(gvalue,obj) \
49 g_value_set_object (gvalue, obj);
52 typedef struct _GstPlayBin GstPlayBin;
53 typedef struct _GstPlayBinClass GstPlayBinClass;
57 GstPlayBaseBin parent;
59 /* the configurable elements */
61 GstElement *audio_sink;
62 GstElement *video_sink;
63 GstElement *visualisation;
64 GstElement *volume_element;
65 GstElement *textoverlay_element;
68 /* these are the currently active sinks */
71 /* the last captured frame for snapshots */
74 /* our cache for the sinks */
77 /* font description */
81 struct _GstPlayBinClass
83 GstPlayBaseBinClass parent_class;
104 static void gst_play_bin_class_init (GstPlayBinClass * klass);
105 static void gst_play_bin_init (GstPlayBin * play_bin);
106 static void gst_play_bin_dispose (GObject * object);
108 static gboolean setup_sinks (GstPlayBaseBin * play_base_bin,
109 GstPlayBaseGroup * group);
110 static void remove_sinks (GstPlayBin * play_bin);
112 static void gst_play_bin_set_property (GObject * object, guint prop_id,
113 const GValue * value, GParamSpec * spec);
114 static void gst_play_bin_get_property (GObject * object, guint prop_id,
115 GValue * value, GParamSpec * spec);
117 static gboolean gst_play_bin_send_event (GstElement * element,
119 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
120 GstStateChange transition);
122 static GstElementClass *parent_class;
124 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
126 static GstElementDetails gst_play_bin_details = {
128 "Generic/Bin/Player",
129 "Autoplug and play media from an uri",
130 "Wim Taymans <wim@fluendo.com>"
134 gst_play_bin_get_type (void)
136 static GType gst_play_bin_type = 0;
138 if (!gst_play_bin_type) {
139 static const GTypeInfo gst_play_bin_info = {
140 sizeof (GstPlayBinClass),
143 (GClassInitFunc) gst_play_bin_class_init,
148 (GInstanceInitFunc) gst_play_bin_init,
152 gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
153 "GstPlayBin", &gst_play_bin_info, 0);
156 return gst_play_bin_type;
160 gst_play_bin_class_init (GstPlayBinClass * klass)
162 GObjectClass *gobject_klass;
163 GstElementClass *gstelement_klass;
164 GstBinClass *gstbin_klass;
165 GstPlayBaseBinClass *playbasebin_klass;
167 gobject_klass = (GObjectClass *) klass;
168 gstelement_klass = (GstElementClass *) klass;
169 gstbin_klass = (GstBinClass *) klass;
170 playbasebin_klass = (GstPlayBaseBinClass *) klass;
172 parent_class = g_type_class_ref (gst_play_base_bin_get_type ());
174 gobject_klass->set_property = gst_play_bin_set_property;
175 gobject_klass->get_property = gst_play_bin_get_property;
177 g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
178 g_param_spec_object ("video-sink", "Video Sink",
179 "the video output element to use (NULL = default sink)",
180 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
181 g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
182 g_param_spec_object ("audio-sink", "Audio Sink",
183 "the audio output element to use (NULL = default sink)",
184 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
185 g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
186 g_param_spec_object ("vis-plugin", "Vis plugin",
187 "the visualization element to use (NULL = none)",
188 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
189 g_object_class_install_property (gobject_klass, ARG_VOLUME,
190 g_param_spec_double ("volume", "volume", "volume",
191 0.0, VOLUME_MAX_DOUBLE, 1.0, G_PARAM_READWRITE));
192 g_object_class_install_property (gobject_klass, ARG_FRAME,
193 gst_param_spec_mini_object ("frame", "Frame",
194 "The last frame (NULL = no video available)",
195 GST_TYPE_BUFFER, G_PARAM_READABLE));
196 g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
197 g_param_spec_string ("subtitle-font-desc",
198 "Subtitle font description",
199 "Pango font description of font "
200 "to be used for subtitle rendering", NULL, G_PARAM_WRITABLE));
202 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_dispose);
204 gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
206 gstelement_klass->change_state =
207 GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
208 gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
210 playbasebin_klass->setup_output_pads = setup_sinks;
214 gst_play_bin_init (GstPlayBin * play_bin)
216 play_bin->video_sink = NULL;
217 play_bin->audio_sink = NULL;
218 play_bin->visualisation = NULL;
219 play_bin->volume_element = NULL;
220 play_bin->textoverlay_element = NULL;
221 play_bin->volume = 1.0;
222 play_bin->sinks = NULL;
223 play_bin->frame = NULL;
224 play_bin->font_desc = NULL;
225 play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
226 NULL, (GDestroyNotify) gst_object_unref);
230 gst_play_bin_dispose (GObject * object)
232 GstPlayBin *play_bin;
234 play_bin = GST_PLAY_BIN (object);
236 if (play_bin->cache != NULL) {
237 remove_sinks (play_bin);
238 g_hash_table_destroy (play_bin->cache);
239 play_bin->cache = NULL;
242 if (play_bin->audio_sink != NULL) {
243 gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
244 gst_object_unref (play_bin->audio_sink);
245 play_bin->audio_sink = NULL;
247 if (play_bin->video_sink != NULL) {
248 gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
249 gst_object_unref (play_bin->video_sink);
250 play_bin->video_sink = NULL;
252 if (play_bin->visualisation != NULL) {
253 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
254 gst_object_unref (play_bin->visualisation);
255 play_bin->visualisation = NULL;
257 g_free (play_bin->font_desc);
258 play_bin->font_desc = NULL;
260 G_OBJECT_CLASS (parent_class)->dispose (object);
265 gst_play_bin_set_property (GObject * object, guint prop_id,
266 const GValue * value, GParamSpec * pspec)
268 GstPlayBin *play_bin;
270 g_return_if_fail (GST_IS_PLAY_BIN (object));
272 play_bin = GST_PLAY_BIN (object);
276 if (play_bin->video_sink != NULL) {
277 gst_object_unref (play_bin->video_sink);
279 play_bin->video_sink = g_value_get_object (value);
280 if (play_bin->video_sink != NULL) {
281 gst_object_ref (play_bin->video_sink);
282 gst_object_sink (GST_OBJECT (play_bin->video_sink));
284 /* when changing the videosink, we just remove the
285 * video pipeline from the cache so that it will be
286 * regenerated with the new sink element */
287 g_hash_table_remove (play_bin->cache, "vbin");
290 if (play_bin->audio_sink != NULL) {
291 gst_object_unref (play_bin->audio_sink);
293 play_bin->audio_sink = g_value_get_object (value);
294 if (play_bin->audio_sink != NULL) {
295 gst_object_ref (play_bin->audio_sink);
296 gst_object_sink (GST_OBJECT (play_bin->audio_sink));
298 g_hash_table_remove (play_bin->cache, "abin");
301 if (play_bin->visualisation != NULL) {
302 gst_object_unref (play_bin->visualisation);
304 play_bin->visualisation = g_value_get_object (value);
305 if (play_bin->visualisation != NULL) {
306 gst_object_ref (play_bin->visualisation);
307 gst_object_sink (GST_OBJECT (play_bin->visualisation));
311 play_bin->volume = g_value_get_double (value);
312 if (play_bin->volume_element) {
313 g_object_set (G_OBJECT (play_bin->volume_element), "volume",
314 play_bin->volume, NULL);
318 g_free (play_bin->font_desc);
319 play_bin->font_desc = g_strdup (g_value_get_string (value));
320 if (play_bin->textoverlay_element) {
321 g_object_set (G_OBJECT (play_bin->textoverlay_element),
322 "font-desc", g_value_get_string (value), NULL);
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
332 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
335 GstPlayBin *play_bin;
337 g_return_if_fail (GST_IS_PLAY_BIN (object));
339 play_bin = GST_PLAY_BIN (object);
343 _gst_gvalue_set_gstobject (value, play_bin->video_sink);
346 _gst_gvalue_set_gstobject (value, play_bin->audio_sink);
349 _gst_gvalue_set_gstobject (value, play_bin->visualisation);
352 g_value_set_double (value, play_bin->volume);
355 gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 /* signal fired when the identity has received a new buffer. This is used for
364 * making screenshots.
367 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
369 GstPlayBin *play_bin = GST_PLAY_BIN (data);
371 if (play_bin->frame) {
372 gst_buffer_unref (play_bin->frame);
374 play_bin->frame = gst_buffer_ref (frame);
377 /* make the element (bin) that contains the elements needed to perform
378 * video display. We connect a handoff signal to identity so that we
379 * can grab snapshots. Identity's sinkpad is ghosted to vbin.
381 * +-------------------------------------------------------------+
383 * | +--------+ +----------+ +----------+ +---------+ |
384 * | |identity| |colorspace| |videoscale| |videosink| |
385 * | +-sink src-sink src-sink src-sink | |
386 * | | +---+----+ +----------+ +----------+ +---------+ |
388 * +----------|--------------------------------------------------+
391 /* FIXME: this might return NULL if no videosink was found, handle
394 gen_video_element (GstPlayBin * play_bin)
401 GstElement *identity;
404 /* first see if we have it in the cache */
405 element = g_hash_table_lookup (play_bin->cache, "vbin");
406 if (element != NULL) {
410 if (play_bin->video_sink) {
411 sink = play_bin->video_sink;
413 sink = gst_element_factory_make ("autovideosink", "videosink");
415 sink = gst_element_factory_make ("xvimagesink", "videosink");
417 /* FIXME: this warrants adding a CORE error category for missing
418 * elements/plugins */
420 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
421 (_("Both autovideosink and xvimagesink elements are missing.")),
426 gst_object_ref (sink);
427 g_hash_table_insert (play_bin->cache, "video_sink", sink);
430 element = gst_bin_new ("vbin");
431 identity = gst_element_factory_make ("identity", "id");
432 g_object_set (identity, "silent", TRUE, NULL);
433 g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
434 conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
435 scale = gst_element_factory_make ("videoscale", "vscale");
436 gst_bin_add (GST_BIN (element), identity);
437 gst_bin_add (GST_BIN (element), conv);
438 gst_bin_add (GST_BIN (element), scale);
439 gst_bin_add (GST_BIN (element), sink);
440 gst_element_link_pads (identity, "src", conv, "sink");
441 gst_element_link_pads (conv, "src", scale, "sink");
442 gst_element_link_pads (scale, "src", sink, "sink");
444 pad = gst_element_get_pad (identity, "sink");
445 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
446 gst_object_unref (pad);
448 gst_element_set_state (element, GST_STATE_READY);
450 /* since we're gonna add it to a bin but don't want to lose it,
451 * we keep a reference. */
452 gst_object_ref (element);
453 g_hash_table_insert (play_bin->cache, "vbin", element);
458 /* make an element for playback of video with subtitles embedded.
460 * +--------------------------------------------------+
461 * | tbin +-------------+ |
462 * | +-----+ | textoverlay | +------+ |
463 * | | csp | +--video_sink | | vbin | |
464 * video_sink-sink src+ +-text_sink src-sink | |
465 * | +-----+ | +-------------+ +------+ |
466 * text_sink-------------+ |
467 * +--------------------------------------------------+
471 gen_text_element (GstPlayBin * play_bin)
473 GstElement *element, *csp, *overlay, *vbin;
476 overlay = gst_element_factory_make ("textoverlay", "overlay");
477 g_object_set (G_OBJECT (overlay),
478 "halign", "center", "valign", "bottom", NULL);
479 play_bin->textoverlay_element = overlay;
480 if (play_bin->font_desc) {
481 g_object_set (G_OBJECT (play_bin->textoverlay_element),
482 "font-desc", play_bin->font_desc, NULL);
484 vbin = gen_video_element (play_bin);
486 g_warning ("No overlay (pango) element, subtitles disabled");
489 csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
490 element = gst_bin_new ("textbin");
491 gst_element_link_many (csp, overlay, vbin, NULL);
492 gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
494 pad = gst_element_get_pad (overlay, "text_sink");
495 #define gst_element_add_ghost_pad(element, pad, name) \
496 gst_element_add_pad (element, gst_ghost_pad_new (name, pad))
497 gst_element_add_ghost_pad (element, pad, "text_sink");
498 gst_object_unref (pad);
500 pad = gst_element_get_pad (csp, "sink");
501 gst_element_add_ghost_pad (element, pad, "sink");
502 gst_object_unref (pad);
507 /* make the element (bin) that contains the elements needed to perform
510 * +-------------------------------------------------------------+
512 * | +---------+ +----------+ +---------+ +---------+ |
513 * | |audioconv| |audioscale| | volume | |audiosink| |
514 * | +-sink src-sink src-sink src-sink | |
515 * | | +---------+ +----------+ +---------+ +---------+ |
517 * +-------------------------------------------------------------+
521 gen_audio_element (GstPlayBin * play_bin)
530 element = g_hash_table_lookup (play_bin->cache, "abin");
531 if (element != NULL) {
534 element = gst_bin_new ("abin");
535 conv = gst_element_factory_make ("audioconvert", "aconv");
536 scale = gst_element_factory_make ("audioscale", "ascale");
538 volume = gst_element_factory_make ("volume", "volume");
539 g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
540 play_bin->volume_element = volume;
542 if (play_bin->audio_sink) {
543 sink = play_bin->audio_sink;
545 sink = gst_element_factory_make ("autoaudiosink", "audiosink");
547 sink = gst_element_factory_make ("alsasink", "audiosink");
550 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
551 (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
554 sink = gst_element_factory_make ("alsasink", "audiosink");
556 g_warning ("could not create autoaudiosink element");
558 play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
561 gst_object_ref (sink);
562 g_hash_table_insert (play_bin->cache, "audio_sink", sink);
564 gst_bin_add (GST_BIN (element), conv);
565 //gst_bin_add (GST_BIN (element), scale);
566 gst_bin_add (GST_BIN (element), volume);
567 gst_bin_add (GST_BIN (element), sink);
569 gst_element_link_pads (conv, "src", /*scale, "sink");
570 gst_element_link_pads (scale, "src", */ volume, "sink");
571 gst_element_link_pads (volume, "src", sink, "sink");
573 pad = gst_element_get_pad (conv, "sink");
574 gst_element_add_ghost_pad (element, pad, "sink");
575 gst_object_unref (pad);
577 gst_element_set_state (element, GST_STATE_READY);
579 /* since we're gonna add it to a bin but don't want to lose it,
580 * we keep a reference. */
581 gst_object_ref (element);
582 g_hash_table_insert (play_bin->cache, "abin", element);
587 /* make the element (bin) that contains the elements needed to perform
588 * visualisation ouput. The idea is to split the audio using tee, then
589 * sending the output to the regular audio bin and the other output to
590 * the vis plugin that transforms it into a video that is rendered with the
591 * normal video bin. The video bin is run in a thread to make sure it does
592 * not block the audio playback pipeline.
594 * +--------------------------------------------------------------------------+
596 * | +------+ +----------------+ |
597 * | | tee | | abin ... | |
598 * | +-sink src-sink | |
599 * | | | | +----------------+ +-------------------+ |
600 * | | | | | vthread | |
601 * | | | | +---------+ +------+ +------+ | +--------------+ | |
602 * | | | | |audioconv| | vis | |vqueue| | | vbin ... | | |
603 * | | | src-sink src-sink src-sink src-sink | | |
604 * | | | | +---------+ +------+ +------+ | +--------------+ | |
605 * | | | | +-------------------+ |
608 +--------------------------------------------------------------------------+
611 gen_vis_element (GstPlayBin * play_bin)
619 GstElement *vqueue, *aqueue;
622 element = gst_bin_new ("visbin");
623 tee = gst_element_factory_make ("tee", "tee");
625 vqueue = gst_element_factory_make ("queue", "vqueue");
626 aqueue = gst_element_factory_make ("queue", "aqueue");
628 asink = gen_audio_element (play_bin);
629 vsink = gen_video_element (play_bin);
631 gst_bin_add (GST_BIN (element), asink);
632 gst_bin_add (GST_BIN (element), vqueue);
633 gst_bin_add (GST_BIN (element), aqueue);
634 gst_bin_add (GST_BIN (element), vsink);
635 gst_bin_add (GST_BIN (element), tee);
637 conv = gst_element_factory_make ("audioconvert", "aconv");
638 if (play_bin->visualisation) {
639 gst_object_ref (play_bin->visualisation);
640 vis = play_bin->visualisation;
642 vis = gst_element_factory_make ("goom", "vis");
645 gst_bin_add (GST_BIN (element), conv);
646 gst_bin_add (GST_BIN (element), vis);
648 gst_element_link_pads (conv, "src", vis, "sink");
649 gst_element_link_pads (vis, "src", vqueue, "sink");
651 gst_element_link_pads (vqueue, "src", vsink, "sink");
653 pad = gst_element_get_pad (aqueue, "sink");
654 rpad = gst_element_get_request_pad (tee, "src%d");
655 gst_pad_link (rpad, pad);
656 gst_object_unref (rpad);
657 gst_object_unref (pad);
658 gst_element_link_pads (aqueue, "src", asink, "sink");
660 pad = gst_element_get_pad (conv, "sink");
661 rpad = gst_element_get_request_pad (tee, "src%d");
662 gst_pad_link (rpad, pad);
663 gst_object_unref (rpad);
664 gst_object_unref (pad);
666 pad = gst_element_get_pad (tee, "sink");
667 gst_element_add_ghost_pad (element, pad, "sink");
668 gst_object_unref (pad);
673 /* get rid of all installed sinks */
675 remove_sinks (GstPlayBin * play_bin)
682 GST_DEBUG ("removesinks");
683 element = g_hash_table_lookup (play_bin->cache, "abin");
684 if (element != NULL) {
685 parent = gst_element_get_parent (element);
686 if (parent != NULL) {
687 /* we remove the element from the parent so that
688 * there is no unwanted state change when the parent
690 play_bin->sinks = g_list_remove (play_bin->sinks, element);
691 gst_element_set_state (element, GST_STATE_NULL);
692 gst_bin_remove (GST_BIN (parent), element);
693 gst_object_unref (parent);
695 pad = gst_element_get_pad (element, "sink");
697 peer = gst_pad_get_peer (pad);
699 gst_pad_unlink (peer, pad);
700 gst_object_unref (peer);
702 gst_object_unref (pad);
705 element = g_hash_table_lookup (play_bin->cache, "vbin");
706 if (element != NULL) {
707 parent = gst_element_get_parent (element);
708 if (parent != NULL) {
709 play_bin->sinks = g_list_remove (play_bin->sinks, element);
710 gst_element_set_state (element, GST_STATE_NULL);
711 gst_bin_remove (GST_BIN (parent), element);
712 gst_object_unref (parent);
714 pad = gst_element_get_pad (element, "sink");
716 peer = gst_pad_get_peer (pad);
718 gst_pad_unlink (peer, pad);
719 gst_object_unref (peer);
721 gst_object_unref (pad);
725 for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
726 GstElement *element = GST_ELEMENT (sinks->data);
730 pad = gst_element_get_pad (element, "sink");
732 GST_LOG ("removing sink %p", element);
734 peer = gst_pad_get_peer (pad);
736 gst_pad_unlink (peer, pad);
737 gst_object_unref (peer);
739 gst_object_unref (pad);
741 gst_element_set_state (element, GST_STATE_NULL);
742 gst_bin_remove (GST_BIN (play_bin), element);
744 g_list_free (play_bin->sinks);
745 play_bin->sinks = NULL;
747 /* FIXME: this is probably some refcounting problem */
748 if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
749 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
750 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
751 play_bin->visualisation);
754 if (play_bin->frame) {
755 gst_buffer_unref (play_bin->frame);
756 play_bin->frame = NULL;
759 play_bin->textoverlay_element = NULL;
762 /* loop over the streams and set up the pipeline to play this
763 * media file. First we count the number of audio and video streams.
764 * If there is no video stream but there exists an audio stream,
765 * we install a visualisation pipeline.
767 * Also make sure to only connect the first audio and video pad. FIXME
768 * this should eventually be handled with a tuner interface so that
769 * one can switch the streams.
772 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
775 GstPadLinkReturn linkres;
777 GstStateChangeReturn stateret;
779 /* this is only for debugging */
780 parent = gst_pad_get_parent_element (srcpad);
782 GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
783 GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
784 gst_object_unref (parent);
787 /* bring it to the PAUSED state so we can link to the peer without
788 * breaking the flow */
789 if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
790 GST_STATE_CHANGE_FAILURE)
793 gst_bin_add (GST_BIN (play_bin), sink);
795 /* we found a sink for this stream, now try to install it */
796 sinkpad = gst_element_get_pad (sink, "sink");
797 linkres = gst_pad_link (srcpad, sinkpad);
798 gst_object_unref (sinkpad);
800 /* try to link the pad of the sink to the stream */
801 if (GST_PAD_LINK_FAILED (linkres))
804 /* we got the sink succesfully linked, now keep the sink
805 * in out internal list */
806 play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
813 GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
821 /* could not link this stream */
822 caps = gst_pad_get_caps (srcpad);
823 capsstr = gst_caps_to_string (caps);
824 g_warning ("could not link %s: %d", capsstr, linkres);
825 GST_DEBUG_OBJECT (play_bin,
826 "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
830 gst_element_set_state (sink, GST_STATE_NULL);
831 gst_bin_remove (GST_BIN (play_bin), sink);
837 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
839 GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
840 GList *streaminfo = NULL, *s;
841 gboolean need_vis = FALSE;
842 gboolean need_text = FALSE;
843 GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
847 /* get rid of existing sinks */
848 if (play_bin->sinks) {
849 remove_sinks (play_bin);
851 GST_DEBUG ("setupsinks");
853 /* find out what to do */
854 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
855 group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
857 } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
858 group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
859 play_bin->visualisation != NULL) {
863 /* now actually connect everything */
864 g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
865 for (s = streaminfo; s; s = g_list_next (s)) {
866 GObject *obj = G_OBJECT (s->data);
870 g_object_get (obj, "type", &type, NULL);
871 g_object_get (obj, "object", &object, NULL);
875 if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
877 sink = gen_vis_element (play_bin);
879 sink = gen_audio_element (play_bin);
881 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
883 res = add_sink (play_bin, sink, pad);
884 gst_object_unref (pad);
888 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
890 sink = gen_text_element (play_bin);
892 textsinkpad = gst_element_get_pad (sink, "text_sink");
894 gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
896 gst_pad_link (textsrcpad, textsinkpad);
897 gst_object_unref (textsinkpad);
898 gst_object_unref (textsrcpad);
900 sink = gen_video_element (play_bin);
902 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
904 res = add_sink (play_bin, sink, pad);
905 gst_object_unref (pad);
908 /* remove the sinks now, pipeline get_state will now wait for the
909 * sinks to preroll */
910 if (play_bin->fakesink) {
911 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
912 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
913 play_bin->fakesink = NULL;
919 /* Send an event to our sinks until one of them works; don't then send to the
920 * remaining sinks (unlike GstBin)
923 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
925 GList *sinks = play_bin->sinks;
929 GstElement *sink = GST_ELEMENT_CAST (sinks->data);
931 gst_event_ref (event);
932 if ((res = gst_element_send_event (sink, event)))
935 sinks = g_list_next (sinks);
938 gst_event_unref (event);
944 do_playbin_seek (GstElement * element, GstEvent * event)
949 gboolean was_playing = FALSE;
952 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
954 flush = flags & GST_SEEK_FLAG_FLUSH;
959 /* need to call _get_state() since a bin state is only updated
961 gst_element_get_state (element, &state, NULL, 0);
962 was_playing = state == GST_STATE_PLAYING;
965 gst_element_set_state (element, GST_STATE_PAUSED);
969 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
972 /* need to reset the stream time to 0 after a flushing seek */
973 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
975 /* and continue playing */
976 gst_element_set_state (element, GST_STATE_PLAYING);
981 /* We only want to send the event to a single sink (overriding GstBin's
982 * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
983 * events appropriately. So, this is a messy duplication of code. */
985 gst_play_bin_send_event (GstElement * element, GstEvent * event)
987 gboolean res = FALSE;
988 GstEventType event_type = GST_EVENT_TYPE (event);
991 switch (event_type) {
993 res = do_playbin_seek (element, event);
996 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1003 static GstStateChangeReturn
1004 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1006 GstStateChangeReturn ret;
1007 GstPlayBin *play_bin;
1009 play_bin = GST_PLAY_BIN (element);
1012 switch (transition) {
1013 case GST_STATE_CHANGE_READY_TO_PAUSED:
1014 /* this really is the easiest way to make the state change return
1015 * ASYNC until we added the sinks */
1016 if (!play_bin->fakesink) {
1017 play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1018 gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1025 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1026 if (ret == GST_STATE_CHANGE_FAILURE)
1029 switch (transition) {
1030 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1031 /* Set audio sink state to NULL to release the sound device,
1032 * but only if we own it (else we might be in chain-transition). */
1033 //if (play_bin->audio_sink != NULL &&
1034 // GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1035 // gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1038 case GST_STATE_CHANGE_PAUSED_TO_READY:
1039 /* Check for NULL because the state transition may be done by
1040 * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1041 * case, we don't want to run remove_sinks.
1042 * FIXME: should the NULL test be done in remove_sinks? Should we just
1043 * set the state to NULL in gst_play_bin_dispose?
1045 if (play_bin->cache != NULL) {
1046 remove_sinks (play_bin);
1048 if (play_bin->fakesink) {
1049 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1050 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1051 play_bin->fakesink = NULL;
1062 plugin_init (GstPlugin * plugin)
1064 GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1066 return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1070 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1073 "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,