change NULL to (NULL) for GST_ELEMENT_ERROR
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2_calls.c
index c29951f..f7c3d3a 100644 (file)
 #include <sys/mman.h>
 #include <string.h>
 #include <errno.h>
+#include <unistd.h>
 #include "v4l2_calls.h"
+#include "gstv4l2tuner.h"
+#include "gstv4l2xoverlay.h"
+#include "gstv4l2colorbalance.h"
+
+#include "gstv4l2src.h"
 
 #define DEBUG(format, args...) \
-       GST_DEBUG_ELEMENT(GST_CAT_PLUGIN_INFO, \
+       GST_DEBUG_OBJECT (\
                GST_ELEMENT(v4l2element), \
                "V4L2: " format, ##args)
 
@@ -49,9 +55,9 @@ gst_v4l2_get_capabilities (GstV4l2Element *v4l2element)
        GST_V4L2_CHECK_OPEN(v4l2element);
 
        if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCAP, &(v4l2element->vcap)) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Error getting %s capabilities: %s",
-                       v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Error getting %s capabilities: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -69,104 +75,210 @@ static gboolean
 gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
 {
        gint n;
+       const GList *pads =
+               gst_element_get_pad_list (GST_ELEMENT (v4l2element));
+       GstPadDirection dir = GST_PAD_UNKNOWN;
 
        DEBUG("getting enumerations");
        GST_V4L2_CHECK_OPEN(v4l2element);
 
-       /* create enumeration lists - let's start with format enumeration */
-       for (n=0;;n++) {
-               struct v4l2_fmtdesc format, *fmtptr;
-               format.index = n;
-               if (ioctl(v4l2element->video_fd, VIDIOC_ENUM_PIXFMT, &format) < 0) {
-                       if (errno == EINVAL)
-                               break; /* end of enumeration */
-                       else {
-                               gst_element_error(GST_ELEMENT(v4l2element),
-                                       "Failed to get no. %d in pixelformat enumeration for %s: %s",
-                                       n, v4l2element->device, sys_errlist[errno]);
-                               return FALSE;
-                       }
-               }
-               fmtptr = g_malloc(sizeof(format));
-               memcpy(fmtptr, &format, sizeof(format));
-               v4l2element->formats = g_list_append(v4l2element->formats, fmtptr);
-       }
+       /* sinks have outputs, all others have inputs */
+       if (pads && g_list_length ((GList *) pads) == 1)
+               dir = GST_PAD_DIRECTION (GST_PAD (pads->data));
 
+       if (dir != GST_PAD_SINK) {
        /* and now, the inputs */
-       for (n=0;;n++) {
-               struct v4l2_input input, *inpptr;
-               input.index = n;
-               if (ioctl(v4l2element->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
-                       if (errno == EINVAL)
-                               break; /* end of enumeration */
-                       else {
-                               gst_element_error(GST_ELEMENT(v4l2element),
-                                       "Failed to get no. %d in input enumeration for %s: %s",
-                                       n, v4l2element->device, sys_errlist[errno]);
-                               return FALSE;
+               for (n=0;;n++) {
+                       struct v4l2_input input;
+                       GstV4l2TunerChannel *v4l2channel;
+                       GstTunerChannel *channel;
+
+                       input.index = n;
+                       if (ioctl(v4l2element->video_fd, VIDIOC_ENUMINPUT,
+                                 &input) < 0) {
+                               if (errno == EINVAL)
+                                       break; /* end of enumeration */
+                               else {
+                                       GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                                          ("Failed to get %d in input enumeration for %s: %s",
+                                               n, v4l2element->device,
+                                               g_strerror (errno)));
+                                       return FALSE;
+                               }
+                       }
+
+                       v4l2channel =
+                               g_object_new(GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
+                       channel = GST_TUNER_CHANNEL(v4l2channel);
+                       channel->label = g_strdup(input.name);
+                       channel->flags = GST_TUNER_CHANNEL_INPUT;
+                       v4l2channel->index = n;
+                       if (input.type == V4L2_INPUT_TYPE_TUNER) {
+                               struct v4l2_tuner vtun;
+
+                               v4l2channel->tuner = input.tuner;
+                               channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
+
+                               vtun.index = input.tuner;
+                               if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER,
+                                         &vtun) < 0) {
+                                       GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                               ("Failed to get tuner %d settings on %s: %s",
+                                               input.tuner,
+                                               v4l2element->device,
+                                               g_strerror (errno)));
+                                       g_object_unref(G_OBJECT(channel));
+                                       return FALSE;
+                               }
+                               channel->min_frequency = vtun.rangelow;
+                               channel->max_frequency = vtun.rangehigh;
+                               channel->min_signal = 0;
+                               channel->max_signal = 0xffff;
                        }
+                       if (input.audioset) {
+                               /* we take the first. We don't care for
+                                * the others for now */
+                               while (!(input.audioset &
+                                        (1<<v4l2channel->audio)))
+                                       v4l2channel->audio++;
+                               channel->flags |= GST_TUNER_CHANNEL_AUDIO;
+                       }
+
+                       v4l2element->channels =
+                               g_list_append(v4l2element->channels,
+                                             (gpointer) channel);
                }
-               inpptr = g_malloc(sizeof(input));
-               memcpy(inpptr, &input, sizeof(input));
-               v4l2element->inputs = g_list_append(v4l2element->inputs, inpptr);
-       }
+       } else {
+               /* outputs */
+               for (n=0;;n++) {
+                       struct v4l2_output output;
+                       GstV4l2TunerChannel *v4l2channel;
+                       GstTunerChannel *channel;
+
+                       output.index = n;
+                       if (ioctl(v4l2element->video_fd, VIDIOC_ENUMOUTPUT,
+                                 &output) < 0) {
+                               if (errno == EINVAL)
+                                       break; /* end of enumeration */
+                               else {
+                                       GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                                          ("Failed to get %d in output enumeration for %s: %s",
+                                               n, v4l2element->device,
+                                               g_strerror (errno)));
+                                       return FALSE;
+                               }
+                       }
 
-       /* outputs */
-       for (n=0;;n++) {
-               struct v4l2_output output, *outptr;
-               output.index = n;
-               if (ioctl(v4l2element->video_fd, VIDIOC_ENUMOUTPUT, &output) < 0) {
-                       if (errno == EINVAL)
-                               break; /* end of enumeration */
-                       else {
-                               gst_element_error(GST_ELEMENT(v4l2element),
-                                       "Failed to get no. %d in output enumeration for %s: %s",
-                                       n, v4l2element->device, sys_errlist[errno]);
-                               return FALSE;
+                       v4l2channel = g_object_new(GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
+                       channel = GST_TUNER_CHANNEL(v4l2channel);
+                       channel->label = g_strdup(output.name);
+                       channel->flags = GST_TUNER_CHANNEL_OUTPUT;
+                       v4l2channel->index = n;
+                       if (output.audioset) {
+                               /* we take the first. We don't care for
+                                * the others for now */
+                               while (!(output.audioset &
+                                        (1<<v4l2channel->audio)))
+                                       v4l2channel->audio++;
+                               channel->flags |= GST_TUNER_CHANNEL_AUDIO;
                        }
+
+                       v4l2element->channels =
+                               g_list_append(v4l2element->channels,
+                                             (gpointer) channel);
                }
-               outptr = g_malloc(sizeof(output));
-               memcpy(outptr, &output, sizeof(output));
-               v4l2element->outputs = g_list_append(v4l2element->outputs, outptr);
        }
 
        /* norms... */
        for (n=0;;n++) {
-               struct v4l2_enumstd standard, *stdptr;
+               struct v4l2_standard standard;
+               GstV4l2TunerNorm *v4l2norm;
+               GstTunerNorm *norm;
+
                standard.index = n;
                if (ioctl(v4l2element->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
                        if (errno == EINVAL)
                                break; /* end of enumeration */
                        else {
-                               gst_element_error(GST_ELEMENT(v4l2element),
-                                       "Failed to get no. %d in norm enumeration for %s: %s",
-                                       n, v4l2element->device, sys_errlist[errno]);
+                                       GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                                          ("Failed to get %d in norm enumeration for %s: %s",
+                                               n, v4l2element->device,
+                                               g_strerror (errno)));
                                return FALSE;
                        }
                }
-               stdptr = g_malloc(sizeof(standard));
-               memcpy(stdptr, &standard, sizeof(standard));
-               v4l2element->norms = g_list_append(v4l2element->norms, stdptr);
+
+               v4l2norm = g_object_new(GST_TYPE_V4L2_TUNER_NORM, NULL);
+               norm = GST_TUNER_NORM (v4l2norm);
+               norm->label = g_strdup(standard.name);
+               norm->fps = (gfloat) standard.frameperiod.denominator /
+                               standard.frameperiod.numerator;
+               v4l2norm->index = standard.id;
+
+               v4l2element->norms = g_list_append(v4l2element->norms,
+                                                  (gpointer) norm);
        }
 
        /* and lastly, controls+menus (if appropriate) */
-       for (n=0;;n++) {
-               struct v4l2_queryctrl control, *ctrlptr;
-               GList *menus = NULL;
+       for (n=V4L2_CID_BASE;;n++) {
+               struct v4l2_queryctrl control;
+               GstV4l2ColorBalanceChannel *v4l2channel;
+               GstColorBalanceChannel *channel;
+
+               /* hacky... */
+               if (n == V4L2_CID_LASTP1)
+                       n = V4L2_CID_PRIVATE_BASE;
+
                control.id = n;
                if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
-                       if (errno == EINVAL)
-                               break; /* end of enumeration */
-                       else {
-                               gst_element_error(GST_ELEMENT(v4l2element),
-                                       "Failed to get no. %d in control enumeration for %s: %s",
-                                       n, v4l2element->device, sys_errlist[errno]);
+                       if (errno == EINVAL) {
+                               if (n < V4L2_CID_PRIVATE_BASE)
+                                       continue;
+                               else
+                                       break;
+                       } else {
+                               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                                  ("Failed to get %d in control enumeration for %s: %s",
+                                               n, v4l2element->device,
+                                               g_strerror (errno)));
                                return FALSE;
                        }
                }
-               ctrlptr = g_malloc(sizeof(control));
-               memcpy(ctrlptr, &control, sizeof(control));
-               v4l2element->controls = g_list_append(v4l2element->controls, ctrlptr);
+               if (control.flags & V4L2_CTRL_FLAG_DISABLED)
+                       continue;
+
+               switch (n) {
+                       case V4L2_CID_BRIGHTNESS:
+                       case V4L2_CID_CONTRAST:
+                       case V4L2_CID_SATURATION:
+                       case V4L2_CID_HUE:
+                       case V4L2_CID_BLACK_LEVEL:
+                       case V4L2_CID_AUTO_WHITE_BALANCE:
+                       case V4L2_CID_DO_WHITE_BALANCE:
+                       case V4L2_CID_RED_BALANCE:
+                       case V4L2_CID_BLUE_BALANCE:
+                       case V4L2_CID_GAMMA:
+                       case V4L2_CID_EXPOSURE:
+                       case V4L2_CID_AUTOGAIN:
+                       case V4L2_CID_GAIN:
+                               /* we only handle these for now */
+                               break;
+                       default:
+                               DEBUG("ControlID %s (%d) unhandled, FIXME",
+                                     control.name, n);
+                               control.id++;
+                               break;
+               }
+               if (n != control.id)
+                       continue;
+
+               v4l2channel = g_object_new(GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL,
+                                          NULL);
+               channel = GST_COLOR_BALANCE_CHANNEL(v4l2channel);
+               channel->label = g_strdup(control.name);
+               v4l2channel->index = n;
+
+#if 0
                if (control.type == V4L2_CTRL_TYPE_MENU) {
                        struct v4l2_querymenu menu, *mptr;
                        int i;
@@ -177,9 +289,10 @@ gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
                                        if (errno == EINVAL)
                                                break; /* end of enumeration */
                                        else {
-                                               gst_element_error(GST_ELEMENT(v4l2element),
-                                                       "Failed to get no. %d in menu %d enumeration for %s: %s",
-                                                       i, n, v4l2element->device, sys_errlist[errno]);
+                                               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                                          ("Failed to get %d in menu enumeration for %s: %s",
+                                               n, v4l2element->device,
+                                               g_strerror (errno)));
                                                return FALSE;
                                        }
                                }
@@ -189,6 +302,25 @@ gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
                        }
                }
                v4l2element->menus = g_list_append(v4l2element->menus, menus);
+#endif
+
+               switch (control.type) {
+                       case V4L2_CTRL_TYPE_INTEGER:
+                               channel->min_value = control.minimum;
+                               channel->max_value = control.maximum;
+                               break;
+                       case V4L2_CTRL_TYPE_BOOLEAN:
+                               channel->min_value = FALSE;
+                               channel->max_value = TRUE;
+                               break;
+                       default:
+                               channel->min_value =
+                                       channel->max_value = 0;
+                               break;
+               }
+
+               v4l2element->colors = g_list_append(v4l2element->colors,
+                                                   (gpointer) channel);
        }
 
        return TRUE;
@@ -200,42 +332,60 @@ gst_v4l2_empty_lists (GstV4l2Element *v4l2element)
 {
        DEBUG("deleting enumerations");
 
-       /* empty lists */
-       while (g_list_length(v4l2element->inputs) > 0) {
-               gpointer data = g_list_nth_data(v4l2element->inputs, 0);
-               v4l2element->inputs = g_list_remove(v4l2element->inputs, data);
-               g_free(data);
-       }
-       while (g_list_length(v4l2element->outputs) > 0) {
-               gpointer data = g_list_nth_data(v4l2element->outputs, 0);
-               v4l2element->outputs = g_list_remove(v4l2element->outputs, data);
-               g_free(data);
-       }
-       while (g_list_length(v4l2element->norms) > 0) {
-               gpointer data = g_list_nth_data(v4l2element->norms, 0);
-               v4l2element->norms = g_list_remove(v4l2element->norms, data);
-               g_free(data);
-       }
-       while (g_list_length(v4l2element->formats) > 0) {
-               gpointer data = g_list_nth_data(v4l2element->formats, 0);
-               v4l2element->formats = g_list_remove(v4l2element->formats, data);
-               g_free(data);
-       }
-       while (g_list_length(v4l2element->controls) > 0) {
-               gpointer data = g_list_nth_data(v4l2element->controls, 0);
-               v4l2element->controls = g_list_remove(v4l2element->controls, data);
-               g_free(data);
-       }
-       v4l2element->menus = g_list_remove_all(v4l2element->menus, NULL);
-       while (g_list_length(v4l2element->menus) > 0) {
-               GList *items = (GList *) g_list_nth_data(v4l2element->menus, 0);
-               v4l2element->inputs = g_list_remove(v4l2element->inputs, items);
-               while (g_list_length(items) > 0) {
-                       gpointer data = g_list_nth_data(v4l2element->menus, 0);
-                       items = g_list_remove(items, data);
-                       g_free(data);
-               }
-       }
+       g_list_foreach (v4l2element->channels, (GFunc) g_object_unref, NULL);
+       g_list_free (v4l2element->channels);
+       v4l2element->channels = NULL;
+
+       g_list_foreach (v4l2element->norms, (GFunc) g_object_unref, NULL);
+       g_list_free (v4l2element->norms);
+       v4l2element->norms = NULL;
+
+       g_list_foreach (v4l2element->colors, (GFunc) g_object_unref, NULL);
+       g_list_free (v4l2element->colors);
+       v4l2element->colors = NULL;
+}
+
+/* FIXME: move this stuff to gstv4l2tuner.c? */
+
+static void
+gst_v4l2_set_defaults (GstV4l2Element *v4l2element)
+{
+  GstTunerNorm *norm = NULL;
+  GstTunerChannel *channel = NULL;
+  GstTuner *tuner = GST_TUNER (v4l2element);
+  
+  if (v4l2element->norm)
+    norm = gst_tuner_find_norm_by_name (tuner, v4l2element->norm);
+  if (norm) {
+    gst_tuner_set_norm (tuner, norm);
+  } else {
+    norm = GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2element)));
+    v4l2element->norm = g_strdup (norm->label);
+    gst_tuner_norm_changed (tuner, norm);
+    g_object_notify (G_OBJECT (v4l2element), "norm"); 
+  }
+  
+  if (v4l2element->channel) 
+    channel = gst_tuner_find_channel_by_name (tuner, v4l2element->channel);
+  if (channel) {
+    gst_tuner_set_channel (tuner, channel);
+  } else {
+    channel = GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER (v4l2element)));
+    v4l2element->channel = g_strdup (channel->label);
+    gst_tuner_channel_changed (tuner, channel);
+    g_object_notify (G_OBJECT (v4l2element), "channel"); 
+  }
+  if (v4l2element->frequency != 0) {
+    gst_tuner_set_frequency (tuner, channel, v4l2element->frequency);
+  } else {
+    v4l2element->frequency = gst_tuner_get_frequency (tuner, channel);
+    if (v4l2element->frequency == 0) {
+      /* guess */
+      gst_tuner_set_frequency (tuner, channel, 1000);
+    } else {
+      g_object_notify (G_OBJECT (v4l2element), "frequency");
+    }
+  }
 }
 
 
