~ubuntu-branches/debian/sid/cheese/sid

« back to all changes in this revision

Viewing changes to src/cheese-webcam.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2010-05-04 17:37:18 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20100504173718-k2rx3nryi4vd0xyx
Tags: 2.30.1-1
* New upstream release.
  - HAL dependency has been dropped. Use (g)udev for v4l capability probing
    on Linux. Closes: #573774
  - Split code into separate libraries.
* debian/control.in
  - Drop Build-Depends on libhal-dev.
  - Drop Build-Depends on libebook1.2-dev.
  - Bump Build-Depends on libgtk2.0-dev to (>= 2.19.1).
  - Bump Build-Depends on libgstreamer*-dev to (>= 0.10.23).
  - Add Build-Depends on libcanberra-gtk-dev.
  - Add Build-Depends on libxtst-dev.
  - Add Build-Depends on libgudev-1.0-dev on Linux.
  - Bump Standards-Version to 3.8.4. No further changes.
* Switch to source format 3.0 (quilt)
  - Add debian/source/format.
* debian/rules
  - Drop lpia specific configure flags, lpia is dead.
* Update package layout (based on work by Ubuntu)
  - Move data files into new cheese-common package.
  - Keep binary along with its desktop and dbus service file in the cheese
    package.
  - Add libcheese-gtk18 and libcheese-gtk-dev package for the new
    libcheese-gtk library. Use a symbols file for improved shlibs
    dependencies.
  - Add Conflicts/Replaces to cheese-common to ensure proper upgrades from
    previous versions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2007,2008 Jaap Haitsma <jaap@haitsma.org>
3
 
 * Copyright © 2007,2008 daniel g. siegel <dgsiegel@gnome.org>
4
 
 * Copyright © 2008 Ryan Zeigler <zeiglerr@gmail.com>
5
 
 *
6
 
 * Licensed under the GNU General Public License Version 2
7
 
 *
8
 
 * This program is free software; you can redistribute it and/or modify
9
 
 * it under the terms of the GNU General Public License as published by
10
 
 * the Free Software Foundation; either version 2 of the License, or
11
 
 * (at your option) any later version.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
 */
21
 
#ifdef HAVE_CONFIG_H
22
 
  #include <cheese-config.h>
23
 
#endif
24
 
 
25
 
#include <string.h>
26
 
#include <glib-object.h>
27
 
#include <glib.h>
28
 
#include <glib/gi18n.h>
29
 
#include <gtk/gtk.h>
30
 
#include <gdk/gdkx.h>
31
 
#include <gst/gst.h>
32
 
#include <gdk-pixbuf/gdk-pixbuf.h>
33
 
#include <X11/Xlib.h>
34
 
#include <libhal.h>
35
 
 
36
 
/* for ioctl query */
37
 
#include <fcntl.h>
38
 
#include <unistd.h>
39
 
#include <sys/ioctl.h>
40
 
#include <linux/videodev.h>
41
 
 
42
 
#include "cheese-webcam.h"
43
 
 
44
 
G_DEFINE_TYPE (CheeseWebcam, cheese_webcam, G_TYPE_OBJECT)
45
 
 
46
 
#define CHEESE_WEBCAM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CHEESE_TYPE_WEBCAM, CheeseWebcamPrivate))
47
 
 
48
 
#define CHEESE_WEBCAM_ERROR cheese_webcam_error_quark ()
49
 
 
50
 
#define MIN_DEFAULT_RATE 15.0
51
 
 
52
 
static void find_highest_framerate (CheeseVideoFormat *format);
53
 
 
54
 
enum CheeseWebcamError
55
 
{
56
 
  CHEESE_WEBCAM_ERROR_UNKNOWN,
57
 
  CHEESE_WEBCAM_ERROR_ELEMENT_NOT_FOUND
58
 
};
59
 
 
60
 
typedef struct
61
 
{
62
 
  GtkWidget *video_window;
63
 
 
64
 
  GstElement *pipeline;
65
 
  GstBus *bus;
66
 
 
67
 
  /* We build the active pipeline by linking the appropriate pipelines listed below*/
68
 
  GstElement *webcam_source_bin;
69
 
  GstElement *video_display_bin;
70
 
  GstElement *photo_save_bin;
71
 
  GstElement *video_save_bin;
72
 
 
73
 
  GstElement *video_source;
74
 
  GstElement *capsfilter;
75
 
  GstElement *video_file_sink;
76
 
  GstElement *photo_sink;
77
 
  GstElement *audio_source;
78
 
  GstElement *audio_enc;
79
 
  GstElement *video_enc;
80
 
 
81
 
  GstElement *effect_filter, *csp_post_effect;
82
 
  GstElement *video_balance, *csp_post_balance;
83
 
 
84
 
  gulong photo_handler_signal_id;
85
 
 
86
 
  gboolean is_recording;
87
 
  gboolean pipeline_is_playing;
88
 
  char *photo_filename;
89
 
 
90
 
  int num_webcam_devices;
91
 
  char *device_name;
92
 
  CheeseWebcamDevice *webcam_devices;
93
 
  int x_resolution;
94
 
  int y_resolution;
95
 
  int selected_device;
96
 
  CheeseVideoFormat *current_format;
97
 
 
98
 
  guint eos_timeout_id;
99
 
} CheeseWebcamPrivate;
100
 
 
101
 
enum
102
 
{
103
 
  PROP_0,
104
 
  PROP_VIDEO_WINDOW,
105
 
  PROP_DEVICE_NAME,
106
 
  PROP_X_RESOLUTION,
107
 
  PROP_Y_RESOLUTION
108
 
};
109
 
 
110
 
enum
111
 
{
112
 
  PHOTO_SAVED,
113
 
  VIDEO_SAVED,
114
 
  LAST_SIGNAL
115
 
};
116
 
 
117
 
static guint webcam_signals[LAST_SIGNAL];
118
 
 
119
 
typedef enum
120
 
{
121
 
  RGB,
122
 
  YUV
123
 
} VideoColorSpace;
124
 
 
125
 
typedef struct
126
 
{
127
 
  CheeseWebcamEffect effect;
128
 
  const char *pipeline_desc;
129
 
  VideoColorSpace colorspace; /* The color space the effect works in */
130
 
} EffectToPipelineDesc;
131
 
 
132
 
 
133
 
static const EffectToPipelineDesc EFFECT_TO_PIPELINE_DESC[] = {
134
 
  {CHEESE_WEBCAM_EFFECT_NO_EFFECT,       "identity",                             RGB},
135
 
  {CHEESE_WEBCAM_EFFECT_MAUVE,           "videobalance saturation=1.5 hue=+0.5", YUV},
136
 
  {CHEESE_WEBCAM_EFFECT_NOIR_BLANC,      "videobalance saturation=0",            YUV},
137
 
  {CHEESE_WEBCAM_EFFECT_SATURATION,      "videobalance saturation=2",            YUV},
138
 
  {CHEESE_WEBCAM_EFFECT_HULK,            "videobalance saturation=1.5 hue=-0.5", YUV},
139
 
  {CHEESE_WEBCAM_EFFECT_VERTICAL_FLIP,   "videoflip method=5",                   YUV},
140
 
  {CHEESE_WEBCAM_EFFECT_HORIZONTAL_FLIP, "videoflip method=4",                   YUV},
141
 
  {CHEESE_WEBCAM_EFFECT_SHAGADELIC,      "shagadelictv",                         RGB},
142
 
  {CHEESE_WEBCAM_EFFECT_VERTIGO,         "vertigotv",                            RGB},
143
 
  {CHEESE_WEBCAM_EFFECT_EDGE,            "edgetv",                               RGB},
144
 
  {CHEESE_WEBCAM_EFFECT_DICE,            "dicetv",                               RGB},
145
 
  {CHEESE_WEBCAM_EFFECT_WARP,            "warptv",                               RGB}
146
 
};
147
 
 
148
 
static const int NUM_EFFECTS = G_N_ELEMENTS (EFFECT_TO_PIPELINE_DESC);
149
 
 
150
 
GQuark
151
 
cheese_webcam_error_quark (void)
152
 
{
153
 
  return g_quark_from_static_string ("cheese-webcam-error-quark");
154
 
}
155
 
 
156
 
static GstBusSyncReply
157
 
cheese_webcam_bus_sync_handler (GstBus *bus, GstMessage *message, CheeseWebcam *webcam)
158
 
{
159
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
160
 
  GstXOverlay         *overlay;
161
 
 
162
 
  if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
163
 
    return GST_BUS_PASS;
164
 
 
165
 
  if (!gst_structure_has_name (message->structure, "prepare-xwindow-id"))
166
 
    return GST_BUS_PASS;
167
 
 
168
 
  overlay = GST_X_OVERLAY (GST_MESSAGE_SRC (message));
169
 
 
170
 
  if (g_object_class_find_property (G_OBJECT_GET_CLASS (overlay),
171
 
                                    "force-aspect-ratio"))
172
 
    g_object_set (G_OBJECT (overlay), "force-aspect-ratio", TRUE, NULL);
173
 
 
174
 
  gst_x_overlay_set_xwindow_id (overlay,
175
 
                                GDK_WINDOW_XWINDOW (gtk_widget_get_window (priv->video_window)));
176
 
 
177
 
  gst_message_unref (message);
178
 
 
179
 
  return GST_BUS_DROP;
180
 
}
181
 
 
182
 
static void
183
 
cheese_webcam_change_sink (CheeseWebcam *webcam, GstElement *src,
184
 
                           GstElement *new_sink, GstElement *old_sink)
185
 
{
186
 
  CheeseWebcamPrivate *priv       = CHEESE_WEBCAM_GET_PRIVATE (webcam);
187
 
  gboolean             is_playing = priv->pipeline_is_playing;
188
 
 
189
 
  cheese_webcam_stop (webcam);
190
 
 
191
 
  gst_element_unlink (src, old_sink);
192
 
  gst_object_ref (old_sink);
193
 
  gst_bin_remove (GST_BIN (priv->pipeline), old_sink);
194
 
 
195
 
  gst_bin_add (GST_BIN (priv->pipeline), new_sink);
196
 
  gst_element_link (src, new_sink);
197
 
 
198
 
  if (is_playing)
199
 
    cheese_webcam_play (webcam);
200
 
}
201
 
 
202
 
