/* GStreamer
 *
 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *               2006 Edgard Lima <edgard.lima@indt.org.br>
 *
 * v4l2_calls.c - generic V4L2 calls handling
 *
 * 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/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#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"

GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
#define GST_CAT_DEFAULT v4l2_debug

/******************************************************
 * gst_v4l2_get_capabilities():
 *   get the device's capturing capabilities
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_capabilities (GstV4l2Object * v4l2object)
{
  GST_DEBUG_OBJECT (v4l2object->element, "getting capabilities");
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &(v4l2object->vcap)) < 0) {
    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
        (_("Error getting capabilities for device '%s':"
                " It isn't a v4l2 driver. Check if it is a v4l1 driver"),
            v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  return TRUE;
}


/******************************************************
 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
 *   fill/empty the lists of enumerations
 * return value: TRUE on success, FALSE on error
 ******************************************************/

static gboolean
gst_v4l2_fill_lists (GstV4l2Object * v4l2object)
{
  gint n;

#if 0                           /* output not handled by now */
  GstPadDirection dir = GST_PAD_UNKNOWN;
#endif /* #if 0 - output not handled by now */

  GST_DEBUG_OBJECT (v4l2object->element, "getting enumerations");
  GST_V4L2_CHECK_OPEN (v4l2object);

#if 0                           /* output not handled by now */
  if (dir != GST_PAD_SINK) {
#endif /* #if 0 - output not handled by now */

    GST_DEBUG_OBJECT (v4l2object->element, "  inputs");
    /* and now, the inputs */
    for (n = 0;; n++) {
      struct v4l2_input input;
      GstV4l2TunerChannel *v4l2channel;
      GstTunerChannel *channel;

      input.index = n;
      if (ioctl (v4l2object->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
        if (errno == EINVAL)
          break;                /* end of enumeration */
        else {
          GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
              ("Failed to get %d in input enumeration for %s",
                  n, v4l2object->videodev), GST_ERROR_SYSTEM);
          return FALSE;
        }
      }

      v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
      channel = GST_TUNER_CHANNEL (v4l2channel);
      channel->label = g_strdup ((const gchar *) 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 (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun) < 0) {
          GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
              ("Failed to get tuner %d settings on %s",
                  input.tuner, v4l2object->videodev), GST_ERROR_SYSTEM);
          g_object_unref (G_OBJECT (channel));
          return FALSE;
        }
        channel->freq_multiplicator =
            62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
        channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
        channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
        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;
      }

      v4l2object->inputs =
          g_list_append (v4l2object->inputs, (gpointer) channel);
    }

#if 0                           /* output not handled by now */
  } else {
    /* outputs */
    for (n = 0;; n++) {
      struct v4l2_output output;
      GstV4l2TunerChannel *v4l2channel;
      GstTunerChannel *channel;

      output.index = n;
      if (ioctl (v4l2object->video_fd, VIDIOC_ENUMOUTPUT, &output) < 0) {
        if (errno == EINVAL)
          break;                /* end of enumeration */
        else {
          GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
              ("Failed to get %d in output enumeration for %s",
                  n, v4l2object->videodev)
              , GST_ERROR_SYSTEM);
          return FALSE;
        }
      }

      v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
      channel = GST_TUNER_CHANNEL (v4l2channel);
      channel->label = g_strdup ((const gchar *) 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;
      }

      v4l2object->inputs =
          g_list_append (v4l2object->inputs, (gpointer) channel);
    }
  }
