Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / 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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:camerabingeneral
23  * @short_description: helper functions for #GstCameraBin2 and it's modules
24  *
25  * Common helper functions for #GstCameraBin2, #GstCameraBin2Image and
26  * #GstCameraBin2Video.
27  *
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <string.h>
34
35 #include <glib.h>
36 #include <gst/basecamerabinsrc/gstbasecamerasrc.h>
37 #include <gst/gst-i18n-plugin.h>
38
39 #include "camerabingeneral.h"
40
41 /**
42  * gst_camerabin_add_element:
43  * @bin: add an element to this bin
44  * @new_elem: new element to be added
45  *
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.
49  *
50  * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
51  */
52 gboolean
53 gst_camerabin_add_element (GstBin * bin, GstElement * new_elem)
54 {
55   return gst_camerabin_add_element_full (bin, NULL, new_elem, NULL);
56 }
57
58 /**
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
64  *
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.
69  *
70  * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
71  */
72 gboolean
73 gst_camerabin_add_element_full (GstBin * bin, const gchar * srcpad,
74     GstElement * new_elem, const gchar * dstpad)
75 {
76   gboolean ret;
77
78   g_return_val_if_fail (bin, FALSE);
79   g_return_val_if_fail (new_elem, FALSE);
80
81   ret = gst_camerabin_try_add_element (bin, srcpad, new_elem, dstpad);
82
83   if (!ret) {
84     gchar *elem_name = gst_element_get_name (new_elem);
85     GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION, (NULL),
86         ("linking %s failed", elem_name));
87     g_free (elem_name);
88     gst_object_unref (new_elem);
89   }
90
91   return ret;
92 }
93
94 /**
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
100  *
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
103  * it.
104  *
105  * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
106  */
107 gboolean
108 gst_camerabin_try_add_element (GstBin * bin, const gchar * srcpad,
109     GstElement * new_elem, const gchar * dstpad)
110 {
111   GstPad *bin_pad;
112   GstElement *bin_elem;
113   gboolean ret = TRUE;
114
115   g_return_val_if_fail (bin, FALSE);
116   g_return_val_if_fail (new_elem, FALSE);
117
118   /* Get pads for linking */
119   bin_pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC);
120   /* Add to bin */
121   gst_bin_add (GST_BIN (bin), new_elem);
122   /* Link, if unconnected pad was found, otherwise just add it to bin */
123   if (bin_pad) {
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);
132       ret = FALSE;
133     }
134     gst_object_unref (bin_elem);
135   } else {
136     GST_INFO_OBJECT (bin, "no unlinked source pad in bin");
137   }
138
139   return ret;
140 }
141
142 /**
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
147  *
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.
151  *
152  * Returns: pointer to the new element if successful, NULL otherwise.
153  */
154 GstElement *
155 gst_camerabin_create_and_add_element (GstBin * bin, const gchar * elem_name,
156     const gchar * instance_name)
157 {
158   GstElement *new_elem;
159
160   g_return_val_if_fail (bin, FALSE);
161   g_return_val_if_fail (elem_name, FALSE);
162
163   new_elem = gst_element_factory_make (elem_name, instance_name);
164   if (!new_elem) {
165     GST_ELEMENT_ERROR (bin, CORE, MISSING_PLUGIN,
166         (_("Missing element '%s' - check your GStreamer installation."),
167             elem_name), (NULL));
168   } else if (!gst_camerabin_add_element (bin, new_elem)) {
169     new_elem = NULL;
170   }
171
172   return new_elem;
173 }
174
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. */
178 static GstElement *
179 try_element (GstElement * bin, GstElement * element, gboolean unref)
180 {
181   GstStateChangeReturn ret;
182
183   if (element) {
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);
188       if (unref)
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), user_elem, FALSE);
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, TRUE);
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, TRUE);
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   gpointer data = NULL;
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, &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);
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 }