static gboolean
203
 
cheese_webcam_expose_cb (GtkWidget *widget, GdkEventExpose *event, CheeseWebcam *webcam)
204
 
{
205
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
206
 
  GstState             state;
207
 
  GstXOverlay         *overlay = GST_X_OVERLAY (gst_bin_get_by_interface (GST_BIN (priv->pipeline),
208
 
                                                                          GST_TYPE_X_OVERLAY));
209
 
 
210
 
  gst_element_get_state (priv->pipeline, &state, NULL, 0);
211
 
 
212
 
  if ((state < GST_STATE_PLAYING) || (overlay == NULL))
213
 
  {
214
 
    gdk_draw_rectangle (widget->window, widget->style->black_gc, TRUE,
215
 
                        0, 0, widget->allocation.width, widget->allocation.height);
216
 
  }
217
 
  else
218
 
  {
219
 
    gst_x_overlay_expose (overlay);
220
 
  }
221
 
 
222
 
  return FALSE;
223
 
}
224
 
 
225
 
static void
226
 
cheese_webcam_photo_data_cb (GstElement *element, GstBuffer *buffer,
227
 
                             GstPad *pad, CheeseWebcam *webcam)
228
 
{
229
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
230
 
 
231
 
  GstCaps            *caps;
232
 
  const GstStructure *structure;
233
 
  int                 width, height, stride;
234
 
  GdkPixbuf          *pixbuf;
235
 
  const int           bits_per_pixel = 8;
236
 
 
237
 
  caps      = gst_buffer_get_caps (buffer);
238
 
  structure = gst_caps_get_structure (caps, 0);
239
 
  gst_structure_get_int (structure, "width", &width);
240
 
  gst_structure_get_int (structure, "height", &height);
241
 
 
242
 
  stride = buffer->size / height;
243
 
  pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buffer), GDK_COLORSPACE_RGB,
244
 
                                     FALSE, bits_per_pixel, width, height, stride,
245
 
                                     NULL, NULL);
246
 
 
247
 
  gdk_pixbuf_save (pixbuf, priv->photo_filename, "jpeg", NULL, NULL);
248
 
  g_object_unref (G_OBJECT (pixbuf));
249
 
 
250
 
  g_signal_handler_disconnect (G_OBJECT (priv->photo_sink),
251
 
                               priv->photo_handler_signal_id);
252
 
  priv->photo_handler_signal_id = 0;
253
 
 
254
 
  g_signal_emit (webcam, webcam_signals[PHOTO_SAVED], 0);
255
 
}
256
 
 
257
 
static void
258
 
cheese_webcam_bus_message_cb (GstBus *bus, GstMessage *message, CheeseWebcam *webcam)
259
 
{
260
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
261
 
 
262
 
  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_EOS)
263
 
  {
264
 
    if (priv->is_recording)
265
 
    {
266
 
      g_print ("Received EOS message\n");
267
 
 
268
 
      g_source_remove (priv->eos_timeout_id);
269
 
 
270
 
      g_signal_emit (webcam, webcam_signals[VIDEO_SAVED], 0);
271
 
 
272
 
      cheese_webcam_change_sink (webcam, priv->video_display_bin,
273
 
                                 priv->photo_save_bin, priv->video_save_bin);
274
 
      priv->is_recording = FALSE;
275
 
    }
276
 
  }
277
 
}
278
 
 
279
 
static void
280
 
cheese_webcam_get_video_devices_from_hal (CheeseWebcam *webcam)
281
 
{
282
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
283
 
 
284
 
  int            i, fd, ok;
285
 
  int            num_udis = 0;
286
 
  char         **udis;
287
 
  DBusError      error;
288
 
  LibHalContext *hal_ctx;
289
 
 
290
 
  priv->num_webcam_devices = 0;
291
 
 
292
 
  g_print ("Probing devices with HAL...\n");
293
 
 
294
 
  dbus_error_init (&error);
295
 
  hal_ctx = libhal_ctx_new ();
296
 
  if (hal_ctx == NULL)
297
 
  {
298
 
    g_warning ("Could not create libhal context");
299
 
    dbus_error_free (&error);
300
 
    goto fallback;
301
 
  }
302
 
 
303
 
  if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error)))
304
 
  {
305
 
    g_warning ("libhal_ctx_set_dbus_connection: %s: %s", error.name, error.message);
306
 
    dbus_error_free (&error);
307
 
    goto fallback;
308
 
  }
309
 
 
310
 
  if (!libhal_ctx_init (hal_ctx, &error))
311
 
  {
312
 
    if (dbus_error_is_set (&error))
313
 
    {
314
 
      g_warning ("libhal_ctx_init: %s: %s", error.name, error.message);
315
 
      dbus_error_free (&error);
316
 
    }
317
 
    g_warning ("Could not initialise connection to hald.\n"
318
 
               "Normally this means the HAL daemon (hald) is not running or not ready");
319
 
    goto fallback;
320
 
  }
321
 
 
322
 
  udis = libhal_find_device_by_capability (hal_ctx, "video4linux", &num_udis, &error);
323
 
 
324
 
  if (dbus_error_is_set (&error))
325
 
  {
326
 
    g_warning ("libhal_find_device_by_capability: %s: %s", error.name, error.message);
327
 
    dbus_error_free (&error);
328
 
    goto fallback;
329
 
  }
330
 
 
331
 
  /* Initialize webcam structures */
332
 
  priv->webcam_devices = g_new0 (CheeseWebcamDevice, num_udis);
333
 
 
334
 
  for (i = 0; i < num_udis; i++)
335
 
  {
336
 
    char                   *device;
337
 
    char                   *parent_udi = NULL;
338
 
    char                   *subsystem  = NULL;
339
 
    char                   *gstreamer_src, *product_name;
340
 
    struct v4l2_capability  v2cap;
341
 
    struct video_capability v1cap;
342
 
    gint                    vendor_id     = 0;
343
 
    gint                    product_id    = 0;
344
 
    gchar                  *property_name = NULL;
345
 
 
346
 
    parent_udi = libhal_device_get_property_string (hal_ctx, udis[i], "info.parent", &error);
347
 
    if (dbus_error_is_set (&error))
348
 
    {
349
 
      g_warning ("error getting parent for %s: %s: %s", udis[i], error.name, error.message);
350
 
      dbus_error_free (&error);
351
 
    }
352
 
 
353
 
    if (parent_udi != NULL)
354
 
    {
355
 
      subsystem = libhal_device_get_property_string (hal_ctx, parent_udi, "info.subsystem", NULL);
356
 
      if (subsystem == NULL) continue;
357
 
      property_name = g_strjoin (".", subsystem, "vendor_id", NULL);
358
 
      vendor_id     = libhal_device_get_property_int (hal_ctx, parent_udi, property_name, &error);
359
 
      if (dbus_error_is_set (&error))
360
 
      {
361
 
        g_warning ("error getting vendor id: %s: %s", error.name, error.message);
362
 
        dbus_error_free (&error);
363
 
      }
364
 
      g_free (property_name);
365
 
 
366
 
      property_name = g_strjoin (".", subsystem, "product_id", NULL);
367
 
      product_id    = libhal_device_get_property_int (hal_ctx, parent_udi, property_name, &error);
368
 
      if (dbus_error_is_set (&error))
369
 
      {
370
 
        g_warning ("error getting product id: %s: %s", error.name, error.message);
371
 
        dbus_error_free (&error);
372
 
      }
373
 
      g_free (property_name);
374
 
      libhal_free_string (subsystem);
375
 
      libhal_free_string (parent_udi);
376
 
    }
377
 
 
378
 
    g_print ("Found device %04x:%04x, getting capabilities...\n", vendor_id, product_id);
379
 
 
380
 
    device = libhal_device_get_property_string (hal_ctx, udis[i], "video4linux.device", &error);
381
 
    if (dbus_error_is_set (&error))
382
 
    {
383
 
      g_warning ("error getting V4L device for %s: %s: %s", udis[i], error.name, error.message);
384
 
      dbus_error_free (&error);
385
 
      continue;
386
 
    }
387
 
 
388
 
    /* vbi devices support capture capability too, but cannot be used,
389
 
     * so detect them by device name */
390
 
    if (strstr (device, "vbi"))
391
 
    {
392
 
      g_print ("Skipping vbi device: %s\n", device);
393
 
      libhal_free_string (device);
394
 
      continue;
395
 
    }
396
 
 
397
 
    if ((fd = open (device, O_RDONLY | O_NONBLOCK)) < 0)
398
 
    {
399
 
      g_warning ("Failed to open %s: %s", device, strerror (errno));
400
 
      libhal_free_string (device);
401
 
      continue;
402
 
    }
403
 
    ok = ioctl (fd, VIDIOC_QUERYCAP, &v2cap);
404
 
    if (ok < 0)
405
 
    {
406
 
      ok = ioctl (fd, VIDIOCGCAP, &v1cap);
407
 
      if (ok < 0)
408
 
      {
409
 
        g_warning ("Error while probing v4l capabilities for %s: %s",
410
 
                   device, strerror (errno));
411
 
        libhal_free_string (device);
412
 
        close (fd);
413
 
        continue;
414
 
      }
415
 
      g_print ("Detected v4l device: %s\n", v1cap.name);
416
 
      g_print ("Device type: %d\n", v1cap.type);
417
 
      gstreamer_src = "v4lsrc";
418
 
      product_name  = v1cap.name;
419
 
    }
420
 
    else
421
 
    {
422
 
      guint cap = v2cap.capabilities;
423
 
      g_print ("Detected v4l2 device: %s\n", v2cap.card);
424
 
      g_print ("Driver: %s, version: %d\n", v2cap.driver, v2cap.version);
425
 
 
426
 
      /* g_print ("Bus info: %s\n", v2cap.bus_info); */ /* Doesn't seem anything useful */
427
 
      g_print ("Capabilities: 0x%08X\n", v2cap.capabilities);
428
 
      if (!(cap & V4L2_CAP_VIDEO_CAPTURE))
429
 
      {
430
 
        g_print ("Device %s seems to not have the capture capability, (radio tuner?)\n"
431
 
                 "Removing it from device list.\n", device);
432
 
        libhal_free_string (device);
433
 
        close (fd);
434
 
        continue;
435
 
      }
436
 
      gstreamer_src = "v4l2src";
437
 
      product_name  = (char *) v2cap.card;
438
 
    }
439
 
 
440
 
    g_print ("\n");
441
 
 
442
 
    priv->webcam_devices[priv->num_webcam_devices].hal_udi           = g_strdup (udis[i]);
443
 
    priv->webcam_devices[priv->num_webcam_devices].video_device      = g_strdup (device);
444
 
    priv->webcam_devices[priv->num_webcam_devices].gstreamer_src     = g_strdup (gstreamer_src);
445
 
    priv->webcam_devices[priv->num_webcam_devices].product_name      = g_strdup (product_name);
446
 
    priv->webcam_devices[priv->num_webcam_devices].num_video_formats = 0;
447
 
    priv->webcam_devices[priv->num_webcam_devices].video_formats     =
448
 
      g_array_new (FALSE, FALSE, sizeof (CheeseVideoFormat));
449
 
    priv->webcam_devices[priv->num_webcam_devices].supported_resolutions =
450
 
      g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
451
 
    priv->num_webcam_devices++;
452
 
 
453
 
    libhal_free_string (device);
454
 
    close (fd);
455
 
  }