@@ -259,9 +409,9 @@ gst_v4l2_open (GstV4l2Element *v4l2element)
        /* open the device */
        v4l2element->video_fd = open(v4l2element->device, O_RDWR);
        if (!GST_V4L2_IS_OPEN(v4l2element)) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to open device %s: %s",
-                       v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, OPEN_READ_WRITE,
+                                   (_("Could not open device \"%s\" for reading and writing."), v4l2element->device),
+                                   GST_ERROR_SYSTEM);
                goto error;
        }
 
@@ -270,22 +420,24 @@ gst_v4l2_open (GstV4l2Element *v4l2element)
                goto error;
        }
 
-       /* and get the video window */
-       if (GST_V4L2_IS_OVERLAY(v4l2element)) {
-               if (ioctl(v4l2element->video_fd, VIDIOC_G_WIN, &(v4l2element->vwin)) < 0) {
-                       gst_element_error(GST_ELEMENT(v4l2element),
-                               "Failed to get video window properties of %s: %s",
-                               v4l2element->device, sys_errlist[errno]);
-                       goto error;
-               }
+       /* do we need to be a capture device? */
+       if (GST_IS_V4L2SRC(v4l2element) &&
+           !(v4l2element->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, NOT_FOUND,
+                                   (_("Device \"%s\" is not a capture device."), v4l2element->device),
+                                  ("Capabilities: 0x%x", v4l2element->vcap.capabilities));
+               goto error;
        }
 
        /* create enumerations */
        if (!gst_v4l2_fill_lists(v4l2element))
                goto error;
 
-       gst_info("Opened device '%s' (%s) successfully\n",
-               v4l2element->vcap.name, v4l2element->device);
+       /* set defaults */
+       gst_v4l2_set_defaults (v4l2element);
+
+       GST_INFO_OBJECT (v4l2element, "Opened device '%s' (%s) successfully\n",
+               v4l2element->vcap.card, v4l2element->device);
 
        return TRUE;
 
@@ -334,34 +486,19 @@ gst_v4l2_close (GstV4l2Element *v4l2element)
 
 gboolean
 gst_v4l2_get_norm (GstV4l2Element *v4l2element,
-                   gint           *norm)
+                   v4l2_std_id    *norm)
 {
-       struct v4l2_standard standard;
-       gint n;
-
        DEBUG("getting norm");
        GST_V4L2_CHECK_OPEN(v4l2element);
 
-       if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, &standard) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get the current norm for device %s: %s",
-                       v4l2element->device, sys_errlist[errno]);
+       if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, norm) < 0) {
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get the current norm for device %s: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
-       /* try to find out what norm number this actually is */
-       for (n=0;n<g_list_length(v4l2element->norms);n++) {
-               struct v4l2_enumstd *stdptr = (struct v4l2_enumstd *) g_list_nth_data(v4l2element->norms, n);
-               if (!strcmp(stdptr->std.name, standard.name)) {
-                       *norm = n;
-                       return TRUE;
-               }
-       }
-
-       gst_element_error(GST_ELEMENT(v4l2element),
-               "Failed to find norm '%s' in our list of available norms for device %s",
-               standard.name, v4l2element->device);
-       return FALSE;
+       return TRUE;
 }
 
 
