Move files from gst-plugins-bad into the "subprojects/gst-plugins-bad/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / camerabin2 / camerabingeneral.c
1 /*
2  * GStreamer
3  * Copyright (C) 2008 Nokia Corporation <multimedia@maemo.org>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:camerabingeneral
23  * @title: GstCameraBin2
24  * @short_description: helper functions for #GstCameraBin2 and it's modules
25  *
26  * Common helper functions for #GstCameraBin2, #GstCameraBin2Image and
27  * #GstCameraBin2Video.
28  *
29  */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <string.h>
35
36 #include <glib.h>
37 #include <gst/basecamerabinsrc/gstbasecamerasrc.h>
38 #include <gst/gst-i18n-plugin.h>
39
40 #include "camerabingeneral.h"
41
42 /**
43  * gst_camerabin_add_element:
44  * @bin: add an element to this bin
45  * @new_elem: new element to be added
46  *
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.
50  *
51  * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
52  */
53 gboolean
54 gst_camerabin_add_element (GstBin * bin, GstElement * new_elem)
55 {
56   return gst_camerabin_add_element_full (bin, NULL, new_elem, NULL);
57 }
58
59 /**
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
65  *
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.
70  *
71  * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
72  */
73 gboolean
74 gst_camerabin_add_element_full (GstBin * bin, const gchar * srcpad,
75     GstElement * new_elem, const gchar * dstpad)
76 {
77   gboolean ret;
78
79   g_return_val_if_fail (bin, FALSE);
80   g_return_val_if_fail (new_elem, FALSE);
81
82   ret = gst_camerabin_try_add_element (bin, srcpad, new_elem, dstpad);
83
84   if (!ret) {
85     gchar *elem_name = gst_element_get_name (new_elem);
86     GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION, (NULL),
87         ("linking %s failed", elem_name));
88     g_free (elem_name);
89     gst_object_unref (new_elem);
90   }
91
92   return ret;
93 }
94
95 /**
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
101  *
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
104  * it.
105  *
106  * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
107  */
108 gboolean
109 gst_camerabin_try_add_element (GstBin * bin, const gchar * srcpad,
110     GstElement * new_elem, const gchar * dstpad)
111 {
112   GstPad *bin_pad;
113   GstElement *bin_elem;
114   gboolean ret = TRUE;
115
116   g_return_val_if_fail (bin, FALSE);
117   g_return_val_if_fail (new_elem, FALSE);
118
119   /* Get pads for linking */
120   bin_pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC);
121   /* Add to bin */
122   gst_bin_add (GST_BIN (bin), new_elem);
123   /* Link, if unconnected pad was found, otherwise just add it to bin */
124   if (bin_pad) {
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);
133       ret = FALSE;
134     }
135     gst_object_unref (bin_elem);
136   } else {
137     GST_INFO_OBJECT (bin, "no unlinked source pad in bin");
138   }
139
140   return ret;
141 }
142
143 /**
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
148  *
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.
152  *
153  * Returns: pointer to the new element if successful, NULL otherwise.
154  */
155 GstElement *
156 gst_camerabin_create_and_add_element (GstBin * bin, const gchar * elem_name,
157     const gchar * instance_name)
158 {
159   GstElement *new_elem;
160
161   g_return_val_if_fail (bin, FALSE);
162   g_return_val_if_fail (elem_name, FALSE);
163
164   new_elem = gst_element_factory_make (elem_name, instance_name);
165   if (!new_elem) {
166     GST_ELEMENT_ERROR (bin, CORE, MISSING_PLUGIN,
167         (_("Missing element '%s' - check your GStreamer installation."),
168             elem_name), (NULL));
169   } else if (!gst_camerabin_add_element (bin, new_elem)) {
170     new_elem = NULL;
171   }
172
173   return new_elem;
174 }
175
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. */
179 static GstElement *
180 try_element (GstElement * bin, GstElement * element)
181 {
182   GstStateChangeReturn ret;
183
184   if (element) {
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);
190       element = NULL;
191     }
192   }
193   return element;
194 }
195
196 GstElement *
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)
200 {
201   GstElement *elem;
202
203   if (user_elem) {
204     GST_DEBUG_OBJECT (bin, "trying configured element");
205     elem = try_element (GST_ELEMENT_CAST (bin), gst_object_ref (user_elem));
206   } else {
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);
211     if (elem == NULL) {
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);
217       }
218     }
219   }
220   return elem;
221 }
222
223 /**
224  * gst_camerabin_remove_elements_from_bin:
225  * @bin: removes all elements from this bin
226  *
227  * Removes all elements from this @bin.
228  */
229 void
230 gst_camerabin_remove_elements_from_bin (GstBin * bin)
231 {
232   GstIterator *iter = NULL;
233   GValue value = { 0 };
234   GstElement *elem = NULL;
235   gboolean done = FALSE;
236
237   iter = gst_bin_iterate_elements (bin);
238   while (!done) {
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);
246         break;
247       case GST_ITERATOR_RESYNC:
248         gst_iterator_resync (iter);
249         break;
250       case GST_ITERATOR_ERROR:
251         GST_WARNING_OBJECT (bin, "error in iterating elements");
252         done = TRUE;
253         break;
254       case GST_ITERATOR_DONE:
255         done = TRUE;
256         break;
257     }
258   }
259   gst_iterator_free (iter);
260 }