#endif /* #if 0 - output not handled by now */

  GST_DEBUG_OBJECT (v4l2object->element, "  norms");
  /* norms... */
  for (n = 0;; n++) {
    struct v4l2_standard standard;
    GstV4l2TunerNorm *v4l2norm;
    GstTunerNorm *norm;

    standard.index = n;
    if (ioctl (v4l2object->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
      if (errno == EINVAL)
        break;                  /* end of enumeration */
      else {
        GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
            ("Failed to get %d in norm enumeration for %s",
                n, v4l2object->videodev), GST_ERROR_SYSTEM);
        return FALSE;
      }
    }

    v4l2norm = g_object_new (GST_TYPE_V4L2_TUNER_NORM, NULL);
    norm = GST_TUNER_NORM (v4l2norm);
    norm->label = g_strdup ((const gchar *) standard.name);
    gst_value_set_fraction (&norm->framerate,
        standard.frameperiod.denominator, standard.frameperiod.numerator);
    v4l2norm->index = standard.id;

    v4l2object->stds = g_list_append (v4l2object->stds, (gpointer) norm);
  }

  GST_DEBUG_OBJECT (v4l2object->element, "  controls+menus");
  /* and lastly, controls+menus (if appropriate) */
  for (n = V4L2_CID_BASE;; n++) {
    struct v4l2_queryctrl control;
    GstV4l2ColorBalanceChannel *v4l2channel;
    GstColorBalanceChannel *channel;

    /* when we reached the last official CID, continue with private CIDs */
    if (n == V4L2_CID_LASTP1) {
      GST_DEBUG_OBJECT (v4l2object->element, "checking private CIDs");
      n = V4L2_CID_PRIVATE_BASE;
    }

    control.id = n;
    if (ioctl (v4l2object->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
      if (errno == EINVAL) {
        if (n < V4L2_CID_PRIVATE_BASE)
          /* continue so that we also check private controls */
          continue;
        else
          break;
      } else {
        GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
            ("Failed to get %d in control enumeration for %s",
                n, v4l2object->videodev), GST_ERROR_SYSTEM);
        return FALSE;
      }
    }
    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 (why?) */
        break;
      default:
        GST_DEBUG_OBJECT (v4l2object->element,
            "ControlID %s (%x) unhandled, FIXME", control.name, n);
        control.id++;
        break;
    }
    if (n != control.id)
      continue;

    GST_DEBUG_OBJECT (v4l2object->element,
        "Adding ControlID %s (%x)", control.name, n);
    v4l2channel = g_object_new (GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL, NULL);
    channel = GST_COLOR_BALANCE_CHANNEL (v4l2channel);
    channel->label = g_strdup ((const gchar *) control.name);
    v4l2channel->id = n;

#if 0                           /* FIXME: will be be need just when handling private controls */
    if (control.type == V4L2_CTRL_TYPE_MENU) {
      struct v4l2_querymenu menu, *mptr;
      int i;

      menu.id = n;
      for (i = 0;; i++) {
        menu.index = i;
        if (ioctl (v4l2object->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
          if (errno == EINVAL)
            break;              /* end of enumeration */
          else {
            GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
                ("Failed to get %d in menu enumeration for %s",
                    n, v4l2object->videodev), GST_ERROR_SYSTEM);
            return FALSE;
          }
        }
        mptr = g_malloc (sizeof (menu));
        memcpy (mptr, &menu, sizeof (menu));
        menus = g_list_append (menus, mptr);
      }
    }
    v4l2object->menus = g_list_append (v4l2object->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:
        /* FIX ME we should find out how to handle V4L2_CTRL_TYPE_BUTTON */
        GST_DEBUG_OBJECT (v4l2object->element,
            "No range for ControlID %s (%x), type=%d",
            control.name, n, control.type);
        channel->min_value = channel->max_value = 0;
        break;
    }

    v4l2object->colors = g_list_append (v4l2object->colors, (gpointer) channel);
  }

  GST_DEBUG_OBJECT (v4l2object->element, "done");
  return TRUE;
}


static void
gst_v4l2_empty_lists (GstV4l2Object * v4l2object)
{
  GST_DEBUG_OBJECT (v4l2object->element, "deleting enumerations");

  g_list_foreach (v4l2object->inputs, (GFunc) g_object_unref, NULL);
  g_list_free (v4l2object->inputs);
  v4l2object->inputs = NULL;

  g_list_foreach (v4l2object->stds, (GFunc) g_object_unref, NULL);
  g_list_free (v4l2object->stds);
  v4l2object->stds = NULL;

  g_list_foreach (v4l2object->colors, (GFunc) g_object_unref, NULL);
  g_list_free (v4l2object->colors);
  v4l2object->colors = NULL;
}

