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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:camerabingeneral
23 * @title: GstCameraBin2
24 * @short_description: helper functions for #GstCameraBin2 and it's modules
26 * Common helper functions for #GstCameraBin2, #GstCameraBin2Image and
27 * #GstCameraBin2Video.
37 #include <gst/basecamerabinsrc/gstbasecamerasrc.h>
38 #include <gst/gst-i18n-plugin.h>
40 #include "camerabingeneral.h"
43 * gst_camerabin_add_element:
44 * @bin: add an element to this bin
45 * @new_elem: new element to be added
47 * Adds given element to given @bin. Looks for an unconnected src pad
48 * from the @bin and links the element to it. Raises an error if adding
49 * or linking failed. Unrefs the element in the case of an error.
51 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
54 gst_camerabin_add_element (GstBin * bin, GstElement * new_elem)
56 return gst_camerabin_add_element_full (bin, NULL, new_elem, NULL);
60 * gst_camerabin_add_element_full:
61 * @bin: add an element to this bin
62 * @srcpad: src pad name, or NULL for any
63 * @new_elem: new element to be added
64 * @dstpad: dst pad name, or NULL for any
66 * Adds given element to given @bin. Looks for an unconnected src pad
67 * (with name @srcpad, if specified) from the @bin and links the element
68 * to it. Raises an error if adding or linking failed. Unrefs the element
69 * in the case of an error.
71 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
74 gst_camerabin_add_element_full (GstBin * bin, const gchar * srcpad,
75 GstElement * new_elem, const gchar * dstpad)
79 g_return_val_if_fail (bin, FALSE);
80 g_return_val_if_fail (new_elem, FALSE);
82 ret = gst_camerabin_try_add_element (bin, srcpad, new_elem, dstpad);
85 gchar *elem_name = gst_element_get_name (new_elem);
86 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION, (NULL),
87 ("linking %s failed", elem_name));
89 gst_object_unref (new_elem);
96 * gst_camerabin_try_add_element:
97 * @bin: tries adding an element to this bin
98 * @srcpad: src pad name, or NULL for any
99 * @new_elem: new element to be added
100 * @dstpad: dst pad name, or NULL for any
102 * Adds given element to given @bin. Looks for an unconnected src pad
103 * (with name @srcpad, if specified) from the @bin and links the element to
106 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
109 gst_camerabin_try_add_element (GstBin * bin, const gchar * srcpad,
110 GstElement * new_elem, const gchar * dstpad)
113 GstElement *bin_elem;
116 g_return_val_if_fail (bin, FALSE);
117 g_return_val_if_fail (new_elem, FALSE);
119 /* Get pads for linking */
120 bin_pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC);
122 gst_bin_add (GST_BIN (bin), new_elem);
123 /* Link, if unconnected pad was found, otherwise just add it to bin */
125 GST_DEBUG_OBJECT (bin, "linking %s to %s:%s", GST_OBJECT_NAME (new_elem),
126 GST_DEBUG_PAD_NAME (bin_pad));
127 bin_elem = gst_pad_get_parent_element (bin_pad);
128 gst_object_unref (bin_pad);
129 if (!gst_element_link_pads_full (bin_elem, srcpad, new_elem, dstpad,
130 GST_PAD_LINK_CHECK_CAPS)) {
131 gst_object_ref (new_elem);
132 gst_bin_remove (bin, new_elem);
135 gst_object_unref (bin_elem);
137 GST_INFO_OBJECT (bin, "no unlinked source pad in bin");
144 * gst_camerabin_create_and_add_element:
145 * @bin: tries adding an element to this bin
146 * @elem_name: name of the element to be created
147 * @instance_name: name of the instance of the element to be created
149 * Creates an element according to given name and
150 * adds it to given @bin. Looks for an unconnected src pad
151 * from the @bin and links the element to it.
153 * Returns: pointer to the new element if successful, NULL otherwise.
156 gst_camerabin_create_and_add_element (GstBin * bin, const gchar * elem_name,
157 const gchar * instance_name)
159 GstElement *new_elem;
161 g_return_val_if_fail (bin, FALSE);
162 g_return_val_if_fail (elem_name, FALSE);
164 new_elem = gst_element_factory_make (elem_name, instance_name);
166 GST_ELEMENT_ERROR (bin, CORE, MISSING_PLUGIN,
167 (_("Missing element '%s' - check your GStreamer installation."),
169 } else if (!gst_camerabin_add_element (bin, new_elem)) {
176 /* try to change the state of an element. This function returns the element
177 * when the state change could be performed. When this function returns NULL
178 * an error occurred and the element is unreffed. */
180 try_element (GstElement * bin, GstElement * element)
182 GstStateChangeReturn ret;
185 ret = gst_element_set_state (element, GST_STATE_READY);
186 if (ret == GST_STATE_CHANGE_FAILURE) {
187 GST_DEBUG_OBJECT (bin, "failed state change..");
188 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), gst_object_ref (user_elem));
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);
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);
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 GValue value = { 0 };
234 GstElement *elem = NULL;
235 gboolean done = FALSE;
237 iter = gst_bin_iterate_elements (bin);
239 switch (gst_iterator_next (iter, &value)) {
240 case GST_ITERATOR_OK:
241 elem = (GstElement *) g_value_get_object (&value);
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 g_value_unset (&value);
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);