2 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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.
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.
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.
20 * SECTION:element-gstviewfinderbin
21 * @title: gstviewfinderbin
23 * The gstviewfinderbin element is a displaying element for camerabin2.
25 * ## Example launch line
27 * gst-launch-1.0 -v videotestsrc ! viewfinderbin
29 * Feeds the viewfinderbin with video test data.
37 #include "gstviewfinderbin.h"
38 #include "camerabingeneral.h"
39 #include <gst/pbutils/pbutils.h>
41 #include <gst/gst-i18n-plugin.h>
43 GST_DEBUG_CATEGORY_STATIC (gst_viewfinder_bin_debug);
44 #define GST_CAT_DEFAULT gst_viewfinder_bin_debug
50 PROP_DISABLE_CONVERTERS
53 #define DEFAULT_DISABLE_CONVERTERS FALSE
57 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("video/x-raw(ANY)")
63 /* class initialization */
64 #define gst_viewfinder_bin_parent_class parent_class
65 G_DEFINE_TYPE (GstViewfinderBin, gst_viewfinder_bin, GST_TYPE_BIN);
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);
73 gst_viewfinder_bin_set_video_sink (GstViewfinderBin * vfbin, GstElement * sink);
76 /* Element class functions */
77 static GstStateChangeReturn
78 gst_viewfinder_bin_change_state (GstElement * element, GstStateChange trans);
81 gst_viewfinder_bin_dispose (GObject * object)
83 GstViewfinderBin *viewfinderbin = GST_VIEWFINDER_BIN_CAST (object);
85 if (viewfinderbin->user_video_sink) {
86 gst_object_unref (viewfinderbin->user_video_sink);
87 viewfinderbin->user_video_sink = NULL;
90 if (viewfinderbin->video_sink) {
91 gst_object_unref (viewfinderbin->video_sink);
92 viewfinderbin->video_sink = NULL;
95 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) viewfinderbin);
99 gst_viewfinder_bin_class_init (GstViewfinderBinClass * klass)
101 GObjectClass *gobject_klass;
102 GstElementClass *element_class;
104 gobject_klass = (GObjectClass *) klass;
105 element_class = GST_ELEMENT_CLASS (klass);
107 element_class->change_state =
108 GST_DEBUG_FUNCPTR (gst_viewfinder_bin_change_state);
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;
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));
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));
125 gst_element_class_add_static_pad_template (element_class, &sink_template);
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>");
133 gst_viewfinder_bin_init (GstViewfinderBin * viewfinderbin)
135 GstPadTemplate *templ = gst_static_pad_template_get (&sink_template);
136 viewfinderbin->ghostpad = gst_ghost_pad_new_no_target_from_template ("sink",
138 gst_object_unref (templ);
139 gst_element_add_pad (GST_ELEMENT_CAST (viewfinderbin),
140 viewfinderbin->ghostpad);
142 viewfinderbin->disable_converters = DEFAULT_DISABLE_CONVERTERS;
146 gst_viewfinder_bin_create_elements (GstViewfinderBin * vfbin)
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;
155 GST_DEBUG_OBJECT (vfbin, "Creating internal elements");
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;
166 if (!vfbin->video_sink) {
167 if (vfbin->user_video_sink)
168 vfbin->video_sink = gst_object_ref (vfbin->user_video_sink);
170 vfbin->video_sink = gst_element_factory_make ("autovideosink",
172 if (!vfbin->video_sink) {
173 missing_element_name = "autovideosink";
174 goto missing_element;
178 gst_bin_add (GST_BIN_CAST (vfbin), gst_object_ref (vfbin->video_sink));
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 */
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");
190 gst_bin_remove (GST_BIN_CAST (vfbin), csp);
191 gst_bin_remove (GST_BIN_CAST (vfbin), videoscale);
193 gst_object_unref (csp);
194 gst_object_unref (videoscale);
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);
200 /* add the elements, user wants them */
201 csp = gst_element_factory_make ("videoconvert", "vfbin-csp");
203 missing_element_name = "videoconvert";
204 goto missing_element;
206 gst_bin_add (GST_BIN_CAST (vfbin), csp);
208 videoscale = gst_element_factory_make ("videoscale", "vfbin-videoscale");
210 missing_element_name = "videoscale";
211 goto missing_element;
213 gst_bin_add (GST_BIN_CAST (vfbin), videoscale);
215 gst_element_link_pads_full (csp, "src", videoscale, "sink",
216 GST_PAD_LINK_CHECK_NOTHING);
218 vfbin->elements_created = TRUE;
219 GST_DEBUG_OBJECT (vfbin, "Elements successfully created and linked");
221 updated_converters = TRUE;
223 /* otherwise, just leave it as is */
225 /* if sink was replaced -> link it to the internal converters */
226 if (newsink && !vfbin->disable_converters) {
227 gboolean unref = FALSE;
229 videoscale = gst_bin_get_by_name (GST_BIN_CAST (vfbin),
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"));
241 gst_object_unref (videoscale);
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");
250 /* csp should always exist at this point */
251 firstpad = gst_element_get_static_pad (csp, "sink");
255 /* need to change the ghostpad target if firstpad is set */
257 if (!gst_ghost_pad_set_target (GST_GHOST_PAD (vfbin->ghostpad), firstpad))
259 gst_object_unref (firstpad);
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));
275 GST_WARNING_OBJECT (vfbin, "Creating internal elements failed");
277 gst_object_unref (firstpad);
281 static GstStateChangeReturn
282 gst_viewfinder_bin_change_state (GstElement * element, GstStateChange trans)
284 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
285 GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (element);
288 case GST_STATE_CHANGE_NULL_TO_READY:
289 if (!gst_viewfinder_bin_create_elements (vfbin)) {
290 return GST_STATE_CHANGE_FAILURE;
297 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
300 case GST_STATE_CHANGE_READY_TO_NULL:
310 gst_viewfinder_bin_set_video_sink (GstViewfinderBin * vfbin, GstElement * sink)
312 GST_INFO_OBJECT (vfbin, "Setting video sink to %" GST_PTR_FORMAT, sink);
314 if (vfbin->user_video_sink != sink) {
315 if (vfbin->user_video_sink) {
316 gst_object_unref (vfbin->user_video_sink);
318 vfbin->user_video_sink = sink;
320 gst_object_ref (sink);
325 gst_viewfinder_bin_set_property (GObject * object, guint prop_id,
326 const GValue * value, GParamSpec * pspec)
328 GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (object);
331 case PROP_VIDEO_SINK:
332 gst_viewfinder_bin_set_video_sink (vfbin, g_value_get_object (value));
334 case PROP_DISABLE_CONVERTERS:
335 vfbin->disable_converters = g_value_get_boolean (value);
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 gst_viewfinder_bin_get_property (GObject * object, guint prop_id,
345 GValue * value, GParamSpec * pspec)
347 GstViewfinderBin *vfbin = GST_VIEWFINDER_BIN_CAST (object);
350 case PROP_VIDEO_SINK:
351 g_value_set_object (value, vfbin->video_sink);
353 case PROP_DISABLE_CONVERTERS:
354 g_value_set_boolean (value, vfbin->disable_converters);
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gst_viewfinder_bin_plugin_init (GstPlugin * plugin)
365 GST_DEBUG_CATEGORY_INIT (gst_viewfinder_bin_debug, "viewfinderbin", 0,
367 return gst_element_register (plugin, "viewfinderbin", GST_RANK_NONE,
368 gst_viewfinder_bin_get_type ());