2 * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
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-autovideosink
22 * @see_also: autoaudiosink, ximagesink, xvimagesink, sdlvideosink
26 * autovideosink is a video sink that automatically detects an appropriate
27 * video sink to use. It does so by scanning the registry for all elements
28 * that have <quote>Sink</quote> and <quote>Audio</quote> in the class field
29 * of their element information, and also have a non-zero autoplugging rank.
31 * <title>Example launch line</title>
34 * gst-launch -v -m videotestsrc ! autovideosink
46 #include "gstautovideosink.h"
47 #include "gstautodetect.h"
49 static GstStateChangeReturn
50 gst_auto_video_sink_change_state (GstElement * element,
51 GstStateChange transition);
53 GST_BOILERPLATE (GstAutoVideoSink, gst_auto_video_sink, GstBin, GST_TYPE_BIN);
56 gst_auto_video_sink_base_init (gpointer klass)
58 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
59 const GstElementDetails gst_auto_video_sink_details =
60 GST_ELEMENT_DETAILS ("Auto video sink",
62 "Wrapper video sink for automatically detected video sink",
63 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
64 GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69 gst_element_class_add_pad_template (eklass,
70 gst_static_pad_template_get (&sink_template));
71 gst_element_class_set_details (eklass, &gst_auto_video_sink_details);
75 gst_auto_video_sink_class_init (GstAutoVideoSinkClass * klass)
77 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
79 eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_video_sink_change_state);
83 * Hack to make initial linking work; ideally, this'd work even when
84 * no target has been assigned to the ghostpad yet.
88 gst_auto_video_sink_reset (GstAutoVideoSink * sink)
92 /* fakesink placeholder */
94 gst_element_set_state (sink->kid, GST_STATE_NULL);
95 gst_bin_remove (GST_BIN (sink), sink->kid);
97 sink->kid = gst_element_factory_make ("fakesink", "tempsink");
98 gst_bin_add (GST_BIN (sink), sink->kid);
101 targetpad = gst_element_get_pad (sink->kid, "sink");
102 gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
103 gst_object_unref (targetpad);
107 gst_auto_video_sink_init (GstAutoVideoSink * sink,
108 GstAutoVideoSinkClass * g_class)
110 sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
111 gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
113 gst_auto_video_sink_reset (sink);
115 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
119 gst_auto_video_sink_factory_filter (GstPluginFeature * feature, gpointer data)
124 /* we only care about element factories */
125 if (!GST_IS_ELEMENT_FACTORY (feature))
129 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
130 if (!(strstr (klass, "Sink") && strstr (klass, "Video")))
133 /* only select elements with autoplugging rank */
134 rank = gst_plugin_feature_get_rank (feature);
135 if (rank < GST_RANK_MARGINAL)
142 gst_auto_video_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
146 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
149 return strcmp (gst_plugin_feature_get_name (f2),
150 gst_plugin_feature_get_name (f1));
154 gst_auto_video_sink_find_best (GstAutoVideoSink * sink)
156 GstElement *choice = NULL;
158 gchar *child_name = g_strdup_printf ("%s-actual-sink",
159 GST_OBJECT_NAME (sink));
161 list = gst_registry_feature_filter (gst_registry_get_default (),
162 (GstPluginFeatureFilter) gst_auto_video_sink_factory_filter, FALSE, sink);
163 list = g_list_sort (list, (GCompareFunc) gst_auto_video_sink_compare_ranks);
165 for (walk = list; walk != NULL; walk = walk->next) {
166 GstElementFactory *f = GST_ELEMENT_FACTORY (walk->data);
169 GST_DEBUG_OBJECT (sink, "Trying %s", GST_PLUGIN_FEATURE (f)->name);
170 if ((el = gst_element_factory_create (f, child_name))) {
171 GstStateChangeReturn ret;
173 GST_DEBUG_OBJECT (sink, "Changing state to READY");
175 ret = gst_element_set_state (el, GST_STATE_READY);
176 if (ret == GST_STATE_CHANGE_SUCCESS) {
177 GST_DEBUG_OBJECT (sink, "success");
182 GST_WARNING_OBJECT (sink, "Couldn't set READY: %d", ret);
183 ret = gst_element_set_state (el, GST_STATE_NULL);
184 if (ret != GST_STATE_CHANGE_SUCCESS)
185 GST_WARNING_OBJECT (sink,
186 "Couldn't set element to NULL prior to disposal.");
188 gst_object_unref (el);
194 gst_plugin_feature_list_free (list);
200 gst_auto_video_sink_detect (GstAutoVideoSink * sink)
206 gst_element_set_state (sink->kid, GST_STATE_NULL);
207 gst_bin_remove (GST_BIN (sink), sink->kid);
212 GST_DEBUG_OBJECT (sink, "Creating new kid");
213 if (!(esink = gst_auto_video_sink_find_best (sink))) {
214 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
215 ("Failed to find a supported video sink"));
220 gst_bin_add (GST_BIN (sink), esink);
222 /* attach ghost pad */
223 GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
224 targetpad = gst_element_get_pad (sink->kid, "sink");
225 gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
226 gst_object_unref (targetpad);
227 GST_DEBUG_OBJECT (sink, "done changing auto video sink");
232 static GstStateChangeReturn
233 gst_auto_video_sink_change_state (GstElement * element,
234 GstStateChange transition)
236 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
237 GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (element);
239 switch (transition) {
240 case GST_STATE_CHANGE_NULL_TO_READY:
241 if (!gst_auto_video_sink_detect (sink))
242 return GST_STATE_CHANGE_FAILURE;
248 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
250 switch (transition) {
251 case GST_STATE_CHANGE_READY_TO_NULL:
252 gst_auto_video_sink_reset (sink);