456
 
  libhal_free_string_array (udis);
457
 
 
458
 
  if (priv->num_webcam_devices == 0)
459
 
  {
460
 
    /* Create a fake device so that resolution changing stil works even if the
461
 
     * computer doesn't have a webcam. */
462
 
fallback:
463
 
    if (num_udis == 0)
464
 
    {
465
 
      priv->webcam_devices = g_new0 (CheeseWebcamDevice, 1);
466
 
    }
467
 
    priv->webcam_devices[0].num_video_formats = 0;
468
 
    priv->webcam_devices[0].video_formats     =
469
 
      g_array_new (FALSE, FALSE, sizeof (CheeseVideoFormat));
470
 
    priv->webcam_devices[0].hal_udi = g_strdup ("cheese_fake_videodevice");
471
 
  }
472
 
}
473
 
 
474
 
static void
475
 
cheese_webcam_get_supported_framerates (CheeseVideoFormat *video_format, GstStructure *structure)
476
 
{
477
 
  const GValue *framerates;
478
 
  int           i, j;
479
 
 
480
 
  framerates = gst_structure_get_value (structure, "framerate");
481
 
  if (GST_VALUE_HOLDS_FRACTION (framerates))
482
 
  {
483
 
    video_format->num_framerates            = 1;
484
 
    video_format->framerates                = g_new0 (CheeseFramerate, video_format->num_framerates);
485
 
    video_format->framerates[0].numerator   = gst_value_get_fraction_numerator (framerates);
486
 
    video_format->framerates[0].denominator = gst_value_get_fraction_denominator (framerates);
487
 
  }
488
 
  else if (GST_VALUE_HOLDS_LIST (framerates))
489
 
  {
490
 
    video_format->num_framerates = gst_value_list_get_size (framerates);
491
 
    video_format->framerates     = g_new0 (CheeseFramerate, video_format->num_framerates);
492
 
    for (i = 0; i < video_format->num_framerates; i++)
493
 
    {
494
 
      const GValue *value;
495
 
      value                                   = gst_value_list_get_value (framerates, i);
496
 
      video_format->framerates[i].numerator   = gst_value_get_fraction_numerator (value);
497
 
      video_format->framerates[i].denominator = gst_value_get_fraction_denominator (value);
498
 
    }
499
 
  }
500
 
  else if (GST_VALUE_HOLDS_FRACTION_RANGE (framerates))
501
 
  {
502
 
    int           numerator_min, denominator_min, numerator_max, denominator_max;
503
 
    const GValue *fraction_range_min;
504
 
    const GValue *fraction_range_max;
505
 
 
506
 
    fraction_range_min = gst_value_get_fraction_range_min (framerates);
507
 
    numerator_min      = gst_value_get_fraction_numerator (fraction_range_min);
508
 
    denominator_min    = gst_value_get_fraction_denominator (fraction_range_min);
509
 
 
510
 
    fraction_range_max = gst_value_get_fraction_range_max (framerates);
511
 
    numerator_max      = gst_value_get_fraction_numerator (fraction_range_max);
512
 
    denominator_max    = gst_value_get_fraction_denominator (fraction_range_max);
513
 
    g_print ("FractionRange: %d/%d - %d/%d\n", numerator_min, denominator_min, numerator_max, denominator_max);
514
 
 
515
 
    video_format->num_framerates = (numerator_max - numerator_min + 1) * (denominator_max - denominator_min + 1);
516
 
    video_format->framerates     = g_new0 (CheeseFramerate, video_format->num_framerates);
517
 
    int k = 0;
518
 
    for (i = numerator_min; i <= numerator_max; i++)
519
 
    {
520
 
      for (j = denominator_min; j <= denominator_max; j++)
521
 
      {
522
 
        video_format->framerates[k].numerator   = i;
523
 
        video_format->framerates[k].denominator = j;
524
 
        k++;
525
 
      }
526
 
    }
527
 
  }
528
 
  else
529
 
  {
530
 
    g_critical ("GValue type %s, cannot be handled for framerates", G_VALUE_TYPE_NAME (framerates));
531
 
  }
532
 
}
533
 
 
534
 
static void
535
 
cheese_webcam_add_video_format (CheeseWebcamDevice *webcam_device,
536
 
                                CheeseVideoFormat *video_format, GstStructure *format_structure)
537
 
{
538
 
  int    i;
539
 
  gchar *resolution;
540
 
 
541
 
  cheese_webcam_get_supported_framerates (video_format, format_structure);
542
 
  find_highest_framerate (video_format);
543
 
 
544
 
  g_print ("%s %d x %d num_framerates %d\n", video_format->mimetype, video_format->width,
545
 
           video_format->height, video_format->num_framerates);
546
 
  for (i = 0; i < video_format->num_framerates; i++)
547
 
  {
548
 
    g_print ("%d/%d ", video_format->framerates[i].numerator,
549
 
             video_format->framerates[i].denominator);
550
 
  }
551
 
 
552
 
  resolution = g_strdup_printf ("%ix%i", video_format->width,
553
 
                                video_format->height);
554
 
  i = GPOINTER_TO_INT (g_hash_table_lookup (
555
 
                         webcam_device->supported_resolutions,
556
 
                         resolution));
557
 
  if (i)   /* Resolution already added ? */
558
 
  {
559
 
    CheeseVideoFormat *curr_format = &g_array_index (
560
 
      webcam_device->video_formats,
561
 
      CheeseVideoFormat, i - 1);
562
 
    float new_framerate = (float) video_format->highest_framerate.numerator /
563
 
                          video_format->highest_framerate.denominator;
564
 
    float curr_framerate = (float) curr_format->highest_framerate.numerator /
565
 
                           curr_format->highest_framerate.denominator;
566
 
    if (new_framerate > curr_framerate)
567
 
    {
568
 
      g_print ("higher framerate replacing existing format\n");
569
 
      *curr_format = *video_format;
570
 
    }
571
 
    else
572
 
      g_print ("already added, skipping\n");
573
 
 
574
 
    g_free (resolution);
575
 
    return;
576
 
  }
577
 
 
578
 
  g_array_append_val (webcam_device->video_formats, *video_format);
579
 
  g_hash_table_insert (webcam_device->supported_resolutions, resolution,
580
 
                       GINT_TO_POINTER (webcam_device->num_video_formats + 1));
581
 
 
582
 
  webcam_device->num_video_formats++;
583
 
}
584
 
 
585
 
static gint
586
 
cheese_resolution_compare (gconstpointer _a, gconstpointer _b)
587
 
{
588
 
  const CheeseVideoFormat *a = _a;
589
 
  const CheeseVideoFormat *b = _b;
590
 
 
591
 
  if (a->width == b->width)
592
 
    return a->height - b->height;
593
 
 
594
 
  return a->width - b->width;
595
 
}
596
 
 
597
 
static void
598
 
cheese_webcam_get_supported_video_formats (CheeseWebcamDevice *webcam_device, GstCaps *caps)
599
 
{
600
 
  int i;
601
 
  int num_structures;
602
 
 
603
 
  num_structures = gst_caps_get_size (caps);
604
 
  for (i = 0; i < num_structures; i++)
605
 
  {
606
 
    GstStructure *structure;
607
 
    const GValue *width, *height;
608
 
    structure = gst_caps_get_structure (caps, i);
609
 
 
610
 
    /* only interested in raw formats; we don't want to end up using image/jpeg
611
 
     * (or whatever else the cam may produce) since we won't be able to link
612
 
     * that to ffmpegcolorspace or the effect plugins, which makes it rather
613
 
     * useless (although we could plug a decoder of course) */
614
 
    if (!gst_structure_has_name (structure, "video/x-raw-yuv") &&
615
 
        !gst_structure_has_name (structure, "video/x-raw-rgb"))
616
 
    {
617
 
      continue;
618
 
    }
619
 
 
620
 
    width  = gst_structure_get_value (structure, "width");
621
 
    height = gst_structure_get_value (structure, "height");
622
 
 
623
 
    if (G_VALUE_HOLDS_INT (width))
624
 
    {
625
 
      CheeseVideoFormat video_format;
626
 
 
627
 
      video_format.mimetype = g_strdup (gst_structure_get_name (structure));
628
 
      gst_structure_get_int (structure, "width", &(video_format.width));
629
 
      gst_structure_get_int (structure, "height", &(video_format.height));
630
 
      cheese_webcam_add_video_format (webcam_device, &video_format, structure);
631
 
    }
632
 
    else if (GST_VALUE_HOLDS_INT_RANGE (width))
633
 
    {
634
 
      int min_width, max_width, min_height, max_height;
635
 
      int cur_width, cur_height;
636
 
 
637
 
      min_width  = gst_value_get_int_range_min (width);
638
 
      max_width  = gst_value_get_int_range_max (width);
639
 
      min_height = gst_value_get_int_range_min (height);
640
 
      max_height = gst_value_get_int_range_max (height);
641
 
 
642
 
      cur_width  = min_width;
643
 
      cur_height = min_height;
644
 
 
645
 
      /* Gstreamer will sometimes give us a range with min_xxx == max_xxx,
646
 
       * we use <= here (and not below) to make this work */
647
 
      while (cur_width <= max_width && cur_height <= max_height)
648
 
      {
649
 
        CheeseVideoFormat video_format;
650
 
 
651
 
        video_format.mimetype = g_strdup (gst_structure_get_name (structure));
652
 
        video_format.width    = cur_width;
653
 
        video_format.height   = cur_height;
654
 
        cheese_webcam_add_video_format (webcam_device, &video_format, structure);
655
 
        cur_width  *= 2;
656
 
        cur_height *= 2;
657
 
      }
658
 
 
659
 
      cur_width  = max_width;
660
 
      cur_height = max_height;
661
 
      while (cur_width > min_width && cur_height > min_height)
662
 
      {
663
 
        CheeseVideoFormat video_format;
664
 
 
665
 
        video_format.mimetype = g_strdup (gst_structure_get_name (structure));
666
 
        video_format.width    = cur_width;
667
 
        video_format.height   = cur_height;
668
 
        cheese_webcam_add_video_format (webcam_device, &video_format, structure);
669
 
        cur_width  /= 2;
670
 
        cur_height /= 2;
671
 
      }
672
 
    }
673
 
    else
674
 
    {
675
 
      g_critical ("GValue type %s, cannot be handled for resolution width", G_VALUE_TYPE_NAME (width));
676
 
    }
677
 
  }
678
 
 
679
 
  /* Sort the format array (so that it will show sorted in the resolution
680
 
   * selection GUI), and rebuild the hashtable (as that will be invalid after
681
 
   * the sorting) */
682
 
  g_array_sort (webcam_device->video_formats, cheese_resolution_compare);
683
 
  g_hash_table_remove_all (webcam_device->supported_resolutions);
684
 
  for (i = 0; i < webcam_device->num_video_formats; i++)
685
 
  {
686
 
    CheeseVideoFormat *format = &g_array_index (webcam_device->video_formats,
687
 
                                                CheeseVideoFormat, i);
688
 
    g_hash_table_insert (webcam_device->supported_resolutions,
689
 
                         g_strdup_printf ("%ix%i", format->width,
690
 
                                          format->height),
691
 
                         GINT_TO_POINTER (i + 1));
692
 
  }
693
 
}
694
 
 
695
 