/******************************************************
 * gst_v4l2_open():
 *   open the video device (v4l2object->videodev)
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_open (GstV4l2Object * v4l2object)
{
  struct stat st;

  GST_DEBUG_OBJECT (v4l2object->element, "Trying to open device %s",
      v4l2object->videodev);
  GST_V4L2_CHECK_NOT_OPEN (v4l2object);
  GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);

  /* be sure we have a device */
  if (!v4l2object->videodev)
    v4l2object->videodev = g_strdup ("/dev/video");

  /* check if it is a device */
  if (-1 == stat (v4l2object->videodev, &st)) {
    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
        (_("Cannot identify device '%s'"), v4l2object->videodev),
        GST_ERROR_SYSTEM);
    goto error;
  }
  if (!S_ISCHR (st.st_mode)) {
    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
        (_("This isn't a device '%s'"), v4l2object->videodev),
        GST_ERROR_SYSTEM);
    goto error;
  }

  /* open the device */
  v4l2object->video_fd =
      open (v4l2object->videodev, O_RDWR /* | O_NONBLOCK */ );

  if (!GST_V4L2_IS_OPEN (v4l2object)) {
    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, OPEN_READ_WRITE,
        (_("Could not open device \"%s\" for reading and writing."),
            v4l2object->videodev), GST_ERROR_SYSTEM);
    goto error;
  }

  /* get capabilities */
  if (!gst_v4l2_get_capabilities (v4l2object)) {
    goto error;
  }

  /* do we need to be a capture device? */
  if (GST_IS_V4L2SRC (v4l2object) &&
      !(v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, NOT_FOUND,
        (_("Device \"%s\" is not a capture device."),
            v4l2object->videodev),
        ("Capabilities: 0x%x", v4l2object->vcap.capabilities));
    goto error;
  }

  /* create enumerations */
  if (!gst_v4l2_fill_lists (v4l2object))
    goto error;

  GST_INFO_OBJECT (v4l2object->element,
      "Opened device '%s' (%s) successfully",
      v4l2object->vcap.card, v4l2object->videodev);

  return TRUE;

error:
  if (GST_V4L2_IS_OPEN (v4l2object)) {
    /* close device */
    close (v4l2object->video_fd);
    v4l2object->video_fd = -1;
  }
  /* empty lists */
  gst_v4l2_empty_lists (v4l2object);

  return FALSE;
}


/******************************************************
 * gst_v4l2_close():
 *   close the video device (v4l2object->video_fd)
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_close (GstV4l2Object * v4l2object)
{
  GST_DEBUG_OBJECT (v4l2object->element, "Trying to close %s",
      v4l2object->videodev);
  GST_V4L2_CHECK_OPEN (v4l2object);
  GST_V4L2_CHECK_NOT_ACTIVE (v4l2object);

  /* close device */
  close (v4l2object->video_fd);
  v4l2object->video_fd = -1;

  /* empty lists */
  gst_v4l2_empty_lists (v4l2object);

  return TRUE;
}


