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