@@ -373,27 +510,16 @@ gst_v4l2_get_norm (GstV4l2Element *v4l2element,
 
 gboolean
 gst_v4l2_set_norm (GstV4l2Element *v4l2element,
-                   gint            norm)
+                   v4l2_std_id     norm)
 {
-       struct v4l2_enumstd *standard;
-
-       DEBUG("trying to set norm to %d", norm);
+       DEBUG("trying to set norm to %llx", norm);
        GST_V4L2_CHECK_OPEN(v4l2element);
        GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
 
-       if (norm < 0 || norm >= g_list_length(v4l2element->norms)) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Invalid norm number %d (%d-%d)",
-                       norm, 0, g_list_length(v4l2element->norms));
-               return FALSE;
-       }
-
-       standard = (struct v4l2_enumstd *) g_list_nth_data(v4l2element->norms, norm);
-
-       if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &standard->std) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set norm '%s' (%d) for device %s: %s",
-                       standard->std.name, norm, v4l2element->device, sys_errlist[errno]);
+       if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &norm) < 0) {
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to set norm 0x%llx for device %s: %s",
+                        norm, v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -402,29 +528,6 @@ gst_v4l2_set_norm (GstV4l2Element *v4l2element,
 
 
 /******************************************************
- * gst_v4l2_get_norm_names()
- *   Get the list of available norms
- * return value: the list
- ******************************************************/
-
-GList *
-gst_v4l2_get_norm_names (GstV4l2Element *v4l2element)
-{
-       GList *names = NULL;
-       gint n;
-
-       DEBUG("getting a list of norm names");
-
-       for (n=0;n<g_list_length(v4l2element->norms);n++) {
-               struct v4l2_enumstd *standard = (struct v4l2_enumstd *) g_list_nth_data(v4l2element->norms, n);
-               names = g_list_append(names, g_strdup(standard->std.name));
-       }
-
-       return names;
-}
-
-
-/******************************************************
  * gst_v4l2_get_input()
  *   Get the input of the current device
  * return value: TRUE on success, FALSE on error
@@ -440,9 +543,9 @@ gst_v4l2_get_input (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_OPEN(v4l2element);
 
        if (ioctl(v4l2element->video_fd, VIDIOC_G_INPUT, &n) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get current input on device %s: %s",
-                       v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get current input on device %s: %s",
+                       v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -466,17 +569,10 @@ gst_v4l2_set_input (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_OPEN(v4l2element);
        GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
 
-       if (input < 0 || input >= g_list_length(v4l2element->inputs)) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Invalid input number %d (%d-%d)",
-                       input, 0, g_list_length(v4l2element->inputs));
-               return FALSE;
-       }
-
        if (ioctl(v4l2element->video_fd, VIDIOC_S_INPUT, &input) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set input %d on device %s: %s",
-                       input, v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to set input %d on device %s: %s",
+                        input, v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -485,29 +581,6 @@ gst_v4l2_set_input (GstV4l2Element *v4l2element,
 
 
 /******************************************************
- * gst_v4l2_get_input_names()
- *   Get the list of available input channels
- * return value: the list
- ******************************************************/
-
-GList *
-gst_v4l2_get_input_names (GstV4l2Element *v4l2element)
-{
-       GList *names = NULL;
-       gint n;
-
-       DEBUG("getting a list of input names");
-
-       for (n=0;n<g_list_length(v4l2element->inputs);n++) {
-               struct v4l2_input *input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, n);
-               names = g_list_append(names, g_strdup(input->name));
-       }
-
-       return names;
-}
-
-
-/******************************************************
  * gst_v4l2_get_output()
  *   Get the output of the current device
  * return value: TRUE on success, FALSE on error
@@ -523,9 +596,9 @@ gst_v4l2_get_output (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_OPEN(v4l2element);
 
        if (ioctl(v4l2element->video_fd, VIDIOC_G_OUTPUT, &n) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get current output on device %s: %s",
-                       v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get current output on device %s: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -549,17 +622,10 @@ gst_v4l2_set_output (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_OPEN(v4l2element);
        GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
 
-       if (output < 0 || output >= g_list_length(v4l2element->outputs)) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Invalid output number %d (%d-%d)",
-                       output, 0, g_list_length(v4l2element->outputs));
-               return FALSE;
-       }
-
        if (ioctl(v4l2element->video_fd, VIDIOC_S_OUTPUT, &output) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set output %d on device %s: %s",
-                       output, v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to set output %d on device %s: %s",
+                        output, v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -568,107 +634,61 @@ gst_v4l2_set_output (GstV4l2Element *v4l2element,
 
 
 /******************************************************
- * gst_v4l2_get_output_names()
- *   Get the list of available output channels
- * return value: the list, or NULL on error
- ******************************************************/
-
-GList *
-gst_v4l2_get_output_names (GstV4l2Element *v4l2element)
-{
-       GList *names = NULL;
-       gint n;
-
-       DEBUG("getting a list of output names");
-
-       for (n=0;n<g_list_length(v4l2element->outputs);n++) {
-               struct v4l2_output *output = (struct v4l2_output *) g_list_nth_data(v4l2element->outputs, n);
-               names = g_list_append(names, g_strdup(output->name));
-       }
-
-       return names;
-}
-
-
-/******************************************************
- * gst_v4l_has_tuner():
- *   Check whether the device has a tuner
- * return value: TRUE if it has a tuner, else FALSE
- ******************************************************/
-
-gboolean
-gst_v4l2_has_tuner (GstV4l2Element *v4l2element)
-{
-       gint input_num;
-       struct v4l2_input *input;
-
-       DEBUG("detecting whether device has a tuner");
-       GST_V4L2_CHECK_OPEN(v4l2element);
-
-       if (!gst_v4l2_get_input(v4l2element, &input_num))
-               return FALSE;
-
-       input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, input_num);
-
-       return (input->type == V4L2_INPUT_TYPE_TUNER &&
-               v4l2element->vcap.flags & V4L2_FLAG_TUNER);
-}
-
-
-/******************************************************
- * gst_v4l_get_frequency():
+ * gst_v4l2_get_frequency():
  *   get the current frequency
  * return value: TRUE on success, FALSE on error
  ******************************************************/
 
 gboolean
 gst_v4l2_get_frequency (GstV4l2Element *v4l2element,
+                       gint            tunernum,
                         gulong         *frequency)
 {
-       gint n;
+       struct v4l2_frequency freq;
 
        DEBUG("getting current tuner frequency");
        GST_V4L2_CHECK_OPEN(v4l2element);
 
-       if (!gst_v4l2_has_tuner(v4l2element))
-               return FALSE;
-
-       if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQ, &n) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get current tuner frequency for device %s: %s",
-                       v4l2element->device, sys_errlist[errno]);
+       freq.tuner = tunernum;
+       if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get current tuner frequency for device %s: %s",
+                       v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
-       *frequency = n;
+       *frequency = freq.frequency;
 
        return TRUE;
 }
 
 
 /******************************************************
- * gst_v4l_set_frequency():
+ * gst_v4l2_set_frequency():
  *   set frequency
  * return value: TRUE on success, FALSE on error
  ******************************************************/
 
 gboolean
 gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
