blob: 28735fb476245426fa60a13ae01bcfe70caa8876 [file] [log] [blame]
Parker Schuh44f86922017-01-03 23:59:50 -08001#ifndef AOS_VISION_IMAGE_READER_H_
2#define AOS_VISION_IMAGE_READER_H_
3#include <errno.h>
4#include <fcntl.h>
5#include <malloc.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <sys/mman.h>
10#include <sys/stat.h>
11#include <sys/time.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15#include <inttypes.h>
16#include <functional>
17#include <string>
18
19#include "aos/common/time.h"
20#include "aos/vision/image/V4L2.h"
21#include "aos/vision/image/image_types.h"
22
23namespace camera {
24
25struct CameraParams {
26 int32_t width;
27 int32_t height;
28 int32_t exposure;
29 int32_t brightness;
30 int32_t gain;
31 int32_t fps;
32};
33
34class Reader {
35 public:
36 using ProcessCb = std::function<void(
37 aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>;
38 Reader(const std::string &dev_name, ProcessCb process, CameraParams params);
39
40 aos::vision::ImageFormat get_format();
41
42 void HandleFrame();
43 void StartAsync() {
Parker Schuh44f86922017-01-03 23:59:50 -080044 MMapBuffers();
Parker Schuh309dd722017-02-25 11:31:18 -080045 Start();
Parker Schuh44f86922017-01-03 23:59:50 -080046 }
47 int fd() { return fd_; }
48
49 private:
50 void QueueBuffer(v4l2_buffer *buf);
51 void InitMMap();
52 bool SetCameraControl(uint32_t id, const char *name, int value);
53 void Init();
54 void Start();
55 void MMapBuffers();
56 // File descriptor of the camera
57 int fd_;
58 // Name of the camera device.
59 std::string dev_name_;
60
61 ProcessCb process_;
62
Parker Schuh309dd722017-02-25 11:31:18 -080063 int tick_id_ = 0;
Parker Schuh44f86922017-01-03 23:59:50 -080064 // The number of buffers currently queued in v4l2.
65 uint32_t queued_;
66 struct Buffer;
67 // TODO(parker): This should be a smart pointer, but it cannot
68 // because the buffers are not ummapped.
69 Buffer *buffers_;
70
Parker Schuh309dd722017-02-25 11:31:18 -080071 // TODO(parker): The timestamps should be queue insertion timestamps
72 // which will remove the impact of kNumBuffers.
73 // TODO(parker): Flush the queue (or tweak the FPS) if we fall behind.
74 static const unsigned int kNumBuffers = 5;
Parker Schuh44f86922017-01-03 23:59:50 -080075
76 // set only at initialize
77 CameraParams params_;
78};
79
80} // namespace camera
81
82#endif // AOS_VISION_IMAGE_READER_H_