blob: 334ad813410e50ec23c35219425be70b64163efb [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.
64 size_t ImageSize() const { return rows_ * cols_ * 2 /* bytes per pixel */; }
65
Ravago Jonesdc524752022-12-27 01:15:13 -080066 const aos::ScopedFD &fd() { return fd_; };
67
Brian Silverman9dd793b2020-01-31 23:52:21 -080068 private:
Austin Schuh77d0bbd2022-12-26 14:00:51 -080069 static constexpr int kNumberBuffers = 4;
Brian Silverman9dd793b2020-01-31 23:52:21 -080070
71 struct Buffer {
Brian Silverman967e5df2020-02-09 16:43:34 -080072 void InitializeMessage(size_t max_image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -080073
Brian Silverman967e5df2020-02-09 16:43:34 -080074 void PrepareMessage(int rows, int cols, size_t image_size,
75 aos::monotonic_clock::time_point monotonic_eof);
76
77 void Send() {
milind1f1dca32021-07-03 13:50:07 -070078 (void)builder.Send(message_offset);
Brian Silverman967e5df2020-02-09 16:43:34 -080079 message_offset = flatbuffers::Offset<CameraImage>();
Brian Silverman9dd793b2020-01-31 23:52:21 -080080 }
81
82 absl::Span<const char> DataSpan(size_t image_size) {
Brian Silverman967e5df2020-02-09 16:43:34 -080083 return absl::Span<const char>(
84 reinterpret_cast<char *>(CHECK_NOTNULL(data_pointer)), image_size);
Brian Silverman9dd793b2020-01-31 23:52:21 -080085 }
86
87 aos::Sender<CameraImage> sender;
88 aos::Sender<CameraImage>::Builder builder;
Brian Silverman967e5df2020-02-09 16:43:34 -080089 flatbuffers::Offset<CameraImage> message_offset;
Brian Silverman9dd793b2020-01-31 23:52:21 -080090
91 uint8_t *data_pointer = nullptr;
92 };
93
Brian Silverman967e5df2020-02-09 16:43:34 -080094 struct BufferInfo {
95 int index = -1;
96 aos::monotonic_clock::time_point monotonic_eof =
97 aos::monotonic_clock::min_time;
98
99 explicit operator bool() const { return index != -1; }
100
101 void Clear() {
102 index = -1;
103 monotonic_eof = aos::monotonic_clock::min_time;
104 }
105 };
106
Brian Silverman9dd793b2020-01-31 23:52:21 -0800107 // Attempts to dequeue a buffer (nonblocking). Returns the index of the new
Brian Silverman967e5df2020-02-09 16:43:34 -0800108 // buffer, or BufferInfo() if there wasn't a frame to dequeue.
109 BufferInfo DequeueBuffer();
Brian Silverman9dd793b2020-01-31 23:52:21 -0800110
111 void EnqueueBuffer(int buffer);
112
Brian Silverman9dd793b2020-01-31 23:52:21 -0800113 // The mmaped V4L2 buffers.
114 std::array<Buffer, kNumberBuffers> buffers_;
115
116 // If this is non-negative, it's the buffer number we're currently holding
117 // onto.
Brian Silverman967e5df2020-02-09 16:43:34 -0800118 BufferInfo saved_buffer_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800119
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800120 bool multiplanar_ = false;
121
122 int rows_ = 0;
123 int cols_ = 0;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800124
125 aos::ScopedFD fd_;
Austin Schuh77d0bbd2022-12-26 14:00:51 -0800126
127 aos::EventLoop *event_loop_;
128 aos::Ftrace ftrace_;
129};
130
131// Generic V4L2 reader for pi's and older.
132class V4L2Reader : public V4L2ReaderBase {
133 public:
134 V4L2Reader(aos::EventLoop *event_loop, const std::string &device_name);
135
136 private:
137 const int rows_ = 480;
138 const int cols_ = 640;
139};
140
141// Rockpi specific v4l2 reader. This assumes that the media device has been
142// properly configured before this class is constructed.
143class RockchipV4L2Reader : public V4L2ReaderBase {
144 public:
Ravago Jonesdc524752022-12-27 01:15:13 -0800145 RockchipV4L2Reader(aos::EventLoop *event_loop, aos::internal::EPoll *epoll,
146 const std::string &device_name);
147
148 private:
149 void OnImageReady();
150
151 aos::internal::EPoll *epoll_;
Brian Silverman9dd793b2020-01-31 23:52:21 -0800152};
153
154} // namespace vision
155} // namespace frc971
156
Jim Ostrowski977850f2022-01-22 21:04:22 -0800157#endif // FRC971_VISION_V4L2_READER_H_