3 * Copyright (C) 2008 Nokia Corporation <multimedia@maemo.org>
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.
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.
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.
22 * SECTION:camerabingeneral
23 * @short_description: helper functions for #GstCameraBin2 and it's modules
25 * Common helper functions for #GstCameraBin2, #GstCameraBin2Image and
26 * #GstCameraBin2Video.
36 #include <gst/basecamerabinsrc/gstbasecamerasrc.h>
37 #include <gst/gst-i18n-plugin.h>
39 #include "camerabingeneral.h"
42 * gst_camerabin_add_element:
43 * @bin: add an element to this bin
44 * @new_elem: new element to be added
46 * Adds given element to given @bin. Looks for an unconnected src pad
47 * from the @bin and links the element to it. Raises an error if adding
48 * or linking failed. Unrefs the element in the case of an error.
50 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
53 gst_camerabin_add_element (GstBin * bin, GstElement * new_elem)
55 return gst_camerabin_add_element_full (bin, NULL, new_elem, NULL);
59 * gst_camerabin_add_element_full:
60 * @bin: add an element to this bin
61 * @srcpad: src pad name, or NULL for any
62 * @new_elem: new element to be added
63 * @dstpad: dst pad name, or NULL for any
65 * Adds given element to given @bin. Looks for an unconnected src pad
66 * (with name @srcpad, if specified) from the @bin and links the element
67 * to it. Raises an error if adding or linking failed. Unrefs the element
68 * in the case of an error.
70 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
73 gst_camerabin_add_element_full (GstBin * bin, const gchar * srcpad,
74 GstElement * new_elem, const gchar * dstpad)
78 g_return_val_if_fail (bin, FALSE);
79 g_return_val_if_fail (new_elem, FALSE);
81 ret = gst_camerabin_try_add_element (bin, srcpad, new_elem, dstpad);
84 gchar *elem_name = gst_element_get_name (new_elem);
85 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION, (NULL),
86 ("linking %s failed", elem_name));
88 gst_object_unref (new_elem);
95 * gst_camerabin_try_add_element:
96 * @bin: tries adding an element to this bin
97 * @srcpad: src pad name, or NULL for any
98 * @new_elem: new element to be added
99 * @dstpad: dst pad name, or NULL for any
101 * Adds given element to given @bin. Looks for an unconnected src pad
102 * (with name @srcpad, if specified) from the @bin and links the element to
105 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
108 gst_camerabin_try_add_element (GstBin * bin, const gchar * srcpad,
109 GstElement * new_elem, const gchar * dstpad)
112 GstElement *bin_elem;
115 g_return_val_if_fail (bin, FALSE);
116 g_return_val_if_fail (new_elem, FALSE);
118 /* Get pads for linking */
119 bin_pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC);
121 gst_bin_add (GST_BIN (bin), new_elem);
122 /* Link, if unconnected pad was found, otherwise just add it to bin */
124 GST_DEBUG_OBJECT (bin, "linking %s to %s:%s", GST_OBJECT_NAME (new_elem),
125 GST_DEBUG_PAD_NAME (bin_pad));
126 bin_elem = gst_pad_get_parent_element (bin_pad);
127 gst_object_unref (bin_pad);
128 if (!gst_element_link_pads_full (bin_elem, srcpad, new_elem, dstpad,
129 GST_PAD_LINK_CHECK_CAPS)) {
130 gst_object_ref (new_elem);
131 gst_bin_remove (bin, new_elem);
134 gst_object_unref (bin_elem);
136 GST_INFO_OBJECT (bin, "no unlinked source pad in bin");
143 * gst_camerabin_create_and_add_element:
144 * @bin: tries adding an element to this bin
145 * @elem_name: name of the element to be created
146 * @instance_name: name of the instance of the element to be created
148 * Creates an element according to given name and
149 * adds it to given @bin. Looks for an unconnected src pad
150 * from the @bin and links the element to it.
152 * Returns: pointer to the new element if successful, NULL otherwise.
155 gst_camerabin_create_and_add_element (GstBin * bin, const gchar * elem_name,
156 const gchar * instance_name)
158 GstElement *new_elem;
160 g_return_val_if_fail (bin, FALSE);
161 g_return_val_if_fail (elem_name, FALSE);
163 new_elem = gst_element_factory_make (elem_name, instance_name);
165 GST_ELEMENT_ERROR (bin, CORE, MISSING_PLUGIN,
166 (_("Missing element '%s' - check your GStreamer installation."),
168 } else if (!gst_camerabin_add_element (bin, new_elem)) {
175 /* try to change the state of an element. This function returns the element when
176 * the state change could be performed. When this function returns NULL an error
177 * occured and the element is unreffed if @unref is TRUE. */
179 try_element (GstElement * bin, GstElement * element, gboolean unref)
181 GstStateChangeReturn ret;
184 ret = gst_element_set_state (element, GST_STATE_READY);
185 if (ret == GST_STATE_CHANGE_FAILURE) {
186 GST_DEBUG_OBJECT (bin, "failed state change..");
187 gst_element_set_state (element, GST_STATE_NULL);
189 gst_object_unref (element);
197 gst_camerabin_setup_default_element (GstBin * bin, GstElement * user_elem,
198 const gchar * auto_elem_name, const gchar * default_elem_name,
199 const gchar * instance_name)
204 GST_DEBUG_OBJECT (bin, "trying configured element");
205 elem = try_element (GST_ELEMENT_CAST (bin), user_elem, FALSE);
207 /* only try fallback if no specific sink was chosen */
208 GST_DEBUG_OBJECT (bin, "trying %s", auto_elem_name);
209 elem = gst_element_factory_make (auto_elem_name, instance_name);
210 elem = try_element (GST_ELEMENT_CAST (bin), elem, TRUE);
212 /* if default sink from config.h is different then try it too */
213 if (strcmp (default_elem_name, auto_elem_name)) {
214 GST_DEBUG_OBJECT (bin, "trying %s", default_elem_name);
215 elem = gst_element_factory_make (default_elem_name, instance_name);
216 elem = try_element (GST_ELEMENT_CAST (bin), elem, TRUE);
224 * gst_camerabin_remove_elements_from_bin:
225 * @bin: removes all elements from this bin
227 * Removes all elements from this @bin.
230 gst_camerabin_remove_elements_from_bin (GstBin * bin)
232 GstIterator *iter = NULL;
233 gpointer data = NULL;
234 GstElement *elem = NULL;
235 gboolean done = FALSE;
237 iter = gst_bin_iterate_elements (bin);
239 switch (gst_iterator_next (iter, &data)) {
240 case GST_ITERATOR_OK:
241 elem = GST_ELEMENT (data);
242 gst_bin_remove (bin, elem);
243 gst_element_set_state (GST_ELEMENT (elem), GST_STATE_NULL);
244 /* Iterator increased the element refcount, so unref */
245 gst_object_unref (elem);
247 case GST_ITERATOR_RESYNC:
248 gst_iterator_resync (iter);
250 case GST_ITERATOR_ERROR:
251 GST_WARNING_OBJECT (bin, "error in iterating elements");
254 case GST_ITERATOR_DONE:
259 gst_iterator_free (iter);