I'm currently trying to read video from a MJPEG UVC webcam using C++. Using this tutorial, I'm able to retrieve a JPEG image and to save it on disk. Now, I try to use the JPEG image data without writing it on disk ; I want to use a C++ vector to store it, but I can't find how to achieve that .
As far as I understand, the tutorial maps a memory segment that v4l2 uses to store webcam data in memory :
struct v4l2_buffer bufferinfo;
memset(&bufferinfo, 0, sizeof(bufferinfo));
bufferinfo.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
bufferinfo.memory = V4L2_MEMORY_MMAP;
bufferinfo.index = 0;
if(ioctl(fd, VIDIOC_QUERYBUF, &bufferinfo) < 0){
perror("VIDIOC_QUERYBUF");
exit(1);
}
void* buffer_start = mmap(
NULL,
bufferinfo.length,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
bufferinfo.m.offset
);
This memory segment is populated by v4l2 when we send some command :
if(ioctl(fd, VIDIOC_QBUF, &bufferinfo) < 0){
perror("VIDIOC_QBUF");
exit(1);
}
// The buffer's waiting in the outgoing queue.
if(ioctl(fd, VIDIOC_DQBUF, &bufferinfo) < 0){
perror("VIDIOC_QBUF");
exit(1);
}
To finish, the memory is written to the disk using write function :
int jpgfile;
if((jpgfile = open("/tmp/myimage.jpeg", O_WRONLY | O_CREAT, 0660)) < 0){
perror("open");
exit(1);
}
write(jpgfile, buffer_start, bufferinfo.length);
close(jpgfile);
But how can I copy this memory segment into a vector ?
Thank you in advance.