gst/autodetect/: Make the name of the child element be based on the name of the paren...
[platform/upstream/gstreamer.git] / gst / autodetect / gstautovideosink.c
1 /* GStreamer
2  * (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-autovideosink
22  * @see_also: autoaudiosink, ximagesink, xvimagesink, sdlvideosink
23  *
24  * <refsect2>
25  * <para>
26  * autovideosink is a video sink that automatically detects an appropriate
27  * video sink to use.  It does so by scanning the registry for all elements
28  * that have <quote>Sink</quote> and <quote>Audio</quote> in the class field
29  * of their element information, and also have a non-zero autoplugging rank.
30  * </para>
31  * <title>Example launch line</title>
32  * <para>
33  * <programlisting>
34  * gst-launch -v -m videotestsrc ! autovideosink
35  * </programlisting>
36  * </para>
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include <string.h>
45
46 #include "gstautovideosink.h"
47 #include "gstautodetect.h"
48
49 static GstStateChangeReturn
50 gst_auto_video_sink_change_state (GstElement * element,
51     GstStateChange transition);
52
53 GST_BOILERPLATE (GstAutoVideoSink, gst_auto_video_sink, GstBin, GST_TYPE_BIN);
54
55 static void
56 gst_auto_video_sink_base_init (gpointer klass)
57 {
58   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
59   const GstElementDetails gst_auto_video_sink_details =
60       GST_ELEMENT_DETAILS ("Auto video sink",
61       "Sink/Video",
62       "Wrapper video sink for automatically detected video sink",
63       "Ronald Bultje <rbultje@ronald.bitfreak.net>");
64   GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
65       GST_PAD_SINK,
66       GST_PAD_ALWAYS,
67       GST_STATIC_CAPS_ANY);
68
69   gst_element_class_add_pad_template (eklass,
70       gst_static_pad_template_get (&sink_template));
71   gst_element_class_set_details (eklass, &gst_auto_video_sink_details);
72 }
73
74 static void
75 gst_auto_video_sink_class_init (GstAutoVideoSinkClass * klass)
76 {
77   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
78
79   eklass->change_state = GST_DEBUG_FUNCPTR (gst_auto_video_sink_change_state);
80 }
81
82 /*
83  * Hack to make initial linking work; ideally, this'd work even when
84  * no target has been assigned to the ghostpad yet.
85  */
86
87 static void
88 gst_auto_video_sink_reset (GstAutoVideoSink * sink)
89 {
90   GstPad *targetpad;
91
92   /* fakesink placeholder */
93   if (sink->kid) {
94     gst_element_set_state (sink->kid, GST_STATE_NULL);
95     gst_bin_remove (GST_BIN (sink), sink->kid);
96   }
97   sink->kid = gst_element_factory_make ("fakesink", "tempsink");
98   gst_bin_add (GST_BIN (sink), sink->kid);
99
100   /* pad */
101   targetpad = gst_element_get_pad (sink->kid, "sink");
102   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
103   gst_object_unref (targetpad);
104 }
105
106 static void
107 gst_auto_video_sink_init (GstAutoVideoSink * sink,
108     GstAutoVideoSinkClass * g_class)
109 {
110   sink->pad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
111   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
112
113   gst_auto_video_sink_reset (sink);
114
115   GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
116 }
117
118 static gboolean
119 gst_auto_video_sink_factory_filter (GstPluginFeature * feature, gpointer data)
120 {
121   guint rank;
122   const gchar *klass;
123
124   /* we only care about element factories */
125   if (!GST_IS_ELEMENT_FACTORY (feature))
126     return FALSE;
127
128   /* video sinks */
129   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
130   if (!(strstr (klass, "Sink") && strstr (klass, "Video")))
131     return FALSE;
132
133   /* only select elements with autoplugging rank */
134   rank = gst_plugin_feature_get_rank (feature);
135   if (rank < GST_RANK_MARGINAL)
136     return FALSE;
137
138   return TRUE;
139 }
140
141 static gint
142 gst_auto_video_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
143 {
144   gint diff;
145
146   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
147   if (diff != 0)
148     return diff;
149   return strcmp (gst_plugin_feature_get_name (f2),
150       gst_plugin_feature_get_name (f1));
151 }
152
153 static GstElement *
154 gst_auto_video_sink_find_best (GstAutoVideoSink * sink)
155 {
156   GstElement *choice = NULL;
157   GList *list, *walk;
158   gchar *child_name = g_strdup_printf ("%s-actual-sink",
159       GST_OBJECT_NAME (sink));
160
161   list = gst_registry_feature_filter (gst_registry_get_default (),
162       (GstPluginFeatureFilter) gst_auto_video_sink_factory_filter, FALSE, sink);
163   list = g_list_sort (list, (GCompareFunc) gst_auto_video_sink_compare_ranks);
164
165   for (walk = list; walk != NULL; walk = walk->next) {
166     GstElementFactory *f = GST_ELEMENT_FACTORY (walk->data);
167     GstElement *el;
168
169     GST_DEBUG_OBJECT (sink, "Trying %s", GST_PLUGIN_FEATURE (f)->name);
170     if ((el = gst_element_factory_create (f, child_name))) {
171       GstStateChangeReturn ret;
172
173       GST_DEBUG_OBJECT (sink, "Changing state to READY");
174
175       ret = gst_element_set_state (el, GST_STATE_READY);
176       if (ret == GST_STATE_CHANGE_SUCCESS) {
177         GST_DEBUG_OBJECT (sink, "success");
178         choice = el;
179         goto done;
180       }
181
182       GST_WARNING_OBJECT (sink, "Couldn't set READY: %d", ret);
183       ret = gst_element_set_state (el, GST_STATE_NULL);
184       if (ret != GST_STATE_CHANGE_SUCCESS)
185         GST_WARNING_OBJECT (sink,
186             "Couldn't set element to NULL prior to disposal.");
187
188       gst_object_unref (el);
189     }
190   }
191
192 done:
193   g_free (child_name);
194   gst_plugin_feature_list_free (list);
195
196   return choice;
197 }
198
199 static gboolean
200 gst_auto_video_sink_detect (GstAutoVideoSink * sink)
201 {
202   GstElement *esink;
203   GstPad *targetpad;
204
205   if (sink->kid) {
206     gst_element_set_state (sink->kid, GST_STATE_NULL);
207     gst_bin_remove (GST_BIN (sink), sink->kid);
208     sink->kid = NULL;
209   }
210
211   /* find element */
212   GST_DEBUG_OBJECT (sink, "Creating new kid");
213   if (!(esink = gst_auto_video_sink_find_best (sink))) {
214     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
215         ("Failed to find a supported video sink"));
216     return FALSE;
217   }
218
219   sink->kid = esink;
220   gst_bin_add (GST_BIN (sink), esink);
221
222   /* attach ghost pad */
223   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
224   targetpad = gst_element_get_pad (sink->kid, "sink");
225   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
226   gst_object_unref (targetpad);
227   GST_DEBUG_OBJECT (sink, "done changing auto video sink");
228
229   return TRUE;
230 }
231
232 static GstStateChangeReturn
233 gst_auto_video_sink_change_state (GstElement * element,
234     GstStateChange transition)
235 {
236   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
237   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (element);
238
239   switch (transition) {
240     case GST_STATE_CHANGE_NULL_TO_READY:
241       if (!gst_auto_video_sink_detect (sink))
242         return GST_STATE_CHANGE_FAILURE;
243       break;
244     default:
245       break;
246   }
247
248   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
249
250   switch (transition) {
251     case GST_STATE_CHANGE_READY_TO_NULL:
252       gst_auto_video_sink_reset (sink);
253       break;
254     default:
255       break;
256   }
257
258   return ret;
259 }