summaryrefslogtreecommitdiff
path: root/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c
diff options
context:
space:
mode:
Diffstat (limited to 'v4l2-apps/lib/libv4l/libv4l2/libv4l2.c')
-rw-r--r--v4l2-apps/lib/libv4l/libv4l2/libv4l2.c67
1 files changed, 48 insertions, 19 deletions
diff --git a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c
index 8dfcf9b71..f71d176b5 100644
--- a/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c
+++ b/v4l2-apps/lib/libv4l/libv4l2/libv4l2.c
@@ -97,7 +97,7 @@ static int v4l2_request_read_buffers(int index)
req.count = devices[index].nreadbuffers;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
- if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, &req))){
+ if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, &req)) < 0){
int saved_err = errno;
V4L2_LOG_ERR("requesting buffers: %s\n", strerror(errno));
errno = saved_err;
@@ -121,7 +121,7 @@ static int v4l2_unrequest_read_buffers(int index)
req.count = 0;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
- if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, &req))) {
+ if ((result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, &req)) < 0) {
int saved_err = errno;
V4L2_LOG_ERR("unrequesting buffers: %s\n", strerror(errno));
errno = saved_err;
@@ -386,8 +386,10 @@ int v4l2_fd_open(int fd, int v4l2_flags)
return -1;
}
- /* we only add functionality for video capture devices */
- if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
+ /* we only add functionality for video capture devices, and we do not
+ handle devices which don't do mmap */
+ if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) ||
+ !(cap.capabilities & V4L2_CAP_STREAMING))
return fd;
/* Get current cam format */
@@ -433,10 +435,9 @@ int v4l2_fd_open(int fd, int v4l2_flags)
devices[index].convert_mmap_buf = MAP_FAILED;
for (i = 0; i < V4L2_MAX_NO_FRAMES; i++) {
devices[index].frame_pointers[i] = MAP_FAILED;
+ devices[index].frame_map_count[i] = 0;
}
devices[index].frame_queued = 0;
- devices[index].frame_mapped = 0;
- devices[index].frame_map_count = 0;
if (index >= devices_used)
devices_used = index + 1;
@@ -468,7 +469,7 @@ static int v4l2_get_index(int fd)
int v4l2_close(int fd)
{
- int index, result;
+ int index, result, i;
if ((index = v4l2_get_index(fd)) == -1)
return syscall(SYS_close, fd);
@@ -487,11 +488,12 @@ int v4l2_close(int fd)
v4l2_unmap_buffers(index);
v4lconvert_destroy(devices[index].convert);
if (devices[index].convert_mmap_buf != MAP_FAILED) {
- if (devices[index].frame_mapped || devices[index].frame_map_count)
- V4L2_LOG(
- "v4l2 mmap buffers still mapped on close(), mask: %08x, count: %d\n",
- (unsigned int)devices[index].frame_mapped,
- devices[index].frame_map_count);
+ for (i = 0; i < V4L2_MAX_NO_FRAMES; i++)
+ if (devices[index].frame_map_count[i])
+ break;
+
+ if (i != V4L2_MAX_NO_FRAMES)
+ V4L2_LOG("v4l2 mmap buffers still mapped on close()\n");
else
syscall(SYS_munmap, devices[index].convert_mmap_buf,
devices[index].no_frames * V4L2_FRAME_BUF_SIZE);
@@ -548,6 +550,9 @@ int v4l2_ioctl (int fd, unsigned long int request, ...)
/* Is this a capture request and do we need to take the stream lock? */
switch (request) {
+ case VIDIOC_QUERYCAP:
+ is_capture_request = 1;
+ break;
case VIDIOC_ENUM_FMT:
if (((struct v4l2_fmtdesc *)arg)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
is_capture_request = 1;
@@ -603,6 +608,17 @@ int v4l2_ioctl (int fd, unsigned long int request, ...)
switch (request) {
+ case VIDIOC_QUERYCAP:
+ {
+ struct v4l2_capability *cap = arg;
+
+ result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_QUERYCAP, cap);
+ if (result == 0)
+ /* We always support read() as we fake it using mmap mode */
+ cap->capabilities |= V4L2_CAP_READWRITE;
+ }
+ break;
+
case VIDIOC_ENUM_FMT:
result = v4lconvert_enum_fmt(devices[index].convert, arg);
break;
@@ -688,6 +704,7 @@ int v4l2_ioctl (int fd, unsigned long int request, ...)
case VIDIOC_REQBUFS:
{
+ int i;
struct v4l2_requestbuffers *req = arg;
/* Don't allow mixing read / mmap io, either we control the buffers
@@ -700,7 +717,11 @@ int v4l2_ioctl (int fd, unsigned long int request, ...)
}
/* Are any of our fake (convert_mmap_buf) buffers still mapped ? */
- if (devices[index].frame_mapped || devices[index].frame_map_count) {
+ for (i = 0; i < V4L2_MAX_NO_FRAMES; i++)
+ if (devices[index].frame_map_count[i])
+ break;
+
+ if (i != V4L2_MAX_NO_FRAMES) {
errno = EBUSY;
result = -1;
break;
@@ -723,8 +744,9 @@ int v4l2_ioctl (int fd, unsigned long int request, ...)
v4l2_unmap_buffers(index);
result = syscall(SYS_ioctl, devices[index].fd, VIDIOC_REQBUFS, req);
- if (result)
+ if (result < 0)
break;
+ result = 0; /* some drivers return the number of buffers on success */
/* If we got more frames then we can handle lie to the app */
if (req->count > V4L2_MAX_NO_FRAMES)
@@ -751,6 +773,10 @@ int v4l2_ioctl (int fd, unsigned long int request, ...)
buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index;
buf->length = V4L2_FRAME_BUF_SIZE;
+ if (devices[index].frame_map_count[buf->index])
+ buf->flags |= V4L2_BUF_FLAG_MAPPED;
+ else
+ buf->flags &= ~V4L2_BUF_FLAG_MAPPED;
}
break;
@@ -810,7 +836,12 @@ int v4l2_ioctl (int fd, unsigned long int request, ...)
}
buf->bytesused = result;
+ buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index;
buf->length = V4L2_FRAME_BUF_SIZE;
+ if (devices[index].frame_map_count[buf->index])
+ buf->flags |= V4L2_BUF_FLAG_MAPPED;
+ else
+ buf->flags &= ~V4L2_BUF_FLAG_MAPPED;
result = 0;
}
@@ -943,8 +974,7 @@ void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd,
}
}
- devices[index].frame_mapped |= 1 << buffer_index;
- devices[index].frame_map_count++;
+ devices[index].frame_map_count[buffer_index]++;
result = devices[index].convert_mmap_buf +
buffer_index * V4L2_FRAME_BUF_SIZE;
@@ -985,9 +1015,8 @@ int v4l2_munmap(void *_start, size_t length)
start >= devices[index].convert_mmap_buf &&
(start - devices[index].convert_mmap_buf) % length == 0 &&
buffer_index < devices[index].no_frames) {
- devices[index].frame_mapped &= ~(1 << buffer_index);
- if (devices[index].frame_map_count > 0)
- devices[index].frame_map_count--;
+ if (devices[index].frame_map_count[buffer_index] > 0)
+ devices[index].frame_map_count[buffer_index]--;
unmapped = 1;
}