/******************************************************
 * gst_v4l2_get_norm()
 *   Get the norm of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_norm (GstV4l2Object * v4l2object, v4l2_std_id * norm)
{
  GST_DEBUG_OBJECT (v4l2object->element, "getting norm");
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  if (ioctl (v4l2object->video_fd, VIDIOC_G_STD, norm) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to get the current norm for device %s"),
            v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }



  return TRUE;
}


/******************************************************
 * gst_v4l2_set_norm()
 *   Set the norm of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_set_norm (GstV4l2Object * v4l2object, v4l2_std_id norm)
{
  GST_DEBUG_OBJECT (v4l2object->element, "trying to set norm to %llx", norm);
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  if (ioctl (v4l2object->video_fd, VIDIOC_S_STD, &norm) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to set norm 0x%llx for device %s: %s"),
            norm, v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  return TRUE;
}

/******************************************************
 * gst_v4l2_get_frequency():
 *   get the current frequency
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_frequency (GstV4l2Object * v4l2object,
    gint tunernum, gulong * frequency)
{
  struct v4l2_frequency freq;
  GstTunerChannel *channel;

  GST_DEBUG_OBJECT (v4l2object->element, "getting current tuner frequency");
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));

  freq.tuner = tunernum;
  if (ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to get current tuner frequency for device %s"),
            v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  *frequency = freq.frequency * channel->freq_multiplicator;

  return TRUE;
}


/******************************************************
 * gst_v4l2_set_frequency():
 *   set frequency
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_set_frequency (GstV4l2Object * v4l2object,
    gint tunernum, gulong frequency)
{
  struct v4l2_frequency freq;
  GstTunerChannel *channel;

  GST_DEBUG_OBJECT (v4l2object->element,
      "setting current tuner frequency to %lu", frequency);
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  channel = gst_tuner_get_channel (GST_TUNER (v4l2object->element));

  freq.tuner = tunernum;
  /* fill in type - ignore error */
  ioctl (v4l2object->video_fd, VIDIOC_G_FREQUENCY, &freq);
  freq.frequency = frequency / channel->freq_multiplicator;

  if (ioctl (v4l2object->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to set current tuner frequency for device %s to %lu"),
            v4l2object->videodev, frequency), GST_ERROR_SYSTEM);
    return FALSE;
  }

  return TRUE;
}


/******************************************************
 * 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 (GstV4l2Object * v4l2object,
    gint tunernum, gulong * signal_strength)
{
  struct v4l2_tuner tuner;

  GST_DEBUG_OBJECT (v4l2object->element, "trying to get signal strength");
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  tuner.index = tunernum;
  if (ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to get signal strength for device %s"),
            v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  *signal_strength = tuner.signal;

  return TRUE;
}


/******************************************************
 * 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 (GstV4l2Object * v4l2object,
    int attribute_num, int *value)
{
  struct v4l2_control control;

  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  GST_DEBUG_OBJECT (v4l2object->element, "getting value of attribute %d",
      attribute_num);

  control.id = attribute_num;

  if (ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to get value for control %d on device %s"),
            attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  *value = control.value;

  return TRUE;
}


/******************************************************
 * 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 (GstV4l2Object * v4l2object,
    int attribute_num, const int value)
{
  struct v4l2_control control;

  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  GST_DEBUG_OBJECT (v4l2object->element, "setting value of attribute %d to %d",
      attribute_num, value);

  control.id = attribute_num;
  control.value = value;

  if (ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to set value %d for control %d on device %s"),
            value, attribute_num, v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  return TRUE;
}

gboolean
gst_v4l2_get_input (GstV4l2Object * v4l2object, gint * input)
{
  gint n;

  GST_DEBUG_OBJECT (v4l2object->element, "trying to get input");
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  if (ioctl (v4l2object->video_fd, VIDIOC_G_INPUT, &n) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to get current input on device %s"),
            v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  *input = n;

  return TRUE;
}

gboolean
gst_v4l2_set_input (GstV4l2Object * v4l2object, gint input)
{
  GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  if (ioctl (v4l2object->video_fd, VIDIOC_S_INPUT, &input) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to set input %d on device %s"),
            input, v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  return TRUE;
}


#if 0                           /* output not handled by now, Indeed it will be another element */

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 (ioctl (v4l2object->video_fd, VIDIOC_G_OUTPUT, &n) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to get current output on device %s"),
            v4l2object->videodev), GST_ERROR_SYSTEM);
    return FALSE;
  }

  *output = n;

  return TRUE;
}

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 (!GST_V4L2_IS_ACTIVE (v4l2object))
    return FALSE;

  if (ioctl (v4l2object->video_fd, VIDIOC_S_OUTPUT, &output) < 0) {
    GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to set current output on device %s to %d"),
            v4l2object->videodev, output), GST_ERROR_SYSTEM);
    return FALSE;
  }

  return TRUE;
}

#endif /* #if 0 - output not handled by now , Indeed it will be another element */