V4LCapture.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: V4LCapture.cpp
37 // Author: Alexander Kaspar, Tilo Gockel, Pedram Azad
38 // Date: 19.03.2009
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include "V4LCapture.h"
47 
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/ioctl.h>
51 #include <sys/mman.h>
52 
53 #include <fcntl.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <errno.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 
61 
62 // ****************************************************************************
63 // Constructor / Destructor
64 // ****************************************************************************
65 
66 CV4LCapture::CV4LCapture(const char *pDeviceName, int nChannel, CVideoCaptureInterface::VideoMode videoMode)
67 {
68  m_sDeviceName = "";
69  m_sDeviceName += pDeviceName;
70 
71  m_nChannel = nChannel;
72  m_videoMode = videoMode;
73 
74  width = -1;
75  height = -1;
76 }
77 
79 {
80 }
81 
82 
83 // ****************************************************************************
84 // Methods
85 // ****************************************************************************
86 
88 {
89  video_capability capability;
90  video_channel queryChannel;
91  video_channel selectedChannel;
92  video_window captureWindow;
93  video_picture imageProperties;
94  v4l2_capability cap;
95 
96 
97  selectedChannel.channel = m_nChannel;
98 
99  if ((m_nDeviceHandle = open(m_sDeviceName.c_str(), O_RDWR)) == -1)
100  { // could not open device
101  printf ("Could not open device %s - %s\n", m_sDeviceName.c_str(), strerror(errno));
102  return false;
103  }
104 
105  // get device capabilities
106  if (ioctl(m_nDeviceHandle, VIDIOCGCAP, &capability) == -1)
107  { // query failed
108  printf ("could not obtain device capabilities\n");
109  return false;
110  }
111 
112  if ((capability.type & VID_TYPE_CAPTURE) == 0)
113  { // this device cannot capture video to memory, exit
114  printf ("this device cannot capture video to memory\n");
115  return false;
116  }
117 
118  // enumerate and print out the channels
119  int i = 0;
120  while (i < capability.channels)
121  {
122  queryChannel.channel = i;
123  if (ioctl (m_nDeviceHandle, VIDIOCGCHAN, &queryChannel) != -1)
124  { // ioctl success, queryChannel contains information about this channel
125  printf ("%d. %s\n", queryChannel.channel, queryChannel.name);
126  }
127 
128  i++;
129  }
130 
131  selectedChannel.norm = VIDEO_MODE_NTSC;
132  if (ioctl (m_nDeviceHandle, VIDIOCSCHAN, &selectedChannel) == -1)
133  {
134  // could not set the selected channel
135  printf ("Could not set channel #%d\nNot a fatal error.", selectedChannel.channel);
136  }
137 
138  // set width and height attributes
139  switch (m_videoMode)
140  {
141  case e320x240: width = 320; height = 240; break;
142  case e640x480: width = 640; height = 480; break;
143  case e800x600: width = 800; height = 600; break;
144  case e768x576: width = 768; height = 576; break;
145  case e1024x768: width = 1024; height = 768; break;
146  case e1280x960: width = 1280; height = 960; break;
147  default: return false;
148  }
149 
150  // set the desired width and height
151  if ((capability.type & VID_TYPE_SCALES) != 0)
152  { // supports the ability to scale captured images
153  captureWindow.x = 0;
154  captureWindow.y = 0;
155  captureWindow.width = width;
156  captureWindow.height = height;
157  captureWindow.chromakey = 0;
158  captureWindow.flags = 0;
159  captureWindow.clips = 0;
160  captureWindow.clipcount = 0;
161 
162  if (ioctl (m_nDeviceHandle, VIDIOCSWIN, &captureWindow) == -1)
163  {
164  // could not set window values for capture
165  printf ("Could not set desired dimensions\nNot a fatal error.\n");
166  }
167  }
168 
169  // retrieve the actual width and height of the capturing images
170  if (ioctl(m_nDeviceHandle, VIDIOCGWIN, &captureWindow) == -1)
171  { // could not obtain specifics of capture window
172  printf ("Could not obtain capture window dimensions.\n");
173  }
174  width = captureWindow.width;
175  height = captureWindow.height;
176  printf ("Capturing dimensions are : %d, %d\n", width, height);
177 
178  // request that we capture to 24bit RGB
179 
180  // get image properties
181  if (ioctl (m_nDeviceHandle, VIDIOCGPICT, &imageProperties) != -1)
182  { // successfully retrieved the default image properties
183 
184  // the following values are for requesting 24bit RGB
185  imageProperties.depth = 24;
186  imageProperties.palette = VIDEO_PALETTE_RGB24;
187  if (ioctl (m_nDeviceHandle, VIDIOCSPICT, &imageProperties) == -1)
188  { // failed to set the image properties
189  printf ("Could not set the video depth and palette.\nPerhaps not a fatal error.\n");
190  }
191  }
192 
193  // verify that the requested capture pixel depth and palette succeeded
194  if (ioctl (m_nDeviceHandle, VIDIOCGPICT, &imageProperties) == -1)
195  { // failed to retrieve default image properties
196  printf ("Failed to retrieve the video depth and palette.\n");
197  return false;
198  }
199 
200  if ((imageProperties.depth != 24) || (imageProperties.palette != VIDEO_PALETTE_RGB24))
201  { // not a format our program supports
202  printf ("Format is not 24bit RGB.\n");
203  return false;
204  }
205 
206  printf ("Capture depth is 24bit RGB\n");
207 
208  // obtain memory about capture space
209  if (ioctl (m_nDeviceHandle, VIDIOCGMBUF, &memoryBuffer) == -1)
210  { // failed to retrieve information about capture memory space
211  printf ("Failed to retrieve information about MMIO space.\n");
212  return false;
213  }
214 
215 
216  // obtain memory mapped area
217  memoryMap = (char*)mmap (0, memoryBuffer.size, PROT_READ | PROT_WRITE, MAP_SHARED, m_nDeviceHandle, 0);
218  if ((intptr_t) memoryMap == -1)
219  { // failed to retrieve pointer to memory mapped area
220  printf ("Failed to obtain MMIO space.\n");
221  return false;
222  }
223 
224 
225  printf("Allocating structures...\n");
226  // allocate structures
227  mmaps = (struct video_mmap*)(malloc (memoryBuffer.frames * sizeof (struct video_mmap)));
228 
229  // fill out the fields
230  i = 0;
231  while (i < memoryBuffer.frames)
232  {
233  mmaps[i].frame = i;
234  mmaps[i].width = captureWindow.width;
235  mmaps[i].height = captureWindow.height;
236  mmaps[i].format = imageProperties.palette;
237  ++ i;
238  }
239 
240  printf("Requesting capture buffers...\n");
241  // request capture to each buffer except the last one
242  i = 0;
243  while (i < (memoryBuffer.frames-1))
244  {
245  if (ioctl (m_nDeviceHandle, VIDIOCMCAPTURE, &mmaps[i]) == -1)
246  { // capture request failed
247  }
248  ++ i;
249  }
250 
251  printf("Set index buffer...\n");
252  // set our index to the last buffer
253  bufferIndex = memoryBuffer.frames-1;
254 
255  return true;
256 }
257 
259 {
260  // send a request to begin capturing to the currently indexed buffer
261  if (ioctl (m_nDeviceHandle, VIDIOCMCAPTURE, &mmaps[bufferIndex]) == -1)
262  {
263  // capture request failed
264  }
265 
266  // move bufferIndex to the next frame
267  if (++bufferIndex == memoryBuffer.frames)
268  {
269  // bufferIndex is indexing beyond the last buffer
270  // set it to index the first buffer
271  bufferIndex = 0;
272  }
273 
274  // wait for the currently indexed frame to complete capture
275  if (ioctl(m_nDeviceHandle, VIDIOCSYNC, &mmaps[bufferIndex]) == -1)
276  { // sync request failed
277  }
278 
279  // return the address of the frame data for the current buffer index
280  const unsigned char *frame = (const unsigned char *) (memoryMap + memoryBuffer.offsets[bufferIndex]);
281  unsigned char *output = ppImages[0]->pixels;
282 
283  const int nBytes = ppImages[0]->width * ppImages[0]->height * 3;
284 
285  for (int i = 0; i < nBytes; i += 3)
286  {
287  output[i] = frame[i + 2];
288  output[i + 1] = frame[i + 1];
289  output[i + 2] = frame[i];
290  }
291 
292  return true;
293 }
294 
296 {
297  // free the video_mmap structures
298  free (mmaps);
299 
300  // unmap the capture memory
301  munmap (memoryMap, memoryBuffer.size);
302 
303  // close the device
304  close (m_nDeviceHandle);
305 }
void CloseCamera()
Definition: V4LCapture.cpp:295
int width
The width of the image in pixels.
Definition: ByteImage.h:257
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
GLenum cap
Definition: glext.h:5915
video_mbuf memoryBuffer
Definition: V4LCapture.h:96
char * memoryMap
Definition: V4LCapture.h:98
int m_nChannel
Definition: V4LCapture.h:94
bool CaptureImage(CByteImage **ppImages)
Definition: V4LCapture.cpp:258
CVideoCaptureInterface::VideoMode m_videoMode
Definition: V4LCapture.h:89
int m_nDeviceHandle
Definition: V4LCapture.h:93
int height
The height of the image in pixels.
Definition: ByteImage.h:264
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
std::string m_sDeviceName
Definition: V4LCapture.h:92
int bufferIndex
Definition: V4LCapture.h:101
video_mmap * mmaps
Definition: V4LCapture.h:97
bool OpenCamera()
Definition: V4LCapture.cpp:87
CV4LCapture(const char *pDeviceName="/dev/video0", int nChannel=0, CVideoCaptureInterface::VideoMode videoMode=CVideoCaptureInterface::e640x480)
Definition: V4LCapture.cpp:66


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28