gstv4l2object.c 15.4 KB
Newer Older
1 2
/* GStreamer
 *
3
 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 5 6
 *               2006 Edgard Lima <edgard.lima@indt.org.br>
 *
 * gstv4l2object.c: base class for V4L2 elements
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as published
 * by the Free Software Foundation; either version 2 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 Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA. 
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

31 32 33 34
#ifdef HAVE_GUDEV
#include <gudev/gudev.h>
#endif

35 36
#include "v4l2_calls.h"
#include "gstv4l2tuner.h"
37
#if 0                           /* overlay is still not implemented #ifdef HAVE_XVIDEO */
38 39 40 41
#include "gstv4l2xoverlay.h"
#endif
#include "gstv4l2colorbalance.h"

42 43
#define DEFAULT_PROP_DEVICE		"/dev/video0"
#define DEFAULT_PROP_DEVICE_NAME 	NULL
44
#define DEFAULT_PROP_DEVICE_FD          -1
45
#define DEFAULT_PROP_FLAGS              0
46 47
#define DEFAULT_PROP_NORM               NULL
#define DEFAULT_PROP_CHANNEL            NULL
48 49
#define DEFAULT_PROP_FREQUENCY          0

Stefan Kost's avatar
Stefan Kost committed
50 51 52 53 54 55
enum
{
  PROP_0,
  V4L2_STD_OBJECT_PROPS,
};

56 57 58
GST_DEBUG_CATEGORY_EXTERN (v4l2src_debug);
#define GST_CAT_DEFAULT v4l2src_debug

Stefan Kost's avatar
Stefan Kost committed
59
const GList *
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
{
  GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
  static GList *list = NULL;

  /* well, not perfect, but better than no locking at all.
   * In the worst case we leak a list node, so who cares? */
  GST_CLASS_LOCK (GST_OBJECT_CLASS (klass));

  if (!list) {
    list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
  }

  GST_CLASS_UNLOCK (GST_OBJECT_CLASS (klass));

  return list;
}

78 79 80 81
static gboolean init = FALSE;
static GList *devices = NULL;

#ifdef HAVE_GUDEV
82
static gboolean
83
gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
84 85
    GList ** klass_devices)
{
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
  GUdevClient *client;
  GList *item;

  if (!check) {
    while (devices) {
      gchar *device = devices->data;
      devices = g_list_remove (devices, device);
      g_free (device);
    }

    GST_INFO ("Enumerating video4linux devices from udev");
    client = g_udev_client_new (NULL);
    if (!client) {
      GST_WARNING ("Failed to initialize gudev client");
      goto finish;
    }

    item = g_udev_client_query_by_subsystem (client, "video4linux");
    while (item) {
      GUdevDevice *device = item->data;
      gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
      gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
      GST_INFO ("Found new device: %s, API: %d", devnode, api);
      /* Append v4l2 devices only. If api is 0 probably v4l_id has
         been stripped out of the current udev installation, append
         anyway */
      if (api == 0) {
        GST_WARNING
            ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
      }
      if ((api == 2 || api == 0)) {
        devices = g_list_append (devices, devnode);
      } else {
        g_free (devnode);
      }
      g_object_unref (device);
      item = item->next;
    }
    g_list_free (item);
    init = TRUE;
  }

finish:
  if (client) {
    g_object_unref (client);
  }

  *klass_devices = devices;
134

135 136 137 138 139 140 141 142
  return init;
}
#endif /* HAVE_GUDEV */

static gboolean
gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
    GList ** klass_devices)
{
143
  if (!check) {
Stefan Kost's avatar
Stefan Kost committed
144
    const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
145 146 147
    gint base, n, fd;

    while (devices) {
148
      gchar *device = devices->data;
149
      devices = g_list_remove (devices, device);
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
      g_free (device);
    }

    /*
     * detect /dev entries 
     */
    for (n = 0; n < 64; n++) {
      for (base = 0; dev_base[base] != NULL; base++) {
        struct stat s;
        gchar *device = g_strdup_printf ("%s%d",
            dev_base[base],
            n);

        /*
         * does the /dev/ entry exist at all? 
         */
        if (stat (device, &s) == 0) {
          /*
           * yes: is a device attached? 
           */
          if (S_ISCHR (s.st_mode)) {

            if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
              if (fd > 0)
                close (fd);

              devices = g_list_append (devices, device);
              break;
            }
          }
        }
        g_free (device);
      }
    }
    init = TRUE;
  }

  *klass_devices = devices;

  return init;
}

void
gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
    guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);

  switch (prop_id) {
    case PROP_DEVICE:
200 201 202 203
#ifdef HAVE_GUDEV
      if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
        gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
#else /* !HAVE_GUDEV */
204
      gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
205
#endif /* HAVE_GUDEV */
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
      break;
  }
}