static void
696
 
cheese_webcam_get_webcam_device_data (CheeseWebcam       *webcam,
697
 
                                      CheeseWebcamDevice *webcam_device)
698
 
{
699
 
  char                *pipeline_desc;
700
 
  GstElement          *pipeline;
701
 
  GError              *err;
702
 
  GstStateChangeReturn ret;
703
 
  GstMessage          *msg;
704
 
  GstBus              *bus;
705
 
 
706
 
  {
707
 
    pipeline_desc = g_strdup_printf ("%s name=source device=%s ! fakesink",
708
 
                                     webcam_device->gstreamer_src,
709
 
                                     webcam_device->video_device);
710
 
    err      = NULL;
711
 
    pipeline = gst_parse_launch (pipeline_desc, &err);
712
 
    if ((pipeline != NULL) && (err == NULL))
713
 
    {
714
 
      /* Start the pipeline and wait for max. 10 seconds for it to start up */
715
 
      gst_element_set_state (pipeline, GST_STATE_PLAYING);
716
 
      ret = gst_element_get_state (pipeline, NULL, NULL, 10 * GST_SECOND);
717
 
 
718
 
      /* Check if any error messages were posted on the bus */
719
 
      bus = gst_element_get_bus (pipeline);
720
 
      msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
721
 
      gst_object_unref (bus);
722
 
 
723
 
      if ((msg == NULL) && (ret == GST_STATE_CHANGE_SUCCESS))
724
 
      {
725
 
        GstElement *src;
726
 
        GstPad     *pad;
727
 
        char       *name;
728
 
        GstCaps    *caps;
729
 
 
730
 
        gst_element_set_state (pipeline, GST_STATE_PAUSED);
731
 
 
732
 
        src = gst_bin_get_by_name (GST_BIN (pipeline), "source");
733
 
 
734
 
        g_object_get (G_OBJECT (src), "device-name", &name, NULL);
735
 
        if (name == NULL)
736
 
          name = "Unknown";
737
 
 
738
 
        g_print ("Device: %s (%s)\n", name, webcam_device->video_device);
739
 
        pad  = gst_element_get_pad (src, "src");
740
 
        caps = gst_pad_get_caps (pad);
741
 
        gst_object_unref (pad);
742
 
        cheese_webcam_get_supported_video_formats (webcam_device, caps);
743
 
        gst_caps_unref (caps);
744
 
      }
745
 
      gst_element_set_state (pipeline, GST_STATE_NULL);
746
 
      gst_object_unref (pipeline);
747
 
    }
748
 
    if (err)
749
 
      g_error_free (err);
750
 
 
751
 
    g_free (pipeline_desc);
752
 
  }
753
 
}
754
 
 
755
 
static void
756
 
cheese_webcam_create_fake_format (CheeseWebcam *webcam)
757
 
{
758
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
759
 
 
760
 
  CheeseVideoFormat format;
761
 
 
762
 
  /* Right now just emulate one format: video/x-raw-yuv, 320x240 @ 30 Hz */
763
 
 
764
 
  format.mimetype                  = g_strdup ("video/x-raw-yuv");
765
 
  format.width                     = 320;
766
 
  format.height                    = 240;
767
 
  format.num_framerates            = 1;
768
 
  format.framerates                = g_new0 (CheeseFramerate, 1);
769
 
  format.framerates[0].numerator   = 30;
770
 
  format.framerates[0].denominator = 1;
771
 
 
772
 
  g_array_append_val (priv->webcam_devices[0].video_formats, format);
773
 
  priv->current_format = &format;
774
 
}
775
 
 
776
 
static void
777
 
cheese_webcam_detect_webcam_devices (CheeseWebcam *webcam)
778
 
{
779
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
780
 
 
781
 
  int i;
782
 
 
783
 
  cheese_webcam_get_video_devices_from_hal (webcam);
784
 
 
785
 
  g_print ("Probing supported video formats...\n");
786
 
  for (i = 0; i < priv->num_webcam_devices; i++)
787
 
  {
788
 
    cheese_webcam_get_webcam_device_data (webcam, &(priv->webcam_devices[i]));
789
 
    g_print ("\n");
790
 
  }
791
 
 
792
 
  if (priv->num_webcam_devices == 0)
793
 
    cheese_webcam_create_fake_format (webcam);
794
 
}
795
 
 
796
 
static void
797
 
find_highest_framerate (CheeseVideoFormat *format)
798
 
{
799
 
  int framerate_numerator;
800
 
  int framerate_denominator;
801
 
  int i;
802
 
 
803
 
  /* Select the highest framerate up to 30 Hz*/
804
 
  framerate_numerator   = 1;
805
 
  framerate_denominator = 1;
806
 
  for (i = 0; i < format->num_framerates; i++)
807
 
  {
808
 
    float framerate = format->framerates[i].numerator / format->framerates[i].denominator;
809
 
    if (framerate > ((float) framerate_numerator / framerate_denominator)
810
 
        && framerate <= 30)
811
 
    {
812
 
      framerate_numerator   = format->framerates[i].numerator;
813
 
      framerate_denominator = format->framerates[i].denominator;
814
 
    }
815
 
  }
816
 
 
817
 
  format->highest_framerate.numerator   = framerate_numerator;
818
 
  format->highest_framerate.denominator = framerate_denominator;
819
 
}
820
 
 
821
 
static gboolean
822
 
cheese_webcam_create_webcam_source_bin (CheeseWebcam *webcam)
823
 
{
824
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
825
 
 
826
 
  GError *err = NULL;
827
 
  char   *webcam_input;
828
 
 
829
 
  if (priv->num_webcam_devices == 0)
830
 
  {
831
 
    priv->webcam_source_bin = gst_parse_bin_from_description (
832
 
      "videotestsrc name=video_source ! capsfilter name=capsfilter ! identity",
833
 
      TRUE,
834
 
      &err);
835
 
  }
836
 
  else
837
 
  {
838
 
    CheeseVideoFormat *format;
839
 
    int                i;
840
 
    gchar             *resolution;
841
 
 
842
 
    /* If we have a matching video device use that one, otherwise use the first */
843
 
    priv->selected_device = 0;
844
 
    format                = NULL;
845
 
    for (i = 1; i < priv->num_webcam_devices; i++)
846
 
    {
847
 
      if (g_strcmp0 (priv->webcam_devices[i].video_device, priv->device_name) == 0)
848
 
        priv->selected_device = i;
849
 
    }
850
 
    CheeseWebcamDevice *selected_webcam = &(priv->webcam_devices[priv->selected_device]);
851
 
 
852
 
    resolution = g_strdup_printf ("%ix%i", priv->x_resolution,
853
 
                                  priv->y_resolution);
854
 
 
855
 
    /* Use the previously set resolution from gconf if it is set and the
856
 
     * camera supports it. */
857
 
    if (priv->x_resolution != 0 && priv->y_resolution != 0)
858
 
    {
859
 
      i = GPOINTER_TO_INT (g_hash_table_lookup (selected_webcam->supported_resolutions, resolution));
860
 
      if (i)
861
 
        format = &g_array_index (selected_webcam->video_formats,
862
 
                                 CheeseVideoFormat, i - 1);
863
 
    }
864
 
 
865
 
    if (!format)
866
 
    {
867
 
      /* Select the highest resolution */
868
 
      format = &(g_array_index (selected_webcam->video_formats,
869
 
                                CheeseVideoFormat, 0));
870
 
      for (i = 1; i < selected_webcam->num_video_formats; i++)
871
 
      {
872
 
        CheeseVideoFormat *new = &g_array_index (selected_webcam->video_formats,
873
 
                                                 CheeseVideoFormat, i);
874
 
        gfloat newrate = new->highest_framerate.numerator /
875
 
                         new->highest_framerate.denominator;
876
 
        if ((new->width + new->height) > (format->width + format->height) &&
877
 
            (newrate >= MIN_DEFAULT_RATE))
878
 
        {
879
 
          format = new;
880
 
        }
881
 
      }
882
 
    }
883
 
 
884
 
    priv->current_format = format;
885
 
    g_free (resolution);
886
 
 
887
 
    if (format == NULL)
888
 
      goto fallback;
889
 
 
890
 
    webcam_input = g_strdup_printf (
891
 
      "%s name=video_source device=%s ! capsfilter name=capsfilter caps=video/x-raw-rgb,width=%d,height=%d,framerate=%d/%d;video/x-raw-yuv,width=%d,height=%d,framerate=%d/%d ! identity",
892
 
      selected_webcam->gstreamer_src,
893
 
      selected_webcam->video_device,
894
 
      format->width,
895
 
      format->height,
896
 
      format->highest_framerate.numerator,
897
 
      format->highest_framerate.denominator,
898
 
      format->width,
899
 
      format->height,
900
 
      format->highest_framerate.numerator,
901
 
      format->highest_framerate.denominator);
902
 
    g_print ("%s\n", webcam_input);
903
 
 
904
 
    priv->webcam_source_bin = gst_parse_bin_from_description (webcam_input,
905
 
                                                              TRUE, &err);
906
 
    g_free (webcam_input);
907
 
 
908
 
    if (priv->webcam_source_bin == NULL)
909
 
      goto fallback;
910
 
  }
911
 
 
912
 
  priv->video_source = gst_bin_get_by_name (GST_BIN (priv->webcam_source_bin), "video_source");
913
 
  priv->capsfilter   = gst_bin_get_by_name (GST_BIN (priv->webcam_source_bin), "capsfilter");
914
 
  return TRUE;
915
 
 
916
 
fallback:
917
 
  if (err != NULL)
918
 
  {
919
 
    g_error_free (err);
920
 
    err = NULL;
921
 
  }
922
 
 
923
 
  priv->webcam_source_bin = gst_parse_bin_from_description ("videotestsrc name=video_source",
924
 
                                                            TRUE, &err);
925
 
  priv->video_source = gst_bin_get_by_name (GST_BIN (priv->webcam_source_bin), "video_source");
926
 
  if (err != NULL)
927
 
  {
928
 
    g_error_free (err);
929
 
    return FALSE;
930
 
  }
931
 
  priv->capsfilter = gst_bin_get_by_name (GST_BIN (priv->webcam_source_bin),
932
 
                                          "capsfilter");
933
 
  return TRUE;
934
 
}
935
 
 
936
 
