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 typedef struct _GstPlayBin GstPlayBin;
43 typedef struct _GstPlayBinClass GstPlayBinClass;
47 GstPlayBaseBin parent;
49 /* the configurable elements */
51 GstElement *audio_sink;
52 GstElement *video_sink;
53 GstElement *visualisation;
54 GstElement *volume_element;
55 GstElement *textoverlay_element;
58 /* these are the currently active sinks */
61 /* the last captured frame for snapshots */
64 /* our cache for the sinks */
67 /* font description */
71 struct _GstPlayBinClass
73 GstPlayBaseBinClass parent_class;
94 static void gst_play_bin_class_init (GstPlayBinClass * klass);
95 static void gst_play_bin_init (GstPlayBin * play_bin);
96 static void gst_play_bin_dispose (GObject * object);
98 static void setup_sinks (GstPlayBaseBin * play_base_bin,
99 GstPlayBaseGroup * group);
100 static void remove_sinks (GstPlayBin * play_bin);
102 static void gst_play_bin_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * spec);
104 static void gst_play_bin_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * spec);
107 static gboolean gst_play_bin_send_event (GstElement * element,
109 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
110 GstStateChange transition);
112 static GstElementClass *parent_class;
114 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
116 static GstElementDetails gst_play_bin_details = {
118 "Generic/Bin/Player",
119 "Autoplug and play media from an uri",
120 "Wim Taymans <wim@fluendo.com>"
124 gst_play_bin_get_type (void)
126 static GType gst_play_bin_type = 0;
128 if (!gst_play_bin_type) {
129 static const GTypeInfo gst_play_bin_info = {
130 sizeof (GstPlayBinClass),
133 (GClassInitFunc) gst_play_bin_class_init,
138 (GInstanceInitFunc) gst_play_bin_init,
142 gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
143 "GstPlayBin", &gst_play_bin_info, 0);
146 return gst_play_bin_type;
150 gst_play_bin_class_init (GstPlayBinClass * klass)
152 GObjectClass *gobject_klass;
153 GstElementClass *gstelement_klass;
154 GstBinClass *gstbin_klass;
155 GstPlayBaseBinClass *playbasebin_klass;
157 gobject_klass = (GObjectClass *) klass;
158 gstelement_klass = (GstElementClass *) klass;
159 gstbin_klass = (GstBinClass *) klass;
160 playbasebin_klass = (GstPlayBaseBinClass *) klass;
162 parent_class = g_type_class_ref (gst_play_base_bin_get_type ());
164 gobject_klass->set_property = gst_play_bin_set_property;
165 gobject_klass->get_property = gst_play_bin_get_property;
167 g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
168 g_param_spec_object ("video-sink", "Video Sink",
169 "the video output element to use (NULL = default sink)",
170 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
171 g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
172 g_param_spec_object ("audio-sink", "Audio Sink",
173 "the audio output element to use (NULL = default sink)",
174 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
175 g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
176 g_param_spec_object ("vis-plugin", "Vis plugin",
177 "the visualization element to use (NULL = none)",
178 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
179 g_object_class_install_property (gobject_klass, ARG_VOLUME,
180 g_param_spec_double ("volume", "volume", "volume",
181 0.0, VOLUME_MAX_DOUBLE, 1.0, G_PARAM_READWRITE));
182 g_object_class_install_property (gobject_klass, ARG_FRAME,
183 gst_param_spec_mini_object ("frame", "Frame",
184 "The last frame (NULL = no video available)",
185 GST_TYPE_BUFFER, G_PARAM_READABLE));
186 g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
187 g_param_spec_string ("subtitle-font-desc",
188 "Subtitle font description",
189 "Pango font description of font "
190 "to be used for subtitle rendering", NULL, G_PARAM_WRITABLE));
192 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_dispose);
194 gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
196 gstelement_klass->change_state =
197 GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
198 gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
200 playbasebin_klass->setup_output_pads = setup_sinks;
204 gst_play_bin_init (GstPlayBin * play_bin)
206 play_bin->video_sink = NULL;
207 play_bin->audio_sink = NULL;
208 play_bin->visualisation = NULL;
209 play_bin->volume_element = NULL;
210 play_bin->textoverlay_element = NULL;
211 play_bin->volume = 1.0;
212 play_bin->sinks = NULL;
213 play_bin->frame = NULL;
214 play_bin->font_desc = NULL;
215 play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
216 NULL, (GDestroyNotify) gst_object_unref);
220 gst_play_bin_dispose (GObject * object)
222 GstPlayBin *play_bin;
224 play_bin = GST_PLAY_BIN (object);
226 if (play_bin->cache != NULL) {
227 remove_sinks (play_bin);
228 g_hash_table_destroy (play_bin->cache);
229 play_bin->cache = NULL;
232 if (play_bin->audio_sink != NULL) {
233 gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
234 gst_object_unref (play_bin->audio_sink);
235 play_bin->audio_sink = NULL;
237 if (play_bin->video_sink != NULL) {
238 gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
239 gst_object_unref (play_bin->video_sink);
240 play_bin->video_sink = NULL;
242 if (play_bin->visualisation != NULL) {
243 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
244 gst_object_unref (play_bin->visualisation);
245 play_bin->visualisation = NULL;
247 g_free (play_bin->font_desc);
248 play_bin->font_desc = NULL;
250 G_OBJECT_CLASS (parent_class)->dispose (object);
255 gst_play_bin_set_property (GObject * object, guint prop_id,
256 const GValue * value, GParamSpec * pspec)
258 GstPlayBin *play_bin;
260 g_return_if_fail (GST_IS_PLAY_BIN (object));
262 play_bin = GST_PLAY_BIN (object);
266 if (play_bin->video_sink != NULL) {
267 gst_object_unref (play_bin->video_sink);
269 play_bin->video_sink = g_value_get_object (value);
270 if (play_bin->video_sink != NULL) {
271 gst_object_ref (play_bin->video_sink);
272 gst_object_sink (GST_OBJECT (play_bin->video_sink));
274 /* when changing the videosink, we just remove the
275 * video pipeline from the cache so that it will be
276 * regenerated with the new sink element */
277 g_hash_table_remove (play_bin->cache, "vbin");
280 if (play_bin->audio_sink != NULL) {
281 gst_object_unref (play_bin->audio_sink);
283 play_bin->audio_sink = g_value_get_object (value);
284 if (play_bin->audio_sink != NULL) {
285 gst_object_ref (play_bin->audio_sink);
286 gst_object_sink (GST_OBJECT (play_bin->audio_sink));
288 g_hash_table_remove (play_bin->cache, "abin");
291 if (play_bin->visualisation != NULL) {
292 gst_object_unref (play_bin->visualisation);
294 play_bin->visualisation = g_value_get_object (value);
295 if (play_bin->visualisation != NULL) {
296 gst_object_ref (play_bin->visualisation);
297 gst_object_sink (GST_OBJECT (play_bin->visualisation));
301 play_bin->volume = g_value_get_double (value);
302 if (play_bin->volume_element) {
303 g_object_set (G_OBJECT (play_bin->volume_element), "volume",
304 play_bin->volume, NULL);
308 g_free (play_bin->font_desc);
309 play_bin->font_desc = g_strdup (g_value_get_string (value));
310 if (play_bin->textoverlay_element) {
311 g_object_set (G_OBJECT (play_bin->textoverlay_element),
312 "font-desc", g_value_get_string (value), NULL);
316 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
325 GstPlayBin *play_bin;
327 g_return_if_fail (GST_IS_PLAY_BIN (object));
329 play_bin = GST_PLAY_BIN (object);
333 g_value_set_object (value, play_bin->video_sink);
336 g_value_set_object (value, play_bin->audio_sink);
339 g_value_set_object (value, play_bin->visualisation);
342 g_value_set_double (value, play_bin->volume);
345 gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
353 /* signal fired when the identity has received a new buffer. This is used for
354 * making screenshots.
357 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
359 GstPlayBin *play_bin = GST_PLAY_BIN (data);
361 if (play_bin->frame) {
362 gst_buffer_unref (play_bin->frame);
364 play_bin->frame = gst_buffer_ref (frame);
367 /* make the element (bin) that contains the elements needed to perform
368 * video display. We connect a handoff signal to identity so that we
369 * can grab snapshots. Identity's sinkpad is ghosted to vbin.
371 * +-------------------------------------------------------------+
373 * | +--------+ +----------+ +----------+ +---------+ |
374 * | |identity| |colorspace| |videoscale| |videosink| |
375 * | +-sink src-sink src-sink src-sink | |
376 * | | +---+----+ +----------+ +----------+ +---------+ |
378 * +----------|--------------------------------------------------+
381 /* FIXME: this might return NULL if no videosink was found, handle
384 gen_video_element (GstPlayBin * play_bin)
391 GstElement *identity;
394 /* first see if we have it in the cache */
395 element = g_hash_table_lookup (play_bin->cache, "vbin");
396 if (element != NULL) {
400 if (play_bin->video_sink) {
401 sink = play_bin->video_sink;
403 sink = gst_element_factory_make ("autovideosink", "videosink");
405 sink = gst_element_factory_make ("xvimagesink", "videosink");
407 /* FIXME: this warrants adding a CORE error category for missing
408 * elements/plugins */
410 GST_ELEMENT_ERROR (play_bin, CORE, FAILED,
411 (_("Both autovideosink and xvimagesink elements are missing.")),
416 gst_object_ref (sink);
417 g_hash_table_insert (play_bin->cache, "video_sink", sink);
420 element = gst_bin_new ("vbin");
421 identity = gst_element_factory_make ("identity", "id");
422 g_object_set (identity, "silent", TRUE, NULL);
423 g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
424 conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
425 scale = gst_element_factory_make ("videoscale", "vscale");
426 gst_bin_add (GST_BIN (element), identity);
427 gst_bin_add (GST_BIN (element), conv);
428 gst_bin_add (GST_BIN (element), scale);
429 gst_bin_add (GST_BIN (element), sink);
430 gst_element_link_pads (identity, "src", conv, "sink");
431 gst_element_link_pads (conv, "src", scale, "sink");
432 gst_element_link_pads (scale, "src", sink, "sink");
434 pad = gst_element_get_pad (identity, "sink");
435 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
436 gst_object_unref (pad);
438 gst_element_set_state (element, GST_STATE_READY);
440 /* since we're gonna add it to a bin but don't want to lose it,
441 * we keep a reference. */
442 gst_object_ref (element);
443 g_hash_table_insert (play_bin->cache, "vbin", element);
448 /* make an element for playback of video with subtitles embedded.
450 * +--------------------------------------------------+
451 * | tbin +-------------+ |
452 * | +-----+ | textoverlay | +------+ |
453 * | | csp | +--video_sink | | vbin | |
454 * video_sink-sink src+ +-text_sink src-sink | |
455 * | +-----+ | +-------------+ +------+ |
456 * text_sink-------------+ |
457 * +--------------------------------------------------+
461 gen_text_element (GstPlayBin * play_bin)
463 GstElement *element, *csp, *overlay, *vbin;
466 overlay = gst_element_factory_make ("textoverlay", "overlay");
467 g_object_set (G_OBJECT (overlay),
468 "halign", "center", "valign", "bottom", NULL);
469 play_bin->textoverlay_element = overlay;
470 if (play_bin->font_desc) {
471 g_object_set (G_OBJECT (play_bin->textoverlay_element),
472 "font-desc", play_bin->font_desc, NULL);
474 vbin = gen_video_element (play_bin);
476 g_warning ("No overlay (pango) element, subtitles disabled");
479 csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
480 element = gst_bin_new ("textbin");
481 gst_element_link_many (csp, overlay, vbin, NULL);
482 gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
484 pad = gst_element_get_pad (overlay, "text_sink");
485 #define gst_element_add_ghost_pad(element, pad, name) \
486 gst_element_add_pad (element, gst_ghost_pad_new (name, pad))
487 gst_element_add_ghost_pad (element, pad, "text_sink");
488 gst_object_unref (pad);
490 pad = gst_element_get_pad (csp, "sink");
491 gst_element_add_ghost_pad (element, pad, "sink");
492 gst_object_unref (pad);
497 /* make the element (bin) that contains the elements needed to perform
500 * +-------------------------------------------------------------+
502 * | +---------+ +----------+ +---------+ +---------+ |
503 * | |audioconv| |audioscale| | volume | |audiosink| |
504 * | +-sink src-sink src-sink src-sink | |
505 * | | +---------+ +----------+ +---------+ +---------+ |
507 * +-------------------------------------------------------------+
511 gen_audio_element (GstPlayBin * play_bin)
520 element = g_hash_table_lookup (play_bin->cache, "abin");
521 if (element != NULL) {
524 element = gst_bin_new ("abin");
525 conv = gst_element_factory_make ("audioconvert", "aconv");
526 scale = gst_element_factory_make ("audioscale", "ascale");
528 volume = gst_element_factory_make ("volume", "volume");
529 g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
530 play_bin->volume_element = volume;
532 if (play_bin->audio_sink) {
533 sink = play_bin->audio_sink;
535 sink = gst_element_factory_make ("autoaudiiosink", "audiosink");
537 sink = gst_element_factory_make ("alsasink", "audiosink");
539 /* FIXME: this warrants adding a CORE error category for missing
540 * elements/plugins */
542 GST_ELEMENT_ERROR (play_bin, CORE, FAILED,
543 (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
546 sink = gst_element_factory_make ("alsasink", "audiosink");
548 g_warning ("could not create autoaudiosink element");
550 play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
553 gst_object_ref (sink);
554 g_hash_table_insert (play_bin->cache, "audio_sink", sink);
556 gst_bin_add (GST_BIN (element), conv);
557 //gst_bin_add (GST_BIN (element), scale);
558 gst_bin_add (GST_BIN (element), volume);
559 gst_bin_add (GST_BIN (element), sink);
561 gst_element_link_pads (conv, "src", /*scale, "sink");
562 gst_element_link_pads (scale, "src", */ volume, "sink");
563 gst_element_link_pads (volume, "src", sink, "sink");
565 pad = gst_element_get_pad (conv, "sink");
566 gst_element_add_ghost_pad (element, pad, "sink");
567 gst_object_unref (pad);
569 gst_element_set_state (element, GST_STATE_READY);
571 /* since we're gonna add it to a bin but don't want to lose it,
572 * we keep a reference. */
573 gst_object_ref (element);
574 g_hash_table_insert (play_bin->cache, "abin", element);
579 /* make the element (bin) that contains the elements needed to perform
580 * visualisation ouput. The idea is to split the audio using tee, then
581 * sending the output to the regular audio bin and the other output to
582 * the vis plugin that transforms it into a video that is rendered with the
583 * normal video bin. The video bin is run in a thread to make sure it does
584 * not block the audio playback pipeline.
586 * +--------------------------------------------------------------------------+
588 * | +------+ +----------------+ |
589 * | | tee | | abin ... | |
590 * | +-sink src-sink | |
591 * | | | | +----------------+ +-------------------+ |
592 * | | | | | vthread | |
593 * | | | | +---------+ +------+ +------+ | +--------------+ | |
594 * | | | | |audioconv| | vis | |vqueue| | | vbin ... | | |
595 * | | | src-sink src-sink src-sink src-sink | | |
596 * | | | | +---------+ +------+ +------+ | +--------------+ | |
597 * | | | | +-------------------+ |
600 +--------------------------------------------------------------------------+
603 gen_vis_element (GstPlayBin * play_bin)
611 GstElement *vqueue, *aqueue;
614 element = gst_bin_new ("visbin");
615 tee = gst_element_factory_make ("tee", "tee");
617 vqueue = gst_element_factory_make ("queue", "vqueue");
618 aqueue = gst_element_factory_make ("queue", "aqueue");
620 asink = gen_audio_element (play_bin);
621 vsink = gen_video_element (play_bin);
623 gst_bin_add (GST_BIN (element), asink);
624 gst_bin_add (GST_BIN (element), vqueue);
625 gst_bin_add (GST_BIN (element), aqueue);
626 gst_bin_add (GST_BIN (element), vsink);
627 gst_bin_add (GST_BIN (element), tee);
629 conv = gst_element_factory_make ("audioconvert", "aconv");
630 if (play_bin->visualisation) {
631 gst_object_ref (play_bin->visualisation);
632 vis = play_bin->visualisation;
634 vis = gst_element_factory_make ("goom", "vis");
637 gst_bin_add (GST_BIN (element), conv);
638 gst_bin_add (GST_BIN (element), vis);
640 gst_element_link_pads (conv, "src", vis, "sink");
641 gst_element_link_pads (vis, "src", vqueue, "sink");
643 gst_element_link_pads (vqueue, "src", vsink, "sink");
645 pad = gst_element_get_pad (aqueue, "sink");
646 rpad = gst_element_get_request_pad (tee, "src%d");
647 gst_pad_link (rpad, pad);
648 gst_object_unref (rpad);
649 gst_object_unref (pad);
650 gst_element_link_pads (aqueue, "src", asink, "sink");
652 pad = gst_element_get_pad (conv, "sink");
653 rpad = gst_element_get_request_pad (tee, "src%d");
654 gst_pad_link (rpad, pad);
655 gst_object_unref (rpad);
656 gst_object_unref (pad);
658 pad = gst_element_get_pad (tee, "sink");
659 gst_element_add_ghost_pad (element, pad, "sink");
660 gst_object_unref (pad);
665 /* get rid of all installed sinks */
667 remove_sinks (GstPlayBin * play_bin)
674 GST_DEBUG ("removesinks");
675 element = g_hash_table_lookup (play_bin->cache, "abin");
676 if (element != NULL) {
677 parent = gst_element_get_parent (element);
678 if (parent != NULL) {
679 /* we remove the element from the parent so that
680 * there is no unwanted state change when the parent
682 play_bin->sinks = g_list_remove (play_bin->sinks, element);
683 gst_element_set_state (element, GST_STATE_NULL);
684 gst_bin_remove (GST_BIN (parent), element);
685 gst_object_unref (parent);
687 pad = gst_element_get_pad (element, "sink");
689 peer = gst_pad_get_peer (pad);
691 gst_pad_unlink (peer, pad);
692 gst_object_unref (peer);
694 gst_object_unref (pad);
697 element = g_hash_table_lookup (play_bin->cache, "vbin");
698 if (element != NULL) {
699 parent = gst_element_get_parent (element);
700 if (parent != NULL) {
701 play_bin->sinks = g_list_remove (play_bin->sinks, element);
702 gst_element_set_state (element, GST_STATE_NULL);
703 gst_bin_remove (GST_BIN (parent), element);
704 gst_object_unref (parent);
706 pad = gst_element_get_pad (element, "sink");
708 peer = gst_pad_get_peer (pad);
710 gst_pad_unlink (peer, pad);
711 gst_object_unref (peer);
713 gst_object_unref (pad);
717 for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
718 GstElement *element = GST_ELEMENT (sinks->data);
722 pad = gst_element_get_pad (element, "sink");
724 GST_LOG ("removing sink %p", element);
726 peer = gst_pad_get_peer (pad);
728 gst_pad_unlink (peer, pad);
729 gst_object_unref (peer);
731 gst_object_unref (pad);
733 gst_element_set_state (element, GST_STATE_NULL);
734 gst_bin_remove (GST_BIN (play_bin), element);
736 g_list_free (play_bin->sinks);
737 play_bin->sinks = NULL;
739 /* FIXME: this is probably some refcounting problem */
740 if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
741 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
742 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
743 play_bin->visualisation);
746 if (play_bin->frame) {
747 gst_buffer_unref (play_bin->frame);
748 play_bin->frame = NULL;
751 play_bin->textoverlay_element = NULL;
754 /* loop over the streams and set up the pipeline to play this
755 * media file. First we count the number of audio and video streams.
756 * If there is no video stream but there exists an audio stream,
757 * we install a visualisation pipeline.
759 * Also make sure to only connect the first audio and video pad. FIXME
760 * this should eventually be handled with a tuner interface so that
761 * one can switch the streams.
764 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
767 GstPadLinkReturn res;
770 gst_bin_add (GST_BIN (play_bin), sink);
772 /* we found a sink for this stream, now try to install it */
773 sinkpad = gst_element_get_pad (sink, "sink");
774 res = gst_pad_link (srcpad, sinkpad);
775 gst_object_unref (sinkpad);
777 /* this is only for debugging */
778 parent = gst_pad_get_parent_element (srcpad);
780 GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
781 GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
782 gst_object_unref (parent);
785 /* try to link the pad of the sink to the stream */
789 /* could not link this stream */
790 capsstr = gst_caps_to_string (gst_pad_get_caps (srcpad));
791 g_warning ("could not link %s: %d", capsstr, res);
794 gst_element_set_state (sink, GST_STATE_NULL);
795 gst_bin_remove (GST_BIN (play_bin), sink);
797 /* we got the sink succesfully linked, now keep the sink
798 * in out internal list */
799 play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
800 gst_element_set_state (sink,
801 (GST_STATE (play_bin) == GST_STATE_PLAYING) ?
802 GST_STATE_PLAYING : GST_STATE_PAUSED);
809 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
811 GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
812 GList *streaminfo = NULL, *s;
813 gboolean need_vis = FALSE;
814 gboolean need_text = FALSE;
815 GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
818 /* get rid of existing sinks */
819 if (play_bin->sinks) {
820 remove_sinks (play_bin);
822 GST_DEBUG ("setupsinks");
824 /* find out what to do */
825 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
826 group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
828 } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
829 group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
830 play_bin->visualisation != NULL) {
834 /* now actually connect everything */
835 g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
836 for (s = streaminfo; s; s = g_list_next (s)) {
837 GObject *obj = G_OBJECT (s->data);
841 g_object_get (obj, "type", &type, NULL);
842 g_object_get (obj, "object", &object, NULL);
846 if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
848 sink = gen_vis_element (play_bin);
850 sink = gen_audio_element (play_bin);
852 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
854 add_sink (play_bin, sink, pad);
855 gst_object_unref (pad);
859 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
861 sink = gen_text_element (play_bin);
863 textsinkpad = gst_element_get_pad (sink, "text_sink");
865 gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
867 gst_pad_link (textsrcpad, textsinkpad);
868 gst_object_unref (textsinkpad);
869 gst_object_unref (textsrcpad);
871 sink = gen_video_element (play_bin);
873 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
875 add_sink (play_bin, sink, pad);
876 gst_object_unref (pad);
879 /* remove the sinks now, pipeline get_state will now wait for the
880 * sinks to preroll */
881 if (play_bin->fakesink) {
882 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
883 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
884 play_bin->fakesink = NULL;
888 /* Send an event to our sinks until one of them works; don't then send to the
889 * remaining sinks (unlike GstBin)
892 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
894 GList *sinks = play_bin->sinks;
898 GstElement *sink = GST_ELEMENT_CAST (sinks->data);
900 gst_event_ref (event);
901 if ((res = gst_element_send_event (sink, event)))
904 sinks = g_list_next (sinks);
907 gst_event_unref (event);
913 do_playbin_seek (GstElement * element, GstEvent * event)
918 gboolean was_playing = FALSE;
921 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
923 flush = flags & GST_SEEK_FLAG_FLUSH;
928 /* need to call _get_state() since a bin state is only updated
930 gst_element_get_state (element, &state, NULL, 0);
931 was_playing = state == GST_STATE_PLAYING;
934 gst_element_set_state (element, GST_STATE_PAUSED);
938 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
941 /* need to reset the stream time to 0 after a flushing seek */
942 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
944 /* and continue playing */
945 gst_element_set_state (element, GST_STATE_PLAYING);
950 /* We only want to send the event to a single sink (overriding GstBin's
951 * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
952 * events appropriately. So, this is a messy duplication of code. */
954 gst_play_bin_send_event (GstElement * element, GstEvent * event)
956 gboolean res = FALSE;
957 GstEventType event_type = GST_EVENT_TYPE (event);
960 switch (event_type) {
962 res = do_playbin_seek (element, event);
965 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
972 static GstStateChangeReturn
973 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
975 GstStateChangeReturn ret;
976 GstPlayBin *play_bin;
978 play_bin = GST_PLAY_BIN (element);
981 switch (transition) {
982 case GST_STATE_CHANGE_READY_TO_PAUSED:
983 /* this really is the easiest way to make the state change return
984 * ASYNC until we added the sinks */
985 if (!play_bin->fakesink) {
986 play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
987 gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
994 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
995 if (ret == GST_STATE_CHANGE_FAILURE)
998 switch (transition) {
999 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1000 /* Set audio sink state to NULL to release the sound device,
1001 * but only if we own it (else we might be in chain-transition). */
1002 //if (play_bin->audio_sink != NULL &&
1003 // GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1004 // gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1007 case GST_STATE_CHANGE_PAUSED_TO_READY:
1008 /* Check for NULL because the state transition may be done by
1009 * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1010 * case, we don't want to run remove_sinks.
1011 * FIXME: should the NULL test be done in remove_sinks? Should we just
1012 * set the state to NULL in gst_play_bin_dispose?
1014 if (play_bin->cache != NULL) {
1015 remove_sinks (play_bin);
1017 if (play_bin->fakesink) {
1018 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1019 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1020 play_bin->fakesink = NULL;
1031 plugin_init (GstPlugin * plugin)
1033 GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1035 return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1039 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1042 "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,