1 /* GStreamer
2 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 /**
20 * SECTION:element-gstviewfinderbin
21 * @title: gstviewfinderbin
22 *
23 * The gstviewfinderbin element is a displaying element for camerabin2.
24 *
25 * ## Example launch line
26 * |[
27 * gst-launch-1.0 -v videotestsrc ! viewfinderbin
28 * ]|
29 * Feeds the viewfinderbin with video test data.
30 *
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "gstviewfinderbin.h"
38 #include "camerabingeneral.h"
39 #include <gst/pbutils/pbutils.h>
40
41 #include <gst/gst-i18n-plugin.h>
42
43 GST_DEBUG_CATEGORY_STATIC (gst_viewfinder_bin_debug);
44 #define GST_CAT_DEFAULT gst_viewfinder_bin_debug
45
46 enum
47 {
48 PROP_0,
49 PROP_VIDEO_SINK,
50 PROP_DISABLE_CONVERTERS
51 };
52
53 #define DEFAULT_DISABLE_CONVERTERS FALSE
54
55 /* pad templates */
56
57 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_PAD_SINK,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS ("video/x-raw(ANY)")
61 );
62
63 /* class initialization */
64 #define gst_viewfinder_bin_parent_class parent_class
65 G_DEFINE_TYPE (GstViewfinderBin, gst_viewfinder_bin, GST_TYPE_BIN);
66
67 static void gst_viewfinder_bin_set_property (GObject * object, guint prop_id,
68 const GValue * value, GParamSpec * spec);
69 static void gst_viewfinder_bin_get_property (GObject * object, guint prop_id,
70 GValue * value, GParamSpec * spec);
71
72 static void
73 gst_viewfinder_bin_set_video_sink (GstViewfinderBin * vfbin, GstElement * sink);
74
75
76 /* Element class functions */
77 static GstStateChangeReturn
78 gst_viewfinder_bin_change_state (GstElement * element, GstStateChange trans);
79
80 static void
gst_viewfinder_bin_dispose(GObject * object)81 gst_viewfinder_bin_dispose (GObject * object)
82 {
83 GstViewfinderBin *viewfinderbin = GST_VIEWFINDER_BIN_CAST (object);
84
85 if (viewfinderbin->user_video_sink) {
86 gst_object_unref (viewfinderbin->user_video_sink);
87 viewfinderbin->user_video_sink = NULL;
88 }
89
90 if (viewfinderbin->video_sink) {
91 gst_object_unref (viewfinderbin->video_sink);
92 viewfinderbin->video_sink = NULL;
93 }
94
95 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) viewfinderbin);
96 }
97
98 static void
gst_viewfinder_bin_class_init(GstViewfinderBinClass * klass)99 gst_viewfinder_bin_class_init (GstViewfinderBinClass * klass)
100 {
101 GObjectClass *gobject_klass;
102 GstElementClass *element_class;
103
104 gobject_klass = (GObjectClass *) klass;
105 element_class = GST_ELEMENT_CLASS (klass);
106
107 element_class->change_state =
108 GST_DEBUG_FUNCPTR (gst_viewfinder_bin_change_state);
109
110 gobject_klass->dispose = gst_viewfinder_bin_dispose;
111 gobject_klass->set_property = gst_viewfinder_bin_set_property;
112 gobject_klass->get_property = gst_viewfinder_bin_get_property;
113
114 g_object_class_install_property (gobject_klass, PROP_VIDEO_SINK,
115 g_param_spec_object ("video-sink", "Video Sink",
116 "the video output element to use (NULL = default)",
117 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118
119 g_object_class_install_property (gobject_klass, PROP_DISABLE_CONVERTERS,
120 g_param_spec_boolean ("disable-converters", "Disable conversion elements",
121 "If video converters should be disabled (must be set on NULL)",
122 DEFAULT_DISABLE_CONVERTERS,
123 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124
125 gst_element_class_add_static_pad_template (element_class, &sink_template);
126
127 gst_element_class_set_static_metadata (element_class, "Viewfinder Bin",
128 "Sink/Video", "Viewfinder Bin used in camerabin2",
129 "Thiago Santos <thiago.sousa.santos@collabora.com>");
130 }
131
132 static void
gst_viewfinder_bin_init(GstViewfinderBin * viewfinderbin)133 gst_viewfinder_bin_init (GstViewfinderBin * viewfinderbin)
134 {
135 GstPadTemplate *templ = gst_static_pad_template_get (&sink_template);
136 viewfinderbin->ghostpad = gst_ghost_pad_new_no_target_from_template ("sink",
137 templ);
138 gst_object_unref (templ);
139 gst_element_add_pad (GST_ELEMENT_CAST (viewfinderbin),
140 viewfinderbin->ghostpad);
141
142 viewfinderbin->disable_converters = DEFAULT_DISABLE_CONVERTERS;
143 }
144
145 static gboolean
gst_viewfinder_bin_create_elements(GstViewfinderBin * vfbin)146 gst_viewfinder_bin_create_elements (GstViewfinderBin * vfbin)
147 {
148 GstElement *csp = NULL;
149 GstElement *videoscale = NULL;
150 GstPad *firstpad = NULL;
151 const gchar *missing_element_name;
152 gboolean newsink = FALSE;
153 gboolean updated_converters = FALSE;
154
155 GST_DEBUG_OBJECT (vfbin, "Creating internal elements");
156
157 /* First check if we need to add/replace the internal sink */
158 if (vfbin->video_sink) {
159 if (vfbin->user_video_sink && vfbin->video_sink != vfbin->user_video_sink) {
160 gst_bin_remove (GST_BIN_CAST (vfbin), vfbin->video_sink);
161 gst_object_unref (vfbin->video_sink);
162 vfbin->video_sink = NULL;
163 }
164 }
165
166 if (!vfbin->video_sink) {
167 if (vfbin->user_video_sink)
168 vfbin->video_sink = gst_object_ref (vfbin->user_video_sink);
169 else {
170 vfbin->video_sink = gst_element_factory_make ("autovideosink",
171 "vfbin-sink");
172 if (!vfbin->video_sink) {
173 missing_element_name = "autovideosink";
174 goto missing_element;
175 }
176 }
177
178 gst_bin_add (GST_BIN_CAST (vfbin), gst_object_ref (vfbin->video_sink));
179 newsink = TRUE;
180 }
181
182 /* check if we want add/remove the conversion elements */
183 if (vfbin->elements_created && vfbin->disable_converters) {
184 /* remove the elements, user doesn't want them */
185
186 gst_ghost_pad_set_target (GST_GHOST_PAD (vfbin->ghostpad), NULL);
187 csp = gst_bin_get_by_name (GST_BIN_CAST (vfbin), "vfbin-csp");
188 videoscale = gst_bin_get_by_name (GST_BIN_CAST (vfbin), "vfbin-videoscale");
189
190 gst_bin_remove (GST_BIN_CAST (vfbin), csp);
191 gst_bin_remove (GST_BIN_CAST (vfbin), videoscale);
192
193 gst_object_unref (csp);
194 gst_object_unref (videoscale);
195
196 updated_converters = TRUE;
197 } else if (!vfbin->elements_created && !vfbin->disable_converters) {
198 gst_ghost_pad_set_target (GST_GHOST_PAD (vfbin->ghostpad), NULL);
199
200 /* add the elements, user wants them */
201 csp = gst_element_factory_make ("videoconvert", "vfbin-csp");
202 if (!csp) {
203 missing_element_name = "videoconvert";
204 goto missing_element;
205 }
206 gst_bin_add (GST_BIN_CAST (vfbin), csp);
207
208 videoscale = gst_element_factory_make ("videoscale", "vfbin->videoscale");
209 if (!videoscale) {
210 missing_element_name = "videoscale";
211 goto missing_element;
212 }
213 gst_bin_add (GST_BIN_CAST (vfbin), videoscale);
214
215 gst_element_link_pads_full (csp, "src", videoscale, "sink",
216 GST_PAD_LINK_CHECK_NOTHING);
217
218 vfbin->elements_created = TRUE;
219 GST_DEBUG_OBJECT (vfbin, "Elements succesfully created and linked");
220
221 updated_converters = TRUE;
222 }
223 /* otherwise, just leave it as is */
224
225 /* if sink was replaced -> link it to the internal converters */
226 if (newsink && !vfbin->disable_converters) {
227 gboolean unref = FALSE;
228 if (!videoscale) {
229 videoscale = gst_bin_get_by_name (GST_BIN_CAST (vfbin),
230 "vfbin-videscale");
231 unref = TRUE;
232 }
233
234 if (!gst_element_link_pads_full (videoscale, "src", vfbin->video_sink,
235 "sink", GST_PAD_LINK_CHECK_CAPS)) {
236 GST_ELEMENT_ERROR (vfbin, CORE, NEGOTIATION, (NULL),
237 ("linking videoscale and viewfindersink failed"));
238 }
239
240 if (unref)
241 gst_object_unref (videoscale);
242 videoscale = NULL;
243 }
244
245 /* Check if we need a new ghostpad target */
246 if (updated_converters || (newsink && vfbin->disable_converters)) {
247 if (vfbin->disable_converters) {
248 firstpad = gst_element_get_static_pad (vfbin->video_sink, "sink");
249 } else {
250 /* csp should always exist at this point */
251 firstpad = gst_element_get_static_pad (csp, "sink");
252 }
253 }
254
255 /* need to change the ghostpad target if firstpad is set */
256 if (firstpad) {
257 if (!gst_ghost_pad_set_target (GST_GHOST_PAD (vfbin->ghostpad), firstpad))
258 goto error;
259 gst_object_unref (firstpad);
260 firstpad = NULL;
261 }
262
263 return TRUE;
264
265 missing_element:
266 gst_element_post_message (GST_ELEMENT_CAST (vfbin),
267 gst_missing_element_message_new (GST_ELEMENT_CAST (vfbin),
268 missing_element_name));
269 GST_ELEMENT_ERROR (vfbin, CORE, MISSING_PLUGIN,
270 (_("Missing element '%s' - check your GStreamer installation."),
271 missing_element_name), (NULL));
272 goto error;
273
274 error:
275 GST_WARNING_OBJECT (vfbin, "Creating internal elements failed");
276 if (firstpad)
277 gst_object_unref (firstpad);
278 return FALSE;
279 }
280
281 static GstStateChangeReturn
gst_viewfinder_bin_change_state(GstElement * element,GstStateChange trans)282 gst_viewfinder_bin_change_state (GstElement * element, GstStateChange trans)
283 {
284 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
285 GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (element);
286
287 switch (trans) {
288 case GST_STATE_CHANGE_NULL_TO_READY:
289 if (!gst_viewfinder_bin_create_elements (vfbin)) {
290 return GST_STATE_CHANGE_FAILURE;
291 }
292 break;
293 default:
294 break;
295 }
296
297 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
298
299 switch (trans) {
300 case GST_STATE_CHANGE_READY_TO_NULL:
301 break;
302 default:
303 break;
304 }
305
306 return ret;
307 }
308
309 static void
gst_viewfinder_bin_set_video_sink(GstViewfinderBin * vfbin,GstElement * sink)310 gst_viewfinder_bin_set_video_sink (GstViewfinderBin * vfbin, GstElement * sink)
311 {
312 GST_INFO_OBJECT (vfbin, "Setting video sink to %" GST_PTR_FORMAT, sink);
313
314 if (vfbin->user_video_sink != sink) {
315 if (vfbin->user_video_sink) {
316 gst_object_unref (vfbin->user_video_sink);
317 }
318 vfbin->user_video_sink = sink;
319 if (sink)
320 gst_object_ref (sink);
321 }
322 }
323
324 static void
gst_viewfinder_bin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)325 gst_viewfinder_bin_set_property (GObject * object, guint prop_id,
326 const GValue * value, GParamSpec * pspec)
327 {
328 GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (object);
329
330 switch (prop_id) {
331 case PROP_VIDEO_SINK:
332 gst_viewfinder_bin_set_video_sink (vfbin, g_value_get_object (value));
333 break;
334 case PROP_DISABLE_CONVERTERS:
335 vfbin->disable_converters = g_value_get_boolean (value);
336 break;
337 default:
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
339 break;
340 }
341 }
342
343 static void
gst_viewfinder_bin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)344 gst_viewfinder_bin_get_property (GObject * object, guint prop_id,
345 GValue * value, GParamSpec * pspec)
346 {
347 GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (object);
348
349 switch (prop_id) {
350 case PROP_VIDEO_SINK:
351 g_value_set_object (value, vfbin->video_sink);
352 break;
353 case PROP_DISABLE_CONVERTERS:
354 g_value_set_boolean (value, vfbin->disable_converters);
355 break;
356 default:
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358 break;
359 }
360 }
361
362 gboolean
gst_viewfinder_bin_plugin_init(GstPlugin * plugin)363 gst_viewfinder_bin_plugin_init (GstPlugin * plugin)
364 {
365 GST_DEBUG_CATEGORY_INIT (gst_viewfinder_bin_debug, "viewfinderbin", 0,
366 "ViewFinderBin");
367 return gst_element_register (plugin, "viewfinderbin", GST_RANK_NONE,
368 gst_viewfinder_bin_get_type ());
369 }
370