source: flair-src/branches/sanscv/lib/FlairSensorActuator/src/V4LCamera.cpp@ 328

Last change on this file since 328 was 328, checked in by Sanahuja Guillaume, 3 years ago

add imagesize to iplimage

File size: 13.0 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2014/07/17
6// filename: V4LCamera.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: base class for V4l camera
14//
15//
16/*********************************************************************/
17
18#include "V4LCamera.h"
19#include <GroupBox.h>
20#include <DoubleSpinBox.h>
21#include <CheckBox.h>
22#include <Label.h>
23#include <Image.h>
24#include <FrameworkManager.h>
25#include <fcntl.h>
26#include <linux/videodev2.h>
27#include <sys/ioctl.h>
28#include <unistd.h>
29#include <cstring>
30#include <sys/mman.h>
31#include <VisionFilter.h>
32
33#define DEFAULT_V4L_BUFFERS 4
34
35using std::string;
36using namespace flair::core;
37using namespace flair::gui;
38
39namespace flair {
40namespace sensor {
41
42V4LCamera::V4LCamera(string name,
43 uint8_t camera_index, uint16_t width, uint16_t height,
44 Image::Type::Format format, uint8_t priority)
45 : Thread(getFrameworkManager(), name, priority),
46 Camera(name, width, height, format) {
47
48 string deviceName="/dev/video"+std::to_string(camera_index);
49 device = open(deviceName.c_str(), O_RDWR | O_NONBLOCK);
50 if (device == -1) {
51 Thread::Err("Cannot open %s\n",deviceName.c_str());
52 } else {
53 Printf("V4LCamera %s, opened %s\n",name.c_str(),deviceName.c_str());
54 }
55
56 struct v4l2_capability cap;
57 memset(&cap, 0, sizeof (v4l2_capability));
58 if (xioctl (device, VIDIOC_QUERYCAP, &cap)==-1) {
59 Thread::Err("VIDIOC_QUERYCAP xioctl\n");
60 }
61 if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
62 Thread::Err("device is unable to capture video memory.\n");
63 }
64 /*
65 * unnecessary?
66 struct video_capability capability;
67 memset(&capability, 0, sizeof (video_capability));
68 capability.type = cap.capabilities;
69
70 // Query channels number
71 if (xioctl(device, VIDIOC_G_INPUT, &capability.channels)==-1) {
72 Thread::Err("VIDIOC_G_INPUT xioctl\n");
73 }
74 */
75 //get v4l2_format
76 struct v4l2_format form;
77 form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
78 if(xioctl (device, VIDIOC_G_FMT,&form)==-1) {
79 Thread::Err("VIDIOC_G_FMT xioctl\n");
80 }
81
82 //set width, height and format
83 if (format == Image::Type::Format::UYVY) {
84 form.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
85 } else if (format == Image::Type::Format::YUYV) {
86 form.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
87 } else {
88 Thread::Err("format not supported\n");
89 }
90
91 form.fmt.pix.width = width;
92 form.fmt.pix.height = height;
93 form.fmt.win.chromakey = 0;
94 form.fmt.win.field = V4L2_FIELD_ANY;
95 form.fmt.win.clips = 0;
96 form.fmt.win.clipcount = 0;
97 form.fmt.pix.field = V4L2_FIELD_ANY;
98 if(xioctl (device, VIDIOC_S_FMT,&form)==-1) {
99 Thread::Err("VIDIOC_S_FMT xioctl\n");
100 }
101
102 //alloc and queue bufs
103 AllocBuffers();
104 for (int bufferIndex = 0; bufferIndex < nbBuffers;++bufferIndex) {
105 QueueBuffer(bufferIndex);
106 }
107
108 //alloc img buf
109 Printf("TODO: alloc bufs in cmem and uses it for v4l\n");
110 output->buffer=AllocFunction(width*height*2);
111
112 // enable the streaming
113 v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
114 if (xioctl (device, VIDIOC_STREAMON,&type)==-1) {
115 Thread::Err("VIDIOC_STREAMON xioctl\n");
116 }
117
118 // skip first frame. it is often bad -- this is unnotied in traditional apps,
119 // but could be fatal if bad jpeg is enabled
120 bufferIndex=-1;
121 GrabFrame();
122
123 // ground station
124 gain = new DoubleSpinBox(GetGroupBox()->NewRow(), "gain:", 0, 1, 0.1);
125 exposure = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "exposure:", 0,1, 0.1);
126 bright = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "bright:", 0, 1, 0.1);
127 contrast = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "contrast:", 0,1, 0.1);
128 hue = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "hue:", 0, 1, 0.1);
129 sharpness = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "sharpness:", 0, 1, 0.1);
130 sat = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "saturation:", 0, 1,0.1);
131 autogain = new CheckBox(GetGroupBox()->NewRow(), "autogain:");
132 autoexposure = new CheckBox(GetGroupBox()->LastRowLastCol(), "autoexposure:");
133 awb = new CheckBox(GetGroupBox()->LastRowLastCol(), "awb:");
134 fps = new Label(GetGroupBox()->NewRow(), "fps");
135
136 hasProblems=false;
137}
138
139V4LCamera::~V4LCamera() {
140 FreeFunction(output->buffer);
141 SafeStop();
142 Join();
143}
144
145void V4LCamera::Run(void) {
146 Time cam_time, new_time, fpsNow, fpsPrev;
147 int fpsCounter = 0;
148
149 // init image old
150 GrabFrame();
151 cam_time = GetTime();
152 fpsPrev = cam_time;
153
154 while (!ToBeStopped()) {
155 //check for ps3eye deconnection in hds uav
156 if(hasProblems==false) {
157 struct v4l2_format form;
158 form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
159 xioctl(device, VIDIOC_G_FMT,&form);
160 if(xioctl (device, VIDIOC_G_FMT,&form)<0) {
161 Thread::Warn("camera disconnected\n");
162 hasProblems=true;
163 }
164 }
165
166 // fps counter
167 fpsCounter++;
168 if (GetTime() > (fpsPrev + 5 * (Time)1000000000)) {
169 // every 5 secondes
170 fpsNow = GetTime();
171 fps->SetText("fps: %.1f",
172 fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
173 fpsCounter = 0;
174 fpsPrev = fpsNow;
175 }
176
177 // cam properties
178 if (gain->ValueChanged() == true && autogain->Value() == false)
179 SetGain(gain->Value());
180 if (exposure->ValueChanged() == true && autoexposure->Value() == false)
181 SetExposure(exposure->Value());
182 if (bright->ValueChanged() == true)
183 SetBrightness(bright->Value());
184 if (sat->ValueChanged() == true)
185 SetSaturation(sat->Value());
186 if (contrast->ValueChanged() == true)
187 SetContrast(contrast->Value());
188 if (hue->ValueChanged() == true)
189 SetHue(hue->Value());
190 if (sharpness->ValueChanged() == true)
191 SetProperty(V4L2_CID_SHARPNESS, sharpness->Value());
192 if (autogain->ValueChanged() == true) {
193 if (autogain->Value() == true) {
194 gain->setEnabled(false);
195 } else {
196 gain->setEnabled(true);
197 SetGain(gain->Value());
198 }
199 SetAutoGain(autogain->Value());
200 }
201 if (autoexposure->ValueChanged() == true) {
202 if (autoexposure->Value() == true) {
203 exposure->setEnabled(false);
204 } else {
205 exposure->setEnabled(true);
206 SetExposure(exposure->Value());
207 }
208 SetAutoExposure(autoexposure->Value());
209 }
210 if (awb->ValueChanged() == true)
211 SetProperty(V4L2_CID_AUTO_WHITE_BALANCE, awb->Value());
212
213 // get picture
214 GrabFrame();
215 new_time = GetTime();
216
217 //check for ps3eye deconnection in hds uav
218 if(new_time-cam_time>100*1000*1000) {
219 Thread::Warn("delta trop grand\n");
220 hasProblems=true;
221 }
222//Printf("todo allocate 1 frame and copy here\n");
223 output->GetMutex();
224 memcpy(output->buffer,buffers[bufferIndex].start,buffers[bufferIndex].length);
225 output->ReleaseMutex();
226
227 output->SetDataTime(cam_time);
228 ProcessUpdate(output);
229
230 cam_time = new_time;
231 }
232
233 close(device);
234}
235
236int V4LCamera::QueueBuffer(int index) {
237 struct v4l2_buffer buf;
238 if(index>=0 && index<nbBuffers) {
239 memset(&buf, 0, sizeof (v4l2_buffer));
240 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
241 buf.memory = V4L2_MEMORY_MMAP;
242 buf.index = (unsigned long)index;
243
244 if (xioctl (device, VIDIOC_QBUF, &buf)==-1) {
245 Thread::Err("VIDIOC_QBUF xioctl\n");
246 return -1;
247 }
248 }
249 return 0;
250}
251
252int V4LCamera::GrabFrame(void) {
253 //queue previous buffer
254 if(QueueBuffer(bufferIndex)<0) return -1;
255
256 fd_set fds;
257 struct timeval tv;
258 FD_ZERO (&fds);
259 FD_SET (device, &fds);
260
261 tv.tv_sec = 0;
262 tv.tv_usec = 100000;
263//Printf("%lld\n",GetTimeMS());
264 int r = select (device+1, &fds, NULL, NULL, &tv);
265//Printf("%lld select ok\n",GetTimeMS());
266 if (-1 == r) {
267 char errorMsg[256];
268 Thread::Err("select (%s)\n", strerror_r(-r, errorMsg, sizeof(errorMsg)));
269 return -1;
270 }
271
272 if (0 == r) {
273 Thread::Err("select timeout\n");
274 return -1;
275 }
276
277 struct v4l2_buffer buf;
278 memset(&buf, 0, sizeof (v4l2_buffer));
279 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
280 buf.memory = V4L2_MEMORY_MMAP;
281
282 //get last captured image
283 int prevDQbuf=-1;
284 for(int i=0;i<4;i++) {
285 //Printf("%lld ioctl\n",GetTimeMS());
286 if (xioctl (device, VIDIOC_DQBUF, &buf)==-1) {
287 //Printf("iter %i err %i\n",i,errno);
288 if (errno==EAGAIN) {
289 //Printf("exit buf %i\n",buf.index);
290 break;
291 } else {
292 Thread::Err("VIDIOC_DQBUF xioctl\n");
293 return -1;
294 }
295 } else {
296 //Printf("buf %i\n",buf.index);
297 if(prevDQbuf!=-1) {
298 QueueBuffer(prevDQbuf);
299 //Printf("queued %i\n",prevDQbuf);
300 }
301 prevDQbuf=buf.index;
302 }
303 //Printf("%lld ioctl ok\n",GetTimeMS());
304 }
305
306 if(buf.index >= nbBuffers) {
307 Thread::Err("buf.index >= nbBuffers\n");
308 return -1;
309 }
310
311 bufferIndex=buf.index;
312
313 return 1;
314}
315
316int V4LCamera::AllocBuffers(void) {
317 struct v4l2_requestbuffers requestbuffers;
318 memset(&requestbuffers, 0, sizeof (v4l2_requestbuffers));
319
320 unsigned int buffer_number = DEFAULT_V4L_BUFFERS;
321
322 try_again:
323
324 requestbuffers.count = buffer_number;
325 requestbuffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
326 requestbuffers.memory = V4L2_MEMORY_MMAP;
327
328 if (xioctl (device, VIDIOC_REQBUFS, &requestbuffers)==-1) {
329 if (EINVAL == errno) {
330 Thread::Err("memory mapping not supported\n");
331 } else {
332 Thread::Err ("VIDIOC_REQBUFS xioctl\n");
333 }
334 return -1;
335 }
336
337 if (requestbuffers.count < buffer_number) {
338 if (buffer_number == 1) {
339 Thread::Err("Insufficient buffer memory\n");
340 return -1;
341 } else {
342 buffer_number--;
343 Thread::Warn ("Insufficient buffer memory, decreasing buffers\n");
344 goto try_again;
345 }
346 }
347
348 for (int n_buffers = 0; n_buffers < requestbuffers.count; ++n_buffers) {
349 struct v4l2_buffer buf;
350
351 memset(&buf, 0, sizeof (v4l2_buffer));
352
353 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
354 buf.memory = V4L2_MEMORY_MMAP;
355 buf.index = n_buffers;
356
357 if (xioctl (device, VIDIOC_QUERYBUF, &buf)==-1) {
358 Thread::Err("VIDIOC_QUERYBUF xioctl\n");
359 return -1;
360 }
361
362 buffers[n_buffers].length = buf.length;
363 buffers[n_buffers].start =
364 mmap (NULL /* start anywhere */,
365 buf.length,
366 PROT_READ | PROT_WRITE /* required */,
367 MAP_SHARED /* recommended */,
368 device, buf.m.offset);
369//Printf("buffer %i start %x length %i\n",n_buffers,buffers[n_buffers].start,buffers[n_buffers].length);
370 if (MAP_FAILED == buffers[n_buffers].start) {
371 Thread::Err("mmap\n");
372 return -1;
373 }
374 }
375 nbBuffers=requestbuffers.count;
376//Printf("allocated %i buffers\n",nbBuffers);
377 return 1;
378};
379
380bool V4LCamera::HasProblems(void) {
381 return hasProblems;
382}
383
384void V4LCamera::SetAutoGain(bool value) {
385 SetProperty(V4L2_CID_AUTOGAIN, value);
386}
387
388void V4LCamera::SetAutoExposure(bool value) {
389 Thread::Warn("not implemented\n");
390}
391
392void V4LCamera::SetGain(float value) {
393 SetProperty(V4L2_CID_GAIN, value);
394}
395
396void V4LCamera::SetExposure(float value) {
397 SetProperty(V4L2_CID_EXPOSURE, value);
398}
399
400void V4LCamera::SetBrightness(float value) {
401 SetProperty(V4L2_CID_BRIGHTNESS, value);
402}
403
404void V4LCamera::SetSaturation(float value) {
405 SetProperty(V4L2_CID_SATURATION, value);
406}
407
408void V4LCamera::SetHue(float value) {
409 SetProperty(V4L2_CID_HUE, value);
410}
411
412void V4LCamera::SetContrast(float value) {
413 SetProperty(V4L2_CID_CONTRAST, value);
414}
415
416float V4LCamera::GetProperty(int property) {
417 //get min and max value
418 struct v4l2_queryctrl queryctrl;
419 queryctrl.id = property;
420 if(xioctl (device, VIDIOC_QUERYCTRL,&queryctrl)==-1) return -1;
421 int min = queryctrl.minimum;
422 int max = queryctrl.maximum;
423
424 //set value
425 struct v4l2_control control;
426 memset (&control, 0, sizeof (v4l2_control));
427 control.id = property;
428 if(xioctl (device,VIDIOC_G_CTRL, &control)==-1) return -1;
429
430 return ((float)control.value - min + 1) / (max - min);
431}
432
433void V4LCamera::SetProperty(int property,float value) {
434 //get min and max value
435 struct v4l2_queryctrl queryctrl;
436 queryctrl.id = property;
437 xioctl (device, VIDIOC_QUERYCTRL,&queryctrl);
438 int min = queryctrl.minimum;
439 int max = queryctrl.maximum;
440
441 //set value
442 struct v4l2_control control;
443 memset (&control, 0, sizeof (v4l2_control));
444 control.id = property;
445 control.value = (int)(value * (max - min) + min);
446 xioctl (device,VIDIOC_S_CTRL, &control);
447}
448
449int V4LCamera::xioctl( int fd, int request, void *arg) {
450 int r;
451
452 do r = ioctl (fd, request, arg);
453 while (-1 == r && EINTR == errno);
454
455 return r;
456}
457
458} // end namespace sensor
459} // end namespace flair
Note: See TracBrowser for help on using the repository browser.