aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-video-src.c
blob: ca8370d82b66ce45592529ec95c0665043f5813a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * empathy-gst-video-src.c - Source for EmpathyGstVideoSrc
 * Copyright (C) 2008 Collabora Ltd.
 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#include <stdio.h>
#include <stdlib.h>

#include <gst/interfaces/colorbalance.h>

#define DEBUG_FLAG EMPATHY_DEBUG_VOIP
#include <libempathy/empathy-debug.h>

#include "empathy-video-src.h"

G_DEFINE_TYPE(EmpathyGstVideoSrc, empathy_video_src, GST_TYPE_BIN)

/* Keep in sync with EmpathyGstVideoSrcChannel */
static const gchar *channel_names[NR_EMPATHY_GST_VIDEO_SRC_CHANNELS] = {
  "contrast", "brightness", "gamma" };

/* signal enum */
#if 0
enum
{
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0};
#endif

/* private structure */
typedef struct _EmpathyGstVideoSrcPrivate EmpathyGstVideoSrcPrivate;

struct _EmpathyGstVideoSrcPrivate
{
  gboolean dispose_has_run;
  GstElement *src;
  /* Element implementing a ColorBalance interface */
  GstElement *balance;
};

#define EMPATHY_GST_VIDEO_SRC_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_VIDEO_SRC, \
    EmpathyGstVideoSrcPrivate))
/**
 * empathy_gst_add_to_bin - create a new gst element, add to bin and link it.
 * @bin - bin to add the new element to.
 * @src - src element for the new element (may be NULL).
 * @name - name of the factory for the new element
 *
 * Returns: The newly created element ot %NULL on failure
 */
static GstElement *
empathy_gst_add_to_bin (GstBin *bin,
  GstElement *src,
  const gchar *factoryname)
{
  GstElement *ret;

  if ((ret = gst_element_factory_make (factoryname, NULL)) == NULL)
  {
    g_message ("Element factory \"%s\" not found.", factoryname);
    goto error;
  }

  if (!gst_bin_add (bin, ret))
  {
    g_warning ("Couldn't add \"%s\" to bin.", factoryname);
    goto error;
  }

  /* do not link if src == NULL, just exit here */
  if (src == NULL)
    return ret;

  if (!gst_element_link (src, ret))
  {
    g_warning ("Failed to link \"%s\".", factoryname);
    gst_bin_remove (bin, ret);
    goto error;
  }

  return ret;

error:
  if (ret != NULL)
    gst_object_unref (ret);
  return NULL;
}


static void
empathy_video_src_init (EmpathyGstVideoSrc *obj)
{
  EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (obj);
  GstElement *element, *element_back;
  GstPad *ghost, *src;
  GstCaps *caps;
  gchar *str;

  /* allocate caps here, so we can update it by optional elements */
  caps = gst_caps_new_simple ("video/x-raw-yuv",
    "width", G_TYPE_INT, 320,
    "height", G_TYPE_INT, 240,
    NULL);

  /* allocate any data required by the object here */
  if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
      NULL, "v4l2src")) == NULL)
    g_error ("Couldn't add \"v4l2src\" (gst-plugins-good missing?)");

  /* we need to save our source to priv->src */
  priv->src = element;

  /* videomaxrate is optional as it's part of gst-plugins-bad. So don't
   * fail if it doesn't exist. */
  element_back = element;
  if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
      element, "videomaxrate")) == NULL)
    {
      g_message ("Couldn't add \"videomaxrate\" (gst-plugins-bad missing?)");
      element = element_back;
    }
  else
    {
      gst_caps_set_simple (caps,
        "framerate", GST_TYPE_FRACTION, 15, 1,
        NULL);
    }

  str = gst_caps_to_string (caps);
  DEBUG ("Current video src caps are : %s", str);
  g_free (str);

  if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
      element, "ffmpegcolorspace")) == NULL)
    g_error ("Failed to add \"ffmpegcolorspace\" (gst-plugins-base missing?)");

  if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
      element, "videoscale")) == NULL)
    g_error ("Failed to add \"videoscale\", (gst-plugins-base missing?)");

  if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
      element, "capsfilter")) == NULL)
    g_error (
      "Failed to add \"capsfilter\" (gstreamer core elements missing?)");

  g_object_set (G_OBJECT (element), "caps", caps, NULL);


  /* optionally add postproc_tmpnoise to improve the performance of encoders */
  element_back = element;
  if ((element = empathy_gst_add_to_bin (GST_BIN (obj),
      element, "postproc_tmpnoise")) == NULL)
    {
      g_message ("Failed to add \"postproc_tmpnoise\" (gst-ffmpeg missing?)");
      element = element_back;
    }

  src = gst_element_get_static_pad (element, "src");
  g_assert (src != NULL);

  ghost = gst_ghost_pad_new ("src", src);
  if (ghost == NULL)
    g_error ("Unable to create ghost pad for the videosrc");

  if (!gst_element_add_pad (GST_ELEMENT (obj), ghost))
    g_error ("pad with the same name already existed or "
            "the pad already had another parent.");

  gst_object_unref (G_OBJECT (src));
}

static void empathy_video_src_dispose (GObject *object);
static void empathy_video_src_finalize (GObject *object);

