Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 3 | #include <cassert> |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 4 | #include <ast_video_types.hpp> |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 5 | #include <iostream> |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 6 | #include <mutex> |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 7 | #include <vector> |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 8 | #include <boost/asio.hpp> |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 9 | |
| 10 | namespace AstVideo { |
Ed Tanous | d5f3999 | 2017-04-18 13:41:22 -0700 | [diff] [blame] | 11 | |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 12 | // |
| 13 | // Cursor struct is used in User Mode |
| 14 | // |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 15 | struct AST_CUR_ATTRIBUTION_TAG { |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 16 | unsigned int posX; |
| 17 | unsigned int posY; |
| 18 | unsigned int cur_width; |
| 19 | unsigned int cur_height; |
| 20 | unsigned int cur_type; // 0:mono 1:color 2:disappear cursor |
| 21 | unsigned int cur_change_flag; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 22 | }; |
Ed Tanous | d5f3999 | 2017-04-18 13:41:22 -0700 | [diff] [blame] | 23 | |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 24 | // |
| 25 | // For storing Cursor Information |
| 26 | // |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 27 | struct AST_CURSOR_TAG { |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 28 | AST_CUR_ATTRIBUTION_TAG attr; |
| 29 | // unsigned char icon[MAX_CUR_OFFSETX*MAX_CUR_OFFSETY*2]; |
| 30 | unsigned char *icon; //[64*64*2]; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 31 | }; |
Ed Tanous | d5f3999 | 2017-04-18 13:41:22 -0700 | [diff] [blame] | 32 | |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 33 | // |
| 34 | // For select image format, i.e. 422 JPG420, 444 JPG444, lumin/chrom table, 0 |
| 35 | // ~ 11, low to high |
| 36 | // |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 37 | struct FEATURES_TAG { |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 38 | short jpg_fmt; // 422:JPG420, 444:JPG444 |
| 39 | short lumin_tbl; |
| 40 | short chrom_tbl; |
| 41 | short tolerance_noise; |
| 42 | int w; |
| 43 | int h; |
| 44 | unsigned char *buf; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 45 | }; |
Ed Tanous | d5f3999 | 2017-04-18 13:41:22 -0700 | [diff] [blame] | 46 | |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 47 | // |
| 48 | // For configure video engine control registers |
| 49 | // |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 50 | struct IMAGE_INFO { |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 51 | short do_image_refresh; // Action 0:motion 1:fullframe 2:quick cursor |
| 52 | char qc_valid; // quick cursor enable/disable |
| 53 | unsigned int len; |
| 54 | int crypttype; |
| 55 | char cryptkey[16]; |
| 56 | union { |
| 57 | FEATURES_TAG features; |
| 58 | AST_CURSOR_TAG cursor_info; |
| 59 | } parameter; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 60 | }; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 61 | |
| 62 | class SimpleVideoPuller { |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 63 | public: |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 64 | SimpleVideoPuller() : image_info(){}; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 65 | |
| 66 | void initialize() { |
| 67 | std::cout << "Opening /dev/video\n"; |
| 68 | video_fd = open("/dev/video", O_RDWR); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 69 | if (video_fd == 0) { |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 70 | std::cout << "Failed to open /dev/video\n"; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 71 | throw std::runtime_error("Failed to open /dev/video"); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 72 | } |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 73 | std::cout << "Opened successfully\n"; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | RawVideoBuffer read_video() { |
| 77 | assert(video_fd != 0); |
| 78 | RawVideoBuffer raw; |
| 79 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 80 | image_info.do_image_refresh = 1; // full frame refresh |
| 81 | image_info.qc_valid = 0; // quick cursor disabled |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 82 | image_info.parameter.features.buf = |
| 83 | reinterpret_cast<unsigned char *>(raw.buffer.data()); |
| 84 | image_info.crypttype = -1; |
| 85 | std::cout << "Writing\n"; |
| 86 | |
| 87 | int status; |
| 88 | /* |
| 89 | status = write(video_fd, reinterpret_cast<char*>(&image_info), |
| 90 | sizeof(image_info)); |
| 91 | if (status != sizeof(image_info)) { |
| 92 | std::cout << "Write failed. Return: " << status << "\n"; |
| 93 | perror("perror output:"); |
| 94 | } |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 95 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 96 | std::cout << "Write done\n"; |
| 97 | */ |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 98 | std::cout << "Reading\n"; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 99 | status = read(video_fd, reinterpret_cast<char *>(&image_info), |
| 100 | sizeof(image_info)); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 101 | std::cout << "Done reading\n"; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 102 | |
| 103 | if (status != 0) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 104 | std::cerr << "Read failed with status " << status << "\n"; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | raw.buffer.resize(image_info.len); |
| 108 | |
| 109 | raw.height = image_info.parameter.features.h; |
| 110 | raw.width = image_info.parameter.features.w; |
| 111 | if (image_info.parameter.features.jpg_fmt == 422) { |
| 112 | raw.mode = YuvMode::YUV420; |
| 113 | } else { |
| 114 | raw.mode = YuvMode::YUV444; |
| 115 | } |
| 116 | return raw; |
| 117 | } |
| 118 | |
| 119 | private: |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 120 | int video_fd{}; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 121 | IMAGE_INFO image_info; |
| 122 | }; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 123 | |
| 124 | #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |
| 125 | class AsyncVideoPuller { |
| 126 | public: |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 127 | using video_callback = std::function<void (RawVideoBuffer &)>; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 128 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 129 | explicit AsyncVideoPuller(boost::asio::io_service &io_service) |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 130 | : image_info(), dev_video(io_service, open("/dev/video", O_RDWR)) { |
| 131 | videobuf = std::make_shared<RawVideoBuffer>(); |
| 132 | |
| 133 | image_info.do_image_refresh = 1; // full frame refresh |
| 134 | image_info.qc_valid = 0; // quick cursor disabled |
| 135 | image_info.parameter.features.buf = |
| 136 | reinterpret_cast<unsigned char *>(videobuf->buffer.data()); |
| 137 | image_info.crypttype = -1; |
| 138 | }; |
| 139 | |
| 140 | void register_callback(video_callback &callback) { |
| 141 | std::lock_guard<std::mutex> lock(callback_mutex); |
| 142 | callbacks.push_back(callback); |
| 143 | start_read(); |
| 144 | } |
| 145 | |
| 146 | void start_read() { |
| 147 | auto mutable_buffer = boost::asio::buffer(&image_info, sizeof(image_info)); |
| 148 | boost::asio::async_read( |
| 149 | dev_video, mutable_buffer, [this](const boost::system::error_code &ec, |
| 150 | std::size_t bytes_transferred) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 151 | if (ec != nullptr) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 152 | std::cerr << "Read failed with status " << ec << "\n"; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 153 | } else { |
| 154 | this->read_done(); |
| 155 | } |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | void read_done() { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 160 | std::cout << "Done reading\n"; |
Ed Tanous | e2fc45a | 2017-04-26 09:19:10 -0700 | [diff] [blame] | 161 | videobuf->buffer.resize(image_info.len); |
| 162 | |
| 163 | videobuf->height = image_info.parameter.features.h; |
| 164 | videobuf->width = image_info.parameter.features.w; |
| 165 | if (image_info.parameter.features.jpg_fmt == 422) { |
| 166 | videobuf->mode = YuvMode::YUV420; |
| 167 | } else { |
| 168 | videobuf->mode = YuvMode::YUV444; |
| 169 | } |
| 170 | std::lock_guard<std::mutex> lock(callback_mutex); |
| 171 | for (auto &callback : callbacks) { |
| 172 | // TODO(ed) call callbacks async and double buffer frames |
| 173 | callback(*videobuf); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | std::shared_ptr<RawVideoBuffer> videobuf; |
| 179 | boost::asio::posix::stream_descriptor dev_video; |
| 180 | IMAGE_INFO image_info; |
| 181 | std::mutex callback_mutex; |
| 182 | std::vector<video_callback> callbacks; |
| 183 | }; |
| 184 | #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 185 | } // namespace AstVideo |