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