prepare 2.30.1.1 release
[empathy-mirror.git] / libempathy-gtk / empathy-video-src.c
blob96324e06c4a0a0ffa955ad3e55429a08851d300b
1 /*
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
22 #include <stdio.h>
23 #include <stdlib.h>
25 #include <gst/interfaces/colorbalance.h>
27 #include "empathy-video-src.h"
29 G_DEFINE_TYPE(EmpathyGstVideoSrc, empathy_video_src, GST_TYPE_BIN)
31 /* Keep in sync with EmpathyGstVideoSrcChannel */
32 static gchar *channel_names[NR_EMPATHY_GST_VIDEO_SRC_CHANNELS] = { "contrast",
33 "brightness", "gamma" };
35 /* signal enum */
36 #if 0
37 enum
39 LAST_SIGNAL
42 static guint signals[LAST_SIGNAL] = {0};
43 #endif
45 /* private structure */
46 typedef struct _EmpathyGstVideoSrcPrivate EmpathyGstVideoSrcPrivate;
48 struct _EmpathyGstVideoSrcPrivate
50 gboolean dispose_has_run;
51 GstElement *src;
52 /* Element implementing a ColorBalance interface */
53 GstElement *balance;
56 #define EMPATHY_GST_VIDEO_SRC_GET_PRIVATE(o) \
57 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_VIDEO_SRC, \
58 EmpathyGstVideoSrcPrivate))
60 static void
61 empathy_video_src_init (EmpathyGstVideoSrc *obj)
63 EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (obj);
64 GstElement *scale, *colorspace, *capsfilter;
65 GstPad *ghost, *src;
66 GstCaps *caps;
68 /* allocate any data required by the object here */
69 scale = gst_element_factory_make ("videoscale", NULL);
70 colorspace = gst_element_factory_make ("ffmpegcolorspace", NULL);
72 capsfilter = gst_element_factory_make ("capsfilter", NULL);
73 caps = gst_caps_new_simple ("video/x-raw-yuv",
74 "width", G_TYPE_INT, 320,
75 "height", G_TYPE_INT, 240,
76 NULL);
78 g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);
80 priv->src = gst_element_factory_make ("gconfvideosrc", NULL);
82 gst_bin_add_many (GST_BIN (obj), priv->src, scale, colorspace, capsfilter,
83 NULL);
84 gst_element_link_many (priv->src, scale, colorspace, capsfilter, NULL);
86 src = gst_element_get_static_pad (capsfilter, "src");
88 ghost = gst_ghost_pad_new ("src", src);
89 gst_element_add_pad (GST_ELEMENT (obj), ghost);
91 gst_object_unref (G_OBJECT (src));
94 static void empathy_video_src_dispose (GObject *object);
95 static void empathy_video_src_finalize (GObject *object);
97 static void
98 empathy_video_src_class_init (EmpathyGstVideoSrcClass *empathy_video_src_class)
100 GObjectClass *object_class = G_OBJECT_CLASS (empathy_video_src_class);
102 g_type_class_add_private (empathy_video_src_class,
103 sizeof (EmpathyGstVideoSrcPrivate));
105 object_class->dispose = empathy_video_src_dispose;
106 object_class->finalize = empathy_video_src_finalize;
109 void
110 empathy_video_src_dispose (GObject *object)
112 EmpathyGstVideoSrc *self = EMPATHY_GST_VIDEO_SRC (object);
113 EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);
115 if (priv->dispose_has_run)
116 return;
118 priv->dispose_has_run = TRUE;
120 /* release any references held by the object here */
122 if (G_OBJECT_CLASS (empathy_video_src_parent_class)->dispose)
123 G_OBJECT_CLASS (empathy_video_src_parent_class)->dispose (object);
126 void
127 empathy_video_src_finalize (GObject *object)
129 //EmpathyGstVideoSrc *self = EMPATHY_GST_VIDEO_SRC (object);
130 //EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);
132 /* free any data held directly by the object here */
134 G_OBJECT_CLASS (empathy_video_src_parent_class)->finalize (object);
137 GstElement *
138 empathy_video_src_new (void)
140 static gboolean registered = FALSE;
142 if (!registered) {
143 if (!gst_element_register (NULL, "empathyvideosrc",
144 GST_RANK_NONE, EMPATHY_TYPE_GST_VIDEO_SRC))
145 return NULL;
146 registered = TRUE;
148 return gst_element_factory_make ("empathyvideosrc", NULL);
151 void
152 empathy_video_src_set_channel (GstElement *src,
153 EmpathyGstVideoSrcChannel channel, guint percent)
155 GstElement *color;
156 GstColorBalance *balance;
157 const GList *channels;
158 GList *l;
160 /* Find something supporting GstColorBalance */
161 color = gst_bin_get_by_interface (GST_BIN (src), GST_TYPE_COLOR_BALANCE);
163 if (color == NULL)
164 return;
166 balance = GST_COLOR_BALANCE (color);
168 channels = gst_color_balance_list_channels (balance);
170 for (l = (GList *) channels; l != NULL; l = g_list_next (l))
172 GstColorBalanceChannel *c = GST_COLOR_BALANCE_CHANNEL (l->data);
174 if (g_ascii_strcasecmp (c->label, channel_names[channel]) == 0)
176 gst_color_balance_set_value (balance, c,
177 ((c->max_value - c->min_value) * percent)/100
178 + c->min_value);
179 break;
183 g_object_unref (color);
186 guint
187 empathy_video_src_get_channel (GstElement *src,
188 EmpathyGstVideoSrcChannel channel)
190 GstElement *color;
191 GstColorBalance *balance;
192 const GList *channels;
193 GList *l;
194 guint percent = 0;
196 /* Find something supporting GstColorBalance */
197 color = gst_bin_get_by_interface (GST_BIN (src), GST_TYPE_COLOR_BALANCE);
199 if (color == NULL)
200 return percent;
202 balance = GST_COLOR_BALANCE (color);
204 channels = gst_color_balance_list_channels (balance);
206 for (l = (GList *) channels; l != NULL; l = g_list_next (l))
208 GstColorBalanceChannel *c = GST_COLOR_BALANCE_CHANNEL (l->data);
210 if (g_ascii_strcasecmp (c->label, channel_names[channel]) == 0)
212 percent =
213 ((gst_color_balance_get_value (balance, c)
214 - c->min_value) * 100) /
215 (c->max_value - c->min_value);
217 break;
221 g_object_unref (color);
223 return percent;
227 guint
228 empathy_video_src_get_supported_channels (GstElement *src)
230 GstElement *color;
231 GstColorBalance *balance;
232 const GList *channels;
233 GList *l;
234 guint result = 0;
236 /* Find something supporting GstColorBalance */
237 color = gst_bin_get_by_interface (GST_BIN (src), GST_TYPE_COLOR_BALANCE);
239 if (color == NULL)
240 goto out;
242 balance = GST_COLOR_BALANCE (color);
244 channels = gst_color_balance_list_channels (balance);
246 for (l = (GList *) channels; l != NULL; l = g_list_next (l))
248 GstColorBalanceChannel *channel = GST_COLOR_BALANCE_CHANNEL (l->data);
249 int i;
251 for (i = 0; i < NR_EMPATHY_GST_VIDEO_SRC_CHANNELS; i++)
253 if (g_ascii_strcasecmp (channel->label, channel_names[i]) == 0)
255 result |= (1 << i);
256 break;
261 g_object_unref (color);
263 out:
264 return result;