Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 1 | #include "y2020/vision/v4l2_reader.h" |
| 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <linux/videodev2.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/types.h> |
| 8 | |
| 9 | namespace frc971 { |
| 10 | namespace vision { |
| 11 | |
| 12 | V4L2Reader::V4L2Reader(aos::EventLoop *event_loop, |
| 13 | const std::string &device_name) |
| 14 | : fd_(open(device_name.c_str(), O_RDWR | O_NONBLOCK)) { |
| 15 | PCHECK(fd_.get() != -1); |
| 16 | |
| 17 | // First, clean up after anybody else who left the device streaming. |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 18 | StreamOff(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 19 | |
| 20 | { |
| 21 | struct v4l2_format format; |
| 22 | memset(&format, 0, sizeof(format)); |
| 23 | format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 24 | format.fmt.pix.width = cols_; |
| 25 | format.fmt.pix.height = rows_; |
| 26 | format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 27 | // This means we want to capture from a progressive (non-interlaced) source. |
| 28 | format.fmt.pix.field = V4L2_FIELD_NONE; |
| 29 | PCHECK(Ioctl(VIDIOC_S_FMT, &format) == 0); |
| 30 | CHECK_EQ(static_cast<int>(format.fmt.pix.width), cols_); |
| 31 | CHECK_EQ(static_cast<int>(format.fmt.pix.height), rows_); |
| 32 | CHECK_EQ(static_cast<int>(format.fmt.pix.bytesperline), |
| 33 | cols_ * 2 /* bytes per pixel */); |
| 34 | CHECK_EQ(format.fmt.pix.sizeimage, ImageSize()); |
| 35 | } |
| 36 | |
| 37 | { |
| 38 | struct v4l2_requestbuffers request; |
| 39 | memset(&request, 0, sizeof(request)); |
| 40 | request.count = buffers_.size(); |
| 41 | request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 42 | request.memory = V4L2_MEMORY_USERPTR; |
| 43 | PCHECK(Ioctl(VIDIOC_REQBUFS, &request) == 0); |
| 44 | CHECK_EQ(request.count, buffers_.size()) |
| 45 | << ": Kernel refused to give us the number of buffers we asked for"; |
| 46 | } |
| 47 | |
| 48 | for (size_t i = 0; i < buffers_.size(); ++i) { |
| 49 | buffers_[i].sender = event_loop->MakeSender<CameraImage>("/camera"); |
| 50 | EnqueueBuffer(i); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 55 | PCHECK(Ioctl(VIDIOC_STREAMON, &type) == 0); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | absl::Span<const char> V4L2Reader::ReadLatestImage() { |
| 60 | // First, enqueue any old buffer we already have. This is the one which may |
| 61 | // have been sent. |
| 62 | if (saved_buffer_ != -1) { |
| 63 | EnqueueBuffer(saved_buffer_); |
| 64 | saved_buffer_ = -1; |
| 65 | } |
| 66 | while (true) { |
| 67 | const int previous_buffer = saved_buffer_; |
| 68 | saved_buffer_ = DequeueBuffer(); |
| 69 | if (saved_buffer_ != -1) { |
| 70 | // We got a new buffer. Return the previous one (if relevant) and keep |
| 71 | // going. |
| 72 | if (previous_buffer != -1) { |
| 73 | EnqueueBuffer(previous_buffer); |
| 74 | } |
| 75 | continue; |
| 76 | } |
| 77 | if (previous_buffer == -1) { |
| 78 | // There were no images to read. Return an indication of that. |
| 79 | return absl::Span<const char>(); |
| 80 | } |
| 81 | // We didn't get a new one, but we already got one in a previous |
| 82 | // iteration, which means we found an image so return it. |
| 83 | saved_buffer_ = previous_buffer; |
| 84 | return buffers_[saved_buffer_].DataSpan(ImageSize()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void V4L2Reader::SendLatestImage() { |
| 89 | buffers_[saved_buffer_].Send(rows_, cols_, ImageSize()); |
| 90 | } |
| 91 | |
| 92 | int V4L2Reader::Ioctl(unsigned long number, void *arg) { |
| 93 | return ioctl(fd_.get(), number, arg); |
| 94 | } |
| 95 | |
| 96 | int V4L2Reader::DequeueBuffer() { |
| 97 | struct v4l2_buffer buffer; |
| 98 | memset(&buffer, 0, sizeof(buffer)); |
| 99 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 100 | buffer.memory = V4L2_MEMORY_USERPTR; |
| 101 | const int result = Ioctl(VIDIOC_DQBUF, &buffer); |
| 102 | if (result == -1 && errno == EAGAIN) { |
| 103 | return -1; |
| 104 | } |
| 105 | PCHECK(result == 0) << ": VIDIOC_DQBUF failed"; |
| 106 | CHECK_LT(buffer.index, buffers_.size()); |
| 107 | LOG(INFO) << "dequeued " << buffer.index; |
| 108 | CHECK_EQ(reinterpret_cast<uintptr_t>(buffers_[buffer.index].data_pointer), |
| 109 | buffer.m.userptr); |
| 110 | CHECK_EQ(ImageSize(), buffer.length); |
| 111 | return buffer.index; |
| 112 | } |
| 113 | |
| 114 | void V4L2Reader::EnqueueBuffer(int buffer_number) { |
| 115 | LOG(INFO) << "enqueueing " << buffer_number; |
| 116 | CHECK_GE(buffer_number, 0); |
| 117 | CHECK_LT(buffer_number, static_cast<int>(buffers_.size())); |
| 118 | buffers_[buffer_number].InitializeMessage(ImageSize()); |
| 119 | struct v4l2_buffer buffer; |
| 120 | memset(&buffer, 0, sizeof(buffer)); |
| 121 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 122 | buffer.memory = V4L2_MEMORY_USERPTR; |
| 123 | buffer.index = buffer_number; |
| 124 | buffer.m.userptr = |
| 125 | reinterpret_cast<uintptr_t>(buffers_[buffer_number].data_pointer); |
| 126 | buffer.length = ImageSize(); |
| 127 | PCHECK(Ioctl(VIDIOC_QBUF, &buffer) == 0); |
| 128 | } |
| 129 | |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 130 | void V4L2Reader::StreamOff() { |
| 131 | int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 132 | const int result = Ioctl(VIDIOC_STREAMOFF, &type); |
| 133 | if (result == 0) { |
| 134 | return; |
| 135 | } |
| 136 | // Some devices (like Alex's webcam) return this if streaming isn't currently |
| 137 | // on, unlike what the documentations says should happen. |
| 138 | if (errno == EBUSY) { |
| 139 | return; |
| 140 | } |
| 141 | PLOG(FATAL) << "VIDIOC_STREAMOFF failed"; |
| 142 | } |
| 143 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 144 | } // namespace vision |
| 145 | } // namespace frc971 |