v4l2-ctl: break down the streaming_set()
PATCH 1/2 v4l2-ctl: break down the streaming_set()
This patch breaks down the streaming_set() into smaller ones, which can be resued for supporting m2m devices.
Further cleanup or consolidation can be applied with separate patches, since this one tries not to modify logics.
--- utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 888 ++++++++++++++++++---------------- 1 file changed, 480 insertions(+), 408 deletions(-)
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +480 -408
@@ -27,7 +27,8 @@ static unsigned stream_skip; static unsigned stream_pat; static bool stream_loop; static unsigned reqbufs_count = 3; -static char *file; +static char *file_cap; +static char *file_out; #define NUM_PATTERNS (4)
@@ -198,12 +199,12 @@ void streaming_cmd(int ch, char *optarg) stream_pat %= NUM_PATTERNS; break; case OptStreamTo: - file = optarg; - if (!strcmp(file, "-")) + file_cap = optarg; + if (!strcmp(file_cap, "-")) options[OptSilent] = true; break; case OptStreamFrom: - file = optarg; + file_out = optarg; break; case OptStreamMmap: case OptStreamUser:
@@ -526,475 +527,546 @@ static bool fill_buffer_from_file(void *buffers[], unsigned buffer_lengths[], return true; } -void streaming_set(int fd) +static void do_setup_cap_buffers(int fd, struct v4l2_requestbuffers *reqbufs, + bool is_mplane, unsigned num_planes, bool is_mmap, + void *buffers[], unsigned buffer_lengths[]) { - if (options[OptStreamMmap] || options[OptStreamUser]) { - struct v4l2_requestbuffers reqbufs; - struct v4l2_event_subscription sub; - int fd_flags = fcntl(fd, F_GETFL); - bool is_mplane = capabilities & - (V4L2_CAP_VIDEO_CAPTURE_MPLANE | - V4L2_CAP_VIDEO_M2M_MPLANE); - bool is_mmap = options[OptStreamMmap]; - bool use_poll = options[OptStreamPoll]; - __u32 type = is_mplane ? - V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE; - FILE *fout = NULL; - unsigned num_planes = 1; - - memset(&reqbufs, 0, sizeof(reqbufs)); - reqbufs.count = reqbufs_count; - reqbufs.type = type; - reqbufs.memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; - memset(&sub, 0, sizeof(sub)); - sub.type = V4L2_EVENT_EOS; - ioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub); - - if (file) { - if (!strcmp(file, "-")) - fout = stdout; - else - fout = fopen(file, "w+"); - } + for (unsigned i = 0; i < reqbufs->count; i++) { + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + struct v4l2_buffer buf; - if (doioctl(fd, VIDIOC_REQBUFS, &reqbufs)) + memset(&buf, 0, sizeof(buf)); + memset(planes, 0, sizeof(planes)); + buf.type = reqbufs->type; + buf.memory = reqbufs->memory; + buf.index = i; + if (is_mplane) { + buf.m.planes = planes; + buf.length = VIDEO_MAX_PLANES; + } + if (doioctl(fd, VIDIOC_QUERYBUF, &buf)) return; - void *buffers[reqbufs.count * VIDEO_MAX_PLANES]; - unsigned buffer_lengths[reqbufs.count * VIDEO_MAX_PLANES]; - - for (unsigned i = 0; i < reqbufs.count; i++) { - struct v4l2_plane planes[VIDEO_MAX_PLANES]; - struct v4l2_buffer buf; - - memset(&buf, 0, sizeof(buf)); - memset(planes, 0, sizeof(planes)); - buf.type = reqbufs.type; - buf.memory = reqbufs.memory; - buf.index = i; - if (is_mplane) { - buf.m.planes = planes; - buf.length = VIDEO_MAX_PLANES; - } - if (doioctl(fd, VIDIOC_QUERYBUF, &buf)) - return; + if (is_mplane) { + num_planes = buf.length; + for (unsigned j = 0; j < num_planes; j++) { + unsigned p = i * num_planes + j; - if (is_mplane) { - num_planes = buf.length; - for (unsigned j = 0; j < num_planes; j++) { - unsigned p = i * num_planes + j; - - buffer_lengths[p] = planes[j].length; - if (is_mmap) { - buffers[p] = mmap(NULL, planes[j].length, - PROT_READ | PROT_WRITE, MAP_SHARED, - fd, planes[j].m.mem_offset); - - if (buffers[p] == MAP_FAILED) { - fprintf(stderr, "mmap failed\n"); - return; - } - } else { - buffers[p] = calloc(1, planes[j].length); - planes[j].m.userptr = (unsigned long)buffers[p]; - } - } - } else { - buffer_lengths[i] = buf.length; + buffer_lengths[p] = planes[j].length; if (is_mmap) { - buffers[i] = mmap(NULL, buf.length, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); + buffers[p] = mmap(NULL, planes[j].length, + PROT_READ | PROT_WRITE, MAP_SHARED, + fd, planes[j].m.mem_offset); - if (buffers[i] == MAP_FAILED) { + if (buffers[p] == MAP_FAILED) { fprintf(stderr, "mmap failed\n"); return; } - } else { - buffers[i] = calloc(1, buf.length); - buf.m.userptr = (unsigned long)buffers[i]; + } + else { + buffers[p] = calloc(1, planes[j].length); + planes[j].m.userptr = (unsigned long)buffers[p]; } } - if (doioctl(fd, VIDIOC_QBUF, &buf)) - return; } - - type = reqbufs.type; - if (doioctl(fd, VIDIOC_STREAMON, &type)) - return; - - if (use_poll) - fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK); - - unsigned count = 0, last = 0; - struct timeval tv_last; - bool eos = false; - - while (!eos) { - struct v4l2_plane planes[VIDEO_MAX_PLANES]; - struct v4l2_buffer buf; - fd_set read_fds; - fd_set exception_fds; - char ch = '.'; - int ret; - - if (use_poll) { - struct timeval tv; - int r; - - FD_ZERO(&read_fds); - FD_SET(fd, &read_fds); - FD_ZERO(&exception_fds); - FD_SET(fd, &exception_fds); - - /* Timeout. */ - tv.tv_sec = 2; - tv.tv_usec = 0; - - r = select(fd + 1, &read_fds, NULL, &exception_fds, &tv); - - if (r == -1) { - if (EINTR == errno) - continue; - fprintf(stderr, "select error: %s\n", - strerror(errno)); - return; - } - - if (r == 0) { - fprintf(stderr, "select timeout\n"); + else { + buffer_lengths[i] = buf.length; + if (is_mmap) { + buffers[i] = mmap(NULL, buf.length, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); + + if (buffers[i] == MAP_FAILED) { + fprintf(stderr, "mmap failed\n"); return; } } + else { + buffers[i] = calloc(1, buf.length); + buf.m.userptr = (unsigned long)buffers[i]; + } + } + if (doioctl(fd, VIDIOC_QBUF, &buf)) + return; + } +} - if (FD_ISSET(fd, &exception_fds)) { - struct v4l2_event ev; +static void do_setup_out_buffers(int fd, struct v4l2_requestbuffers *reqbufs, + bool is_mplane, unsigned num_planes, bool is_mmap, + void *buffers[], unsigned buffer_lengths[], FILE *fin) +{ + struct v4l2_format fmt; + memset(&fmt, 0, sizeof(fmt)); + fmt.type = reqbufs->type; + doioctl(fd, VIDIOC_G_FMT, &fmt); + + if (!precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat % NUM_PATTERNS)) { + fprintf(stderr, "unsupported pixelformat\n"); + return; + } - while (!ioctl(fd, VIDIOC_DQEVENT, &ev)) { - if (ev.type != V4L2_EVENT_EOS) - continue; - eos = true; - break; - } - } - if (!FD_ISSET(fd, &read_fds)) - continue; + for (unsigned i = 0; i < reqbufs->count; i++) { + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + struct v4l2_buffer buf; - memset(&buf, 0, sizeof(buf)); - memset(planes, 0, sizeof(planes)); - buf.type = reqbufs.type; - buf.memory = reqbufs.memory; - if (is_mplane) { - buf.m.planes = planes; - buf.length = VIDEO_MAX_PLANES; - } + memset(&buf, 0, sizeof(buf)); + memset(planes, 0, sizeof(planes)); + buf.type = reqbufs->type; + buf.memory = reqbufs->memory; + buf.index = i; + if (is_mplane) { + buf.m.planes = planes; + buf.length = VIDEO_MAX_PLANES; + } + if (doioctl(fd, VIDIOC_QUERYBUF, &buf)) + return; - ret = test_ioctl(fd, VIDIOC_DQBUF, &buf); - if (ret < 0 && errno == EAGAIN) - continue; - if (ret < 0) { - fprintf(stderr, "%s: failed: %s\n", "VIDIOC_DQBUF", strerror(errno)); - return; - } - if (fout && !stream_skip) { - for (unsigned j = 0; j < num_planes; j++) { - unsigned p = buf.index * num_planes + j; - unsigned used = is_mplane ? planes[j].bytesused : buf.bytesused; - unsigned offset = is_mplane ? planes[j].data_offset : 0; - unsigned sz; - - if (offset > used) { - // Should never happen - fprintf(stderr, "offset %d > used %d!\n", - offset, used); - offset = 0; - } - used -= offset; - sz = fwrite((char *)buffers[p] + offset, 1, used, fout); + if (is_mplane) { + num_planes = buf.length; + for (unsigned j = 0; j < num_planes; j++) { + unsigned p = i * num_planes + j; - if (sz != used) - fprintf(stderr, "%u != %u\n", sz, used); - } - } - if (buf.flags & V4L2_BUF_FLAG_KEYFRAME) - ch = 'K'; - else if (buf.flags & V4L2_BUF_FLAG_PFRAME) - ch = 'P'; - else if (buf.flags & V4L2_BUF_FLAG_BFRAME) - ch = 'B'; - if (verbose) - print_buffer(stderr, buf); - if (test_ioctl(fd, VIDIOC_QBUF, &buf)) - return; + buffer_lengths[p] = planes[j].length; + buf.m.planes[j].bytesused = planes[j].length; + if (is_mmap) { + buffers[p] = mmap(NULL, planes[j].length, + PROT_READ | PROT_WRITE, MAP_SHARED, + fd, planes[j].m.mem_offset); - if (!verbose) { - fprintf(stderr, "%c", ch); - fflush(stderr); + if (buffers[p] == MAP_FAILED) { + fprintf(stderr, "mmap failed\n"); + return; + } + } + else { + buffers[p] = calloc(1, planes[j].length); + planes[j].m.userptr = (unsigned long)buffers[p]; + } } - - if (count == 0) { - gettimeofday(&tv_last, NULL); - } else { - struct timeval tv_cur, res; - - gettimeofday(&tv_cur, NULL); - timersub(&tv_cur, &tv_last, &res); - if (res.tv_sec) { - unsigned fps = (100 * (count - last)) / - (res.tv_sec * 100 + res.tv_usec / 10000); - last = count; - tv_last = tv_cur; - fprintf(stderr, " %d fps\n", fps); + // TODO fill_buffer_mp(buffers[i], &fmt.fmt.pix_mp); + if (fin) + fill_buffer_from_file(buffers, buffer_lengths, + buf.index, num_planes, fin); + } + else { + buffer_lengths[i] = buf.length; + buf.bytesused = buf.length; + if (is_mmap) { + buffers[i] = mmap(NULL, buf.length, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); + + if (buffers[i] == MAP_FAILED) { + fprintf(stderr, "mmap failed\n"); + return; } } - count++; - if (stream_skip) { - stream_skip--; - continue; + else { + buffers[i] = calloc(1, buf.length); + buf.m.userptr = (unsigned long)buffers[i]; } - if (stream_count == 0) - continue; - if (--stream_count == 0) - break; + if (!fin || !fill_buffer_from_file(buffers, buffer_lengths, + buf.index, num_planes, fin)) + fill_buffer(buffers[i], &fmt.fmt.pix); } - doioctl(fd, VIDIOC_STREAMOFF, &type); - fcntl(fd, F_SETFL, fd_flags); - fprintf(stderr, "\n"); + if (doioctl(fd, VIDIOC_QBUF, &buf)) + return; + } +} - for (unsigned i = 0; i < reqbufs.count; i++) { - for (unsigned j = 0; j < num_planes; j++) { - unsigned p = i * num_planes + j; +static void do_release_buffers(struct v4l2_requestbuffers *reqbufs, + unsigned num_planes, bool is_mmap, + void *buffers[], unsigned buffer_lengths[]) +{ + for (unsigned i = 0; i < reqbufs->count; i++) { + for (unsigned j = 0; j < num_planes; j++) { + unsigned p = i * num_planes + j; - if (is_mmap) - munmap(buffers[p], buffer_lengths[p]); - else - free(buffers[p]); - } + if (is_mmap) + munmap(buffers[p], buffer_lengths[p]); + else + free(buffers[p]); } - if (fout && fout != stdout) - fclose(fout); } - - if (options[OptStreamOutMmap] || options[OptStreamOutUser]) { - struct v4l2_format fmt; - struct v4l2_requestbuffers reqbufs; - int fd_flags = fcntl(fd, F_GETFL); - bool is_mplane = capabilities & - (V4L2_CAP_VIDEO_OUTPUT_MPLANE | - V4L2_CAP_VIDEO_M2M_MPLANE); - bool is_mmap = options[OptStreamOutMmap]; - bool use_poll = options[OptStreamPoll]; - __u32 type = is_mplane ? - V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT; - FILE *fin = NULL; - unsigned num_planes = 1; - - memset(&fmt, 0, sizeof(fmt)); - fmt.type = type; - doioctl(fd, VIDIOC_G_FMT, &fmt); - - if (!precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat % NUM_PATTERNS)) { - fprintf(stderr, "unsupported pixelformat\n"); - return; - } +} - memset(&reqbufs, 0, sizeof(reqbufs)); - reqbufs.count = reqbufs_count; - reqbufs.type = type; - reqbufs.memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; +static int do_handle_cap(int fd, struct v4l2_requestbuffers *reqbufs, + bool is_mplane, unsigned num_planes, + void *buffers[], unsigned buffer_lengths[], FILE *fout, + unsigned *count, unsigned *last, struct timeval *tv_last) +{ + char ch = '+'; + int ret; + + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + struct v4l2_buffer buf; + memset(&buf, 0, sizeof(buf)); + memset(planes, 0, sizeof(planes)); + + buf.type = reqbufs->type; + buf.memory = reqbufs->memory; + if (is_mplane) { + buf.m.planes = planes; + buf.length = VIDEO_MAX_PLANES; + } - if (file) { - if (!strcmp(file, "-")) - fin = stdin; - else - fin = fopen(file, "r"); + ret = test_ioctl(fd, VIDIOC_DQBUF, &buf); + if (ret < 0 && errno == EAGAIN) + return 0; + if (ret < 0) { + fprintf(stderr, "%s: failed: %s\n", "VIDIOC_DQBUF", strerror(errno)); + return -1; + } + if (fout && !stream_skip) { + for (unsigned j = 0; j < num_planes; j++) { + unsigned p = buf.index * num_planes + j; + unsigned used = is_mplane ? planes[j].bytesused : buf.bytesused; + unsigned offset = is_mplane ? planes[j].data_offset : 0; + unsigned sz; + + if (offset > used) { + // Should never happen + fprintf(stderr, "offset %d > used %d!\n", + offset, used); + offset = 0; + } + used -= offset; + sz = fwrite((char *)buffers[p] + offset, 1, used, fout); + + if (sz != used) + fprintf(stderr, "%u != %u\n", sz, used); } + } + if (buf.flags & V4L2_BUF_FLAG_KEYFRAME) + ch = 'K'; + else if (buf.flags & V4L2_BUF_FLAG_PFRAME) + ch = 'P'; + else if (buf.flags & V4L2_BUF_FLAG_BFRAME) + ch = 'B'; + if (verbose) + print_buffer(stderr, buf); + if (test_ioctl(fd, VIDIOC_QBUF, &buf)) + return -1; + + if (!verbose) { + fprintf(stderr, "%c", ch); + fflush(stderr); + } - if (doioctl(fd, VIDIOC_REQBUFS, &reqbufs)) - return; + if (*count == 0) { + gettimeofday(tv_last, NULL); + } + else { + struct timeval tv_cur, res; + + gettimeofday(&tv_cur, NULL); + timersub(&tv_cur, tv_last, &res); + if (res.tv_sec) { + unsigned fps = (100 * (*count - *last)) / + (res.tv_sec * 100 + res.tv_usec / 10000); + *last = *count; + *tv_last = tv_cur; + fprintf(stderr, " %d fps\n", fps); + } + } + *count += 1; + if (stream_skip) { + stream_skip--; + return 0; + } + if (stream_count == 0) + return 0; + if (--stream_count == 0) + return -1; - void *buffers[reqbufs.count * VIDEO_MAX_PLANES]; - unsigned buffer_lengths[reqbufs.count * VIDEO_MAX_PLANES]; - - for (unsigned i = 0; i < reqbufs.count; i++) { - struct v4l2_plane planes[VIDEO_MAX_PLANES]; - struct v4l2_buffer buf; + return 0; +} - memset(&buf, 0, sizeof(buf)); - memset(planes, 0, sizeof(planes)); - buf.type = reqbufs.type; - buf.memory = reqbufs.memory; - buf.index = i; - if (is_mplane) { - buf.m.planes = planes; - buf.length = VIDEO_MAX_PLANES; - } - if (doioctl(fd, VIDIOC_QUERYBUF, &buf)) - return; +static int do_handle_out(int fd, struct v4l2_requestbuffers *reqbufs, + bool is_mplane, unsigned num_planes, + void *buffers[], unsigned buffer_lengths[], FILE *fin, + unsigned *count, unsigned *last, struct timeval *tv_last) +{ + int ret; + + struct v4l2_plane planes[VIDEO_MAX_PLANES]; + struct v4l2_buffer buf; + + memset(&buf, 0, sizeof(buf)); + memset(planes, 0, sizeof(planes)); + buf.type = reqbufs->type; + buf.memory = reqbufs->memory; + if (is_mplane) { + buf.m.planes = planes; + buf.length = VIDEO_MAX_PLANES; + } - if (is_mplane) { - num_planes = buf.length; - for (unsigned j = 0; j < num_planes; j++) { - unsigned p = i * num_planes + j; - - buffer_lengths[p] = planes[j].length; - buf.m.planes[j].bytesused = planes[j].length; - if (is_mmap) { - buffers[p] = mmap(NULL, planes[j].length, - PROT_READ | PROT_WRITE, MAP_SHARED, - fd, planes[j].m.mem_offset); - - if (buffers[p] == MAP_FAILED) { - fprintf(stderr, "mmap failed\n"); - return; - } - } else { - buffers[p] = calloc(1, planes[j].length); - planes[j].m.userptr = (unsigned long)buffers[p]; - } - } - // TODO fill_buffer_mp(buffers[i], &fmt.fmt.pix_mp); - if (fin) - fill_buffer_from_file(buffers, buffer_lengths, - buf.index, num_planes, fin); - } else { - buffer_lengths[i] = buf.length; - buf.bytesused = buf.length; - if (is_mmap) { - buffers[i] = mmap(NULL, buf.length, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); + ret = test_ioctl(fd, VIDIOC_DQBUF, &buf); + if (ret < 0 && errno == EAGAIN) + return 0; + if (ret < 0) { + fprintf(stderr, "%s: failed: %s\n", "VIDIOC_DQBUF", strerror(errno)); + return -1; + } + if (fin && !fill_buffer_from_file(buffers, buffer_lengths, + buf.index, num_planes, fin)) + return -1; + if (is_mplane) { + for (unsigned j = 0; j < buf.length; j++) buf.m.planes[j].bytesused = buf.m.planes[j].length; + } + else { + buf.bytesused = buf.length; + } + if (test_ioctl(fd, VIDIOC_QBUF, &buf)) + return -1; - if (buffers[i] == MAP_FAILED) { - fprintf(stderr, "mmap failed\n"); - return; - } - } else { - buffers[i] = calloc(1, buf.length); - buf.m.userptr = (unsigned long)buffers[i]; - } - if (!fin || !fill_buffer_from_file(buffers, buffer_lengths, - buf.index, num_planes, fin)) - fill_buffer(buffers[i], &fmt.fmt.pix); - } - if (doioctl(fd, VIDIOC_QBUF, &buf)) - return; + fprintf(stderr, "-"); + fflush(stderr); + + if (*count == 0) { + gettimeofday(tv_last, NULL); + } + else { + struct timeval tv_cur, res; + + gettimeofday(&tv_cur, NULL); + timersub(&tv_cur, tv_last, &res); + if (res.tv_sec) { + unsigned fps = (100 * (*count - *last)) / + (res.tv_sec * 100 + res.tv_usec / 10000); + *last = *count; + *tv_last = tv_cur; + fprintf(stderr, " %d fps\n", fps); } + } + *count += 1; + if (stream_count == 0) + return 0; + if (--stream_count == 0) + return -1; - type = reqbufs.type; - if (doioctl(fd, VIDIOC_STREAMON, &type)) - return; + return 0; +} + +void streaming_set_cap(int fd) +{ + struct v4l2_requestbuffers reqbufs; + struct v4l2_event_subscription sub; + int fd_flags = fcntl(fd, F_GETFL); + bool is_mplane = capabilities & + (V4L2_CAP_VIDEO_CAPTURE_MPLANE | + V4L2_CAP_VIDEO_M2M_MPLANE); + bool is_mmap = options[OptStreamMmap]; + bool use_poll = options[OptStreamPoll]; + __u32 type = is_mplane ? + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE; + FILE *fout = NULL; + unsigned num_planes = 1; + + memset(&reqbufs, 0, sizeof(reqbufs)); + reqbufs.count = reqbufs_count; + reqbufs.type = type; + reqbufs.memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + memset(&sub, 0, sizeof(sub)); + sub.type = V4L2_EVENT_EOS; + ioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub); + + if (file_cap) { + if (!strcmp(file_cap, "-")) + fout = stdout; + else + fout = fopen(file_cap, "w+"); + } - if (use_poll) - fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK); + if (doioctl(fd, VIDIOC_REQBUFS, &reqbufs)) + return; - unsigned count = 0, last = 0; - struct timeval tv_last; + void *buffers[reqbufs.count * VIDEO_MAX_PLANES]; + unsigned buffer_lengths[reqbufs.count * VIDEO_MAX_PLANES]; - for (;;) { - struct v4l2_plane planes[VIDEO_MAX_PLANES]; - struct v4l2_buffer buf; - int ret; + do_setup_cap_buffers(fd, &reqbufs, is_mplane, num_planes, + is_mmap, buffers, buffer_lengths); - if (use_poll) { - fd_set fds; - struct timeval tv; - int r; + type = reqbufs.type; + if (doioctl(fd, VIDIOC_STREAMON, &type)) + return; - FD_ZERO(&fds); - FD_SET(fd, &fds); + if (use_poll) + fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK); - /* Timeout. */ - tv.tv_sec = 2; - tv.tv_usec = 0; + unsigned count = 0, last = 0; + struct timeval tv_last; + bool eos = false; - r = select(fd + 1, NULL, &fds, NULL, &tv); + while (!eos) { + fd_set read_fds; + fd_set exception_fds; + int r; - if (r == -1) { - if (EINTR == errno) - continue; - fprintf(stderr, "select error: %s\n", - strerror(errno)); - return; - } + if (use_poll) { + struct timeval tv; - if (r == 0) { - fprintf(stderr, "select timeout\n"); - return; - } - } + FD_ZERO(&read_fds); + FD_SET(fd, &read_fds); + FD_ZERO(&exception_fds); + FD_SET(fd, &exception_fds); - memset(&buf, 0, sizeof(buf)); - memset(planes, 0, sizeof(planes)); - buf.type = reqbufs.type; - buf.memory = reqbufs.memory; - if (is_mplane) { - buf.m.planes = planes; - buf.length = VIDEO_MAX_PLANES; + /* Timeout. */ + tv.tv_sec = 2; + tv.tv_usec = 0; + + r = select(fd + 1, &read_fds, NULL, &exception_fds, &tv); + + if (r == -1) { + if (EINTR == errno) + continue; + fprintf(stderr, "select error: %s\n", + strerror(errno)); + return; } - ret = test_ioctl(fd, VIDIOC_DQBUF, &buf); - if (ret < 0 && errno == EAGAIN) - continue; - if (ret < 0) { - fprintf(stderr, "%s: failed: %s\n", "VIDIOC_DQBUF", strerror(errno)); + if (r == 0) { + fprintf(stderr, "select timeout\n"); return; } - if (fin && !fill_buffer_from_file(buffers, buffer_lengths, - buf.index, num_planes, fin)) + } + + if (FD_ISSET(fd, &exception_fds)) { + struct v4l2_event ev; + + while (!ioctl(fd, VIDIOC_DQEVENT, &ev)) { + if (ev.type != V4L2_EVENT_EOS) + continue; + eos = true; break; - if (is_mplane) { - for (unsigned j = 0; j < buf.length; j++) - buf.m.planes[j].bytesused = buf.m.planes[j].length; - } else { - buf.bytesused = buf.length; } - if (test_ioctl(fd, VIDIOC_QBUF, &buf)) - return; + } - fprintf(stderr, "."); - fflush(stderr); - - if (count == 0) { - gettimeofday(&tv_last, NULL); - } else { - struct timeval tv_cur, res; - - gettimeofday(&tv_cur, NULL); - timersub(&tv_cur, &tv_last, &res); - if (res.tv_sec) { - unsigned fps = (100 * (count - last)) / - (res.tv_sec * 100 + res.tv_usec / 10000); - last = count; - tv_last = tv_cur; - fprintf(stderr, " %d fps\n", fps); - } - } - count++; - if (stream_count == 0) - continue; - if (--stream_count == 0) + if (FD_ISSET(fd, &read_fds)) { + r = do_handle_cap(fd, &reqbufs, is_mplane, num_planes, + buffers, buffer_lengths, fout, + &count, &last, &tv_last); + if (r == -1) break; } - if (options[OptDecoderCmd]) { - doioctl(fd, VIDIOC_DECODER_CMD, &dec_cmd); - options[OptDecoderCmd] = false; - } - doioctl(fd, VIDIOC_STREAMOFF, &type); - fcntl(fd, F_SETFL, fd_flags); - fprintf(stderr, "\n"); - for (unsigned i = 0; i < reqbufs.count; i++) { - for (unsigned j = 0; j < num_planes; j++) { - unsigned p = i * num_planes + j; + } + doioctl(fd, VIDIOC_STREAMOFF, &type); + fcntl(fd, F_SETFL, fd_flags); + fprintf(stderr, "\n"); + + do_release_buffers(&reqbufs, num_planes, is_mmap, buffers, + buffer_lengths); + + if (fout && fout != stdout) + fclose(fout); +} + +void streaming_set_out(int fd) +{ + struct v4l2_requestbuffers reqbufs; + int fd_flags = fcntl(fd, F_GETFL); + bool is_mplane = capabilities & + (V4L2_CAP_VIDEO_OUTPUT_MPLANE | + V4L2_CAP_VIDEO_M2M_MPLANE); + bool is_mmap = options[OptStreamOutMmap]; + bool use_poll = options[OptStreamPoll]; + __u32 type = is_mplane ? + V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT; + FILE *fin = NULL; + unsigned num_planes = 1; + + + memset(&reqbufs, 0, sizeof(reqbufs)); + reqbufs.count = reqbufs_count; + reqbufs.type = type; + reqbufs.memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + + if (file_out) { + if (!strcmp(file_out, "-")) + fin = stdin; + else + fin = fopen(file_out, "r"); + } + + if (doioctl(fd, VIDIOC_REQBUFS, &reqbufs)) + return; + + void *buffers[reqbufs.count * VIDEO_MAX_PLANES]; + unsigned buffer_lengths[reqbufs.count * VIDEO_MAX_PLANES]; + + do_setup_out_buffers(fd, &reqbufs, is_mplane, num_planes, + is_mmap, buffers, buffer_lengths, fin); + + type = reqbufs.type; + if (doioctl(fd, VIDIOC_STREAMON, &type)) + return; + + if (use_poll) + fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK); - if (is_mmap) - munmap(buffers[p], buffer_lengths[p]); - else - free(buffers[p]); + unsigned count = 0, last = 0; + struct timeval tv_last; + + for (;;) { + int r; + + if (use_poll) { + fd_set fds; + struct timeval tv; + + FD_ZERO(&fds); + FD_SET(fd, &fds); + + /* Timeout. */ + tv.tv_sec = 2; + tv.tv_usec = 0; + + r = select(fd + 1, NULL, &fds, NULL, &tv); + + if (r == -1) { + if (EINTR == errno) + continue; + fprintf(stderr, "select error: %s\n", + strerror(errno)); + return; + } + + if (r == 0) { + fprintf(stderr, "select timeout\n"); + return; } } - if (fin && fin != stdin) - fclose(fin); + r = do_handle_out(fd, &reqbufs, is_mplane, num_planes, + buffers, buffer_lengths, fin, + &count, &last, &tv_last); + if (r == -1) + break; + + } + + if (options[OptDecoderCmd]) { + doioctl(fd, VIDIOC_DECODER_CMD, &dec_cmd); + options[OptDecoderCmd] = false; } + doioctl(fd, VIDIOC_STREAMOFF, &type); + fcntl(fd, F_SETFL, fd_flags); + fprintf(stderr, "\n"); + + do_release_buffers(&reqbufs, num_planes, is_mmap, buffers, + buffer_lengths); + + if (fin && fin != stdin) + fclose(fin); +} + +void streaming_set(int fd) +{ + bool do_cap = options[OptStreamMmap] || options[OptStreamUser]; + bool do_out = options[OptStreamOutMmap] || options[OptStreamOutUser]; + + if (do_cap) + streaming_set_cap(fd); + else if (do_out) + streaming_set_out(fd); } void streaming_list(int fd)
Signature · 1 line
1.8.1.5
PATCH 2/2 v4l2-ctl: initial attempt to support M2M device streaming
--- utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 189 +++++++++++++++++++++++++++++++++- 1 file changed, 188 insertions(+), 1 deletion(-)
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +188 -1
@@ -1058,12 +1058,199 @@ void streaming_set_out(int fd) fclose(fin); } +enum stream_type { + CAP, + OUT, +}; + +void streaming_set_m2m(int fd) +{ + int fd_flags = fcntl(fd, F_GETFL); + bool use_poll = options[OptStreamPoll]; + + bool is_mplane = capabilities & + (V4L2_CAP_VIDEO_CAPTURE_MPLANE | + V4L2_CAP_VIDEO_OUTPUT_MPLANE | + V4L2_CAP_VIDEO_M2M_MPLANE); + unsigned num_planes = 1; + bool is_mmap = options[OptStreamMmap]; + + __u32 type[2]; + FILE *file[2] = {NULL, NULL}; + struct v4l2_requestbuffers reqbufs[2]; + + type[CAP] = is_mplane ? + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE; + + type[OUT] = is_mplane ? + V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT; + + memset(&reqbufs, 0, sizeof(reqbufs)); + + reqbufs[CAP].count = reqbufs_count; + reqbufs[CAP].type = type[CAP]; + reqbufs[CAP].memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + + reqbufs[OUT].count = reqbufs_count; + reqbufs[OUT].type = type[OUT]; + reqbufs[OUT].memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + + struct v4l2_event_subscription sub; + + memset(&sub, 0, sizeof(sub)); + sub.type = V4L2_EVENT_EOS; + ioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub); + + if (file_cap) { + if (!strcmp(file_cap, "-")) + file[CAP] = stdout; + else + file[CAP] = fopen(file_cap, "w+"); + } + + if (file_out) { + if (!strcmp(file_out, "-")) + file[OUT] = stdin; + else + file[OUT] = fopen(file_out, "r"); + } + + if (doioctl(fd, VIDIOC_REQBUFS, &reqbufs[CAP]) || + doioctl(fd, VIDIOC_REQBUFS, &reqbufs[OUT])) + return; + + void *buffers[2][reqbufs_count * VIDEO_MAX_PLANES]; + unsigned buffer_lengths[2][reqbufs_count * VIDEO_MAX_PLANES]; + + do_setup_cap_buffers(fd, &reqbufs[CAP], is_mplane, num_planes, + is_mmap, buffers[CAP], buffer_lengths[CAP]); + + do_setup_out_buffers(fd, &reqbufs[OUT], is_mplane, num_planes, + is_mmap, buffers[OUT], buffer_lengths[OUT], + file[OUT]); + + if (doioctl(fd, VIDIOC_STREAMON, &type[CAP]) || + doioctl(fd, VIDIOC_STREAMON, &type[OUT])) + return; + + if (use_poll) + fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK); + + unsigned count[2] = { 0, 0 }; + unsigned last[2] = { 0, 0 }; + struct timeval tv_last[2]; + bool eos[2] = { false, false}; + fd_set read_fds; + fd_set write_fds; + fd_set exception_fds; + + while (!eos[CAP] || !eos[OUT]) { + + int r; + + if (use_poll) { + struct timeval tv; + + FD_ZERO(&read_fds); + FD_SET(fd, &read_fds); + + FD_ZERO(&write_fds); + FD_SET(fd, &write_fds); + + FD_ZERO(&exception_fds); + FD_SET(fd, &exception_fds); + + /* Timeout. */ + tv.tv_sec = 2; + tv.tv_usec = 0; + + r = select(fd + 1, &read_fds, &write_fds, &exception_fds, &tv); + + if (r == -1) { + if (EINTR == errno) + continue; + fprintf(stderr, "select error: %s\n", + strerror(errno)); + return; + } + + if (r == 0) { + fprintf(stderr, "select timeout\n"); + return; + } + } + + if (FD_ISSET(fd, &exception_fds)) { + struct v4l2_event ev; + + while (!ioctl(fd, VIDIOC_DQEVENT, &ev)) { + + if (ev.type != V4L2_EVENT_EOS) + continue; + + eos[CAP] = true; + doioctl(fd, VIDIOC_STREAMOFF, &type[CAP]); + break; + } + } + + if (!eos[CAP]) { + if (FD_ISSET(fd, &read_fds)) { + r = do_handle_cap(fd, &reqbufs[CAP], is_mplane, num_planes, + buffers[CAP], buffer_lengths[CAP], file[CAP], + &count[CAP], &last[CAP], &tv_last[CAP]); + if (r < 0) { + eos[CAP] = true; + doioctl(fd, VIDIOC_STREAMOFF, &type[CAP]); + } + } + } + + if (!eos[OUT]) { + if (FD_ISSET(fd, &write_fds)) { + r = do_handle_out(fd, &reqbufs[OUT], is_mplane, num_planes, + buffers[OUT], buffer_lengths[OUT], file[OUT], + &count[OUT], &last[OUT], &tv_last[OUT]); + if (r < 0) { + eos[OUT] = true; + + if (options[OptDecoderCmd]) { + doioctl(fd, VIDIOC_DECODER_CMD, &dec_cmd); + options[OptDecoderCmd] = false; + } + + doioctl(fd, VIDIOC_STREAMOFF, &type[OUT]); + + } + } + + } + } + + fcntl(fd, F_SETFL, fd_flags); + fprintf(stderr, "\n"); + + do_release_buffers(&reqbufs[CAP], num_planes, is_mmap, + buffers[CAP], buffer_lengths[CAP]); + + do_release_buffers(&reqbufs[OUT], num_planes, is_mmap, + buffers[OUT], buffer_lengths[OUT]); + + if (file[CAP] && file[CAP] != stdout) + fclose(file[CAP]); + + if (file[OUT] && file[OUT] != stdin) + fclose(file[OUT]); +} + void streaming_set(int fd) { bool do_cap = options[OptStreamMmap] || options[OptStreamUser]; bool do_out = options[OptStreamOutMmap] || options[OptStreamOutUser]; - if (do_cap) + if (do_cap && do_out) + streaming_set_m2m(fd); + else if (do_cap) streaming_set_cap(fd); else if (do_out) streaming_set_out(fd);
Signature · 1 line
1.8.1.5
Quoted 10 lines · On Wed April 10 2013 07:35:34 Tzu-Jung Lee wrote:
This patch breaks down the
streaming_set()into smaller ones, which can be resued for supporting m2m devices.Further cleanup or consolidation can be applied with separate patches, since this one tries not to modify logics.
--- utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 888 ++++++++++++++++++---------------- 1 file changed, 480 insertions(+), 408 deletions(-)
Thanks! This looks very nice. I knew this code had to be cleaned up at some point :-)
Regards,
Hans
Quoted 41 lines · On Wed April 10 2013 07:35:34 Tzu-Jung Lee wrote:
This patch breaks down the
streaming_set()into smaller ones, which can be resued for supporting m2m devices.Further cleanup or consolidation can be applied with separate patches, since this one tries not to modify logics.
--- utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 888 ++++++++++++++++++---------------- 1 file changed, 480 insertions(+), 408 deletions(-)utils/v4l2-ctl/v4l2-ctl-streaming.cpp +5
-4@@ -27,7 +27,8 @@ static unsigned stream_skip; static unsigned stream_pat; static bool stream_loop; static unsigned reqbufs_count = 3; -static char *file; +static char *file_cap; +static char *file_out; #define NUM_PATTERNS (4)@@ -198,12 +199,12 @@ void streaming_cmd(int ch, char *optarg) stream_pat %= NUM_PATTERNS; break; case OptStreamTo: - file = optarg; - if (!strcmp(file, "-")) + file_cap = optarg; + if (!strcmp(file_cap, "-")) options[OptSilent] = true; break; case OptStreamFrom: - file = optarg; + file_out = optarg; break; case OptStreamMmap: case OptStreamUser:
utils/v4l2-ctl/v4l2-ctl-streaming.cpp static bool fill_buffer_from_file(void *buffers[], unsigned buffer_lengths[], · line 531
Quoted 6 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +2
-1@@ -526,475 +527,546 @@ static bool fill_buffer_from_file(void *buffers[], unsigned buffer_lengths[], return true; } -void streaming_set(int fd) +static void do_setup_cap_buffers(int fd, struct v4l2_requestbuffers *reqbufs, + bool is_mplane, unsigned num_planes, bool is_mmap,
num_planes should be a reference: 'unsigned &num_planes'. This function changes num_planes and the caller needs that! Ditto for do_setup_out_buffers.
Regards,
Hans
Re: [PATCH 2/2] v4l2-ctl: initial attempt to support M2M device streaming
In reply to Tzu-Jung LeeQuoted 16 lines · On Wed April 10 2013 07:35:35 Tzu-Jung Lee wrote:
--- utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 189 +++++++++++++++++++++++++++++++++- 1 file changed, 188 insertions(+), 1 deletion(-)utils/v4l2-ctl/v4l2-ctl-streaming.cpp +4
-0@@ -1058,12 +1058,199 @@ void streaming_set_out(int fd) fclose(fin); } +enum stream_type { + CAP, + OUT, +};
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1074
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +10
-0+ +void streaming_set_m2m(int fd) +{ + int fd_flags = fcntl(fd, F_GETFL); + bool use_poll = options[OptStreamPoll]; + + bool is_mplane = capabilities & + (V4L2_CAP_VIDEO_CAPTURE_MPLANE | + V4L2_CAP_VIDEO_OUTPUT_MPLANE | + V4L2_CAP_VIDEO_M2M_MPLANE);
This should only check for V4L2_CAP_VIDEO_M2M_MPLANE. Only if that is set is M2M valid. I think it is probably a good idea to add some capability checks to each streaming_set_*() function checking up front whether the device supports that particular streaming option.
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1075
Quoted 1 line
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +1
-0+ unsigned num_planes = 1;
num_planes may differ between capture and output, so this should be num_planes[2] = { 1, 1 }
Quoted 5 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +5
-0+ bool is_mmap = options[OptStreamMmap]; + + __u32 type[2]; + FILE *file[2] = {NULL, NULL}; + struct v4l2_requestbuffers reqbufs[2];
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1090
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +10
-0+ + type[CAP] = is_mplane ? + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE; + + type[OUT] = is_mplane ? + V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT; + + memset(&reqbufs, 0, sizeof(reqbufs)); + + reqbufs[CAP].count = reqbufs_count;
Capture and output can have different reqbufs_count values. That static needs to be split up in a capture and output variant.
Quoted 42 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +42
-0+ reqbufs[CAP].type = type[CAP]; + reqbufs[CAP].memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + + reqbufs[OUT].count = reqbufs_count; + reqbufs[OUT].type = type[OUT]; + reqbufs[OUT].memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + + struct v4l2_event_subscription sub; + + memset(&sub, 0, sizeof(sub)); + sub.type = V4L2_EVENT_EOS; + ioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub); + + if (file_cap) { + if (!strcmp(file_cap, "-")) + file[CAP] = stdout; + else + file[CAP] = fopen(file_cap, "w+"); + } + + if (file_out) { + if (!strcmp(file_out, "-")) + file[OUT] = stdin; + else + file[OUT] = fopen(file_out, "r"); + } + + if (doioctl(fd, VIDIOC_REQBUFS, &reqbufs[CAP]) || + doioctl(fd, VIDIOC_REQBUFS, &reqbufs[OUT])) + return; + + void *buffers[2][reqbufs_count * VIDEO_MAX_PLANES]; + unsigned buffer_lengths[2][reqbufs_count * VIDEO_MAX_PLANES]; + + do_setup_cap_buffers(fd, &reqbufs[CAP], is_mplane, num_planes, + is_mmap, buffers[CAP], buffer_lengths[CAP]); + + do_setup_out_buffers(fd, &reqbufs[OUT], is_mplane, num_planes, + is_mmap, buffers[OUT], buffer_lengths[OUT], + file[OUT]); + + if (doioctl(fd, VIDIOC_STREAMON, &type[CAP]) ||
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1142
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +10
-0+ doioctl(fd, VIDIOC_STREAMON, &type[OUT])) + return; + + if (use_poll) + fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK); + + unsigned count[2] = { 0, 0 }; + unsigned last[2] = { 0, 0 }; + struct timeval tv_last[2]; + bool eos[2] = { false, false};
No. eos is valid for the capture only. There is no eos[OUT].
Quoted 71 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +71
-0+ fd_set read_fds; + fd_set write_fds; + fd_set exception_fds; + + while (!eos[CAP] || !eos[OUT]) { + + int r; + + if (use_poll) { + struct timeval tv; + + FD_ZERO(&read_fds); + FD_SET(fd, &read_fds); + + FD_ZERO(&write_fds); + FD_SET(fd, &write_fds); + + FD_ZERO(&exception_fds); + FD_SET(fd, &exception_fds); + + /* Timeout. */ + tv.tv_sec = 2; + tv.tv_usec = 0; + + r = select(fd + 1, &read_fds, &write_fds, &exception_fds, &tv); + + if (r == -1) { + if (EINTR == errno) + continue; + fprintf(stderr, "select error: %s\n", + strerror(errno)); + return; + } + + if (r == 0) { + fprintf(stderr, "select timeout\n"); + return; + } + } + + if (FD_ISSET(fd, &exception_fds)) { + struct v4l2_event ev; + + while (!ioctl(fd, VIDIOC_DQEVENT, &ev)) { + + if (ev.type != V4L2_EVENT_EOS) + continue; + + eos[CAP] = true; + doioctl(fd, VIDIOC_STREAMOFF, &type[CAP]); + break; + } + } + + if (!eos[CAP]) { + if (FD_ISSET(fd, &read_fds)) { + r = do_handle_cap(fd, &reqbufs[CAP], is_mplane, num_planes, + buffers[CAP], buffer_lengths[CAP], file[CAP], + &count[CAP], &last[CAP], &tv_last[CAP]); + if (r < 0) { + eos[CAP] = true; + doioctl(fd, VIDIOC_STREAMOFF, &type[CAP]); + } + } + } + + if (!eos[OUT]) { + if (FD_ISSET(fd, &write_fds)) { + r = do_handle_out(fd, &reqbufs[OUT], is_mplane, num_planes, + buffers[OUT], buffer_lengths[OUT], file[OUT], + &count[OUT], &last[OUT], &tv_last[OUT]);
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1223
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +10
-0+ if (r < 0) { + eos[OUT] = true; + + if (options[OptDecoderCmd]) { + doioctl(fd, VIDIOC_DECODER_CMD, &dec_cmd); + options[OptDecoderCmd] = false; + } + + doioctl(fd, VIDIOC_STREAMOFF, &type[OUT]); +
Rather than using eos[OUT], just 'break' out of the loop here.
Quoted 25 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +22
-0+ } + } + + } + } + + fcntl(fd, F_SETFL, fd_flags); + fprintf(stderr, "\n"); + + do_release_buffers(&reqbufs[CAP], num_planes, is_mmap, + buffers[CAP], buffer_lengths[CAP]); + + do_release_buffers(&reqbufs[OUT], num_planes, is_mmap, + buffers[OUT], buffer_lengths[OUT]); + + if (file[CAP] && file[CAP] != stdout) + fclose(file[CAP]); + + if (file[OUT] && file[OUT] != stdin) + fclose(file[OUT]); +} + void streaming_set(int fd) {
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1256
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +3
-1bool do_cap = options[OptStreamMmap] || options[OptStreamUser]; bool do_out = options[OptStreamOutMmap] || options[OptStreamOutUser]; - if (do_cap) + if (do_cap && do_out) + streaming_set_m2m(fd); + else if (do_cap) streaming_set_cap(fd); else if (do_out) streaming_set_out(fd);
Regards,
Hans
Quoted 9 lines · On Wed April 10 2013 07:35:34 Tzu-Jung Lee wrote:
This patch breaks down the
streaming_set()into smaller ones, which can be resued for supporting m2m devices.Further cleanup or consolidation can be applied with separate patches, since this one tries not to modify logics.
--- utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 888 ++++++++++++++++++---------------- 1 file changed, 480 insertions(+), 408 deletions(-)
Can you also add a 'Signed-off-by' line for these patches? While this code isn't kernel code I still think it would be nice to properly attribute these changes to you.
Regards,
Hans
Re: [PATCH 2/2] v4l2-ctl: initial attempt to support M2M device streaming
In reply to Hans VerkuilQuoted 33 lines · Hans Verkuil
Quoted 16 lines · On Wed April 10 2013 07:35:35 Tzu-Jung Lee wrote:
--- utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 189 +++++++++++++++++++++++++++++++++- 1 file changed, 188 insertions(+), 1 deletion(-)utils/v4l2-ctl/v4l2-ctl-streaming.cpp +4
-0@@ -1058,12 +1058,199 @@ void streaming_set_out(int fd) fclose(fin); } +enum stream_type { + CAP, + OUT, +};utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1074
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +10
-0+ +void streaming_set_m2m(int fd) +{ + int fd_flags = fcntl(fd, F_GETFL); + bool use_poll = options[OptStreamPoll]; + + bool is_mplane = capabilities & + (V4L2_CAP_VIDEO_CAPTURE_MPLANE | + V4L2_CAP_VIDEO_OUTPUT_MPLANE | + V4L2_CAP_VIDEO_M2M_MPLANE);This should only check for
V4L2_CAP_VIDEO_M2M_MPLANE. Only if that is set is M2M valid. I think it is probably a good idea to add some capability checks to each streaming_set_*() function checking up front whether the device supports that particular streaming option.
Quoted 4 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1075
Quoted 1 line
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +1
-0+ unsigned num_planes = 1;
num_planesmay differ between capture and output, so this should be num_planes[2] = { 1, 1 }
Sure.
Quoted 149 lines
Quoted 5 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +5
-0+ bool is_mmap = options[OptStreamMmap]; + + __u32 type[2]; + FILE *file[2] = {NULL, NULL}; + struct v4l2_requestbuffers reqbufs[2];utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1090
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +10
-0+ + type[CAP] = is_mplane ? + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE; + + type[OUT] = is_mplane ? + V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_OUTPUT; + + memset(&reqbufs, 0, sizeof(reqbufs)); + + reqbufs[CAP].count = reqbufs_count;Capture and output can have different
reqbufs_countvalues. That static needs to be split up in a capture and output variant.Quoted 42 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +42
-0+ reqbufs[CAP].type = type[CAP]; + reqbufs[CAP].memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + + reqbufs[OUT].count = reqbufs_count; + reqbufs[OUT].type = type[OUT]; + reqbufs[OUT].memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR; + + struct v4l2_event_subscription sub; + + memset(&sub, 0, sizeof(sub)); + sub.type = V4L2_EVENT_EOS; + ioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub); + + if (file_cap) { + if (!strcmp(file_cap, "-")) + file[CAP] = stdout; + else + file[CAP] = fopen(file_cap, "w+"); + } + + if (file_out) { + if (!strcmp(file_out, "-")) + file[OUT] = stdin; + else + file[OUT] = fopen(file_out, "r"); + } + + if (doioctl(fd, VIDIOC_REQBUFS, &reqbufs[CAP]) || + doioctl(fd, VIDIOC_REQBUFS, &reqbufs[OUT])) + return; + + void *buffers[2][reqbufs_count * VIDEO_MAX_PLANES]; + unsigned buffer_lengths[2][reqbufs_count * VIDEO_MAX_PLANES]; + + do_setup_cap_buffers(fd, &reqbufs[CAP], is_mplane, num_planes, + is_mmap, buffers[CAP], buffer_lengths[CAP]); + + do_setup_out_buffers(fd, &reqbufs[OUT], is_mplane, num_planes, + is_mmap, buffers[OUT], buffer_lengths[OUT], + file[OUT]); + + if (doioctl(fd, VIDIOC_STREAMON, &type[CAP]) ||utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1142
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +10
-0+ doioctl(fd, VIDIOC_STREAMON, &type[OUT])) + return; + + if (use_poll) + fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK); + + unsigned count[2] = { 0, 0 }; + unsigned last[2] = { 0, 0 }; + struct timeval tv_last[2]; + bool eos[2] = { false, false};No. eos is valid for the capture only. There is no eos[OUT].
Quoted 75 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +75
-0+ fd_set read_fds; + fd_set write_fds; + fd_set exception_fds; + + while (!eos[CAP] || !eos[OUT]) { + + int r; + + if (use_poll) { + struct timeval tv; + + FD_ZERO(&read_fds); + FD_SET(fd, &read_fds); + + FD_ZERO(&write_fds); + FD_SET(fd, &write_fds); + + FD_ZERO(&exception_fds); + FD_SET(fd, &exception_fds); + + /* Timeout. */ + tv.tv_sec = 2; + tv.tv_usec = 0; + + r = select(fd + 1, &read_fds, &write_fds, &exception_fds, &tv); + + if (r == -1) { + if (EINTR == errno) + continue; + fprintf(stderr, "select error: %s\n", + strerror(errno)); + return; + } + + if (r == 0) { + fprintf(stderr, "select timeout\n"); + return; + } + } + + if (FD_ISSET(fd, &exception_fds)) { + struct v4l2_event ev; + + while (!ioctl(fd, VIDIOC_DQEVENT, &ev)) { + + if (ev.type != V4L2_EVENT_EOS) + continue; + + eos[CAP] = true; + doioctl(fd, VIDIOC_STREAMOFF, &type[CAP]); + break; + } + } + + if (!eos[CAP]) { + if (FD_ISSET(fd, &read_fds)) { + r = do_handle_cap(fd, &reqbufs[CAP], is_mplane, num_planes, + buffers[CAP], buffer_lengths[CAP], file[CAP], + &count[CAP], &last[CAP], &tv_last[CAP]); + if (r < 0) { + eos[CAP] = true; + doioctl(fd, VIDIOC_STREAMOFF, &type[CAP]); + } + } + } + + if (!eos[OUT]) { + if (FD_ISSET(fd, &write_fds)) { + r = do_handle_out(fd, &reqbufs[OUT], is_mplane, num_planes, + buffers[OUT], buffer_lengths[OUT], file[OUT], + &count[OUT], &last[OUT], &tv_last[OUT]); + if (r < 0) { + eos[OUT] = true; + + if (options[OptDecoderCmd]) {
Quoted 8 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1223
Quoted 6 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +6
-0+ doioctl(fd, VIDIOC_DECODER_CMD, &dec_cmd); + options[OptDecoderCmd] = false; + } + + doioctl(fd, VIDIOC_STREAMOFF, &type[OUT]); +Rather than using eos[OUT], just 'break' out of the loop here.
If we break out here, the unfinished captured stream will also be left out, isn't it?
Oh, I'm working on a M2M device, which works with bitstreams only, and no frames on Linux.
So we keep feeding input streams via the OUTPUT path, and get transcoded bitstream from
CAPTURE path. In this case, we need to keep pulling out the transcoded bitstreams in the loop.
I think this is also only relevant to compressed formats again.
Quoted 39 lines
Quoted 25 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +22
-0+ } + } + + } + } + + fcntl(fd, F_SETFL, fd_flags); + fprintf(stderr, "\n"); + + do_release_buffers(&reqbufs[CAP], num_planes, is_mmap, + buffers[CAP], buffer_lengths[CAP]); + + do_release_buffers(&reqbufs[OUT], num_planes, is_mmap, + buffers[OUT], buffer_lengths[OUT]); + + if (file[CAP] && file[CAP] != stdout) + fclose(file[CAP]); + + if (file[OUT] && file[OUT] != stdin) + fclose(file[OUT]); +} + void streaming_set(int fd) {utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1256
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +3
-1bool do_cap = options[OptStreamMmap] || options[OptStreamUser]; bool do_out = options[OptStreamOutMmap] || options[OptStreamOutUser]; - if (do_cap) + if (do_cap && do_out) + streaming_set_m2m(fd); + else if (do_cap) streaming_set_cap(fd); else if (do_out) streaming_set_out(fd);Regards,
Hans
Re: [PATCH 2/2] v4l2-ctl: initial attempt to support M2M device streaming
Quoted 19 lines · On Wed 10 April 2013 10:38:13 Tzu-Jung Lee wrote:
Quoted 9 lines · Hans Verkuil
Quoted 9 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +9
-0+ if (!eos[OUT]) { + if (FD_ISSET(fd, &write_fds)) { + r = do_handle_out(fd, &reqbufs[OUT], is_mplane, num_planes, + buffers[OUT], buffer_lengths[OUT], file[OUT], + &count[OUT], &last[OUT], &tv_last[OUT]); + if (r < 0) { + eos[OUT] = true; + + if (options[OptDecoderCmd]) {Quoted 8 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1223
Quoted 6 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +6
-0+ doioctl(fd, VIDIOC_DECODER_CMD, &dec_cmd); + options[OptDecoderCmd] = false; + } + + doioctl(fd, VIDIOC_STREAMOFF, &type[OUT]); +Rather than using eos[OUT], just 'break' out of the loop here.
Quoted 2 lines
If we break out here, the unfinished captured stream will also be left out, isn't it?
That's a good point. So it's OK to keep the eos[2] array. However, to make this work correctly the while loop should use '&&' instead of '||'. I.e., only break out when both sides have eos set.
In addition, once eos is set for one of the two sides, make sure that you no longer attempt to select on that fd for read+exception (capture) or write (output).
You may also have to modify the way stream_count and skip are handled in an m2m case: those should only apply to the output side of things, it makes no sense to apply them to the capture side.
Regards,
Hans
Quoted 48 lines
Oh, I'm working on a M2M device, which works with bitstreams only, and no frames on Linux.
So we keep feeding input streams via the OUTPUT path, and get transcoded bitstream from
CAPTURE path. In this case, we need to keep pulling out the transcoded bitstreams in the loop.
I think this is also only relevant to compressed formats again.Quoted 39 lines
Quoted 25 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +22
-0+ } + } + + } + } + + fcntl(fd, F_SETFL, fd_flags); + fprintf(stderr, "\n"); + + do_release_buffers(&reqbufs[CAP], num_planes, is_mmap, + buffers[CAP], buffer_lengths[CAP]); + + do_release_buffers(&reqbufs[OUT], num_planes, is_mmap, + buffers[OUT], buffer_lengths[OUT]); + + if (file[CAP] && file[CAP] != stdout) + fclose(file[CAP]); + + if (file[OUT] && file[OUT] != stdin) + fclose(file[OUT]); +} + void streaming_set(int fd) {utils/v4l2-ctl/v4l2-ctl-streaming.cpp void streaming_set_out(int fd) · line 1256
Quoted 10 lines
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +3
-1bool do_cap = options[OptStreamMmap] || options[OptStreamUser]; bool do_out = options[OptStreamOutMmap] || options[OptStreamOutUser]; - if (do_cap) + if (do_cap && do_out) + streaming_set_m2m(fd); + else if (do_cap) streaming_set_cap(fd); else if (do_out) streaming_set_out(fd);Regards,
Hans