static void
937
 
cheese_webcam_set_error_element_not_found (GError **error, const char *factoryname)
938
 
{
939
 
  if (error == NULL)
940
 
    return;
941
 
 
942
 
  if (*error == NULL)
943
 
  {
944
 
    g_set_error (error, CHEESE_WEBCAM_ERROR, CHEESE_WEBCAM_ERROR_ELEMENT_NOT_FOUND, "%s.", factoryname);
945
 
  }
946
 
  else
947
 
  {
948
 
    /* Ensure that what is found is not a substring of an element; all strings
949
 
     * should have a ' ' or nothing to the left and a '.' or ',' to the right */
950
 
    gchar *found = g_strrstr ((*error)->message, factoryname);
951
 
    gchar  prev  = 0;
952
 
    gchar  next  = 0;
953
 
 
954
 
    if (found != NULL)
955
 
    {
956
 
      prev = *(found - 1);
957
 
      next = *(found + strlen (factoryname));
958
 
    }
959
 
 
960
 
    if (found == NULL ||
961
 
        ((found != (*error)->message && prev != ' ') && /* Prefix check */
962
 
         (next != ',' && next != '.'))) /* Postfix check */
963
 
    {
964
 
      g_prefix_error (error, "%s, ", factoryname);
965
 
    }
966
 
  }
967
 
}
968
 
 
969
 
static gboolean
970
 
cheese_webcam_create_video_display_bin (CheeseWebcam *webcam, GError **error)
971
 
{
972
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
973
 
 
974
 
  GstElement *tee, *video_display_queue, *video_scale, *video_sink, *save_queue;
975
 
 
976
 
  gboolean ok;
977
 
  GstPad  *pad;
978
 
 
979
 
  priv->video_display_bin = gst_bin_new ("video_display_bin");
980
 
 
981
 
  cheese_webcam_create_webcam_source_bin (webcam);
982
 
 
983
 
  if ((priv->effect_filter = gst_element_factory_make ("identity", "effect")) == NULL)
984
 
  {
985
 
    cheese_webcam_set_error_element_not_found (error, "identity");
986
 
  }
987
 
  if ((priv->csp_post_effect = gst_element_factory_make ("ffmpegcolorspace", "csp_post_effect")) == NULL)
988
 
  {
989
 
    cheese_webcam_set_error_element_not_found (error, "ffmpegcolorspace");
990
 
  }
991
 
  if ((priv->video_balance = gst_element_factory_make ("videobalance", "video_balance")) == NULL)
992
 
  {
993
 
    cheese_webcam_set_error_element_not_found (error, "videobalance");
994
 
    return FALSE;
995
 
  }
996
 
  if ((priv->csp_post_balance = gst_element_factory_make ("ffmpegcolorspace", "csp_post_balance")) == NULL)
997
 
  {
998
 
    cheese_webcam_set_error_element_not_found (error, "ffmpegcolorspace");
999
 
    return FALSE;
1000
 
  }
1001
 
 
1002
 
  if ((tee = gst_element_factory_make ("tee", "tee")) == NULL)
1003
 
  {
1004
 
    cheese_webcam_set_error_element_not_found (error, "tee");
1005
 
  }
1006
 
 
1007
 
  if ((save_queue = gst_element_factory_make ("queue", "save_queue")) == NULL)
1008
 
  {
1009
 
    cheese_webcam_set_error_element_not_found (error, "queue");
1010
 
  }
1011
 
 
1012
 
  if ((video_display_queue = gst_element_factory_make ("queue", "video_display_queue")) == NULL)
1013
 
  {
1014
 
    cheese_webcam_set_error_element_not_found (error, "queue");
1015
 
  }
1016
 
 
1017
 
  if ((video_scale = gst_element_factory_make ("videoscale", "video_scale")) == NULL)
1018
 
  {
1019
 
    cheese_webcam_set_error_element_not_found (error, "videoscale");
1020
 
  }
1021
 
  else
1022
 
  {
1023
 
    /* Use bilinear scaling */
1024
 
    g_object_set (video_scale, "method", 1, NULL);
1025
 
  }
1026
 
 
1027
 
  if ((video_sink = gst_element_factory_make ("gconfvideosink", "video_sink")) == NULL)
1028
 
  {
1029
 
    cheese_webcam_set_error_element_not_found (error, "gconfvideosink");
1030
 
  }
1031
 
 
1032
 
  if (error != NULL && *error != NULL)
1033
 
    return FALSE;
1034
 
 
1035
 
  gst_bin_add_many (GST_BIN (priv->video_display_bin), priv->webcam_source_bin,
1036
 
                    priv->effect_filter, priv->csp_post_effect,
1037
 
                    priv->video_balance, priv->csp_post_balance,
1038
 
                    tee, save_queue,
1039
 
                    video_display_queue, video_scale, video_sink, NULL);
1040
 
 
1041
 
  ok = gst_element_link_many (priv->webcam_source_bin, priv->effect_filter,
1042
 
                              priv->csp_post_effect,
1043
 
                              priv->video_balance, priv->csp_post_balance,
1044
 
                              tee, NULL);
1045
 
 
1046
 
  ok &= gst_element_link_many (tee, save_queue, NULL);
1047
 
  ok &= gst_element_link_many (tee, video_display_queue, video_scale, video_sink, NULL);
1048
 
 
1049
 
  /* add ghostpad */
1050
 
  pad = gst_element_get_pad (save_queue, "src");
1051
 
  gst_element_add_pad (priv->video_display_bin, gst_ghost_pad_new ("src", pad));
1052
 
  gst_object_unref (GST_OBJECT (pad));
1053
 
 
1054
 
 
1055
 
  if (!ok)
1056
 
    g_error ("Unable to create display pipeline");
1057
 
 
1058
 
  return TRUE;
1059
 
}
1060
 
 
1061
 
static gboolean
1062
 
cheese_webcam_create_photo_save_bin (CheeseWebcam *webcam, GError **error)
1063
 
{
1064
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1065
 
 
1066
 
  GstElement *csp_photo_save_bin;
1067
 
 
1068
 
  gboolean ok;
1069
 
  GstPad  *pad;
1070
 
  GstCaps *caps;
1071
 
 
1072
 
  priv->photo_save_bin = gst_bin_new ("photo_save_bin");
1073
 
 
1074
 
  if ((csp_photo_save_bin = gst_element_factory_make ("ffmpegcolorspace", "csp_photo_save_bin")) == NULL)
1075
 
  {
1076
 
    cheese_webcam_set_error_element_not_found (error, "ffmpegcolorspace");
1077
 
  }
1078
 
  if ((priv->photo_sink = gst_element_factory_make ("fakesink", "photo_sink")) == NULL)
1079
 
  {
1080
 
    cheese_webcam_set_error_element_not_found (error, "fakesink");
1081
 
  }
1082
 
 
1083
 
  if (error != NULL && *error != NULL)
1084
 
    return FALSE;
1085
 
 
1086
 
  gst_bin_add_many (GST_BIN (priv->photo_save_bin), csp_photo_save_bin,
1087
 
                    priv->photo_sink, NULL);
1088
 
 
1089
 
  /* add ghostpad */
1090
 
  pad = gst_element_get_pad (csp_photo_save_bin, "sink");
1091
 
  gst_element_add_pad (priv->photo_save_bin, gst_ghost_pad_new ("sink", pad));
1092
 
  gst_object_unref (GST_OBJECT (pad));
1093
 
 
1094
 
  caps = gst_caps_new_simple ("video/x-raw-rgb",
1095
 
                              "bpp", G_TYPE_INT, 24,
1096
 
                              "depth", G_TYPE_INT, 24,
1097
 
                              NULL);
1098
 
  ok = gst_element_link_filtered (csp_photo_save_bin, priv->photo_sink, caps);
1099
 
  gst_caps_unref (caps);
1100
 
 
1101
 
  g_object_set (G_OBJECT (priv->photo_sink), "signal-handoffs", TRUE, NULL);
1102
 
 
1103
 
  if (!ok)
1104
 
    g_error ("Unable to create photo save pipeline");
1105
 
 
1106
 
  return TRUE;
1107
 
}
1108
 
 
1109
 
static gboolean
1110
 
