gst/autodetect/: These are sinks.
[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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include "gstautovideosink.h"
27 #include "gstautodetect.h"
28
29 static GstStateChangeReturn
30 gst_auto_video_sink_change_state (GstElement * element,
31     GstStateChange transition);
32
33 GST_BOILERPLATE (GstAutoVideoSink, gst_auto_video_sink, GstBin, GST_TYPE_BIN);
34
35 static void
36 gst_auto_video_sink_base_init (gpointer klass)
37 {
38   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
39   GstElementDetails gst_auto_video_sink_details = {
40     "Auto video sink",
41     "Sink/Video",
42     "Video sink embedding the Auto-settings for video output",
43     "Ronald Bultje <rbultje@ronald.bitfreak.net>"
44   };
45   GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
46       GST_PAD_SINK,
47       GST_PAD_ALWAYS,
48       GST_STATIC_CAPS_ANY);
49
50   gst_element_class_add_pad_template (eklass,
51       gst_static_pad_template_get (&sink_template));
52   gst_element_class_set_details (eklass, &gst_auto_video_sink_details);
53 }
54
55 static void
56 gst_auto_video_sink_class_init (GstAutoVideoSinkClass * klass)
57 {
58   GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
59
60   eklass->change_state = gst_auto_video_sink_change_state;
61 }
62
63 /*
64  * Hack to make initial linking work; ideally, this'd work even when
65  * no target has been assigned to the ghostpad yet.
66  */
67
68 static void
69 gst_auto_video_sink_reset (GstAutoVideoSink * sink)
70 {
71   GstPad *targetpad;
72
73   /* fakesink placeholder */
74   if (sink->kid) {
75     gst_bin_remove (GST_BIN (sink), sink->kid);
76   }
77   sink->kid = gst_element_factory_make ("fakesink", "tempsink");
78   gst_bin_add (GST_BIN (sink), sink->kid);
79
80   /* pad */
81   targetpad = gst_element_get_pad (sink->kid, "sink");
82   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
83   gst_object_unref (targetpad);
84 }
85
86 static void
87 gst_auto_video_sink_init (GstAutoVideoSink * sink,
88     GstAutoVideoSinkClass * g_class)
89 {
90   sink->pad = gst_ghost_pad_new_notarget ("sink", GST_PAD_SINK);
91   gst_element_add_pad (GST_ELEMENT (sink), sink->pad);
92
93   gst_auto_video_sink_reset (sink);
94
95   GST_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
96 }
97
98 static gboolean
99 gst_auto_video_sink_factory_filter (GstPluginFeature * feature, gpointer data)
100 {
101   guint rank;
102   const gchar *klass;
103
104   /* we only care about element factories */
105   if (!GST_IS_ELEMENT_FACTORY (feature))
106     return FALSE;
107
108   /* video sinks */
109   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
110   if (strcmp (klass, "Sink/Video") != 0)
111     return FALSE;
112
113   /* only select elements with autoplugging rank */
114   rank = gst_plugin_feature_get_rank (feature);
115   if (rank < GST_RANK_MARGINAL)
116     return FALSE;
117
118   return TRUE;
119 }
120
121 static gint
122 gst_auto_video_sink_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
123 {
124   gint diff;
125
126   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
127   if (diff != 0)
128     return diff;
129   return strcmp (gst_plugin_feature_get_name (f2),
130       gst_plugin_feature_get_name (f1));
131 }
132
133 static GstElement *
134 gst_auto_video_sink_find_best (GstAutoVideoSink * sink)
135 {
136   GList *list;
137
138   list = gst_registry_feature_filter (gst_registry_get_default (),
139       (GstPluginFeatureFilter) gst_auto_video_sink_factory_filter, FALSE, sink);
140   list = g_list_sort (list, (GCompareFunc) gst_auto_video_sink_compare_ranks);
141
142   for (; list != NULL; list = list->next) {
143     GstElementFactory *f = GST_ELEMENT_FACTORY (list->data);
144     GstElement *el;
145
146     GST_DEBUG_OBJECT (sink, "Trying %s", GST_PLUGIN_FEATURE (f)->name);
147     if ((el = gst_element_factory_create (f, "actual-sink"))) {
148       GST_DEBUG_OBJECT (sink, "Changing state to READY");
149       if (gst_element_set_state (el,
150               GST_STATE_READY) == GST_STATE_CHANGE_SUCCESS) {
151         GST_DEBUG_OBJECT (sink, "success");
152         return el;
153       }
154
155       gst_object_unref (GST_OBJECT (el));
156     }
157   }
158
159   return NULL;
160 }
161
162 static gboolean
163 gst_auto_video_sink_detect (GstAutoVideoSink * sink)
164 {
165   GstElement *esink;
166   GstPad *targetpad;
167
168   if (sink->kid) {
169     gst_bin_remove (GST_BIN (sink), sink->kid);
170     sink->kid = NULL;
171   }
172
173   /* find element */
174   GST_DEBUG_OBJECT (sink, "Creating new kid");
175   if (!(esink = gst_auto_video_sink_find_best (sink))) {
176     GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
177         ("Failed to find a supported video sink"));
178     return FALSE;
179   }
180   sink->kid = esink;
181   gst_bin_add (GST_BIN (sink), esink);
182
183   /* attach ghost pad */
184   GST_DEBUG_OBJECT (sink, "Re-assigning ghostpad");
185   targetpad = gst_element_get_pad (sink->kid, "sink");
186   gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
187   gst_object_unref (targetpad);
188   GST_DEBUG_OBJECT (sink, "done changing auto video sink");
189
190   return TRUE;
191 }
192
193 static GstStateChangeReturn
194 gst_auto_video_sink_change_state (GstElement * element,
195     GstStateChange transition)
196 {
197   GstAutoVideoSink *sink = GST_AUTO_VIDEO_SINK (element);
198
199   GST_DEBUG_OBJECT (element, "Change state 0x%x", transition);
200
201   switch (transition) {
202     case GST_STATE_CHANGE_NULL_TO_READY:
203       if (!gst_auto_video_sink_detect (sink))
204         return GST_STATE_CHANGE_FAILURE;
205       break;
206     case GST_STATE_CHANGE_READY_TO_NULL:
207       gst_auto_video_sink_reset (sink);
208       break;
209     default:
210       break;
211   }
212
213   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
214 }