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.
21 * SECTION:element-playbin
25 * Playbin provides a stand-alone everything-in-one abstraction for an
26 * audio and/or video player.
29 * It can handle both audio and video files and features
32 * automatic file type recognition and based on that automatic
33 * selection and usage of the right audio/video/subtitle demuxers/decoders
36 * visualisations for audio files
39 * subtitle support for video files
42 * stream selection between different audio/subtitles streams
45 * meta info (tag) extraction
48 * easy access to the last video frame
51 * buffering when playing streams over a network
58 * <title>Usage</title>
60 * A playbin element can be created just like any other element using
61 * gst_element_factory_make(). The file/URI to play should be set via the "uri"
62 * property. This must be an absolute URI, relative file paths are not allowed.
63 * Example URIs are file:///home/joe/movie.avi or http://www.joedoe.com/foo.ogg
66 * Playbin is a #GstPipeline. It will notify the application of everything
67 * that's happening (errors, end of stream, tags found, state changes, etc.)
68 * by posting messages on its #GstBus. The application needs to watch the
72 * Playback can be initiated by setting the element to PLAYING state using
73 * gst_element_set_state(). Note that the state change will take place in
74 * the background in a separate thread, when the function returns playback
75 * is probably not happening yet and any errors might not have occured yet.
76 * Applications using playbin should ideally be written to deal with things
77 * completely asynchroneous.
80 * When playback has finished (an EOS message has been received on the bus)
81 * or an error has occured (an ERROR message has been received on the bus) or
82 * the user wants to play a different track, playbin should be set back to
83 * READY or NULL state, then the "uri" property should be set to the new
84 * location and then playbin be set to PLAYING state again.
87 * Seeking can be done using gst_element_seek_simple() or gst_element_seek()
88 * on the playbin element. Again, the seek will not be executed
89 * instantaneously, but will be done in a background thread. When the seek
90 * call returns the seek will most likely still be in process. An application
91 * may wait for the seek to finish (or fail) using gst_element_get_state() with
92 * -1 as the timeout, but this will block the user interface and is not
96 * Applications may query the current position and duration of the stream
97 * via gst_element_query_position() and gst_element_query_duration() and
98 * setting the format passed to GST_FORMAT_TIME. If the query was successful,
99 * the duration or position will have been returned in units of nanoseconds.
101 * <title>Advanced Usage: specifying the audio and video sink</title>
103 * By default, if no audio sink or video sink has been specified via the
104 * "audio-sink" or "video-sink" property, playbin will use the autoaudiosink
105 * and autovideosink elements to find the first-best available output method.
106 * This should work in most cases, but is not always desirable. Often either
107 * the user or application might want to specify more explicitly what to use
108 * for audio and video output.
111 * If the application wants more control over how audio or video should be
112 * output, it may create the audio/video sink elements itself (for example
113 * using gst_element_factory_make()) and provide them to playbin using the
114 * "audio-sink" or "video-sink" property.
117 * GNOME-based applications, for example, will usually want to create
118 * gconfaudiosink and gconfvideosink elements and make playbin use those,
119 * so that output happens to whatever the user has configured in the GNOME
120 * Multimedia System Selector confinguration dialog.
123 * The sink elements do not necessarily need to be ready-made sinks. It is
124 * possible to create container elements that look like a sink to playbin,
125 * but in reality contain a number of custom elements linked together. This
126 * can be achieved by creating a #GstBin and putting elements in there and
127 * linking them, and then creating a sink #GstGhostPad for the bin and pointing
128 * it to the sink pad of the first element within the bin. This can be used
129 * for a number of purposes, for example to force output to a particular
130 * format or to modify or observe the data before it is output.
133 * It is also possible to 'suppress' audio and/or video output by using
134 * 'fakesink' elements (or capture it from there using the fakesink element's
135 * "handoff" signal, which, nota bene, is fired from the streaming thread!).
137 * <title>Retrieving Tags and Other Meta Data</title>
139 * Most of the common meta data (artist, title, etc.) can be retrieved by
140 * watching for TAG messages on the pipeline's bus (see above).
143 * Other more specific meta information like width/height/framerate of video
144 * streams or samplerate/number of channels of audio streams can be obtained
145 * using the "stream-info" property, which will return a GList of stream info
146 * objects, one for each stream. These are opaque objects that can only be
147 * accessed via the standard GObject property interface, ie. g_object_get().
148 * Each stream info object has the following properties:
150 * <listitem>"object" (GstObject) (the decoder source pad usually)</listitem>
151 * <listitem>"type" (enum) (if this is an audio/video/subtitle stream)</listitem>
152 * <listitem>"decoder" (string) (name of decoder used to decode this stream)</listitem>
153 * <listitem>"mute" (boolean) (to mute or unmute this stream)</listitem>
154 * <listitem>"caps" (GstCaps) (caps of the decoded stream)</listitem>
155 * <listitem>"language-code" (string) (ISO-639 language code for this stream, mostly used for audio/subtitle streams)</listitem>
156 * <listitem>"codec" (string) (format this stream was encoded in)</listitem>
158 * Stream information from the stream-info properties is best queried once
159 * playbin has changed into PAUSED or PLAYING state (which can be detected
160 * via a state-changed message on the bus where old_state=READY and
161 * new_state=PAUSED), since before that the list might not be complete yet or
162 * not contain all available information (like language-codes).
164 * <title>Buffering status</title>
166 * Playbin handles buffering automatically. When playbin is buffering, it
167 * will post BUFFERING messages on the bus with a percentage value that
168 * shows the progress of the buffering. Applications may want to show this
169 * information to the user in some form or another. Here is how to extract
170 * the percentage information from the message:
174 * switch (GST_MESSAGE_TYPE (msg)) {
175 * case GST_MESSAGE_BUFFERING: {
177 * gst_structure_get_int (msg->structure, "buffer-percent", &percent);
178 * g_print ("Buffering (%%u percent done)", percent);
185 * <title>Embedding the video window in your application</title>
187 * By default, playbin (or rather the video sinks used) will create their own
188 * window. Applications will usually want to force output to a window of their
189 * own, however. This can be done using the GstXOverlay interface, which most
190 * video sinks implement. See the documentation there for more details.
192 * <title>Specifying which CD/DVD device to use</title>
194 * The device to use for CDs/DVDs needs to be set on the source element
195 * playbin creates before it is opened. The only way to do this at the moment
196 * is to connect to playbin's "notify::source" signal, which will be emitted
197 * by playbin when it has created the source element for a particular URI.
198 * In the signal callback you can check if the source element has a "device"
199 * property and set it appropriately. In future ways might be added to specify
200 * the device as part of the URI, but at the time of writing this is not
203 * <title>Examples</title>
205 * Here is a simple pipeline to play back a video or audio file:
207 * gst-launch -v playbin uri=file:///path/to/somefile.avi
209 * This will play back the given AVI video file, given that the video and
210 * audio decoders required to decode the content are installed. Since no
211 * special audio sink or video sink is supplied (not possible via gst-launch),
212 * playbin will try to find a suitable audio and video sink automatically
213 * using the autoaudiosink and autovideosink elements.
216 * Here is a another pipeline to play track 4 of an audio CD:
218 * gst-launch -v playbin uri=cdda://4
220 * This will play back track 4 on an audio CD in your disc drive (assuming
221 * the drive is detected automatically by the plugin).
224 * Here is a another pipeline to play title 1 of a DVD:
226 * gst-launch -v playbin uri=dvd://1
228 * This will play back title 1 of a DVD in your disc drive (assuming
229 * the drive is detected automatically by the plugin).
241 #include <gst/gst-i18n-plugin.h>
243 #include "gstplaybasebin.h"
245 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_debug);
246 #define GST_CAT_DEFAULT gst_play_bin_debug
248 #define GST_TYPE_PLAY_BIN (gst_play_bin_get_type())
249 #define GST_PLAY_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PLAY_BIN,GstPlayBin))
250 #define GST_PLAY_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PLAY_BIN,GstPlayBinClass))
251 #define GST_IS_PLAY_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PLAY_BIN))
252 #define GST_IS_PLAY_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PLAY_BIN))
254 #define VOLUME_MAX_DOUBLE 10.0
255 #define CONNECTION_SPEED_DEFAULT 0
257 typedef struct _GstPlayBin GstPlayBin;
258 typedef struct _GstPlayBinClass GstPlayBinClass;
262 GstPlayBaseBin parent;
264 /* the configurable elements */
265 GstElement *fakesink;
266 GstElement *audio_sink;
267 GstElement *video_sink;
268 GstElement *visualisation;
269 GstElement *pending_visualisation;
270 GstElement *volume_element;
271 GstElement *textoverlay_element;
274 /* these are the currently active sinks */
277 /* the last captured frame for snapshots */
280 /* our cache for the sinks */
283 /* font description */
286 /* connection speed in bits/sec (0 = unknown) */
287 guint connection_speed;
289 /* indication if the pipeline is live */
293 struct _GstPlayBinClass
295 GstPlayBaseBinClass parent_class;
317 static void gst_play_bin_class_init (GstPlayBinClass * klass);
318 static void gst_play_bin_init (GstPlayBin * play_bin);
319 static void gst_play_bin_dispose (GObject * object);
321 static gboolean setup_sinks (GstPlayBaseBin * play_base_bin,
322 GstPlayBaseGroup * group);
323 static void remove_sinks (GstPlayBin * play_bin);
325 static void gst_play_bin_set_property (GObject * object, guint prop_id,
326 const GValue * value, GParamSpec * spec);
327 static void gst_play_bin_get_property (GObject * object, guint prop_id,
328 GValue * value, GParamSpec * spec);
330 static gboolean gst_play_bin_send_event (GstElement * element,
332 static gboolean gst_play_bin_set_clock_func (GstElement * element,
334 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
335 GstStateChange transition);
337 static void gst_play_bin_handle_message (GstBin * bin, GstMessage * message);
339 static GstElementClass *parent_class;
341 //static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
343 static const GstElementDetails gst_play_bin_details =
344 GST_ELEMENT_DETAILS ("Player Bin",
345 "Generic/Bin/Player",
346 "Autoplug and play media from an uri",
347 "Wim Taymans <wim@fluendo.com>");
350 gst_play_bin_get_type (void)
352 static GType gst_play_bin_type = 0;
354 if (!gst_play_bin_type) {
355 static const GTypeInfo gst_play_bin_info = {
356 sizeof (GstPlayBinClass),
359 (GClassInitFunc) gst_play_bin_class_init,
364 (GInstanceInitFunc) gst_play_bin_init,
368 gst_play_bin_type = g_type_register_static (GST_TYPE_PLAY_BASE_BIN,
369 "GstPlayBin", &gst_play_bin_info, 0);
372 return gst_play_bin_type;
376 gst_play_bin_class_init (GstPlayBinClass * klass)
378 GObjectClass *gobject_klass;
379 GstElementClass *gstelement_klass;
380 GstBinClass *gstbin_klass;
381 GstPlayBaseBinClass *playbasebin_klass;
383 gobject_klass = (GObjectClass *) klass;
384 gstelement_klass = (GstElementClass *) klass;
385 gstbin_klass = (GstBinClass *) klass;
386 playbasebin_klass = (GstPlayBaseBinClass *) klass;
388 parent_class = g_type_class_peek_parent (klass);
390 gobject_klass->set_property = gst_play_bin_set_property;
391 gobject_klass->get_property = gst_play_bin_get_property;
393 g_object_class_install_property (gobject_klass, ARG_VIDEO_SINK,
394 g_param_spec_object ("video-sink", "Video Sink",
395 "the video output element to use (NULL = default sink)",
396 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
397 g_object_class_install_property (gobject_klass, ARG_AUDIO_SINK,
398 g_param_spec_object ("audio-sink", "Audio Sink",
399 "the audio output element to use (NULL = default sink)",
400 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
401 g_object_class_install_property (gobject_klass, ARG_VIS_PLUGIN,
402 g_param_spec_object ("vis-plugin", "Vis plugin",
403 "the visualization element to use (NULL = none)",
404 GST_TYPE_ELEMENT, G_PARAM_READWRITE));
405 g_object_class_install_property (gobject_klass, ARG_VOLUME,
406 g_param_spec_double ("volume", "volume", "volume",
407 0.0, VOLUME_MAX_DOUBLE, 1.0, G_PARAM_READWRITE));
408 g_object_class_install_property (gobject_klass, ARG_FRAME,
409 gst_param_spec_mini_object ("frame", "Frame",
410 "The last frame (NULL = no video available)",
411 GST_TYPE_BUFFER, G_PARAM_READABLE));
412 g_object_class_install_property (gobject_klass, ARG_FONT_DESC,
413 g_param_spec_string ("subtitle-font-desc",
414 "Subtitle font description",
415 "Pango font description of font "
416 "to be used for subtitle rendering", NULL, G_PARAM_WRITABLE));
418 * GstPlayBin::connection-speed
420 * Network connection speed in kbps (0 = unknown)
424 g_object_class_install_property (gobject_klass, ARG_CONNECTION_SPEED,
425 g_param_spec_uint ("connection-speed", "Connection Speed",
426 "Network connection speed in kbps (0 = unknown)",
427 0, G_MAXUINT, CONNECTION_SPEED_DEFAULT, G_PARAM_READWRITE));
429 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_play_bin_dispose);
431 gst_element_class_set_details (gstelement_klass, &gst_play_bin_details);
433 gstelement_klass->change_state =
434 GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
435 gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
436 gstelement_klass->set_clock = GST_DEBUG_FUNCPTR (gst_play_bin_set_clock_func);
438 gstbin_klass->handle_message =
439 GST_DEBUG_FUNCPTR (gst_play_bin_handle_message);
441 playbasebin_klass->setup_output_pads = setup_sinks;
445 gst_play_bin_init (GstPlayBin * play_bin)
447 play_bin->video_sink = NULL;
448 play_bin->audio_sink = NULL;
449 play_bin->visualisation = NULL;
450 play_bin->pending_visualisation = NULL;
451 play_bin->volume_element = NULL;
452 play_bin->textoverlay_element = NULL;
453 play_bin->volume = 1.0;
454 play_bin->sinks = NULL;
455 play_bin->frame = NULL;
456 play_bin->font_desc = NULL;
457 play_bin->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
458 NULL, (GDestroyNotify) gst_object_unref);
462 gst_play_bin_dispose (GObject * object)
464 GstPlayBin *play_bin;
466 play_bin = GST_PLAY_BIN (object);
468 if (play_bin->cache != NULL) {
469 remove_sinks (play_bin);
470 g_hash_table_destroy (play_bin->cache);
471 play_bin->cache = NULL;
474 if (play_bin->audio_sink != NULL) {
475 gst_element_set_state (play_bin->audio_sink, GST_STATE_NULL);
476 gst_object_unref (play_bin->audio_sink);
477 play_bin->audio_sink = NULL;
479 if (play_bin->video_sink != NULL) {
480 gst_element_set_state (play_bin->video_sink, GST_STATE_NULL);
481 gst_object_unref (play_bin->video_sink);
482 play_bin->video_sink = NULL;
484 if (play_bin->visualisation != NULL) {
485 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
486 gst_object_unref (play_bin->visualisation);
487 play_bin->visualisation = NULL;
489 if (play_bin->pending_visualisation != NULL) {
490 gst_element_set_state (play_bin->pending_visualisation, GST_STATE_NULL);
491 gst_object_unref (play_bin->pending_visualisation);
492 play_bin->pending_visualisation = NULL;
494 if (play_bin->textoverlay_element != NULL) {
495 gst_object_unref (play_bin->textoverlay_element);
496 play_bin->textoverlay_element = NULL;
498 g_free (play_bin->font_desc);
499 play_bin->font_desc = NULL;
501 G_OBJECT_CLASS (parent_class)->dispose (object);
505 gst_play_bin_vis_unblocked (GstPad * tee_pad, gboolean blocked,
512 gst_play_bin_vis_blocked (GstPad * tee_pad, gboolean blocked,
515 GstPlayBin *play_bin = GST_PLAY_BIN (user_data);
516 GstBin *vis_bin = NULL;
517 GstPad *vis_sink_pad = NULL, *vis_src_pad = NULL, *vqueue_pad = NULL;
520 /* We want to disable visualisation */
521 if (!GST_IS_ELEMENT (play_bin->pending_visualisation)) {
522 /* Set visualisation element to READY */
523 gst_element_set_state (play_bin->visualisation, GST_STATE_READY);
528 GST_BIN_CAST (gst_object_get_parent (GST_OBJECT_CAST (play_bin->
531 if (!GST_IS_BIN (vis_bin) || !GST_IS_PAD (tee_pad)) {
535 vis_src_pad = gst_element_get_pad (play_bin->visualisation, "src");
536 vis_sink_pad = gst_pad_get_peer (tee_pad);
538 /* Can be fakesink */
539 if (GST_IS_PAD (vis_src_pad)) {
540 vqueue_pad = gst_pad_get_peer (vis_src_pad);
543 if (!GST_IS_PAD (vis_sink_pad)) {
547 /* Check the bin's state */
548 GST_OBJECT_LOCK (vis_bin);
549 bin_state = GST_STATE (vis_bin);
550 GST_OBJECT_UNLOCK (vis_bin);
553 gst_pad_unlink (tee_pad, vis_sink_pad);
554 gst_object_unref (vis_sink_pad);
557 if (GST_IS_PAD (vqueue_pad)) {
558 gst_pad_unlink (vis_src_pad, vqueue_pad);
559 gst_object_unref (vis_src_pad);
563 /* Remove from vis_bin */
564 gst_bin_remove (vis_bin, play_bin->visualisation);
565 /* Set state to NULL */
566 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
567 /* And loose our ref */
568 gst_object_unref (play_bin->visualisation);
570 if (play_bin->pending_visualisation) {
571 /* Ref this new visualisation element before adding to the bin */
572 gst_object_ref (play_bin->pending_visualisation);
573 /* Add the new one */
574 gst_bin_add (vis_bin, play_bin->pending_visualisation);
575 /* Synchronizing state */
576 gst_element_set_state (play_bin->pending_visualisation, bin_state);
578 vis_sink_pad = gst_element_get_pad (play_bin->pending_visualisation,
580 vis_src_pad = gst_element_get_pad (play_bin->pending_visualisation, "src");
582 if (!GST_IS_PAD (vis_sink_pad) || !GST_IS_PAD (vis_src_pad)) {
587 gst_pad_link (tee_pad, vis_sink_pad);
588 gst_pad_link (vis_src_pad, vqueue_pad);
592 gst_object_unref (play_bin->visualisation);
593 play_bin->visualisation = play_bin->pending_visualisation;
594 play_bin->pending_visualisation = NULL;
598 gst_object_unref (vis_sink_pad);
601 gst_object_unref (vis_src_pad);
604 gst_object_unref (vqueue_pad);
607 gst_object_unref (vis_bin);
610 /* Unblock the pad */
611 gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_bin_vis_unblocked,
616 gst_play_bin_set_property (GObject * object, guint prop_id,
617 const GValue * value, GParamSpec * pspec)
619 GstPlayBin *play_bin;
621 play_bin = GST_PLAY_BIN (object);
625 if (play_bin->video_sink != NULL) {
626 gst_object_unref (play_bin->video_sink);
628 play_bin->video_sink = g_value_get_object (value);
629 if (play_bin->video_sink != NULL) {
630 gst_object_ref (play_bin->video_sink);
631 gst_object_sink (GST_OBJECT_CAST (play_bin->video_sink));
633 /* when changing the videosink, we just remove the
634 * video pipeline from the cache so that it will be
635 * regenerated with the new sink element */
636 g_hash_table_remove (play_bin->cache, "vbin");
639 if (play_bin->audio_sink != NULL) {
640 gst_object_unref (play_bin->audio_sink);
642 play_bin->audio_sink = g_value_get_object (value);
643 if (play_bin->audio_sink != NULL) {
644 gst_object_ref (play_bin->audio_sink);
645 gst_object_sink (GST_OBJECT_CAST (play_bin->audio_sink));
647 g_hash_table_remove (play_bin->cache, "abin");
651 /* Do we already have a visualisation change pending ? */
652 if (play_bin->pending_visualisation) {
653 gst_object_unref (play_bin->pending_visualisation);
654 play_bin->pending_visualisation = g_value_get_object (value);
656 if (play_bin->pending_visualisation) {
657 gst_object_ref (play_bin->pending_visualisation);
658 gst_object_sink (GST_OBJECT_CAST (play_bin->pending_visualisation));
661 play_bin->pending_visualisation = g_value_get_object (value);
664 if (play_bin->pending_visualisation) {
665 gst_object_ref (play_bin->pending_visualisation);
666 gst_object_sink (GST_OBJECT_CAST (play_bin->pending_visualisation));
669 /* Was there a visualisation already set ? */
670 if (play_bin->visualisation != NULL) {
671 GstBin *vis_bin = NULL;
674 GST_BIN_CAST (gst_object_get_parent (GST_OBJECT_CAST (play_bin->
677 /* Check if the visualisation is already in a bin */
678 if (GST_IS_BIN (vis_bin)) {
679 GstPad *vis_sink_pad = NULL, *tee_pad = NULL;
681 /* Now get tee pad and block it async */
682 vis_sink_pad = gst_element_get_pad (play_bin->visualisation,
684 if (!GST_IS_PAD (vis_sink_pad)) {
687 tee_pad = gst_pad_get_peer (vis_sink_pad);
688 if (!GST_IS_PAD (tee_pad)) {
692 /* Block with callback */
693 gst_pad_set_blocked_async (tee_pad, TRUE, gst_play_bin_vis_blocked,
697 gst_object_unref (vis_sink_pad);
700 gst_object_unref (tee_pad);
702 gst_object_unref (vis_bin);
704 play_bin->visualisation = play_bin->pending_visualisation;
705 play_bin->pending_visualisation = NULL;
708 play_bin->visualisation = play_bin->pending_visualisation;
709 play_bin->pending_visualisation = NULL;
715 play_bin->volume = g_value_get_double (value);
716 if (play_bin->volume_element) {
717 g_object_set (G_OBJECT (play_bin->volume_element), "volume",
718 play_bin->volume, NULL);
722 g_free (play_bin->font_desc);
723 play_bin->font_desc = g_strdup (g_value_get_string (value));
724 if (play_bin->textoverlay_element) {
725 g_object_set (G_OBJECT (play_bin->textoverlay_element),
726 "font-desc", g_value_get_string (value), NULL);
729 case ARG_CONNECTION_SPEED:
730 play_bin->connection_speed = g_value_get_uint (value) * 1000;
733 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
739 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
742 GstPlayBin *play_bin;
744 play_bin = GST_PLAY_BIN (object);
748 g_value_set_object (value, play_bin->video_sink);
751 g_value_set_object (value, play_bin->audio_sink);
754 g_value_set_object (value, play_bin->visualisation);
757 g_value_set_double (value, play_bin->volume);
760 gst_value_set_mini_object (value, GST_MINI_OBJECT (play_bin->frame));
762 case ARG_CONNECTION_SPEED:
763 g_value_set_uint (value, play_bin->connection_speed / 1000);
766 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771 /* signal fired when the identity has received a new buffer. This is used for
772 * making screenshots.
775 handoff (GstElement * identity, GstBuffer * frame, gpointer data)
777 GstPlayBin *play_bin = GST_PLAY_BIN (data);
778 GstBuffer **frame_p = &play_bin->frame;
780 gst_mini_object_replace ((GstMiniObject **) frame_p,
781 GST_MINI_OBJECT_CAST (frame));
783 /* applications need to know the buffer caps,
784 * make sure they are always set on the frame */
785 if (GST_BUFFER_CAPS (play_bin->frame) == NULL) {
788 if ((pad = gst_element_get_pad (identity, "sink"))) {
789 gst_buffer_set_caps (play_bin->frame, GST_PAD_CAPS (pad));
790 gst_object_unref (pad);
795 /* make the element (bin) that contains the elements needed to perform
796 * video display. We connect a handoff signal to identity so that we
797 * can grab snapshots. Identity's sinkpad is ghosted to vbin.
799 * +-------------------------------------------------------------+
801 * | +--------+ +----------+ +----------+ +---------+ |
802 * | |identity| |colorspace| |videoscale| |videosink| |
803 * | +-sink src-sink src-sink src-sink | |
804 * | | +---+----+ +----------+ +----------+ +---------+ |
806 * +----------|--------------------------------------------------+
810 gen_video_element (GstPlayBin * play_bin)
817 GstElement *identity;
820 /* first see if we have it in the cache */
821 element = g_hash_table_lookup (play_bin->cache, "vbin");
822 if (element != NULL) {
826 if (play_bin->video_sink) {
827 sink = play_bin->video_sink;
829 sink = gst_element_factory_make ("autovideosink", "videosink");
831 sink = gst_element_factory_make ("xvimagesink", "videosink");
836 gst_object_ref (sink);
837 g_hash_table_insert (play_bin->cache, "video_sink", sink);
839 /* create a bin to hold objects, as we create them we add them to this bin so
840 * that when something goes wrong we only need to unref the bin */
841 element = gst_bin_new ("vbin");
842 gst_bin_add (GST_BIN_CAST (element), sink);
844 conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
847 gst_bin_add (GST_BIN_CAST (element), conv);
849 scale = gst_element_factory_make ("videoscale", "vscale");
852 gst_bin_add (GST_BIN_CAST (element), scale);
854 identity = gst_element_factory_make ("identity", "id");
855 g_object_set (identity, "silent", TRUE, NULL);
856 g_signal_connect (identity, "handoff", G_CALLBACK (handoff), play_bin);
857 gst_bin_add (GST_BIN_CAST (element), identity);
859 gst_element_link_pads (identity, "src", conv, "sink");
860 gst_element_link_pads (conv, "src", scale, "sink");
861 /* be more careful with the pad from the custom sink element, it might not
863 if (!gst_element_link_pads (scale, "src", sink, NULL))
866 pad = gst_element_get_pad (identity, "sink");
867 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
868 gst_object_unref (pad);
870 gst_element_set_state (element, GST_STATE_READY);
872 /* since we're gonna add it to a bin but don't want to lose it,
873 * we keep a reference. */
874 gst_object_ref (element);
875 g_hash_table_insert (play_bin->cache, "vbin", element);
882 /* FIXME: this warrants adding a CORE error category for missing
883 * elements/plugins */
884 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
885 (_("Both autovideosink and xvimagesink elements are missing.")),
891 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
892 (_("Missing element '%s' - check your GStreamer installation."),
893 "ffmpegcolorspace"), (NULL));
894 gst_object_unref (element);
900 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
901 (_("Missing element '%s' - check your GStreamer installation."),
902 "videoscale"), ("possibly a liboil version mismatch?"));
903 gst_object_unref (element);
908 GST_ELEMENT_ERROR (play_bin, CORE, PAD,
909 (NULL), ("Failed to configure the video sink."));
910 gst_object_unref (element);
915 /* make an element for playback of video with subtitles embedded.
917 * +--------------------------------------------------+
918 * | tbin +-------------+ |
919 * | +-----+ | textoverlay | +------+ |
920 * | | csp | +--video_sink | | vbin | |
921 * video_sink-sink src+ +-text_sink src-sink | |
922 * | +-----+ | +-------------+ +------+ |
923 * text_sink-------------+ |
924 * +--------------------------------------------------+
926 * If there is no subtitle renderer this function will simply return the
927 * videosink without the text_sink pad.
930 gen_text_element (GstPlayBin * play_bin)
932 GstElement *element, *csp, *overlay, *vbin;
935 /* Create the video rendering bin, error is posted when this fails. */
936 vbin = gen_video_element (play_bin);
941 overlay = gst_element_factory_make ("textoverlay", "overlay");
943 /* If no overlay return the video bin without subtitle support. */
948 element = gst_bin_new ("textbin");
950 /* Set some parameters */
951 g_object_set (G_OBJECT (overlay),
952 "halign", "center", "valign", "bottom", NULL);
953 if (play_bin->font_desc) {
954 g_object_set (G_OBJECT (overlay), "font-desc", play_bin->font_desc, NULL);
958 play_bin->textoverlay_element = GST_ELEMENT_CAST (gst_object_ref (overlay));
960 /* we know this will succeed, as the video bin already created one before */
961 csp = gst_element_factory_make ("ffmpegcolorspace", "subtitlecsp");
963 /* Add our elements */
964 gst_bin_add_many (GST_BIN_CAST (element), csp, overlay, vbin, NULL);
967 gst_element_link_pads (csp, "src", overlay, "video_sink");
968 gst_element_link_pads (overlay, "src", vbin, "sink");
970 /* Add ghost pads on the subtitle bin */
971 pad = gst_element_get_pad (overlay, "text_sink");
972 gst_element_add_pad (element, gst_ghost_pad_new ("text_sink", pad));
973 gst_object_unref (pad);
975 pad = gst_element_get_pad (csp, "sink");
976 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
977 gst_object_unref (pad);
979 /* Set state to READY */
980 gst_element_set_state (element, GST_STATE_READY);
987 GST_WARNING_OBJECT (play_bin,
988 "No overlay (pango) element, subtitles disabled");
993 /* make the element (bin) that contains the elements needed to perform
996 * +-------------------------------------------------------------+
998 * | +---------+ +----------+ +---------+ +---------+ |
999 * | |audioconv| |audioscale| | volume | |audiosink| |
1000 * | +-sink src-sink src-sink src-sink | |
1001 * | | +---------+ +----------+ +---------+ +---------+ |
1003 * +-------------------------------------------------------------+
1006 gen_audio_element (GstPlayBin * play_bin)
1009 GstElement *element;
1016 element = g_hash_table_lookup (play_bin->cache, "abin");
1017 if (element != NULL)
1020 if (play_bin->audio_sink) {
1021 sink = play_bin->audio_sink;
1023 sink = gst_element_factory_make ("autoaudiosink", "audiosink");
1025 sink = gst_element_factory_make ("alsasink", "audiosink");
1030 play_bin->audio_sink = GST_ELEMENT_CAST (gst_object_ref (sink));
1033 gst_object_ref (sink);
1034 g_hash_table_insert (play_bin->cache, "audio_sink", sink);
1036 element = gst_bin_new ("abin");
1037 gst_bin_add (GST_BIN_CAST (element), sink);
1039 conv = gst_element_factory_make ("audioconvert", "aconv");
1041 goto no_audioconvert;
1042 gst_bin_add (GST_BIN_CAST (element), conv);
1044 scale = gst_element_factory_make ("audioresample", "aresample");
1046 goto no_audioresample;
1047 gst_bin_add (GST_BIN_CAST (element), scale);
1049 volume = gst_element_factory_make ("volume", "volume");
1050 g_object_set (G_OBJECT (volume), "volume", play_bin->volume, NULL);
1051 play_bin->volume_element = volume;
1052 gst_bin_add (GST_BIN_CAST (element), volume);
1054 res = gst_element_link_pads (conv, "src", scale, "sink");
1055 res &= gst_element_link_pads (scale, "src", volume, "sink");
1056 res &= gst_element_link_pads (volume, "src", sink, NULL);
1060 pad = gst_element_get_pad (conv, "sink");
1061 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
1062 gst_object_unref (pad);
1064 gst_element_set_state (element, GST_STATE_READY);
1066 /* since we're gonna add it to a bin but don't want to lose it,
1067 * we keep a reference. */
1068 gst_object_ref (element);
1069 g_hash_table_insert (play_bin->cache, "abin", element);
1076 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1077 (_("Both autoaudiosink and alsasink elements are missing.")), (NULL));
1082 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1083 (_("Missing element '%s' - check your GStreamer installation."),
1084 "audioconvert"), ("possibly a liboil version mismatch?"));
1085 gst_object_unref (element);
1091 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1092 (_("Missing element '%s' - check your GStreamer installation."),
1093 "audioresample"), ("possibly a liboil version mismatch?"));
1094 gst_object_unref (element);
1099 GST_ELEMENT_ERROR (play_bin, CORE, PAD,
1100 (NULL), ("Failed to configure the audio sink."));
1101 gst_object_unref (element);
1106 /* make the element (bin) that contains the elements needed to perform
1107 * visualisation ouput. The idea is to split the audio using tee, then
1108 * sending the output to the regular audio bin and the other output to
1109 * the vis plugin that transforms it into a video that is rendered with the
1110 * normal video bin. The video bin is run in a thread to make sure it does
1111 * not block the audio playback pipeline.
1113 * +--------------------------------------------------------------------+
1115 * | +------+ +--------+ +----------------+ |
1116 * | | tee | | aqueue | | abin ... | |
1117 * | +-sink src-sink src-sink | |
1118 * | | | | +--------+ +----------------+ |
1120 * | | | | +------+ +---------+ +------+ +-----------+ |
1121 * | | | | |vqueue| |audioconv| | vis | | vbin ... | |
1122 * | | | src-sink src-sink src-sink src-sink | |
1123 * | | | | +------+ +---------+ +------+ +-----------+ |
1127 +---------------------------------------------------------------------+
1130 gen_vis_element (GstPlayBin * play_bin)
1133 GstElement *element;
1139 GstElement *vqueue, *aqueue;
1142 /* errors are already posted when these fail. */
1143 asink = gen_audio_element (play_bin);
1146 vsink = gen_video_element (play_bin);
1148 gst_object_unref (asink);
1152 element = gst_bin_new ("visbin");
1153 tee = gst_element_factory_make ("tee", "tee");
1155 vqueue = gst_element_factory_make ("queue", "vqueue");
1156 aqueue = gst_element_factory_make ("queue", "aqueue");
1158 gst_bin_add (GST_BIN_CAST (element), asink);
1159 gst_bin_add (GST_BIN_CAST (element), vqueue);
1160 gst_bin_add (GST_BIN_CAST (element), aqueue);
1161 gst_bin_add (GST_BIN_CAST (element), vsink);
1162 gst_bin_add (GST_BIN_CAST (element), tee);
1164 conv = gst_element_factory_make ("audioconvert", "aconv");
1166 goto no_audioconvert;
1167 gst_bin_add (GST_BIN_CAST (element), conv);
1169 if (play_bin->visualisation) {
1170 gst_object_ref (play_bin->visualisation);
1171 vis = play_bin->visualisation;
1173 vis = gst_element_factory_make ("goom", "vis");
1177 gst_bin_add (GST_BIN_CAST (element), vis);
1179 res = gst_element_link_pads (vqueue, "src", conv, "sink");
1180 res &= gst_element_link_pads (conv, "src", vis, "sink");
1181 res &= gst_element_link_pads (vis, "src", vsink, "sink");
1185 pad = gst_element_get_pad (aqueue, "sink");
1186 rpad = gst_element_get_request_pad (tee, "src%d");
1187 gst_pad_link (rpad, pad);
1188 gst_object_unref (rpad);
1189 gst_object_unref (pad);
1190 gst_element_link_pads (aqueue, "src", asink, "sink");
1192 pad = gst_element_get_pad (vqueue, "sink");
1193 rpad = gst_element_get_request_pad (tee, "src%d");
1194 gst_pad_link (rpad, pad);
1195 gst_object_unref (rpad);
1196 gst_object_unref (pad);
1198 pad = gst_element_get_pad (tee, "sink");
1199 gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
1200 gst_object_unref (pad);
1207 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1208 (_("Missing element '%s' - check your GStreamer installation."),
1209 "audioconvert"), ("possibly a liboil version mismatch?"));
1210 gst_object_unref (element);
1215 GST_ELEMENT_ERROR (play_bin, CORE, MISSING_PLUGIN,
1216 (_("Missing element '%s' - check your GStreamer installation."),
1218 gst_object_unref (element);
1223 GST_ELEMENT_ERROR (play_bin, CORE, PAD,
1224 (NULL), ("Failed to configure the visualisation element."));
1225 gst_object_unref (element);
1230 /* get rid of all installed sinks */
1232 remove_sinks (GstPlayBin * play_bin)
1236 GstElement *element;
1239 if (play_bin->cache == NULL)
1242 GST_DEBUG ("removesinks");
1243 element = g_hash_table_lookup (play_bin->cache, "abin");
1244 if (element != NULL) {
1245 parent = gst_element_get_parent (element);
1246 if (parent != NULL) {
1247 /* we remove the element from the parent so that
1248 * there is no unwanted state change when the parent
1250 play_bin->sinks = g_list_remove (play_bin->sinks, element);
1251 gst_element_set_state (element, GST_STATE_NULL);
1252 gst_bin_remove (GST_BIN_CAST (parent), element);
1253 gst_object_unref (parent);
1255 pad = gst_element_get_pad (element, "sink");
1257 peer = gst_pad_get_peer (pad);
1259 gst_pad_unlink (peer, pad);
1260 gst_object_unref (peer);
1262 gst_object_unref (pad);
1265 element = g_hash_table_lookup (play_bin->cache, "vbin");
1266 if (element != NULL) {
1267 parent = gst_element_get_parent (element);
1268 if (parent != NULL) {
1269 play_bin->sinks = g_list_remove (play_bin->sinks, element);
1270 gst_element_set_state (element, GST_STATE_NULL);
1271 gst_bin_remove (GST_BIN_CAST (parent), element);
1272 gst_object_unref (parent);
1274 pad = gst_element_get_pad (element, "sink");
1276 peer = gst_pad_get_peer (pad);
1278 gst_pad_unlink (peer, pad);
1279 gst_object_unref (peer);
1281 gst_object_unref (pad);
1285 for (sinks = play_bin->sinks; sinks; sinks = g_list_next (sinks)) {
1286 GstElement *element = GST_ELEMENT_CAST (sinks->data);
1290 pad = gst_element_get_pad (element, "sink");
1292 GST_LOG ("removing sink %p", element);
1294 peer = gst_pad_get_peer (pad);
1296 gst_pad_unlink (peer, pad);
1297 gst_object_unref (peer);
1299 gst_object_unref (pad);
1301 gst_element_set_state (element, GST_STATE_NULL);
1302 gst_bin_remove (GST_BIN_CAST (play_bin), element);
1304 g_list_free (play_bin->sinks);
1305 play_bin->sinks = NULL;
1307 if (play_bin->visualisation) {
1308 GstElement *vis_bin;
1311 GST_ELEMENT_CAST (gst_element_get_parent (play_bin->visualisation));
1313 gst_element_set_state (play_bin->visualisation, GST_STATE_NULL);
1316 gst_bin_remove (GST_BIN_CAST (vis_bin), play_bin->visualisation);
1317 gst_object_unref (vis_bin);
1321 if (play_bin->frame) {
1322 gst_buffer_unref (play_bin->frame);
1323 play_bin->frame = NULL;
1326 if (play_bin->textoverlay_element) {
1327 gst_object_unref (play_bin->textoverlay_element);
1328 play_bin->textoverlay_element = NULL;
1332 /* loop over the streams and set up the pipeline to play this
1333 * media file. First we count the number of audio and video streams.
1334 * If there is no video stream but there exists an audio stream,
1335 * we install a visualisation pipeline.
1337 * Also make sure to only connect the first audio and video pad. FIXME
1338 * this should eventually be handled with a tuner interface so that
1339 * one can switch the streams.
1341 * This function takes ownership of @sink.*
1344 add_sink (GstPlayBin * play_bin, GstElement * sink, GstPad * srcpad,
1345 GstPad * subtitle_pad)
1348 GstPadLinkReturn linkres;
1350 GstStateChangeReturn stateret;
1353 g_return_val_if_fail (sink != NULL, FALSE);
1355 /* For live pipelines we need to add the bin in the same state as the
1356 * parent so that it starts as soon as it is prerolled. */
1357 if (play_bin->is_live)
1358 state = GST_STATE_PLAYING;
1360 state = GST_STATE_PAUSED;
1362 /* this is only for debugging */
1363 parent = gst_pad_get_parent_element (srcpad);
1365 GST_DEBUG ("Adding sink with state %d (parent: %d, peer: %d)",
1366 GST_STATE (sink), GST_STATE (play_bin), GST_STATE (parent));
1367 gst_object_unref (parent);
1369 gst_bin_add (GST_BIN_CAST (play_bin), sink);
1371 /* for live pipelines, disable the sync in the sinks until core handles this
1373 if (play_bin->is_live)
1374 gst_element_set_clock (sink, NULL);
1376 /* bring it to the PAUSED state so we can link to the peer without
1377 * breaking the flow */
1378 stateret = gst_element_set_state (sink, state);
1379 if (stateret == GST_STATE_CHANGE_FAILURE)
1382 /* we found a sink for this stream, now try to install it */
1383 sinkpad = gst_element_get_pad (sink, "sink");
1384 linkres = gst_pad_link (srcpad, sinkpad);
1385 gst_object_unref (sinkpad);
1387 /* try to link the pad of the sink to the stream */
1388 if (GST_PAD_LINK_FAILED (linkres))
1391 if (GST_IS_PAD (subtitle_pad)) {
1392 sinkpad = gst_element_get_pad (sink, "text_sink");
1393 linkres = gst_pad_link (subtitle_pad, sinkpad);
1394 gst_object_unref (sinkpad);
1397 /* try to link the subtitle pad of the sink to the stream, this is not
1399 if (GST_PAD_LINK_FAILED (linkres))
1400 goto subtitle_failed;
1403 /* we got the sink succesfully linked, now keep the sink
1404 * in our internal list */
1405 play_bin->sinks = g_list_prepend (play_bin->sinks, sink);
1412 gst_element_set_state (sink, GST_STATE_NULL);
1413 gst_bin_remove (GST_BIN_CAST (play_bin), sink);
1414 GST_DEBUG_OBJECT (play_bin, "state change failure when adding sink");
1422 /* could not link this stream */
1423 caps = gst_pad_get_caps (srcpad);
1424 capsstr = gst_caps_to_string (caps);
1425 g_warning ("could not link %s: %d", capsstr, linkres);
1426 GST_DEBUG_OBJECT (play_bin,
1427 "link failed when adding sink, caps %s, reason %d", capsstr, linkres);
1429 gst_caps_unref (caps);
1431 gst_element_set_state (sink, GST_STATE_NULL);
1432 gst_bin_remove (GST_BIN_CAST (play_bin), sink);
1440 /* could not link this stream */
1441 caps = gst_pad_get_caps (subtitle_pad);
1442 capsstr = gst_caps_to_string (caps);
1443 GST_DEBUG_OBJECT (play_bin,
1444 "subtitle link failed when adding sink, caps %s, reason %d", capsstr,
1447 gst_caps_unref (caps);
1455 setup_sinks (GstPlayBaseBin * play_base_bin, GstPlayBaseGroup * group)
1457 GstPlayBin *play_bin = GST_PLAY_BIN (play_base_bin);
1458 GList *streaminfo = NULL, *s;
1459 gboolean need_vis = FALSE;
1460 gboolean need_text = FALSE;
1461 GstPad *textsrcpad = NULL, *pad = NULL;
1463 gboolean res = TRUE;
1465 /* get rid of existing sinks */
1466 if (play_bin->sinks) {
1467 remove_sinks (play_bin);
1469 GST_DEBUG_OBJECT (play_base_bin, "setupsinks");
1471 /* find out what to do */
1472 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0 &&
1473 group->type[GST_STREAM_TYPE_TEXT - 1].npads > 0) {
1475 } else if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads == 0 &&
1476 group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0 &&
1477 play_bin->visualisation != NULL) {
1481 /* now actually connect everything */
1482 g_object_get (G_OBJECT (play_base_bin), "stream-info", &streaminfo, NULL);
1483 for (s = streaminfo; s; s = g_list_next (s)) {
1484 GObject *obj = G_OBJECT (s->data);
1488 g_object_get (obj, "type", &type, NULL);
1489 g_object_get (obj, "object", &object, NULL);
1493 if (group->type[GST_STREAM_TYPE_AUDIO - 1].npads > 0) {
1495 sink = gen_vis_element (play_bin);
1497 sink = gen_audio_element (play_bin);
1502 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_AUDIO - 1].preroll,
1504 res = add_sink (play_bin, sink, pad, NULL);
1505 gst_object_unref (pad);
1509 if (group->type[GST_STREAM_TYPE_VIDEO - 1].npads > 0) {
1511 GstObject *parent = NULL, *grandparent = NULL;
1512 GstPad *ghost = NULL;
1514 sink = gen_text_element (play_bin);
1516 gst_element_get_pad (group->type[GST_STREAM_TYPE_TEXT - 1].preroll,
1518 /* This pad is from subtitle-bin, we need to create a ghost pad to have
1519 common grandparents */
1520 parent = gst_object_get_parent (GST_OBJECT_CAST (textsrcpad));
1522 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no parent !");
1523 gst_object_unref (textsrcpad);
1528 grandparent = gst_object_get_parent (parent);
1530 GST_WARNING_OBJECT (textsrcpad, "subtitle pad has no grandparent !");
1531 gst_object_unref (parent);
1532 gst_object_unref (textsrcpad);
1537 /* We ghost the pad on subtitle_bin only, if the text pad is from the
1538 media demuxer we keep it as it is */
1539 if (!GST_IS_PLAY_BIN (grandparent)) {
1540 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from a subtitle "
1541 "file, ghosting to a suitable hierarchy");
1542 ghost = gst_ghost_pad_new ("text_src", textsrcpad);
1543 if (!GST_IS_PAD (ghost)) {
1544 GST_WARNING_OBJECT (textsrcpad, "failed creating ghost pad for "
1546 gst_object_unref (parent);
1547 gst_object_unref (grandparent);
1548 gst_object_unref (textsrcpad);
1553 if (gst_element_add_pad (GST_ELEMENT_CAST (grandparent), ghost)) {
1554 gst_object_unref (textsrcpad);
1555 textsrcpad = gst_object_ref (ghost);
1557 GST_WARNING_OBJECT (ghost, "failed adding ghost pad on subtitle-bin");
1558 gst_object_unref (ghost);
1559 gst_object_unref (textsrcpad);
1563 GST_DEBUG_OBJECT (textsrcpad, "this subtitle pad is from the demuxer "
1564 "no changes to hierarchy needed");
1567 gst_object_unref (parent);
1568 gst_object_unref (grandparent);
1570 sink = gen_video_element (play_bin);
1575 pad = gst_element_get_pad (group->type[GST_STREAM_TYPE_VIDEO - 1].preroll,
1577 res = add_sink (play_bin, sink, pad, textsrcpad);
1578 gst_object_unref (pad);
1580 gst_object_unref (textsrcpad);
1583 /* remove the sinks now, pipeline get_state will now wait for the
1584 * sinks to preroll */
1585 if (play_bin->fakesink) {
1586 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1587 gst_bin_remove (GST_BIN_CAST (play_bin), play_bin->fakesink);
1588 play_bin->fakesink = NULL;
1594 /* Send an event to our sinks until one of them works; don't then send to the
1595 * remaining sinks (unlike GstBin)
1598 gst_play_bin_send_event_to_sink (GstPlayBin * play_bin, GstEvent * event)
1600 GList *sinks = play_bin->sinks;
1601 gboolean res = TRUE;
1604 GstElement *sink = GST_ELEMENT_CAST (sinks->data);
1606 gst_event_ref (event);
1607 if ((res = gst_element_send_event (sink, event))) {
1608 GST_DEBUG_OBJECT (play_bin,
1609 "Sent event succesfully to sink %" GST_PTR_FORMAT, sink);
1612 GST_DEBUG_OBJECT (play_bin,
1613 "Event failed when sent to sink %" GST_PTR_FORMAT, sink);
1615 sinks = g_list_next (sinks);
1618 gst_event_unref (event);
1624 do_playbin_seek (GstElement * element, GstEvent * event)
1629 gboolean was_playing = FALSE;
1632 gst_event_parse_seek (event, &rate, NULL, &flags, NULL, NULL, NULL, NULL);
1634 flush = flags & GST_SEEK_FLAG_FLUSH;
1639 /* need to call _get_state() since a bin state is only updated
1640 * with this call. */
1641 gst_element_get_state (element, &state, NULL, 0);
1642 was_playing = state == GST_STATE_PLAYING;
1645 gst_element_set_state (element, GST_STATE_PAUSED);
1646 gst_element_get_state (element, NULL, NULL, 50 * GST_MSECOND);
1650 GST_DEBUG_OBJECT (element, "Sending seek event to a sink");
1651 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1654 /* need to reset the stream time to 0 after a flushing seek */
1656 gst_pipeline_set_new_stream_time (GST_PIPELINE (element), 0);
1659 /* and continue playing */
1660 gst_element_set_state (element, GST_STATE_PLAYING);
1665 /* We only want to send the event to a single sink (overriding GstBin's
1666 * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
1667 * events appropriately. So, this is a messy duplication of code. */
1669 gst_play_bin_send_event (GstElement * element, GstEvent * event)
1671 gboolean res = FALSE;
1672 GstEventType event_type = GST_EVENT_TYPE (event);
1675 switch (event_type) {
1676 case GST_EVENT_SEEK:
1677 res = do_playbin_seek (element, event);
1680 res = gst_play_bin_send_event_to_sink (GST_PLAY_BIN (element), event);
1687 /* Override the set_clock function, we don't want to set a clock on the sinks
1688 * when we are live pipeline so that they don't synchronize until this is
1691 gst_play_bin_set_clock_func (GstElement * element, GstClock * clock)
1695 GstPlayBin *play_bin;
1696 gboolean res = TRUE;
1697 GstElement *asink, *vsink;
1699 bin = GST_BIN (element);
1700 play_bin = GST_PLAY_BIN (element);
1702 asink = g_hash_table_lookup (play_bin->cache, "audio_sink");
1703 vsink = g_hash_table_lookup (play_bin->cache, "video_sink");
1705 GST_DEBUG_OBJECT (play_bin, "setting clock, is_live %d", play_bin->is_live);
1707 GST_OBJECT_LOCK (bin);
1708 if (element->clock != clock) {
1709 for (children = bin->children; children; children = g_list_next (children)) {
1710 GstElement *child = GST_ELEMENT (children->data);
1712 if (play_bin->is_live && (child == asink || child == vsink))
1713 res &= gst_element_set_clock (child, NULL);
1715 res &= gst_element_set_clock (child, clock);
1718 GST_OBJECT_UNLOCK (bin);
1724 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
1725 GList * structure_list)
1729 for (l = structure_list; l != NULL; l = l->next) {
1730 GValue val = { 0, };
1733 *first = gst_structure_copy ((GstStructure *) l->data);
1735 g_value_init (&val, GST_TYPE_STRUCTURE);
1736 g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
1737 gst_value_list_append_value (list_val, &val);
1738 g_value_unset (&val);
1742 /* if it's a redirect message with multiple redirect locations we might
1743 * want to pick a different 'best' location depending on the required
1744 * bitrates and the connection speed */
1746 gst_play_bin_handle_redirect_message (GstPlayBin * playbin, GstMessage * msg)
1748 const GValue *locations_list, *location_val;
1749 GstMessage *new_msg;
1750 GstStructure *new_structure = NULL;
1751 GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
1752 GValue new_list = { 0, };
1755 GST_DEBUG_OBJECT (playbin, "redirect message: %" GST_PTR_FORMAT, msg);
1756 GST_DEBUG_OBJECT (playbin, "connection speed: %u", playbin->connection_speed);
1758 if (playbin->connection_speed == 0 || msg->structure == NULL)
1761 locations_list = gst_structure_get_value (msg->structure, "locations");
1762 if (locations_list == NULL)
1765 size = gst_value_list_get_size (locations_list);
1769 /* maintain existing order as much as possible, just sort references
1770 * with too high a bitrate to the end (the assumption being that if
1771 * bitrates are given they are given for all interesting streams and
1772 * that the you-need-at-least-version-xyz redirect has the same bitrate
1773 * as the lowest referenced redirect alternative) */
1774 for (i = 0; i < size; ++i) {
1775 const GstStructure *s;
1778 location_val = gst_value_list_get_value (locations_list, i);
1779 s = (const GstStructure *) g_value_get_boxed (location_val);
1780 if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
1781 GST_DEBUG_OBJECT (playbin, "no bitrate: %" GST_PTR_FORMAT, s);
1782 l_neutral = g_list_append (l_neutral, (gpointer) s);
1783 } else if (bitrate > playbin->connection_speed) {
1784 GST_DEBUG_OBJECT (playbin, "bitrate too high: %" GST_PTR_FORMAT, s);
1785 l_bad = g_list_append (l_bad, (gpointer) s);
1786 } else if (bitrate <= playbin->connection_speed) {
1787 GST_DEBUG_OBJECT (playbin, "bitrate OK: %" GST_PTR_FORMAT, s);
1788 l_good = g_list_append (l_good, (gpointer) s);
1792 g_value_init (&new_list, GST_TYPE_LIST);
1793 value_list_append_structure_list (&new_list, &new_structure, l_good);
1794 value_list_append_structure_list (&new_list, &new_structure, l_neutral);
1795 value_list_append_structure_list (&new_list, &new_structure, l_bad);
1796 gst_structure_set_value (new_structure, "locations", &new_list);
1797 g_value_unset (&new_list);
1799 g_list_free (l_good);
1800 g_list_free (l_neutral);
1801 g_list_free (l_bad);
1803 new_msg = gst_message_new_element (msg->src, new_structure);
1804 gst_message_unref (msg);
1806 GST_DEBUG_OBJECT (playbin, "new redirect message: %" GST_PTR_FORMAT, new_msg);
1811 gst_play_bin_handle_message (GstBin * bin, GstMessage * msg)
1813 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT && msg->structure != NULL
1814 && gst_structure_has_name (msg->structure, "redirect")) {
1815 msg = gst_play_bin_handle_redirect_message (GST_PLAY_BIN (bin), msg);
1818 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
1821 static GstStateChangeReturn
1822 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
1824 GstStateChangeReturn ret;
1825 GstPlayBin *play_bin;
1827 play_bin = GST_PLAY_BIN (element);
1830 switch (transition) {
1831 case GST_STATE_CHANGE_READY_TO_PAUSED:
1832 /* this really is the easiest way to make the state change return
1833 * ASYNC until we added the sinks */
1834 if (!play_bin->fakesink) {
1835 play_bin->fakesink = gst_element_factory_make ("fakesink", "test");
1836 gst_bin_add (GST_BIN_CAST (play_bin), play_bin->fakesink);
1843 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1844 if (ret == GST_STATE_CHANGE_FAILURE)
1847 switch (transition) {
1848 case GST_STATE_CHANGE_READY_TO_PAUSED:
1849 /* remember us being a live pipeline */
1850 play_bin->is_live = (ret == GST_STATE_CHANGE_NO_PREROLL);
1851 GST_DEBUG_OBJECT (play_bin, "is live: %d", play_bin->is_live);
1853 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1854 /* FIXME Release audio device when we implement that */
1856 case GST_STATE_CHANGE_PAUSED_TO_READY:
1857 /* remove sinks we added */
1858 remove_sinks (play_bin);
1859 /* and there might be a fakesink we need to clean up now */
1860 if (play_bin->fakesink) {
1861 gst_element_set_state (play_bin->fakesink, GST_STATE_NULL);
1862 gst_bin_remove (GST_BIN_CAST (play_bin), play_bin->fakesink);
1863 play_bin->fakesink = NULL;
1874 plugin_init (GstPlugin * plugin)
1876 GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
1879 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1881 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1882 #endif /* ENABLE_NLS */
1884 return gst_element_register (plugin, "playbin", GST_RANK_NONE,
1888 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1891 "player bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,