cheese_webcam_create_video_save_bin (CheeseWebcam *webcam, GError **error)
1111
 
{
1112
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1113
 
 
1114
 
  GstElement *audio_queue, *audio_convert, *audio_enc;
1115
 
  GstElement *video_save_csp, *video_save_rate, *video_save_scale, *video_enc;
1116
 
  GstElement *mux;
1117
 
  GstPad     *pad;
1118
 
  gboolean    ok;
1119
 
 
1120
 
  priv->video_save_bin = gst_bin_new ("video_save_bin");
1121
 
 
1122
 
  if ((priv->audio_source = gst_element_factory_make ("gconfaudiosrc", "audio_source")) == NULL)
1123
 
  {
1124
 
    cheese_webcam_set_error_element_not_found (error, "gconfaudiosrc");
1125
 
  }
1126
 
  if ((audio_queue = gst_element_factory_make ("queue", "audio_queue")) == NULL)
1127
 
  {
1128
 
    cheese_webcam_set_error_element_not_found (error, "queue");
1129
 
  }
1130
 
  if ((audio_convert = gst_element_factory_make ("audioconvert", "audio_convert")) == NULL)
1131
 
  {
1132
 
    cheese_webcam_set_error_element_not_found (error, "audioconvert");
1133
 
  }
1134
 
  if ((audio_enc = gst_element_factory_make ("vorbisenc", "audio_enc")) == NULL)
1135
 
  {
1136
 
    cheese_webcam_set_error_element_not_found (error, "vorbisenc");
1137
 
  }
1138
 
 
1139
 
  if ((video_save_csp = gst_element_factory_make ("ffmpegcolorspace", "video_save_csp")) == NULL)
1140
 
  {
1141
 
    cheese_webcam_set_error_element_not_found (error, "ffmpegcolorspace");
1142
 
  }
1143
 
  if ((video_enc = gst_element_factory_make ("theoraenc", "video_enc")) == NULL)
1144
 
  {
1145
 
    cheese_webcam_set_error_element_not_found (error, "theoraenc");
1146
 
  }
1147
 
  else
1148
 
  {
1149
 
    g_object_set (video_enc, "keyframe-force", 1, NULL);
1150
 
  }
1151
 
 
1152
 
  if ((video_save_rate = gst_element_factory_make ("videorate", "video_save_rate")) == NULL)
1153
 
  {
1154
 
    cheese_webcam_set_error_element_not_found (error, "videorate");
1155
 
  }
1156
 
  if ((video_save_scale = gst_element_factory_make ("videoscale", "video_save_scale")) == NULL)
1157
 
  {
1158
 
    cheese_webcam_set_error_element_not_found (error, "videoscale");
1159
 
  }
1160
 
  else
1161
 
  {
1162
 
    /* Use bilinear scaling */
1163
 
    g_object_set (video_save_scale, "method", 1, NULL);
1164
 
  }
1165
 
 
1166
 
  if ((mux = gst_element_factory_make ("oggmux", "mux")) == NULL)
1167
 
  {
1168
 
    cheese_webcam_set_error_element_not_found (error, "oggmux");
1169
 
  }
1170
 
  else
1171
 
  {
1172
 
    g_object_set (G_OBJECT (mux),
1173
 
                  "max-delay", (guint64) 10000000,
1174
 
                  "max-page-delay", (guint64) 10000000, NULL);
1175
 
  }
1176
 
 
1177
 
  if ((priv->video_file_sink = gst_element_factory_make ("filesink", "video_file_sink")) == NULL)
1178
 
  {
1179
 
    cheese_webcam_set_error_element_not_found (error, "filesink");
1180
 
  }
1181
 
 
1182
 
  if (error != NULL && *error != NULL)
1183
 
    return FALSE;
1184
 
 
1185
 
  gst_bin_add_many (GST_BIN (priv->video_save_bin), priv->audio_source, audio_queue,
1186
 
                    audio_convert, audio_enc, video_save_csp, video_save_rate, video_save_scale, video_enc,
1187
 
                    mux, priv->video_file_sink, NULL);
1188
 
 
1189
 
  /* add ghostpad */
1190
 
  pad = gst_element_get_pad (video_save_csp, "sink");
1191
 
  gst_element_add_pad (priv->video_save_bin, gst_ghost_pad_new ("sink", pad));
1192
 
  gst_object_unref (GST_OBJECT (pad));
1193
 
 
1194
 
 
1195
 
  ok = gst_element_link_many (priv->audio_source, audio_queue, audio_convert,
1196
 
                              audio_enc, mux, priv->video_file_sink, NULL);
1197
 
 
1198
 
  ok &= gst_element_link_many (video_save_csp, video_save_rate, video_save_scale, video_enc,
1199
 
                               NULL);
1200
 
  ok &= gst_element_link (video_enc, mux);
1201
 
 
1202
 
  if (!ok)
1203
 
    g_error ("Unable to create video save pipeline");
1204
 
 
1205
 
  return TRUE;
1206
 
}
1207
 
 
1208
 
int
1209
 
cheese_webcam_get_num_webcam_devices (CheeseWebcam *webcam)
1210
 
{
1211
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1212
 
 
1213
 
  return priv->num_webcam_devices;
1214
 
}
1215
 
 
1216
 
gboolean
1217
 
cheese_webcam_switch_webcam_device (CheeseWebcam *webcam)
1218
 
{
1219
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1220
 
 
1221
 
  gboolean was_recording        = FALSE;
1222
 
  gboolean pipeline_was_playing = FALSE;
1223
 
  gboolean disp_bin_created     = FALSE;
1224
 
  gboolean disp_bin_added       = FALSE;
1225
 
  gboolean disp_bin_linked      = FALSE;
1226
 
  GError  *error                = NULL;
1227
 
 
1228
 
  if (priv->is_recording)
1229
 
  {
1230
 
    cheese_webcam_stop_video_recording (webcam);
1231
 
    was_recording = TRUE;
1232
 
  }
1233
 
 
1234
 
  if (priv->pipeline_is_playing)
1235
 
  {
1236
 
    cheese_webcam_stop (webcam);
1237
 
    pipeline_was_playing = TRUE;
1238
 
  }
1239
 
 
1240
 
  gst_bin_remove (GST_BIN (priv->pipeline), priv->video_display_bin);
1241
 
 
1242
 
  disp_bin_created = cheese_webcam_create_video_display_bin (webcam, &error);
1243
 
  if (!disp_bin_created)
1244
 
  {
1245
 
    return FALSE;
1246
 
  }
1247
 
  disp_bin_added = gst_bin_add (GST_BIN (priv->pipeline), priv->video_display_bin);
1248
 
  if (!disp_bin_added)
1249
 
  {
1250
 
    gst_object_sink (priv->video_display_bin);
1251
 
    return FALSE;
1252
 
  }
1253
 
 
1254
 
  disp_bin_linked = gst_element_link (priv->video_display_bin, priv->photo_save_bin);
1255
 
  if (!disp_bin_linked)
1256
 
  {
1257
 
    gst_bin_remove (GST_BIN (priv->pipeline), priv->video_display_bin);
1258
 
    return FALSE;
1259
 
  }
1260
 
 
1261
 
  if (pipeline_was_playing)
1262
 
  {
1263
 
    cheese_webcam_play (webcam);
1264
 
  }
1265
 
 
1266
 
  /* if (was_recording)
1267
 
   * {
1268
 
   * Restart recording... ?
1269
 
   * } */
1270
 
 
1271
 
  return TRUE;
1272
 
}
1273
 
 
1274
 
void
1275
 
cheese_webcam_play (CheeseWebcam *webcam)
1276
 
{
1277
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1278
 
 
1279
 
  gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1280
 
  priv->pipeline_is_playing = TRUE;
1281
 
}
1282
 
 
1283
 
void
1284
 
cheese_webcam_stop (CheeseWebcam *webcam)
1285
 
{
1286
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1287
 
 
1288
 
  gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1289
 
  priv->pipeline_is_playing = FALSE;
1290
 
}
1291
 
 
1292
 
static void
1293
 
cheese_webcam_change_effect_filter (CheeseWebcam *webcam, GstElement *new_filter)
1294
 
{
1295
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1296
 
 
1297
 
  gboolean is_playing = priv->pipeline_is_playing;
1298
 
  gboolean ok;
1299
 
 
1300
 
  cheese_webcam_stop (webcam);
1301
 
 
1302
 
  gst_element_unlink_many (priv->webcam_source_bin, priv->effect_filter,
1303
 
                           priv->csp_post_effect, NULL);
1304
 
 
1305
 
  gst_bin_remove (GST_BIN (priv->video_display_bin), priv->effect_filter);
1306
 
 
1307
 
  gst_bin_add (GST_BIN (priv->video_display_bin), new_filter);
1308
 
  ok = gst_element_link_many (priv->webcam_source_bin, new_filter,
1309
 
                              priv->csp_post_effect, NULL);
1310
 
  g_return_if_fail (ok);
1311
 
 
1312
 
  if (is_playing)
1313
 
    cheese_webcam_play (webcam);
1314
 
 
1315
 
  priv->effect_filter = new_filter;
1316
 
}
1317
 
 
1318
 
void
1319
 
cheese_webcam_set_effect (CheeseWebcam *webcam, CheeseWebcamEffect effect)
1320
 
