2 * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3 * (c) 2006 Jan Schmidt <thaytan@noraisin.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-autovideosink
23 * @see_also: autoaudiosink, ximagesink, xvimagesink, sdlvideosink
25 * autovideosink is a video sink that automatically detects an appropriate
26 * video sink to use. It does so by scanning the registry for all elements
27 * that have <quote>Sink</quote> and <quote>Video</quote> in the class field
28 * of their element information, and also have a non-zero autoplugging rank.
31 * <title>Example launch line</title>
33 * gst-launch -v -m videotestsrc ! autovideosink
44 #include "gstautovideosink.h"
45 #include "gstautodetect.h"
54 static GstStateChangeReturn
55 gst_auto_video_sink_change_state (GstElement * element,
56 GstStateChange transition);
57 static void gst_auto_video_sink_dispose (GstAutoVideoSink * sink);
58 static void gst_auto_video_sink_clear_kid (GstAutoVideoSink * sink);
60 static void gst_auto_video_sink_set_property (GObject * object, guint prop_id,
61 const GValue * value, GParamSpec * pspec);
62 static void gst_auto_video_sink_get_property (GObject * object, guint prop_id,
63 GValue * value, GParamSpec * pspec);
65 #define gst_auto_video_sink_parent_class parent_class
66 G_DEFINE_TYPE (GstAutoVideoSink, gst_auto_video_sink, GST_TYPE_BIN);
68 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
74 gst_auto_video_sink_class_init (GstAutoVideoSinkClass * klass)
76 GObjectClass *gobject_class;
77 GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
79 gobject_class = G_OBJECT_CLASS (klass);
80 gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_video_sink_dispose;
81 gobject_class->set_property = gst_auto_video_sink_set_property;
82 gobject_class->get_property = gst_auto_video_sink_get_property;
84 eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_video_sink_change_state);
86 * GstAutoVideoSink:filter-caps
88 * This property will filter out candidate sinks that can handle the specified
89 * caps. By default only video sinks that support raw rgb and yuv video
92 * This property can only be set before the element goes to the READY state.
96 g_object_class_install_property (gobject_class, PROP_CAPS,
97 g_param_spec_boxed ("filter-caps", "Filter caps",
98 "Filter sink candidates using these caps.", GST_TYPE_CAPS,
99 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101 gst_element_class_add_pad_template (eklass,
102 gst_static_pad_template_get (&sink_template));
103 gst_element_class_set_details_simple (eklass, "Auto video sink",
105 "Wrapper video sink for automatically detected video sink",
106 "Jan Schmidt <thaytan@noraisin.net>");
110 gst_auto_video_sink_dispose (GstAutoVideoSink * sink)
112 gst_auto_video_sink_clear_kid (sink);
114 if (sink->filter_caps)
115 gst_caps_unref (sink->filter_caps);
116 sink->filter_caps = NULL;
118 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
122 gst_auto_video_sink_clear_kid (GstAutoVideoSink * sink)
125 gst_element_set_state (sink->kid, GST_STATE_NULL);
126 gst_bin_remove (GST_BIN (sink), sink->kid);
128 /* Don't lose the SINK flag */
129 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
134 * Hack to make initial linking work; ideally, this'd work even when
135 * no target has been assigned to the ghostpad yet.
139 gst_auto_video_sink_reset (GstAutoVideoSink * sink)
143 /* Remove any existing element */
144 gst_auto_video_sink_clear_kid (sink);
146 /* fakesink placeholder */
147 sink->kid = gst_element_factory_make ("fakesink", "tempsink");
148 gst_bin_add (GST_BIN (sink), sink->kid);
150 /* pad, setting this target should always work */
151 targetpad = gst_element_get_static_pad (sink->kid, "sink");
152 gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
153 gst_object_unref (targetpad);
156 static GstStaticCaps raw_caps =
157 GST_STATIC_CAPS ("video/x-raw-yuv; video/x-raw-rgb");
160 gst_auto_video_sink_init (GstAutoVideoSink * sink)
162 sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
163 gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
165 gst_auto_video_sink_reset (sink);
167 /* set the default raw video caps */
168 sink->filter_caps = gst_static_caps_get (&raw_caps);
171 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
175 gst_auto_video_sink_factory_filter (GstPluginFeature * feature, gpointer data)
180 /* we only care about element factories */
181 if (!GST_IS_ELEMENT_FACTORY (feature))
185 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
186 if (!(strstr (klass, "Sink") && strstr (klass, "Video")))
189 /* only select elements with autoplugging rank */
190 rank = gst_plugin_feature_get_rank (feature);
191 if (rank < GST_RANK_MARGINAL)
198 gst_auto_video_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
202 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
205 return strcmp (gst_plugin_feature_get_name (f2),
206 gst_plugin_feature_get_name (f1));
210 gst_auto_video_sink_create_element_with_pretty_name (GstAutoVideoSink * sink,
211 GstElementFactory * factory)
214 gchar *name, *marker;
216 marker = g_strdup (GST_OBJECT_NAME (factory));
217 if (g_str_has_suffix (marker, "sink"))
218 marker[strlen (marker) - 4] = '\0';
219 if (g_str_has_prefix (marker, "gst"))
220 g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
221 name = g_strdup_printf ("%s-actual-sink-%s", GST_OBJECT_NAME (sink), marker);
224 element = gst_element_factory_create (factory, name);
231 gst_auto_video_sink_find_best (GstAutoVideoSink * sink)
234 GstElement *choice = NULL;
235 GstMessage *message = NULL;
236 GSList *errors = NULL;
237 GstBus *bus = gst_bus_new ();
238 GstPad *el_pad = NULL;
239 GstCaps *el_caps = NULL;
240 gboolean no_match = TRUE;
242 list = gst_registry_feature_filter (gst_registry_get_default (),
243 (GstPluginFeatureFilter) gst_auto_video_sink_factory_filter, FALSE, sink);
244 list = g_list_sort (list, (GCompareFunc) gst_auto_video_sink_compare_ranks);
246 GST_LOG_OBJECT (sink, "Trying to find usable video devices ...");
248 for (item = list; item != NULL; item = item->next) {
249 GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
252 if ((el = gst_auto_video_sink_create_element_with_pretty_name (sink, f))) {
253 GstStateChangeReturn ret;
255 GST_DEBUG_OBJECT (sink, "Testing %s", GST_OBJECT_NAME (f));
257 /* If autovideosink has been provided with filter caps,
258 * accept only sinks that match with the filter caps */
259 if (sink->filter_caps) {
260 el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "sink");
261 el_caps = gst_pad_get_caps (el_pad, NULL);
262 gst_object_unref (el_pad);
263 GST_DEBUG_OBJECT (sink,
264 "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
265 sink->filter_caps, el_caps);
266 no_match = !gst_caps_can_intersect (sink->filter_caps, el_caps);
267 gst_caps_unref (el_caps);
270 GST_DEBUG_OBJECT (sink, "Incompatible caps");
271 gst_object_unref (el);
274 GST_DEBUG_OBJECT (sink, "Found compatible caps");
278 gst_element_set_bus (el, bus);
279 ret = gst_element_set_state (el, GST_STATE_READY);
280 if (ret == GST_STATE_CHANGE_SUCCESS) {
281 GST_DEBUG_OBJECT (sink, "This worked!");
286 /* collect all error messages */
287 while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
288 GST_DEBUG_OBJECT (sink, "error message %" GST_PTR_FORMAT, message);
289 errors = g_slist_append (errors, message);
292 gst_element_set_state (el, GST_STATE_NULL);
293 gst_object_unref (el);
297 GST_DEBUG_OBJECT (sink, "done trying");
300 /* FIXME: we forward the first error for now; but later on it might make
301 * sense to actually analyse them */
302 gst_message_ref (GST_MESSAGE (errors->data));
303 GST_DEBUG_OBJECT (sink, "reposting message %p", errors->data);
304 gst_element_post_message (GST_ELEMENT (sink), GST_MESSAGE (errors->data));
306 /* send warning message to application and use a fakesink */
307 GST_ELEMENT_WARNING (sink, RESOURCE, NOT_FOUND, (NULL),
308 ("Failed to find a usable video sink"));
309 choice = gst_element_factory_make ("fakesink", "fake-video-sink");
310 if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
311 g_object_set (choice, "sync", TRUE, NULL);
312 gst_element_set_state (choice, GST_STATE_READY);
315 gst_object_unref (bus);
316 gst_plugin_feature_list_free (list);
317 g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
318 g_slist_free (errors);
324 gst_auto_video_sink_detect (GstAutoVideoSink * sink)
329 gst_auto_video_sink_clear_kid (sink);
332 GST_DEBUG_OBJECT (sink, "Creating new kid");
333 if (!(esink = gst_auto_video_sink_find_best (sink)))
337 gst_bin_add (GST_BIN (sink), esink);
339 /* attach ghost pad */
340 GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
341 targetpad = gst_element_get_static_pad (sink->kid, "sink");
342 if (!gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad))
345 gst_object_unref (targetpad);
346 GST_DEBUG_OBJECT (sink, "done changing auto video sink");
353 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
354 ("Failed to find a supported video sink"));
359 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
360 ("Failed to set target pad"));
361 gst_object_unref (targetpad);
366 static GstStateChangeReturn
367 gst_auto_video_sink_change_state (GstElement * element,
368 GstStateChange transition)
370 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
371 GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (element);
373 switch (transition) {
374 case GST_STATE_CHANGE_NULL_TO_READY:
375 if (!gst_auto_video_sink_detect (sink))
376 return GST_STATE_CHANGE_FAILURE;
382 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
384 switch (transition) {
385 case GST_STATE_CHANGE_READY_TO_NULL:
386 gst_auto_video_sink_reset (sink);
396 gst_auto_video_sink_set_property (GObject * object, guint prop_id,
397 const GValue * value, GParamSpec * pspec)
399 GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
403 if (sink->filter_caps)
404 gst_caps_unref (sink->filter_caps);
405 sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
414 gst_auto_video_sink_get_property (GObject * object, guint prop_id,
415 GValue * value, GParamSpec * pspec)
417 GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
421 gst_value_set_caps (value, sink->filter_caps);
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);