2 * empathy-gst-video-src.c - Source for EmpathyGstVideoSrc
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <gst/interfaces/colorbalance.h>
27 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
28 #include <libempathy/empathy-debug.h>
30 #include "empathy-video-src.h"
32 G_DEFINE_TYPE(EmpathyGstVideoSrc, empathy_video_src, GST_TYPE_BIN)
34 /* Keep in sync with EmpathyGstVideoSrcChannel */
35 static const gchar *channel_names[NR_EMPATHY_GST_VIDEO_SRC_CHANNELS] = {
36 "contrast", "brightness", "gamma" };
45 static guint signals[LAST_SIGNAL] = {0};
48 /* private structure */
49 typedef struct _EmpathyGstVideoSrcPrivate EmpathyGstVideoSrcPrivate;
51 struct _EmpathyGstVideoSrcPrivate
53 gboolean dispose_has_run;
55 /* Element implementing a ColorBalance interface */
59 #define EMPATHY_GST_VIDEO_SRC_GET_PRIVATE(o) \
60 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_VIDEO_SRC, \
61 EmpathyGstVideoSrcPrivate))
63 * empathy_gst_add_to_bin - create a new gst element, add to bin and link it.
64 * @bin - bin to add the new element to.
65 * @src - src element for the new element (may be NULL).
66 * @name - name of the factory for the new element
68 * Returns: The newly created element ot %NULL on failure
71 empathy_gst_add_to_bin (GstBin *bin,
73 const gchar *factoryname)
77 if ((ret = gst_element_factory_make (factoryname, NULL)) == NULL)
79 g_message ("Element factory \"%s\" not found.", factoryname);
83 if (!gst_bin_add (bin, ret))
85 g_warning ("Couldn't add \"%s\" to bin.", factoryname);
89 /* do not link if src == NULL, just exit here */
93 if (!gst_element_link (src, ret))
95 g_warning ("Failed to link \"%s\".", factoryname);
96 gst_bin_remove (bin, ret);
104 gst_object_unref (ret);
110 empathy_video_src_init (EmpathyGstVideoSrc *obj)
112 EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (obj);
113 GstElement *element, *element_back;
118 /* allocate caps here, so we can update it by optional elements */
119 caps = gst_caps_new_simple ("video/x-raw-yuv",
120 "width", G_TYPE_INT, 320,
121 "height", G_TYPE_INT, 240,
124 /* allocate any data required by the object here */
125 if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
126 NULL, "gconfvideosrc")) == NULL)
127 g_error ("Couldn't add \"gconfvideosrc\" (gst-plugins-good missing?)");
129 /* we need to save our source to priv->src */
132 /* videomaxrate is optional as it's part of gst-plugins-bad. So don't
133 * fail if it doesn't exist. */
134 element_back = element;
135 if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
136 element, "videomaxrate")) == NULL)
138 g_message ("Couldn't add \"videomaxrate\" (gst-plugins-bad missing?)");
139 element = element_back;
143 gst_caps_set_simple (caps,
144 "framerate", GST_TYPE_FRACTION, 15, 1,
148 str = gst_caps_to_string (caps);
149 DEBUG ("Current video src caps are : %s", str);
152 if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
153 element, "ffmpegcolorspace")) == NULL)
154 g_error ("Failed to add \"ffmpegcolorspace\" (gst-plugins-base missing?)");
156 if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
157 element, "videoscale")) == NULL)
158 g_error ("Failed to add \"videoscale\", (gst-plugins-base missing?)");
160 if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
161 element, "capsfilter")) == NULL)
163 "Failed to add \"capsfilter\" (gstreamer core elements missing?)");
165 g_object_set (G_OBJECT (element), "caps", caps, NULL);
168 /* optionally add postproc_tmpnoise to improve the performance of encoders */
169 element_back = element;
170 if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
171 element, "postproc_tmpnoise")) == NULL)
173 g_message ("Failed to add \"postproc_tmpnoise\" (gst-ffmpeg missing?)");
174 element = element_back;
177 src = gst_element_get_static_pad (element, "src");
178 g_assert (src != NULL);
180 ghost = gst_ghost_pad_new ("src", src);
182 g_error ("Unable to create ghost pad for the videosrc");
184 if (!gst_element_add_pad (GST_ELEMENT (obj), ghost))
185 g_error ("pad with the same name already existed or "
186 "the pad already had another parent.");
188 gst_object_unref (G_OBJECT (src));
191 static void empathy_video_src_dispose (GObject *object);
192 static void empathy_video_src_finalize (GObject *object);
195 empathy_video_src_class_init (EmpathyGstVideoSrcClass *empathy_video_src_class)
197 GObjectClass *object_class = G_OBJECT_CLASS (empathy_video_src_class);
199 g_type_class_add_private (empathy_video_src_class,
200 sizeof (EmpathyGstVideoSrcPrivate));
202 object_class->dispose = empathy_video_src_dispose;
203 object_class->finalize = empathy_video_src_finalize;
207 empathy_video_src_dispose (GObject *object)
209 EmpathyGstVideoSrc *self = EMPATHY_GST_VIDEO_SRC (object);
210 EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);
212 if (priv->dispose_has_run)
215 priv->dispose_has_run = TRUE;
217 /* release any references held by the object here */
219 if (G_OBJECT_CLASS (empathy_video_src_parent_class)->dispose)
220 G_OBJECT_CLASS (empathy_video_src_parent_class)->dispose (object);
224 empathy_video_src_finalize (GObject *object)
226 //EmpathyGstVideoSrc *self = EMPATHY_GST_VIDEO_SRC (object);
227 //EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);
229 /* free any data held directly by the object here */
231 G_OBJECT_CLASS (empathy_video_src_parent_class)->finalize (object);
235 empathy_video_src_new (void)
237 static gboolean registered = FALSE;
240 if (!gst_element_register (NULL, "empathyvideosrc",
241 GST_RANK_NONE, EMPATHY_TYPE_GST_VIDEO_SRC))
245 return gst_element_factory_make ("empathyvideosrc", NULL);
249 empathy_video_src_set_channel (GstElement *src,
250 EmpathyGstVideoSrcChannel channel, guint percent)
253 GstColorBalance *balance;
254 const GList *channels;
257 /* Find something supporting GstColorBalance */
258 color = gst_bin_get_by_interface (GST_BIN (src), GST_TYPE_COLOR_BALANCE);
263 balance = GST_COLOR_BALANCE (color);
265 channels = gst_color_balance_list_channels (balance);
267 for (l = (GList *) channels; l != NULL; l = g_list_next (l))
269 GstColorBalanceChannel *c = GST_COLOR_BALANCE_CHANNEL (l->data);
271 if (g_ascii_strcasecmp (c->label, channel_names[channel]) == 0)
273 gst_color_balance_set_value (balance, c,
274 ((c->max_value - c->min_value) * percent)/100
280 g_object_unref (color);
284 empathy_video_src_get_channel (GstElement *src,
285 EmpathyGstVideoSrcChannel channel)
288 GstColorBalance *balance;
289 const GList *channels;
293 /* Find something supporting GstColorBalance */
294 color = gst_bin_get_by_interface (GST_BIN (src), GST_TYPE_COLOR_BALANCE);
299 balance = GST_COLOR_BALANCE (color);
301 channels = gst_color_balance_list_channels (balance);
303 for (l = (GList *) channels; l != NULL; l = g_list_next (l))
305 GstColorBalanceChannel *c = GST_COLOR_BALANCE_CHANNEL (l->data);
307 if (g_ascii_strcasecmp (c->label, channel_names[channel]) == 0)
310 ((gst_color_balance_get_value (balance, c)
311 - c->min_value) * 100) /
312 (c->max_value - c->min_value);
318 g_object_unref (color);
325 empathy_video_src_get_supported_channels (GstElement *src)
328 GstColorBalance *balance;
329 const GList *channels;
333 /* Find something supporting GstColorBalance */
334 color = gst_bin_get_by_interface (GST_BIN (src), GST_TYPE_COLOR_BALANCE);
339 balance = GST_COLOR_BALANCE (color);
341 channels = gst_color_balance_list_channels (balance);
343 for (l = (GList *) channels; l != NULL; l = g_list_next (l))
345 GstColorBalanceChannel *channel = GST_COLOR_BALANCE_CHANNEL (l->data);
348 for (i = 0; i < NR_EMPATHY_GST_VIDEO_SRC_CHANNELS; i++)
350 if (g_ascii_strcasecmp (channel->label, channel_names[i]) == 0)
358 g_object_unref (color);