blob: 85ca475916c32b6c35bf3104e3f754d0b1359c89 [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"
Ravago Jonesdc524752022-12-27 01:15:13 -08008#include "aos/events/epoll.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -08009#include "aos/events/event_loop.h"
Austin Schuhe4acc1e2022-12-26 14:01:34 -080010#include "aos/ftrace.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080011#include "aos/scoped/scoped_fd.h"
Jim Ostrowski977850f2022-01-22 21:04:22 -080012#include "frc971/vision/vision_generated.h"
Austin Schuh77d0bbd2022-12-26 14:00:51 -080013#include "glog/logging.h"
Brian Silverman9dd793b2020-01-31 23:52:21 -080014
15namespace frc971 {
16namespace vision {
17
18// Reads images from a V4L2 capture device (aka camera).
Austin Schuh77d0bbd2022-12-26 14:00:51 -080019class V4L2ReaderBase {
Brian Silverman9dd793b2020-01-31 23:52:21 -080020 public:
21 // device_name is the name of the device file (like "/dev/video0").
Austin Schuh77d0bbd2022-12-26 14:00:51 -080022 V4L2ReaderBase(aos::EventLoop *event_loop, const std::string &device_name);
Brian Silverman9dd793b2020-01-31 23:52:21 -080023
Austin Schuh77d0bbd2022-12-26 14:00:51 -080024 V4L2ReaderBase(const V4L2ReaderBase &) = delete;
25 V4L2ReaderBase &operator=(const V4L2ReaderBase &) = delete;
Brian Silverman9dd793b2020-01-31 23:52:21 -080026
27 // Reads the latest image.
28 //
Brian Silverman967e5df2020-02-09 16:43:34 -080029 // Returns false if no image was available since the last image was read.
30 // Call LatestImage() to get a reference to the data, which will be valid
31 // until this method is called again.
32 bool ReadLatestImage();
Brian Silverman9dd793b2020-01-31 23:52:21 -080033
34 // Sends the latest image.
35 //
36 // ReadLatestImage() must have returned a non-empty span the last time it was
37 // called. After calling this, the data which was returned from
38 // ReadLatestImage() will no longer be valid.
39 void SendLatestImage();
40
Brian Silverman967e5df2020-02-09 16:43:34 -080041 const CameraImage &LatestImage() {
42 Buffer *const buffer = &buffers_[saved_buffer_.index];
43 return *flatbuffers::GetTemporaryPointer(*buffer->builder.fbb(),
44 buffer->message_offset);
45 }
46
milind-udaebe9b2022-01-09 18:25:24 -080047 // Sets the exposure duration of the camera. duration is the number of 100
48 // microsecond units.
49 void SetExposure(size_t duration);
50
51 // Switches from manual to auto exposure.
52 void UseAutoExposure();
53
Austin Schuh77d0bbd2022-12-26 14:00:51 -080054 protected:
55 void StreamOff();
56 void StreamOn();
57
58 int Ioctl(unsigned long number, void *arg);
59
60 bool multiplanar() const { return multiplanar_; }
61
62 // TODO(Brian): This concept won't exist once we start using variable-size
63 // H.264 frames.
milind-u6b094092023-01-09 19:26:12 -080064 size_t ImageSize() const { return ImageSize(rows_, cols_); }
65 static size_t ImageSize(int rows, int cols) {
66 return rows * cols * 2 /* bytes per pixel */;
67 }
Austin Schuh77d0bbd2022-12-26 14:00:51 -080068
Ravago Jonesdc524752022-12-27 01:15:13 -080069 const aos::ScopedFD &fd() { return fd_; };
70
Brian Silverman9dd793b2020-01-31 23:52:21 -080071 private:
Austin Schuh77d0bbd2022-12-26 14:00:51 -080072 static constexpr int kNumberBuffers = 4;
Brian Silverman9dd793b2020-01-31 23:52:21 -080073
74 struct Buffer {
Brian Silverman967e5df2020-02-09 16:43:34 -080075 void InitializeMessage(size_t max_image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -080076
Brian Silverman967e5df2020-02-09 16:43:34 -080077 void PrepareMessage(int rows, int cols, size_t image_size,
78 aos::monotonic_clock::time_point monotonic_eof);
79
80 void Send() {
milind1f1dca32021-07-03 13:50:07 -070081 (void)builder.Send(message_offset);
Brian Silverman967e5df2020-02-09 16:43:34 -080082 message_offset = flatbuffers::Offset<CameraImage>();
Brian Silverman9dd793b2020-01-31 23:52:21 -080083 }
84
85 absl::Span<const char> DataSpan(size_t image_size) {
Brian Silverman967e5df2020-02-09 16:43:34 -080086 return absl::Span<const char>(
87 reinterpret_cast<char *>(CHECK_NOTNULL(data_pointer)), image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -080088 }
89
90 aos::Sender<CameraImage> sender;
91 aos::Sender<CameraImage>::Builder builder;
Brian Silverman967e5df2020-02-09 16:43:34 -080092 flatbuffers::Offset<CameraImage> message_offset;
Brian Silverman9dd793b2020-01-31 23:52:21 -080093
94 uint8_t *data_pointer = nullptr;
95 };
96
Brian Silverman967e5df2020-02-09 16:43:34 -080097 struct BufferInfo {
98 int index = -1;
99 aos::monotonic_clock::time_point monotonic_eof =
100 aos::monotonic_clock::min_time;
101
102 explicit operator bool() const { return index != -1; }
103
104 void Clear() {
105 index = -1;
106 monotonic_eof = aos::monotonic_clock::min_time;
107 }
108 };
109
Brian Silverman9dd793b2020-01-31 23:52:21 -0800110 // Attempts to dequeue a buffer (nonblocking). Returns the index of the new
Brian Silverman967e5df2020-02-09 16:43:34 -0800111 // buffer, or BufferInfo() if there wasn't a frame to dequeue.
112 BufferInfo DequeueBuffer();
Brian Silverman9dd793b2020-01-31 23:52:21 -0800113
114 void EnqueueBuffer(int buffer);
115
Brian Silverman9dd793b2020-01-31 23:52:21 -0800116 // The mmaped V4L2 buffers.
117 std::array<Buffer, kNumberBuffers> buffers_;
118
119 // If this is non-negative, it's the buffer number we're currently holding
120 // onto.
Brian Silverman967e5df2020-02-09 16:43:34 -0800121 BufferInfo saved_buffer_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800122
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800123 bool multiplanar_ = false;
124
125 int rows_ = 0;
126 int cols_ = 0;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800127
128 aos::ScopedFD fd_;
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800129
130 aos::EventLoop *event_loop_;
131 aos::Ftrace ftrace_;
132};
133
134// Generic V4L2 reader for pi's and older.
135class V4L2Reader : public V4L2ReaderBase {
136 public:
137 V4L2Reader(aos::EventLoop *event_loop, const std::string &device_name);
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800138};
139
140// Rockpi specific v4l2 reader. This assumes that the media device has been
141// properly configured before this class is constructed.
142class RockchipV4L2Reader : public V4L2ReaderBase {
143 public:
Ravago Jonesdc524752022-12-27 01:15:13 -0800144 RockchipV4L2Reader(aos::EventLoop *event_loop, aos::internal::EPoll *epoll,
145 const std::string &device_name);
146
147 private:
148 void OnImageReady();
149
150 aos::internal::EPoll *epoll_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800151};
152
153} // namespace vision
154} // namespace frc971
155
Jim Ostrowski977850f2022-01-22 21:04:22 -0800156#endif // FRC971_VISION_V4L2_READER_H_