blob: 6338ba55e1b4d4a0789e6b801ad2d8678e1587f3 [file] [log] [blame]
Jim Ostrowski977850f2022-01-22 21:04:22 -08001#ifndef FRC971_VISION_V4L2_READER_H_
2#define FRC971_VISION_V4L2_READER_H_
Brian Silverman9dd793b2020-01-31 23:52:21 -08003
4#include <array>
5#include <string>
6
7#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "glog/logging.h"
9
Ravago Jonesc6b919f2023-01-01 21:34:12 -080010#include "aos/containers/ring_buffer.h"
Ravago Jonesdc524752022-12-27 01:15:13 -080011#include "aos/events/epoll.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080012#include "aos/events/event_loop.h"
Austin Schuhe4acc1e2022-12-26 14:01:34 -080013#include "aos/ftrace.h"
Ravago Jonesc6b919f2023-01-01 21:34:12 -080014#include "aos/realtime.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080015#include "aos/scoped/scoped_fd.h"
Ravago Jonesc6b919f2023-01-01 21:34:12 -080016#include "aos/util/threaded_consumer.h"
Jim Ostrowski977850f2022-01-22 21:04:22 -080017#include "frc971/vision/vision_generated.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080018
19namespace frc971 {
20namespace vision {
21
22// Reads images from a V4L2 capture device (aka camera).
Austin Schuh77d0bbd2022-12-26 14:00:51 -080023class V4L2ReaderBase {
Brian Silverman9dd793b2020-01-31 23:52:21 -080024 public:
25 // device_name is the name of the device file (like "/dev/video0").
milind-ufd08c432023-02-05 15:15:21 -080026 // image_channel is the channel to send images on
27 V4L2ReaderBase(aos::EventLoop *event_loop, std::string_view device_name,
28 std::string_view image_channel);
Brian Silverman9dd793b2020-01-31 23:52:21 -080029
Austin Schuh77d0bbd2022-12-26 14:00:51 -080030 V4L2ReaderBase(const V4L2ReaderBase &) = delete;
31 V4L2ReaderBase &operator=(const V4L2ReaderBase &) = delete;
Brian Silverman9dd793b2020-01-31 23:52:21 -080032
33 // Reads the latest image.
34 //
Brian Silverman967e5df2020-02-09 16:43:34 -080035 // Returns false if no image was available since the last image was read.
36 // Call LatestImage() to get a reference to the data, which will be valid
37 // until this method is called again.
38 bool ReadLatestImage();
Brian Silverman9dd793b2020-01-31 23:52:21 -080039
Ravago Jones65469be2023-01-13 21:28:23 -080040 void MaybeEnqueue();
41
Brian Silverman9dd793b2020-01-31 23:52:21 -080042 // Sends the latest image.
43 //
44 // ReadLatestImage() must have returned a non-empty span the last time it was
45 // called. After calling this, the data which was returned from
46 // ReadLatestImage() will no longer be valid.
47 void SendLatestImage();
48
Brian Silverman967e5df2020-02-09 16:43:34 -080049 const CameraImage &LatestImage() {
50 Buffer *const buffer = &buffers_[saved_buffer_.index];
51 return *flatbuffers::GetTemporaryPointer(*buffer->builder.fbb(),
52 buffer->message_offset);
53 }
54
milind-udaebe9b2022-01-09 18:25:24 -080055 // Sets the exposure duration of the camera. duration is the number of 100
56 // microsecond units.
Ravago Jones65469be2023-01-13 21:28:23 -080057 virtual void SetExposure(size_t duration);
milind-udaebe9b2022-01-09 18:25:24 -080058
59 // Switches from manual to auto exposure.
60 void UseAutoExposure();
61
Austin Schuh77d0bbd2022-12-26 14:00:51 -080062 protected:
63 void StreamOff();
64 void StreamOn();
65
Ravago Jonesc6b919f2023-01-01 21:34:12 -080066 // Enqueues a buffer for v4l2 to stream into (expensive).
67 void EnqueueBuffer(int buffer_index);
68
69 // Initializations that need to happen in the main thread.
70 //
71 // Implementations of MarkBufferToBeEnqueued should call this before calling
72 // EnqueueBuffer.
73 void ReinitializeBuffer(int buffer_index) {
74 CHECK_GE(buffer_index, 0);
75 CHECK_LT(buffer_index, static_cast<int>(buffers_.size()));
76 buffers_[buffer_index].InitializeMessage(ImageSize());
77 }
78
79 // Submits a buffer to be enqueued later in a lower priority thread.
80 // Legacy V4L2Reader still does this in the main thread.
81 virtual void MarkBufferToBeEnqueued(int buffer_index);
82
Austin Schuh77d0bbd2022-12-26 14:00:51 -080083 int Ioctl(unsigned long number, void *arg);
84
85 bool multiplanar() const { return multiplanar_; }
86
87 // TODO(Brian): This concept won't exist once we start using variable-size
88 // H.264 frames.
milind-u6b094092023-01-09 19:26:12 -080089 size_t ImageSize() const { return ImageSize(rows_, cols_); }
90 static size_t ImageSize(int rows, int cols) {
91 return rows * cols * 2 /* bytes per pixel */;
92 }
Austin Schuh77d0bbd2022-12-26 14:00:51 -080093
Ravago Jonesdc524752022-12-27 01:15:13 -080094 const aos::ScopedFD &fd() { return fd_; };
95
Austin Schuh77d0bbd2022-12-26 14:00:51 -080096 static constexpr int kNumberBuffers = 4;
Brian Silverman9dd793b2020-01-31 23:52:21 -080097
Ravago Jonesc6b919f2023-01-01 21:34:12 -080098 private:
Brian Silverman9dd793b2020-01-31 23:52:21 -080099 struct Buffer {
Brian Silverman967e5df2020-02-09 16:43:34 -0800100 void InitializeMessage(size_t max_image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -0800101
Brian Silverman967e5df2020-02-09 16:43:34 -0800102 void PrepareMessage(int rows, int cols, size_t image_size,
103 aos::monotonic_clock::time_point monotonic_eof);
104
105 void Send() {
Ravago Jonesda1b0082023-01-21 15:33:19 -0800106 builder.CheckOk(builder.Send(message_offset));
Brian Silverman967e5df2020-02-09 16:43:34 -0800107 message_offset = flatbuffers::Offset<CameraImage>();
Brian Silverman9dd793b2020-01-31 23:52:21 -0800108 }
109
110 absl::Span<const char> DataSpan(size_t image_size) {
Brian Silverman967e5df2020-02-09 16:43:34 -0800111 return absl::Span<const char>(
112 reinterpret_cast<char *>(CHECK_NOTNULL(data_pointer)), image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -0800113 }
114
115 aos::Sender<CameraImage> sender;
116 aos::Sender<CameraImage>::Builder builder;
Brian Silverman967e5df2020-02-09 16:43:34 -0800117 flatbuffers::Offset<CameraImage> message_offset;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800118
119 uint8_t *data_pointer = nullptr;
milind-ufd08c432023-02-05 15:15:21 -0800120
121 std::string_view image_channel_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800122 };
123
Brian Silverman967e5df2020-02-09 16:43:34 -0800124 struct BufferInfo {
125 int index = -1;
126 aos::monotonic_clock::time_point monotonic_eof =
127 aos::monotonic_clock::min_time;
128
129 explicit operator bool() const { return index != -1; }
130
131 void Clear() {
132 index = -1;
133 monotonic_eof = aos::monotonic_clock::min_time;
134 }
135 };
136
Brian Silverman9dd793b2020-01-31 23:52:21 -0800137 // Attempts to dequeue a buffer (nonblocking). Returns the index of the new
Brian Silverman967e5df2020-02-09 16:43:34 -0800138 // buffer, or BufferInfo() if there wasn't a frame to dequeue.
139 BufferInfo DequeueBuffer();
Brian Silverman9dd793b2020-01-31 23:52:21 -0800140
Brian Silverman9dd793b2020-01-31 23:52:21 -0800141 // The mmaped V4L2 buffers.
142 std::array<Buffer, kNumberBuffers> buffers_;
143
144 // If this is non-negative, it's the buffer number we're currently holding
145 // onto.
Brian Silverman967e5df2020-02-09 16:43:34 -0800146 BufferInfo saved_buffer_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800147
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800148 bool multiplanar_ = false;
149
150 int rows_ = 0;
151 int cols_ = 0;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800152
153 aos::ScopedFD fd_;
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800154
155 aos::EventLoop *event_loop_;
156 aos::Ftrace ftrace_;
milind-ufd08c432023-02-05 15:15:21 -0800157
158 std::string_view image_channel_;
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800159};
160
161// Generic V4L2 reader for pi's and older.
162class V4L2Reader : public V4L2ReaderBase {
163 public:
milind-ufd08c432023-02-05 15:15:21 -0800164 V4L2Reader(aos::EventLoop *event_loop, std::string_view device_name,
165 std::string_view image_channel = "/camera");
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800166};
167
168// Rockpi specific v4l2 reader. This assumes that the media device has been
169// properly configured before this class is constructed.
170class RockchipV4L2Reader : public V4L2ReaderBase {
171 public:
Ravago Jonesdc524752022-12-27 01:15:13 -0800172 RockchipV4L2Reader(aos::EventLoop *event_loop, aos::internal::EPoll *epoll,
milind-ufd08c432023-02-05 15:15:21 -0800173 std::string_view device_name,
174 std::string_view image_sensor_subdev,
175 std::string_view image_channel = "/camera");
Ravago Jones65469be2023-01-13 21:28:23 -0800176
milind-ufd08c432023-02-05 15:15:21 -0800177 virtual ~RockchipV4L2Reader();
Ravago Jonesfd8aa202023-01-16 14:21:45 -0800178
Ravago Jones65469be2023-01-13 21:28:23 -0800179 void SetExposure(size_t duration) override;
180
181 void SetGain(size_t gain);
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800182 void SetGainExt(size_t gain);
183
Ravago Jonesda1b0082023-01-21 15:33:19 -0800184 void SetVerticalBlanking(size_t vblank);
Ravago Jonesdc524752022-12-27 01:15:13 -0800185
186 private:
187 void OnImageReady();
188
Ravago Jonesc6b919f2023-01-01 21:34:12 -0800189 void MarkBufferToBeEnqueued(int buffer) override;
190
Ravago Jones65469be2023-01-13 21:28:23 -0800191 int ImageSensorIoctl(unsigned long number, void *arg);
192
Ravago Jonesdc524752022-12-27 01:15:13 -0800193 aos::internal::EPoll *epoll_;
Ravago Jones65469be2023-01-13 21:28:23 -0800194
195 aos::ScopedFD image_sensor_fd_;
Ravago Jonesc6b919f2023-01-01 21:34:12 -0800196
Austin Schuh2cd5fe82023-02-04 16:21:19 -0800197 static constexpr int kEnqueueFifoPriority = 1;
198
Ravago Jonesc6b919f2023-01-01 21:34:12 -0800199 aos::util::ThreadedConsumer<int, kNumberBuffers> buffer_requeuer_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800200};
201
202} // namespace vision
203} // namespace frc971
204
Jim Ostrowski977850f2022-01-22 21:04:22 -0800205#endif // FRC971_VISION_V4L2_READER_H_