Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 1 | #ifndef FRC971_VISION_V4L2_READER_H_ |
| 2 | #define FRC971_VISION_V4L2_READER_H_ |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 3 | |
| 4 | #include <array> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "absl/types/span.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 8 | #include "aos/events/event_loop.h" |
| 9 | #include "aos/scoped/scoped_fd.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 10 | #include "frc971/vision/vision_generated.h" |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 11 | #include "glog/logging.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 12 | |
| 13 | namespace frc971 { |
| 14 | namespace vision { |
| 15 | |
| 16 | // Reads images from a V4L2 capture device (aka camera). |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 17 | class V4L2ReaderBase { |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 18 | public: |
| 19 | // device_name is the name of the device file (like "/dev/video0"). |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 20 | V4L2ReaderBase(aos::EventLoop *event_loop, const std::string &device_name); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 21 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 22 | V4L2ReaderBase(const V4L2ReaderBase &) = delete; |
| 23 | V4L2ReaderBase &operator=(const V4L2ReaderBase &) = delete; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 24 | |
| 25 | // Reads the latest image. |
| 26 | // |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 27 | // Returns false if no image was available since the last image was read. |
| 28 | // Call LatestImage() to get a reference to the data, which will be valid |
| 29 | // until this method is called again. |
| 30 | bool ReadLatestImage(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 31 | |
| 32 | // Sends the latest image. |
| 33 | // |
| 34 | // ReadLatestImage() must have returned a non-empty span the last time it was |
| 35 | // called. After calling this, the data which was returned from |
| 36 | // ReadLatestImage() will no longer be valid. |
| 37 | void SendLatestImage(); |
| 38 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 39 | const CameraImage &LatestImage() { |
| 40 | Buffer *const buffer = &buffers_[saved_buffer_.index]; |
| 41 | return *flatbuffers::GetTemporaryPointer(*buffer->builder.fbb(), |
| 42 | buffer->message_offset); |
| 43 | } |
| 44 | |
milind-u | daebe9b | 2022-01-09 18:25:24 -0800 | [diff] [blame] | 45 | // Sets the exposure duration of the camera. duration is the number of 100 |
| 46 | // microsecond units. |
| 47 | void SetExposure(size_t duration); |
| 48 | |
| 49 | // Switches from manual to auto exposure. |
| 50 | void UseAutoExposure(); |
| 51 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 52 | protected: |
| 53 | void StreamOff(); |
| 54 | void StreamOn(); |
| 55 | |
| 56 | int Ioctl(unsigned long number, void *arg); |
| 57 | |
| 58 | bool multiplanar() const { return multiplanar_; } |
| 59 | |
| 60 | // TODO(Brian): This concept won't exist once we start using variable-size |
| 61 | // H.264 frames. |
| 62 | size_t ImageSize() const { return rows_ * cols_ * 2 /* bytes per pixel */; } |
| 63 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 64 | private: |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 65 | static constexpr int kNumberBuffers = 4; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 66 | |
| 67 | struct Buffer { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 68 | void InitializeMessage(size_t max_image_size); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 69 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 70 | void PrepareMessage(int rows, int cols, size_t image_size, |
| 71 | aos::monotonic_clock::time_point monotonic_eof); |
| 72 | |
| 73 | void Send() { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 74 | (void)builder.Send(message_offset); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 75 | message_offset = flatbuffers::Offset<CameraImage>(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | absl::Span<const char> DataSpan(size_t image_size) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 79 | return absl::Span<const char>( |
| 80 | reinterpret_cast<char *>(CHECK_NOTNULL(data_pointer)), image_size); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | aos::Sender<CameraImage> sender; |
| 84 | aos::Sender<CameraImage>::Builder builder; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 85 | flatbuffers::Offset<CameraImage> message_offset; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 86 | |
| 87 | uint8_t *data_pointer = nullptr; |
| 88 | }; |
| 89 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 90 | struct BufferInfo { |
| 91 | int index = -1; |
| 92 | aos::monotonic_clock::time_point monotonic_eof = |
| 93 | aos::monotonic_clock::min_time; |
| 94 | |
| 95 | explicit operator bool() const { return index != -1; } |
| 96 | |
| 97 | void Clear() { |
| 98 | index = -1; |
| 99 | monotonic_eof = aos::monotonic_clock::min_time; |
| 100 | } |
| 101 | }; |
| 102 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 103 | // Attempts to dequeue a buffer (nonblocking). Returns the index of the new |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 104 | // buffer, or BufferInfo() if there wasn't a frame to dequeue. |
| 105 | BufferInfo DequeueBuffer(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 106 | |
| 107 | void EnqueueBuffer(int buffer); |
| 108 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 109 | // The mmaped V4L2 buffers. |
| 110 | std::array<Buffer, kNumberBuffers> buffers_; |
| 111 | |
| 112 | // If this is non-negative, it's the buffer number we're currently holding |
| 113 | // onto. |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 114 | BufferInfo saved_buffer_; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 115 | |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 116 | bool multiplanar_ = false; |
| 117 | |
| 118 | int rows_ = 0; |
| 119 | int cols_ = 0; |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 120 | |
| 121 | aos::ScopedFD fd_; |
Austin Schuh | 77d0bbd | 2022-12-26 14:00:51 -0800 | [diff] [blame^] | 122 | |
| 123 | aos::EventLoop *event_loop_; |
| 124 | aos::Ftrace ftrace_; |
| 125 | }; |
| 126 | |
| 127 | // Generic V4L2 reader for pi's and older. |
| 128 | class V4L2Reader : public V4L2ReaderBase { |
| 129 | public: |
| 130 | V4L2Reader(aos::EventLoop *event_loop, const std::string &device_name); |
| 131 | |
| 132 | private: |
| 133 | const int rows_ = 480; |
| 134 | const int cols_ = 640; |
| 135 | }; |
| 136 | |
| 137 | // Rockpi specific v4l2 reader. This assumes that the media device has been |
| 138 | // properly configured before this class is constructed. |
| 139 | class RockchipV4L2Reader : public V4L2ReaderBase { |
| 140 | public: |
| 141 | RockchipV4L2Reader(aos::EventLoop *event_loop, const std::string &device_name); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | } // namespace vision |
| 145 | } // namespace frc971 |
| 146 | |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 147 | #endif // FRC971_VISION_V4L2_READER_H_ |