static void
empathy_video_src_class_init (EmpathyGstVideoSrcClass *empathy_video_src_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (empathy_video_src_class);

  g_type_class_add_private (empathy_video_src_class,
    sizeof (EmpathyGstVideoSrcPrivate));

  object_class->dispose = empathy_video_src_dispose;
  object_class->finalize = empathy_video_src_finalize;
}

void
empathy_video_src_dispose (GObject *object)
{
  EmpathyGstVideoSrc *self = EMPATHY_GST_VIDEO_SRC (object);
  EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  /* release any references held by the object here */

  if (G_OBJECT_CLASS (empathy_video_src_parent_class)->dispose)
    G_OBJECT_CLASS (empathy_video_src_parent_class)->dispose (object);
}

void
empathy_video_src_finalize (GObject *object)
{
  //EmpathyGstVideoSrc *self = EMPATHY_GST_VIDEO_SRC (object);
  //EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);

  /* free any data held directly by the object here */

  G_OBJECT_CLASS (empathy_video_src_parent_class)->finalize (object);
}

GstElement *
empathy_video_src_new (void)
{
  static gboolean registered = FALSE;

  if (!registered) {
    if (!gst_element_register (NULL, "empathyvideosrc",
            GST_RANK_NONE, EMPATHY_TYPE_GST_VIDEO_SRC))
      return NULL;
    registered = TRUE;
  }
  return gst_element_factory_make ("empathyvideosrc", NULL);
}

static GstColorBalance *
dup_color_balance (GstElement *src)
{
  GstElement *color;

  /* Find something supporting GstColorBalance */
  color = gst_bin_get_by_interface (GST_BIN (src), GST_TYPE_COLOR_BALANCE);

  if (color == NULL)
    return NULL;

  /* colorbalance is wrapped by GstImplementsInterface, we
   * need to check if it is actually supported for this instance
   * in its current state before trying to use it */
  if (!GST_IS_COLOR_BALANCE (color))
    {
      g_object_unref (color);
      return NULL;
    }

  return GST_COLOR_BALANCE (color);
}

void
empathy_video_src_set_channel (GstElement *src,
  EmpathyGstVideoSrcChannel channel, guint percent)
{
  GstColorBalance *balance;
  const GList *channels;
  GList *l;

  balance = dup_color_balance (src);
  if (balance == NULL)
    return;

  channels = gst_color_balance_list_channels (balance);

  for (l = (GList *) channels; l != NULL; l = g_list_next (l))
    {
      GstColorBalanceChannel *c = GST_COLOR_BALANCE_CHANNEL (l->data);

      if (g_ascii_strcasecmp (c->label, channel_names[channel]) == 0)
        {
          gst_color_balance_set_value (balance, c,
            ((c->max_value - c->min_value) * percent)/100
              + c->min_value);
          break;
        }
    }

  g_object_unref (balance);
}

guint
empathy_video_src_get_channel (GstElement *src,
  EmpathyGstVideoSrcChannel channel)
{
  GstColorBalance *balance;
  const GList *channels;
  GList *l;
  guint percent = 0;

  balance = dup_color_balance (src);
  if (balance == NULL)
    return percent;

  channels = gst_color_balance_list_channels (balance);

  for (l = (GList *) channels; l != NULL; l = g_list_next (l))
    {
      GstColorBalanceChannel *c = GST_COLOR_BALANCE_CHANNEL (l->data);

      if (g_ascii_strcasecmp (c->label, channel_names[channel]) == 0)
        {
          percent =
            ((gst_color_balance_get_value (balance, c)
                - c->min_value) * 100) /
              (c->max_value - c->min_value);

          break;
        }
    }

  g_object_unref (balance);

  return percent;
}


guint
empathy_video_src_get_supported_channels (GstElement *src)
{
  GstColorBalance *balance;
  const GList *channels;
  GList *l;
  guint result = 0;

  balance = dup_color_balance (src);
  if (balance == NULL)
    goto out;

  channels = gst_color_balance_list_channels (balance);

  for (l = (GList *) channels; l != NULL; l = g_list_next (l))
    {
      GstColorBalanceChannel *channel = GST_COLOR_BALANCE_CHANNEL (l->data);
      int i;

      for (i = 0; i < NR_EMPATHY_GST_VIDEO_SRC_CHANNELS; i++)
        {
          if (g_ascii_strcasecmp (channel->label, channel_names[i]) == 0)
            {
              result |= (1 << i);
              break;
            }
        }
    }

  g_object_unref (balance);

out:
  return result;
}

void
empathy_video_src_change_device (EmpathyGstVideoSrc *self,
    const gchar *device)
{
  EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);
  GstState state;

  gst_element_get_state (priv->src, &state, NULL, 0);

  g_return_if_fail (state == GST_STATE_NULL);

  g_object_set (priv->src, "device", device, NULL);
}

gchar *
empathy_video_src_dup_device (EmpathyGstVideoSrc *self)
{
  EmpathyGstVideoSrcPrivate *priv = EMPATHY_GST_VIDEO_SRC_GET_PRIVATE (self);
  gchar *device;

  g_object_get (priv->src, "device", &device, NULL);

  return device;
}