blob: ba6b82a96897d5d091e1290814ef69d67246713f [file] [log] [blame]
Nokia Corporation3751eae2009-02-05 15:48:32 +02001/*
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 #GstCameraBin and it's modules
24 *
25 * Common helper functions for #GstCameraBin, #GstCameraBinImage and
26 * #GstCameraBinVideo.
27 *
28 */
29
30#include "camerabingeneral.h"
31#include <glib.h>
32
33GST_DEBUG_CATEGORY (gst_camerabin_debug);
34
Nokia Corporation3751eae2009-02-05 15:48:32 +020035/**
36 * gst_camerabin_add_element:
37 * @bin: add an element to this bin
38 * @new_elem: new element to be added
39 *
40 * Adds given element to given @bin. Looks for an unconnected src pad
41 * from the @bin and links the element to it. Raises an error if adding
42 * or linking failed.
43 *
44 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
45 */
46gboolean
47gst_camerabin_add_element (GstBin * bin, GstElement * new_elem)
48{
49 gboolean ret = FALSE;
50
51 ret = gst_camerabin_try_add_element (bin, new_elem);
52
53 if (!ret) {
54 gchar *elem_name = gst_element_get_name (new_elem);
55 GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION, (NULL),
56 ("linking %s failed", elem_name));
57 g_free (elem_name);
58 }
59
60 return ret;
61}
62
63/**
64 * gst_camerabin_try_add_element:
65 * @bin: tries adding an element to this bin
66 * @new_elem: new element to be added
67 *
68 * Adds given element to given @bin. Looks for an unconnected src pad
69 * from the @bin and links the element to it.
70 *
71 * Returns: %TRUE if adding and linking succeeded, %FALSE otherwise.
72 */
73gboolean
74gst_camerabin_try_add_element (GstBin * bin, GstElement * new_elem)
75{
76 GstPad *bin_pad;
77 GstElement *bin_elem;
78 gboolean ret = TRUE;
79
80 if (!bin || !new_elem) {
81 return FALSE;
82 }
83
84 /* Get pads for linking */
Nokia Corporation3751eae2009-02-05 15:48:32 +020085 bin_pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC);
Nokia Corporation3751eae2009-02-05 15:48:32 +020086 /* Add to bin */
87 gst_bin_add (GST_BIN (bin), new_elem);
88 /* Link, if unconnected pad was found, otherwise just add it to bin */
89 if (bin_pad) {
Stefan Kosta9713912009-06-12 14:26:24 +030090 GST_DEBUG_OBJECT (bin, "linking %s to %s:%s", GST_OBJECT_NAME (new_elem),
91 GST_DEBUG_PAD_NAME (bin_pad));
Nokia Corporation3751eae2009-02-05 15:48:32 +020092 bin_elem = gst_pad_get_parent_element (bin_pad);
93 gst_object_unref (bin_pad);
94 if (!gst_element_link (bin_elem, new_elem)) {
95 gst_bin_remove (bin, new_elem);
96 ret = FALSE;
97 }
98 gst_object_unref (bin_elem);
Stefan Kosta9713912009-06-12 14:26:24 +030099 } else {
100 GST_INFO_OBJECT (bin, "no unlinked source pad in bin");
Nokia Corporation3751eae2009-02-05 15:48:32 +0200101 }
102
103 return ret;
104}
105
106/**
107 * gst_camerabin_create_and_add_element:
108 * @bin: tries adding an element to this bin
109 * @elem_name: name of the element to be created
110 *
111 * Creates an element according to given name and
112 * adds it to given @bin. Looks for an unconnected src pad
113 * from the @bin and links the element to it.
114 *
115 * Returns: pointer to the new element if successful, NULL otherwise.
116 */
117GstElement *
118gst_camerabin_create_and_add_element (GstBin * bin, const gchar * elem_name)
119{
120 GstElement *new_elem = NULL;
121
Nokia Corporation3751eae2009-02-05 15:48:32 +0200122 new_elem = gst_element_factory_make (elem_name, NULL);
123 if (!new_elem) {
124 GST_ELEMENT_ERROR (bin, CORE, MISSING_PLUGIN, (NULL),
125 ("could not create \"%s\" element.", elem_name));
126 } else if (!gst_camerabin_add_element (bin, new_elem)) {
127 new_elem = NULL;
128 }
129
130 return new_elem;
131}
132
133/**
134 * gst_camerabin_remove_elements_from_bin:
135 * @bin: removes all elements from this bin
136 *
137 * Removes all elements from this @bin.
138 */
139void
140gst_camerabin_remove_elements_from_bin (GstBin * bin)
141{
142 GstIterator *iter = NULL;
143 gpointer data = NULL;
144 GstElement *elem = NULL;
145 gboolean done = FALSE;
146
147 iter = gst_bin_iterate_elements (bin);
148 while (!done) {
149 switch (gst_iterator_next (iter, &data)) {
150 case GST_ITERATOR_OK:
151 elem = GST_ELEMENT (data);
152 gst_bin_remove (bin, elem);
153 /* Iterator increased the element refcount, so unref */
154 gst_object_unref (elem);
155 break;
156 case GST_ITERATOR_RESYNC:
157 gst_iterator_resync (iter);
158 break;
159 case GST_ITERATOR_ERROR:
160 GST_WARNING_OBJECT (bin, "error in iterating elements");
161 done = TRUE;
162 break;
163 case GST_ITERATOR_DONE:
164 done = TRUE;
165 break;
166 }
167 }
168 gst_iterator_free (iter);
169}
170
171/**
172 * gst_camerabin_drop_eos_probe:
173 * @pad: pad receiving the event
174 * @event: received event
175 * @u_data: not used
176 *
177 * Event probe that drop all eos events.
178 *
179 * Returns: FALSE to drop the event, TRUE otherwise
180 */
181gboolean
182gst_camerabin_drop_eos_probe (GstPad * pad, GstEvent * event, gpointer u_data)
183{
184 gboolean ret = TRUE;
185
186 switch (GST_EVENT_TYPE (event)) {
187 case GST_EVENT_EOS:
188 GST_DEBUG ("dropping eos in %s:%s", GST_DEBUG_PAD_NAME (pad));
189 ret = FALSE;
190 break;
191 default:
192 break;
193 }
194 return ret;
195}