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 *volume_element;
69 GstElement *textoverlay_element;
72 /* these are the currently active sinks */
75 /* the last captured frame for snapshots */
78 /* our cache for the sinks */
81 /* font description */
85 struct _GstPlayBinClass
87 GstPlayBaseBinClass parent_class;
108 static void gst_play_bin_class_init (GstPlayBinClass * klass);
109 static void gst_play_bin_init (GstPlayBin * play_bin);
110 static void gst_play_bin_dispose (GObject * object);
112 static gboolean setup_sinks (GstPlayBaseBin * play_base_bin,
113 GstPlayBaseGroup * group);
114 static void remove_sinks (GstPlayBin * play_bin);
116 static void gst_play_bin_set_property (GObject * object, guint prop_id,
117 const GValue * value, GParamSpec * spec);
118 static void gst_play_bin_get_property (GObject * object, guint prop_id,
119 GValue * value, GParamSpec * spec);
121 static gboolean gst_play_bin_send_event (GstElement * element,
123 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
124 GstStateChange transition);
126 static GstElementClass *parent_class;
128 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
130 static GstElementDetails gst_play_bin_details = {
132 "Generic/Bin/Player",
133 "Autoplug and play media from an uri",
134 "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->volume_element = NULL;
224 play_bin->textoverlay_element = NULL;
225 play_bin->volume = 1.0;
226 play_bin->sinks = NULL;
227 play_bin->frame = NULL;
228 play_bin->font_desc = NULL;
229 play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
230 NULL, (GDestroyNotify) gst_object_unref);
234 gst_play_bin_dispose (GObject * object)
236 GstPlayBin *play_bin;
238 play_bin = GST_PLAY_BIN (object);
240 if (play_bin->cache != NULL) {
241 remove_sinks (play_bin);
242 g_hash_table_destroy (play_bin->cache);
243 play_bin->cache = NULL;
246 if (play_bin->audio_sink != NULL) {
247 gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
248 gst_object_unref (play_bin->audio_sink);
249 play_bin->audio_sink = NULL;
251 if (play_bin->video_sink != NULL) {
252 gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
253 gst_object_unref (play_bin->video_sink);
254 play_bin->video_sink = NULL;
256 if (play_bin->visualisation != NULL) {
257 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
258 gst_object_unref (play_bin->visualisation);
259 play_bin->visualisation = NULL;
261 g_free (play_bin->font_desc);
262 play_bin->font_desc = NULL;
264 G_OBJECT_CLASS (parent_class)->dispose (object);
269 gst_play_bin_set_property (GObject * object, guint prop_id,
270 const GValue * value, GParamSpec * pspec)
272 GstPlayBin *play_bin;
274 g_return_if_fail (GST_IS_PLAY_BIN (object));
276 play_bin = GST_PLAY_BIN (object);
280 if (play_bin->video_sink != NULL) {
281 gst_object_unref (play_bin->video_sink);
283 play_bin->video_sink = g_value_get_object (value);
284 if (play_bin->video_sink != NULL) {
285 gst_object_ref (play_bin->video_sink);
286 gst_object_sink (GST_OBJECT (play_bin->video_sink));
288 /* when changing the videosink, we just remove the
289 * video pipeline from the cache so that it will be
290 * regenerated with the new sink element */
291 g_hash_table_remove (play_bin->cache, "vbin");
294 if (play_bin->audio_sink != NULL) {
295 gst_object_unref (play_bin->audio_sink);
297 play_bin->audio_sink = g_value_get_object (value);
298 if (play_bin->audio_sink != NULL) {
299 gst_object_ref (play_bin->audio_sink);
300 gst_object_sink (GST_OBJECT (play_bin->audio_sink));
302 g_hash_table_remove (play_bin->cache, "abin");
305 if (play_bin->visualisation != NULL) {
306 gst_object_unref (play_bin->visualisation);
308 play_bin->visualisation = g_value_get_object (value);
309 if (play_bin->visualisation != NULL) {
310 gst_object_ref (play_bin->visualisation);
311 gst_object_sink (GST_OBJECT (play_bin->visualisation));
315 play_bin->volume = g_value_get_double (value);
316 if (play_bin->volume_element) {
317 g_object_set (G_OBJECT (play_bin->volume_element), "volume",
318 play_bin->volume, NULL);
322 g_free (play_bin->font_desc);
323 play_bin->font_desc = g_strdup (g_value_get_string (value));
324 if (play_bin->textoverlay_element) {
325 g_object_set (G_OBJECT (play_bin->textoverlay_element),
326 "font-desc", g_value_get_string (value), NULL);
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
339 GstPlayBin *play_bin;
341 g_return_if_fail (GST_IS_PLAY_BIN (object));
343 play_bin = GST_PLAY_BIN (object);
347 _gst_gvalue_set_gstobject (value, play_bin->video_sink);
350 _gst_gvalue_set_gstobject (value, play_bin->audio_sink);
353 _gst_gvalue_set_gstobject (value, play_bin->visualisation);
356 g_value_set_double (value, play_bin->volume);
359 gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 /* signal fired when the identity has received a new buffer. This is used for
368 * making screenshots.
371 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
373 GstPlayBin *play_bin = GST_PLAY_BIN (data);
375 gst_mini_object_replace ((GstMiniObject **) & play_bin->frame,
376 GST_MINI_OBJECT_CAST (frame));
378 /* applications need to know the buffer caps,
379 * make sure they are always set on the frame */
380 if (GST_BUFFER_CAPS (play_bin->frame) == NULL) {
383 if ((pad = gst_element_get_pad (identity, "sink"))) {
384 gst_buffer_set_caps (play_bin->frame, GST_PAD_CAPS (pad));
385 gst_object_unref (pad);
390 /* make the element (bin) that contains the elements needed to perform
391 * video display. We connect a handoff signal to identity so that we
392 * can grab snapshots. Identity's sinkpad is ghosted to vbin.
394 * +-------------------------------------------------------------+
396 * | +--------+ +----------+ +----------+ +---------+ |
397 * | |identity| |colorspace| |videoscale| |videosink| |
398 * | +-sink src-sink src-sink src-sink | |
399 * | | +---+----+ +----------+ +----------+ +---------+ |
401 * +----------|--------------------------------------------------+
404 /* FIXME: this might return NULL if no videosink was found, handle
407 gen_video_element (GstPlayBin * play_bin)
414 GstElement *identity;
417 /* first see if we have it in the cache */
418 element = g_hash_table_lookup (play_bin->cache, "vbin");
419 if (element != NULL) {
423 if (play_bin->video_sink) {
424 sink = play_bin->video_sink;
426 sink = gst_element_factory_make ("autovideosink", "videosink");
428 sink = gst_element_factory_make ("xvimagesink", "videosink");
430 /* FIXME: this warrants adding a CORE error category for missing
431 * elements/plugins */
433 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
434 (_("Both autovideosink and xvimagesink elements are missing.")),
439 gst_object_ref (sink);
440 g_hash_table_insert (play_bin->cache, "video_sink", sink);
443 element = gst_bin_new ("vbin");
444 identity = gst_element_factory_make ("identity", "id");
445 g_object_set (identity, "silent", TRUE, NULL);
446 g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
447 conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
448 scale = gst_element_factory_make ("videoscale", "vscale");
449 gst_bin_add (GST_BIN (element), identity);
450 gst_bin_add (GST_BIN (element), conv);
451 gst_bin_add (GST_BIN (element), scale);
452 gst_bin_add (GST_BIN (element), sink);
453 gst_element_link_pads (identity, "src", conv, "sink");
454 gst_element_link_pads (conv, "src", scale, "sink");
455 gst_element_link_pads (scale, "src", sink, "sink");
457 pad = gst_element_get_pad (identity, "sink");
458 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
459 gst_object_unref (pad);
461 gst_element_set_state (element, GST_STATE_READY);
463 /* since we're gonna add it to a bin but don't want to lose it,
464 * we keep a reference. */
465 gst_object_ref (element);
466 g_hash_table_insert (play_bin->cache, "vbin", element);
471 /* make an element for playback of video with subtitles embedded.
473 * +--------------------------------------------------+
474 * | tbin +-------------+ |
475 * | +-----+ | textoverlay | +------+ |
476 * | | csp | +--video_sink | | vbin | |
477 * video_sink-sink src+ +-text_sink src-sink | |
478 * | +-----+ | +-------------+ +------+ |
479 * text_sink-------------+ |
480 * +--------------------------------------------------+
484 gen_text_element (GstPlayBin * play_bin)
486 GstElement *element, *csp, *overlay, *vbin;
489 overlay = gst_element_factory_make ("textoverlay", "overlay");
490 g_object_set (G_OBJECT (overlay),
491 "halign", "center", "valign", "bottom", NULL);
492 play_bin->textoverlay_element = overlay;
493 if (play_bin->font_desc) {
494 g_object_set (G_OBJECT (play_bin->textoverlay_element),
495 "font-desc", play_bin->font_desc, NULL);
497 vbin = gen_video_element (play_bin);
499 g_warning ("No overlay (pango) element, subtitles disabled");
502 csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
503 element = gst_bin_new ("textbin");
504 gst_element_link_many (csp, overlay, vbin, NULL);
505 gst_bin_add_many (GST_BIN (element), csp, overlay, vbin, NULL);
507 pad = gst_element_get_pad (overlay, "text_sink");
508 #define gst_element_add_ghost_pad(element, pad, name) \
509 gst_element_add_pad (element, gst_ghost_pad_new (name, pad))
510 gst_element_add_ghost_pad (element, pad, "text_sink");
511 gst_object_unref (pad);
513 pad = gst_element_get_pad (csp, "sink");
514 gst_element_add_ghost_pad (element, pad, "sink");
515 gst_object_unref (pad);
520 /* make the element (bin) that contains the elements needed to perform
523 * +-------------------------------------------------------------+
525 * | +---------+ +----------+ +---------+ +---------+ |
526 * | |audioconv| |audioscale| | volume | |audiosink| |
527 * | +-sink src-sink src-sink src-sink | |
528 * | | +---------+ +----------+ +---------+ +---------+ |
530 * +-------------------------------------------------------------+
534 gen_audio_element (GstPlayBin * play_bin)
543 element = g_hash_table_lookup (play_bin->cache, "abin");
544 if (element != NULL) {
547 element = gst_bin_new ("abin");
548 conv = gst_element_factory_make ("audioconvert", "aconv");
549 scale = gst_element_factory_make ("audioscale", "ascale");
551 volume = gst_element_factory_make ("volume", "volume");
552 g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
553 play_bin->volume_element = volume;
555 if (play_bin->audio_sink) {
556 sink = play_bin->audio_sink;
558 sink = gst_element_factory_make ("autoaudiosink", "audiosink");
560 sink = gst_element_factory_make ("alsasink", "audiosink");
563 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
564 (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
567 sink = gst_element_factory_make ("alsasink", "audiosink");
569 g_warning ("could not create autoaudiosink element");
571 play_bin->audio_sink = GST_ELEMENT (gst_object_ref (sink));
574 gst_object_ref (sink);
575 g_hash_table_insert (play_bin->cache, "audio_sink", sink);
577 gst_bin_add (GST_BIN (element), conv);
578 //gst_bin_add (GST_BIN (element), scale);
579 gst_bin_add (GST_BIN (element), volume);
580 gst_bin_add (GST_BIN (element), sink);
582 gst_element_link_pads (conv, "src", /*scale, "sink");
583 gst_element_link_pads (scale, "src", */ volume, "sink");
584 gst_element_link_pads (volume, "src", sink, "sink");
586 pad = gst_element_get_pad (conv, "sink");
587 gst_element_add_ghost_pad (element, pad, "sink");
588 gst_object_unref (pad);
590 gst_element_set_state (element, GST_STATE_READY);
592 /* since we're gonna add it to a bin but don't want to lose it,
593 * we keep a reference. */
594 gst_object_ref (element);
595 g_hash_table_insert (play_bin->cache, "abin", element);
600 /* make the element (bin) that contains the elements needed to perform
601 * visualisation ouput. The idea is to split the audio using tee, then
602 * sending the output to the regular audio bin and the other output to
603 * the vis plugin that transforms it into a video that is rendered with the
604 * normal video bin. The video bin is run in a thread to make sure it does
605 * not block the audio playback pipeline.
607 * +--------------------------------------------------------------------------+
609 * | +------+ +----------------+ |
610 * | | tee | | abin ... | |
611 * | +-sink src-sink | |
612 * | | | | +----------------+ +-------------------+ |
613 * | | | | | vthread | |
614 * | | | | +---------+ +------+ +------+ | +--------------+ | |
615 * | | | | |audioconv| | vis | |vqueue| | | vbin ... | | |
616 * | | | src-sink src-sink src-sink src-sink | | |
617 * | | | | +---------+ +------+ +------+ | +--------------+ | |
618 * | | | | +-------------------+ |
621 +--------------------------------------------------------------------------+
624 gen_vis_element (GstPlayBin * play_bin)
632 GstElement *vqueue, *aqueue;
635 element = gst_bin_new ("visbin");
636 tee = gst_element_factory_make ("tee", "tee");
638 vqueue = gst_element_factory_make ("queue", "vqueue");
639 aqueue = gst_element_factory_make ("queue", "aqueue");
641 asink = gen_audio_element (play_bin);
642 vsink = gen_video_element (play_bin);
644 gst_bin_add (GST_BIN (element), asink);
645 gst_bin_add (GST_BIN (element), vqueue);
646 gst_bin_add (GST_BIN (element), aqueue);
647 gst_bin_add (GST_BIN (element), vsink);
648 gst_bin_add (GST_BIN (element), tee);
650 conv = gst_element_factory_make ("audioconvert", "aconv");
651 if (play_bin->visualisation) {
652 gst_object_ref (play_bin->visualisation);
653 vis = play_bin->visualisation;
655 vis = gst_element_factory_make ("goom", "vis");
658 gst_bin_add (GST_BIN (element), conv);
659 gst_bin_add (GST_BIN (element), vis);
661 gst_element_link_pads (conv, "src", vis, "sink");
662 gst_element_link_pads (vis, "src", vqueue, "sink");
664 gst_element_link_pads (vqueue, "src", vsink, "sink");
666 pad = gst_element_get_pad (aqueue, "sink");
667 rpad = gst_element_get_request_pad (tee, "src%d");
668 gst_pad_link (rpad, pad);
669 gst_object_unref (rpad);
670 gst_object_unref (pad);
671 gst_element_link_pads (aqueue, "src", asink, "sink");
673 pad = gst_element_get_pad (conv, "sink");
674 rpad = gst_element_get_request_pad (tee, "src%d");
675 gst_pad_link (rpad, pad);
676 gst_object_unref (rpad);
677 gst_object_unref (pad);
679 pad = gst_element_get_pad (tee, "sink");
680 gst_element_add_ghost_pad (element, pad, "sink");
681 gst_object_unref (pad);
686 /* get rid of all installed sinks */
688 remove_sinks (GstPlayBin * play_bin)
695 GST_DEBUG ("removesinks");
696 element = g_hash_table_lookup (play_bin->cache, "abin");
697 if (element != NULL) {
698 parent = gst_element_get_parent (element);
699 if (parent != NULL) {
700 /* we remove the element from the parent so that
701 * there is no unwanted state change when the parent
703 play_bin->sinks = g_list_remove (play_bin->sinks, element);
704 gst_element_set_state (element, GST_STATE_NULL);
705 gst_bin_remove (GST_BIN (parent), element);
706 gst_object_unref (parent);
708 pad = gst_element_get_pad (element, "sink");
710 peer = gst_pad_get_peer (pad);
712 gst_pad_unlink (peer, pad);
713 gst_object_unref (peer);
715 gst_object_unref (pad);
718 element = g_hash_table_lookup (play_bin->cache, "vbin");
719 if (element != NULL) {
720 parent = gst_element_get_parent (element);
721 if (parent != NULL) {
722 play_bin->sinks = g_list_remove (play_bin->sinks, element);
723 gst_element_set_state (element, GST_STATE_NULL);
724 gst_bin_remove (GST_BIN (parent), element);
725 gst_object_unref (parent);
727 pad = gst_element_get_pad (element, "sink");
729 peer = gst_pad_get_peer (pad);
731 gst_pad_unlink (peer, pad);
732 gst_object_unref (peer);
734 gst_object_unref (pad);
738 for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
739 GstElement *element = GST_ELEMENT (sinks->data);
743 pad = gst_element_get_pad (element, "sink");
745 GST_LOG ("removing sink %p", element);
747 peer = gst_pad_get_peer (pad);
749 gst_pad_unlink (peer, pad);
750 gst_object_unref (peer);
752 gst_object_unref (pad);
754 gst_element_set_state (element, GST_STATE_NULL);
755 gst_bin_remove (GST_BIN (play_bin), element);
757 g_list_free (play_bin->sinks);
758 play_bin->sinks = NULL;
760 /* FIXME: this is probably some refcounting problem */
761 if (play_bin->visualisation && GST_OBJECT_PARENT (play_bin->visualisation)) {
762 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
763 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (play_bin->visualisation)),
764 play_bin->visualisation);
767 if (play_bin->frame) {
768 gst_buffer_unref (play_bin->frame);
769 play_bin->frame = NULL;
772 play_bin->textoverlay_element = NULL;
775 /* loop over the streams and set up the pipeline to play this
776 * media file. First we count the number of audio and video streams.
777 * If there is no video stream but there exists an audio stream,
778 * we install a visualisation pipeline.
780 * Also make sure to only connect the first audio and video pad. FIXME
781 * this should eventually be handled with a tuner interface so that
782 * one can switch the streams.
785 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad)
788 GstPadLinkReturn linkres;
790 GstStateChangeReturn stateret;
792 /* this is only for debugging */
793 parent = gst_pad_get_parent_element (srcpad);
795 GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
796 GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
797 gst_object_unref (parent);
800 /* bring it to the PAUSED state so we can link to the peer without
801 * breaking the flow */
802 if ((stateret = gst_element_set_state (sink, GST_STATE_PAUSED)) ==
803 GST_STATE_CHANGE_FAILURE)
806 gst_bin_add (GST_BIN (play_bin), sink);
808 /* we found a sink for this stream, now try to install it */
809 sinkpad = gst_element_get_pad (sink, "sink");
810 linkres = gst_pad_link (srcpad, sinkpad);
811 gst_object_unref (sinkpad);
813 /* try to link the pad of the sink to the stream */
814 if (GST_PAD_LINK_FAILED (linkres))
817 /* we got the sink succesfully linked, now keep the sink
818 * in out internal list */
819 play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
826 GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
834 /* could not link this stream */
835 caps = gst_pad_get_caps (srcpad);
836 capsstr = gst_caps_to_string (caps);
837 g_warning ("could not link %s: %d", capsstr, linkres);
838 GST_DEBUG_OBJECT (play_bin,
839 "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
843 gst_element_set_state (sink, GST_STATE_NULL);
844 gst_bin_remove (GST_BIN (play_bin), sink);
850 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
852 GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
853 GList *streaminfo = NULL, *s;
854 gboolean need_vis = FALSE;
855 gboolean need_text = FALSE;
856 GstPad *textsrcpad = NULL, *textsinkpad = NULL, *pad;
860 /* get rid of existing sinks */
861 if (play_bin->sinks) {
862 remove_sinks (play_bin);
864 GST_DEBUG ("setupsinks");
866 /* find out what to do */
867 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
868 group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
870 } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
871 group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
872 play_bin->visualisation != NULL) {
876 /* now actually connect everything */
877 g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
878 for (s = streaminfo; s; s = g_list_next (s)) {
879 GObject *obj = G_OBJECT (s->data);
883 g_object_get (obj, "type", &type, NULL);
884 g_object_get (obj, "object", &object, NULL);
888 if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
890 sink = gen_vis_element (play_bin);
892 sink = gen_audio_element (play_bin);
894 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
896 res = add_sink (play_bin, sink, pad);
897 gst_object_unref (pad);
901 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
903 sink = gen_text_element (play_bin);
905 textsinkpad = gst_element_get_pad (sink, "text_sink");
907 gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
909 gst_pad_link (textsrcpad, textsinkpad);
910 gst_object_unref (textsinkpad);
911 gst_object_unref (textsrcpad);
913 sink = gen_video_element (play_bin);
915 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
917 res = add_sink (play_bin, sink, pad);
918 gst_object_unref (pad);
921 /* remove the sinks now, pipeline get_state will now wait for the
922 * sinks to preroll */
923 if (play_bin->fakesink) {
924 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
925 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
926 play_bin->fakesink = NULL;
932 /* Send an event to our sinks until one of them works; don't then send to the
933 * remaining sinks (unlike GstBin)
936 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
938 GList *sinks = play_bin->sinks;
942 GstElement *sink = GST_ELEMENT_CAST (sinks->data);
944 gst_event_ref (event);
945 if ((res = gst_element_send_event (sink, event)))
948 sinks = g_list_next (sinks);
951 gst_event_unref (event);
957 do_playbin_seek (GstElement * element, GstEvent * event)
962 gboolean was_playing = FALSE;
965 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
967 flush = flags & GST_SEEK_FLAG_FLUSH;
972 /* need to call _get_state() since a bin state is only updated
974 gst_element_get_state (element, &state, NULL, 0);
975 was_playing = state == GST_STATE_PLAYING;
978 gst_element_set_state (element, GST_STATE_PAUSED);
982 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
985 /* need to reset the stream time to 0 after a flushing seek */
986 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
988 /* and continue playing */
989 gst_element_set_state (element, GST_STATE_PLAYING);
994 /* We only want to send the event to a single sink (overriding GstBin's
995 * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
996 * events appropriately. So, this is a messy duplication of code. */
998 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1000 gboolean res = FALSE;
1001 GstEventType event_type = GST_EVENT_TYPE (event);
1004 switch (event_type) {
1005 case GST_EVENT_SEEK:
1006 res = do_playbin_seek (element, event);
1009 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1016 static GstStateChangeReturn
1017 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1019 GstStateChangeReturn ret;
1020 GstPlayBin *play_bin;
1022 play_bin = GST_PLAY_BIN (element);
1025 switch (transition) {
1026 case GST_STATE_CHANGE_READY_TO_PAUSED:
1027 /* this really is the easiest way to make the state change return
1028 * ASYNC until we added the sinks */
1029 if (!play_bin->fakesink) {
1030 play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1031 gst_bin_add (GST_BIN (play_bin), play_bin->fakesink);
1038 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1039 if (ret == GST_STATE_CHANGE_FAILURE)
1042 switch (transition) {
1043 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1044 /* Set audio sink state to NULL to release the sound device,
1045 * but only if we own it (else we might be in chain-transition). */
1046 //if (play_bin->audio_sink != NULL &&
1047 // GST_STATE (play_bin->audio_sink) == GST_STATE_PAUSED) {
1048 // gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
1051 case GST_STATE_CHANGE_PAUSED_TO_READY:
1052 /* Check for NULL because the state transition may be done by
1053 * gst_bin_dispose which is called by gst_play_bin_dispose, and in that
1054 * case, we don't want to run remove_sinks.
1055 * FIXME: should the NULL test be done in remove_sinks? Should we just
1056 * set the state to NULL in gst_play_bin_dispose?
1058 if (play_bin->cache != NULL) {
1059 remove_sinks (play_bin);
1061 if (play_bin->fakesink) {
1062 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1063 gst_bin_remove (GST_BIN (play_bin), play_bin->fakesink);
1064 play_bin->fakesink = NULL;
1075 plugin_init (GstPlugin * plugin)
1077 GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1079 return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1083 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1086 "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,