{
1321
 
  GString    *rgb_effects_str = g_string_new ("");
1322
 
  GString    *yuv_effects_str = g_string_new ("");
1323
 
  char       *effects_pipeline_desc;
1324
 
  int         i;
1325
 
  GstElement *effect_filter;
1326
 
  GError     *err = NULL;
1327
 
 
1328
 
  for (i = 0; i < NUM_EFFECTS; i++)
1329
 
  {
1330
 
    if (effect & EFFECT_TO_PIPELINE_DESC[i].effect)
1331
 
    {
1332
 
      if (EFFECT_TO_PIPELINE_DESC[i].colorspace == RGB)
1333
 
      {
1334
 
        g_string_append (rgb_effects_str, EFFECT_TO_PIPELINE_DESC[i].pipeline_desc);
1335
 
        g_string_append (rgb_effects_str, " ! ");
1336
 
      }
1337
 
      else
1338
 
      {
1339
 
        g_string_append (yuv_effects_str, " ! ");
1340
 
        g_string_append (yuv_effects_str, EFFECT_TO_PIPELINE_DESC[i].pipeline_desc);
1341
 
      }
1342
 
    }
1343
 
  }
1344
 
  effects_pipeline_desc = g_strconcat ("ffmpegcolorspace ! ",
1345
 
                                       rgb_effects_str->str,
1346
 
                                       "ffmpegcolorspace",
1347
 
                                       yuv_effects_str->str,
1348
 
                                       NULL);
1349
 
 
1350
 
  effect_filter = gst_parse_bin_from_description (effects_pipeline_desc, TRUE, &err);
1351
 
  if (!effect_filter || (err != NULL))
1352
 
  {
1353
 
    g_error_free (err);
1354
 
    g_error ("ERROR effect_filter\n");
1355
 
  }
1356
 
  cheese_webcam_change_effect_filter (webcam, effect_filter);
1357
 
 
1358
 
  g_free (effects_pipeline_desc);
1359
 
  g_string_free (rgb_effects_str, TRUE);
1360
 
  g_string_free (yuv_effects_str, TRUE);
1361
 
}
1362
 
 
1363
 
void
1364
 
cheese_webcam_start_video_recording (CheeseWebcam *webcam, char *filename)
1365
 
{
1366
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1367
 
 
1368
 
  g_object_set (CHEESE_WEBCAM_GET_PRIVATE (webcam)->video_file_sink, "location", filename, NULL);
1369
 
  cheese_webcam_change_sink (webcam, priv->video_display_bin,
1370
 
                             priv->video_save_bin, priv->photo_save_bin);
1371
 
  priv->is_recording = TRUE;
1372
 
}
1373
 
 
1374
 
static gboolean
1375
 
cheese_webcam_force_stop_video_recording (gpointer data)
1376
 
{
1377
 
  CheeseWebcam        *webcam = CHEESE_WEBCAM (data);
1378
 
  CheeseWebcamPrivate *priv   = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1379
 
 
1380
 
  if (priv->is_recording)
1381
 
  {
1382
 
    g_print ("Cannot cleanly shutdown recording pipeline, forcing\n");
1383
 
    g_signal_emit (webcam, webcam_signals[VIDEO_SAVED], 0);
1384
 
 
1385
 
    cheese_webcam_change_sink (webcam, priv->video_display_bin,
1386
 
                               priv->photo_save_bin, priv->video_save_bin);
1387
 
    priv->is_recording = FALSE;
1388
 
  }
1389
 
 
1390
 
  return FALSE;
1391
 
}
1392
 
 
1393
 
void
1394
 
cheese_webcam_stop_video_recording (CheeseWebcam *webcam)
1395
 
{
1396
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1397
 
  GstState             state;
1398
 
 
1399
 
  gst_element_get_state (priv->pipeline, &state, NULL, 0);
1400
 
 
1401
 
  if (state == GST_STATE_PLAYING)
1402
 
  {
1403
 
    /* Send EOS message down the pipeline by stopping video and audio source*/
1404
 
    g_print ("Sending EOS event down the recording pipeline\n");
1405
 
    gst_element_send_event (priv->video_source, gst_event_new_eos ());
1406
 
    gst_element_send_event (priv->audio_source, gst_event_new_eos ());
1407
 
    priv->eos_timeout_id = g_timeout_add (3000, cheese_webcam_force_stop_video_recording, webcam);
1408
 
  }
1409
 
  else
1410
 
  {
1411
 
    cheese_webcam_force_stop_video_recording (webcam);
1412
 
  }
1413
 
}
1414
 
 
1415
 
gboolean
1416
 
cheese_webcam_take_photo (CheeseWebcam *webcam, char *filename)
1417
 
{
1418
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1419
 
 
1420
 
  if (priv->photo_handler_signal_id != 0)
1421
 
  {
1422
 
    g_print ("Still waiting for previous photo data, ignoring new request\n");
1423
 
    return FALSE;
1424
 
  }
1425
 
 
1426
 
  g_free (priv->photo_filename);
1427
 
  priv->photo_filename = g_strdup (filename);
1428
 
 
1429
 
  /* Take the photo by connecting the handoff signal */
1430
 
  priv->photo_handler_signal_id = g_signal_connect (G_OBJECT (priv->photo_sink),
1431
 
                                                    "handoff",
1432
 
                                                    G_CALLBACK (cheese_webcam_photo_data_cb),
1433
 
                                                    webcam);
1434
 
  return TRUE;
1435
 
}
1436
 
 
1437
 
static void
1438
 
cheese_webcam_finalize (GObject *object)
1439
 
{
1440
 
  CheeseWebcam *webcam;
1441
 
  int           i, j;
1442
 
 
1443
 
  webcam = CHEESE_WEBCAM (object);
1444
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1445
 
 
1446
 
  cheese_webcam_stop (webcam);
1447
 
  gst_object_unref (priv->pipeline);
1448
 
 
1449
 
  if (priv->is_recording)
1450
 
    gst_object_unref (priv->photo_save_bin);
1451
 
  else
1452
 
    gst_object_unref (priv->video_save_bin);
1453
 
 
1454
 
  g_free (priv->photo_filename);
1455
 
  g_free (priv->device_name);
1456
 
 
1457
 
  /* Free CheeseWebcamDevice array */
1458
 
  for (i = 0; i < priv->num_webcam_devices; i++)
1459
 
  {
1460
 
    for (j = 0; j < priv->webcam_devices[i].num_video_formats; j++)
1461
 
    {
1462
 
      g_free (g_array_index (priv->webcam_devices[i].video_formats, CheeseVideoFormat, j).framerates);
1463
 
      g_free (g_array_index (priv->webcam_devices[i].video_formats, CheeseVideoFormat, j).mimetype);
1464
 
    }
1465
 
    g_free (priv->webcam_devices[i].video_device);
1466
 
    g_free (priv->webcam_devices[i].hal_udi);
1467
 
    g_free (priv->webcam_devices[i].gstreamer_src);
1468
 
    g_free (priv->webcam_devices[i].product_name);
1469
 
    g_array_free (priv->webcam_devices[i].video_formats, TRUE);
1470
 
    g_hash_table_destroy (priv->webcam_devices[i].supported_resolutions);
1471
 
  }
1472
 
  g_free (priv->webcam_devices);
1473
 
 
1474
 
  G_OBJECT_CLASS (cheese_webcam_parent_class)->finalize (object);
1475
 
}
1476
 
 
1477
 
static void
1478
 
cheese_webcam_get_property (GObject *object, guint prop_id, GValue *value,
1479
 
                            GParamSpec *pspec)
1480
 
{
1481
 
  CheeseWebcam *self;
1482
 
 
1483
 
  self = CHEESE_WEBCAM (object);
1484
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (self);
1485
 
 
1486
 
  switch (prop_id)
1487
 
  {
1488
 
    case PROP_VIDEO_WINDOW:
1489
 
      g_value_set_pointer (value, priv->video_window);
1490
 
      break;
1491
 
    case PROP_DEVICE_NAME:
1492
 
      g_value_set_string (value, priv->device_name);
1493
 
      break;
1494
 
    case PROP_X_RESOLUTION:
1495
 
      g_value_set_int (value, priv->x_resolution);
1496
 
      break;
1497
 
    case PROP_Y_RESOLUTION:
1498
 
      g_value_set_int (value, priv->y_resolution);
1499
 
      break;
1500
 
    default:
1501
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1502
 
      break;
1503
 
  }
1504
 
}
1505
 
 
1506
 
static void
1507
 
cheese_webcam_set_property (GObject *object, guint prop_id, const GValue *value,
1508
 
                            GParamSpec *pspec)
1509
 
{
1510
 
  CheeseWebcam *self;
1511
 
 
1512
 
  self = CHEESE_WEBCAM (object);
1513
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (self);
1514
 
 
1515
 
  switch (prop_id)
1516
 
  {
1517
 
    case PROP_VIDEO_WINDOW:
1518
 
      priv->video_window = g_value_get_pointer (value);
1519
 
      g_signal_connect (priv->video_window, "expose-event",
1520
 
                        G_CALLBACK (cheese_webcam_expose_cb), self);
1521
 
      break;
1522
 
    case PROP_DEVICE_NAME:
1523
 
      g_free (priv->device_name);
1524
 
      priv->device_name = g_value_dup_string (value);
1525
 
      break;
1526
 
    case PROP_X_RESOLUTION:
1527
 
      priv->x_resolution = g_value_get_int (value);
1528
 
      break;
1529
 
    case PROP_Y_RESOLUTION:
1530
 
      priv->y_resolution = g_value_get_int (value);
1531
 
      break;
1532
 
    default:
1533
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1534
 
      break;
1535
 
  }
1536
 
}
1537
 
 
1538
 
static void
1539
 
cheese_webcam_class_init (CheeseWebcamClass *klass)
1540
 
{
1541
 
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
1542
 
 
1543
 
  object_class->finalize     = cheese_webcam_finalize;
1544
 
  object_class->get_property = cheese_webcam_get_property;
1545
 
  object_class->set_property = cheese_webcam_set_property;
1546
 
 
1547
 
  webcam_signals[PHOTO_SAVED] = g_signal_new ("photo-saved", G_OBJECT_CLASS_TYPE (klass),
1548
 
                                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1549
 
                                              G_STRUCT_OFFSET (CheeseWebcamClass, photo_saved),
1550
 
                                              NULL, NULL,
1551
 
                                              g_cclosure_marshal_VOID__VOID,
1552
 
                                              G_TYPE_NONE, 0);
1553
 
 
1554
 
  webcam_signals[VIDEO_SAVED] = g_signal_new ("video-saved", G_OBJECT_CLASS_TYPE (klass),
1555
 
                                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1556
 
                                              G_STRUCT_OFFSET (CheeseWebcamClass, video_saved),
1557
 
                                              NULL, NULL,
1558
 
                                              g_cclosure_marshal_VOID__VOID,
1559
 
                                              G_TYPE_NONE, 0);
1560
 
 
1561
 
 
1562
 
  g_object_class_install_property (object_class, PROP_VIDEO_WINDOW,
1563
 
                                   g_param_spec_pointer ("video-window",
1564
 
                                                         NULL,
1565
 
                                                         NULL,
1566
 
                                                         G_PARAM_READWRITE));
1567
 
 
1568
 
  g_object_class_install_property (object_class, PROP_DEVICE_NAME,
1569
 
                                   g_param_spec_string ("device-name",
1570
 
                                                        NULL,
1571
 
                                                        NULL,
1572
 
                                                        "",
1573
 
                                                        G_PARAM_READWRITE));
1574
 
 
1575
 
  g_object_class_install_property (object_class, PROP_X_RESOLUTION,
1576
 
                                   g_param_spec_int ("x-resolution",
1577
 
                                                     NULL,
1578
 
                                                     NULL,
1579
 
                                                     0,
1580
 
                                                     G_MAXINT,
1581
 
                                                     0,
1582
 
                                                     G_PARAM_READWRITE |
1583
 
                                                     G_PARAM_CONSTRUCT_ONLY));
1584
 
 
1585
 
  g_object_class_install_property (object_class, PROP_Y_RESOLUTION,
1586
 
                                   g_param_spec_int ("y-resolution",
1587
 
                                                     NULL,
1588
 
                                                     NULL,
1589
 
                                                     0,
1590
 
                                                     G_MAXINT,
1591
 
                                                     0,
1592
 
                                                     G_PARAM_READWRITE |
1593
 
                                                     G_PARAM_CONSTRUCT_ONLY));
1594
 
 
1595
 
 
1596
 
  g_type_class_add_private (klass, sizeof (CheeseWebcamPrivate));
1597
 
}
1598
 
 
1599
 