+                       gint            tunernum,
                         gulong          frequency)
 {
-       gint n = frequency;
+       struct v4l2_frequency freq;
 
        DEBUG("setting current tuner frequency to %lu", frequency);
        GST_V4L2_CHECK_OPEN(v4l2element);
        GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
 
-       if (!gst_v4l2_has_tuner(v4l2element))
-               return FALSE;
+       freq.tuner = tunernum;
+       /* fill in type - ignore error */
+       ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq);
+       freq.frequency = frequency;
 
-       if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQ, &n) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set tuner frequency to %lu for device %s: %s",
-                       frequency, v4l2element->device, sys_errlist[errno]);
+       if (ioctl(v4l2element->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0) {
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to set tuner frequency to %lu for device %s: %s",
+                       frequency, v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -677,13 +697,14 @@ gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
 
 
 /******************************************************
- * gst_v4l_signal_strength():
+ * gst_v4l2_signal_strength():
  *   get the strength of the signal on the current input
  * return value: TRUE on success, FALSE on error
  ******************************************************/
 
 gboolean
 gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
+                         gint            tunernum,
                           gulong         *signal_strength)
 {
        struct v4l2_tuner tuner;
@@ -691,10 +712,11 @@ gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
        DEBUG("trying to get signal strength");
        GST_V4L2_CHECK_OPEN(v4l2element);
 
+       tuner.index = tunernum;
        if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set signal strength for device %s: %s",
-                       v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get signal strength for device %s: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -705,118 +727,28 @@ gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
 
 
 /******************************************************
- * gst_v4l_has_audio():
- *   Check whether the device has audio capabilities
- * return value: TRUE if it has a tuner, else FALSE
- ******************************************************/
-
-gboolean
-gst_v4l2_has_audio (GstV4l2Element *v4l2element)
-{
-       gint input_num;
-       struct v4l2_input *input;
-
-       DEBUG("detecting whether device has audio");
-       GST_V4L2_CHECK_OPEN(v4l2element);
-
-       if (!gst_v4l2_get_input(v4l2element, &input_num))
-               return FALSE;
-
-       input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, input_num);
-
-       return (input->capability & V4L2_INPUT_CAP_AUDIO);
-}
-
-
-/******************************************************
- * gst_v4l_get_attributes():
- *   get a list of attributes available on this device
- * return value: the list
- ******************************************************/
-
-GList *
-gst_v4l2_get_attributes        (GstV4l2Element *v4l2element)
-{
-       gint i;
-       GList *list = NULL;
-
-       DEBUG("getting a list of available attributes");
-
-       for (i=0;i<g_list_length(v4l2element->controls);i++) {
-               struct v4l2_queryctrl *control = (struct v4l2_queryctrl *) g_list_nth_data(v4l2element->controls, i);
-               GstV4l2Attribute* attribute = g_malloc(sizeof(GstV4l2Attribute));
-               attribute->name = g_strdup(control->name);
-               attribute->index = i;
-               attribute->list_items = NULL;
-               switch (control->type) {
-                       case V4L2_CTRL_TYPE_INTEGER:
-                               attribute->val_type = GST_V4L2_ATTRIBUTE_VALUE_TYPE_INT;
-                               break;
-                       case V4L2_CTRL_TYPE_BOOLEAN:
-                               attribute->val_type = GST_V4L2_ATTRIBUTE_VALUE_TYPE_BOOLEAN;
-                               break;
-                       case V4L2_CTRL_TYPE_MENU: {
-                               /* list items */
-                               gint n;
-                               GList *menus = (GList *) g_list_nth_data(v4l2element->menus, i);
-                               for (n=0;n<g_list_length(menus);n++) {
-                                       struct v4l2_querymenu *menu = g_list_nth_data(menus, n);
-                                       attribute->list_items = g_list_append(attribute->list_items, g_strdup(menu->name));
-                               }
-                               attribute->val_type = GST_V4L2_ATTRIBUTE_VALUE_TYPE_LIST;
-                               break; }
-                       case V4L2_CTRL_TYPE_BUTTON:
-                               attribute->val_type = GST_V4L2_ATTRIBUTE_VALUE_TYPE_BUTTON;
-                               break;
-               }
-               switch (control->category) {
-                       case V4L2_CTRL_CAT_VIDEO:
-                               attribute->type = GST_V4L2_ATTRIBUTE_TYPE_VIDEO;
-                               break;
-                       case V4L2_CTRL_CAT_AUDIO:
-                               attribute->type = GST_V4L2_ATTRIBUTE_TYPE_AUDIO;
-                               break;
-                       case V4L2_CTRL_CAT_EFFECT:
-                               attribute->type = GST_V4L2_ATTRIBUTE_TYPE_EFFECT;
-                               break;
-               }
-               gst_v4l2_get_attribute(v4l2element, i, &attribute->value);
-               attribute->min = control->minimum;
-               attribute->max = control->maximum;
-       }
-
-       return list;
-}
-
-
-/******************************************************
- * gst_v4l_get_attribute():
+ * gst_v4l2_get_attribute():
  *   try to get the value of one specific attribute
  * return value: TRUE on success, FALSE on error
  ******************************************************/
 
 gboolean
 gst_v4l2_get_attribute (GstV4l2Element *v4l2element,
-                         gint            attribute_num,
-                         gint           *value)
+                         int             attribute_num,
+                         int            *value)
 {
        struct v4l2_control control;
 
-       DEBUG("getting value of attribute %d", attribute_num);
        GST_V4L2_CHECK_OPEN(v4l2element);
 
-       if (attribute_num < 0 || attribute_num >= g_list_length(v4l2element->controls)) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Invalid control ID %d", attribute_num);
-               return FALSE;
-       }
+       DEBUG("getting value of attribute %d", attribute_num);
 
        control.id = attribute_num;
 
        if (ioctl(v4l2element->video_fd, VIDIOC_G_CTRL, &control) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get value for control %d on device %s: %s",
-                       attribute_num, v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get value for control %d on device %s: %s",
+                        attribute_num, v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -827,34 +759,29 @@ gst_v4l2_get_attribute    (GstV4l2Element *v4l2element,
 
 
 /******************************************************
- * gst_v4l_set_attribute():
+ * gst_v4l2_set_attribute():
  *   try to set the value of one specific attribute
  * return value: TRUE on success, FALSE on error
  ******************************************************/
 
 gboolean
 gst_v4l2_set_attribute (GstV4l2Element *v4l2element,
-                         gint            attribute_num,
-                         gint            value)
+                         int             attribute_num,
+                         const int       value)
 {
        struct v4l2_control control;
 
-       DEBUG("setting value of attribute %d to %d", attribute_num, value);
        GST_V4L2_CHECK_OPEN(v4l2element);
 
-       if (attribute_num < 0 || attribute_num >= g_list_length(v4l2element->controls)) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Invalid control ID %d", attribute_num);
-               return FALSE;
-       }
+       DEBUG("setting value of attribute %d to %d", attribute_num, value);
 
        control.id = attribute_num;
        control.value = value;
 
        if (ioctl(v4l2element->video_fd, VIDIOC_S_CTRL, &control) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set value %d for control %d on device %s: %s",
-                       value, attribute_num, v4l2element->device, sys_errlist[errno]);
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to set value %d for control %d on device %s: %s",
+                       value, attribute_num, v4l2element->device, g_strerror(errno)));
                return FALSE;
        }