Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit 94eeaab

Browse files
committed
* Set buffers to 32
Signed-off-by: Christian Berger <christian.berger@gu.se>
1 parent e3e1895 commit 94eeaab

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ To run this microservice using our pre-built Docker multi-arch images to open
4242
a Video4Linux-supported camera, simply start it as follows:
4343

4444
```
45-
docker run --rm -ti --init --ipc=host -v /tmp:/tmp -e DISPLAY=$DISPLAY --device /dev/video0 chalmersrevere/opendlv-device-camera-v4l-multi:v0.0.4 --camera=/dev/video0 --width=640 --height=480 --freq=20 --verbose
45+
docker run --rm -ti --init --ipc=host -v /tmp:/tmp -e DISPLAY=$DISPLAY --device /dev/video0 chalmersrevere/opendlv-device-camera-v4l-multi:v0.0.5 --camera=/dev/video0 --width=640 --height=480 --freq=20 --verbose
4646
```
4747

4848
## Build from sources on the example of Ubuntu 16.04 LTS

src/opendlv-device-camera-v4l.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <chrono>
3333
#include <cstdint>
3434
#include <cstdlib>
35+
#include <cstring>
3536
#include <iostream>
3637
#include <memory>
3738
#include <sstream>
@@ -64,7 +65,7 @@ int32_t main(int32_t argc, char **argv) {
6465

6566
const float FREQ{static_cast<float>(std::stof(commandlineArguments["freq"]))};
6667
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;
6869
return retCode = 1;
6970
}
7071

@@ -84,22 +85,22 @@ int32_t main(int32_t argc, char **argv) {
8485
// V4L initialization.
8586
int videoDevice = open(commandlineArguments["camera"].c_str(), O_RDWR);
8687
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;
8889
return retCode = 1;
8990
}
9091

9192
struct v4l2_capability v4l2_cap;
9293
::memset(&v4l2_cap, 0, sizeof(struct v4l2_capability));
9394
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;
9596
return retCode = 1;
9697
}
9798
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;
99100
return retCode = 1;
100101
}
101102
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;
103104
return retCode = 1;
104105
}
105106

@@ -113,24 +114,24 @@ int32_t main(int32_t argc, char **argv) {
113114
v4l2_fmt.fmt.pix.field = V4L2_FIELD_ANY;
114115

115116
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;
117118
return retCode = 1;
118119
}
119120

120121
if ((v4l2_fmt.fmt.pix.width != WIDTH) ||
121122
(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;
123124
return retCode = 1;
124125
}
125126

126127
bool isMJPEG{false};
127128
bool isYUYV422{false};
128129
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;
130131
isMJPEG = true;
131132
}
132133
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;
134135
isYUYV422 = true;
135136
}
136137

@@ -144,19 +145,19 @@ int32_t main(int32_t argc, char **argv) {
144145
if (0 > ::ioctl(videoDevice, VIDIOC_S_PARM, &v4l2_stream_parm) ||
145146
v4l2_stream_parm.parm.capture.timeperframe.numerator != 1 ||
146147
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;
148149
return retCode = 1;
149150
}
150151

151-
const uint32_t BUFFER_COUNT{static_cast<uint32_t>(FREQ) + 10};
152+
const uint32_t BUFFER_COUNT{32};
152153

153154
struct v4l2_requestbuffers v4l2_req_bufs;
154155
::memset(&v4l2_req_bufs, 0, sizeof(struct v4l2_requestbuffers));
155156
v4l2_req_bufs.count = BUFFER_COUNT;
156157
v4l2_req_bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
157158
v4l2_req_bufs.memory = V4L2_MEMORY_MMAP;
158159
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;
160161
return retCode = 1;
161162
}
162163

@@ -174,15 +175,15 @@ int32_t main(int32_t argc, char **argv) {
174175
v4l2_buf.memory = V4L2_MEMORY_MMAP;
175176

176177
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;
178179
return retCode = 1;
179180
}
180181

181182
buffers[i].length = v4l2_buf.length;
182183

183184
buffers[i].buf = mmap(0, buffers[i].length, PROT_READ, MAP_SHARED, videoDevice, v4l2_buf.m.offset);
184185
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;
186187
return retCode = 1;
187188
}
188189
}
@@ -196,32 +197,32 @@ int32_t main(int32_t argc, char **argv) {
196197
v4l2_buf.memory = V4L2_MEMORY_MMAP;
197198

198199
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;
200201
return retCode = 1;
201202
}
202203
}
203204

204205
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
205206
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;
207208
return retCode = 1;
208209
}
209210

210211
std::unique_ptr<cluon::SharedMemory> sharedMemoryI420(new cluon::SharedMemory{NAME_I420, WIDTH * HEIGHT * 3/2});
211212
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;
213214
return retCode = 1;
214215
}
215216

216217
std::unique_ptr<cluon::SharedMemory> sharedMemoryARGB(new cluon::SharedMemory{NAME_ARGB, WIDTH * HEIGHT * 4});
217218
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;
219220
return retCode = 1;
220221
}
221222

222223
if ( (sharedMemoryI420 && sharedMemoryI420->valid()) &&
223224
(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;
225226

226227
// Define timeout for select system call.
227228
struct timeval timeout {};
@@ -258,7 +259,7 @@ int32_t main(int32_t argc, char **argv) {
258259
v4l2_buf.memory = V4L2_MEMORY_MMAP;
259260

260261
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;
262263
return false;
263264
}
264265

@@ -309,7 +310,7 @@ int32_t main(int32_t argc, char **argv) {
309310
}
310311

311312
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;
313314
return false;
314315
}
315316
}
@@ -321,7 +322,7 @@ int32_t main(int32_t argc, char **argv) {
321322

322323
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
323324
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;
325326
return retCode = 1;
326327
}
327328

0 commit comments

Comments
 (0)