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