Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 1 | #ifndef AOS_VISION_IMAGE_READER_H_ |
| 2 | #define AOS_VISION_IMAGE_READER_H_ |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 3 | |
| 4 | #include <inttypes.h> |
| 5 | #include <functional> |
| 6 | #include <string> |
| 7 | |
| 8 | #include "aos/common/time.h" |
| 9 | #include "aos/vision/image/V4L2.h" |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame^] | 10 | #include "aos/vision/image/camera_params.pb.h" |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 11 | #include "aos/vision/image/image_types.h" |
| 12 | |
| 13 | namespace camera { |
| 14 | |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame^] | 15 | aos::vision::CameraParams MakeCameraParams(int32_t width, int32_t height, |
| 16 | int32_t exposure, int32_t brightness, |
| 17 | int32_t gain, int32_t fps); |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 18 | |
| 19 | class Reader { |
| 20 | public: |
| 21 | using ProcessCb = std::function<void( |
| 22 | aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>; |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame^] | 23 | Reader(const std::string &dev_name, ProcessCb process, |
| 24 | aos::vision::CameraParams params); |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 25 | |
| 26 | aos::vision::ImageFormat get_format(); |
| 27 | |
| 28 | void HandleFrame(); |
| 29 | void StartAsync() { |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 30 | MMapBuffers(); |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 31 | Start(); |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 32 | } |
| 33 | int fd() { return fd_; } |
| 34 | |
| 35 | private: |
| 36 | void QueueBuffer(v4l2_buffer *buf); |
| 37 | void InitMMap(); |
| 38 | bool SetCameraControl(uint32_t id, const char *name, int value); |
| 39 | void Init(); |
| 40 | void Start(); |
| 41 | void MMapBuffers(); |
| 42 | // File descriptor of the camera |
| 43 | int fd_; |
| 44 | // Name of the camera device. |
| 45 | std::string dev_name_; |
| 46 | |
| 47 | ProcessCb process_; |
| 48 | |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 49 | int tick_id_ = 0; |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 50 | // The number of buffers currently queued in v4l2. |
| 51 | uint32_t queued_; |
| 52 | struct Buffer; |
| 53 | // TODO(parker): This should be a smart pointer, but it cannot |
| 54 | // because the buffers are not ummapped. |
| 55 | Buffer *buffers_; |
| 56 | |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 57 | // TODO(parker): The timestamps should be queue insertion timestamps |
| 58 | // which will remove the impact of kNumBuffers. |
| 59 | // TODO(parker): Flush the queue (or tweak the FPS) if we fall behind. |
| 60 | static const unsigned int kNumBuffers = 5; |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 61 | |
| 62 | // set only at initialize |
Parker Schuh | 24ee58d | 2017-03-11 16:13:23 -0800 | [diff] [blame^] | 63 | aos::vision::CameraParams params_; |
Parker Schuh | 44f8692 | 2017-01-03 23:59:50 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace camera |
| 67 | |
| 68 | #endif // AOS_VISION_IMAGE_READER_H_ |