Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2_calls.c
index 8f1a6c6..74b2e0a 100644 (file)
@@ -46,6 +46,9 @@
 #include "gstv4l2colorbalance.h"
 
 #include "gstv4l2src.h"
+#include "gstv4l2sink.h"
+
+#include "gst/gst-i18n-plugin.h"
 
 /* Those are ioctl calls */
 #ifndef V4L2_CID_HCENTER
@@ -118,11 +121,12 @@ gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
   GST_DEBUG_OBJECT (e, "  channels");
   /* and now, the channels */
   for (n = 0;; n++) {
-    struct v4l2_input input = { 0, };
+    struct v4l2_input input;
     GstV4l2TunerChannel *v4l2channel;
-
     GstTunerChannel *channel;
 
+    memset (&input, 0, sizeof (input));
+
     input.index = n;
     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
       if (errno == EINVAL)
@@ -141,7 +145,7 @@ gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
     GST_LOG_OBJECT (e, "   name:      '%s'", input.name);
     GST_LOG_OBJECT (e, "   type:      %08x", input.type);
     GST_LOG_OBJECT (e, "   audioset:  %08x", input.audioset);
-    GST_LOG_OBJECT (e, "   std:       %016x", input.std);
+    GST_LOG_OBJECT (e, "   std:       %016x", (guint) input.std);
     GST_LOG_OBJECT (e, "   status:    %08x", input.status);
 
     v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
@@ -222,35 +226,39 @@ gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
         standard.frameperiod.denominator, standard.frameperiod.numerator);
     v4l2norm->index = standard.id;
 
+    GST_DEBUG_OBJECT (v4l2object->element, "index=%08x, label=%s",
+        (unsigned int) v4l2norm->index, norm->label);
+
     v4l2object->norms = g_list_prepend (v4l2object->norms, (gpointer) norm);
   }
   v4l2object->norms = g_list_reverse (v4l2object->norms);
 
   GST_DEBUG_OBJECT (e, "  controls+menus");
