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