32
32
#include < chrono>
33
33
#include < cstdint>
34
34
#include < cstdlib>
35
+ #include < cstring>
35
36
#include < iostream>
36
37
#include < memory>
37
38
#include < sstream>
@@ -64,7 +65,7 @@ int32_t main(int32_t argc, char **argv) {
64
65
65
66
const float FREQ{static_cast <float >(std::stof (commandlineArguments[" freq" ]))};
66
67
if ( !(FREQ > 0 ) ) {
67
- std::cerr << argv[ 0 ] << " : freq must be larger than 0; found " << FREQ << " ." << std::endl;
68
+ std::cerr << " [opendlv-device-camera-v4l] : freq must be larger than 0; found " << FREQ << " ." << std::endl;
68
69
return retCode = 1 ;
69
70
}
70
71
@@ -84,22 +85,22 @@ int32_t main(int32_t argc, char **argv) {
84
85
// V4L initialization.
85
86
int videoDevice = open (commandlineArguments[" camera" ].c_str (), O_RDWR);
86
87
if (-1 == videoDevice) {
87
- std::cerr << argv[ 0 ] << " : Failed to open capture device: " << commandlineArguments[" camera" ] << std::endl;
88
+ std::cerr << " [opendlv-device-camera-v4l] : Failed to open capture device: " << commandlineArguments[" camera" ] << std::endl;
88
89
return retCode = 1 ;
89
90
}
90
91
91
92
struct v4l2_capability v4l2_cap;
92
93
::memset (&v4l2_cap, 0 , sizeof (struct v4l2_capability ));
93
94
if (0 > ::ioctl (videoDevice, VIDIOC_QUERYCAP, &v4l2_cap)) {
94
- std::cerr << argv[ 0 ] << " : Failed to query capture device: " << commandlineArguments[" camera" ] << std::endl;
95
+ std::cerr << " [opendlv-device-camera-v4l] : Failed to query capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
95
96
return retCode = 1 ;
96
97
}
97
98
if (!(v4l2_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
98
- std::cerr << argv[ 0 ] << " : Capture device: " << commandlineArguments[" camera" ] << " does not support V4L2_CAP_CAPTURE." << std::endl;
99
+ std::cerr << " [opendlv-device-camera-v4l] : Capture device: " << commandlineArguments[" camera" ] << " does not support V4L2_CAP_CAPTURE." << std::endl;
99
100
return retCode = 1 ;
100
101
}
101
102
if (!(v4l2_cap.capabilities & V4L2_CAP_STREAMING)) {
102
- std::cerr << argv[ 0 ] << " : Capture device: " << commandlineArguments[" camera" ] << " does not support V4L2_CAP_STREAMING." << std::endl;
103
+ std::cerr << " [opendlv-device-camera-v4l] : Capture device: " << commandlineArguments[" camera" ] << " does not support V4L2_CAP_STREAMING." << std::endl;
103
104
return retCode = 1 ;
104
105
}
105
106
@@ -113,24 +114,24 @@ int32_t main(int32_t argc, char **argv) {
113
114
v4l2_fmt.fmt .pix .field = V4L2_FIELD_ANY;
114
115
115
116
if (0 > ::ioctl (videoDevice, VIDIOC_S_FMT, &v4l2_fmt)) {
116
- std::cerr << argv[ 0 ] << " : Capture device: " << commandlineArguments[" camera" ] << " does not support requested format." << std::endl;
117
+ std::cerr << " [opendlv-device-camera-v4l] : Capture device: " << commandlineArguments[" camera" ] << " does not support requested format." << " , error: " << errno << " : " << strerror (errno) << std::endl;
117
118
return retCode = 1 ;
118
119
}
119
120
120
121
if ((v4l2_fmt.fmt .pix .width != WIDTH) ||
121
122
(v4l2_fmt.fmt .pix .height != HEIGHT)) {
122
- std::cerr << argv[ 0 ] << " : Capture device: " << commandlineArguments[" camera" ] << " does not support requested " << WIDTH << " x " << HEIGHT << std::endl;
123
+ std::cerr << " [opendlv-device-camera-v4l] : Capture device: " << commandlineArguments[" camera" ] << " does not support requested " << WIDTH << " x " << HEIGHT << std::endl;
123
124
return retCode = 1 ;
124
125
}
125
126
126
127
bool isMJPEG{false };
127
128
bool isYUYV422{false };
128
129
if (v4l2_fmt.fmt .pix .pixelformat == V4L2_PIX_FMT_MJPEG) {
129
- std::clog << argv[ 0 ] << " : Capture device: " << commandlineArguments[" camera" ] << " provides MJPEG stream." << std::endl;
130
+ std::clog << " [opendlv-device-camera-v4l] : Capture device: " << commandlineArguments[" camera" ] << " provides MJPEG stream." << std::endl;
130
131
isMJPEG = true ;
131
132
}
132
133
if (v4l2_fmt.fmt .pix .pixelformat == V4L2_PIX_FMT_YUYV) {
133
- std::clog << argv[ 0 ] << " : Capture device: " << commandlineArguments[" camera" ] << " provides YUYV 4:2:2 stream." << std::endl;
134
+ std::clog << " [opendlv-device-camera-v4l] : Capture device: " << commandlineArguments[" camera" ] << " provides YUYV 4:2:2 stream." << std::endl;
134
135
isYUYV422 = true ;
135
136
}
136
137
@@ -144,19 +145,19 @@ int32_t main(int32_t argc, char **argv) {
144
145
if (0 > ::ioctl (videoDevice, VIDIOC_S_PARM, &v4l2_stream_parm) ||
145
146
v4l2_stream_parm.parm .capture .timeperframe .numerator != 1 ||
146
147
v4l2_stream_parm.parm .capture .timeperframe .denominator != static_cast <uint32_t >(FREQ)) {
147
- std::cerr << argv[ 0 ] << " : Capture device: " << commandlineArguments[" camera" ] << " does not support requested " << FREQ << " fps." << std::endl;
148
+ std::cerr << " [opendlv-device-camera-v4l] : Capture device: " << commandlineArguments[" camera" ] << " does not support requested " << FREQ << " fps." << std::endl;
148
149
return retCode = 1 ;
149
150
}
150
151
151
- const uint32_t BUFFER_COUNT{static_cast < uint32_t >(FREQ) + 10 };
152
+ const uint32_t BUFFER_COUNT{32 };
152
153
153
154
struct v4l2_requestbuffers v4l2_req_bufs;
154
155
::memset (&v4l2_req_bufs, 0 , sizeof (struct v4l2_requestbuffers ));
155
156
v4l2_req_bufs.count = BUFFER_COUNT;
156
157
v4l2_req_bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
157
158
v4l2_req_bufs.memory = V4L2_MEMORY_MMAP;
158
159
if (0 > ::ioctl (videoDevice, VIDIOC_REQBUFS, &v4l2_req_bufs)) {
159
- std::cerr << argv[ 0 ] << " : Could not allocate buffers for capture device: " << commandlineArguments[" camera" ] << std::endl;
160
+ std::cerr << " [opendlv-device-camera-v4l] : Could not allocate buffers for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
160
161
return retCode = 1 ;
161
162
}
162
163
@@ -174,15 +175,15 @@ int32_t main(int32_t argc, char **argv) {
174
175
v4l2_buf.memory = V4L2_MEMORY_MMAP;
175
176
176
177
if (0 > ::ioctl (videoDevice, VIDIOC_QUERYBUF, &v4l2_buf)) {
177
- std::cerr << argv[ 0 ] << " : Could not query buffer " << i << " for capture device: " << commandlineArguments[" camera" ] << std::endl;
178
+ std::cerr << " [opendlv-device-camera-v4l] : Could not query buffer " << + i << " for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
178
179
return retCode = 1 ;
179
180
}
180
181
181
182
buffers[i].length = v4l2_buf.length ;
182
183
183
184
buffers[i].buf = mmap (0 , buffers[i].length , PROT_READ, MAP_SHARED, videoDevice, v4l2_buf.m .offset );
184
185
if (MAP_FAILED == buffers[i].buf ) {
185
- std::cerr << argv[ 0 ] << " : Could not map buffer " << i << " for capture device: " << commandlineArguments[" camera" ] << std::endl;
186
+ std::cerr << " [opendlv-device-camera-v4l] : Could not map buffer " << + i << " for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
186
187
return retCode = 1 ;
187
188
}
188
189
}
@@ -196,32 +197,32 @@ int32_t main(int32_t argc, char **argv) {
196
197
v4l2_buf.memory = V4L2_MEMORY_MMAP;
197
198
198
199
if (0 > ::ioctl (videoDevice, VIDIOC_QBUF, &v4l2_buf)) {
199
- std::cerr << argv[ 0 ] << " : Could not queue buffer " << i << " for capture device: " << commandlineArguments[" camera" ] << std::endl;
200
+ std::cerr << " [opendlv-device-camera-v4l] : Could not queue buffer " << + i << " for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
200
201
return retCode = 1 ;
201
202
}
202
203
}
203
204
204
205
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
205
206
if (0 > ::ioctl (videoDevice, VIDIOC_STREAMON, &type)) {
206
- std::cerr << argv[ 0 ] << " : Could not start video stream for capture device: " << commandlineArguments[" camera" ] << std::endl;
207
+ std::cerr << " [opendlv-device-camera-v4l] : Could not start video stream for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
207
208
return retCode = 1 ;
208
209
}
209
210
210
211
std::unique_ptr<cluon::SharedMemory> sharedMemoryI420 (new cluon::SharedMemory{NAME_I420, WIDTH * HEIGHT * 3 /2 });
211
212
if (!sharedMemoryI420 || !sharedMemoryI420->valid ()) {
212
- std::cerr << argv[ 0 ] << " : Failed to create shared memory '" << NAME_I420 << " '." << std::endl;
213
+ std::cerr << " [opendlv-device-camera-v4l] : Failed to create shared memory '" << NAME_I420 << " '." << std::endl;
213
214
return retCode = 1 ;
214
215
}
215
216
216
217
std::unique_ptr<cluon::SharedMemory> sharedMemoryARGB (new cluon::SharedMemory{NAME_ARGB, WIDTH * HEIGHT * 4 });
217
218
if (!sharedMemoryARGB || !sharedMemoryARGB->valid ()) {
218
- std::cerr << argv[ 0 ] << " : Failed to create shared memory '" << NAME_ARGB << " '." << std::endl;
219
+ std::cerr << " [opendlv-device-camera-v4l] : Failed to create shared memory '" << NAME_ARGB << " '." << std::endl;
219
220
return retCode = 1 ;
220
221
}
221
222
222
223
if ( (sharedMemoryI420 && sharedMemoryI420->valid ()) &&
223
224
(sharedMemoryARGB && sharedMemoryARGB->valid ()) ) {
224
- std::clog << argv[ 0 ] << " : Data from camera '" << commandlineArguments[" camera" ]<< " ' available in I420 format in shared memory '" << sharedMemoryI420->name () << " ' (" << sharedMemoryI420->size () << " ) and in ARGB format in shared memory '" << sharedMemoryARGB->name () << " ' (" << sharedMemoryARGB->size () << " )." << std::endl;
225
+ std::clog << " [opendlv-device-camera-v4l] : Data from camera '" << commandlineArguments[" camera" ]<< " ' available in I420 format in shared memory '" << sharedMemoryI420->name () << " ' (" << sharedMemoryI420->size () << " ) and in ARGB format in shared memory '" << sharedMemoryARGB->name () << " ' (" << sharedMemoryARGB->size () << " )." << std::endl;
225
226
226
227
// Define timeout for select system call.
227
228
struct timeval timeout {};
@@ -258,7 +259,7 @@ int32_t main(int32_t argc, char **argv) {
258
259
v4l2_buf.memory = V4L2_MEMORY_MMAP;
259
260
260
261
if (0 > ::ioctl (videoDevice, VIDIOC_DQBUF, &v4l2_buf)) {
261
- std::cerr << argv[ 0 ] << " : Could not dequeue buffer for capture device: " << commandlineArguments[" camera" ] << std::endl;
262
+ std::cerr << " [opendlv-device-camera-v4l] : Could not dequeue buffer for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
262
263
return false ;
263
264
}
264
265
@@ -309,7 +310,7 @@ int32_t main(int32_t argc, char **argv) {
309
310
}
310
311
311
312
if (0 > ::ioctl (videoDevice, VIDIOC_QBUF, &v4l2_buf)) {
312
- std::cerr << argv[ 0 ] << " : Could not requeue buffer for capture device: " << commandlineArguments[" camera" ] << std::endl;
313
+ std::cerr << " [opendlv-device-camera-v4l] : Could not requeue buffer for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
313
314
return false ;
314
315
}
315
316
}
@@ -321,7 +322,7 @@ int32_t main(int32_t argc, char **argv) {
321
322
322
323
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
323
324
if (0 > ::ioctl (videoDevice, VIDIOC_STREAMOFF, &type)) {
324
- std::cerr << argv[ 0 ] << " : Could not stop video stream for capture device: " << commandlineArguments[" camera" ] << std::endl;
325
+ std::cerr << " [opendlv-device-camera-v4l] : Could not stop video stream for capture device: " << commandlineArguments[" camera" ] << " , error: " << errno << " : " << strerror (errno) << std::endl;
325
326
return retCode = 1 ;
326
327
}
327
328
0 commit comments