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"); |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 88 | MarkBufferToBeEnqueued(i); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 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 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 95 | void V4L2ReaderBase::MarkBufferToBeEnqueued(int buffer_index) { |
| 96 | ReinitializeBuffer(buffer_index); |
| 97 | EnqueueBuffer(buffer_index); |
| 98 | } |
| 99 | |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 100 | void V4L2ReaderBase::MaybeEnqueue() { |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 101 | // First, enqueue any old buffer we already have. This is the one which |
| 102 | // may have been sent. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 103 | if (saved_buffer_) { |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 104 | MarkBufferToBeEnqueued(saved_buffer_.index); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 105 | saved_buffer_.Clear(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 106 | } |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 107 | ftrace_.FormatMessage("Enqueued previous buffer %d", saved_buffer_.index); |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool V4L2ReaderBase::ReadLatestImage() { |
| 111 | MaybeEnqueue(); |
| 112 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 113 | while (true) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 114 | const BufferInfo previous_buffer = saved_buffer_; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 115 | saved_buffer_ = DequeueBuffer(); |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 116 | ftrace_.FormatMessage("Dequeued %d", saved_buffer_.index); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 117 | if (saved_buffer_) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 118 | // We got a new buffer. Return the previous one (if relevant) and keep |
| 119 | // going. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 120 | if (previous_buffer) { |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 121 | ftrace_.FormatMessage("Previous %d", previous_buffer.index); |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 122 | MarkBufferToBeEnqueued(previous_buffer.index); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 123 | } |
| 124 | continue; |
| 125 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 126 | if (!previous_buffer) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 127 | // There were no images to read. Return an indication of that. |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 128 | ftrace_.FormatMessage("No images to read"); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 129 | return false; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 130 | } |
| 131 | // We didn't get a new one, but we already got one in a previous |
| 132 | // iteration, which means we found an image so return it. |
Austin Schuh | e4acc1e | 2022-12-26 14:01:34 -0800 | [diff] [blame] | 133 | ftrace_.FormatMessage("Got saved buffer %d", saved_buffer_.index); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 134 | saved_buffer_ = previous_buffer; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 135 | buffers_[saved_buffer_.index].PrepareMessage(rows_, cols_, ImageSize(), |
| 136 | saved_buffer_.monotonic_eof); |
| 137 | return true; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 141 | void V4L2ReaderBase::SendLatestImage() { |
| 142 | buffers_[saved_buffer_.index].Send(); |
| 143 | |
| 144 | MarkBufferToBeEnqueued(saved_buffer_.index); |
| 145 | saved_buffer_.Clear(); |
| 146 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 147 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 148 | void V4L2ReaderBase::SetExposure(size_t duration) { |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 149 | v4l2_control manual_control; |
| 150 | manual_control.id = V4L2_CID_EXPOSURE_AUTO; |
| 151 | manual_control.value = V4L2_EXPOSURE_MANUAL; |
| 152 | PCHECK(Ioctl(VIDIOC_S_CTRL, &manual_control) == 0); |
| 153 | |
| 154 | v4l2_control exposure_control; |
| 155 | exposure_control.id = V4L2_CID_EXPOSURE_ABSOLUTE; |
| 156 | exposure_control.value = static_cast<int>(duration); // 100 micro s units |
| 157 | PCHECK(Ioctl(VIDIOC_S_CTRL, &exposure_control) == 0); |
| 158 | } |
| 159 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 160 | void V4L2ReaderBase::UseAutoExposure() { |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 161 | v4l2_control control; |
| 162 | control.id = V4L2_CID_EXPOSURE_AUTO; |
| 163 | control.value = V4L2_EXPOSURE_AUTO; |
| 164 | PCHECK(Ioctl(VIDIOC_S_CTRL, &control) == 0); |
| 165 | } |
| 166 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 167 | void V4L2ReaderBase::Buffer::InitializeMessage(size_t max_image_size) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 168 | message_offset = flatbuffers::Offset<CameraImage>(); |
| 169 | builder = aos::Sender<CameraImage>::Builder(); |
| 170 | builder = sender.MakeBuilder(); |
| 171 | // The kernel has an undocumented requirement that the buffer is aligned |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 172 | // 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] | 173 | // and only print something in dmesg with the relevant dynamic debug |
| 174 | // prints turned on. |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 175 | builder.fbb()->StartIndeterminateVector(max_image_size, 1, 128, |
| 176 | &data_pointer); |
| 177 | CHECK_EQ(reinterpret_cast<uintptr_t>(data_pointer) % 128, 0u) |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 178 | << ": Flatbuffers failed to align things as requested"; |
| 179 | } |
| 180 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 181 | void V4L2ReaderBase::Buffer::PrepareMessage( |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 182 | int rows, int cols, size_t image_size, |
| 183 | aos::monotonic_clock::time_point monotonic_eof) { |
| 184 | CHECK(data_pointer != nullptr); |
| 185 | data_pointer = nullptr; |
| 186 | |
| 187 | const auto data_offset = builder.fbb()->EndIndeterminateVector(image_size, 1); |
| 188 | auto image_builder = builder.MakeBuilder<CameraImage>(); |
| 189 | image_builder.add_data(data_offset); |
| 190 | image_builder.add_rows(rows); |
| 191 | image_builder.add_cols(cols); |
| 192 | image_builder.add_monotonic_timestamp_ns( |
| 193 | std::chrono::nanoseconds(monotonic_eof.time_since_epoch()).count()); |
| 194 | message_offset = image_builder.Finish(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 197 | int V4L2ReaderBase::Ioctl(unsigned long number, void *arg) { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 198 | return ioctl(fd_.get(), number, arg); |
| 199 | } |
| 200 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 201 | V4L2ReaderBase::BufferInfo V4L2ReaderBase::DequeueBuffer() { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 202 | struct v4l2_buffer buffer; |
| 203 | memset(&buffer, 0, sizeof(buffer)); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 204 | buffer.memory = V4L2_MEMORY_USERPTR; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 205 | if (multiplanar()) { |
| 206 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 207 | struct v4l2_plane planes[1]; |
| 208 | std::memset(planes, 0, sizeof(planes)); |
| 209 | buffer.m.planes = planes; |
| 210 | buffer.length = 1; |
| 211 | const int result = Ioctl(VIDIOC_DQBUF, &buffer); |
| 212 | if (result == -1 && errno == EAGAIN) { |
| 213 | return BufferInfo(); |
| 214 | } |
| 215 | PCHECK(result == 0) << ": VIDIOC_DQBUF failed"; |
| 216 | CHECK_LT(buffer.index, buffers_.size()); |
| 217 | |
| 218 | CHECK_EQ(reinterpret_cast<uintptr_t>(buffers_[buffer.index].data_pointer), |
| 219 | planes[0].m.userptr); |
| 220 | |
| 221 | CHECK_EQ(ImageSize(), planes[0].length); |
| 222 | } else { |
| 223 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 224 | const int result = Ioctl(VIDIOC_DQBUF, &buffer); |
| 225 | if (result == -1 && errno == EAGAIN) { |
| 226 | return BufferInfo(); |
| 227 | } |
| 228 | PCHECK(result == 0) << ": VIDIOC_DQBUF failed"; |
| 229 | CHECK_LT(buffer.index, buffers_.size()); |
| 230 | CHECK_EQ(reinterpret_cast<uintptr_t>(buffers_[buffer.index].data_pointer), |
| 231 | buffer.m.userptr); |
| 232 | CHECK_EQ(ImageSize(), buffer.length); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 233 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 234 | CHECK(buffer.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 235 | if (!FLAGS_ignore_timestamps) { |
| 236 | // Require that we have good timestamp on images |
| 237 | CHECK_EQ(buffer.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK, |
| 238 | static_cast<uint32_t>(V4L2_BUF_FLAG_TSTAMP_SRC_EOF)); |
| 239 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 240 | return {static_cast<int>(buffer.index), |
| 241 | aos::time::from_timeval(buffer.timestamp)}; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 244 | void V4L2ReaderBase::EnqueueBuffer(int buffer_number) { |
| 245 | // TODO(austin): Detect multiplanar and do this all automatically. |
| 246 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 247 | CHECK_GE(buffer_number, 0); |
| 248 | CHECK_LT(buffer_number, static_cast<int>(buffers_.size())); |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 249 | CHECK(buffers_[buffer_number].data_pointer != nullptr); |
| 250 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 251 | struct v4l2_buffer buffer; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 252 | struct v4l2_plane planes[1]; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 253 | memset(&buffer, 0, sizeof(buffer)); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 254 | memset(&planes, 0, sizeof(planes)); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 255 | buffer.memory = V4L2_MEMORY_USERPTR; |
| 256 | buffer.index = buffer_number; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 257 | if (multiplanar()) { |
| 258 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 259 | buffer.m.planes = planes; |
| 260 | buffer.length = 1; |
| 261 | planes[0].m.userptr = |
| 262 | reinterpret_cast<uintptr_t>(buffers_[buffer_number].data_pointer); |
| 263 | planes[0].length = ImageSize(); |
| 264 | planes[0].bytesused = planes[0].length; |
| 265 | } else { |
| 266 | buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 267 | buffer.m.userptr = |
| 268 | reinterpret_cast<uintptr_t>(buffers_[buffer_number].data_pointer); |
| 269 | buffer.length = ImageSize(); |
| 270 | } |
| 271 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 272 | PCHECK(Ioctl(VIDIOC_QBUF, &buffer) == 0); |
| 273 | } |
| 274 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 275 | void V4L2ReaderBase::StreamOff() { |
| 276 | int type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 277 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 278 | const int result = Ioctl(VIDIOC_STREAMOFF, &type); |
| 279 | if (result == 0) { |
| 280 | return; |
| 281 | } |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 282 | // Some devices (like Alex's webcam) return this if streaming isn't |
| 283 | // currently on, unlike what the documentations says should happen. |
Brian Silverman | 8f24adb | 2020-02-02 17:15:58 -0800 | [diff] [blame] | 284 | if (errno == EBUSY) { |
| 285 | return; |
| 286 | } |
| 287 | PLOG(FATAL) << "VIDIOC_STREAMOFF failed"; |
| 288 | } |
| 289 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 290 | V4L2Reader::V4L2Reader(aos::EventLoop *event_loop, |
| 291 | const std::string &device_name) |
| 292 | : V4L2ReaderBase(event_loop, device_name) { |
| 293 | // Don't know why this magic call to SetExposure is required (before the |
| 294 | // camera settings are configured) to make things work on boot of the pi, but |
| 295 | // it seems to be-- without it, the image exposure is wrong (too dark). Note-- |
| 296 | // any valid value seems to work-- just choosing 1 for now |
| 297 | |
| 298 | SetExposure(1); |
| 299 | |
| 300 | struct v4l2_format format; |
| 301 | memset(&format, 0, sizeof(format)); |
| 302 | format.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE |
| 303 | : V4L2_BUF_TYPE_VIDEO_CAPTURE; |
milind-u | 6b09409 | 2023-01-09 19:26:12 -0800 | [diff] [blame] | 304 | |
| 305 | constexpr int kWidth = 640; |
| 306 | constexpr int kHeight = 480; |
| 307 | format.fmt.pix.width = kWidth; |
| 308 | format.fmt.pix.height = kHeight; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 309 | format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 310 | // This means we want to capture from a progressive (non-interlaced) |
| 311 | // source. |
| 312 | format.fmt.pix.field = V4L2_FIELD_NONE; |
| 313 | PCHECK(Ioctl(VIDIOC_S_FMT, &format) == 0); |
milind-u | 6b09409 | 2023-01-09 19:26:12 -0800 | [diff] [blame] | 314 | CHECK_EQ(static_cast<int>(format.fmt.pix.width), kWidth); |
| 315 | CHECK_EQ(static_cast<int>(format.fmt.pix.height), kHeight); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 316 | CHECK_EQ(static_cast<int>(format.fmt.pix.bytesperline), |
milind-u | 6b09409 | 2023-01-09 19:26:12 -0800 | [diff] [blame] | 317 | kWidth * 2 /* bytes per pixel */); |
| 318 | CHECK_EQ(format.fmt.pix.sizeimage, ImageSize(kHeight, kWidth)); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 319 | |
| 320 | StreamOn(); |
| 321 | } |
| 322 | |
| 323 | RockchipV4L2Reader::RockchipV4L2Reader(aos::EventLoop *event_loop, |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 324 | aos::internal::EPoll *epoll, |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 325 | const std::string &device_name, |
| 326 | const std::string &image_sensor_subdev) |
| 327 | : V4L2ReaderBase(event_loop, device_name), |
| 328 | epoll_(epoll), |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 329 | image_sensor_fd_(open(image_sensor_subdev.c_str(), O_RDWR | O_NONBLOCK)), |
| 330 | buffer_requeuer_([this](int buffer) { EnqueueBuffer(buffer); }, 20) { |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 331 | PCHECK(image_sensor_fd_.get() != -1) |
| 332 | << " Failed to open device " << device_name; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 333 | StreamOn(); |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 334 | epoll_->OnReadable(fd().get(), [this]() { OnImageReady(); }); |
| 335 | } |
| 336 | |
Ravago Jones | fd8aa20 | 2023-01-16 14:21:45 -0800 | [diff] [blame] | 337 | RockchipV4L2Reader::~RockchipV4L2Reader() { epoll_->DeleteFd(fd().get()); } |
| 338 | |
Ravago Jones | c6b919f | 2023-01-01 21:34:12 -0800 | [diff] [blame^] | 339 | void RockchipV4L2Reader::MarkBufferToBeEnqueued(int buffer) { |
| 340 | ReinitializeBuffer(buffer); |
| 341 | buffer_requeuer_.Push(buffer); |
| 342 | } |
| 343 | |
Ravago Jones | dc52475 | 2022-12-27 01:15:13 -0800 | [diff] [blame] | 344 | void RockchipV4L2Reader::OnImageReady() { |
| 345 | if (!ReadLatestImage()) { |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | SendLatestImage(); |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 352 | int RockchipV4L2Reader::ImageSensorIoctl(unsigned long number, void *arg) { |
| 353 | return ioctl(image_sensor_fd_.get(), number, arg); |
| 354 | } |
| 355 | |
| 356 | void RockchipV4L2Reader::SetExposure(size_t duration) { |
| 357 | v4l2_control exposure_control; |
| 358 | exposure_control.id = V4L2_CID_EXPOSURE; |
| 359 | exposure_control.value = static_cast<int>(duration); |
| 360 | PCHECK(ImageSensorIoctl(VIDIOC_S_CTRL, &exposure_control) == 0); |
| 361 | } |
| 362 | |
| 363 | void RockchipV4L2Reader::SetGain(size_t gain) { |
Ravago Jones | a0a2e06 | 2023-01-03 21:45:18 -0800 | [diff] [blame] | 364 | v4l2_control gain_control; |
| 365 | gain_control.id = V4L2_CID_GAIN; |
| 366 | gain_control.value = static_cast<int>(gain); |
| 367 | PCHECK(ImageSensorIoctl(VIDIOC_S_CTRL, &gain_control) == 0); |
| 368 | } |
| 369 | |
| 370 | void RockchipV4L2Reader::SetGainExt(size_t gain) { |
Ravago Jones | 65469be | 2023-01-13 21:28:23 -0800 | [diff] [blame] | 371 | struct v4l2_ext_controls controls; |
| 372 | memset(&controls, 0, sizeof(controls)); |
| 373 | struct v4l2_ext_control control[1]; |
| 374 | memset(&control, 0, sizeof(control)); |
| 375 | |
| 376 | controls.ctrl_class = V4L2_CTRL_CLASS_IMAGE_SOURCE; |
| 377 | controls.count = 1; |
| 378 | controls.controls = control; |
| 379 | control[0].id = V4L2_CID_ANALOGUE_GAIN; |
| 380 | control[0].value = gain; |
| 381 | |
| 382 | PCHECK(ImageSensorIoctl(VIDIOC_S_EXT_CTRLS, &controls) == 0); |
| 383 | } |
| 384 | |
Ravago Jones | da1b008 | 2023-01-21 15:33:19 -0800 | [diff] [blame] | 385 | void RockchipV4L2Reader::SetVerticalBlanking(size_t vblank) { |
| 386 | struct v4l2_ext_controls controls; |
| 387 | memset(&controls, 0, sizeof(controls)); |
| 388 | struct v4l2_ext_control control[1]; |
| 389 | memset(&control, 0, sizeof(control)); |
Ravago Jones | a0a2e06 | 2023-01-03 21:45:18 -0800 | [diff] [blame] | 390 | |
Ravago Jones | da1b008 | 2023-01-21 15:33:19 -0800 | [diff] [blame] | 391 | controls.ctrl_class = V4L2_CTRL_CLASS_IMAGE_SOURCE; |
| 392 | controls.count = 1; |
| 393 | controls.controls = control; |
| 394 | control[0].id = V4L2_CID_VBLANK; |
| 395 | control[0].value = vblank; |
| 396 | |
| 397 | PCHECK(ImageSensorIoctl(VIDIOC_S_EXT_CTRLS, &controls) == 0); |
Ravago Jones | a0a2e06 | 2023-01-03 21:45:18 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 400 | } // namespace vision |
| 401 | } // namespace frc971 |