+
   /* and lastly, controls+menus (if appropriate) */
   for (n = V4L2_CID_BASE;; n++) {
     struct v4l2_queryctrl control = { 0, };
     GstV4l2ColorBalanceChannel *v4l2channel;
-
     GstColorBalanceChannel *channel;
 
     /* when we reached the last official CID, continue with private CIDs */
     if (n == V4L2_CID_LASTP1) {
       GST_DEBUG_OBJECT (e, "checking private CIDs");
       n = V4L2_CID_PRIVATE_BASE;
-      /* FIXME: We are still not handling private controls. We need a new GstInterface
-         to export those controls */
-      break;
     }
+    GST_DEBUG_OBJECT (e, "checking control %08x", n);
 
     control.id = n;
     if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
       if (errno == EINVAL) {
-        if (n < V4L2_CID_PRIVATE_BASE)
+        if (n < V4L2_CID_PRIVATE_BASE) {
+          GST_DEBUG_OBJECT (e, "skipping control %08x", n);
           /* continue so that we also check private controls */
           continue;
-        else
+        } else {
+          GST_DEBUG_OBJECT (e, "controls finished");
           break;
+        }
       } else {
         GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
             (_("Failed getting controls attributes on device '%s'."),
@@ -260,8 +268,10 @@ gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
         return FALSE;
       }
     }
-    if (control.flags & V4L2_CTRL_FLAG_DISABLED)
+    if (control.flags & V4L2_CTRL_FLAG_DISABLED) {
+      GST_DEBUG_OBJECT (e, "skipping disabled control");
       continue;
+    }
 
     switch (n) {
       case V4L2_CID_BRIGHTNESS:
@@ -277,6 +287,9 @@ gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
       case V4L2_CID_EXPOSURE:
       case V4L2_CID_AUTOGAIN:
       case V4L2_CID_GAIN:
+#ifdef V4L2_CID_SHARPNESS
+      case V4L2_CID_SHARPNESS:
+#endif
         /* we only handle these for now (why?) */
         break;
       case V4L2_CID_HFLIP:
@@ -441,15 +454,21 @@ gst_v4l2_open (GstV4l2Object * v4l2object)
   if (libv4l2_fd != -1)
     v4l2object->video_fd = libv4l2_fd;
 
+  v4l2object->can_poll_device = TRUE;
+
   /* get capabilities, error will be posted */
   if (!gst_v4l2_get_capabilities (v4l2object))
     goto error;
 
   /* do we need to be a capture device? */
-  if (GST_IS_V4L2SRC (v4l2object) &&
+  if (GST_IS_V4L2SRC (v4l2object->element) &&
       !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
     goto not_capture;
 
+  if (GST_IS_V4L2SINK (v4l2object->element) &&
+      !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT))
+    goto not_output;
+
   /* create enumerations, posts errors. */
   if (!gst_v4l2_fill_lists (v4l2object))
     goto error;
@@ -494,6 +513,14 @@ not_capture:
         ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
     goto error;
   }
+not_output:
+  {
+    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
+        (_("Device '%s' is not a output device."),
+            v4l2object->videodev),
+        ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
+    goto error;
+  }
 error:
   {
     if (GST_V4L2_IS_OPEN (v4l2object)) {
@@ -573,7 +600,8 @@ std_failed:
 gboolean
 gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
 {
-  GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
+  GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to "
+      "%" G_GINT64_MODIFIER "x", (guint64) norm);
 
   if (!GST_V4L2_IS_OPEN (v4l2object))
     return FALSE;
@@ -602,7 +630,7 @@ gboolean
 gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
     gint tunernum, gulong * frequency)
 {
-  struct v4l2_frequency freq;
+  struct v4l2_frequency freq = { 0, };
 
   GstTunerChannel *channel;
 
@@ -641,7 +669,7 @@ gboolean
 gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
     gint tunernum, gulong frequency)
 {
-  struct v4l2_frequency freq;
+  struct v4l2_frequency freq = { 0, };
 
   GstTunerChannel *channel;
 
@@ -682,7 +710,7 @@ gboolean
 gst_v4l2_signal_strength (GstV4l2Object * v4l2object,
     gint tunernum, gulong * signal_strength)
 {
-  struct v4l2_tuner tuner;
+  struct v4l2_tuner tuner = { 0, };
 
   GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
 
@@ -716,7 +744,7 @@ gboolean
 gst_v4l2_get_attribute (GstV4l2Object * v4l2object,
     int attribute_num, int *value)
 {
-  struct v4l2_control control;
+  struct v4l2_control control = { 0, };
 
   GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
       attribute_num);
@@ -753,7 +781,7 @@ gboolean
 gst_v4l2_set_attribute (GstV4l2Object * v4l2object,
     int attribute_num, const int value)
 {
-  struct v4l2_control control;
+  struct v4l2_control control = { 0, };
 
   GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
       attribute_num, value);
@@ -799,11 +827,14 @@ gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
 
   /* ERRORS */
 input_failed:
-  {
+  if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
+    /* only give a warning message if driver actually claims to have tuner
+     * support
+     */
     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
         (_("Failed to get current input on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
-    return FALSE;
   }
+  return FALSE;
 }
 
 gboolean
@@ -821,10 +852,70 @@ gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
 
   /* ERRORS */
 input_failed:
-  {
+  if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
+    /* only give a warning message if driver actually claims to have tuner
+     * support
+     */
     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
         (_("Failed to set input %d on device %s."),
             input, v4l2object->videodev), GST_ERROR_SYSTEM);
+  }
+  return FALSE;
+}
+
+gboolean
+gst_v4l2_get_output (GstV4l2Object * v4l2object, gint * output)
+{
+  gint n;
+
+  GST_DEBUG_OBJECT (v4l2object->element, "trying to get output");
+
+  if (!GST_V4L2_IS_OPEN (v4l2object))
     return FALSE;
+
+  if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0)
+    goto output_failed;
+
+  *output = n;
+
+  GST_DEBUG_OBJECT (v4l2object->element, "output: %d", n);
+
+  return TRUE;
+
+  /* ERRORS */
+output_failed:
+  if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
+    /* only give a warning message if driver actually claims to have tuner
+     * support
+     */
+    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
+        (_("Failed to get current output on device '%s'. May be it is a radio device"), v4l2object->videodev), GST_ERROR_SYSTEM);
+  }
+  return FALSE;
+}
+
+gboolean
+gst_v4l2_set_output (GstV4l2Object * v4l2object, gint output)
+{
+  GST_DEBUG_OBJECT (v4l2object->element, "trying to set output to %d", output);
+
+  if (!GST_V4L2_IS_OPEN (v4l2object))
+    return FALSE;
+
+  if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0)
+    goto output_failed;
+
+  return TRUE;
+
+  /* ERRORS */
+output_failed:
+  if (v4l2object->vcap.capabilities & V4L2_CAP_TUNER) {
+    /* only give a warning message if driver actually claims to have tuner
+     * support
+     */
+    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
+        (_("Failed to set output %d on device %s."),
+            output, v4l2object->videodev), GST_ERROR_SYSTEM);
   }
+  return FALSE;
 }