static void
1600
 
cheese_webcam_init (CheeseWebcam *webcam)
1601
 
{
1602
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1603
 
 
1604
 
  priv->is_recording            = FALSE;
1605
 
  priv->pipeline_is_playing     = FALSE;
1606
 
  priv->photo_filename          = NULL;
1607
 
  priv->webcam_devices          = NULL;
1608
 
  priv->device_name             = NULL;
1609
 
  priv->photo_handler_signal_id = 0;
1610
 
}
1611
 
 
1612
 
CheeseWebcam *
1613
 
cheese_webcam_new (GtkWidget *video_window, char *webcam_device_name,
1614
 
                   int x_resolution, int y_resolution)
1615
 
{
1616
 
  CheeseWebcam *webcam;
1617
 
 
1618
 
  if (webcam_device_name)
1619
 
  {
1620
 
    webcam = g_object_new (CHEESE_TYPE_WEBCAM, "video-window", video_window,
1621
 
                           "device_name", webcam_device_name,
1622
 
                           "x-resolution", x_resolution,
1623
 
                           "y-resolution", y_resolution, NULL);
1624
 
  }
1625
 
  else
1626
 
  {
1627
 
    webcam = g_object_new (CHEESE_TYPE_WEBCAM, "video-window", video_window,
1628
 
                           "x-resolution", x_resolution,
1629
 
                           "y-resolution", y_resolution, NULL);
1630
 
  }
1631
 
 
1632
 
  return webcam;
1633
 
}
1634
 
 
1635
 
void
1636
 
cheese_webcam_setup (CheeseWebcam *webcam, char *hal_dev_udi, GError **error)
1637
 
{
1638
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1639
 
 
1640
 
  gboolean ok        = TRUE;
1641
 
  GError  *tmp_error = NULL;
1642
 
 
1643
 
  cheese_webcam_detect_webcam_devices (webcam);
1644
 
 
1645
 
  if (hal_dev_udi != NULL)
1646
 
  {
1647
 
    cheese_webcam_set_device_by_dev_udi (webcam, hal_dev_udi);
1648
 
  }
1649
 
 
1650
 
  priv->pipeline = gst_pipeline_new ("pipeline");
1651
 
 
1652
 
  cheese_webcam_create_video_display_bin (webcam, &tmp_error);
1653
 
 
1654
 
  cheese_webcam_create_photo_save_bin (webcam, &tmp_error);
1655
 
 
1656
 
  cheese_webcam_create_video_save_bin (webcam, &tmp_error);
1657
 
  if (tmp_error != NULL)
1658
 
  {
1659
 
    g_propagate_error (error, tmp_error);
1660
 
    g_prefix_error (error, _("One or more needed gstreamer elements are missing: "));
1661
 
    return;
1662
 
  }
1663
 
 
1664
 
  gst_bin_add_many (GST_BIN (priv->pipeline), priv->video_display_bin,
1665
 
                    priv->photo_save_bin, NULL);
1666
 
 
1667
 
  ok = gst_element_link (priv->video_display_bin, priv->photo_save_bin);
1668
 
 
1669
 
  priv->bus = gst_element_get_bus (priv->pipeline);
1670
 
  gst_bus_add_signal_watch (priv->bus);
1671
 
 
1672
 
  g_signal_connect (G_OBJECT (priv->bus), "message",
1673
 
                    G_CALLBACK (cheese_webcam_bus_message_cb), webcam);
1674
 
 
1675
 
  gst_bus_set_sync_handler (priv->bus, (GstBusSyncHandler) cheese_webcam_bus_sync_handler, webcam);
1676
 
 
1677
 
  if (!ok)
1678
 
    g_error ("Unable link pipeline for photo");
1679
 
}
1680
 
 
1681
 
int
1682
 
cheese_webcam_get_selected_device_index (CheeseWebcam *webcam)
1683
 
{
1684
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1685
 
 
1686
 
  return priv->selected_device;
1687
 
}
1688
 
 
1689
 
GArray *
1690
 
cheese_webcam_get_webcam_devices (CheeseWebcam *webcam)
1691
 
{
1692
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1693
 
 
1694
 
  GArray *devices_arr;
1695
 
 
1696
 
  devices_arr = g_array_sized_new (FALSE,
1697
 
                                   TRUE,
1698
 
                                   sizeof (CheeseWebcamDevice),
1699
 
                                   priv->num_webcam_devices);
1700
 
  devices_arr = g_array_append_vals (devices_arr,
1701
 
                                     priv->webcam_devices,
1702
 
                                     priv->num_webcam_devices);
1703
 
  return devices_arr;
1704
 
}
1705
 
 
1706
 
void
1707
 
cheese_webcam_set_device_by_dev_file (CheeseWebcam *webcam, char *file)
1708
 
{
1709
 
  g_object_set (webcam, "device_name", file, NULL);
1710
 
}
1711
 
 
1712
 
void
1713
 
cheese_webcam_set_device_by_dev_udi (CheeseWebcam *webcam, char *udi)
1714
 
{
1715
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1716
 
 
1717
 
  int i;
1718
 
 
1719
 
  for (i = 0; i < priv->num_webcam_devices; i++)
1720
 
  {
1721
 
    if (strcmp (priv->webcam_devices[i].hal_udi, udi) == 0)
1722
 
    {
1723
 
      g_object_set (webcam, "device_name", priv->webcam_devices[i].video_device, NULL);
1724
 
      break;
1725
 
    }
1726
 
  }
1727
 
}
1728
 
 
1729
 
GArray *
1730
 
cheese_webcam_get_video_formats (CheeseWebcam *webcam)
1731
 
{
1732
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1733
 
 
1734
 
  return priv->webcam_devices[priv->selected_device].video_formats;
1735
 
}
1736
 
 
1737
 
void
1738
 
cheese_webcam_set_video_format (CheeseWebcam *webcam, CheeseVideoFormat *format)
1739
 
{
1740
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1741
 
 
1742
 
  GstCaps *new_caps;
1743
 
 
1744
 
  new_caps = gst_caps_new_simple ("video/x-raw-rgb",
1745
 
                                  "width", G_TYPE_INT,
1746
 
                                  format->width,
1747
 
                                  "height", G_TYPE_INT,
1748
 
                                  format->height,
1749
 
                                  "framerate", GST_TYPE_FRACTION,
1750
 
                                  format->highest_framerate.numerator,
1751
 
                                  format->highest_framerate.denominator,
1752
 
                                  NULL);
1753
 
 
1754
 
  gst_caps_append (new_caps, gst_caps_new_simple ("video/x-raw-yuv",
1755
 
                                                  "width", G_TYPE_INT,
1756
 
                                                  format->width,
1757
 
                                                  "height", G_TYPE_INT,
1758
 
                                                  format->height,
1759
 
                                                  "framerate", GST_TYPE_FRACTION,
1760
 
                                                  format->highest_framerate.numerator,
1761
 
                                                  format->highest_framerate.denominator,
1762
 
                                                  NULL));
1763
 
 
1764
 
  priv->current_format = format;
1765
 
 
1766
 
  cheese_webcam_stop (webcam);
1767
 
  g_object_set (priv->capsfilter, "caps", new_caps, NULL);
1768
 
  cheese_webcam_play (webcam);
1769
 
}
1770
 
 
1771
 
CheeseVideoFormat *
1772
 
cheese_webcam_get_current_video_format (CheeseWebcam *webcam)
1773
 
{
1774
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1775
 
 
1776
 
  return priv->current_format;
1777
 
}
1778
 
 
1779
 
void
1780
 
cheese_webcam_get_balance_property_range (CheeseWebcam *webcam,
1781
 
                                          gchar *property,
1782
 
                                          gdouble *min, gdouble *max, gdouble *def)
1783
 
{
1784
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1785
 
  GParamSpec          *pspec;
1786
 
 
1787
 
  *min = 0.0;
1788
 
  *max = 0.0;
1789
 
  *def = 0.0;
1790
 
 
1791
 
  pspec = g_object_class_find_property (
1792
 
    G_OBJECT_GET_CLASS (G_OBJECT (priv->video_balance)), property);
1793
 
 
1794
 
  g_return_if_fail (pspec != NULL);
1795
 
 
1796
 
  *min = G_PARAM_SPEC_DOUBLE (pspec)->minimum;
1797
 
  *max = G_PARAM_SPEC_DOUBLE (pspec)->maximum;
1798
 
  *def = G_PARAM_SPEC_DOUBLE (pspec)->default_value;
1799
 
}
1800
 
 
1801
 
void
1802
 
cheese_webcam_set_balance_property (CheeseWebcam *webcam, gchar *property, gdouble value)
1803
 
{
1804
 
  CheeseWebcamPrivate *priv = CHEESE_WEBCAM_GET_PRIVATE (webcam);
1805
 
 
1806
 
  g_object_set (G_OBJECT (priv->video_balance), property, value, NULL);
1807
 
}