// %flair:license{ // This file is part of the Flair framework distributed under the // CECILL-C License, Version 1.0. // %flair:license} // created: 2014/07/17 // filename: V4LCamera.cpp // // author: Guillaume Sanahuja // Copyright Heudiasyc UMR UTC/CNRS 7253 // // version: $Id: $ // // purpose: base class for V4l camera // // /*********************************************************************/ #include "V4LCamera.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEFAULT_V4L_BUFFERS 4 using std::string; using namespace flair::core; using namespace flair::gui; namespace flair { namespace sensor { V4LCamera::V4LCamera(string name, uint8_t camera_index, uint16_t width, uint16_t height, Image::Type::Format format, uint8_t priority) : Thread(getFrameworkManager(), name, priority), Camera(name, width, height, format) { string deviceName="/dev/video"+std::to_string(camera_index); device = open(deviceName.c_str(), O_RDWR | O_NONBLOCK); if (device == -1) { Thread::Err("Cannot open %s\n",deviceName.c_str()); } else { Printf("V4LCamera %s, opened %s\n",name.c_str(),deviceName.c_str()); } struct v4l2_capability cap; memset(&cap, 0, sizeof (v4l2_capability)); if (xioctl (device, VIDIOC_QUERYCAP, &cap)==-1) { Thread::Err("VIDIOC_QUERYCAP xioctl\n"); } if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) { Thread::Err("device is unable to capture video memory.\n"); } //get v4l2_format struct v4l2_format form; form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if(xioctl (device, VIDIOC_G_FMT,&form)==-1) { Thread::Err("VIDIOC_G_FMT xioctl\n"); } //set width, height and format if (format == Image::Type::Format::UYVY) { form.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; } else if (format == Image::Type::Format::YUYV) { form.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; } else { Thread::Err("format not supported\n"); } form.fmt.pix.width = width; form.fmt.pix.height = height; form.fmt.win.chromakey = 0; form.fmt.win.field = V4L2_FIELD_ANY; form.fmt.win.clips = 0; form.fmt.win.clipcount = 0; form.fmt.pix.field = V4L2_FIELD_ANY; if(xioctl (device, VIDIOC_S_FMT,&form)==-1) { Thread::Err("VIDIOC_S_FMT xioctl\n"); } //alloc and queue bufs AllocBuffers(); for (int bufferIndex = 0; bufferIndex < nbBuffers;++bufferIndex) { QueueBuffer(bufferIndex); } // enable the streaming v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (xioctl (device, VIDIOC_STREAMON,&type)==-1) { Thread::Err("VIDIOC_STREAMON xioctl\n"); } // skip first frame. it is often bad -- this is unnotied in traditional apps, // but could be fatal if bad jpeg is enabled bufferIndex=-1; GrabFrame(); // ground station gain = new DoubleSpinBox(GetGroupBox()->NewRow(), "gain:", 0, 1, 0.1); exposure = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "exposure:", 0,1, 0.1); bright = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "bright:", 0, 1, 0.1); contrast = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "contrast:", 0,1, 0.1); hue = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "hue:", 0, 1, 0.1); sharpness = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "sharpness:", 0, 1, 0.1); sat = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "saturation:", 0, 1,0.1); autogain = new CheckBox(GetGroupBox()->NewRow(), "autogain:"); autoexposure = new CheckBox(GetGroupBox()->LastRowLastCol(), "autoexposure:"); awb = new CheckBox(GetGroupBox()->LastRowLastCol(), "awb:"); fps = new Label(GetGroupBox()->NewRow(), "fps"); hasProblems=false; } V4LCamera::~V4LCamera() { for (int n_buffers = 0; n_buffers < nbBuffers; n_buffers++) { FreeFunction((char*)buffers[n_buffers].start); } SafeStop(); Join(); } void V4LCamera::Run(void) { Time cam_time, new_time, fpsNow, fpsPrev; int fpsCounter = 0; // init image old GrabFrame(); cam_time = GetTime(); fpsPrev = cam_time; while (!ToBeStopped()) { //check for ps3eye deconnection in hds uav if(hasProblems==false) { struct v4l2_format form; form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; xioctl(device, VIDIOC_G_FMT,&form); if(xioctl (device, VIDIOC_G_FMT,&form)<0) { Thread::Warn("camera disconnected\n"); hasProblems=true; } } // fps counter fpsCounter++; if (GetTime() > (fpsPrev + 5 * (Time)1000000000)) { // every 5 secondes fpsNow = GetTime(); fps->SetText("fps: %.1f", fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.)); fpsCounter = 0; fpsPrev = fpsNow; } // cam properties if (gain->ValueChanged() == true && autogain->Value() == false) SetGain(gain->Value()); if (exposure->ValueChanged() == true && autoexposure->Value() == false) SetExposure(exposure->Value()); if (bright->ValueChanged() == true) SetBrightness(bright->Value()); if (sat->ValueChanged() == true) SetSaturation(sat->Value()); if (contrast->ValueChanged() == true) SetContrast(contrast->Value()); if (hue->ValueChanged() == true) SetHue(hue->Value()); if (sharpness->ValueChanged() == true) SetProperty(V4L2_CID_SHARPNESS, sharpness->Value()); if (autogain->ValueChanged() == true) { if (autogain->Value() == true) { gain->setEnabled(false); } else { gain->setEnabled(true); SetGain(gain->Value()); } SetAutoGain(autogain->Value()); } if (autoexposure->ValueChanged() == true) { if (autoexposure->Value() == true) { exposure->setEnabled(false); } else { exposure->setEnabled(true); SetExposure(exposure->Value()); } SetAutoExposure(autoexposure->Value()); } if (awb->ValueChanged() == true) SetProperty(V4L2_CID_AUTO_WHITE_BALANCE, awb->Value()); // get picture GrabFrame(); new_time = GetTime(); //check for ps3eye deconnection in hds uav if(new_time-cam_time>100*1000*1000) { Thread::Warn("delta trop grand\n"); hasProblems=true; } output->GetMutex(); output->buffer=(char*)buffers[bufferIndex].start; output->ReleaseMutex(); output->SetDataTime(cam_time); ProcessUpdate(output); cam_time = new_time; } close(device); } int V4LCamera::QueueBuffer(int index) { struct v4l2_buffer buf; if(index>=0 && indexGetDataType().GetSize(); buffers[n_buffers].start =AllocFunction(output->GetDataType().GetSize()); } return 1; }; bool V4LCamera::HasProblems(void) { return hasProblems; } void V4LCamera::SetAutoGain(bool value) { SetProperty(V4L2_CID_AUTOGAIN, value); } void V4LCamera::SetAutoExposure(bool value) { Thread::Warn("not implemented\n"); } void V4LCamera::SetGain(float value) { SetProperty(V4L2_CID_GAIN, value); } void V4LCamera::SetExposure(float value) { SetProperty(V4L2_CID_EXPOSURE, value); } void V4LCamera::SetBrightness(float value) { SetProperty(V4L2_CID_BRIGHTNESS, value); } void V4LCamera::SetSaturation(float value) { SetProperty(V4L2_CID_SATURATION, value); } void V4LCamera::SetHue(float value) { SetProperty(V4L2_CID_HUE, value); } void V4LCamera::SetContrast(float value) { SetProperty(V4L2_CID_CONTRAST, value); } float V4LCamera::GetProperty(int property) { //get min and max value struct v4l2_queryctrl queryctrl; queryctrl.id = property; if(xioctl (device, VIDIOC_QUERYCTRL,&queryctrl)==-1) return -1; int min = queryctrl.minimum; int max = queryctrl.maximum; //set value struct v4l2_control control; memset (&control, 0, sizeof (v4l2_control)); control.id = property; if(xioctl (device,VIDIOC_G_CTRL, &control)==-1) return -1; return ((float)control.value - min + 1) / (max - min); } void V4LCamera::SetProperty(int property,float value) { //get min and max value struct v4l2_queryctrl queryctrl; queryctrl.id = property; xioctl (device, VIDIOC_QUERYCTRL,&queryctrl); int min = queryctrl.minimum; int max = queryctrl.maximum; //set value struct v4l2_control control; memset (&control, 0, sizeof (v4l2_control)); control.id = property; control.value = (int)(value * (max - min) + min); xioctl (device,VIDIOC_S_CTRL, &control); } int V4LCamera::xioctl( int fd, int request, void *arg) { int r; do r = ioctl (fd, request, arg); while (-1 == r && EINTR == errno); return r; } } // end namespace sensor } // end namespace flair