change NULL to (NULL) for GST_ELEMENT_ERROR
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2_calls.c
index 8b65dc7..f7c3d3a 100644 (file)
@@ -34,6 +34,8 @@
 #include "gstv4l2xoverlay.h"
 #include "gstv4l2colorbalance.h"
 
+#include "gstv4l2src.h"
+
 #define DEBUG(format, args...) \
        GST_DEBUG_OBJECT (\
                GST_ELEMENT(v4l2element), \
@@ -53,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, g_strerror(errno));
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Error getting %s capabilities: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -97,10 +99,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 input enumeration for %s: %s",
+                                       GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                                          ("Failed to get %d in input enumeration for %s: %s",
                                                n, v4l2element->device,
-                                               g_strerror(errno));
+                                               g_strerror (errno)));
                                        return FALSE;
                                }
                        }
@@ -120,11 +122,11 @@ gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
                                vtun.index = input.tuner;
                                if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER,
                                          &vtun) < 0) {
-                                       gst_element_error(GST_ELEMENT(v4l2element),
-                                               "Failed to get tuner %d settings on %s: %s",
+                                       GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                               ("Failed to get tuner %d settings on %s: %s",
                                                input.tuner,
                                                v4l2element->device,
-                                               g_strerror(errno));
+                                               g_strerror (errno)));
                                        g_object_unref(G_OBJECT(channel));
                                        return FALSE;
                                }
@@ -159,10 +161,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 output enumeration for %s: %s",
+                                       GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                                                          ("Failed to get %d in output enumeration for %s: %s",
                                                n, v4l2element->device,
-                                               g_strerror(errno));
+                                               g_strerror (errno)));
                                        return FALSE;
                                }
                        }
@@ -198,9 +200,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 norm enumeration for %s: %s",
-                                       n, v4l2element->device, g_strerror(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;
                        }
                }
@@ -234,9 +237,10 @@ gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
                                else
                                        break;
                        } else {
-                               gst_element_error(GST_ELEMENT(v4l2element),
-                                       "Failed to get no. %d in control enumeration for %s: %s",
-                                       n, v4l2element->device, g_strerror(errno));
+                               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;
                        }
                }
@@ -285,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, g_strerror(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;
                                        }
                                }
@@ -340,6 +345,49 @@ gst_v4l2_empty_lists (GstV4l2Element *v4l2element)
        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");
+    }
+  }
+}
+
 
 /******************************************************
  * gst_v4l2_open():
@@ -361,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, g_strerror(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;
        }
 
@@ -372,11 +420,23 @@ gst_v4l2_open (GstV4l2Element *v4l2element)
                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",
+       /* set defaults */
+       gst_v4l2_set_defaults (v4l2element);
+
+       GST_INFO_OBJECT (v4l2element, "Opened device '%s' (%s) successfully\n",
                v4l2element->vcap.card, v4l2element->device);
 
        return TRUE;
@@ -432,9 +492,9 @@ gst_v4l2_get_norm (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_OPEN(v4l2element);
 
        if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, norm) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get the current norm for device %s: %s",
-                       v4l2element->device, g_strerror(errno));
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get the current norm for device %s: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -457,9 +517,9 @@ gst_v4l2_set_norm (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
 
        if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &norm) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set norm 0x%llx for device %s: %s",
-                       norm, v4l2element->device, g_strerror(errno));
+               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;
        }
 
@@ -483,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, g_strerror(errno));
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get current input on device %s: %s",
+                       v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -510,9 +570,9 @@ gst_v4l2_set_input (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
 
        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, g_strerror(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;
        }
 
@@ -536,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, g_strerror(errno));
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get current output on device %s: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -563,9 +623,9 @@ gst_v4l2_set_output (GstV4l2Element *v4l2element,
        GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
 
        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, g_strerror(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;
        }
 
@@ -591,9 +651,9 @@ gst_v4l2_get_frequency (GstV4l2Element *v4l2element,
 
        freq.tuner = tunernum;
        if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get current tuner frequency for device %s: %s",
-                       v4l2element->device, g_strerror(errno));
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get current tuner frequency for device %s: %s",
+                       v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -626,9 +686,9 @@ gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
        freq.frequency = frequency;
 
        if (ioctl(v4l2element->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to set tuner frequency to %lu for device %s: %s",
-                       frequency, v4l2element->device, g_strerror(errno));
+               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;
        }
 
@@ -654,9 +714,9 @@ gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
 
        tuner.index = tunernum;
        if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
-               gst_element_error(GST_ELEMENT(v4l2element),
-                       "Failed to get signal strength for device %s: %s",
-                       v4l2element->device, g_strerror(errno));
+               GST_ELEMENT_ERROR (v4l2element, RESOURCE, SETTINGS, (NULL),
+                       ("Failed to get signal strength for device %s: %s",
+                        v4l2element->device, g_strerror(errno)));
                return FALSE;
        }
 
@@ -686,9 +746,9 @@ gst_v4l2_get_attribute      (GstV4l2Element *v4l2element,
        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, g_strerror(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;
        }
 
@@ -719,9 +779,9 @@ gst_v4l2_set_attribute      (GstV4l2Element *v4l2element,
        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, g_strerror(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;
        }