Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / autodetect / gstautovideosink.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2006 Jan Schmidt <thaytan@noraisin.net>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:element-autovideosink
23  * @see_also: autoaudiosink, ximagesink, xvimagesink, sdlvideosink
24  *
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.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch -v -m videotestsrc ! autovideosink
34  * ]|
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <string.h>
43
44 #include "gstautovideosink.h"
45 #include "gstautodetect.h"
46
47 /* Properties */
48 enum
49 {
50   PROP_0,
51   PROP_CAPS,
52 };
53
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);
59
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);
64
65 #define gst_auto_video_sink_parent_class parent_class
66 G_DEFINE_TYPE (GstAutoVideoSink, gst_auto_video_sink, GST_TYPE_BIN);
67
68 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69     GST_PAD_SINK,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS_ANY);
72
73 static void
74 gst_auto_video_sink_class_init (GstAutoVideoSinkClass * klass)
75 {
76   GObjectClass *gobject_class;
77   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
78
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;
83
84   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_video_sink_change_state);
85   /**
86    * GstAutoVideoSink:filter-caps
87    *
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
90    * are selected.
91    *
92    * This property can only be set before the element goes to the READY state.
93    *
94    * Since: 0.10.7
95    **/
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));
100
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",
104       "Sink/Video",
105       "Wrapper video sink for automatically detected video sink",
106       "Jan Schmidt <thaytan@noraisin.net>");
107 }
108
109 static void
110 gst_auto_video_sink_dispose (GstAutoVideoSink * sink)
111 {
112   gst_auto_video_sink_clear_kid (sink);
113
114   if (sink->filter_caps)
115     gst_caps_unref (sink->filter_caps);
116   sink->filter_caps = NULL;
117
118   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) sink);
119 }
120
121 static void
122 gst_auto_video_sink_clear_kid (GstAutoVideoSink * sink)
123 {
124   if (sink->kid) {
125     gst_element_set_state (sink->kid, GST_STATE_NULL);
126     gst_bin_remove (GST_BIN (sink), sink->kid);
127     sink->kid = NULL;
128     /* Don't lose the SINK flag */
129     GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
130   }
131 }
132
133 /*
134  * Hack to make initial linking work; ideally, this'd work even when
135  * no target has been assigned to the ghostpad yet.
136  */
137
138 static void
139 gst_auto_video_sink_reset (GstAutoVideoSink * sink)
140 {
141   GstPad *targetpad;
142
143   /* Remove any existing element */
144   gst_auto_video_sink_clear_kid (sink);
145
146   /* fakesink placeholder */
147   sink->kid = gst_element_factory_make ("fakesink", "tempsink");
148   gst_bin_add (GST_BIN (sink), sink->kid);
149
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);
154 }
155
156 static GstStaticCaps raw_caps = GST_STATIC_CAPS ("video/x-raw");
157
158 static void
159 gst_auto_video_sink_init (GstAutoVideoSink * sink)
160 {
161   sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
162   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
163
164   gst_auto_video_sink_reset (sink);
165
166   /* set the default raw video caps */
167   sink->filter_caps = gst_static_caps_get (&raw_caps);
168
169   /* mark as sink */
170   GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
171 }
172
173 static gboolean
174 gst_auto_video_sink_factory_filter (GstPluginFeature * feature, gpointer data)
175 {
176   guint rank;
177   const gchar *klass;
178
179   /* we only care about element factories */
180   if (!GST_IS_ELEMENT_FACTORY (feature))
181     return FALSE;
182
183   /* video sinks */
184   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
185   if (!(strstr (klass, "Sink") && strstr (klass, "Video")))
186     return FALSE;
187
188   /* only select elements with autoplugging rank */
189   rank = gst_plugin_feature_get_rank (feature);
190   if (rank < GST_RANK_MARGINAL)
191     return FALSE;
192
193   return TRUE;
194 }
195
196 static gint
197 gst_auto_video_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
198 {
199   gint diff;
200
201   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
202   if (diff != 0)
203     return diff;
204   return strcmp (gst_plugin_feature_get_name (f2),
205       gst_plugin_feature_get_name (f1));
206 }
207
208 static GstElement *
209 gst_auto_video_sink_create_element_with_pretty_name (GstAutoVideoSink * sink,
210     GstElementFactory * factory)
211 {
212   GstElement *element;
213   gchar *name, *marker;
214
215   marker = g_strdup (GST_OBJECT_NAME (factory));
216   if (g_str_has_suffix (marker, "sink"))
217     marker[strlen (marker) - 4] = '\0';
218   if (g_str_has_prefix (marker, "gst"))
219     g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
220   name = g_strdup_printf ("%s-actual-sink-%s", GST_OBJECT_NAME (sink), marker);
221   g_free (marker);
222
223   element = gst_element_factory_create (factory, name);
224   g_free (name);
225
226   return element;
227 }
228
229 static GstElement *
230 gst_auto_video_sink_find_best (GstAutoVideoSink * sink)
231 {
232   GList *list, *item;
233   GstElement *choice = NULL;
234   GstMessage *message = NULL;
235   GSList *errors = NULL;
236   GstBus *bus = gst_bus_new ();
237   GstPad *el_pad = NULL;
238   GstCaps *el_caps = NULL;
239   gboolean no_match = TRUE;
240
241   list = gst_registry_feature_filter (gst_registry_get_default (),
242       (GstPluginFeatureFilter) gst_auto_video_sink_factory_filter, FALSE, sink);
243   list = g_list_sort (list, (GCompareFunc) gst_auto_video_sink_compare_ranks);
244
245   GST_LOG_OBJECT (sink, "Trying to find usable video devices ...");
246
247   for (item = list; item != NULL; item = item->next) {
248     GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
249     GstElement *el;
250
251     if ((el = gst_auto_video_sink_create_element_with_pretty_name (sink, f))) {
252       GstStateChangeReturn ret;
253
254       GST_DEBUG_OBJECT (sink, "Testing %s", GST_OBJECT_NAME (f));
255
256       /* If autovideosink has been provided with filter caps,
257        * accept only sinks that match with the filter caps */
258       if (sink->filter_caps) {
259         el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "sink");
260         el_caps = gst_pad_get_caps (el_pad, NULL);
261         gst_object_unref (el_pad);
262         GST_DEBUG_OBJECT (sink,
263             "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
264             sink->filter_caps, el_caps);
265         no_match = !gst_caps_can_intersect (sink->filter_caps, el_caps);
266         gst_caps_unref (el_caps);
267
268         if (no_match) {
269           GST_DEBUG_OBJECT (sink, "Incompatible caps");
270           gst_object_unref (el);
271           continue;
272         } else {
273           GST_DEBUG_OBJECT (sink, "Found compatible caps");
274         }
275       }
276
277       gst_element_set_bus (el, bus);
278       ret = gst_element_set_state (el, GST_STATE_READY);
279       if (ret == GST_STATE_CHANGE_SUCCESS) {
280         GST_DEBUG_OBJECT (sink, "This worked!");
281         choice = el;
282         break;
283       }
284
285       /* collect all error messages */
286       while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
287         GST_DEBUG_OBJECT (sink, "error message %" GST_PTR_FORMAT, message);
288         errors = g_slist_append (errors, message);
289       }
290
291       gst_element_set_state (el, GST_STATE_NULL);
292       gst_object_unref (el);
293     }
294   }
295
296   GST_DEBUG_OBJECT (sink, "done trying");
297   if (!choice) {
298     if (errors) {
299       /* FIXME: we forward the first error for now; but later on it might make
300        * sense to actually analyse them */
301       gst_message_ref (GST_MESSAGE (errors->data));
302       GST_DEBUG_OBJECT (sink, "reposting message %p", errors->data);
303       gst_element_post_message (GST_ELEMENT (sink), GST_MESSAGE (errors->data));
304     } else {
305       /* send warning message to application and use a fakesink */
306       GST_ELEMENT_WARNING (sink, RESOURCE, NOT_FOUND, (NULL),
307           ("Failed to find a usable video sink"));
308       choice = gst_element_factory_make ("fakesink", "fake-video-sink");
309       if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
310         g_object_set (choice, "sync", TRUE, NULL);
311       gst_element_set_state (choice, GST_STATE_READY);
312     }
313   }
314   gst_object_unref (bus);
315   gst_plugin_feature_list_free (list);
316   g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
317   g_slist_free (errors);
318
319   return choice;
320 }
321
322 static gboolean
323 gst_auto_video_sink_detect (GstAutoVideoSink * sink)
324 {
325   GstElement *esink;
326   GstPad *targetpad;
327
328   gst_auto_video_sink_clear_kid (sink);
329
330   /* find element */
331   GST_DEBUG_OBJECT (sink, "Creating new kid");
332   if (!(esink = gst_auto_video_sink_find_best (sink)))
333     goto no_sink;
334
335   sink->kid = esink;
336   gst_bin_add (GST_BIN (sink), esink);
337
338   /* attach ghost pad */
339   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
340   targetpad = gst_element_get_static_pad (sink->kid, "sink");
341   if (!gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad))
342     goto target_failed;
343
344   gst_object_unref (targetpad);
345   GST_DEBUG_OBJECT (sink, "done changing auto video sink");
346
347   return TRUE;
348
349   /* ERRORS */
350 no_sink:
351   {
352     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
353         ("Failed to find a supported video sink"));
354     return FALSE;
355   }
356 target_failed:
357   {
358     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
359         ("Failed to set target pad"));
360     gst_object_unref (targetpad);
361     return FALSE;
362   }
363 }
364
365 static GstStateChangeReturn
366 gst_auto_video_sink_change_state (GstElement * element,
367     GstStateChange transition)
368 {
369   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
370   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (element);
371
372   switch (transition) {
373     case GST_STATE_CHANGE_NULL_TO_READY:
374       if (!gst_auto_video_sink_detect (sink))
375         return GST_STATE_CHANGE_FAILURE;
376       break;
377     default:
378       break;
379   }
380
381   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
382
383   switch (transition) {
384     case GST_STATE_CHANGE_READY_TO_NULL:
385       gst_auto_video_sink_reset (sink);
386       break;
387     default:
388       break;
389   }
390
391   return ret;
392 }
393
394 static void
395 gst_auto_video_sink_set_property (GObject * object, guint prop_id,
396     const GValue * value, GParamSpec * pspec)
397 {
398   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
399
400   switch (prop_id) {
401     case PROP_CAPS:
402       if (sink->filter_caps)
403         gst_caps_unref (sink->filter_caps);
404       sink->filter_caps = gst_caps_copy (gst_value_get_caps (value));
405       break;
406     default:
407       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
408       break;
409   }
410 }
411
412 static void
413 gst_auto_video_sink_get_property (GObject * object, guint prop_id,
414     GValue * value, GParamSpec * pspec)
415 {
416   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (object);
417
418   switch (prop_id) {
419     case PROP_CAPS:{
420       gst_value_set_caps (value, sink->filter_caps);
421       break;
422     }
423     default:
424       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
425       break;
426   }
427 }