gboolean
gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
    guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
  gboolean ret = FALSE;

  switch (prop_id) {
    case PROP_DEVICE:
222 223 224 225
#ifdef HAVE_GUDEV
      ret =
          !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
#else /* !HAVE_GUDEV */
226
      ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
227
#endif /* HAVE_GUDEV */
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
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
      break;
  }
  return ret;
}

static GValueArray *
gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
{
  GValueArray *array;
  GValue value = { 0 };
  GList *item;

  if (!*klass_devices)
    return NULL;

  array = g_value_array_new (g_list_length (*klass_devices));
  item = *klass_devices;
  g_value_init (&value, G_TYPE_STRING);
  while (item) {
    gchar *device = item->data;

    g_value_set_string (&value, device);
    g_value_array_append (array, &value);

    item = item->next;
  }
  g_value_unset (&value);

  return array;
}

GValueArray *
gst_v4l2_probe_get_values (GstPropertyProbe * probe,
    guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
  GValueArray *array = NULL;

  switch (prop_id) {
    case PROP_DEVICE:
      array = gst_v4l2_class_list_devices (klass, klass_devices);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
      break;
  }

  return array;
}

#define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
GType
gst_v4l2_device_get_type (void)
{
  static GType v4l2_device_type = 0;

  if (v4l2_device_type == 0) {
    static const GFlagsValue values[] = {
289 290 291 292 293 294 295 296 297
      {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
      {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
      {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},

      {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
      {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},

      {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
      {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
298 299 300 301 302 303 304 305 306 307 308 309

      {0, NULL, NULL}
    };

    v4l2_device_type =
        g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
  }

  return v4l2_device_type;
}

void
310
gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class)
311
{
312 313 314 315 316 317
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device", "Device location",
          DEFAULT_PROP_DEVICE, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
          "Name of the device", DEFAULT_PROP_DEVICE_NAME, G_PARAM_READABLE));
318 319 320 321
  g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
      g_param_spec_int ("device-fd", "File descriptor",
          "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
          G_PARAM_READABLE));
322 323 324
  g_object_class_install_property (gobject_class, PROP_FLAGS,
      g_param_spec_flags ("flags", "Flags", "Device type flags",
          GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS, G_PARAM_READABLE));
325 326 327
}

GstV4l2Object *
328
gst_v4l2_object_new (GstElement * element,
329
    GstV4l2GetInOutFunction get_in_out_func,
330 331
    GstV4l2SetInOutFunction set_in_out_func,
    GstV4l2UpdateFpsFunction update_fps_func)
332 333 334 335 336 337 338 339 340 341 342
{
  GstV4l2Object *v4l2object;

  /*
   * some default values 
   */
  v4l2object = g_new0 (GstV4l2Object, 1);

  v4l2object->element = element;
  v4l2object->get_in_out_func = get_in_out_func;
  v4l2object->set_in_out_func = set_in_out_func;
343
  v4l2object->update_fps_func = update_fps_func;
344 345

  v4l2object->video_fd = -1;
346
  v4l2object->poll = gst_poll_new (TRUE);
347
  v4l2object->buffer = NULL;
348
  v4l2object->videodev = g_strdup (DEFAULT_PROP_DEVICE);
349

350 351
  v4l2object->norms = NULL;
  v4l2object->channels = NULL;
352 353 354 355 356 357 358 359
  v4l2object->colors = NULL;

  v4l2object->xwindow_id = 0;

  return v4l2object;
}

void
360
gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
361
{
362
  g_return_if_fail (v4l2object != NULL);
363

364 365 366
  if (v4l2object->videodev)
    g_free (v4l2object->videodev);

367 368 369
  if (v4l2object->poll)
    gst_poll_free (v4l2object->poll);

370 371 372 373 374 375
  if (v4l2object->channel)
    g_free (v4l2object->channel);

  if (v4l2object->norm)
    g_free (v4l2object->norm);

376
  g_free (v4l2object);
377 378 379
}

gboolean
380
gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
381 382 383 384
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
Wim Taymans's avatar
Wim Taymans committed
385
      g_free (v4l2object->videodev);
386
      v4l2object->videodev = g_value_dup_string (value);
387
      break;
388
#if 0
389
    case PROP_NORM:
390 391 392
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerNorm *norm = gst_tuner_find_norm_by_name (tuner,
393
            (gchar *) g_value_get_string (value));
394 395 396 397 398 399 400

        if (norm) {
          /* like gst_tuner_set_norm (tuner, norm)
             without g_object_notify */
          gst_v4l2_tuner_set_norm (v4l2object, norm);
        }
      } else {
401 402
        g_free (v4l2object->norm);
        v4l2object->norm = g_value_dup_string (value);
403 404
      }
      break;
405
    case PROP_CHANNEL:
