Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 1 | #include "frc971/vision/v4l2_reader.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 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 | |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 9 | DEFINE_bool(ignore_timestamps, false, |
| 10 | "Don't require timestamps on images. Used to allow webcams"); |
| 11 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 12 | namespace frc971 { |
| 13 | namespace vision { |
| 14 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 15 | V4L2ReaderBase::V4L2ReaderBase(aos::EventLoop *event_loop, |
| 16 | const std::string &device_name) |
| 17 | : fd_(open(device_name.c_str(), O_RDWR | O_NONBLOCK)), |
| 18 | event_loop_(event_loop) { |
Jim Ostrowski | fec0c33 | 2022-02-06 23:28:26 -0800 | [diff] [blame] | 19 | PCHECK(fd_.get() != -1) << " Failed to open device " << device_name; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 20 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 21 | // Figure out if we are multi-planar or not. |
| 22 | { |
| 23 | struct v4l2_capability capability; |
| 24 | memset(&capability, 0, sizeof(capability)); |
| 25 | PCHECK(Ioctl(VIDIOC_QUERYCAP, &capability) == 0); |
| 26 | |
| 27 | LOG(INFO) << "Opening " << device_name; |
| 28 | LOG(INFO) << " driver " << capability.driver; |
| 29 | LOG(INFO) << " card " << capability.card; |
| 30 | LOG(INFO) << " bus_info " << capability.bus_info; |
| 31 | if (capability.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) { |
| 32 | LOG(INFO) << " Multi-planar"; |
| 33 | multiplanar_ = true; |
| 34 | } |
| 35 | } |
| 36 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 37 | // First, clean up after anybody else who left the device streaming. |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 38 | StreamOff(); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 39 | } |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 41 | void V4L2ReaderBase::StreamOn() { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 42 | { |
| 43 | struct v4l2_requestbuffers request; |
| 44 | memset(&request, 0, sizeof(request)); |
| 45 | request.count = buffers_.size(); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 46 | request.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 47 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 48 | request.memory = V4L2_MEMORY_USERPTR; |
| 49 | PCHECK(Ioctl(VIDIOC_REQBUFS, &request) == 0); |
| 50 | CHECK_EQ(request.count, buffers_.size()) |
| 51 | << ": Kernel refused to give us the number of buffers we asked for"; |
| 52 | } |
| 53 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 54 | { |
| 55 | struct v4l2_format format; |
| 56 | memset(&format, 0, sizeof(format)); |
| 57 | format.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 58 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 59 | PCHECK(Ioctl(VIDIOC_G_FMT, &format) == 0); |
| 60 | |
| 61 | if (multiplanar()) { |
| 62 | cols_ = format.fmt.pix_mp.width; |
| 63 | rows_ = format.fmt.pix_mp.height; |
| 64 | LOG(INFO) << "Format is " << cols_ << ", " << rows_; |
| 65 | CHECK_EQ(format.fmt.pix_mp.pixelformat, V4L2_PIX_FMT_YUYV) |
| 66 | << ": Invalid pixel format"; |
| 67 | |
| 68 | CHECK_EQ(format.fmt.pix_mp.num_planes, 1u); |
| 69 | |
| 70 | CHECK_EQ(static_cast<int>(format.fmt.pix_mp.plane_fmt[0].bytesperline), |
| 71 | cols_ * 2 /* bytes per pixel */); |
| 72 | CHECK_EQ(format.fmt.pix_mp.plane_fmt[0].sizeimage, ImageSize()); |
| 73 | } else { |
| 74 | cols_ = format.fmt.pix.width; |
| 75 | rows_ = format.fmt.pix.height; |
| 76 | LOG(INFO) << "Format is " << cols_ << ", " << rows_; |
| 77 | CHECK_EQ(format.fmt.pix.pixelformat, V4L2_PIX_FMT_YUYV) |
| 78 | << ": Invalid pixel format"; |
| 79 | |
| 80 | CHECK_EQ(static_cast<int>(format.fmt.pix.bytesperline), |
| 81 | cols_ * 2 /* bytes per pixel */); |
| 82 | CHECK_EQ(format.fmt.pix.sizeimage, ImageSize()); |
| 83 | } |
| 84 | } |
| 85 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 86 | for (size_t i = 0; i < buffers_.size(); ++i) { |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 87 | buffers_[i].sender = event_loop_->MakeSender<CameraImage>("/camera"); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 88 | EnqueueBuffer(i); |
| 89 | } |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 90 | int type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 91 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 92 | PCHECK(Ioctl(VIDIOC_STREAMON, &type) == 0); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 95 | bool V4L2ReaderBase::ReadLatestImage() { |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 96 | // First, enqueue any old buffer we already have. This is the one which |
| 97 | // may have been sent. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 98 | if (saved_buffer_) { |
| 99 | EnqueueBuffer(saved_buffer_.index); |
| 100 | saved_buffer_.Clear(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 101 | } |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 102 | ftrace_.FormatMessage("Enqueued previous buffer %d", saved_buffer_.index); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 103 | while (true) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 104 | const BufferInfo previous_buffer = saved_buffer_; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 105 | saved_buffer_ = DequeueBuffer(); |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 106 | ftrace_.FormatMessage("Dequeued %d", saved_buffer_.index); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 107 | if (saved_buffer_) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 108 | // We got a new buffer. Return the previous one (if relevant) and keep |
| 109 | // going. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 110 | if (previous_buffer) { |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 111 | ftrace_.FormatMessage("Previous %d", previous_buffer.index); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 112 | EnqueueBuffer(previous_buffer.index); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 113 | } |
| 114 | continue; |
| 115 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 116 | if (!previous_buffer) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 117 | // There were no images to read. Return an indication of that. |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 118 | ftrace_.FormatMessage("No images to read"); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 119 | return false; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 120 | } |
| 121 | // We didn't get a new one, but we already got one in a previous |
| 122 | // iteration, which means we found an image so return it. |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 123 | ftrace_.FormatMessage("Got saved buffer %d", saved_buffer_.index); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 124 | saved_buffer_ = previous_buffer; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 125 | buffers_[saved_buffer_.index].PrepareMessage(rows_, cols_, ImageSize(), |
| 126 | saved_buffer_.monotonic_eof); |
| 127 | return true; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 131 | void V4L2ReaderBase::SendLatestImage() { buffers_[saved_buffer_.index].Send(); } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 132 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 133 | void V4L2ReaderBase::SetExposure(size_t duration) { |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 134 | v4l2_control manual_control; |
| 135 | manual_control.id = V4L2_CID_EXPOSURE_AUTO; |
| 136 | manual_control.value = V4L2_EXPOSURE_MANUAL; |
| 137 | PCHECK(Ioctl(VIDIOC_S_CTRL, &manual_control) == 0); |
| 138 | |
| 139 | v4l2_control exposure_control; |
| 140 | exposure_control.id = V4L2_CID_EXPOSURE_ABSOLUTE; |
| 141 | exposure_control.value = static_cast<int>(duration); // 100 micro s units |
| 142 | PCHECK(Ioctl(VIDIOC_S_CTRL, &exposure_control) == 0); |
| 143 | } |
| 144 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 145 | void V4L2ReaderBase::UseAutoExposure() { |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 146 | v4l2_control control; |
| 147 | control.id = V4L2_CID_EXPOSURE_AUTO; |
| 148 | control.value = V4L2_EXPOSURE_AUTO; |
| 149 | PCHECK(Ioctl(VIDIOC_S_CTRL, &control) == 0); |
| 150 | } |
| 151 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 152 | void V4L2ReaderBase::Buffer::InitializeMessage(size_t max_image_size) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 153 | message_offset = flatbuffers::Offset<CameraImage>(); |
| 154 | builder = aos::Sender<CameraImage>::Builder(); |
| 155 | builder = sender.MakeBuilder(); |
| 156 | // The kernel has an undocumented requirement that the buffer is aligned |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 157 | // to 128 bytes. If you give it a nonaligned pointer, it will return EINVAL |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 158 | // and only print something in dmesg with the relevant dynamic debug |
| 159 | // prints turned on. |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 160 | builder.fbb()->StartIndeterminateVector(max_image_size, 1, 128, |
| 161 | &data_pointer); |
| 162 | CHECK_EQ(reinterpret_cast<uintptr_t>(data_pointer) % 128, 0u) |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 163 | << ": Flatbuffers failed to align things as requested"; |
| 164 | } |
| 165 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 166 | void V4L2ReaderBase::Buffer::PrepareMessage( |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 167 | int rows, int cols, size_t image_size, |
| 168 | aos::monotonic_clock::time_point monotonic_eof) { |
| 169 | CHECK(data_pointer != nullptr); |
| 170 | data_pointer = nullptr; |
| 171 | |
| 172 | const auto data_offset = builder.fbb()->EndIndeterminateVector(image_size, 1); |
| 173 | auto image_builder = builder.MakeBuilder<CameraImage>(); |
| 174 | image_builder.add_data(data_offset); |
| 175 | image_builder.add_rows(rows); |
| 176 | image_builder.add_cols(cols); |
| 177 | image_builder.add_monotonic_timestamp_ns( |
| 178 | std::chrono::nanoseconds(monotonic_eof.time_since_epoch()).count()); |
| 179 | message_offset = image_builder.Finish(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 182 | int V4L2ReaderBase::Ioctl(unsigned long number, void *arg) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 183 | return ioctl(fd_.get(), number, arg); |
| 184 | } |
| 185 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 186 | V4L2ReaderBase::BufferInfo V4L2ReaderBase::DequeueBuffer() { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 187 | struct v4l2_buffer buffer; |
| 188 | memset(&buffer, 0, sizeof(buffer)); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 189 | buffer.memory = V4L2_MEMORY_USERPTR; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 190 | if (multiplanar()) { |
| 191 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 192 | struct v4l2_plane planes[1]; |
| 193 | std::memset(planes, 0, sizeof(planes)); |
| 194 | buffer.m.planes = planes; |
| 195 | buffer.length = 1; |
| 196 | const int result = Ioctl(VIDIOC_DQBUF, &buffer); |
| 197 | if (result == -1 && errno == EAGAIN) { |
| 198 | return BufferInfo(); |
| 199 | } |
| 200 | PCHECK(result == 0) << ": VIDIOC_DQBUF failed"; |
| 201 | CHECK_LT(buffer.index, buffers_.size()); |
| 202 | |
| 203 | CHECK_EQ(reinterpret_cast<uintptr_t>(buffers_[buffer.index].data_pointer), |
| 204 | planes[0].m.userptr); |
| 205 | |
| 206 | CHECK_EQ(ImageSize(), planes[0].length); |
| 207 | } else { |
| 208 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 209 | const int result = Ioctl(VIDIOC_DQBUF, &buffer); |
| 210 | if (result == -1 && errno == EAGAIN) { |
| 211 | return BufferInfo(); |
| 212 | } |
| 213 | PCHECK(result == 0) << ": VIDIOC_DQBUF failed"; |
| 214 | CHECK_LT(buffer.index, buffers_.size()); |
| 215 | CHECK_EQ(reinterpret_cast<uintptr_t>(buffers_[buffer.index].data_pointer), |
| 216 | buffer.m.userptr); |
| 217 | CHECK_EQ(ImageSize(), buffer.length); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 218 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 219 | CHECK(buffer.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 220 | if (!FLAGS_ignore_timestamps) { |
| 221 | // Require that we have good timestamp on images |
| 222 | CHECK_EQ(buffer.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK, |
| 223 | static_cast<uint32_t>(V4L2_BUF_FLAG_TSTAMP_SRC_EOF)); |
| 224 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 225 | return {static_cast<int>(buffer.index), |
| 226 | aos::time::from_timeval(buffer.timestamp)}; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 229 | void V4L2ReaderBase::EnqueueBuffer(int buffer_number) { |
| 230 | // TODO(austin): Detect multiplanar and do this all automatically. |
| 231 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 232 | CHECK_GE(buffer_number, 0); |
| 233 | CHECK_LT(buffer_number, static_cast<int>(buffers_.size())); |
| 234 | buffers_[buffer_number].InitializeMessage(ImageSize()); |
| 235 | struct v4l2_buffer buffer; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 236 | struct v4l2_plane planes[1]; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 237 | memset(&buffer, 0, sizeof(buffer)); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 238 | memset(&planes, 0, sizeof(planes)); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 239 | buffer.memory = V4L2_MEMORY_USERPTR; |
| 240 | buffer.index = buffer_number; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 241 | if (multiplanar()) { |
| 242 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 243 | buffer.m.planes = planes; |
| 244 | buffer.length = 1; |
| 245 | planes[0].m.userptr = |
| 246 | reinterpret_cast<uintptr_t>(buffers_[buffer_number].data_pointer); |
| 247 | planes[0].length = ImageSize(); |
| 248 | planes[0].bytesused = planes[0].length; |
| 249 | } else { |
| 250 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 251 | buffer.m.userptr = |
| 252 | reinterpret_cast<uintptr_t>(buffers_[buffer_number].data_pointer); |
| 253 | buffer.length = ImageSize(); |
| 254 | } |
| 255 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 256 | PCHECK(Ioctl(VIDIOC_QBUF, &buffer) == 0); |
| 257 | } |
| 258 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 259 | void V4L2ReaderBase::StreamOff() { |
| 260 | int type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 261 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 262 | const int result = Ioctl(VIDIOC_STREAMOFF, &type); |
| 263 | if (result == 0) { |
| 264 | return; |
| 265 | } |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 266 | // Some devices (like Alex's webcam) return this if streaming isn't |
| 267 | // currently on, unlike what the documentations says should happen. |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 268 | if (errno == EBUSY) { |
| 269 | return; |
| 270 | } |
| 271 | PLOG(FATAL) << "VIDIOC_STREAMOFF failed"; |
| 272 | } |
| 273 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 274 | V4L2Reader::V4L2Reader(aos::EventLoop *event_loop, |
| 275 | const std::string &device_name) |
| 276 | : V4L2ReaderBase(event_loop, device_name) { |
| 277 | // Don't know why this magic call to SetExposure is required (before the |
| 278 | // camera settings are configured) to make things work on boot of the pi, but |
| 279 | // it seems to be-- without it, the image exposure is wrong (too dark). Note-- |
| 280 | // any valid value seems to work-- just choosing 1 for now |
| 281 | |
| 282 | SetExposure(1); |
| 283 | |
| 284 | struct v4l2_format format; |
| 285 | memset(&format, 0, sizeof(format)); |
| 286 | format.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 287 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 288 | format.fmt.pix.width = cols_; |
| 289 | format.fmt.pix.height = rows_; |
| 290 | format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 291 | // This means we want to capture from a progressive (non-interlaced) |
| 292 | // source. |
| 293 | format.fmt.pix.field = V4L2_FIELD_NONE; |
| 294 | PCHECK(Ioctl(VIDIOC_S_FMT, &format) == 0); |
| 295 | CHECK_EQ(static_cast<int>(format.fmt.pix.width), cols_); |
| 296 | CHECK_EQ(static_cast<int>(format.fmt.pix.height), rows_); |
| 297 | CHECK_EQ(static_cast<int>(format.fmt.pix.bytesperline), |
| 298 | cols_ * 2 /* bytes per pixel */); |
| 299 | CHECK_EQ(format.fmt.pix.sizeimage, ImageSize()); |
| 300 | |
| 301 | StreamOn(); |
| 302 | } |
| 303 | |
| 304 | RockchipV4L2Reader::RockchipV4L2Reader(aos::EventLoop *event_loop, |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame^] | 305 | aos::internal::EPoll *epoll, |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 306 | const std::string &device_name) |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame^] | 307 | : V4L2ReaderBase(event_loop, device_name), epoll_(epoll) { |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 308 | StreamOn(); |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame^] | 309 | epoll_->OnReadable(fd().get(), [this]() { OnImageReady(); }); |
| 310 | } |
| 311 | |
| 312 | void RockchipV4L2Reader::OnImageReady() { |
| 313 | if (!ReadLatestImage()) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | SendLatestImage(); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 320 | } // namespace vision |
| 321 | } // namespace frc971 |