blob: 25fc0bdddddbc22ff45aaf3267cf00fea0a438cf [file] [log] [blame]
Parker Schuh44f86922017-01-03 23:59:50 -08001#ifndef AOS_VISION_IMAGE_READER_H_
2#define AOS_VISION_IMAGE_READER_H_
Parker Schuh44f86922017-01-03 23:59:50 -08003
4#include <inttypes.h>
5#include <functional>
6#include <string>
7
John Park33858a32018-09-28 23:05:48 -07008#include "aos/time/time.h"
Parker Schuh44f86922017-01-03 23:59:50 -08009#include "aos/vision/image/V4L2.h"
Parker Schuh24ee58d2017-03-11 16:13:23 -080010#include "aos/vision/image/camera_params.pb.h"
Parker Schuh44f86922017-01-03 23:59:50 -080011#include "aos/vision/image/image_types.h"
12
13namespace camera {
14
Parker Schuh24ee58d2017-03-11 16:13:23 -080015aos::vision::CameraParams MakeCameraParams(int32_t width, int32_t height,
16 int32_t exposure, int32_t brightness,
17 int32_t gain, int32_t fps);
Parker Schuh44f86922017-01-03 23:59:50 -080018
19class Reader {
20 public:
21 using ProcessCb = std::function<void(
22 aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>;
Parker Schuh24ee58d2017-03-11 16:13:23 -080023 Reader(const std::string &dev_name, ProcessCb process,
24 aos::vision::CameraParams params);
Parker Schuh44f86922017-01-03 23:59:50 -080025
26 aos::vision::ImageFormat get_format();
27
28 void HandleFrame();
29 void StartAsync() {
Parker Schuh44f86922017-01-03 23:59:50 -080030 MMapBuffers();
Parker Schuh309dd722017-02-25 11:31:18 -080031 Start();
Parker Schuh44f86922017-01-03 23:59:50 -080032 }
33 int fd() { return fd_; }
34
Alex Perry5b1e8e32019-04-07 13:25:31 -070035 bool SetCameraControl(uint32_t id, const char *name, int value);
36
Parker Schuh44f86922017-01-03 23:59:50 -080037 private:
38 void QueueBuffer(v4l2_buffer *buf);
39 void InitMMap();
Parker Schuh44f86922017-01-03 23:59:50 -080040 void Init();
41 void Start();
42 void MMapBuffers();
43 // File descriptor of the camera
44 int fd_;
45 // Name of the camera device.
46 std::string dev_name_;
47
48 ProcessCb process_;
49
Parker Schuh309dd722017-02-25 11:31:18 -080050 int tick_id_ = 0;
Parker Schuh44f86922017-01-03 23:59:50 -080051 // The number of buffers currently queued in v4l2.
52 uint32_t queued_;
53 struct Buffer;
54 // TODO(parker): This should be a smart pointer, but it cannot
55 // because the buffers are not ummapped.
56 Buffer *buffers_;
57
Parker Schuh309dd722017-02-25 11:31:18 -080058 // TODO(parker): The timestamps should be queue insertion timestamps
59 // which will remove the impact of kNumBuffers.
60 // TODO(parker): Flush the queue (or tweak the FPS) if we fall behind.
61 static const unsigned int kNumBuffers = 5;
Parker Schuh44f86922017-01-03 23:59:50 -080062
63 // set only at initialize
Parker Schuh24ee58d2017-03-11 16:13:23 -080064 aos::vision::CameraParams params_;
Parker Schuh44f86922017-01-03 23:59:50 -080065};
66
67} // namespace camera
68
69#endif // AOS_VISION_IMAGE_READER_H_