c9bc0b5b063d9cfe8d9c1f7d2225bcf3a3b06519
[platform/upstream/gstreamer.git] / gst / autodetect / gstautovideosrc.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2006 Jan Schmidt <thaytan@noraisin.net>
4  * (c) 2008 Stefan Kost <ensonic@users.sf.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-autovideosrc
24  * @see_also: autoaudiosrc, v4l2src, v4lsrc
25  *
26  * autovideosrc is a video src that automatically detects an appropriate
27  * video source to use.  It does so by scanning the registry for all elements
28  * that have <quote>Source</quote> and <quote>Video</quote> in the class field
29  * of their element information, and also have a non-zero autoplugging rank.
30  *
31  * <refsect2>
32  * <title>Example launch line</title>
33  * |[
34  * gst-launch -v -m autovideosrc ! xvimagesink
35  * ]|
36  * </refsect2>
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <string.h>
44
45 #include "gstautovideosrc.h"
46 #include "gstautodetect.h"
47
48 /* Properties */
49 enum
50 {
51   PROP_0,
52   PROP_CAPS,
53 };
54
55 static GstStateChangeReturn
56 gst_auto_video_src_change_state (GstElement * element,
57     GstStateChange transition);
58 static void gst_auto_video_src_dispose (GstAutoVideoSrc * src);
59 static void gst_auto_video_src_clear_kid (GstAutoVideoSrc * src);
60
61 static void gst_auto_video_src_set_property (GObject * object, guint prop_id,
62     const GValue * value, GParamSpec * pspec);
63 static void gst_auto_video_src_get_property (GObject * object, guint prop_id,
64     GValue * value, GParamSpec * pspec);
65
66 #define gst_auto_video_src_parent_class parent_class
67 G_DEFINE_TYPE (GstAutoVideoSrc, gst_auto_video_src, GST_TYPE_BIN);
68
69 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS_ANY);
73
74 static void
75 gst_auto_video_src_class_init (GstAutoVideoSrcClass * klass)
76 {
77   GObjectClass *gobject_class;
78   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
79
80   gobject_class = G_OBJECT_CLASS (klass);
81   gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_video_src_dispose;
82   gobject_class->set_property = gst_auto_video_src_set_property;
83   gobject_class->get_property = gst_auto_video_src_get_property;
84
85   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_video_src_change_state);
86
87   /**
88    * GstAutoVideoSrc:filter-caps
89    *
90    * This property will filter out candidate sources that can handle the specified
91    * caps. By default only video sources that support raw rgb and yuv video
92    * are selected.
93    *
94    * This property can only be set before the element goes to the READY state.
95    *
96    * Since: 0.10.14
97    **/
98   g_object_class_install_property (gobject_class, PROP_CAPS,
99       g_param_spec_boxed ("filter-caps", "Filter caps",
100           "Filter src candidates using these caps.", GST_TYPE_CAPS,
101           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102
103   gst_element_class_add_pad_template (eklass,
104       gst_static_pad_template_get (&src_template));
105   gst_element_class_set_details_simple (eklass, "Auto video source",
106       "Source/Video",
107       "Wrapper video source for automatically detected video source",
108       "Jan Schmidt <thaytan@noraisin.net>, "
109       "Stefan Kost <ensonic@users.sf.net>");
110 }
111
112 static void
113 gst_auto_video_src_dispose (GstAutoVideoSrc * src)
114 {
115   gst_auto_video_src_clear_kid (src);
116
117   if (src->filter_caps)
118     gst_caps_unref (src->filter_caps);
119   src->filter_caps = NULL;
120
121   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) src);
122 }
123
124 static void
125 gst_auto_video_src_clear_kid (GstAutoVideoSrc * src)
126 {
127   if (src->kid) {
128     gst_element_set_state (src->kid, GST_STATE_NULL);
129     gst_bin_remove (GST_BIN (src), src->kid);
130     src->kid = NULL;
131     /* Don't loose SOURCE flag */
132     GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
133   }
134 }
135
136 /*
137  * Hack to make initial linking work; ideally, this'd work even when
138  * no target has been assigned to the ghostpad yet.
139  */
140
141 static void
142 gst_auto_video_src_reset (GstAutoVideoSrc * src)
143 {
144   GstPad *targetpad;
145
146   /* Remove any existing element */
147   gst_auto_video_src_clear_kid (src);
148
149   /* fakesrc placeholder */
150   src->kid = gst_element_factory_make ("fakesrc", "tempsrc");
151   gst_bin_add (GST_BIN (src), src->kid);
152
153   /* pad */
154   targetpad = gst_element_get_static_pad (src->kid, "src");
155   gst_ghost_pad_set_target (GST_GHOST_PAD (src->pad), targetpad);
156   gst_object_unref (targetpad);
157 }
158
159 static GstStaticCaps raw_caps =
160     GST_STATIC_CAPS ("video/x-raw-yuv; video/x-raw-rgb");
161
162 static void
163 gst_auto_video_src_init (GstAutoVideoSrc * src)
164 {
165   src->pad = gst_ghost_pad_new_no_target ("src", GST_PAD_SRC);
166   gst_element_add_pad (GST_ELEMENT (src), src->pad);
167
168   gst_auto_video_src_reset (src);
169
170   /* set the default raw video caps */
171   src->filter_caps = gst_static_caps_get (&raw_caps);
172
173   /* mark as source */
174   GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
175 }
176
177 static gboolean
178 gst_auto_video_src_factory_filter (GstPluginFeature * feature, gpointer data)
179 {
180   guint rank;
181   const gchar *klass;
182
183   /* we only care about element factories */
184   if (!GST_IS_ELEMENT_FACTORY (feature))
185     return FALSE;
186
187   /* video sources */
188   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
189   if (!(strstr (klass, "Source") && strstr (klass, "Video")))
190     return FALSE;
191
192   /* only select elements with autoplugging rank */
193   rank = gst_plugin_feature_get_rank (feature);
194   if (rank < GST_RANK_MARGINAL)
195     return FALSE;
196
197   return TRUE;
198 }
199
200 static gint
201 gst_auto_video_src_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
202 {
203   gint diff;
204
205   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
206   if (diff != 0)
207     return diff;
208   return strcmp (gst_plugin_feature_get_name (f2),
209       gst_plugin_feature_get_name (f1));
210 }
211
212 static GstElement *
213 gst_auto_video_src_create_element_with_pretty_name (GstAutoVideoSrc * src,
214     GstElementFactory * factory)
215 {
216   GstElement *element;
217   gchar *name, *marker;
218
219   marker = g_strdup (GST_OBJECT_NAME (factory));
220   if (g_str_has_suffix (marker, "src"))
221     marker[strlen (marker) - 4] = '\0';
222   if (g_str_has_prefix (marker, "gst"))
223     g_memmove (marker, marker + 3, strlen (marker + 3) + 1);
224   name = g_strdup_printf ("%s-actual-src-%s", GST_OBJECT_NAME (src), marker);
225   g_free (marker);
226
227   element = gst_element_factory_create (factory, name);
228   g_free (name);
229
230   return element;
231 }
232
233 static GstElement *
234 gst_auto_video_src_find_best (GstAutoVideoSrc * src)
235 {
236   GList *list, *item;
237   GstElement *choice = NULL;
238   GstMessage *message = NULL;
239   GSList *errors = NULL;
240   GstBus *bus = gst_bus_new ();
241   GstPad *el_pad = NULL;
242   GstCaps *el_caps = NULL;
243   gboolean no_match = TRUE;
244
245   list = gst_registry_feature_filter (gst_registry_get_default (),
246       (GstPluginFeatureFilter) gst_auto_video_src_factory_filter, FALSE, src);
247   list = g_list_sort (list, (GCompareFunc) gst_auto_video_src_compare_ranks);
248
249   GST_LOG_OBJECT (src, "Trying to find usable video devices ...");
250
251   for (item = list; item != NULL; item = item->next) {
252     GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
253     GstElement *el;
254
255     if ((el = gst_auto_video_src_create_element_with_pretty_name (src, f))) {
256       GstStateChangeReturn ret;
257
258       GST_DEBUG_OBJECT (src, "Testing %s", GST_OBJECT_NAME (f));
259
260       /* If AutoVideoSrc has been provided with filter caps,
261        * accept only sources that match with the filter caps */
262       if (src->filter_caps) {
263         el_pad = gst_element_get_static_pad (GST_ELEMENT (el), "src");
264         el_caps = gst_pad_get_caps (el_pad, NULL);
265         gst_object_unref (el_pad);
266         GST_DEBUG_OBJECT (src,
267             "Checking caps: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
268             src->filter_caps, el_caps);
269         no_match = !gst_caps_can_intersect (src->filter_caps, el_caps);
270         gst_caps_unref (el_caps);
271
272         if (no_match) {
273           GST_DEBUG_OBJECT (src, "Incompatible caps");
274           gst_object_unref (el);
275           continue;
276         } else {
277           GST_DEBUG_OBJECT (src, "Found compatible caps");
278         }
279       }
280
281       gst_element_set_bus (el, bus);
282       ret = gst_element_set_state (el, GST_STATE_READY);
283       if (ret == GST_STATE_CHANGE_SUCCESS) {
284         GST_DEBUG_OBJECT (src, "This worked!");
285         choice = el;
286         break;
287       }
288
289       /* collect all error messages */
290       while ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ERROR))) {
291         GST_DEBUG_OBJECT (src, "error message %" GST_PTR_FORMAT, message);
292         errors = g_slist_append (errors, message);
293       }
294
295       gst_element_set_state (el, GST_STATE_NULL);
296       gst_object_unref (el);
297     }
298   }
299
300   GST_DEBUG_OBJECT (src, "done trying");
301   if (!choice) {
302     if (errors) {
303       /* FIXME: we forward the first error for now; but later on it might make
304        * sense to actually analyse them */
305       gst_message_ref (GST_MESSAGE (errors->data));
306       GST_DEBUG_OBJECT (src, "reposting message %p", errors->data);
307       gst_element_post_message (GST_ELEMENT (src), GST_MESSAGE (errors->data));
308     } else {
309       /* send warning message to application and use a fakesrc */
310       GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
311           ("Failed to find a usable video source"));
312       choice = gst_element_factory_make ("fakesrc", "fake-video-src");
313       if (g_object_class_find_property (G_OBJECT_GET_CLASS (choice), "sync"))
314         g_object_set (choice, "sync", TRUE, NULL);
315       gst_element_set_state (choice, GST_STATE_READY);
316     }
317   }
318   gst_object_unref (bus);
319   gst_plugin_feature_list_free (list);
320   g_slist_foreach (errors, (GFunc) gst_mini_object_unref, NULL);
321   g_slist_free (errors);
322
323   return choice;
324 }
325
326 static gboolean
327 gst_auto_video_src_detect (GstAutoVideoSrc * src)
328 {
329   GstElement *esrc;
330   GstPad *targetpad;
331
332   gst_auto_video_src_clear_kid (src);
333
334   /* find element */
335   GST_DEBUG_OBJECT (src, "Creating new kid");
336   if (!(esrc = gst_auto_video_src_find_best (src)))
337     goto no_src;
338
339   src->kid = esrc;
340   gst_bin_add (GST_BIN (src), esrc);
341
342   /* attach ghost pad */
343   GST_DEBUG_OBJECT (src, "Re-assigning ghostpad");
344   targetpad = gst_element_get_static_pad (src->kid, "src");
345   if (!gst_ghost_pad_set_target (GST_GHOST_PAD (src->pad), targetpad))
346     goto target_failed;
347
348   gst_object_unref (targetpad);
349   GST_DEBUG_OBJECT (src, "done changing auto video source");
350
351   return TRUE;
352
353   /* ERRORS */
354 no_src:
355   {
356     GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
357         ("Failed to find a supported video source"));
358     return FALSE;
359   }
360 target_failed:
361   {
362     GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
363         ("Failed to set target pad"));
364     gst_object_unref (targetpad);
365     return FALSE;
366   }
367 }
368
369 static GstStateChangeReturn
370 gst_auto_video_src_change_state (GstElement * element,
371     GstStateChange transition)
372 {
373   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
374   GstAutoVideoSrc *src = GST_AUTO_VIDEO_SRC (element);
375
376   switch (transition) {
377     case GST_STATE_CHANGE_NULL_TO_READY:
378       if (!gst_auto_video_src_detect (src))
379         return GST_STATE_CHANGE_FAILURE;
380       break;
381     default:
382       break;
383   }
384
385   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
386
387   switch (transition) {
388     case GST_STATE_CHANGE_READY_TO_NULL:
389       gst_auto_video_src_reset (src);
390       break;
391     default:
392       break;
393   }
394
395   return ret;
396 }
397
398 static void
399 gst_auto_video_src_set_property (GObject * object, guint prop_id,
400     const GValue * value, GParamSpec * pspec)
401 {
402   GstAutoVideoSrc *src = GST_AUTO_VIDEO_SRC (object);
403
404   switch (prop_id) {
405     case PROP_CAPS:
406       if (src->filter_caps)
407         gst_caps_unref (src->filter_caps);
408       src->filter_caps = gst_caps_copy (gst_value_get_caps (value));
409       break;
410     default:
411       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412       break;
413   }
414 }
415
416 static void
417 gst_auto_video_src_get_property (GObject * object, guint prop_id,
418     GValue * value, GParamSpec * pspec)
419 {
420   GstAutoVideoSrc *src = GST_AUTO_VIDEO_SRC (object);
421
422   switch (prop_id) {
423     case PROP_CAPS:{
424       gst_value_set_caps (value, src->filter_caps);
425       break;
426     }
427     default:
428       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
429       break;
430   }
431 }