406 407 408
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
409
            (gchar *) g_value_get_string (value));
410 411 412 413 414 415 416

        if (channel) {
          /* like gst_tuner_set_channel (tuner, channel)
             without g_object_notify */
          gst_v4l2_tuner_set_channel (v4l2object, channel);
        }
      } else {
417 418
        g_free (v4l2object->channel);
        v4l2object->channel = g_value_dup_string (value);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
      }
      break;
    case PROP_FREQUENCY:
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_get_channel (tuner);

        if (channel &&
            GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
          /* like
             gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
             without g_object_notify */
          gst_v4l2_tuner_set_frequency (v4l2object, channel,
              g_value_get_ulong (value));
        }
      } else {
        v4l2object->frequency = g_value_get_ulong (value);
      }
      break;
438
#endif
439 440 441 442 443 444 445 446 447
    default:
      return FALSE;
      break;
  }
  return TRUE;
}


gboolean
448
gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
449 450 451 452 453 454 455 456
    guint prop_id, GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
      g_value_set_string (value, v4l2object->videodev);
      break;
    case PROP_DEVICE_NAME:
    {
457
      const guchar *new = NULL;
458

459
      if (GST_V4L2_IS_OPEN (v4l2object)) {
460
        new = v4l2object->vcap.card;
461
      } else if (gst_v4l2_open (v4l2object)) {
462
        new = v4l2object->vcap.card;
463 464
        gst_v4l2_close (v4l2object);
      }
465
      g_value_set_string (value, (gchar *) new);
466 467
      break;
    }
468 469 470 471 472 473 474 475
    case PROP_DEVICE_FD:
    {
      if (GST_V4L2_IS_OPEN (v4l2object))
        g_value_set_int (value, v4l2object->video_fd);
      else
        g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
      break;
    }
476 477 478 479 480 481 482 483
    case PROP_FLAGS:
    {
      guint flags = 0;

      if (GST_V4L2_IS_OPEN (v4l2object)) {
        flags |= v4l2object->vcap.capabilities &
            (V4L2_CAP_VIDEO_CAPTURE |
            V4L2_CAP_VIDEO_OUTPUT |
Edgard Lima's avatar
Edgard Lima committed
484 485 486
            V4L2_CAP_VIDEO_OVERLAY |
            V4L2_CAP_VBI_CAPTURE |
            V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
      }
      g_value_set_flags (value, flags);
      break;
    }
    default:
      return FALSE;
      break;
  }
  return TRUE;
}

static void
gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
{
  GstTunerNorm *norm = NULL;
  GstTunerChannel *channel = NULL;
  GstTuner *tuner = GST_TUNER (v4l2object->element);

505 506
  if (v4l2object->norm)
    norm = gst_tuner_find_norm_by_name (tuner, v4l2object->norm);
507 508 509 510 511 512
  if (norm) {
    gst_tuner_set_norm (tuner, norm);
  } else {
    norm =
        GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
    if (norm) {
513 514
      g_free (v4l2object->norm);
      v4l2object->norm = g_strdup (norm->label);
515 516 517 518
      gst_tuner_norm_changed (tuner, norm);
    }
  }

519 520
  if (v4l2object->channel)
    channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
521 522 523 524
  if (channel) {
    gst_tuner_set_channel (tuner, channel);
  } else {
    channel =
525 526
        GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
            (v4l2object->element)));
527 528 529 530 531
    if (channel) {
      g_free (v4l2object->channel);
      v4l2object->channel = g_strdup (channel->label);
      gst_tuner_channel_changed (tuner, channel);
    }
532 533
  }

534 535
  if (channel
      && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
536 537 538 539 540 541 542 543 544 545 546 547 548 549
    if (v4l2object->frequency != 0) {
      gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
    } else {
      v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
      if (v4l2object->frequency == 0) {
        /* guess */
        gst_tuner_set_frequency (tuner, channel, 1000);
      } else {
      }
    }
  }
}

gboolean
550
gst_v4l2_object_start (GstV4l2Object * v4l2object)
551 552 553 554 555 556
{
  if (gst_v4l2_open (v4l2object))
    gst_v4l2_set_defaults (v4l2object);
  else
    return FALSE;

557
#if 0                           /* overlay is still not implemented #ifdef HAVE_XVIDEO */
558 559 560 561 562 563 564
  gst_v4l2_xoverlay_start (v4l2object);
#endif

  return TRUE;
}

gboolean
565
gst_v4l2_object_stop (GstV4l2Object * v4l2object)
566
{
567
#if 0                           /* overlay is still not implemented #ifdef HAVE_XVIDEO */
568 569 570 571 572 573 574 575
  gst_v4l2_xoverlay_stop (v4l2object);
#endif

  if (!gst_v4l2_close (v4l2object))
    return FALSE;

  return TRUE;
}